• 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 "bms_extension_data_mgr.h"
20 #include "bms_extension_profile.h"
21 #include "bundle_mgr_ext_register.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 BmsExtension BmsExtensionDataMgr::bmsExtension_;
26 void *BmsExtensionDataMgr::handler_ = nullptr;
27 namespace {
28 static std::mutex stateMutex_;
29 const std::string BMS_EXTENSION_PATH = "/system/etc/app/bms-extensions.json";
30 const uint32_t API_VERSION_BASE = 1000;
31 }
32 
BmsExtensionDataMgr()33 BmsExtensionDataMgr::BmsExtensionDataMgr()
34 {
35 }
36 
Init()37 ErrCode BmsExtensionDataMgr::Init()
38 {
39     std::lock_guard<std::mutex> stateLock(stateMutex_);
40     if (bmsExtension_.bmsExtensionBundleMgr.extensionName.empty() || !handler_) {
41         BmsExtensionProfile bmsExtensionProfile;
42         auto res = bmsExtensionProfile.ParseBmsExtension(BMS_EXTENSION_PATH, bmsExtension_);
43         if (res != ERR_OK) {
44             APP_LOGW("ParseBmsExtension failed, errCode is %{public}d", res);
45             return ERR_APPEXECFWK_PARSE_UNEXPECTED;
46         }
47         APP_LOGD("parse bms-extension.json success, which is: %{public}s", bmsExtension_.ToString().c_str());
48         if (!OpenHandler()) {
49             APP_LOGW("dlopen bms-extension so failed");
50             return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
51         }
52     }
53     return ERR_OK;
54 }
55 
OpenHandler()56 bool BmsExtensionDataMgr::OpenHandler()
57 {
58     APP_LOGI("OpenHandler start");
59     auto handle = &handler_;
60     if (handle == nullptr) {
61         APP_LOGE("OpenHandler error handle is nullptr.");
62         return false;
63     }
64     auto libPath = bmsExtension_.bmsExtensionBundleMgr.libPath.c_str();
65     auto lib64Path = bmsExtension_.bmsExtensionBundleMgr.lib64Path.c_str();
66     *handle = dlopen(lib64Path, RTLD_NOW | RTLD_GLOBAL);
67     if (*handle == nullptr) {
68         APP_LOGW("failed to open %{public}s, err:%{public}s", lib64Path, dlerror());
69         *handle = dlopen(libPath, RTLD_NOW | RTLD_GLOBAL);
70     }
71     if (*handle == nullptr) {
72         APP_LOGE("failed to open %{public}s, err:%{public}s", libPath, dlerror());
73         return false;
74     }
75     APP_LOGI("OpenHandler end");
76     return true;
77 }
78 
CheckApiInfo(const BundleInfo & bundleInfo,uint32_t sdkVersion)79 bool BmsExtensionDataMgr::CheckApiInfo(const BundleInfo &bundleInfo, uint32_t sdkVersion)
80 {
81     if ((Init() == ERR_OK) && handler_) {
82         auto bundleMgrExtPtr =
83             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
84         if (bundleMgrExtPtr) {
85             return bundleMgrExtPtr->CheckApiInfo(bundleInfo);
86         }
87         APP_LOGE("create class: %{public}s failed.", bmsExtension_.bmsExtensionBundleMgr.extensionName.c_str());
88         return false;
89     }
90     APP_LOGW("access bms-extension failed.");
91     return CheckApiInfo(bundleInfo.compatibleVersion, sdkVersion);
92 }
93 
CheckApiInfo(uint32_t compatibleVersion,uint32_t sdkVersion)94 bool BmsExtensionDataMgr::CheckApiInfo(uint32_t compatibleVersion, uint32_t sdkVersion)
95 {
96     APP_LOGD("CheckApiInfo with compatibleVersion:%{public}d, sdkVersion:%{public}d", compatibleVersion, sdkVersion);
97     uint32_t compatibleVersionOHOS = compatibleVersion % API_VERSION_BASE;
98     return compatibleVersionOHOS <= sdkVersion;
99 }
100 
HapVerify(const std::string & filePath,Security::Verify::HapVerifyResult & hapVerifyResult)101 ErrCode BmsExtensionDataMgr::HapVerify(const std::string &filePath, Security::Verify::HapVerifyResult &hapVerifyResult)
102 {
103     if ((Init() == ERR_OK) && handler_) {
104         auto bundleMgrExtPtr =
105             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
106         if (bundleMgrExtPtr == nullptr) {
107             APP_LOGW("bundleMgrExtPtr is nullptr.");
108             return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
109         }
110         return bundleMgrExtPtr->HapVerify(filePath, hapVerifyResult);
111     }
112     APP_LOGW("access bms-extension failed.");
113     return ERR_BUNDLEMANAGER_INSTALL_FAILED_SIGNATURE_EXTENSION_NOT_EXISTED;
114 }
115 
QueryAbilityInfos(const Want & want,int32_t userId,std::vector<AbilityInfo> & abilityInfos)116 ErrCode BmsExtensionDataMgr::QueryAbilityInfos(const Want &want, int32_t userId,
117     std::vector<AbilityInfo> &abilityInfos)
118 {
119     if ((Init() == ERR_OK) && handler_) {
120         auto bundleMgrExtPtr =
121             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
122         if (bundleMgrExtPtr == nullptr) {
123             APP_LOGW("bundleMgrExtPtr is nullptr.");
124             return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
125         }
126         return bundleMgrExtPtr->QueryAbilityInfos(want, userId, abilityInfos);
127     }
128     APP_LOGW("access bms-extension failed.");
129     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
130 }
131 
QueryAbilityInfosWithFlag(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,bool isNewVersion)132 ErrCode BmsExtensionDataMgr::QueryAbilityInfosWithFlag(const Want &want, int32_t flags, int32_t userId,
133     std::vector<AbilityInfo> &abilityInfos, bool isNewVersion)
134 {
135     if ((Init() == ERR_OK) && handler_) {
136         auto bundleMgrExtPtr =
137             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
138         if (bundleMgrExtPtr == nullptr) {
139             APP_LOGW("bundleMgrExtPtr is nullptr.");
140             return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
141         }
142         return bundleMgrExtPtr->QueryAbilityInfosWithFlag(want, flags, userId, abilityInfos, isNewVersion);
143     }
144     APP_LOGW("access bms-extension failed.");
145     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
146 }
147 
GetBundleInfos(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId,bool isNewVersion)148 ErrCode BmsExtensionDataMgr::GetBundleInfos(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId,
149     bool isNewVersion)
150 {
151     if ((Init() == ERR_OK) && handler_) {
152         auto bundleMgrExtPtr =
153             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
154         if (bundleMgrExtPtr == nullptr) {
155             APP_LOGW("bundleMgrExtPtr is nullptr.");
156             return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
157         }
158         return bundleMgrExtPtr->GetBundleInfos(flags, bundleInfos, userId, isNewVersion);
159     }
160     APP_LOGW("access bms-extension failed.");
161     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
162 }
163 
GetBundleInfo(const std::string & bundleName,int32_t flags,int32_t userId,BundleInfo & bundleInfo,bool isNewVersion)164 ErrCode BmsExtensionDataMgr::GetBundleInfo(const std::string &bundleName, int32_t flags, int32_t userId,
165     BundleInfo &bundleInfo, bool isNewVersion)
166 {
167     if ((Init() == ERR_OK) && handler_) {
168         auto bundleMgrExtPtr =
169             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
170         if (bundleMgrExtPtr == nullptr) {
171             APP_LOGW("bundleMgrExtPtr is nullptr.");
172             return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
173         }
174         return bundleMgrExtPtr->GetBundleInfo(bundleName, flags, userId, bundleInfo, isNewVersion);
175     }
176     APP_LOGW("access bms-extension failed.");
177     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
178 }
179 
Uninstall(const std::string & bundleName)180 ErrCode BmsExtensionDataMgr::Uninstall(const std::string &bundleName)
181 {
182     if ((Init() == ERR_OK) && handler_) {
183         auto bundleMgrExtPtr =
184             BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
185         if (bundleMgrExtPtr == nullptr) {
186             APP_LOGW("bundleMgrExtPtr is nullptr.");
187             return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
188         }
189         return bundleMgrExtPtr->Uninstall(bundleName);
190     }
191     APP_LOGW("access bms-extension failed.");
192     return ERR_BUNDLE_MANAGER_INSTALL_FAILED_BUNDLE_EXTENSION_NOT_EXISTED;
193 }
194 } // AppExecFwk
195 } // OHOS
196