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 "driver_pkg_manager.h"
27
28 namespace OHOS {
29 namespace ExternalDeviceManager {
30 using namespace std;
31 using namespace OHOS;
32 using namespace OHOS::AAFwk;
33 using namespace OHOS::AppExecFwk;
34 using namespace OHOS::ExternalDeviceManager;
35
36 IMPLEMENT_SINGLE_INSTANCE(DriverPkgManager);
37
DriverPkgManager()38 DriverPkgManager::DriverPkgManager()
39 {
40 };
41
~DriverPkgManager()42 DriverPkgManager::~DriverPkgManager()
43 {
44 if (UnRegisterCallback() == 0) {
45 EDM_LOGE(MODULE_PKG_MGR, "~DriverPkgManager UnRegisterCallback Fail");
46 }
47 delete bundleStateCallback_;
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 std::map<string, DriverInfo> drvInfos_;
78 if (!bundleStateCallback_->GetAllDriverInfos(drvInfos_)) {
79 EDM_LOGE(MODULE_PKG_MGR, "bundleStateCallback_ GetAllDriverInfos Err");
80 return EDM_ERR_NOT_SUPPORT;
81 }
82 // register calback to BMS
83 return RegisterCallback(bundleStateCallback_);
84 }
85
QueryMatchDriver(shared_ptr<DeviceInfo> devInfo)86 shared_ptr<BundleInfoNames> DriverPkgManager::QueryMatchDriver(shared_ptr<DeviceInfo> devInfo)
87 {
88 EDM_LOGI(MODULE_PKG_MGR, "Enter QueryMatchDriver");
89 shared_ptr<IBusExtension> extInstance = nullptr;
90 auto ret = make_shared<BundleInfoNames>();
91 ret->bundleName.clear();
92 ret->abilityName.clear();
93
94 if (bundleStateCallback_ == nullptr) {
95 EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver bundleStateCallback_ null");
96 return nullptr;
97 }
98
99 std::map<string, DriverInfo> drvInfos_;
100 if (!bundleStateCallback_->GetAllDriverInfos(drvInfos_)) {
101 EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetAllDriverInfos Err");
102 return nullptr;
103 }
104
105 if (drvInfos_.empty()) {
106 EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver drvInfos_ Empty");
107 return nullptr;
108 }
109
110 for (auto [key, val] : drvInfos_) {
111 extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(val.GetBusName());
112 if (extInstance == nullptr) {
113 EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetInstance at bus:%{public}s", val.GetBusName().c_str());
114 continue;
115 }
116
117 if (extInstance->MatchDriver(val, *devInfo)) {
118 string bundleName = key;
119 ret->bundleName = bundleName.substr(0, bundleName.find_first_of(bundleStateCallback_->GetStiching()));
120 ret->abilityName = bundleName.substr(bundleName.find_last_of(bundleStateCallback_->GetStiching()) + 1);
121 return ret;
122 } else {
123 string drvInfoStr;
124 val.Serialize(drvInfoStr);
125 EDM_LOGI(MODULE_PKG_MGR, "PKG Serialize:%{public}s", drvInfoStr.c_str());
126 }
127 }
128
129 EDM_LOGI(MODULE_PKG_MGR, "QueryMatchDriver return null");
130 return nullptr;
131 }
132
RegisterCallback(const sptr<IBundleStatusCallback> & callback)133 int32_t DriverPkgManager::RegisterCallback(const sptr<IBundleStatusCallback> &callback)
134 {
135 EDM_LOGI(MODULE_PKG_MGR, "RegisterCallback called");
136 if (bundleStateCallback_ == nullptr) {
137 EDM_LOGE(MODULE_PKG_MGR, "failed to register callback, bundleStateCallback_ is null");
138 return EDM_ERR_INVALID_OBJECT;
139 }
140
141 if (!bundleStateCallback_->CheckBundleMgrProxyPermission()) {
142 EDM_LOGE(MODULE_PKG_MGR, "failed to register callback, Permission check is false");
143 return EDM_ERR_NOT_SUPPORT;
144 }
145
146 if (bundleMonitor_ == nullptr) {
147 EDM_LOGE(MODULE_PKG_MGR, "failed to register callback, bundleMonitor_ is null");
148 return EDM_ERR_INVALID_OBJECT;
149 }
150
151 if ((int32_t)(bundleMonitor_->Subscribe(callback)) == 1) {
152 return EDM_OK;
153 }
154
155 return EDM_NOK;
156 }
157
UnRegisterCallback()158 int32_t DriverPkgManager::UnRegisterCallback()
159 {
160 EDM_LOGI(MODULE_PKG_MGR, "UnRegisterCallback called");
161 if (bundleStateCallback_ == nullptr) {
162 EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, bundleStateCallback_ is null");
163 return EDM_ERR_INVALID_OBJECT;
164 }
165
166 if (!bundleStateCallback_->CheckBundleMgrProxyPermission()) {
167 EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, Permission check is false");
168 return EDM_ERR_NOT_SUPPORT;
169 }
170
171 if (bundleMonitor_ == nullptr) {
172 EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, bundleMonitor is null");
173 return EDM_ERR_INVALID_OBJECT;
174 }
175
176 if ((int32_t)(bundleMonitor_->UnSubscribe()) == 1) {
177 return EDM_OK;
178 }
179
180 return EDM_NOK;
181 }
182
RegisterOnBundleUpdate(PCALLBACKFUN pFun)183 int32_t DriverPkgManager::RegisterOnBundleUpdate(PCALLBACKFUN pFun)
184 {
185 if (pFun == nullptr) {
186 return EDM_ERR_INVALID_OBJECT;
187 }
188
189 if (bundleStateCallback_ == nullptr) {
190 EDM_LOGE(MODULE_PKG_MGR, "failed to register callback, bundleStateCallback_ is null");
191 return EDM_ERR_INVALID_OBJECT;
192 }
193 bundleStateCallback_->m_pFun = pFun;
194 return EDM_OK;
195 }
196
UnRegisterOnBundleUpdate()197 int32_t DriverPkgManager::UnRegisterOnBundleUpdate()
198 {
199 if (bundleStateCallback_ == nullptr) {
200 EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, bundleStateCallback_ is null");
201 return EDM_ERR_INVALID_OBJECT;
202 }
203 bundleStateCallback_->m_pFun = nullptr;
204 return EDM_OK;
205 }
206 } // namespace ExternalDeviceManager
207 } // namespace OHOS