1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "privacy_window_manager_client.h"
16
17 #include <thread>
18 #include "accesstoken_log.h"
19 #include "iservice_registry.h"
20 #include "privacy_error.h"
21
22 #include "privacy_mock_session_manager_proxy.h"
23 #include "privacy_scene_session_manager_proxy.h"
24 #include "privacy_scene_session_manager_lite_proxy.h"
25 #include "privacy_session_manager_proxy.h"
26 #include "privacy_window_manager_proxy.h"
27 #include "scene_board_judgement.h"
28 #include "system_ability_definition.h"
29
30 namespace OHOS {
31 namespace Security {
32 namespace AccessToken {
33 namespace {
34 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
35 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PrivacyWindowManagerClient"
36 };
37 std::recursive_mutex g_instanceMutex;
38 static const int MAX_PTHREAD_NAME_LEN = 15; // pthread name max length
39 } // namespace
40
GetInstance()41 PrivacyWindowManagerClient& PrivacyWindowManagerClient::GetInstance()
42 {
43 static PrivacyWindowManagerClient* instance = nullptr;
44 if (instance == nullptr) {
45 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
46 if (instance == nullptr) {
47 instance = new PrivacyWindowManagerClient();
48 }
49 }
50 return *instance;
51 }
52
PrivacyWindowManagerClient()53 PrivacyWindowManagerClient::PrivacyWindowManagerClient() : deathCallback_(nullptr)
54 {
55 std::lock_guard<std::mutex> lock(proxyMutex_);
56 serviceDeathObserver_ = sptr<PrivacyWindowManagerDeathRecipient>::MakeSptr();
57 }
58
~PrivacyWindowManagerClient()59 PrivacyWindowManagerClient::~PrivacyWindowManagerClient()
60 {
61 ACCESSTOKEN_LOG_INFO(LABEL, "~PrivacyWindowManagerClient().");
62 std::lock_guard<std::mutex> lock(proxyMutex_);
63 RemoveDeathRecipient();
64 }
65
RegisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)66 int32_t PrivacyWindowManagerClient::RegisterWindowManagerAgent(WindowManagerAgentType type,
67 const sptr<IWindowManagerAgent>& windowManagerAgent)
68 {
69 if (type == WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW) {
70 return RegisterWindowManagerAgentLite(type, windowManagerAgent);
71 }
72 auto proxy = GetProxy();
73 if (proxy == nullptr) {
74 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
75 return ERR_SERVICE_ABNORMAL;
76 }
77 return proxy->RegisterWindowManagerAgent(type, windowManagerAgent);
78 }
79
UnregisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)80 int32_t PrivacyWindowManagerClient::UnregisterWindowManagerAgent(WindowManagerAgentType type,
81 const sptr<IWindowManagerAgent>& windowManagerAgent)
82 {
83 if (type == WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW) {
84 return UnregisterWindowManagerAgentLite(type, windowManagerAgent);
85 }
86 auto proxy = GetProxy();
87 if (proxy == nullptr) {
88 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
89 return ERR_SERVICE_ABNORMAL;
90 }
91 return proxy->UnregisterWindowManagerAgent(type, windowManagerAgent);
92 }
93
RegisterWindowManagerAgentLite(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)94 int32_t PrivacyWindowManagerClient::RegisterWindowManagerAgentLite(WindowManagerAgentType type,
95 const sptr<IWindowManagerAgent>& windowManagerAgent)
96 {
97 auto proxy = GetLiteProxy();
98 if (proxy == nullptr) {
99 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
100 return ERR_SERVICE_ABNORMAL;
101 }
102 return proxy->RegisterWindowManagerAgent(type, windowManagerAgent);
103 }
104
UnregisterWindowManagerAgentLite(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)105 int32_t PrivacyWindowManagerClient::UnregisterWindowManagerAgentLite(WindowManagerAgentType type,
106 const sptr<IWindowManagerAgent>& windowManagerAgent)
107 {
108 auto proxy = GetLiteProxy();
109 if (proxy == nullptr) {
110 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
111 return ERR_SERVICE_ABNORMAL;
112 }
113 return proxy->UnregisterWindowManagerAgent(type, windowManagerAgent);
114 }
115
AddDeathCallback(void (* callback)())116 void PrivacyWindowManagerClient::AddDeathCallback(void (*callback)())
117 {
118 std::lock_guard<std::mutex> lock(deathMutex_);
119 deathCallback_ = callback;
120 }
121
InitSessionManagerServiceProxy()122 void PrivacyWindowManagerClient::InitSessionManagerServiceProxy()
123 {
124 if (sessionManagerServiceProxy_) {
125 return;
126 }
127 sptr<ISystemAbilityManager> systemAbilityManager =
128 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
129 if (!systemAbilityManager) {
130 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to get system ability mgr.");
131 return;
132 }
133 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
134 if (!remoteObject) {
135 ACCESSTOKEN_LOG_ERROR(LABEL, "Remote object is nullptr");
136 return;
137 }
138 mockSessionManagerServiceProxy_ = new PrivacyMockSessionManagerProxy(remoteObject);
139 if (!mockSessionManagerServiceProxy_) {
140 ACCESSTOKEN_LOG_WARN(LABEL, "Get mock session manager service proxy failed, nullptr");
141 return;
142 }
143 sptr<IRemoteObject> remoteObject2 = mockSessionManagerServiceProxy_->GetSessionManagerService();
144 if (!remoteObject2) {
145 ACCESSTOKEN_LOG_ERROR(LABEL, "Remote object2 is nullptr");
146 return;
147 }
148 sessionManagerServiceProxy_ = new PrivacySessionManagerProxy(remoteObject2);
149 if (!sessionManagerServiceProxy_) {
150 ACCESSTOKEN_LOG_ERROR(LABEL, "SessionManagerServiceProxy_ is nullptr");
151 }
152 }
153
InitSceneSessionManagerProxy()154 void PrivacyWindowManagerClient::InitSceneSessionManagerProxy()
155 {
156 if (sceneSessionManagerProxy_) {
157 return;
158 }
159 if (!sessionManagerServiceProxy_) {
160 ACCESSTOKEN_LOG_ERROR(LABEL, "SessionManagerServiceProxy_ is nullptr");
161 return;
162 }
163
164 sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy_->GetSceneSessionManager();
165 if (!remoteObject) {
166 ACCESSTOKEN_LOG_WARN(LABEL, "Get scene session manager proxy failed, scene session manager service is null");
167 return;
168 }
169 sceneSessionManagerProxy_ = new PrivacySceneSessionManagerProxy(remoteObject);
170 if (sceneSessionManagerProxy_ == nullptr) {
171 ACCESSTOKEN_LOG_WARN(LABEL, "SceneSessionManagerProxy_ is null.");
172 return;
173 }
174 if (!serviceDeathObserver_) {
175 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to create death Recipient ptr WMSDeathRecipient");
176 return;
177 }
178 if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(serviceDeathObserver_)) {
179 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to add death recipient");
180 return;
181 }
182 ACCESSTOKEN_LOG_INFO(LABEL, "InitSceneSessionManagerProxy end.");
183 }
184
InitSceneSessionManagerLiteProxy()185 void PrivacyWindowManagerClient::InitSceneSessionManagerLiteProxy()
186 {
187 if (sceneSessionManagerLiteProxy_) {
188 return;
189 }
190 if (!sessionManagerServiceProxy_) {
191 ACCESSTOKEN_LOG_ERROR(LABEL, "SessionManagerServiceProxy_ is nullptr");
192 return;
193 }
194
195 sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy_->GetSceneSessionManagerLite();
196 if (!remoteObject) {
197 ACCESSTOKEN_LOG_WARN(LABEL, "Get scene session manager proxy failed, scene session manager service is null");
198 return;
199 }
200 sceneSessionManagerLiteProxy_ = new PrivacySceneSessionManagerLiteProxy(remoteObject);
201 if (sceneSessionManagerLiteProxy_ == nullptr) {
202 ACCESSTOKEN_LOG_WARN(LABEL, "SceneSessionManagerLiteProxy_ is null.");
203 return;
204 }
205 if (!serviceDeathObserver_) {
206 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to create death Recipient ptr WMSDeathRecipient");
207 return;
208 }
209 if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(serviceDeathObserver_)) {
210 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to add death recipient");
211 return;
212 }
213 ACCESSTOKEN_LOG_INFO(LABEL, "InitSceneSessionManagerLiteProxy end.");
214 }
215
GetSSMProxy()216 sptr<ISceneSessionManager> PrivacyWindowManagerClient::GetSSMProxy()
217 {
218 std::lock_guard<std::mutex> lock(proxyMutex_);
219 InitSessionManagerServiceProxy();
220 InitSceneSessionManagerProxy();
221 return sceneSessionManagerProxy_;
222 }
223
GetSSMLiteProxy()224 sptr<ISceneSessionManagerLite> PrivacyWindowManagerClient::GetSSMLiteProxy()
225 {
226 std::lock_guard<std::mutex> lock(proxyMutex_);
227 InitSessionManagerServiceProxy();
228 InitSceneSessionManagerLiteProxy();
229 return sceneSessionManagerLiteProxy_;
230 }
231
InitWMSProxy()232 void PrivacyWindowManagerClient::InitWMSProxy()
233 {
234 if (wmsProxy_) {
235 return;
236 }
237 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
238 if (sam == nullptr) {
239 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
240 return;
241 }
242 auto windowManagerSa = sam->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
243 if (windowManagerSa == nullptr) {
244 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
245 WINDOW_MANAGER_SERVICE_ID);
246 return;
247 }
248
249 if (serviceDeathObserver_ != nullptr) {
250 windowManagerSa->AddDeathRecipient(serviceDeathObserver_);
251 }
252
253 wmsProxy_ = new PrivacyWindowManagerProxy(windowManagerSa);
254 if (wmsProxy_ == nullptr) {
255 ACCESSTOKEN_LOG_ERROR(LABEL, "WmsProxy_ is null.");
256 return;
257 }
258 ACCESSTOKEN_LOG_INFO(LABEL, "InitWMSProxy end.");
259 }
260
GetWMSProxy()261 sptr<IWindowManager> PrivacyWindowManagerClient::GetWMSProxy()
262 {
263 std::lock_guard<std::mutex> lock(proxyMutex_);
264 InitWMSProxy();
265 return wmsProxy_;
266 }
267
OnRemoteDiedHandle()268 void PrivacyWindowManagerClient::OnRemoteDiedHandle()
269 {
270 std::lock_guard<std::mutex> lock(proxyMutex_);
271 ACCESSTOKEN_LOG_INFO(LABEL, "Window manager remote died.");
272 RemoveDeathRecipient();
273
274 std::function<void()> runner = [this]() {
275 std::string name = "WindowMgrDiedHandler";
276 pthread_setname_np(pthread_self(), name.substr(0, MAX_PTHREAD_NAME_LEN).c_str());
277 auto sleepTime = std::chrono::milliseconds(1000);
278 std::this_thread::sleep_for(sleepTime);
279 std::lock_guard<std::mutex> lock(deathMutex_);
280 if (this->deathCallback_) {
281 this->deathCallback_();
282 }
283 };
284
285 std::thread initThread(runner);
286 initThread.detach();
287 }
288
GetLiteProxy()289 sptr<IWindowManagerLite> PrivacyWindowManagerClient::GetLiteProxy()
290 {
291 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
292 return nullptr;
293 }
294 return GetSSMLiteProxy();
295 }
296
GetProxy()297 sptr<IWindowManager> PrivacyWindowManagerClient::GetProxy()
298 {
299 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
300 return GetSSMProxy();
301 }
302 return GetWMSProxy();
303 }
304
RemoveDeathRecipient()305 void PrivacyWindowManagerClient::RemoveDeathRecipient()
306 {
307 if (serviceDeathObserver_ == nullptr) {
308 return;
309 }
310 // remove SceneSessionManager
311 if (sceneSessionManagerProxy_ != nullptr) {
312 sceneSessionManagerProxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
313 }
314 //remove SceneSessionManagerLite
315 if (sceneSessionManagerLiteProxy_ != nullptr) {
316 sceneSessionManagerLiteProxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
317 }
318 // remove WMSProxy
319 if (wmsProxy_ != nullptr) {
320 wmsProxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
321 }
322 mockSessionManagerServiceProxy_ = nullptr;
323 sessionManagerServiceProxy_ = nullptr;
324 sceneSessionManagerProxy_ = nullptr;
325 sceneSessionManagerLiteProxy_ = nullptr;
326 wmsProxy_ = nullptr;
327 }
328 } // namespace AccessToken
329 } // namespace Security
330 } // namespace OHOS
331
332