• 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_LOGD("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_LOGD("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 
GetBundleStats(const std::string & bundleName,int32_t userId,std::vector<int64_t> & bundleStats)195 ErrCode BmsExtensionDataMgr::GetBundleStats(
196     const std::string &bundleName, int32_t userId, std::vector<int64_t> &bundleStats)
197 {
198     if ((Init() != ERR_OK) || handler_ == nullptr) {
199         APP_LOGW("link failed");
200         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
201     }
202     auto bundleMgrExtPtr =
203         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
204     if (bundleMgrExtPtr == nullptr) {
205         APP_LOGW("GetBundleMgrExt failed");
206         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
207     }
208     return bundleMgrExtPtr->GetBundleStats(bundleName, userId, bundleStats);
209 }
210 
ClearData(const std::string & bundleName,int32_t userId)211 ErrCode BmsExtensionDataMgr::ClearData(const std::string &bundleName, int32_t userId)
212 {
213     if ((Init() != ERR_OK) || handler_ == nullptr) {
214         APP_LOGW("link failed");
215         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
216     }
217     auto bundleMgrExtPtr =
218         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
219     if (bundleMgrExtPtr == nullptr) {
220         APP_LOGW("GetBundleMgrExt failed");
221         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
222     }
223     return bundleMgrExtPtr->ClearData(bundleName, userId);
224 }
225 
ClearCache(const std::string & bundleName,sptr<IRemoteObject> callback,int32_t userId)226 ErrCode BmsExtensionDataMgr::ClearCache(const std::string &bundleName, sptr<IRemoteObject> callback, int32_t userId)
227 {
228     if ((Init() != ERR_OK) || handler_ == nullptr) {
229         APP_LOGW("link failed");
230         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
231     }
232     auto bundleMgrExtPtr =
233         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
234     if (bundleMgrExtPtr == nullptr) {
235         APP_LOGW("GetBundleMgrExt failed");
236         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
237     }
238     return bundleMgrExtPtr->ClearCache(bundleName, callback, userId);
239 }
240 
GetUidByBundleName(const std::string & bundleName,int32_t userId,int32_t & uid)241 ErrCode BmsExtensionDataMgr::GetUidByBundleName(const std::string &bundleName, int32_t userId, int32_t &uid)
242 {
243     if ((Init() != ERR_OK) || handler_ == nullptr) {
244         APP_LOGW("link failed");
245         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
246     }
247     auto bundleMgrExtPtr =
248         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
249     if (bundleMgrExtPtr == nullptr) {
250         APP_LOGW("GetBundleMgrExt failed");
251         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
252     }
253     return bundleMgrExtPtr->GetUidByBundleName(bundleName, userId, uid);
254 }
255 
GetBundleNameByUid(int32_t uid,std::string & bundleName)256 ErrCode BmsExtensionDataMgr::GetBundleNameByUid(int32_t uid, std::string &bundleName)
257 {
258     if ((Init() != ERR_OK) || handler_ == nullptr) {
259         APP_LOGW("link failed");
260         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
261     }
262     auto bundleMgrExtPtr =
263         BundleMgrExtRegister::GetInstance().GetBundleMgrExt(bmsExtension_.bmsExtensionBundleMgr.extensionName);
264     if (bundleMgrExtPtr == nullptr) {
265         APP_LOGW("GetBundleMgrExt failed");
266         return ERR_BUNDLE_MANAGER_EXTENSION_INTERNAL_ERR;
267     }
268     return bundleMgrExtPtr->GetBundleNameByUid(uid, bundleName);
269 }
270 } // AppExecFwk
271 } // OHOS
272