1 /*
2 * Copyright (c) 2023 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 <dlfcn.h>
17
18 #include "app_log_wrapper.h"
19 #include "app_log_tag_wrapper.h"
20 #include "bms_extension_data_mgr.h"
21 #include "bms_extension_profile.h"
22 #include "bundle_mgr_ext_register.h"
23 #include "parameter.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 BmsExtension BmsExtensionDataMgr::bmsExtension_;
28 void *BmsExtensionDataMgr::handler_ = nullptr;
29 namespace {
30 static std::mutex stateMutex;
31 const std::string BMS_EXTENSION_PATH = "/system/etc/app/bms-extensions.json";
32 const uint32_t API_VERSION_BASE = 1000;
33 }
34
BmsExtensionDataMgr()35 BmsExtensionDataMgr::BmsExtensionDataMgr()
36 {
37 }
38
Init()39 ErrCode BmsExtensionDataMgr::Init()
40 {
41 std::lock_guard<std::mutex> stateLock(stateMutex);
42 if (bmsExtension_.bmsExtensionBundleMgr.extensionName.empty() || !handler_) {
43 BmsExtensionProfile bmsExtensionProfile;
44 auto res = bmsExtensionProfile.ParseBmsExtension(BMS_EXTENSION_PATH, bmsExtension_);
45 if (res != ERR_OK) {
46 APP_LOGW("ParseBmsExtension failed %{public}d", res);
47 return ERR_APPEXECFWK_PARSE_UNEXPECTED;
48 }
49 APP_LOGD("parse bms-extension.json success, which is: %{public}s", bmsExtension_.ToString().c_str());
50 if (!OpenHandler()) {
51 APP_LOGW("dlopen bms-extension so failed");
52 return ERR_APPEXECFWK_NULL_PTR;
53 }
54 }
55 return ERR_OK;
56 }
57
OpenHandler()58 bool BmsExtensionDataMgr::OpenHandler()
59 {
60 APP_LOGD("OpenHandler start");
61 auto handle = &handler_;
62 if (handle == nullptr) {
63 APP_LOGE("OpenHandler error handle is nullptr");
64 return false;
65 }
66 auto libPath = bmsExtension_.bmsExtensionBundleMgr.libPath.c_str();
67 auto lib64Path = bmsExtension_.bmsExtensionBundleMgr.lib64Path.c_str();
68 *handle = dlopen(lib64Path, RTLD_NOW | RTLD_GLOBAL);
69 if (*handle == nullptr) {
70 APP_LOGW("open %{public}s failed %{public}s", lib64Path, dlerror());
71 *handle = dlopen(libPath, RTLD_NOW | RTLD_GLOBAL);
72 }
73 if (*handle == nullptr) {
74 APP_LOGE("open %{public}s failed %{public}s", libPath, dlerror());
75 return false;
76 }
77 APP_LOGD("OpenHandler end");
78 return true;
79 }
80
CheckApiInfo(const BundleInfo & bundleInfo,uint32_t sdkVersion)81 bool BmsExtensionDataMgr::CheckApiInfo(const BundleInfo &bundleInfo, uint32_t sdkVersion)
82 {
83 if ((Init() == ERR_OK) && handler_) {
84 auto bundleMgrExtPtr =
85 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
86 bool res = false;
87 if (bundleMgrExtPtr) {
88 res = bundleMgrExtPtr->CheckApiInfo(bundleInfo);
89 if (!res) {
90 APP_LOGE("CheckApiInfo in bms-extension failed");
91 }
92 return res;
93 }
94 APP_LOGE("create class: %{public}s failed", bmsExtension_.bmsExtensionBundleMgr.extensionName.c_str());
95 return res;
96 }
97 APP_LOGW("access bms-extension failed");
98 return CheckApiInfo(bundleInfo.compatibleVersion, sdkVersion);
99 }
100
CheckApiInfo(uint32_t compatibleVersion,uint32_t sdkVersion)101 bool BmsExtensionDataMgr::CheckApiInfo(uint32_t compatibleVersion, uint32_t sdkVersion)
102 {
103 APP_LOGD("CheckApiInfo with compatibleVersion:%{public}d, sdkVersion:%{public}d", compatibleVersion, sdkVersion);
104 uint32_t compatibleVersionOHOS = compatibleVersion % API_VERSION_BASE;
105 bool res = compatibleVersionOHOS <= sdkVersion;
106 if (!res) {
107 APP_LOGE("Ext CheckApiInfo failed with compatibleVersion:%{public}d, sdkVersion:%{public}d",
108 compatibleVersionOHOS, sdkVersion);
109 }
110 return res;
111 }
112
HapVerify(const std::string & filePath,Security::Verify::HapVerifyResult & hapVerifyResult)113 ErrCode BmsExtensionDataMgr::HapVerify(const std::string &filePath, Security::Verify::HapVerifyResult &hapVerifyResult)
114 {
115 if ((Init() == ERR_OK) && handler_) {
116 auto bundleMgrExtPtr =
117 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
118 if (bundleMgrExtPtr == nullptr) {
119 APP_LOGW("bundleMgrExtPtr is nullptr");
120 return ERR_APPEXECFWK_NULL_PTR;
121 }
122 return bundleMgrExtPtr->HapVerify(filePath, hapVerifyResult);
123 }
124 APP_LOGW("access bms-extension failed");
125 return ERR_BUNDLEMANAGER_INSTALL_FAILED_SIGNATURE_EXTENSION_NOT_EXISTED;
126 }
127
IsRdDevice()128 bool BmsExtensionDataMgr::IsRdDevice()
129 {
130 if ((Init() != ERR_OK) || handler_ == nullptr) {
131 APP_LOGW("link failed");
132 return false;
133 }
134 auto bundleMgrExtPtr =
135 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
136 if (bundleMgrExtPtr == nullptr) {
137 APP_LOGW("GetBundleMgrExt failed");
138 return false;
139 }
140 return bundleMgrExtPtr->IsRdDevice();
141 }
142
QueryAbilityInfos(const Want & want,int32_t userId,std::vector<AbilityInfo> & abilityInfos)143 ErrCode BmsExtensionDataMgr::QueryAbilityInfos(const Want &want, int32_t userId,
144 std::vector<AbilityInfo> &abilityInfos)
145 {
146 if ((Init() == ERR_OK) && handler_) {
147 auto bundleMgrExtPtr =
148 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
149 if (bundleMgrExtPtr == nullptr) {
150 LOG_W(BMS_TAG_QUERY, "bundleMgrExtPtr is nullptr");
151 return ERR_APPEXECFWK_NULL_PTR;
152 }
153 return bundleMgrExtPtr->QueryAbilityInfos(want, userId, abilityInfos);
154 }
155 LOG_W(BMS_TAG_QUERY, "access bms-extension failed");
156 return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
157 }
158
QueryAbilityInfosWithFlag(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,bool isNewVersion)159 ErrCode BmsExtensionDataMgr::QueryAbilityInfosWithFlag(const Want &want, int32_t flags, int32_t userId,
160 std::vector<AbilityInfo> &abilityInfos, bool isNewVersion)
161 {
162 if ((Init() == ERR_OK) && handler_) {
163 auto bundleMgrExtPtr =
164 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
165 if (bundleMgrExtPtr == nullptr) {
166 LOG_W(BMS_TAG_QUERY, "bundleMgrExtPtr is nullptr");
167 return ERR_APPEXECFWK_NULL_PTR;
168 }
169 return bundleMgrExtPtr->QueryAbilityInfosWithFlag(want, flags, userId, abilityInfos, isNewVersion);
170 }
171 LOG_W(BMS_TAG_QUERY, "access bms-extension failed");
172 return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
173 }
174
GetBundleInfos(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId,bool isNewVersion)175 ErrCode BmsExtensionDataMgr::GetBundleInfos(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId,
176 bool isNewVersion)
177 {
178 if ((Init() == ERR_OK) && handler_) {
179 auto bundleMgrExtPtr =
180 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
181 if (bundleMgrExtPtr == nullptr) {
182 LOG_W(BMS_TAG_QUERY, "bundleMgrExtPtr is nullptr");
183 return ERR_APPEXECFWK_NULL_PTR;
184 }
185 return bundleMgrExtPtr->GetBundleInfos(flags, bundleInfos, userId, isNewVersion);
186 }
187 LOG_W(BMS_TAG_QUERY, "access bms-extension failed");
188 return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
189 }
190
GetBundleInfo(const std::string & bundleName,int32_t flags,int32_t userId,BundleInfo & bundleInfo,bool isNewVersion)191 ErrCode BmsExtensionDataMgr::GetBundleInfo(const std::string &bundleName, int32_t flags, int32_t userId,
192 BundleInfo &bundleInfo, bool isNewVersion)
193 {
194 if ((Init() == ERR_OK) && handler_) {
195 auto bundleMgrExtPtr =
196 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
197 if (bundleMgrExtPtr == nullptr) {
198 LOG_W(BMS_TAG_QUERY, "bundleMgrExtPtr is nullptr");
199 return ERR_APPEXECFWK_NULL_PTR;
200 }
201 return bundleMgrExtPtr->GetBundleInfo(bundleName, flags, userId, bundleInfo, isNewVersion);
202 }
203 LOG_W(BMS_TAG_QUERY, "access bms-extension failed");
204 return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
205 }
206
Uninstall(const std::string & bundleName)207 ErrCode BmsExtensionDataMgr::Uninstall(const std::string &bundleName)
208 {
209 if ((Init() == ERR_OK) && handler_) {
210 auto bundleMgrExtPtr =
211 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
212 if (bundleMgrExtPtr == nullptr) {
213 APP_LOGW("bundleMgrExtPtr is nullptr");
214 return ERR_APPEXECFWK_NULL_PTR;
215 }
216 return bundleMgrExtPtr->Uninstall(bundleName);
217 }
218 APP_LOGW("access bms-extension failed");
219 return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
220 }
221
GetBundleStats(const std::string & bundleName,int32_t userId,std::vector<int64_t> & bundleStats)222 ErrCode BmsExtensionDataMgr::GetBundleStats(
223 const std::string &bundleName, int32_t userId, std::vector<int64_t> &bundleStats)
224 {
225 if ((Init() != ERR_OK) || handler_ == nullptr) {
226 APP_LOGW("link failed");
227 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
228 }
229 auto bundleMgrExtPtr =
230 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
231 if (bundleMgrExtPtr == nullptr) {
232 APP_LOGW("GetBundleMgrExt failed");
233 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
234 }
235 return bundleMgrExtPtr->GetBundleStats(bundleName, userId, bundleStats);
236 }
237
ClearData(const std::string & bundleName,int32_t userId)238 ErrCode BmsExtensionDataMgr::ClearData(const std::string &bundleName, int32_t userId)
239 {
240 if ((Init() != ERR_OK) || handler_ == nullptr) {
241 APP_LOGW("link failed");
242 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
243 }
244 auto bundleMgrExtPtr =
245 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
246 if (bundleMgrExtPtr == nullptr) {
247 APP_LOGW("GetBundleMgrExt failed");
248 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
249 }
250 return bundleMgrExtPtr->ClearData(bundleName, userId);
251 }
252
ClearCache(const std::string & bundleName,sptr<IRemoteObject> callback,int32_t userId)253 ErrCode BmsExtensionDataMgr::ClearCache(const std::string &bundleName, sptr<IRemoteObject> callback, int32_t userId)
254 {
255 if ((Init() != ERR_OK) || handler_ == nullptr) {
256 APP_LOGW("link failed");
257 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
258 }
259 auto bundleMgrExtPtr =
260 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
261 if (bundleMgrExtPtr == nullptr) {
262 APP_LOGW("GetBundleMgrExt failed");
263 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
264 }
265 return bundleMgrExtPtr->ClearCache(bundleName, callback, userId);
266 }
267
GetUidByBundleName(const std::string & bundleName,int32_t userId,int32_t & uid)268 ErrCode BmsExtensionDataMgr::GetUidByBundleName(const std::string &bundleName, int32_t userId, int32_t &uid)
269 {
270 if ((Init() != ERR_OK) || handler_ == nullptr) {
271 APP_LOGW("link failed");
272 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
273 }
274 auto bundleMgrExtPtr =
275 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
276 if (bundleMgrExtPtr == nullptr) {
277 APP_LOGW("GetBundleMgrExt failed");
278 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
279 }
280 return bundleMgrExtPtr->GetUidByBundleName(bundleName, userId, uid);
281 }
282
GetBundleNameByUid(int32_t uid,std::string & bundleName)283 ErrCode BmsExtensionDataMgr::GetBundleNameByUid(int32_t uid, std::string &bundleName)
284 {
285 if ((Init() != ERR_OK) || handler_ == nullptr) {
286 APP_LOGW("link failed");
287 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
288 }
289 auto bundleMgrExtPtr =
290 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
291 if (bundleMgrExtPtr == nullptr) {
292 APP_LOGW("GetBundleMgrExt failed");
293 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
294 }
295 return bundleMgrExtPtr->GetBundleNameByUid(uid, bundleName);
296 }
297
VerifyActivationLock(bool & res)298 ErrCode BmsExtensionDataMgr::VerifyActivationLock(bool &res)
299 {
300 if ((Init() != ERR_OK) || handler_ == nullptr) {
301 APP_LOGW("link failed");
302 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
303 }
304 auto bundleMgrExtPtr =
305 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
306 if (bundleMgrExtPtr == nullptr) {
307 APP_LOGW("GetBundleMgrExt failed");
308 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
309 }
310 return bundleMgrExtPtr->VerifyActivationLock(res);
311 }
312
GetBackupUninstallList(int32_t userId,std::set<std::string> & uninstallBundles)313 ErrCode BmsExtensionDataMgr::GetBackupUninstallList(int32_t userId, std::set<std::string> &uninstallBundles)
314 {
315 if (Init() != ERR_OK || handler_ == nullptr) {
316 APP_LOGW("link failed");
317 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
318 }
319 auto bundleMgrExtPtr =
320 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
321 if (bundleMgrExtPtr == nullptr) {
322 APP_LOGW("GetBundleMgrExt failed");
323 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
324 }
325 return bundleMgrExtPtr->GetBackupUninstallList(userId, uninstallBundles);
326 }
327
ClearBackupUninstallFile(int32_t userId)328 ErrCode BmsExtensionDataMgr::ClearBackupUninstallFile(int32_t userId)
329 {
330 if (Init() != ERR_OK || handler_ == nullptr) {
331 APP_LOGW("link failed");
332 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
333 }
334 auto bundleMgrExtPtr =
335 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
336 if (bundleMgrExtPtr == nullptr) {
337 APP_LOGW("GetBundleMgrExt failed");
338 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
339 }
340 return bundleMgrExtPtr->ClearBackupUninstallFile(userId);
341 }
342
IsAppInBlocklist(const std::string & bundleName,const int32_t userId)343 bool BmsExtensionDataMgr::IsAppInBlocklist(const std::string &bundleName, const int32_t userId)
344 {
345 if ((Init() != ERR_OK) || handler_ == nullptr) {
346 APP_LOGW("link failed");
347 return false;
348 }
349 auto bundleMgrExtPtr =
350 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
351 if (bundleMgrExtPtr == nullptr) {
352 APP_LOGW("GetBundleMgrExt failed");
353 return false;
354 }
355 return bundleMgrExtPtr->IsAppInBlocklist(bundleName, userId);
356 }
357
CheckWhetherCanBeUninstalled(const std::string & bundleName,const std::string & appIdentifier)358 bool BmsExtensionDataMgr::CheckWhetherCanBeUninstalled(const std::string &bundleName,
359 const std::string &appIdentifier)
360 {
361 if ((Init() != ERR_OK) || handler_ == nullptr) {
362 APP_LOGW("link failed");
363 return true;
364 }
365 auto bundleMgrExtPtr =
366 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
367 if (bundleMgrExtPtr == nullptr) {
368 APP_LOGW("GetBundleMgrExt failed");
369 return true;
370 }
371 return bundleMgrExtPtr->CheckWhetherCanBeUninstalled(bundleName, appIdentifier);
372 }
373
AddResourceInfoByBundleName(const std::string & bundleName,const int32_t userId)374 ErrCode BmsExtensionDataMgr::AddResourceInfoByBundleName(const std::string &bundleName, const int32_t userId)
375 {
376 if (Init() != ERR_OK || handler_ == nullptr) {
377 APP_LOGW("link failed");
378 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
379 }
380 auto bundleMgrExtPtr =
381 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
382 if (bundleMgrExtPtr == nullptr) {
383 APP_LOGW("GetBundleMgrExt failed");
384 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
385 }
386 ErrCode ret = bundleMgrExtPtr->AddResourceInfoByBundleName(bundleName, userId);
387 APP_LOGD("call bundle mgr ext return %{public}d by bundleName:%{public}s userId:%{private}d",
388 ret, bundleName.c_str(), userId);
389 return ret;
390 }
391
AddResourceInfoByAbility(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,const int32_t userId)392 ErrCode BmsExtensionDataMgr::AddResourceInfoByAbility(const std::string &bundleName, const std::string &moduleName,
393 const std::string &abilityName, const int32_t userId)
394 {
395 if (Init() != ERR_OK || handler_ == nullptr) {
396 APP_LOGW("link failed");
397 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
398 }
399 auto bundleMgrExtPtr =
400 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
401 if (bundleMgrExtPtr == nullptr) {
402 APP_LOGW("GetBundleMgrExt failed");
403 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
404 }
405 ErrCode ret = bundleMgrExtPtr->AddResourceInfoByAbility(bundleName, moduleName, abilityName, userId);
406 APP_LOGD("call bundle mgr ext return %{public}d by bundleName:%{public}s moduleName:%{public}s \
407 abilityName:%{public}s userId:%{private}d",
408 ret, bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), userId);
409 return ret;
410 }
411
DeleteResourceInfo(const std::string & key)412 ErrCode BmsExtensionDataMgr::DeleteResourceInfo(const std::string &key)
413 {
414 if (Init() != ERR_OK || handler_ == nullptr) {
415 APP_LOGW("link failed");
416 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
417 }
418 auto bundleMgrExtPtr =
419 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
420 if (bundleMgrExtPtr == nullptr) {
421 APP_LOGW("GetBundleMgrExt failed");
422 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
423 }
424 ErrCode ret = bundleMgrExtPtr->DeleteResourceInfo(key);
425 APP_LOGD("call bundle mgr ext return %{public}d by key:%{private}s", ret, key.c_str());
426 return ret;
427 }
428
KeyOperation(const std::vector<CodeProtectBundleInfo> & codeProtectBundleInfos,int32_t type)429 ErrCode BmsExtensionDataMgr::KeyOperation(
430 const std::vector<CodeProtectBundleInfo> &codeProtectBundleInfos, int32_t type)
431 {
432 if ((Init() != ERR_OK) || handler_ == nullptr) {
433 APP_LOGW("link failed");
434 return ERR_OK;
435 }
436 auto bundleMgrExtPtr =
437 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
438 if (bundleMgrExtPtr == nullptr) {
439 APP_LOGW("GetBundleMgrExt failed");
440 return ERR_OK;
441 }
442 auto ret = bundleMgrExtPtr->KeyOperation(codeProtectBundleInfos, type);
443 if (!codeProtectBundleInfos.empty()) {
444 APP_LOGI("KeyOperation %{public}s %{public}d ret %{public}d",
445 codeProtectBundleInfos[0].bundleName.c_str(), type, ret);
446 }
447 return ret;
448 }
449
OptimizeDisposedPredicates(const std::string & callingName,const std::string & appId,int32_t userId,int32_t appIndex,NativeRdb::AbsRdbPredicates & absRdbPredicates)450 ErrCode BmsExtensionDataMgr::OptimizeDisposedPredicates(const std::string &callingName, const std::string &appId,
451 int32_t userId, int32_t appIndex, NativeRdb::AbsRdbPredicates &absRdbPredicates)
452 {
453 if (Init() != ERR_OK || handler_ == nullptr) {
454 APP_LOGW("link failed");
455 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
456 }
457 auto bundleMgrExtPtr =
458 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
459 if (bundleMgrExtPtr == nullptr) {
460 APP_LOGW("GetBundleMgrExt failed");
461 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
462 }
463 ErrCode ret = bundleMgrExtPtr->OptimizeDisposedPredicates(callingName, appId, userId, appIndex, absRdbPredicates);
464 APP_LOGD("call bundle mgr ext OptimizeDisposedPredicates, return %{public}d, result:%{private}s",
465 ret, absRdbPredicates.ToString().c_str());
466 return ret;
467 }
468
GetBundleResourceInfo(const std::string & bundleName,const uint32_t flags,BundleResourceInfo & bundleResourceInfo,const int32_t appIndex)469 ErrCode BmsExtensionDataMgr::GetBundleResourceInfo(const std::string &bundleName, const uint32_t flags,
470 BundleResourceInfo &bundleResourceInfo, const int32_t appIndex)
471 {
472 if (Init() != ERR_OK || handler_ == nullptr) {
473 APP_LOGW("link failed");
474 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
475 }
476 auto bundleMgrExtPtr =
477 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
478 if (bundleMgrExtPtr == nullptr) {
479 APP_LOGW("GetBundleMgrExt failed");
480 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
481 }
482 ErrCode ret = bundleMgrExtPtr->GetBundleResourceInfo(bundleName, flags, bundleResourceInfo, appIndex);
483 APP_LOGD("call bundle mgr ext GetBundleResourceInfo, return %{public}d, bundleName:%{public}s",
484 ret, bundleName.c_str());
485 return ret;
486 }
487
GetLauncherAbilityResourceInfo(const std::string & bundleName,const uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfo,const int32_t appIndex)488 ErrCode BmsExtensionDataMgr::GetLauncherAbilityResourceInfo(const std::string &bundleName, const uint32_t flags,
489 std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfo, const int32_t appIndex)
490 {
491 if (Init() != ERR_OK || handler_ == nullptr) {
492 APP_LOGW("link failed");
493 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
494 }
495 auto bundleMgrExtPtr =
496 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
497 if (bundleMgrExtPtr == nullptr) {
498 APP_LOGW("GetBundleMgrExt failed");
499 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
500 }
501 ErrCode ret =
502 bundleMgrExtPtr->GetLauncherAbilityResourceInfo(bundleName, flags, launcherAbilityResourceInfo, appIndex);
503 APP_LOGD("call bundle mgr ext GetLauncherAbilityResourceInfo, return %{public}d, bundleName:%{public}s",
504 ret, bundleName.c_str());
505 return ret;
506 }
507
GetAllBundleResourceInfo(const uint32_t flags,std::vector<BundleResourceInfo> & bundleResourceInfos)508 ErrCode BmsExtensionDataMgr::GetAllBundleResourceInfo(const uint32_t flags,
509 std::vector<BundleResourceInfo> &bundleResourceInfos)
510 {
511 if (Init() != ERR_OK || handler_ == nullptr) {
512 APP_LOGW("link failed");
513 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
514 }
515 auto bundleMgrExtPtr =
516 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
517 if (bundleMgrExtPtr == nullptr) {
518 APP_LOGW("GetBundleMgrExt failed");
519 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
520 }
521 ErrCode ret = bundleMgrExtPtr->GetAllBundleResourceInfo(flags, bundleResourceInfos);
522 APP_LOGD("call bundle mgr ext GetAllBundleResourceInfo, return %{public}d", ret);
523 return ret;
524 }
525
GetAllLauncherAbilityResourceInfo(const uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfos)526 ErrCode BmsExtensionDataMgr::GetAllLauncherAbilityResourceInfo(const uint32_t flags,
527 std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfos)
528 {
529 if (Init() != ERR_OK || handler_ == nullptr) {
530 APP_LOGW("link failed");
531 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
532 }
533 auto bundleMgrExtPtr =
534 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
535 if (bundleMgrExtPtr == nullptr) {
536 APP_LOGW("GetBundleMgrExt failed");
537 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
538 }
539 ErrCode ret = bundleMgrExtPtr->GetAllLauncherAbilityResourceInfo(flags, launcherAbilityResourceInfos);
540 APP_LOGD("call bundle mgr ext GetAllLauncherAbilityResourceInfo, return %{public}d", ret);
541 return ret;
542 }
543
CheckBundleNameAndStratAbility(const std::string & bundleName,const std::string & appIdentifier)544 void BmsExtensionDataMgr::CheckBundleNameAndStratAbility(const std::string &bundleName,
545 const std::string &appIdentifier)
546 {
547 if (Init() != ERR_OK || handler_ == nullptr) {
548 APP_LOGW("link failed");
549 return;
550 }
551 if (bundleName.empty()) {
552 APP_LOGW("bundleName empty");
553 return;
554 }
555 auto bundleMgrExtPtr =
556 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
557 if (bundleMgrExtPtr == nullptr) {
558 APP_LOGW("GetBundleMgrExt failed");
559 return;
560 }
561 bundleMgrExtPtr->CheckBundleNameAndStratAbility(bundleName, appIdentifier);
562 }
563
IsTargetApp(const std::string & bundleName,const std::string & appIdentifier)564 bool BmsExtensionDataMgr::IsTargetApp(const std::string &bundleName, const std::string &appIdentifier)
565 {
566 if (Init() != ERR_OK || handler_ == nullptr) {
567 APP_LOGW("link failed");
568 return false;
569 }
570 if (bundleName.empty()) {
571 APP_LOGW("bundleName empty");
572 return false;
573 }
574 auto bundleMgrExtPtr =
575 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
576 if (bundleMgrExtPtr == nullptr) {
577 APP_LOGW("GetBundleMgrExt failed");
578 return false;
579 }
580 return bundleMgrExtPtr->IsTargetApp(bundleName, appIdentifier);
581 }
582
DetermineCloneNum(const std::string & bundleName,const std::string & appIdentifier,int32_t & cloneNum)583 bool BmsExtensionDataMgr::DetermineCloneNum(
584 const std::string &bundleName, const std::string &appIdentifier, int32_t &cloneNum)
585 {
586 if (Init() != ERR_OK || handler_ == nullptr) {
587 APP_LOGW("link failed");
588 return false;
589 }
590 if (bundleName.empty()) {
591 APP_LOGW("bundleName empty");
592 return false;
593 }
594 auto bundleMgrExtPtr =
595 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
596 if (bundleMgrExtPtr == nullptr) {
597 APP_LOGW("GetBundleMgrExt failed");
598 return false;
599 }
600 return bundleMgrExtPtr->DetermineCloneNum(bundleName, appIdentifier, cloneNum);
601 }
602
GetCompatibleDeviceType(const std::string & bundleName)603 std::string BmsExtensionDataMgr::GetCompatibleDeviceType(const std::string &bundleName)
604 {
605 if ((Init() == ERR_OK) && handler_) {
606 auto bundleMgrExtPtr =
607 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
608 if (bundleMgrExtPtr) {
609 return bundleMgrExtPtr->GetCompatibleDeviceType(bundleName);
610 }
611 APP_LOGE("create class: %{public}s failed", bmsExtension_.bmsExtensionBundleMgr.extensionName.c_str());
612 return GetDeviceType();
613 }
614 APP_LOGW("access bms-extension failed");
615 return GetDeviceType();
616 }
617
VerifyActivationLockToken(bool & res)618 ErrCode BmsExtensionDataMgr::VerifyActivationLockToken(bool &res)
619 {
620 if ((Init() != ERR_OK) || handler_ == nullptr) {
621 APP_LOGW("link failed");
622 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
623 }
624 auto bundleMgrExtPtr =
625 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
626 if (bundleMgrExtPtr == nullptr) {
627 APP_LOGW("GetBundleMgrExt failed");
628 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
629 }
630 return bundleMgrExtPtr->VerifyActivationLockToken(res);
631 }
632
IsNeedToSkipPreBundleInstall()633 bool BmsExtensionDataMgr::IsNeedToSkipPreBundleInstall()
634 {
635 if (Init() != ERR_OK || handler_ == nullptr) {
636 APP_LOGW("link failed");
637 return false;
638 }
639 auto bundleMgrExtPtr =
640 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
641 if (bundleMgrExtPtr == nullptr) {
642 APP_LOGW("GetBundleMgrExt failed");
643 return false;
644 }
645 return bundleMgrExtPtr->IsNeedToSkipPreBundleInstall();
646 }
647
GetBundleNamesForUidExt(const int32_t uid,std::vector<std::string> & bundleNames)648 ErrCode BmsExtensionDataMgr::GetBundleNamesForUidExt(const int32_t uid, std::vector<std::string> &bundleNames)
649 {
650 if ((Init() != ERR_OK) || handler_ == nullptr) {
651 APP_LOGW("link failed");
652 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
653 }
654 auto bundleMgrExtPtr =
655 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
656 if (bundleMgrExtPtr == nullptr) {
657 APP_LOGW("GetBundleMgrExt failed");
658 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
659 }
660 return bundleMgrExtPtr->GetBundleNamesForUidExt(uid, bundleNames);
661 }
662
RegisterPreInstallWithCard()663 ErrCode BmsExtensionDataMgr::RegisterPreInstallWithCard()
664 {
665 if ((Init() != ERR_OK) || handler_ == nullptr) {
666 APP_LOGW("link failed");
667 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
668 }
669 auto bundleMgrExtPtr =
670 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
671 if (bundleMgrExtPtr == nullptr) {
672 APP_LOGW("GetBundleMgrExt failed");
673 return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
674 }
675 return bundleMgrExtPtr->RegisterPreInstallWithCard();
676 }
677
IsMCFlagSet()678 bool BmsExtensionDataMgr::IsMCFlagSet()
679 {
680 if (Init() != ERR_OK) {
681 APP_LOGW("init failed");
682 return false;
683 }
684 std::shared_ptr<BundleMgrExt> bundleMgrExtPtr =
685 BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
686 if (bundleMgrExtPtr == nullptr) {
687 APP_LOGW("bundleMgrExtPtr null");
688 return false;
689 }
690 return bundleMgrExtPtr->IsMCFlagSet();
691 }
692 } // AppExecFwk
693 } // OHOS
694