• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "app_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 static constexpr int32_t ERROR = -1;
27 std::recursive_mutex g_instanceMutex;
28 std::u16string DESCRIPTOR = u"ohos.appexecfwk.AppMgr";
29 constexpr int32_t CYCLE_LIMIT = 1000;
30 } // namespace
31 
GetInstance()32 AppManagerAccessClient& AppManagerAccessClient::GetInstance()
33 {
34     static AppManagerAccessClient* instance = nullptr;
35     if (instance == nullptr) {
36         std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
37         if (instance == nullptr) {
38             AppManagerAccessClient* tmp = new AppManagerAccessClient();
39             instance = std::move(tmp);
40         }
41     }
42     return *instance;
43 }
44 
AppManagerAccessClient()45 AppManagerAccessClient::AppManagerAccessClient()
46 {}
47 
~AppManagerAccessClient()48 AppManagerAccessClient::~AppManagerAccessClient()
49 {
50     std::lock_guard<std::mutex> lock(proxyMutex_);
51     ReleaseProxy();
52 }
53 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)54 int32_t AppManagerAccessClient::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver>& observer)
55 {
56     LOGI(ATM_DOMAIN, ATM_TAG, "Entry");
57     if (observer == nullptr) {
58         LOGE(ATM_DOMAIN, ATM_TAG, "Callback is nullptr.");
59         return ERROR;
60     }
61     auto proxy = GetProxy();
62     if (proxy == nullptr) {
63         LOGE(ATM_DOMAIN, ATM_TAG, "Proxy is null.");
64         return ERROR;
65     }
66     std::vector<std::string> bundleNameList;
67 
68     MessageParcel data;
69     MessageParcel reply;
70     MessageOption option;
71     if (!data.WriteInterfaceToken(DESCRIPTOR)) {
72         LOGE(ATM_DOMAIN, ATM_TAG, "WriteInterfaceToken failed");
73         return ERROR;
74     }
75     if (!data.WriteRemoteObject(observer->AsObject())) {
76         LOGE(ATM_DOMAIN, ATM_TAG, "Observer write failed.");
77         return ERROR;
78     }
79     if (!data.WriteStringVector(bundleNameList)) {
80         LOGE(ATM_DOMAIN, ATM_TAG, "BundleNameList write failed.");
81         return ERROR;
82     }
83     int32_t error = proxy->SendRequest(
84         static_cast<uint32_t>(AppManagerAccessClient::Message::REGISTER_APPLICATION_STATE_OBSERVER),
85         data, reply, option);
86     if (error != ERR_NONE) {
87         LOGE(ATM_DOMAIN, ATM_TAG, "RegisterAppStatus failed, error: %{public}d", error);
88         return ERROR;
89     }
90     return reply.ReadInt32();
91 }
92 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)93 int32_t AppManagerAccessClient::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
94 {
95     if (observer == nullptr) {
96         LOGE(ATM_DOMAIN, ATM_TAG, "Callback is nullptr.");
97         return ERROR;
98     }
99     auto proxy = GetProxy();
100     if (proxy == nullptr) {
101         LOGE(ATM_DOMAIN, ATM_TAG, "Proxy is null");
102         return ERROR;
103     }
104 
105     MessageParcel data;
106     MessageParcel reply;
107     MessageOption option;
108     if (!data.WriteInterfaceToken(DESCRIPTOR)) {
109         LOGE(ATM_DOMAIN, ATM_TAG, "WriteInterfaceToken failed");
110         return ERROR;
111     }
112     if (!data.WriteRemoteObject(observer->AsObject())) {
113         LOGE(ATM_DOMAIN, ATM_TAG, "Observer write failed.");
114         return ERROR;
115     }
116     int32_t error = proxy->SendRequest(
117         static_cast<uint32_t>(AppManagerAccessClient::Message::UNREGISTER_APPLICATION_STATE_OBSERVER),
118         data, reply, option);
119     if (error != ERR_NONE) {
120         LOGE(ATM_DOMAIN, ATM_TAG, "Set microphoneMute failed, error: %d", error);
121         return error;
122     }
123     return reply.ReadInt32();
124 }
125 
GetForegroundApplications(std::vector<AppStateData> & list)126 int32_t AppManagerAccessClient::GetForegroundApplications(std::vector<AppStateData>& list)
127 {
128     auto proxy = GetProxy();
129     if (proxy == nullptr) {
130         LOGE(ATM_DOMAIN, ATM_TAG, "Proxy is null");
131         return ERROR;
132     }
133 
134     MessageParcel data;
135     MessageParcel reply;
136     MessageOption option;
137     if (!data.WriteInterfaceToken(DESCRIPTOR)) {
138         LOGE(ATM_DOMAIN, ATM_TAG, "WriteInterfaceToken failed");
139         return ERROR;
140     }
141     int32_t error = proxy->SendRequest(
142         static_cast<uint32_t>(AppManagerAccessClient::Message::GET_FOREGROUND_APPLICATIONS), data, reply, option);
143     if (error != ERR_NONE) {
144         LOGE(ATM_DOMAIN, ATM_TAG, "GetForegroundApplications failed, error: %{public}d", error);
145         return error;
146     }
147     uint32_t infoSize = reply.ReadUint32();
148     if (infoSize > CYCLE_LIMIT) {
149         LOGE(ATM_DOMAIN, ATM_TAG, "InfoSize is too large");
150         return ERROR;
151     }
152     for (uint32_t i = 0; i < infoSize; i++) {
153         std::unique_ptr<AppStateData> info(reply.ReadParcelable<AppStateData>());
154         if (info != nullptr) {
155             list.emplace_back(*info);
156         }
157     }
158     return reply.ReadInt32();
159 }
160 
InitProxy()161 void AppManagerAccessClient::InitProxy()
162 {
163     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
164     if (sam == nullptr) {
165         LOGE(ATM_DOMAIN, ATM_TAG, "GetSystemAbilityManager is null");
166         return;
167     }
168     auto appManagerSa = sam->GetSystemAbility(APP_MGR_SERVICE_ID);
169     if (appManagerSa == nullptr) {
170         LOGE(ATM_DOMAIN, ATM_TAG, "GetSystemAbility %{public}d is null",
171             APP_MGR_SERVICE_ID);
172         return;
173     }
174 
175     serviceDeathObserver_ = sptr<AppMgrDeathRecipient>::MakeSptr();
176     if (serviceDeathObserver_ != nullptr) {
177         appManagerSa->AddDeathRecipient(serviceDeathObserver_);
178     }
179 
180     proxy_ = appManagerSa;
181 }
182 
RegisterDeathCallback(const std::shared_ptr<AppManagerDeathCallback> & callback)183 void AppManagerAccessClient::RegisterDeathCallback(const std::shared_ptr<AppManagerDeathCallback>& callback)
184 {
185     std::lock_guard<std::mutex> lock(deathCallbackMutex_);
186     if (callback == nullptr) {
187         LOGE(ATM_DOMAIN, ATM_TAG, "AppManagerAccessClient: Callback is nullptr.");
188         return;
189     }
190     appManagerDeathCallbackList_.emplace_back(callback);
191 }
192 
OnRemoteDiedHandle()193 void AppManagerAccessClient::OnRemoteDiedHandle()
194 {
195     std::vector<std::shared_ptr<AppManagerDeathCallback>> tmpCallbackList;
196     {
197         std::lock_guard<std::mutex> lock(deathCallbackMutex_);
198         tmpCallbackList.assign(appManagerDeathCallbackList_.begin(), appManagerDeathCallbackList_.end());
199     }
200 
201     for (size_t i = 0; i < tmpCallbackList.size(); i++) {
202         if (tmpCallbackList[i] == nullptr) {
203             LOGE(ATM_DOMAIN, ATM_TAG, "AppManagerDeathCallback is null");
204             continue;
205         }
206         tmpCallbackList[i]->NotifyAppManagerDeath();
207     }
208     {
209         std::lock_guard<std::mutex> lock(proxyMutex_);
210         ReleaseProxy();
211     }
212 }
213 
GetProxy()214 sptr<IRemoteObject> AppManagerAccessClient::GetProxy()
215 {
216     std::lock_guard<std::mutex> lock(proxyMutex_);
217     if (proxy_ == nullptr || proxy_->IsObjectDead()) {
218         InitProxy();
219     }
220     return proxy_;
221 }
222 
ReleaseProxy()223 void AppManagerAccessClient::ReleaseProxy()
224 {
225     if (proxy_ != nullptr && serviceDeathObserver_ != nullptr) {
226         proxy_->RemoveDeathRecipient(serviceDeathObserver_);
227     }
228     proxy_ = nullptr;
229     serviceDeathObserver_ = nullptr;
230 }
231 
OnRemoteDied(const wptr<IRemoteObject> & object)232 void AppManagerAccessClient::AppMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
233 {
234     LOGI(ATM_DOMAIN, ATM_TAG, "%{public}s called", __func__);
235     AppManagerAccessClient::GetInstance().OnRemoteDiedHandle();
236 }
237 } // namespace AccessToken
238 } // namespace Security
239 } // namespace OHOS
240 
241