• 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 "drv_bundle_state_callback.h"
17 #include "usb_bus_extension.h"
18 
19 #include <want.h>
20 #include <element_name.h>
21 
22 #include "system_ability_definition.h"
23 #include "iservice_registry.h"
24 #include "bundle_constants.h"
25 #include "os_account_manager.h"
26 #include  "pkg_db_helper.h"
27 
28 #include "hdf_log.h"
29 #include "edm_errors.h"
30 #include "hilog_wrapper.h"
31 #include "hitrace_meter.h"
32 #include "bus_extension_core.h"
33 namespace OHOS {
34 namespace ExternalDeviceManager {
35 using namespace std;
36 using namespace OHOS;
37 using namespace OHOS::AAFwk;
38 using namespace OHOS::AppExecFwk;
39 using namespace OHOS::ExternalDeviceManager;
40 
41 constexpr uint64_t LABEL = HITRACE_TAG_OHOS;
42 const string DRV_INFO_BUS = "bus";
43 const string DRV_INFO_VENDOR = "vendor";
44 const string DRV_INFO_VERSION = "version";
45 
DrvBundleStateCallback()46 DrvBundleStateCallback::DrvBundleStateCallback()
47 {
48     stiching.clear();
49     stiching += "########";
50 
51     if (GetAllDriverInfos()) {
52         EDM_LOGI(MODULE_PKG_MGR, "GetAllDriverInfos in DrvBundleStateCallback OK");
53     } else {
54         EDM_LOGE(MODULE_PKG_MGR, "GetAllDriverInfos in DrvBundleStateCallback ERR");
55     }
56 };
57 
~DrvBundleStateCallback()58 DrvBundleStateCallback::~DrvBundleStateCallback()
59 {
60     return;
61 };
62 
PrintTest()63 void DrvBundleStateCallback::PrintTest()
64 {
65     std::vector<std::string> allBundleAbilityNames;
66     std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
67     int32_t ret = helper->QueryAllSize(allBundleAbilityNames);
68     if (ret <= 0) {
69         EDM_LOGE(MODULE_PKG_MGR, "QueryAllSize failed");
70         return;
71     }
72     cout << "allBundleAbilityNames_ size = " << allBundleAbilityNames.size() << endl;
73 }
74 
OnBundleStateChanged(const uint8_t installType,const int32_t resultCode,const std::string & resultMsg,const std::string & bundleName)75 void DrvBundleStateCallback::OnBundleStateChanged(const uint8_t installType, const int32_t resultCode,
76     const std::string &resultMsg, const std::string &bundleName)
77 {
78     EDM_LOGI(MODULE_PKG_MGR, "OnBundleStateChanged");
79     return;
80 };
81 
82 /**
83     * @brief Called when a new application package has been installed on the device.
84     * @param bundleName Indicates the name of the bundle whose state has been installed.
85     * @param userId Indicates the id of the bundle whose state has been installed.
86     */
OnBundleAdded(const std::string & bundleName,const int userId)87 void DrvBundleStateCallback::OnBundleAdded(const std::string &bundleName, const int userId)
88 {
89     EDM_LOGI(MODULE_PKG_MGR, "OnBundleAdded");
90     StartTrace(LABEL, "OnBundleAdded");
91     if (QueryExtensionAbilityInfos(bundleName, userId) != ERR_OK) {
92         return;
93     }
94 
95     if (ParseBaseDriverInfo()) {
96         EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error");
97     }
98     FinishTrace(LABEL);
99 }
100 /**
101     * @brief Called when a new application package has been Updated on the device.
102     * @param bundleName Indicates the name of the bundle whose state has been Updated.
103     * @param userId Indicates the id of the bundle whose state has been Updated.
104     */
OnBundleUpdated(const std::string & bundleName,const int userId)105 void DrvBundleStateCallback::OnBundleUpdated(const std::string &bundleName, const int userId)
106 {
107     EDM_LOGI(MODULE_PKG_MGR, "OnBundleUpdated");
108     StartTrace(LABEL, "OnBundleUpdated");
109     if (QueryExtensionAbilityInfos(bundleName, userId) != ERR_OK) {
110         return;
111     }
112 
113     if (ParseBaseDriverInfo()) {
114         EDM_LOGE(MODULE_PKG_MGR, "OnBundleUpdated error");
115     }
116     FinishTrace(LABEL);
117 }
118 
119 /**
120     * @brief Called when a new application package has been Removed on the device.
121     * @param bundleName Indicates the name of the bundle whose state has been Removed.
122     * @param userId Indicates the id of the bundle whose state has been Removed.
123     */
OnBundleRemoved(const std::string & bundleName,const int userId)124 void DrvBundleStateCallback::OnBundleRemoved(const std::string &bundleName, const int userId)
125 {
126     EDM_LOGI(MODULE_PKG_MGR, "OnBundleRemoved");
127     StartTrace(LABEL, "OnBundleRemoved");
128     OnBundleDrvRemoved(bundleName);
129     FinishTrace(LABEL);
130 }
131 
AsObject()132 sptr<IRemoteObject> DrvBundleStateCallback::AsObject()
133 {
134     return nullptr;
135 }
136 
GetAllDriverInfos()137 bool DrvBundleStateCallback::GetAllDriverInfos()
138 {
139     if (initOnce) {
140         return true;
141     }
142 
143     // query history bundle
144     auto iBundleMgr = GetBundleMgrProxy();
145     if (iBundleMgr == nullptr) {
146         EDM_LOGE(MODULE_PKG_MGR, "Can not get iBundleMgr");
147         return false;
148     }
149     std::vector<BundleInfo> bundleInfos;
150 
151     int32_t userId = GetCurrentActiveUserId();
152     int32_t flags = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY) + \
153                     static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA);
154     if (!(iBundleMgr->GetBundleInfos(flags, bundleInfos, userId))) {
155         EDM_LOGE(MODULE_PKG_MGR, "GetBundleInfos err");
156         return false;
157     }
158 
159     StorageHistoryDrvInfo(bundleInfos);
160     initOnce = true;
161     return true;
162 }
163 
GetStiching()164 string DrvBundleStateCallback::GetStiching()
165 {
166     return stiching;
167 }
168 
CheckBundleMgrProxyPermission()169 bool DrvBundleStateCallback::CheckBundleMgrProxyPermission()
170 {
171     // check permission
172     auto iBundleMgr = GetBundleMgrProxy();
173     if (iBundleMgr == nullptr) {
174         EDM_LOGE(MODULE_PKG_MGR, "Can not get iBundleMgr");
175         return false;
176     }
177     if (!iBundleMgr->VerifySystemApi(Constants::INVALID_API_VERSION)) {
178         EDM_LOGE(MODULE_PKG_MGR, "non-system app calling system api");
179         return false;
180     }
181     if (!iBundleMgr->VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
182         EDM_LOGE(MODULE_PKG_MGR, "register bundle status callback failed due to lack of permission");
183         return false;
184     }
185     return true;
186 }
187 
QueryExtensionAbilityInfos(const std::string & bundleName,const int userId)188 ErrCode DrvBundleStateCallback::QueryExtensionAbilityInfos(const std::string &bundleName, const int userId)
189 {
190     // clear ExtensionAbilityInfos vector
191     extensionInfos_.clear();
192 
193     if (bundleName.empty()) {
194         EDM_LOGE(MODULE_PKG_MGR, "BundleName empty");
195         return ERR_DRV_STATUS_CALLBACK_ERROR;
196     }
197 
198     if (bundleMgr_ == nullptr) {
199         EDM_LOGE(MODULE_PKG_MGR, "BundleMgr_ nullptr");
200         return ERR_DRV_STATUS_CALLBACK_ERROR;
201     }
202 
203     BundleInfo tmpBundleInfo;
204     int32_t flags = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY) + \
205                     static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA);
206     if (!(bundleMgr_->GetBundleInfo(bundleName, flags, tmpBundleInfo, userId))) {
207         EDM_LOGE(MODULE_PKG_MGR, "GetBundleInfo err");
208         return ERR_DRV_STATUS_CALLBACK_ERROR;
209     }
210 
211     extensionInfos_ = tmpBundleInfo.extensionInfos;
212     if (extensionInfos_.empty()) {
213         EDM_LOGE(MODULE_PKG_MGR, "GetBundleInfo extensionInfos_ empty");
214         return ERR_DRV_STATUS_CALLBACK_ERROR;
215     }
216 
217     return ERR_OK;
218 }
219 
ChangeValue(DriverInfo & tmpDrvInfo,std::vector<Metadata> & metadata)220 void DrvBundleStateCallback::ChangeValue(DriverInfo &tmpDrvInfo, std::vector<Metadata> &metadata)
221 {
222     for (auto data : metadata) {
223         if (data.name == DRV_INFO_BUS) {
224             tmpDrvInfo.bus_ = data.value;
225         }
226         if (data.name == DRV_INFO_VENDOR) {
227             tmpDrvInfo.vendor_ = data.value;
228         }
229         if (data.name == DRV_INFO_VERSION) {
230             tmpDrvInfo.version_ = data.value;
231         }
232     }
233 }
234 
ClearDriverInfo(DriverInfo & tmpDrvInfo)235 void DrvBundleStateCallback::ClearDriverInfo(DriverInfo &tmpDrvInfo)
236 {
237     tmpDrvInfo.bus_.clear();
238     tmpDrvInfo.vendor_.clear();
239     tmpDrvInfo.version_.clear();
240     tmpDrvInfo.driverInfoExt_ = nullptr;
241 }
242 
ParseExtensionInfos(ExtensionAbilityType & type,string & bundleName,string & abilityName)243 void DrvBundleStateCallback::ParseExtensionInfos(ExtensionAbilityType &type,
244     string &bundleName, string &abilityName)
245 {
246     type = extensionInfos_.back().type;
247     bundleName = extensionInfos_.back().bundleName;
248     abilityName = extensionInfos_.back().name;
249 }
250 
ParseBaseDriverInfo()251 bool DrvBundleStateCallback::ParseBaseDriverInfo()
252 {
253     shared_ptr<IBusExtension> extInstance = nullptr;
254     DriverInfo tmpDrvInfo;
255     ExtensionAbilityType type;
256     std::vector<Metadata> metadata;
257     string bundleName;
258     string abilityName;
259     bool ret = false;
260 
261     // parase infos to innerDrvInfos_
262     while (!extensionInfos_.empty()) {
263         ClearDriverInfo(tmpDrvInfo);
264         ParseExtensionInfos(type, bundleName, abilityName);
265 
266         metadata = extensionInfos_.back().metadata;
267         extensionInfos_.pop_back();
268 
269         if ((type != ExtensionAbilityType::DRIVER) || metadata.empty()) {
270             continue;
271         }
272 
273         ChangeValue(tmpDrvInfo, metadata);
274 
275         extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(tmpDrvInfo.GetBusName());
276         if (extInstance == nullptr) {
277             EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetInstance at bus:%{public}s", tmpDrvInfo.bus_.c_str());
278             continue;
279         }
280 
281         tmpDrvInfo.driverInfoExt_ = extInstance->ParseDriverInfo(metadata);
282         if (tmpDrvInfo.driverInfoExt_ == nullptr) {
283             EDM_LOGE(MODULE_PKG_MGR, "ParseDriverInfo null");
284             continue;
285         }
286         string driverInfo;
287         tmpDrvInfo.Serialize(driverInfo);
288 
289         string tempbundleName = bundleName;
290         bundleName += stiching + abilityName;
291 
292         std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
293         bool flag = true;
294         helper->CheckIfNeedUpdateEx(flag, bundleName);
295         int32_t addOrUpdate = helper->AddOrUpdateRightRecord(tempbundleName, bundleName, driverInfo);
296         if (addOrUpdate < 0) {
297             EDM_LOGE(MODULE_PKG_MGR, "add or update failed: %{public}s, addOrUpdate=%{public}d",
298                 bundleName.c_str(), addOrUpdate);
299             continue;
300         }
301 
302         if (m_pFun != nullptr) {
303             m_pFun(flag ? BUNDLE_UPDATED : BUNDLE_ADDED, BusType::BUS_TYPE_USB, tempbundleName, abilityName);
304         }
305         ret = true;
306     }
307     return ret;
308 }
309 
GetCurrentActiveUserId()310 int32_t DrvBundleStateCallback::GetCurrentActiveUserId()
311 {
312     std::vector<int32_t> activeIds;
313     int ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds);
314     if (ret != 0) {
315         EDM_LOGE(MODULE_PKG_MGR, "QueryActiveOsAccountIds failed ret:%{public}d", ret);
316         return Constants::INVALID_USERID;
317     }
318     if (activeIds.empty()) {
319         EDM_LOGE(MODULE_PKG_MGR, "QueryActiveOsAccountIds activeIds empty");
320         return Constants::ALL_USERID;
321     }
322     return activeIds[0];
323 }
324 
GetBundleMgrProxy()325 sptr<OHOS::AppExecFwk::IBundleMgr> DrvBundleStateCallback::GetBundleMgrProxy()
326 {
327     if (bundleMgr_ == nullptr) {
328         std::lock_guard<std::mutex> lock(bundleMgrMutex_);
329         if (bundleMgr_ == nullptr) {
330             auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
331             if (systemAbilityManager == nullptr) {
332                 EDM_LOGE(MODULE_PKG_MGR, "GetBundleMgr GetSystemAbilityManager is null");
333                 return nullptr;
334             }
335             auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
336             if (bundleMgrSa == nullptr) {
337                 EDM_LOGE(MODULE_PKG_MGR, "GetBundleMgr GetSystemAbility is null");
338                 return nullptr;
339             }
340             auto bundleMgr = OHOS::iface_cast<IBundleMgr>(bundleMgrSa);
341             if (bundleMgr == nullptr) {
342                 EDM_LOGE(MODULE_PKG_MGR, "GetBundleMgr iface_cast get null");
343             }
344             bundleMgr_ = bundleMgr;
345         }
346     }
347     return bundleMgr_;
348 }
349 
StorageHistoryDrvInfo(std::vector<BundleInfo> & bundleInfos)350 void DrvBundleStateCallback::StorageHistoryDrvInfo(std::vector<BundleInfo> &bundleInfos)
351 {
352     while (!bundleInfos.empty()) {
353         extensionInfos_.clear();
354         extensionInfos_ = bundleInfos.back().extensionInfos;
355         bundleInfos.pop_back();
356         if (extensionInfos_.empty()) {
357             continue;
358         }
359 
360         if (ParseBaseDriverInfo()) {
361             EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error");
362         }
363     }
364 }
365 
OnBundleDrvRemoved(const std::string & bundleName)366 void DrvBundleStateCallback::OnBundleDrvRemoved(const std::string &bundleName)
367 {
368     std::vector<std::string> bundleAbilityNames;
369     std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
370     int32_t retRdb = helper->QueryAllBundleAbilityNames(bundleName, bundleAbilityNames);
371     if (retRdb <= 0) {
372         EDM_LOGE(MODULE_PKG_MGR, "QueryAllBundleAbilityNames failed: %{public}s", bundleName.c_str());
373         return;
374     }
375     int32_t totalNames = static_cast<int32_t>(bundleAbilityNames.size());
376     EDM_LOGE(MODULE_PKG_MGR, "totalbundleAbilityNames: %{public}d", totalNames);
377     for (int32_t i = 0; i < totalNames; i++) {
378         if (m_pFun != nullptr) {
379             std::string bundleAbilityName = bundleAbilityNames.at(i);
380             std::string bundleName = bundleAbilityName.substr(0, bundleAbilityName.find_first_of(GetStiching()));
381             std::string abilityName = bundleAbilityName.substr(bundleAbilityName.find_last_of(GetStiching()) + 1);
382             EDM_LOGI(MODULE_PKG_MGR, "bundleName: %{public}s abilityName: %{public}s",
383                 bundleName.c_str(), abilityName.c_str());
384             m_pFun(BUNDLE_REMOVED, BusType::BUS_TYPE_USB, bundleName, abilityName);
385         }
386     }
387 
388     int32_t ret = helper->DeleteRightRecord(bundleName);
389     if (ret < 0) {
390         EDM_LOGE(MODULE_PKG_MGR, "delete failed: %{public}s", bundleName.c_str());
391         return;
392     }
393 }
394 }
395 }