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 "form_manager_access_client.h"
16 #include <unistd.h>
17
18 #include "accesstoken_log.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
27 LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "FormManagerAccessClient"
28 };
29 std::recursive_mutex g_instanceMutex;
30 } // namespace
31
GetInstance()32 FormManagerAccessClient& FormManagerAccessClient::GetInstance()
33 {
34 static FormManagerAccessClient* instance = nullptr;
35 if (instance == nullptr) {
36 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
37 if (instance == nullptr) {
38 instance = new FormManagerAccessClient();
39 }
40 }
41 return *instance;
42 }
43
FormManagerAccessClient()44 FormManagerAccessClient::FormManagerAccessClient()
45 {}
46
~FormManagerAccessClient()47 FormManagerAccessClient::~FormManagerAccessClient()
48 {
49 std::lock_guard<std::mutex> lock(proxyMutex_);
50 ReleaseProxy();
51 }
52
RegisterAddObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)53 int32_t FormManagerAccessClient::RegisterAddObserver(
54 const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
55 {
56 if (callerToken == nullptr) {
57 ACCESSTOKEN_LOG_ERROR(LABEL, "Callback is nullptr.");
58 return -1;
59 }
60 auto proxy = GetProxy();
61 if (proxy == nullptr) {
62 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
63 return -1;
64 }
65 return proxy->RegisterAddObserver(bundleName, callerToken);
66 }
67
RegisterRemoveObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)68 int32_t FormManagerAccessClient::RegisterRemoveObserver(
69 const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
70 {
71 if (callerToken == nullptr) {
72 ACCESSTOKEN_LOG_ERROR(LABEL, "Callback is nullptr.");
73 return -1;
74 }
75 auto proxy = GetProxy();
76 if (proxy == nullptr) {
77 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
78 return -1;
79 }
80 return proxy->RegisterRemoveObserver(bundleName, callerToken);
81 }
82
HasFormVisible(const uint32_t tokenId)83 bool FormManagerAccessClient::HasFormVisible(const uint32_t tokenId)
84 {
85 auto proxy = GetProxy();
86 if (proxy == nullptr) {
87 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
88 return false;
89 }
90 return proxy->HasFormVisible(tokenId);
91 }
92
InitProxy()93 void FormManagerAccessClient::InitProxy()
94 {
95 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
96 if (sam == nullptr) {
97 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null.");
98 return;
99 }
100 auto formManagerSa = sam->GetSystemAbility(FORM_MGR_SERVICE_ID);
101 if (formManagerSa == nullptr) {
102 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null.",
103 APP_MGR_SERVICE_ID);
104 return;
105 }
106
107 serviceDeathObserver_ = new (std::nothrow) FormMgrDeathRecipient();
108 if (serviceDeathObserver_ != nullptr) {
109 formManagerSa->AddDeathRecipient(serviceDeathObserver_);
110 }
111
112 proxy_ = new FormManagerAccessProxy(formManagerSa);
113 if (proxy_ == nullptr) {
114 ACCESSTOKEN_LOG_ERROR(LABEL, "Iface_cast get null.");
115 }
116 }
117
OnRemoteDiedHandle()118 void FormManagerAccessClient::OnRemoteDiedHandle()
119 {
120 std::lock_guard<std::mutex> lock(proxyMutex_);
121 ReleaseProxy();
122 }
123
GetProxy()124 sptr<IFormMgr> FormManagerAccessClient::GetProxy()
125 {
126 std::lock_guard<std::mutex> lock(proxyMutex_);
127 if (proxy_ == nullptr) {
128 InitProxy();
129 }
130 return proxy_;
131 }
132
ReleaseProxy()133 void FormManagerAccessClient::ReleaseProxy()
134 {
135 if (proxy_ != nullptr && serviceDeathObserver_ != nullptr) {
136 proxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
137 }
138 proxy_ = nullptr;
139 serviceDeathObserver_ = nullptr;
140 }
141 } // namespace AccessToken
142 } // namespace Security
143 } // namespace OHOS
144