• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_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, "AppManagerAccessClient"
28 };
29 std::recursive_mutex g_instanceMutex;
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             instance = new AppManagerAccessClient();
39         }
40     }
41     return *instance;
42 }
43 
AppManagerAccessClient()44 AppManagerAccessClient::AppManagerAccessClient()
45 {}
46 
~AppManagerAccessClient()47 AppManagerAccessClient::~AppManagerAccessClient()
48 {
49     std::lock_guard<std::mutex> lock(proxyMutex_);
50     ReleaseProxy();
51 }
52 
KillProcessesByAccessTokenId(const uint32_t accessTokenId)53 int32_t AppManagerAccessClient::KillProcessesByAccessTokenId(const uint32_t accessTokenId)
54 {
55     auto proxy = GetProxy();
56     if (proxy == nullptr) {
57         ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
58         return -1;
59     }
60     sptr<IAmsMgr> amsService = proxy->GetAmsMgr();
61     if (amsService == nullptr) {
62         ACCESSTOKEN_LOG_ERROR(LABEL, "AmsService is null.");
63     }
64     return amsService->KillProcessesByAccessTokenId(accessTokenId);
65 }
66 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)67 int32_t AppManagerAccessClient::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver>& observer)
68 {
69     ACCESSTOKEN_LOG_INFO(LABEL, "Entry");
70     if (observer == nullptr) {
71         ACCESSTOKEN_LOG_ERROR(LABEL, "Callback is nullptr.");
72         return -1;
73     }
74     auto proxy = GetProxy();
75     if (proxy == nullptr) {
76         ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
77         return -1;
78     }
79     std::vector<std::string> bundleNameList;
80     return proxy->RegisterApplicationStateObserver(observer, bundleNameList);
81 }
82 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)83 int32_t AppManagerAccessClient::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
84 {
85     if (observer == nullptr) {
86         ACCESSTOKEN_LOG_ERROR(LABEL, "Callback is nullptr.");
87         return -1;
88     }
89     auto proxy = GetProxy();
90     if (proxy == nullptr) {
91         ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
92         return -1;
93     }
94     return proxy->UnregisterApplicationStateObserver(observer);
95 }
96 
GetForegroundApplications(std::vector<AppStateData> & list)97 int32_t AppManagerAccessClient::GetForegroundApplications(std::vector<AppStateData>& list)
98 {
99     auto proxy = GetProxy();
100     if (proxy == nullptr) {
101         ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
102         return -1;
103     }
104     return proxy->GetForegroundApplications(list);
105 }
106 
InitProxy()107 void AppManagerAccessClient::InitProxy()
108 {
109     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
110     if (sam == nullptr) {
111         ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
112         return;
113     }
114     auto appManagerSa = sam->GetSystemAbility(APP_MGR_SERVICE_ID);
115     if (appManagerSa == nullptr) {
116         ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
117             APP_MGR_SERVICE_ID);
118         return;
119     }
120 
121     serviceDeathObserver_ = sptr<AppMgrDeathRecipient>::MakeSptr();
122     if (serviceDeathObserver_ != nullptr) {
123         appManagerSa->AddDeathRecipient(serviceDeathObserver_);
124     }
125 
126     proxy_ = new AppManagerAccessProxy(appManagerSa);
127     if (proxy_ == nullptr) {
128         ACCESSTOKEN_LOG_ERROR(LABEL, "Iface_cast get null");
129     }
130 }
131 
RegisterDeathCallback(const std::shared_ptr<AppManagerDeathCallback> & callback)132 void AppManagerAccessClient::RegisterDeathCallback(const std::shared_ptr<AppManagerDeathCallback>& callback)
133 {
134     std::lock_guard<std::mutex> lock(deathCallbackMutex_);
135     if (callback == nullptr) {
136         ACCESSTOKEN_LOG_ERROR(LABEL, "AppManagerAccessClient: Callback is nullptr.");
137         return;
138     }
139     appManagerDeathCallbackList_.emplace_back(callback);
140 }
141 
OnRemoteDiedHandle()142 void AppManagerAccessClient::OnRemoteDiedHandle()
143 {
144     std::vector<std::shared_ptr<AppManagerDeathCallback>> tmpCallbackList;
145     {
146         std::lock_guard<std::mutex> lock(deathCallbackMutex_);
147         tmpCallbackList.assign(appManagerDeathCallbackList_.begin(), appManagerDeathCallbackList_.end());
148     }
149 
150     for (size_t i = 0; i < tmpCallbackList.size(); i++) {
151         tmpCallbackList[i]->NotifyAppManagerDeath();
152     }
153     {
154         std::lock_guard<std::mutex> lock(proxyMutex_);
155         ReleaseProxy();
156     }
157 }
158 
GetProxy()159 sptr<IAppMgr> AppManagerAccessClient::GetProxy()
160 {
161     std::lock_guard<std::mutex> lock(proxyMutex_);
162     if (proxy_ == nullptr) {
163         InitProxy();
164     }
165     return proxy_;
166 }
167 
ReleaseProxy()168 void AppManagerAccessClient::ReleaseProxy()
169 {
170     if (proxy_ != nullptr && serviceDeathObserver_ != nullptr) {
171         proxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
172     }
173     proxy_ = nullptr;
174     serviceDeathObserver_ = nullptr;
175 }
176 } // namespace AccessToken
177 } // namespace Security
178 } // namespace OHOS
179 
180