1 /*
2 * Copyright (c) 2023-2025 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 "common_event_support.h"
23 #include "common_event_subscribe_info.h"
24 #include "bus_extension_core.h"
25 #include "pkg_db_helper.h"
26 #include "driver_pkg_manager.h"
27 #include "driver_os_account_subscriber.h"
28 #include "os_account_manager.h"
29 #include "driver_report_sys_event.h"
30
31 namespace OHOS {
32 namespace ExternalDeviceManager {
33 using namespace std;
34 using namespace OHOS;
35 using namespace OHOS::AAFwk;
36 using namespace OHOS::AppExecFwk;
37 using namespace OHOS::ExternalDeviceManager;
38 using namespace OHOS::AccountSA;
39
40 static constexpr const char *ACCOUNT_SWITCHING_SUBSCRIBE_NAME = "DRIVER_ACCOUNT_SWITCHING_SUBSCRIBE";
41 static constexpr const char *ACCOUNT_SWITCHED_SUBSCRIBE_NAME = "DRIVER_ACCOUNT_SWITCHED_SUBSCRIBE";
42
43 IMPLEMENT_SINGLE_INSTANCE(DriverPkgManager);
44
DriverPkgManager()45 DriverPkgManager::DriverPkgManager()
46 {
47 };
48
~DriverPkgManager()49 DriverPkgManager::~DriverPkgManager()
50 {
51 if (UnRegisterBundleStatusCallback() == 0) {
52 EDM_LOGE(MODULE_PKG_MGR, "~DriverPkgManager UnRegisterBundleStatusCallback Fail");
53 }
54 bundleStateCallback_ = nullptr;
55 }
56
PrintTest()57 void DriverPkgManager::PrintTest()
58 {
59 bundleStateCallback_->PrintTest();
60 }
61
Init()62 int32_t DriverPkgManager::Init()
63 {
64 bundleStateCallback_ = new DrvBundleStateCallback();
65 if (bundleStateCallback_ == nullptr) {
66 EDM_LOGE(MODULE_PKG_MGR, "bundleStateCallback_ new Err");
67 return EDM_ERR_INVALID_OBJECT;
68 }
69
70 bundleStateCallback_->GetAllDriverInfos();
71 return EDM_OK;
72 }
73
Init(shared_future<int32_t> bmsFuture,shared_future<int32_t> accountFuture,shared_future<int32_t> commEventFuture)74 int32_t DriverPkgManager::Init(shared_future<int32_t> bmsFuture, shared_future<int32_t> accountFuture,
75 shared_future<int32_t> commEventFuture)
76 {
77 bmsFuture_ = bmsFuture;
78 accountFuture_ = accountFuture;
79 commEventFuture_ = commEventFuture;
80
81 bundleStateCallback_ = new DrvBundleStateCallback(bmsFuture, accountFuture, commEventFuture);
82 if (bundleStateCallback_ == nullptr) {
83 EDM_LOGE(MODULE_PKG_MGR, "bundleStateCallback_ new Err");
84 return EDM_ERR_INVALID_OBJECT;
85 }
86
87 bundleStateCallback_->GetAllDriverInfosAsync();
88 return EDM_OK;
89 }
90
SubscribeOsAccountSwitch()91 bool DriverPkgManager::SubscribeOsAccountSwitch()
92 {
93 EDM_LOGI(MODULE_PKG_MGR, "SubscribeOsAccountSwitch start");
94 OsAccountSubscribeInfo switchingSubscribeInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHING,
95 ACCOUNT_SWITCHING_SUBSCRIBE_NAME);
96 OsAccountSubscribeInfo switchedSubscribeInfo(OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHED, ACCOUNT_SWITCHED_SUBSCRIBE_NAME);
97 shared_ptr<DriverOsAccountSwitching> driverOsAccountSwitching
98 = make_shared<DriverOsAccountSwitching>(switchingSubscribeInfo, bundleStateCallback_);
99 shared_ptr<DriverOsAccountSwitched> driverOsAccountSwitched
100 = make_shared<DriverOsAccountSwitched>(switchedSubscribeInfo, bundleStateCallback_);
101 auto retCode = OsAccountManager::SubscribeOsAccount(driverOsAccountSwitching);
102 if (retCode != ERR_OK) {
103 EDM_LOGE(MODULE_PKG_MGR, "SubscribeOsAccount Switching fail, retCode=%{public}d", retCode);
104 return false;
105 }
106 retCode = OsAccountManager::SubscribeOsAccount(driverOsAccountSwitched);
107 if (retCode != ERR_OK) {
108 EDM_LOGE(MODULE_PKG_MGR, "SubscribeOsAccount Switched fail, retCode=%{public}d", retCode);
109 return false;
110 }
111 return true;
112 }
113
QueryMatchDriver(shared_ptr<DeviceInfo> devInfo,const std::string & type)114 shared_ptr<DriverInfo> DriverPkgManager::QueryMatchDriver(shared_ptr<DeviceInfo> devInfo, const std::string &type)
115 {
116 EDM_LOGI(MODULE_PKG_MGR, "Enter QueryMatchDriver %{public}s", type.c_str());
117 if (bundleStateCallback_ == nullptr) {
118 EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver bundleStateCallback_ null");
119 return nullptr;
120 }
121
122 if (!bundleStateCallback_->GetAllDriverInfos()) {
123 EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetAllDriverInfos Err");
124 return nullptr;
125 }
126 auto extDevEvent = std::make_shared<ExtDevEvent>(__func__, DRIVER_DEVICE_MATCH);
127 ExtDevReportSysEvent::ParseToExtDevEvent(devInfo, extDevEvent);
128 std::vector<PkgInfoTable> pkgInfos;
129 std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
130 int32_t retRdb = helper->QueryPkgInfos(pkgInfos);
131 if (retRdb < 0) {
132 EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver QueryPkgInfos failed");
133 ExtDevReportSysEvent::ReportExternalDeviceEvent(extDevEvent,
134 ExtDevReportSysEvent::EventErrCode::QUERY_DRIVER_INFO_FAILED);
135 return nullptr;
136 } else if (retRdb == 0) {
137 EDM_LOGD(MODULE_PKG_MGR, "QueryMatchDriver no driver installed");
138 ExtDevReportSysEvent::ReportExternalDeviceEvent(extDevEvent,
139 ExtDevReportSysEvent::EventErrCode::NO_MATCHING_DRIVER_FOUND);
140 return nullptr;
141 }
142 EDM_LOGI(MODULE_PKG_MGR, "Total driverInfos number: %{public}zu", pkgInfos.size());
143 shared_ptr<IBusExtension> extInstance = nullptr;
144 for (const auto &pkgInfo : pkgInfos) {
145 DriverInfo driverInfo(pkgInfo.bundleName, pkgInfo.driverName, pkgInfo.driverUid, pkgInfo.userId);
146 driverInfo.UnSerialize(pkgInfo.driverInfo);
147 extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(driverInfo.GetBusName());
148 if (extInstance != nullptr && extInstance->MatchDriver(driverInfo, *devInfo, type)) {
149 std::shared_ptr<DriverInfo> driverPtr = std::make_shared<DriverInfo>(driverInfo);
150 ExtDevReportSysEvent::ParseToExtDevEvent(driverPtr, extDevEvent);
151 ExtDevReportSysEvent::ReportExternalDeviceEvent(extDevEvent, ExtDevReportSysEvent::EventErrCode::SUCCESS);
152 return driverPtr;
153 }
154 }
155 EDM_LOGI(MODULE_PKG_MGR, "QueryMatchDriver return null");
156 ExtDevReportSysEvent::ReportExternalDeviceEvent(extDevEvent,
157 ExtDevReportSysEvent::EventErrCode::NO_MATCHING_DRIVER_FOUND);
158 return nullptr;
159 }
160
QueryDriverInfo(vector<shared_ptr<DriverInfo>> & driverInfos,bool isByDriverUid,const std::string & driverUid)161 int32_t DriverPkgManager::QueryDriverInfo(vector<shared_ptr<DriverInfo>> &driverInfos,
162 bool isByDriverUid, const std::string &driverUid)
163 {
164 EDM_LOGD(MODULE_PKG_MGR, "DriverPkgManager::QueryDriverInfo enter");
165 if (bundleStateCallback_ == nullptr) {
166 EDM_LOGE(MODULE_PKG_MGR, "QueryDriverInfo bundleStateCallback_ null");
167 return EDM_NOK;
168 }
169
170 if (!bundleStateCallback_->GetAllDriverInfos()) {
171 EDM_LOGE(MODULE_PKG_MGR, "QueryDriverInfo GetAllDriverInfos Err");
172 return EDM_NOK;
173 }
174
175 std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
176 std::vector<PkgInfoTable> pkgInfos;
177 EDM_LOGD(MODULE_PKG_MGR, "pkg QueryDriverInfo driverUid: %{public}s", driverUid.c_str());
178 int32_t pkgSize = helper->QueryPkgInfos(pkgInfos, isByDriverUid, driverUid);
179 if (pkgSize < 0) {
180 return EDM_NOK;
181 }
182 for (const auto &pkgInfo : pkgInfos) {
183 std::shared_ptr<DriverInfo> driverInfo
184 = std::make_shared<DriverInfo>(pkgInfo.bundleName, pkgInfo.driverName, pkgInfo.driverUid, pkgInfo.userId);
185 if (driverInfo->UnSerialize(pkgInfo.driverInfo) != EDM_OK) {
186 return EDM_NOK;
187 }
188 driverInfos.push_back(driverInfo);
189 }
190 EDM_LOGD(MODULE_PKG_MGR, "DriverPkgManager::QueryDriverInfo driverInfos size:%{public}zu", driverInfos.size());
191 return EDM_OK;
192 }
193
RegisterBundleStatusCallback()194 int32_t DriverPkgManager::RegisterBundleStatusCallback()
195 {
196 EDM_LOGI(MODULE_PKG_MGR, "RegisterBundleStatusCallback start");
197 if (bundleStateCallback_ == nullptr) {
198 EDM_LOGE(MODULE_PKG_MGR, "RegisterBundleStatusCallback failed, bundleStateCallback_ is null");
199 return EDM_ERR_INVALID_OBJECT;
200 }
201 EventFwk::MatchingSkills matchingSkills;
202 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
203 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
204 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
205 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
206 bundleMonitor_ = std::make_shared<BundleMonitor>(subscribeInfo);
207
208 if (bundleMonitor_ == nullptr) {
209 EDM_LOGE(MODULE_PKG_MGR, "RegisterBundleStatusCallback failed, bundleMonitor_ is null");
210 return EDM_ERR_INVALID_OBJECT;
211 }
212
213 if (!bundleMonitor_->Subscribe(bundleStateCallback_)) {
214 EDM_LOGE(MODULE_PKG_MGR, "Failed to subscribe bundleMonitor callback");
215 return EDM_NOK;
216 }
217
218 return EDM_OK;
219 }
220
UnRegisterBundleStatusCallback()221 int32_t DriverPkgManager::UnRegisterBundleStatusCallback()
222 {
223 EDM_LOGI(MODULE_PKG_MGR, "UnRegisterCallback called");
224 if (bundleStateCallback_ == nullptr) {
225 EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, bundleStateCallback_ is null");
226 return EDM_ERR_INVALID_OBJECT;
227 }
228
229 if (bundleMonitor_ == nullptr) {
230 EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, bundleMonitor is null");
231 return EDM_ERR_INVALID_OBJECT;
232 }
233
234 if (!bundleMonitor_->UnSubscribe()) {
235 EDM_LOGE(MODULE_PKG_MGR, "Failed to unSubscribe bundleMonitor callback");
236 return EDM_NOK;
237 }
238
239 return EDM_OK;
240 }
241
RegisterOnBundleUpdate(PCALLBACKFUN pFun)242 int32_t DriverPkgManager::RegisterOnBundleUpdate(PCALLBACKFUN pFun)
243 {
244 if (pFun == nullptr) {
245 return EDM_ERR_INVALID_OBJECT;
246 }
247
248 if (bundleStateCallback_ == nullptr) {
249 EDM_LOGE(MODULE_PKG_MGR, "failed to register callback, bundleStateCallback_ is null");
250 return EDM_ERR_INVALID_OBJECT;
251 }
252 bundleStateCallback_->m_pFun = pFun;
253 return EDM_OK;
254 }
255
RegisterBundleCallback(std::shared_ptr<IBundleUpdateCallback> callback)256 int32_t DriverPkgManager::RegisterBundleCallback(std::shared_ptr<IBundleUpdateCallback> callback)
257 {
258 if (callback == nullptr) {
259 return EDM_ERR_INVALID_OBJECT;
260 }
261 if (bundleStateCallback_ == nullptr) {
262 EDM_LOGE(MODULE_PKG_MGR, "Failed to register bundleUpdate callback, bundleStateCallback_ is null");
263 return EDM_ERR_INVALID_OBJECT;
264 }
265 bundleStateCallback_->bundleUpdateCallback_ = callback;
266 return EDM_OK;
267 }
268
UnRegisterOnBundleUpdate()269 int32_t DriverPkgManager::UnRegisterOnBundleUpdate()
270 {
271 if (bundleStateCallback_ == nullptr) {
272 EDM_LOGE(MODULE_PKG_MGR, "failed to unregister callback, bundleStateCallback_ is null");
273 return EDM_ERR_INVALID_OBJECT;
274 }
275 bundleStateCallback_->m_pFun = nullptr;
276 return EDM_OK;
277 }
278 } // namespace ExternalDeviceManager
279 } // namespace OHOS