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 } // namespace
30
GetInstance()31 AppManagerAccessClient& AppManagerAccessClient::GetInstance()
32 {
33 static AppManagerAccessClient instance;
34 return instance;
35 }
36
AppManagerAccessClient()37 AppManagerAccessClient::AppManagerAccessClient()
38 {}
39
~AppManagerAccessClient()40 AppManagerAccessClient::~AppManagerAccessClient()
41 {}
42
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)43 int32_t AppManagerAccessClient::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver>& observer)
44 {
45 ACCESSTOKEN_LOG_INFO(LABEL, "Entry");
46 if (observer == nullptr) {
47 ACCESSTOKEN_LOG_ERROR(LABEL, "AudioPolicyManager: callback is nullptr");
48 return -1;
49 }
50 auto proxy = GetProxy();
51 if (proxy == nullptr) {
52 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
53 return -1;
54 }
55 std::vector<std::string> bundleNameList;
56 return proxy->RegisterApplicationStateObserver(observer, bundleNameList);
57 }
58
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)59 int32_t AppManagerAccessClient::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
60 {
61 if (observer == nullptr) {
62 ACCESSTOKEN_LOG_ERROR(LABEL, "AudioPolicyManager: callback is nullptr");
63 return -1;
64 }
65 auto proxy = GetProxy();
66 if (proxy == nullptr) {
67 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
68 return -1;
69 }
70 return proxy->UnregisterApplicationStateObserver(observer);
71 }
72
GetForegroundApplications(std::vector<AppStateData> & list)73 int32_t AppManagerAccessClient::GetForegroundApplications(std::vector<AppStateData>& list)
74 {
75 auto proxy = GetProxy();
76 if (proxy == nullptr) {
77 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
78 return -1;
79 }
80 return proxy->GetForegroundApplications(list);
81 }
82
InitProxy()83 void AppManagerAccessClient::InitProxy()
84 {
85 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
86 if (sam == nullptr) {
87 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
88 return;
89 }
90 auto appManagerSa = sam->GetSystemAbility(APP_MGR_SERVICE_ID);
91 if (appManagerSa == nullptr) {
92 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
93 APP_MGR_SERVICE_ID);
94 return;
95 }
96
97 serviceDeathObserver_ = new (std::nothrow) AppMgrDeathRecipient();
98 if (serviceDeathObserver_ != nullptr) {
99 appManagerSa->AddDeathRecipient(serviceDeathObserver_);
100 }
101
102 proxy_ = iface_cast<IAppMgr>(appManagerSa);
103 if (proxy_ == nullptr) {
104 ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null");
105 }
106 }
107
RegisterDeathCallbak(const std::shared_ptr<AppManagerDeathCallback> & callback)108 void AppManagerAccessClient::RegisterDeathCallbak(const std::shared_ptr<AppManagerDeathCallback>& callback)
109 {
110 std::lock_guard<std::mutex> lock(deathCallbackMutex_);
111 if (callback == nullptr) {
112 ACCESSTOKEN_LOG_ERROR(LABEL, "AppManagerAccessClient: callback is nullptr");
113 return;
114 }
115 appManagerDeathCallbackList_.emplace_back(callback);
116 }
117
OnRemoteDiedHandle()118 void AppManagerAccessClient::OnRemoteDiedHandle()
119 {
120 std::vector<std::shared_ptr<AppManagerDeathCallback>> tmpCallbackList;
121 {
122 std::lock_guard<std::mutex> lock(deathCallbackMutex_);
123 tmpCallbackList.assign(appManagerDeathCallbackList_.begin(), appManagerDeathCallbackList_.end());
124 }
125
126 for (size_t i = 0; i < tmpCallbackList.size(); i++) {
127 tmpCallbackList[i]->NotifyAppManagerDeath();
128 }
129 std::lock_guard<std::mutex> lock(proxyMutex_);
130 proxy_ = nullptr;
131 }
132
133 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
134 PrivacySecCompEnhanceAgent::GetInstance().OnAppMgrRemoteDiedHandle();
135 #endif
136
GetProxy()137 sptr<IAppMgr> AppManagerAccessClient::GetProxy()
138 {
139 std::lock_guard<std::mutex> lock(proxyMutex_);
140 if (proxy_ == nullptr) {
141 InitProxy();
142 }
143 return proxy_;
144 }
145 } // namespace AccessToken
146 } // namespace Security
147 } // namespace OHOS
148
149