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