1 /*
2 * Copyright (c) 2022 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 "window_manager_privacy_client.h"
16
17 #include "accesstoken_log.h"
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "WindowManagerPrivacyClient"
27 };
28 } // namespace
29
GetInstance()30 WindowManagerPrivacyClient& WindowManagerPrivacyClient::GetInstance()
31 {
32 static WindowManagerPrivacyClient instance;
33 return instance;
34 }
35
WindowManagerPrivacyClient()36 WindowManagerPrivacyClient::WindowManagerPrivacyClient()
37 {}
38
~WindowManagerPrivacyClient()39 WindowManagerPrivacyClient::~WindowManagerPrivacyClient()
40 {}
41
RegisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)42 bool WindowManagerPrivacyClient::RegisterWindowManagerAgent(WindowManagerAgentType type,
43 const sptr<IWindowManagerAgent>& windowManagerAgent)
44 {
45 auto proxy = GetProxy();
46 if (proxy == nullptr) {
47 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
48 return false;
49 }
50 return proxy->RegisterWindowManagerAgent(type, windowManagerAgent);
51 }
52
UnregisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)53 bool WindowManagerPrivacyClient::UnregisterWindowManagerAgent(WindowManagerAgentType type,
54 const sptr<IWindowManagerAgent>& windowManagerAgent)
55 {
56 auto proxy = GetProxy();
57 if (proxy == nullptr) {
58 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
59 return false;
60 }
61 return proxy->UnregisterWindowManagerAgent(type, windowManagerAgent);
62 }
63
InitProxy()64 void WindowManagerPrivacyClient::InitProxy()
65 {
66 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
67 if (sam == nullptr) {
68 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
69 return;
70 }
71 auto windowManagerSa = sam->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
72 if (windowManagerSa == nullptr) {
73 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
74 WINDOW_MANAGER_SERVICE_ID);
75 return;
76 }
77
78 serviceDeathObserver_ = new (std::nothrow) WindowManagerPrivacyDeathRecipient();
79 if (serviceDeathObserver_ != nullptr) {
80 windowManagerSa->AddDeathRecipient(serviceDeathObserver_);
81 }
82
83 proxy_ = iface_cast<IWindowManager>(windowManagerSa);
84 if (proxy_ == nullptr) {
85 ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null");
86 }
87 }
88
OnRemoteDiedHandle()89 void WindowManagerPrivacyClient::OnRemoteDiedHandle()
90 {
91 std::lock_guard<std::mutex> lock(proxyMutex_);
92 proxy_ = nullptr;
93 }
94
GetProxy()95 sptr<IWindowManager> WindowManagerPrivacyClient::GetProxy()
96 {
97 std::lock_guard<std::mutex> lock(proxyMutex_);
98 if (proxy_ == nullptr) {
99 InitProxy();
100 }
101 return proxy_;
102 }
103 } // namespace AccessToken
104 } // namespace Security
105 } // namespace OHOS
106
107