1 /*
2 * Copyright (c) 2021-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
16 #include "bundle_manager_helper.h"
17
18 #include "bundle_constants.h"
19 #include "bundle_mgr_client.h"
20 #include "event_log_wrapper.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "nlohmann/json.hpp"
24 #include "os_account_manager_helper.h"
25 #include "system_ability_definition.h"
26
27 namespace OHOS {
28 namespace EventFwk {
29 const std::string META_NAME_STATIC_SUBSCRIBER = "ohos.extension.staticSubscriber";
30
31 using namespace OHOS::AppExecFwk::Constants;
32
BundleManagerHelper()33 BundleManagerHelper::BundleManagerHelper() : sptrBundleMgr_(nullptr), bmsDeath_(nullptr)
34 {}
35
~BundleManagerHelper()36 BundleManagerHelper::~BundleManagerHelper()
37 {}
38
GetBundleName(uid_t uid)39 std::string BundleManagerHelper::GetBundleName(uid_t uid)
40 {
41 EVENT_LOGD("enter");
42
43 std::lock_guard<std::mutex> lock(mutex_);
44 std::string bundleName = "";
45
46 if (!GetBundleMgrProxy()) {
47 EVENT_LOGE("failed to get bms proxy");
48 return bundleName;
49 }
50 std::string identity = IPCSkeleton::ResetCallingIdentity();
51 sptrBundleMgr_->GetNameForUid(uid, bundleName);
52 IPCSkeleton::SetCallingIdentity(identity);
53 return bundleName;
54 }
55
QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos,const int32_t & userId)56 bool BundleManagerHelper::QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos,
57 const int32_t &userId)
58 {
59 EVENT_LOGD("enter");
60
61 std::lock_guard<std::mutex> lock(mutex_);
62
63 if (!GetBundleMgrProxy()) {
64 EVENT_LOGE("failed to get bms proxy");
65 return false;
66 }
67
68 return sptrBundleMgr_->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::STATICSUBSCRIBER,
69 userId, extensionInfos);
70 }
71
QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)72 bool BundleManagerHelper::QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
73 {
74 EVENT_LOGD("enter");
75
76 std::lock_guard<std::mutex> lock(mutex_);
77
78 if (!GetBundleMgrProxy()) {
79 EVENT_LOGE("failed to get bms proxy");
80 return false;
81 }
82 std::vector<int> osAccountIds;
83 if (DelayedSingleton<OsAccountManagerHelper>::GetInstance()->QueryActiveOsAccountIds(osAccountIds) != ERR_OK) {
84 EVENT_LOGE("failed to QueryActiveOsAccountIds!");
85 return false;
86 }
87 if (osAccountIds.size() == 0) {
88 EVENT_LOGE("no os account acquired!");
89 return false;
90 }
91 for (auto userId : osAccountIds) {
92 EVENT_LOGD("active userId = %{public}d", userId);
93 sptrBundleMgr_->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::STATICSUBSCRIBER,
94 userId, extensionInfos);
95 }
96 return true;
97 }
98
GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo & extension,std::vector<std::string> & profileInfos)99 bool BundleManagerHelper::GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo &extension,
100 std::vector<std::string> &profileInfos)
101 {
102 EVENT_LOGD("enter");
103
104 std::lock_guard<std::mutex> lock(mutex_);
105
106 if (!GetBundleMgrProxy()) {
107 EVENT_LOGE("failed to get bms proxy");
108 return false;
109 }
110
111 AppExecFwk::BundleMgrClient client;
112 return client.GetResConfigFile(extension, META_NAME_STATIC_SUBSCRIBER, profileInfos);
113 }
114
CheckIsSystemAppByUid(uid_t uid)115 bool BundleManagerHelper::CheckIsSystemAppByUid(uid_t uid)
116 {
117 EVENT_LOGD("enter");
118
119 std::lock_guard<std::mutex> lock(mutex_);
120
121 bool isSystemApp = false;
122
123 if (!GetBundleMgrProxy()) {
124 EVENT_LOGE("failed to get bms proxy");
125 return isSystemApp;
126 }
127
128 isSystemApp = sptrBundleMgr_->CheckIsSystemAppByUid(uid);
129
130 return isSystemApp;
131 }
132
CheckIsSystemAppByBundleName(const std::string & bundleName,const int32_t & userId)133 bool BundleManagerHelper::CheckIsSystemAppByBundleName(const std::string &bundleName, const int32_t &userId)
134 {
135 EVENT_LOGD("enter");
136
137 std::lock_guard<std::mutex> lock(mutex_);
138
139 bool isSystemApp = false;
140
141 if (!GetBundleMgrProxy()) {
142 EVENT_LOGE("failed to get bms proxy");
143 return isSystemApp;
144 }
145 std::string identity = IPCSkeleton::ResetCallingIdentity();
146 int32_t uid = sptrBundleMgr_->GetUidByBundleName(bundleName, userId);
147 IPCSkeleton::SetCallingIdentity(identity);
148 if (uid < 0) {
149 EVENT_LOGW("get invalid uid from bundle %{public}s of userId %{public}d", bundleName.c_str(), userId);
150 }
151 isSystemApp = sptrBundleMgr_->CheckIsSystemAppByUid(uid);
152
153 return isSystemApp;
154 }
155
GetBundleMgrProxy()156 bool BundleManagerHelper::GetBundleMgrProxy()
157 {
158 EVENT_LOGD("enter");
159
160 if (!sptrBundleMgr_) {
161 sptr<ISystemAbilityManager> systemAbilityManager =
162 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
163 if (!systemAbilityManager) {
164 EVENT_LOGE("Failed to get system ability mgr.");
165 return false;
166 }
167
168 sptr<IRemoteObject> remoteObject =
169 systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
170 if (!remoteObject) {
171 EVENT_LOGE("Failed to get bundle manager service.");
172 return false;
173 }
174
175 sptrBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
176 if ((!sptrBundleMgr_) || (!sptrBundleMgr_->AsObject())) {
177 EVENT_LOGE("Failed to get system bundle manager services ability");
178 return false;
179 }
180
181 bmsDeath_ = new (std::nothrow) BMSDeathRecipient();
182 if (!bmsDeath_) {
183 EVENT_LOGE("Failed to create death Recipient ptr BMSDeathRecipient");
184 return false;
185 }
186 if (!sptrBundleMgr_->AsObject()->AddDeathRecipient(bmsDeath_)) {
187 EVENT_LOGW("Failed to add death recipient");
188 }
189 }
190
191 return true;
192 }
193
ClearBundleManagerHelper()194 void BundleManagerHelper::ClearBundleManagerHelper()
195 {
196 EVENT_LOGD("enter");
197
198 std::lock_guard<std::mutex> lock(mutex_);
199
200 if ((sptrBundleMgr_ != nullptr) && (sptrBundleMgr_->AsObject() != nullptr)) {
201 sptrBundleMgr_->AsObject()->RemoveDeathRecipient(bmsDeath_);
202 }
203 sptrBundleMgr_ = nullptr;
204 }
205
GetApplicationInfos(const AppExecFwk::ApplicationFlag & flag,std::vector<AppExecFwk::ApplicationInfo> & appInfos)206 bool BundleManagerHelper::GetApplicationInfos(const AppExecFwk::ApplicationFlag &flag,
207 std::vector<AppExecFwk::ApplicationInfo> &appInfos)
208 {
209 EVENT_LOGD("enter");
210
211 std::lock_guard<std::mutex> lock(mutex_);
212
213 std::vector<int> osAccountIds {};
214 if (DelayedSingleton<OsAccountManagerHelper>::GetInstance()->QueryActiveOsAccountIds(osAccountIds) != ERR_OK
215 || osAccountIds.empty()) {
216 EVENT_LOGE("failed to QueryActiveOsAccountIds!");
217 return false;
218 }
219
220 if (!GetBundleMgrProxy()) {
221 EVENT_LOGE("failed to get bms proxy");
222 return false;
223 }
224
225 return sptrBundleMgr_->GetApplicationInfos(flag, osAccountIds[0], appInfos);
226 }
227 } // namespace EventFwk
228 } // namespace OHOS