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