• 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 <element_name.h>
17 #include <want.h>
18 
19 #include "hdf_log.h"
20 #include "hilog_wrapper.h"
21 #include "matching_skills.h"
22 #include "usb_bus_extension.h"
23 #include "common_event_support.h"
24 #include "common_event_subscribe_info.h"
25 #include "bus_extension_core.h"
26 #include "pkg_db_helper.h"
27 #include "driver_pkg_manager.h"
28 
29 namespace OHOS {
30 namespace ExternalDeviceManager {
31 using namespace std;
32 using namespace OHOS;
33 using namespace OHOS::AAFwk;
34 using namespace OHOS::AppExecFwk;
35 using namespace OHOS::ExternalDeviceManager;
36 
37 IMPLEMENT_SINGLE_INSTANCE(DriverPkgManager);
38 
DriverPkgManager()39 DriverPkgManager::DriverPkgManager()
40 {
41 };
42 
~DriverPkgManager()43 DriverPkgManager::~DriverPkgManager()
44 {
45     if (UnRegisterCallback() == 0) {
46         EDM_LOGE(MODULE_PKG_MGR, "~DriverPkgManager UnRegisterCallback Fail");
47     }
48     bundleStateCallback_ = nullptr;
49 }
50 
PrintTest()51 void DriverPkgManager::PrintTest()
52 {
53     bundleStateCallback_->PrintTest();
54 }
55 
Init()56 int32_t DriverPkgManager::Init()
57 {
58     EventFwk::MatchingSkills matchingSkills;
59     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
60     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
61     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
62     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
63     bundleMonitor_ = std::make_shared<BundleMonitor>(subscribeInfo);
64 
65     if (bundleMonitor_ == nullptr) {
66         EDM_LOGE(MODULE_PKG_MGR, "bundleMonitor_ new Err");
67         return EDM_ERR_INVALID_OBJECT;
68     }
69 
70     bundleStateCallback_ = new DrvBundleStateCallback();
71 
72     if (bundleStateCallback_ == nullptr) {
73         EDM_LOGE(MODULE_PKG_MGR, "bundleStateCallback_ new Err");
74         return EDM_ERR_INVALID_OBJECT;
75     }
76 
77     if (!bundleStateCallback_->GetAllDriverInfos()) {
78         EDM_LOGE(MODULE_PKG_MGR, "bundleStateCallback_ GetAllDriverInfos Err");
79         return EDM_ERR_NOT_SUPPORT;
80     }
81     // register calback to BMS
82     return RegisterCallback(bundleStateCallback_);
83 }
84 
QueryMatchDriver(shared_ptr<DeviceInfo> devInfo)85 shared_ptr<BundleInfoNames> DriverPkgManager::QueryMatchDriver(shared_ptr<DeviceInfo> devInfo)
86 {
87     EDM_LOGI(MODULE_PKG_MGR, "Enter QueryMatchDriver");
88     shared_ptr<IBusExtension> extInstance = nullptr;
89     auto ret = make_shared<BundleInfoNames>();
90     ret->bundleName.clear();
91     ret->abilityName.clear();
92     if (bundleStateCallback_ == nullptr) {
93         EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver bundleStateCallback_ null");
94         return nullptr;
95     }
96 
97     if (!bundleStateCallback_->GetAllDriverInfos()) {
98         EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetAllDriverInfos Err");
99         return nullptr;
100     }
101 
102     std::vector<std::string> apps;
103     std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
104     int32_t retRdb = helper->QueryAllDriverInfos(apps);
105     if (retRdb <= 0) {
106         /* error or empty record */
107         return nullptr;
108     }
109     int32_t totalApps = static_cast<int32_t>(apps.size());
110     EDM_LOGE(MODULE_PKG_MGR, "totalApps: %{public}d", totalApps);
111     for (int32_t i = 0; i < totalApps; i++) {
112         std::string app = apps.at(i);
113         DriverInfo driverInfo;
114         driverInfo.UnSerialize(app);
115 
116         extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(driverInfo.GetBusName());
117         if (extInstance == nullptr) {
118             return nullptr;
119         }
120 
121         if (extInstance->MatchDriver(driverInfo, *devInfo)) {
122             std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
123 
124             string bundleName = helper->QueryBundleInfoNames(app);
125             ret->bundleName = bundleName.substr(0, bundleName.find_first_of(bundleStateCallback_->GetStiching()));
126             ret->abilityName = bundleName.substr(bundleName.find_last_of(bundleStateCallback_->GetStiching()) + 1);
127             return ret;
128         }
129     }
130 
131     EDM_LOGI(MODULE_PKG_MGR, "QueryMatchDriver return null");
132     return nullptr;
133 }
134 
RegisterCallback(const sptr<IBundleStatusCallback> & callback)135 int32_t DriverPkgManager::RegisterCallback(const sptr<IBundleStatusCallback> &callback)
136 {
137     EDM_LOGI(MODULE_PKG_MGR, "RegisterCallback called");
138     if (bundleStateCallback_ == nullptr) {
139         EDM_LOGE(MODULE_PKG_MGR, "failed to register callback, bundleStateCallback_ is null");
140         return EDM_ERR_INVALID_OBJECT;
141     }
142 
143     if (!bundleStateCallback_->CheckBundleMgrProxyPermission()) {
144         EDM_LOGE(MODULE_PKG_MGR, "failed to register callback, Permission check is false");
145         return EDM_ERR_NOT_SUPPORT;
146     }
147 
148     if (bundleMonitor_ == nullptr) {
149         EDM_LOGE(MODULE_PKG_MGR, "failed to register callback, bundleMonitor_ is null");
150         return EDM_ERR_INVALID_OBJECT;
151     }
152 
153     if ((int32_t)(bundleMonitor_->Subscribe(callback)) == 1) {
154         return EDM_OK;
155     }
156 
157     return EDM_NOK;
158 }
159 
UnRegisterCallback()160 int32_t DriverPkgManager::UnRegisterCallback()
161 {
162     EDM_LOGI(MODULE_PKG_MGR, "UnRegisterCallback called");
163     if (bundleStateCallback_ == nullptr) {
164         EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, bundleStateCallback_ is null");
165         return EDM_ERR_INVALID_OBJECT;
166     }
167 
168     if (!bundleStateCallback_->CheckBundleMgrProxyPermission()) {
169         EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, Permission check is false");
170         return EDM_ERR_NOT_SUPPORT;
171     }
172 
173     if (bundleMonitor_ == nullptr) {
174         EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, bundleMonitor is null");
175         return EDM_ERR_INVALID_OBJECT;
176     }
177 
178     if ((int32_t)(bundleMonitor_->UnSubscribe()) == 1) {
179         return EDM_OK;
180     }
181 
182     return EDM_NOK;
183 }
184 
RegisterOnBundleUpdate(PCALLBACKFUN pFun)185 int32_t DriverPkgManager::RegisterOnBundleUpdate(PCALLBACKFUN pFun)
186 {
187     if (pFun == nullptr) {
188         return EDM_ERR_INVALID_OBJECT;
189     }
190 
191     if (bundleStateCallback_ == nullptr) {
192         EDM_LOGE(MODULE_PKG_MGR, "failed to register callback, bundleStateCallback_ is null");
193         return EDM_ERR_INVALID_OBJECT;
194     }
195     bundleStateCallback_->m_pFun = pFun;
196     return EDM_OK;
197 }
198 
UnRegisterOnBundleUpdate()199 int32_t DriverPkgManager::UnRegisterOnBundleUpdate()
200 {
201     if (bundleStateCallback_ == nullptr) {
202         EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, bundleStateCallback_ is null");
203         return EDM_ERR_INVALID_OBJECT;
204     }
205     bundleStateCallback_->m_pFun = nullptr;
206     return EDM_OK;
207 }
208 } // namespace ExternalDeviceManager
209 } // namespace OHOS