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_common_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 std::recursive_mutex g_instanceMutex;
35 static const int MAX_PTHREAD_NAME_LEN = 15; // pthread name max length
36 } // namespace
37
GetInstance()38 PrivacyWindowManagerClient& PrivacyWindowManagerClient::GetInstance()
39 {
40 static PrivacyWindowManagerClient* instance = nullptr;
41 if (instance == nullptr) {
42 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
43 if (instance == nullptr) {
44 PrivacyWindowManagerClient* tmp = new PrivacyWindowManagerClient();
45 instance = std::move(tmp);
46 }
47 }
48 return *instance;
49 }
50
PrivacyWindowManagerClient()51 PrivacyWindowManagerClient::PrivacyWindowManagerClient() : deathCallback_(nullptr)
52 {
53 std::lock_guard<std::mutex> lock(proxyMutex_);
54 serviceDeathObserver_ = sptr<PrivacyWindowManagerDeathRecipient>::MakeSptr();
55 }
56
~PrivacyWindowManagerClient()57 PrivacyWindowManagerClient::~PrivacyWindowManagerClient()
58 {
59 LOGI(PRI_DOMAIN, PRI_TAG, "~PrivacyWindowManagerClient().");
60 std::lock_guard<std::mutex> lock(proxyMutex_);
61 RemoveDeathRecipient();
62 }
63
RegisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)64 int32_t PrivacyWindowManagerClient::RegisterWindowManagerAgent(WindowManagerAgentType type,
65 const sptr<IWindowManagerAgent>& windowManagerAgent)
66 {
67 if (type == WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW) {
68 return RegisterWindowManagerAgentLite(type, windowManagerAgent);
69 }
70 auto proxy = GetProxy();
71 if (proxy == nullptr) {
72 LOGE(PRI_DOMAIN, PRI_TAG, "Proxy is null");
73 return ERR_SERVICE_ABNORMAL;
74 }
75 return proxy->RegisterWindowManagerAgent(type, windowManagerAgent);
76 }
77
UnregisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)78 int32_t PrivacyWindowManagerClient::UnregisterWindowManagerAgent(WindowManagerAgentType type,
79 const sptr<IWindowManagerAgent>& windowManagerAgent)
80 {
81 if (type == WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW) {
82 return UnregisterWindowManagerAgentLite(type, windowManagerAgent);
83 }
84 auto proxy = GetProxy();
85 if (proxy == nullptr) {
86 LOGE(PRI_DOMAIN, PRI_TAG, "Proxy is null");
87 return ERR_SERVICE_ABNORMAL;
88 }
89 return proxy->UnregisterWindowManagerAgent(type, windowManagerAgent);
90 }
91
RegisterWindowManagerAgentLite(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)92 int32_t PrivacyWindowManagerClient::RegisterWindowManagerAgentLite(WindowManagerAgentType type,
93 const sptr<IWindowManagerAgent>& windowManagerAgent)
94 {
95 auto proxy = GetLiteProxy();
96 if (proxy == nullptr) {
97 LOGE(PRI_DOMAIN, PRI_TAG, "Proxy is null");
98 return ERR_SERVICE_ABNORMAL;
99 }
100 return proxy->RegisterWindowManagerAgent(type, windowManagerAgent);
101 }
102
UnregisterWindowManagerAgentLite(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)103 int32_t PrivacyWindowManagerClient::UnregisterWindowManagerAgentLite(WindowManagerAgentType type,
104 const sptr<IWindowManagerAgent>& windowManagerAgent)
105 {
106 auto proxy = GetLiteProxy();
107 if (proxy == nullptr) {
108 LOGE(PRI_DOMAIN, PRI_TAG, "Proxy is null");
109 return ERR_SERVICE_ABNORMAL;
110 }
111 return proxy->UnregisterWindowManagerAgent(type, windowManagerAgent);
112 }
113
AddDeathCallback(void (* callback)())114 void PrivacyWindowManagerClient::AddDeathCallback(void (*callback)())
115 {
116 std::lock_guard<std::mutex> lock(deathMutex_);
117 deathCallback_ = callback;
118 }
119
InitSessionManagerServiceProxy()120 void PrivacyWindowManagerClient::InitSessionManagerServiceProxy()
121 {
122 if (sessionManagerServiceProxy_ && sessionManagerServiceProxy_->AsObject() != nullptr &&
123 (!sessionManagerServiceProxy_->AsObject()->IsObjectDead())) {
124 return;
125 }
126 sptr<ISystemAbilityManager> systemAbilityManager =
127 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
128 if (!systemAbilityManager) {
129 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to get system ability mgr.");
130 return;
131 }
132 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
133 if (!remoteObject) {
134 LOGE(PRI_DOMAIN, PRI_TAG, "Remote object is nullptr");
135 return;
136 }
137 mockSessionManagerServiceProxy_ = new PrivacyMockSessionManagerProxy(remoteObject);
138 if (!mockSessionManagerServiceProxy_ || mockSessionManagerServiceProxy_->AsObject() == nullptr ||
139 mockSessionManagerServiceProxy_->AsObject()->IsObjectDead()) {
140 LOGW(PRI_DOMAIN, PRI_TAG, "Get mock session manager service proxy failed, nullptr");
141 return;
142 }
143 sptr<IRemoteObject> remoteObject2 = mockSessionManagerServiceProxy_->GetSessionManagerService();
144 if (!remoteObject2) {
145 LOGE(PRI_DOMAIN, PRI_TAG, "Remote object2 is nullptr");
146 return;
147 }
148 sessionManagerServiceProxy_ = new PrivacySessionManagerProxy(remoteObject2);
149 if (!sessionManagerServiceProxy_ || sessionManagerServiceProxy_->AsObject() == nullptr ||
150 sessionManagerServiceProxy_->AsObject()->IsObjectDead()) {
151 LOGE(PRI_DOMAIN, PRI_TAG, "SessionManagerServiceProxy_ is nullptr");
152 }
153 }
154
InitSceneSessionManagerProxy()155 void PrivacyWindowManagerClient::InitSceneSessionManagerProxy()
156 {
157 if (sceneSessionManagerProxy_ && sceneSessionManagerProxy_->AsObject() != nullptr &&
158 (!sceneSessionManagerProxy_->AsObject()->IsObjectDead())) {
159 return;
160 }
161 if (!sessionManagerServiceProxy_ || sessionManagerServiceProxy_->AsObject() == nullptr ||
162 sessionManagerServiceProxy_->AsObject()->IsObjectDead()) {
163 LOGE(PRI_DOMAIN, PRI_TAG, "SessionManagerServiceProxy_ is nullptr");
164 return;
165 }
166
167 sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy_->GetSceneSessionManager();
168 if (!remoteObject) {
169 LOGW(PRI_DOMAIN, PRI_TAG, "Get scene session manager proxy failed, scene session manager service is null");
170 return;
171 }
172 sceneSessionManagerProxy_ = new PrivacySceneSessionManagerProxy(remoteObject);
173 if (sceneSessionManagerProxy_ == nullptr || sceneSessionManagerProxy_->AsObject() == nullptr ||
174 sceneSessionManagerProxy_->AsObject()->IsObjectDead()) {
175 LOGW(PRI_DOMAIN, PRI_TAG, "SceneSessionManagerProxy_ is null.");
176 return;
177 }
178 if (!serviceDeathObserver_) {
179 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to create death Recipient ptr WMSDeathRecipient");
180 return;
181 }
182 if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(serviceDeathObserver_)) {
183 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to add death recipient");
184 return;
185 }
186 LOGI(PRI_DOMAIN, PRI_TAG, "InitSceneSessionManagerProxy end.");
187 }
188
InitSceneSessionManagerLiteProxy()189 void PrivacyWindowManagerClient::InitSceneSessionManagerLiteProxy()
190 {
191 if (sceneSessionManagerLiteProxy_ && sceneSessionManagerLiteProxy_->AsObject() != nullptr &&
192 (!sceneSessionManagerLiteProxy_->AsObject()->IsObjectDead())) {
193 return;
194 }
195 if (!sessionManagerServiceProxy_) {
196 LOGE(PRI_DOMAIN, PRI_TAG, "SessionManagerServiceProxy_ is nullptr");
197 return;
198 }
199
200 sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy_->GetSceneSessionManagerLite();
201 if (!remoteObject) {
202 LOGW(PRI_DOMAIN, PRI_TAG, "Get scene session manager proxy failed, scene session manager service is null");
203 return;
204 }
205 sceneSessionManagerLiteProxy_ = new PrivacySceneSessionManagerLiteProxy(remoteObject);
206 if (sceneSessionManagerLiteProxy_ == nullptr || sceneSessionManagerLiteProxy_->AsObject() == nullptr ||
207 sceneSessionManagerLiteProxy_->AsObject()->IsObjectDead()) {
208 LOGW(PRI_DOMAIN, PRI_TAG, "SceneSessionManagerLiteProxy_ is null.");
209 return;
210 }
211 if (!serviceDeathObserver_) {
212 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to create death Recipient ptr WMSDeathRecipient");
213 return;
214 }
215 if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(serviceDeathObserver_)) {
216 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to add death recipient");
217 return;
218 }
219 LOGI(PRI_DOMAIN, PRI_TAG, "InitSceneSessionManagerLiteProxy end.");
220 }
221
GetSSMProxy()222 sptr<ISceneSessionManager> PrivacyWindowManagerClient::GetSSMProxy()
223 {
224 std::lock_guard<std::mutex> lock(proxyMutex_);
225 InitSessionManagerServiceProxy();
226 InitSceneSessionManagerProxy();
227 return sceneSessionManagerProxy_;
228 }
229
GetSSMLiteProxy()230 sptr<ISceneSessionManagerLite> PrivacyWindowManagerClient::GetSSMLiteProxy()
231 {
232 std::lock_guard<std::mutex> lock(proxyMutex_);
233 InitSessionManagerServiceProxy();
234 InitSceneSessionManagerLiteProxy();
235 return sceneSessionManagerLiteProxy_;
236 }
237
InitWMSProxy()238 void PrivacyWindowManagerClient::InitWMSProxy()
239 {
240 if (wmsProxy_ && wmsProxy_->AsObject() != nullptr && (!wmsProxy_->AsObject()->IsObjectDead())) {
241 return;
242 }
243 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
244 if (sam == nullptr) {
245 LOGE(PRI_DOMAIN, PRI_TAG, "GetSystemAbilityManager is null");
246 return;
247 }
248 auto windowManagerSa = sam->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
249 if (windowManagerSa == nullptr) {
250 LOGE(PRI_DOMAIN, PRI_TAG, "GetSystemAbility %{public}d is null",
251 WINDOW_MANAGER_SERVICE_ID);
252 return;
253 }
254
255 if (serviceDeathObserver_ != nullptr) {
256 windowManagerSa->AddDeathRecipient(serviceDeathObserver_);
257 }
258
259 wmsProxy_ = new PrivacyWindowManagerProxy(windowManagerSa);
260 if (wmsProxy_ == nullptr || wmsProxy_->AsObject() == nullptr || wmsProxy_->AsObject()->IsObjectDead()) {
261 LOGE(PRI_DOMAIN, PRI_TAG, "WmsProxy_ is null.");
262 return;
263 }
264 LOGI(PRI_DOMAIN, PRI_TAG, "InitWMSProxy end.");
265 }
266
GetWMSProxy()267 sptr<IWindowManager> PrivacyWindowManagerClient::GetWMSProxy()
268 {
269 std::lock_guard<std::mutex> lock(proxyMutex_);
270 InitWMSProxy();
271 return wmsProxy_;
272 }
273
OnRemoteDiedHandle()274 void PrivacyWindowManagerClient::OnRemoteDiedHandle()
275 {
276 std::lock_guard<std::mutex> lock(proxyMutex_);
277 LOGI(PRI_DOMAIN, PRI_TAG, "Window manager remote died.");
278 RemoveDeathRecipient();
279
280 std::function<void()> runner = [this]() {
281 std::string name = "WindowMgrDiedHandler";
282 pthread_setname_np(pthread_self(), name.substr(0, MAX_PTHREAD_NAME_LEN).c_str());
283 auto sleepTime = std::chrono::milliseconds(1000);
284 std::this_thread::sleep_for(sleepTime);
285 std::lock_guard<std::mutex> lock(deathMutex_);
286 if (this->deathCallback_) {
287 this->deathCallback_();
288 }
289 };
290
291 std::thread initThread(runner);
292 initThread.detach();
293 }
294
GetLiteProxy()295 sptr<IWindowManagerLite> PrivacyWindowManagerClient::GetLiteProxy()
296 {
297 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
298 return nullptr;
299 }
300 return GetSSMLiteProxy();
301 }
302
GetProxy()303 sptr<IWindowManager> PrivacyWindowManagerClient::GetProxy()
304 {
305 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
306 return GetSSMProxy();
307 }
308 return GetWMSProxy();
309 }
310
RemoveDeathRecipient()311 void PrivacyWindowManagerClient::RemoveDeathRecipient()
312 {
313 if (serviceDeathObserver_ == nullptr) {
314 return;
315 }
316 // remove SceneSessionManager
317 if (sceneSessionManagerProxy_ != nullptr) {
318 sceneSessionManagerProxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
319 }
320 //remove SceneSessionManagerLite
321 if (sceneSessionManagerLiteProxy_ != nullptr) {
322 sceneSessionManagerLiteProxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
323 }
324 // remove WMSProxy
325 if (wmsProxy_ != nullptr) {
326 wmsProxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
327 }
328 mockSessionManagerServiceProxy_ = nullptr;
329 sessionManagerServiceProxy_ = nullptr;
330 sceneSessionManagerProxy_ = nullptr;
331 sceneSessionManagerLiteProxy_ = nullptr;
332 wmsProxy_ = nullptr;
333 }
334 } // namespace AccessToken
335 } // namespace Security
336 } // namespace OHOS
337
338