• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (bundleMgrExtPtr) {
87             return bundleMgrExtPtr->CheckApiInfo(bundleInfo);
88         }
89         APP_LOGE("create class: %{public}s failed", bmsExtension_.bmsExtensionBundleMgr.extensionName.c_str());
90         return false;
91     }
92     APP_LOGW("access bms-extension failed");
93     return CheckApiInfo(bundleInfo.compatibleVersion, sdkVersion);
94 }
95 
CheckApiInfo(uint32_t compatibleVersion,uint32_t sdkVersion)96 bool BmsExtensionDataMgr::CheckApiInfo(uint32_t compatibleVersion, uint32_t sdkVersion)
97 {
98     APP_LOGD("CheckApiInfo with compatibleVersion:%{public}d, sdkVersion:%{public}d", compatibleVersion, sdkVersion);
99     uint32_t compatibleVersionOHOS = compatibleVersion % API_VERSION_BASE;
100     return compatibleVersionOHOS <= sdkVersion;
101 }
102 
HapVerify(const std::string & filePath,Security::Verify::HapVerifyResult & hapVerifyResult)103 ErrCode BmsExtensionDataMgr::HapVerify(const std::string &filePath, Security::Verify::HapVerifyResult &hapVerifyResult)
104 {
105     if ((Init() == ERR_OK) && handler_) {
106         auto bundleMgrExtPtr =
107             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
108         if (bundleMgrExtPtr == nullptr) {
109             APP_LOGW("bundleMgrExtPtr is nullptr");
110             return ERR_APPEXECFWK_NULL_PTR;
111         }
112         return bundleMgrExtPtr->HapVerify(filePath, hapVerifyResult);
113     }
114     APP_LOGW("access bms-extension failed");
115     return ERR_BUNDLEMANAGER_INSTALL_FAILED_SIGNATURE_EXTENSION_NOT_EXISTED;
116 }
117 
IsRdDevice()118 bool BmsExtensionDataMgr::IsRdDevice()
119 {
120     if ((Init() != ERR_OK) || handler_ == nullptr) {
121         APP_LOGW("link failed");
122         return false;
123     }
124     auto bundleMgrExtPtr =
125         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
126     if (bundleMgrExtPtr == nullptr) {
127         APP_LOGW("GetBundleMgrExt failed");
128         return false;
129     }
130     return bundleMgrExtPtr->IsRdDevice();
131 }
132 
QueryAbilityInfos(const Want & want,int32_t userId,std::vector<AbilityInfo> & abilityInfos)133 ErrCode BmsExtensionDataMgr::QueryAbilityInfos(const Want &want, int32_t userId,
134     std::vector<AbilityInfo> &abilityInfos)
135 {
136     if ((Init() == ERR_OK) && handler_) {
137         auto bundleMgrExtPtr =
138             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
139         if (bundleMgrExtPtr == nullptr) {
140             LOG_W(BMS_TAG_QUERY, "bundleMgrExtPtr is nullptr");
141             return ERR_APPEXECFWK_NULL_PTR;
142         }
143         return bundleMgrExtPtr->QueryAbilityInfos(want, userId, abilityInfos);
144     }
145     LOG_W(BMS_TAG_QUERY, "access bms-extension failed");
146     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
147 }
148 
QueryAbilityInfosWithFlag(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,bool isNewVersion)149 ErrCode BmsExtensionDataMgr::QueryAbilityInfosWithFlag(const Want &want, int32_t flags, int32_t userId,
150     std::vector<AbilityInfo> &abilityInfos, bool isNewVersion)
151 {
152     if ((Init() == ERR_OK) && handler_) {
153         auto bundleMgrExtPtr =
154             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
155         if (bundleMgrExtPtr == nullptr) {
156             LOG_W(BMS_TAG_QUERY, "bundleMgrExtPtr is nullptr");
157             return ERR_APPEXECFWK_NULL_PTR;
158         }
159         return bundleMgrExtPtr->QueryAbilityInfosWithFlag(want, flags, userId, abilityInfos, isNewVersion);
160     }
161     LOG_W(BMS_TAG_QUERY, "access bms-extension failed");
162     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
163 }
164 
GetBundleInfos(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId,bool isNewVersion)165 ErrCode BmsExtensionDataMgr::GetBundleInfos(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId,
166     bool isNewVersion)
167 {
168     if ((Init() == ERR_OK) && handler_) {
169         auto bundleMgrExtPtr =
170             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
171         if (bundleMgrExtPtr == nullptr) {
172             LOG_W(BMS_TAG_QUERY, "bundleMgrExtPtr is nullptr");
173             return ERR_APPEXECFWK_NULL_PTR;
174         }
175         return bundleMgrExtPtr->GetBundleInfos(flags, bundleInfos, userId, isNewVersion);
176     }
177     LOG_W(BMS_TAG_QUERY, "access bms-extension failed");
178     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
179 }
180 
GetBundleInfo(const std::string & bundleName,int32_t flags,int32_t userId,BundleInfo & bundleInfo,bool isNewVersion)181 ErrCode BmsExtensionDataMgr::GetBundleInfo(const std::string &bundleName, int32_t flags, int32_t userId,
182     BundleInfo &bundleInfo, bool isNewVersion)
183 {
184     if ((Init() == ERR_OK) && handler_) {
185         auto bundleMgrExtPtr =
186             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
187         if (bundleMgrExtPtr == nullptr) {
188             LOG_W(BMS_TAG_QUERY, "bundleMgrExtPtr is nullptr");
189             return ERR_APPEXECFWK_NULL_PTR;
190         }
191         return bundleMgrExtPtr->GetBundleInfo(bundleName, flags, userId, bundleInfo, isNewVersion);
192     }
193     LOG_W(BMS_TAG_QUERY, "access bms-extension failed");
194     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
195 }
196 
Uninstall(const std::string & bundleName)197 ErrCode BmsExtensionDataMgr::Uninstall(const std::string &bundleName)
198 {
199     if ((Init() == ERR_OK) && handler_) {
200         auto bundleMgrExtPtr =
201             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
202         if (bundleMgrExtPtr == nullptr) {
203             APP_LOGW("bundleMgrExtPtr is nullptr");
204             return ERR_APPEXECFWK_NULL_PTR;
205         }
206         return bundleMgrExtPtr->Uninstall(bundleName);
207     }
208     APP_LOGW("access bms-extension failed");
209     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
210 }
211 
GetBundleStats(const std::string & bundleName,int32_t userId,std::vector<int64_t> & bundleStats)212 ErrCode BmsExtensionDataMgr::GetBundleStats(
213     const std::string &bundleName, int32_t userId, std::vector<int64_t> &bundleStats)
214 {
215     if ((Init() != ERR_OK) || handler_ == nullptr) {
216         APP_LOGW("link failed");
217         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
218     }
219     auto bundleMgrExtPtr =
220         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
221     if (bundleMgrExtPtr == nullptr) {
222         APP_LOGW("GetBundleMgrExt failed");
223         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
224     }
225     return bundleMgrExtPtr->GetBundleStats(bundleName, userId, bundleStats);
226 }
227 
ClearData(const std::string & bundleName,int32_t userId)228 ErrCode BmsExtensionDataMgr::ClearData(const std::string &bundleName, int32_t userId)
229 {
230     if ((Init() != ERR_OK) || handler_ == nullptr) {
231         APP_LOGW("link failed");
232         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
233     }
234     auto bundleMgrExtPtr =
235         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
236     if (bundleMgrExtPtr == nullptr) {
237         APP_LOGW("GetBundleMgrExt failed");
238         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
239     }
240     return bundleMgrExtPtr->ClearData(bundleName, userId);
241 }
242 
ClearCache(const std::string & bundleName,sptr<IRemoteObject> callback,int32_t userId)243 ErrCode BmsExtensionDataMgr::ClearCache(const std::string &bundleName, sptr<IRemoteObject> callback, int32_t userId)
244 {
245     if ((Init() != ERR_OK) || handler_ == nullptr) {
246         APP_LOGW("link failed");
247         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
248     }
249     auto bundleMgrExtPtr =
250         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
251     if (bundleMgrExtPtr == nullptr) {
252         APP_LOGW("GetBundleMgrExt failed");
253         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
254     }
255     return bundleMgrExtPtr->ClearCache(bundleName, callback, userId);
256 }
257 
GetUidByBundleName(const std::string & bundleName,int32_t userId,int32_t & uid)258 ErrCode BmsExtensionDataMgr::GetUidByBundleName(const std::string &bundleName, int32_t userId, int32_t &uid)
259 {
260     if ((Init() != ERR_OK) || handler_ == nullptr) {
261         APP_LOGW("link failed");
262         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
263     }
264     auto bundleMgrExtPtr =
265         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
266     if (bundleMgrExtPtr == nullptr) {
267         APP_LOGW("GetBundleMgrExt failed");
268         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
269     }
270     return bundleMgrExtPtr->GetUidByBundleName(bundleName, userId, uid);
271 }
272 
GetBundleNameByUid(int32_t uid,std::string & bundleName)273 ErrCode BmsExtensionDataMgr::GetBundleNameByUid(int32_t uid, std::string &bundleName)
274 {
275     if ((Init() != ERR_OK) || handler_ == nullptr) {
276         APP_LOGW("link failed");
277         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
278     }
279     auto bundleMgrExtPtr =
280         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
281     if (bundleMgrExtPtr == nullptr) {
282         APP_LOGW("GetBundleMgrExt failed");
283         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
284     }
285     return bundleMgrExtPtr->GetBundleNameByUid(uid, bundleName);
286 }
287 
VerifyActivationLock(bool & res)288 ErrCode BmsExtensionDataMgr::VerifyActivationLock(bool &res)
289 {
290     if ((Init() != ERR_OK) || handler_ == nullptr) {
291         APP_LOGW("link failed");
292         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
293     }
294     auto bundleMgrExtPtr =
295         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
296     if (bundleMgrExtPtr == nullptr) {
297         APP_LOGW("GetBundleMgrExt failed");
298         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
299     }
300     return bundleMgrExtPtr->VerifyActivationLock(res);
301 }
302 
GetBackupUninstallList(int32_t userId,std::set<std::string> & uninstallBundles)303 ErrCode BmsExtensionDataMgr::GetBackupUninstallList(int32_t userId, std::set<std::string> &uninstallBundles)
304 {
305     if (Init() != ERR_OK || handler_ == nullptr) {
306         APP_LOGW("link failed");
307         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
308     }
309     auto bundleMgrExtPtr =
310         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
311     if (bundleMgrExtPtr == nullptr) {
312         APP_LOGW("GetBundleMgrExt failed");
313         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
314     }
315     return bundleMgrExtPtr->GetBackupUninstallList(userId, uninstallBundles);
316 }
317 
ClearBackupUninstallFile(int32_t userId)318 ErrCode BmsExtensionDataMgr::ClearBackupUninstallFile(int32_t userId)
319 {
320     if (Init() != ERR_OK || handler_ == nullptr) {
321         APP_LOGW("link failed");
322         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
323     }
324     auto bundleMgrExtPtr =
325         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
326     if (bundleMgrExtPtr == nullptr) {
327         APP_LOGW("GetBundleMgrExt failed");
328         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
329     }
330     return bundleMgrExtPtr->ClearBackupUninstallFile(userId);
331 }
332 
IsAppInBlocklist(const std::string & bundleName,const int32_t userId)333 bool BmsExtensionDataMgr::IsAppInBlocklist(const std::string &bundleName, const int32_t userId)
334 {
335     if ((Init() != ERR_OK) || handler_ == nullptr) {
336         APP_LOGW("link failed");
337         return false;
338     }
339     auto bundleMgrExtPtr =
340         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
341     if (bundleMgrExtPtr == nullptr) {
342         APP_LOGW("GetBundleMgrExt failed");
343         return false;
344     }
345     return bundleMgrExtPtr->IsAppInBlocklist(bundleName, userId);
346 }
347 
CheckWhetherCanBeUninstalled(const std::string & bundleName,const std::string & appIdentifier)348 bool BmsExtensionDataMgr::CheckWhetherCanBeUninstalled(const std::string &bundleName,
349     const std::string &appIdentifier)
350 {
351     if ((Init() != ERR_OK) || handler_ == nullptr) {
352         APP_LOGW("link failed");
353         return true;
354     }
355     auto bundleMgrExtPtr =
356         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
357     if (bundleMgrExtPtr == nullptr) {
358         APP_LOGW("GetBundleMgrExt failed");
359         return true;
360     }
361     return bundleMgrExtPtr->CheckWhetherCanBeUninstalled(bundleName, appIdentifier);
362 }
363 
AddResourceInfoByBundleName(const std::string & bundleName,const int32_t userId)364 ErrCode BmsExtensionDataMgr::AddResourceInfoByBundleName(const std::string &bundleName, const int32_t userId)
365 {
366     if (Init() != ERR_OK || handler_ == nullptr) {
367         APP_LOGW("link failed");
368         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
369     }
370     auto bundleMgrExtPtr =
371         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
372     if (bundleMgrExtPtr == nullptr) {
373         APP_LOGW("GetBundleMgrExt failed");
374         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
375     }
376     ErrCode ret = bundleMgrExtPtr->AddResourceInfoByBundleName(bundleName, userId);
377     APP_LOGD("call bundle mgr ext return %{public}d by bundleName:%{public}s userId:%{private}d",
378         ret, bundleName.c_str(), userId);
379     return ret;
380 }
381 
AddResourceInfoByAbility(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,const int32_t userId)382 ErrCode BmsExtensionDataMgr::AddResourceInfoByAbility(const std::string &bundleName, const std::string &moduleName,
383     const std::string &abilityName, const int32_t userId)
384 {
385     if (Init() != ERR_OK || handler_ == nullptr) {
386         APP_LOGW("link failed");
387         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
388     }
389     auto bundleMgrExtPtr =
390         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
391     if (bundleMgrExtPtr == nullptr) {
392         APP_LOGW("GetBundleMgrExt failed");
393         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
394     }
395     ErrCode ret = bundleMgrExtPtr->AddResourceInfoByAbility(bundleName, moduleName, abilityName, userId);
396     APP_LOGD("call bundle mgr ext return %{public}d by bundleName:%{public}s moduleName:%{public}s \
397         abilityName:%{public}s userId:%{private}d",
398         ret, bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), userId);
399     return ret;
400 }
401 
DeleteResourceInfo(const std::string & key)402 ErrCode BmsExtensionDataMgr::DeleteResourceInfo(const std::string &key)
403 {
404     if (Init() != ERR_OK || handler_ == nullptr) {
405         APP_LOGW("link failed");
406         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
407     }
408     auto bundleMgrExtPtr =
409         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
410     if (bundleMgrExtPtr == nullptr) {
411         APP_LOGW("GetBundleMgrExt failed");
412         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
413     }
414     ErrCode ret = bundleMgrExtPtr->DeleteResourceInfo(key);
415     APP_LOGD("call bundle mgr ext return %{public}d by key:%{private}s", ret, key.c_str());
416     return ret;
417 }
418 
KeyOperation(const std::vector<CodeProtectBundleInfo> & codeProtectBundleInfos,int32_t type)419 ErrCode BmsExtensionDataMgr::KeyOperation(
420     const std::vector<CodeProtectBundleInfo> &codeProtectBundleInfos, int32_t type)
421 {
422     if ((Init() != ERR_OK) || handler_ == nullptr) {
423         APP_LOGW("link failed");
424         return ERR_OK;
425     }
426     auto bundleMgrExtPtr =
427         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
428     if (bundleMgrExtPtr == nullptr) {
429         APP_LOGW("GetBundleMgrExt failed");
430         return ERR_OK;
431     }
432     auto ret = bundleMgrExtPtr->KeyOperation(codeProtectBundleInfos, type);
433     if (!codeProtectBundleInfos.empty()) {
434         APP_LOGI("KeyOperation %{public}s %{public}d ret %{public}d",
435             codeProtectBundleInfos[0].bundleName.c_str(), type, ret);
436     }
437     return ret;
438 }
439 
OptimizeDisposedPredicates(const std::string & callingName,const std::string & appId,int32_t userId,int32_t appIndex,NativeRdb::AbsRdbPredicates & absRdbPredicates)440 ErrCode BmsExtensionDataMgr::OptimizeDisposedPredicates(const std::string &callingName, const std::string &appId,
441     int32_t userId, int32_t appIndex, NativeRdb::AbsRdbPredicates &absRdbPredicates)
442 {
443     if (Init() != ERR_OK || handler_ == nullptr) {
444         APP_LOGW("link failed");
445         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
446     }
447     auto bundleMgrExtPtr =
448         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
449     if (bundleMgrExtPtr == nullptr) {
450         APP_LOGW("GetBundleMgrExt failed");
451         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
452     }
453     ErrCode ret = bundleMgrExtPtr->OptimizeDisposedPredicates(callingName, appId, userId, appIndex, absRdbPredicates);
454     APP_LOGD("call bundle mgr ext OptimizeDisposedPredicates, return %{public}d, result:%{private}s",
455         ret, absRdbPredicates.ToString().c_str());
456     return ret;
457 }
458 
GetBundleResourceInfo(const std::string & bundleName,const uint32_t flags,BundleResourceInfo & bundleResourceInfo,const int32_t appIndex)459 ErrCode BmsExtensionDataMgr::GetBundleResourceInfo(const std::string &bundleName, const uint32_t flags,
460     BundleResourceInfo &bundleResourceInfo, const int32_t appIndex)
461 {
462     if (Init() != ERR_OK || handler_ == nullptr) {
463         APP_LOGW("link failed");
464         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
465     }
466     auto bundleMgrExtPtr =
467         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
468     if (bundleMgrExtPtr == nullptr) {
469         APP_LOGW("GetBundleMgrExt failed");
470         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
471     }
472     ErrCode ret = bundleMgrExtPtr->GetBundleResourceInfo(bundleName, flags, bundleResourceInfo, appIndex);
473     APP_LOGD("call bundle mgr ext GetBundleResourceInfo, return %{public}d, bundleName:%{public}s",
474         ret, bundleName.c_str());
475     return ret;
476 }
477 
GetLauncherAbilityResourceInfo(const std::string & bundleName,const uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfo,const int32_t appIndex)478 ErrCode BmsExtensionDataMgr::GetLauncherAbilityResourceInfo(const std::string &bundleName, const uint32_t flags,
479     std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfo, const int32_t appIndex)
480 {
481     if (Init() != ERR_OK || handler_ == nullptr) {
482         APP_LOGW("link failed");
483         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
484     }
485     auto bundleMgrExtPtr =
486         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
487     if (bundleMgrExtPtr == nullptr) {
488         APP_LOGW("GetBundleMgrExt failed");
489         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
490     }
491     ErrCode ret =
492         bundleMgrExtPtr->GetLauncherAbilityResourceInfo(bundleName, flags, launcherAbilityResourceInfo, appIndex);
493     APP_LOGD("call bundle mgr ext GetLauncherAbilityResourceInfo, return %{public}d, bundleName:%{public}s",
494         ret, bundleName.c_str());
495     return ret;
496 }
497 
GetAllBundleResourceInfo(const uint32_t flags,std::vector<BundleResourceInfo> & bundleResourceInfos)498 ErrCode BmsExtensionDataMgr::GetAllBundleResourceInfo(const uint32_t flags,
499     std::vector<BundleResourceInfo> &bundleResourceInfos)
500 {
501     if (Init() != ERR_OK || handler_ == nullptr) {
502         APP_LOGW("link failed");
503         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
504     }
505     auto bundleMgrExtPtr =
506         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
507     if (bundleMgrExtPtr == nullptr) {
508         APP_LOGW("GetBundleMgrExt failed");
509         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
510     }
511     ErrCode ret = bundleMgrExtPtr->GetAllBundleResourceInfo(flags, bundleResourceInfos);
512     APP_LOGD("call bundle mgr ext GetAllBundleResourceInfo, return %{public}d", ret);
513     return ret;
514 }
515 
GetAllLauncherAbilityResourceInfo(const uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfos)516 ErrCode BmsExtensionDataMgr::GetAllLauncherAbilityResourceInfo(const uint32_t flags,
517     std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfos)
518 {
519     if (Init() != ERR_OK || handler_ == nullptr) {
520         APP_LOGW("link failed");
521         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
522     }
523     auto bundleMgrExtPtr =
524         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
525     if (bundleMgrExtPtr == nullptr) {
526         APP_LOGW("GetBundleMgrExt failed");
527         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
528     }
529     ErrCode ret = bundleMgrExtPtr->GetAllLauncherAbilityResourceInfo(flags, launcherAbilityResourceInfos);
530     APP_LOGD("call bundle mgr ext GetAllLauncherAbilityResourceInfo, return %{public}d", ret);
531     return ret;
532 }
533 
CheckBundleNameAndStratAbility(const std::string & bundleName,const std::string & appIdentifier)534 void BmsExtensionDataMgr::CheckBundleNameAndStratAbility(const std::string &bundleName,
535     const std::string &appIdentifier)
536 {
537     if (Init() != ERR_OK || handler_ == nullptr) {
538         APP_LOGW("link failed");
539         return;
540     }
541     if (bundleName.empty()) {
542         APP_LOGW("bundleName empty");
543         return;
544     }
545     auto bundleMgrExtPtr =
546         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
547     if (bundleMgrExtPtr == nullptr) {
548         APP_LOGW("GetBundleMgrExt failed");
549         return;
550     }
551     bundleMgrExtPtr->CheckBundleNameAndStratAbility(bundleName, appIdentifier);
552 }
553 
DetermineCloneNum(const std::string & bundleName,const std::string & appIdentifier,int32_t & cloneNum)554 bool BmsExtensionDataMgr::DetermineCloneNum(
555     const std::string &bundleName, const std::string &appIdentifier, int32_t &cloneNum)
556 {
557     if (Init() != ERR_OK || handler_ == nullptr) {
558         APP_LOGW("link failed");
559         return false;
560     }
561     if (bundleName.empty()) {
562         APP_LOGW("bundleName empty");
563         return false;
564     }
565     auto bundleMgrExtPtr =
566         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
567     if (bundleMgrExtPtr == nullptr) {
568         APP_LOGW("GetBundleMgrExt failed");
569         return false;
570     }
571     return bundleMgrExtPtr->DetermineCloneNum(bundleName, appIdentifier, cloneNum);
572 }
573 
GetCompatibleDeviceType(const std::string & bundleName)574 std::string BmsExtensionDataMgr::GetCompatibleDeviceType(const std::string &bundleName)
575 {
576     if ((Init() == ERR_OK) && handler_) {
577         auto bundleMgrExtPtr =
578             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
579         if (bundleMgrExtPtr) {
580             return bundleMgrExtPtr->GetCompatibleDeviceType(bundleName);
581         }
582         APP_LOGE("create class: %{public}s failed", bmsExtension_.bmsExtensionBundleMgr.extensionName.c_str());
583         return GetDeviceType();
584     }
585     APP_LOGW("access bms-extension failed");
586     return GetDeviceType();
587 }
588 
VerifyActivationLockToken(bool & res)589 ErrCode BmsExtensionDataMgr::VerifyActivationLockToken(bool &res)
590 {
591     if ((Init() != ERR_OK) || handler_ == nullptr) {
592         APP_LOGW("link failed");
593         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
594     }
595     auto bundleMgrExtPtr =
596         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
597     if (bundleMgrExtPtr == nullptr) {
598         APP_LOGW("GetBundleMgrExt failed");
599         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
600     }
601     return bundleMgrExtPtr->VerifyActivationLockToken(res);
602 }
603 
IsNeedToSkipPreBundleInstall()604 bool BmsExtensionDataMgr::IsNeedToSkipPreBundleInstall()
605 {
606     if (Init() != ERR_OK || handler_ == nullptr) {
607         APP_LOGW("link failed");
608         return false;
609     }
610     auto bundleMgrExtPtr =
611         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
612     if (bundleMgrExtPtr == nullptr) {
613         APP_LOGW("GetBundleMgrExt failed");
614         return false;
615     }
616     return bundleMgrExtPtr->IsNeedToSkipPreBundleInstall();
617 }
618 
GetBundleArchiveInfoExt(const std::string & hapFilePath,int32_t fd,BundleInfo & bundleInfo)619 ErrCode BmsExtensionDataMgr::GetBundleArchiveInfoExt(
620     const std::string &hapFilePath, int32_t fd, BundleInfo &bundleInfo)
621 {
622     if ((Init() != ERR_OK) || handler_ == nullptr) {
623         APP_LOGW("link failed");
624         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
625     }
626     auto bundleMgrExtPtr =
627         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
628     if (bundleMgrExtPtr == nullptr) {
629         APP_LOGW("GetBundleMgrExt failed");
630         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
631     }
632     return bundleMgrExtPtr->GetBundleArchiveInfoExt(hapFilePath, fd, bundleInfo);
633 }
634 } // AppExecFwk
635 } // OHOS
636