diff --git a/bundle.json b/bundle.json index 73104a44f60300ee28cbb35d63c25702d640c9aa..c794248d98a659196fa15ceab2c93b4e500cfd31 100644 --- a/bundle.json +++ b/bundle.json @@ -43,7 +43,8 @@ "safwk", "eventhandler", "ace_engine", - "access_token" + "access_token", + "relational_store" ], "third_party": [ "bounds_checking_function", diff --git a/sa_profile/hdf_ext_devmgr.cfg b/sa_profile/hdf_ext_devmgr.cfg index 52158de4c0ef255cd6ac61622f88405770ecd83a..0bcdcf8889b4c77dd2b3e4e2cc18d037a7ed0b02 100644 --- a/sa_profile/hdf_ext_devmgr.cfg +++ b/sa_profile/hdf_ext_devmgr.cfg @@ -1,4 +1,13 @@ { + "jobs" : [{ + "name" : "services:hdf_ext_devmgr", + "cmds" : [ + "mkdir /data/service/el1/public/pkg_service 0770 hdf_ext_devmgr hdf_ext_devmgr", + "chown hdf_ext_devmgr hdf_ext_devmgr /data/service/el1/public/pkg_service", + "chmod 0770 /data/service/el1/public/pkg_service" + ] + } + ], "services" : [{ "name" : "hdf_ext_devmgr", "path" : ["/system/bin/sa_main", "/system/profile/hdf_ext_devmgr.json"], @@ -6,8 +15,21 @@ "uid" : "hdf_ext_devmgr", "gid" : ["hdf_ext_devmgr", "shell", "uhid"], "permission" : [ - "ohos.permission.LISTEN_BUNDLE_CHANGE" + "ohos.permission.LISTEN_BUNDLE_CHANGE", + "ohos.permission.DISTRIBUTED_DATASYNC", + "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER", + "ohos.permission.GET_BUNDLE_INFO", + "ohos.permission.SET_TIME", + "ohos.permission.FACTORY_RESET", + "ohos.permission.MANAGE_LOCAL_ACCOUNTS", + "ohos.permission.MANAGE_SECURE_SETTINGS", + "ohos.permission.ACCESS_CERT_MANAGER_INTERNAL", + "ohos.permission.ACCESS_CERT_MANAGER", + "ohos.permission.NETSYS_INTERNAL" ], + "jobs" : { + "on-start" : "services:hdf_ext_devmgr" + }, "apl" : "system_basic", "secon" : "u:r:hdf_ext_devmgr:s0" } diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h index 13fff43f1073b6ccd7d370211254205ffe690683..7ed26a1eca693d6edf5440098a418a7b4cb03d92 100644 --- a/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h @@ -78,7 +78,7 @@ public: virtual sptr AsObject() override; - bool GetAllDriverInfos(std::map &driverInfos); + bool GetAllDriverInfos(); bool CheckBundleMgrProxyPermission(); @@ -97,6 +97,9 @@ private: ErrCode QueryExtensionAbilityInfos(const std::string &bundleName, const int userId); bool ParseBaseDriverInfo(); void ChangeValue(DriverInfo &tmpDrvInfo, std::vector &metadata); + void ClearDriverInfo(DriverInfo &tmpDrvInfo); + void ParseExtensionInfos(ExtensionAbilityType &type, + string &bundleName, string &abilityName); sptr GetBundleMgrProxy(); int32_t GetCurrentActiveUserId(); void StorageHistoryDrvInfo(std::vector &bundleInfos); diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h new file mode 100644 index 0000000000000000000000000000000000000000..1173ea324637d41937eaf3a2d055d4c315383999 --- /dev/null +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef PKG_DATABASE_H +#define PKG_DATABASE_H + +#include + +#include "data_ability_predicates.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_open_callback.h" +#include "rdb_predicates.h" +#include "rdb_store.h" +#include "result_set.h" +#include "value_object.h" + +namespace OHOS { +namespace ExternalDeviceManager { +static std::string PKG_DB_PATH = "/data/service/el1/public/pkg_service/"; + +constexpr const char *PKG_DB_NAME = "pkg.db"; +constexpr const char *PKG_TABLE_NAME = "pkgInfoTable"; +constexpr int32_t DATABASE_OPEN_VERSION = 1; +constexpr int32_t DATABASE_NEW_VERSION = 2; + +constexpr const char *CREATE_PKG_TABLE = "CREATE TABLE IF NOT EXISTS [pkgInfoTable](" + "[id] INTEGER PRIMARY KEY AUTOINCREMENT, " + "[bundleName] TEXT," + "[bundleAbility] TEXT," + "[driverInfo] TEXT );"; + +class PkgDataBase { +public: + static std::shared_ptr GetInstance(); + int64_t Insert(const OHOS::NativeRdb::ValuesBucket &insertValues); + int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, + const OHOS::NativeRdb::RdbPredicates &predicates); + int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const std::string &whereClause, + const std::vector &whereArgs); + int32_t Delete(const OHOS::NativeRdb::RdbPredicates &rdbPredicates); + int32_t Delete(int32_t &changedRows, const std::string &whereClause, const std::vector &whereArgs); + std::shared_ptr Query( + const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector &columns); + int32_t BeginTransaction(); + int32_t Commit(); + int32_t RollBack(); + +private: + PkgDataBase(); + DISALLOW_COPY_AND_MOVE(PkgDataBase); + static std::shared_ptr instance_; + std::shared_ptr store_; +}; + +class PkgDataBaseCallBack : public OHOS::NativeRdb::RdbOpenCallback { +public: + int32_t OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override; + int32_t OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; + int32_t OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t currentVersion, int32_t targetVersion) override; +}; +} // namespace USB +} // namespace OHOS + +#endif diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..f635eac6c90233ee25e5bcc3ffe7c8fd02c91034 --- /dev/null +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PKG_DB_HELPER_H +#define PKG_DB_HELPER_H + +#include "data_ability_predicates.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_open_callback.h" +#include "rdb_predicates.h" +#include "rdb_store.h" +#include "result_set.h" +#include "pkg_database.h" +#include "value_object.h" + +namespace OHOS { +namespace ExternalDeviceManager { +class PkgDbHelper { +public: + static std::shared_ptr GetInstance(); + /* query (user) record */ + /* query (user, device) record */ + /* query (user, device, app) record */ + /* query users */ + int32_t QueryRightRecordUids(std::vector &uids); + /* query apps */ + int32_t QueryRightRecordApps(int32_t uid, std::vector &apps); + + /* update (user, device, app) record */ + int32_t QueryAllBundleInfoNames(const std::string &bundleAbility, std::vector &bundleInfoNames); + /* add or update (user, device, app) record */ + int32_t AddOrUpdateRightRecord( + const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo); + std::string QueryBundleInfoNames(const std::string &driverInfo); + int32_t QueryAllBundleAbilityNames(const std::string &bundleName, std::vector &bundleAbilityNames); + /* delete (user, device, app) record */ + int32_t DeleteRightRecord(const std::string &bundleName); + /* delete (user, device) record */ + int32_t DeleteDeviceRightRecord(int32_t uid, const std::string &deviceName); + /* delete (user, app) record */ + int32_t DeleteAppRightRecord(int32_t uid, const std::string &bundleName); + /* delete (user, apps) record */ + int32_t DeleteAppsRightRecord(int32_t uid, const std::vector &bundleNames); + /* delete (user) record */ + int32_t DeleteUidRightRecord(int32_t uid); + /* delete (user, time) expired record */ + int32_t DeleteNormalExpiredRightRecord(int32_t uid, uint64_t expiredTime); + /* delete (validTime, device) record */ + int32_t DeleteValidPeriodRightRecord(long validPeriod, const std::string &deviceName); + int32_t CheckIfNeedUpdateEx( + bool &isUpdate, const std::string &bundleName); + int32_t QueryAllSize(std::vector &allBundleAbility); +private: + PkgDbHelper(); + DISALLOW_COPY_AND_MOVE(PkgDbHelper); + int32_t AddOrUpdateRightRecordEx(bool isUpdate, const std::string & bundleName, + const std::string & bundleAbility, const std::string &driverInfo); + int32_t QueryRightRecordCount(void); + int32_t QueryAndGetResultColumnValues(const OHOS::NativeRdb::RdbPredicates &rdbPredicates, + const std::vector &columns, const std::string &columnName, std::vector &columnValues); + int32_t DeleteAndNoOtherOperation(const std::string &whereClause, const std::vector &whereArgs); + int32_t DeleteAndNoOtherOperation(const OHOS::NativeRdb::RdbPredicates &rdbPredicates); + + static std::shared_ptr instance_; + std::mutex databaseMutex_; + std::shared_ptr rightDatabase_; +}; +} // namespace USB +} // namespace OHOS + +#endif diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn b/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn index a5b424fbfc20cc988f5124fdfcfce04485e0b492..2c472d609ff27c931a22b20e174015894dc801ee 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn @@ -26,6 +26,8 @@ ohos_shared_library("drivers_pkg_manager") { "driver_info.cpp", "driver_pkg_manager.cpp", "drv_bundle_state_callback.cpp", + "pkg_database.cpp", + "pkg_db_helper.cpp", ] include_dirs = [ "../../include/drivers_pkg_manager", @@ -53,8 +55,12 @@ ohos_shared_library("drivers_pkg_manager") { "hilog:libhilog", "hisysevent:libhisysevent", "hitrace:hitrace_meter", + "init:libbegetutil", "ipc:ipc_core", "os_account:os_account_innerkits", + "relational_store:native_appdatafwk", + "relational_store:native_dataability", + "relational_store:native_rdb", "samgr:samgr_proxy", ] subsystem_name = "hdf" diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp index ec278586b2e2bec4580f6d2a5ccffac3cfbfb519..c86ff4d76dc1eb385e1242ae66c136441ca1e988 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp @@ -23,6 +23,7 @@ #include "common_event_support.h" #include "common_event_subscribe_info.h" #include "bus_extension_core.h" +#include "pkg_db_helper.h" #include "driver_pkg_manager.h" namespace OHOS { @@ -74,8 +75,7 @@ int32_t DriverPkgManager::Init() return EDM_ERR_INVALID_OBJECT; } - std::map drvInfos_; - if (!bundleStateCallback_->GetAllDriverInfos(drvInfos_)) { + if (!bundleStateCallback_->GetAllDriverInfos()) { EDM_LOGE(MODULE_PKG_MGR, "bundleStateCallback_ GetAllDriverInfos Err"); return EDM_ERR_NOT_SUPPORT; } @@ -90,39 +90,42 @@ shared_ptr DriverPkgManager::QueryMatchDriver(shared_ptr(); ret->bundleName.clear(); ret->abilityName.clear(); - if (bundleStateCallback_ == nullptr) { EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver bundleStateCallback_ null"); return nullptr; } - std::map drvInfos_; - if (!bundleStateCallback_->GetAllDriverInfos(drvInfos_)) { + if (!bundleStateCallback_->GetAllDriverInfos()) { EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetAllDriverInfos Err"); return nullptr; } - if (drvInfos_.empty()) { - EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver drvInfos_ Empty"); + std::vector apps; + std::shared_ptr helper = PkgDbHelper::GetInstance(); + int32_t retRdb = helper->QueryAllBundleInfoNames("uid", apps); + if (retRdb <= 0) { + /* error or empty record */ return nullptr; } - - for (auto [key, val] : drvInfos_) { - extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(val.GetBusName()); + int32_t totalApps = static_cast(apps.size()); + EDM_LOGE(MODULE_PKG_MGR, "totalApps: %{public}d", totalApps); + for (int32_t i = 0; i < totalApps; i++) { + std::string app = apps.at(i); + DriverInfo driverInfo; + driverInfo.UnSerialize(app); + + extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(driverInfo.GetBusName()); if (extInstance == nullptr) { - EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetInstance at bus:%{public}s", val.GetBusName().c_str()); - continue; + return nullptr; } - if (extInstance->MatchDriver(val, *devInfo)) { - string bundleName = key; + if (extInstance->MatchDriver(driverInfo, *devInfo)) { + std::shared_ptr helper = PkgDbHelper::GetInstance(); + + string bundleName = helper->QueryBundleInfoNames(app); ret->bundleName = bundleName.substr(0, bundleName.find_first_of(bundleStateCallback_->GetStiching())); ret->abilityName = bundleName.substr(bundleName.find_last_of(bundleStateCallback_->GetStiching()) + 1); return ret; - } else { - string drvInfoStr; - val.Serialize(drvInfoStr); - EDM_LOGI(MODULE_PKG_MGR, "PKG Serialize:%{public}s", drvInfoStr.c_str()); } } diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp index f3ff7dc442cbf56a4583a061566ec8ef67626dce..7751d717fe33577aaa37ca7a11935ebbc6439ba5 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp @@ -23,6 +23,7 @@ #include "iservice_registry.h" #include "bundle_constants.h" #include "os_account_manager.h" +#include "pkg_db_helper.h" #include "hdf_log.h" #include "edm_errors.h" @@ -44,12 +45,10 @@ const string DRV_INFO_VERSION = "version"; DrvBundleStateCallback::DrvBundleStateCallback() { - allDrvInfos_.clear(); stiching.clear(); stiching += "########"; - std::map drvInfos_; - if (GetAllDriverInfos(drvInfos_)) { + if (GetAllDriverInfos()) { EDM_LOGI(MODULE_PKG_MGR, "GetAllDriverInfos in DrvBundleStateCallback OK"); } else { EDM_LOGE(MODULE_PKG_MGR, "GetAllDriverInfos in DrvBundleStateCallback ERR"); @@ -63,7 +62,14 @@ DrvBundleStateCallback::~DrvBundleStateCallback() void DrvBundleStateCallback::PrintTest() { - cout << "allDrvInfos_ size = " << allDrvInfos_.size() << endl; + std::vector allBundleAbilityNames; + std::shared_ptr helper = PkgDbHelper::GetInstance(); + int32_t ret = helper->QueryAllSize(allBundleAbilityNames); + if (ret <= 0) { + EDM_LOGE(MODULE_PKG_MGR, "QueryAllSize failed"); + return; + } + cout << "allBundleAbilityNames_ size = " << allBundleAbilityNames.size() << endl; } void DrvBundleStateCallback::OnBundleStateChanged(const uint8_t installType, const int32_t resultCode, @@ -87,7 +93,7 @@ void DrvBundleStateCallback::OnBundleAdded(const std::string &bundleName, const } if (ParseBaseDriverInfo()) { - OnBundleDrvAdded(BUNDLE_ADDED); + EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error"); } FinishTrace(LABEL); } @@ -105,7 +111,7 @@ void DrvBundleStateCallback::OnBundleUpdated(const std::string &bundleName, cons } if (ParseBaseDriverInfo()) { - OnBundleDrvUpdated(BUNDLE_UPDATED); + EDM_LOGE(MODULE_PKG_MGR, "OnBundleUpdated error"); } FinishTrace(LABEL); } @@ -128,10 +134,9 @@ sptr DrvBundleStateCallback::AsObject() return nullptr; } -bool DrvBundleStateCallback::GetAllDriverInfos(std::map &driverInfos) +bool DrvBundleStateCallback::GetAllDriverInfos() { if (initOnce) { - driverInfos = allDrvInfos_; return true; } @@ -139,7 +144,6 @@ bool DrvBundleStateCallback::GetAllDriverInfos(std::map &dri auto iBundleMgr = GetBundleMgrProxy(); if (iBundleMgr == nullptr) { EDM_LOGE(MODULE_PKG_MGR, "Can not get iBundleMgr"); - driverInfos = allDrvInfos_; return false; } std::vector bundleInfos; @@ -149,7 +153,6 @@ bool DrvBundleStateCallback::GetAllDriverInfos(std::map &dri static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA); if (!(iBundleMgr->GetBundleInfos(flags, bundleInfos, userId))) { EDM_LOGE(MODULE_PKG_MGR, "GetBundleInfos err"); - driverInfos = allDrvInfos_; return false; } @@ -229,6 +232,22 @@ void DrvBundleStateCallback::ChangeValue(DriverInfo &tmpDrvInfo, std::vector extInstance = nullptr; @@ -239,20 +258,12 @@ bool DrvBundleStateCallback::ParseBaseDriverInfo() string abilityName; bool ret = false; - // clear DriverInfos vector - innerDrvInfos_.clear(); - // parase infos to innerDrvInfos_ while (!extensionInfos_.empty()) { - tmpDrvInfo.bus_.clear(); - tmpDrvInfo.vendor_.clear(); - tmpDrvInfo.version_.clear(); - tmpDrvInfo.driverInfoExt_ = nullptr; + ClearDriverInfo(tmpDrvInfo); + ParseExtensionInfos(type, bundleName, abilityName); - type = extensionInfos_.back().type; metadata = extensionInfos_.back().metadata; - bundleName = extensionInfos_.back().bundleName; - abilityName = extensionInfos_.back().name; extensionInfos_.pop_back(); if ((type != ExtensionAbilityType::DRIVER) || metadata.empty()) { @@ -272,9 +283,29 @@ bool DrvBundleStateCallback::ParseBaseDriverInfo() EDM_LOGE(MODULE_PKG_MGR, "ParseDriverInfo null"); continue; } + string driverInfo; + tmpDrvInfo.Serialize(driverInfo); + string tempbundleName = bundleName; bundleName += stiching + abilityName; - innerDrvInfos_[bundleName] = tmpDrvInfo; + + std::shared_ptr helper = PkgDbHelper::GetInstance(); + bool flag = true; + helper->CheckIfNeedUpdateEx(flag, bundleName); + int32_t addOrUpdate = helper->AddOrUpdateRightRecord(tempbundleName, bundleName, driverInfo); + if (addOrUpdate < 0) { + EDM_LOGE(MODULE_PKG_MGR, "add or update failed: %{public}s, addOrUpdate=%{public}d", + bundleName.c_str(), addOrUpdate); + continue; + } + + if (m_pFun != nullptr) { + if (flag) { + m_pFun(BUNDLE_UPDATED, BusType::BUS_TYPE_USB, tempbundleName, abilityName); + } else { + m_pFun(BUNDLE_ADDED, BusType::BUS_TYPE_USB, tempbundleName, abilityName); + } + } ret = true; } return ret; @@ -322,7 +353,6 @@ sptr DrvBundleStateCallback::GetBundleMgrProxy() void DrvBundleStateCallback::StorageHistoryDrvInfo(std::vector &bundleInfos) { - allDrvInfos_.clear(); while (!bundleInfos.empty()) { extensionInfos_.clear(); extensionInfos_ = bundleInfos.back().extensionInfos; @@ -332,41 +362,37 @@ void DrvBundleStateCallback::StorageHistoryDrvInfo(std::vector &bund } if (ParseBaseDriverInfo()) { - OnBundleDrvAdded(BUNDLE_ADDED); + EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error"); } } } -void DrvBundleStateCallback::OnBundleDrvAdded(int bundleStatus) +void DrvBundleStateCallback::OnBundleDrvRemoved(const std::string &bundleName) { - for (auto ele : innerDrvInfos_) { - allDrvInfos_[ele.first] = innerDrvInfos_[ele.first]; + std::vector bundleAbilityNames; + std::shared_ptr helper = PkgDbHelper::GetInstance(); + int32_t retRdb = helper->QueryAllBundleAbilityNames(bundleName, bundleAbilityNames); + if (retRdb <= 0) { + EDM_LOGE(MODULE_PKG_MGR, "QueryAllBundleAbilityNames failed: %{public}s", bundleName.c_str()); + return; + } + int32_t totalNames = static_cast(bundleAbilityNames.size()); + EDM_LOGE(MODULE_PKG_MGR, "totalbundleAbilityNames: %{public}d", totalNames); + for (int32_t i = 0; i < totalNames; i++) { if (m_pFun != nullptr) { - string bundleName = ele.first.substr(0, ele.first.find_first_of(GetStiching())); - string abilityName = ele.first.substr(ele.first.find_last_of(GetStiching()) + 1); - m_pFun(bundleStatus, BusType::BUS_TYPE_USB, bundleName, abilityName); + std::string bundleAbilityName = bundleAbilityNames.at(i); + std::string bundleName = bundleAbilityName.substr(0, bundleAbilityName.find_first_of(GetStiching())); + std::string abilityName = bundleAbilityName.substr(bundleAbilityName.find_last_of(GetStiching()) + 1); + EDM_LOGI(MODULE_PKG_MGR, "bundleName: %{public}s abilityName: %{public}s", + bundleName.c_str(), abilityName.c_str()); + m_pFun(BUNDLE_REMOVED, BusType::BUS_TYPE_USB, bundleName, abilityName); } } -} - -void DrvBundleStateCallback::OnBundleDrvUpdated(int bundleStatus) -{ - OnBundleDrvAdded(bundleStatus); -} -void DrvBundleStateCallback::OnBundleDrvRemoved(const std::string &bundleName) -{ - for (auto iter = allDrvInfos_.begin(); iter != allDrvInfos_.end();) { - if (iter->first.find(bundleName) != std::string::npos) { - if (m_pFun != nullptr) { - string abilityName = iter->first.substr(iter->first.find_last_of(GetStiching()) + 1); - EDM_LOGI(MODULE_PKG_MGR, "abilityName:%{public}s", abilityName.c_str()); - m_pFun(BUNDLE_REMOVED, BusType::BUS_TYPE_USB, bundleName, abilityName); - } - iter = allDrvInfos_.erase(iter); - } else { - ++iter; - } + int32_t ret = helper->DeleteRightRecord(bundleName); + if (ret < 0) { + EDM_LOGE(MODULE_PKG_MGR, "delete failed: %{public}s", bundleName.c_str()); + return; } } } diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86ba1fcb9ea196fc9aa0f77af44190d8a4367c3e --- /dev/null +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "pkg_database.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace ExternalDeviceManager { +std::shared_ptr PkgDataBase::instance_ = nullptr; + +PkgDataBase::PkgDataBase() +{ + std::string rightDatabaseName = PKG_DB_PATH + "pkg.db"; + int32_t errCode = OHOS::NativeRdb::E_OK; + OHOS::NativeRdb::RdbStoreConfig config(rightDatabaseName); + config.SetSecurityLevel(NativeRdb::SecurityLevel::S1); + PkgDataBaseCallBack sqliteOpenHelperCallback; + store_ = OHOS::NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_OPEN_VERSION, sqliteOpenHelperCallback, errCode); + if (errCode != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "GetRdbStore errCode :%{public}d", errCode); + } else { + EDM_LOGE(MODULE_PKG_MGR, "GetRdbStore success :%{public}d", errCode); + } +} + +std::shared_ptr PkgDataBase::GetInstance() +{ + if (instance_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "PkgDataBase reset to new instance"); + instance_.reset(new PkgDataBase()); + return instance_; + } + return instance_; +} + +int32_t PkgDataBase::BeginTransaction() +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->BeginTransaction(); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction fail :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::Commit() +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Commit store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Commit(); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit fail :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::RollBack() +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "RollBack store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->RollBack(); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "RollBack fail :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int64_t PkgDataBase::Insert(const OHOS::NativeRdb::ValuesBucket &insertValues) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Insert store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int64_t outRowId = 0; + int32_t ret = store_->Insert(outRowId, PKG_TABLE_NAME, insertValues); + EDM_LOGI(MODULE_PKG_MGR, "Insert id=%{public}" PRIu64 "", outRowId); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Insert ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return outRowId; +} + +int32_t PkgDataBase::Update( + int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const OHOS::NativeRdb::RdbPredicates &predicates) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Update(RdbPredicates) store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Update(changedRows, values, predicates); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Update(RdbPredicates) ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, + const std::string &whereClause, const std::vector &whereArgs) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Update(whereClause) store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Update(changedRows, PKG_TABLE_NAME, values, whereClause, whereArgs); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Update(whereClause) ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::Delete( + int32_t &changedRows, const std::string &whereClause, const std::vector &whereArgs) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Delete store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Delete(changedRows, PKG_TABLE_NAME, whereClause, whereArgs); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Delete(whereClause) ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +std::shared_ptr PkgDataBase::Query( + const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector &columns) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query(AbsRdbPredicates) store_ is nullptr"); + return nullptr; + } + return store_->Query(predicates, columns); +} + +int32_t PkgDataBaseCallBack::OnCreate(OHOS::NativeRdb::RdbStore &store) +{ + std::string sql = CREATE_PKG_TABLE; + int32_t ret = store.ExecuteSql(sql); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "OnCreate failed: %{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + EDM_LOGI(MODULE_PKG_MGR, "DB OnCreate Done: %{public}d", ret); + return PKG_OK; +} + +int32_t PkgDataBaseCallBack::OnUpgrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) +{ + EDM_LOGI(MODULE_PKG_MGR, "DB OnUpgrade Enter"); + (void)store; + (void)oldVersion; + (void)newVersion; + return PKG_OK; +} + +int32_t PkgDataBaseCallBack::OnDowngrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) +{ + EDM_LOGI(MODULE_PKG_MGR, "DB OnDowngrade Enter"); + (void)store; + (void)oldVersion; + (void)newVersion; + return PKG_OK; +} +} // namespace USB +} // namespace OHOS diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55e523ecd750285702cb4b9ff95efde8209cd188 --- /dev/null +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp @@ -0,0 +1,257 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "pkg_db_helper.h" +#include "bundle_installer_interface.h" +#include "hilog_wrapper.h" +#include "pkg_database.h" + +using namespace OHOS::NativeRdb; + +namespace OHOS { +namespace ExternalDeviceManager { +std::shared_ptr PkgDbHelper::instance_; + +PkgDbHelper::PkgDbHelper() +{ + rightDatabase_ = PkgDataBase::GetInstance(); +} + +std::shared_ptr PkgDbHelper::GetInstance() +{ + static std::mutex instanceMutex; + std::lock_guard guard(instanceMutex); + if (instance_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "PkgDbHelper reset to new instance"); + instance_.reset(new PkgDbHelper()); + } + return instance_; +} + +int32_t PkgDbHelper::DeleteAndNoOtherOperation( + const std::string &whereClause, const std::vector &whereArgs) +{ + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return ret; + } + int32_t changedRows = 0; + ret = rightDatabase_->Delete(changedRows, whereClause, whereArgs); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Delete error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + return ret; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + } + return ret; +} + +int32_t PkgDbHelper::DeleteRightRecord(const std::string &bundleName) +{ + std::lock_guard guard(databaseMutex_); + std::string whereClause = {"bundleName = ?"}; + std::vector whereArgs = {bundleName}; + int32_t ret = DeleteAndNoOtherOperation(whereClause, whereArgs); + if (ret != PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "failed: detale(uid, dev, app): %{public}d", ret); + } + return ret; +} + +int32_t PkgDbHelper::AddOrUpdateRightRecord( + const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo) +{ + std::lock_guard guard(databaseMutex_); + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return ret; + } + bool isUpdate = false; + ret = CheckIfNeedUpdateEx(isUpdate, bundleAbility); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "check if need update error: %{public}d", ret); + return ret; + } + ret = AddOrUpdateRightRecordEx(isUpdate, bundleName, bundleAbility, driverInfo); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "add or update error: %{public}d", ret); + return ret; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + } + return ret; +} + +int32_t PkgDbHelper::CheckIfNeedUpdateEx( + bool &isUpdate, const std::string &bundleAbility) +{ + std::vector columns; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + rdbPredicates.BeginWrap() + ->EqualTo("bundleAbility", bundleAbility) + ->EndWrap(); + auto resultSet = rightDatabase_->Query(rdbPredicates, columns); + if (resultSet == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query error"); + (void)rightDatabase_->RollBack(); + return PKG_RDB_EXECUTE_FAILTURE; + } + int32_t rowCount = 0; + if (resultSet->GetRowCount(rowCount) != E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "GetRowCount error"); + (void)rightDatabase_->RollBack(); + return PKG_RDB_EXECUTE_FAILTURE; + } + isUpdate = (rowCount > 0 ? true : false); + + return PKG_OK; +} + +int32_t PkgDbHelper::AddOrUpdateRightRecordEx(bool isUpdate, + const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo) +{ + int32_t ret = 0; + ValuesBucket values; + values.Clear(); + values.PutString("bundleName", bundleName); + values.PutString("bundleAbility", bundleAbility); + values.PutString("driverInfo", driverInfo); + EDM_LOGI(MODULE_PKG_MGR, "bundleName: %{public}s driverInfo: %{public}s", + bundleName.c_str(), driverInfo.c_str()); + if (isUpdate) { + int32_t changedRows = 0; + ret = rightDatabase_->Update(changedRows, values, "bundleAbility = ?", + std::vector {bundleAbility}); + } else { + ret = rightDatabase_->Insert(values); + } + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Insert or Update error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + } + return ret; +} + +int32_t PkgDbHelper::QueryAllBundleInfoNames(const std::string &bundleAbility, + std::vector &bundleInfoNames) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"driverInfo"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + return QueryAndGetResultColumnValues(rdbPredicates, columns, "driverInfo", bundleInfoNames); +} + +int32_t PkgDbHelper::QueryAllBundleAbilityNames(const std::string &bundleName, + std::vector &bundleAbilityNames) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"bundleAbility"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + rdbPredicates.EqualTo("bundleName", bundleName)->Distinct(); + return QueryAndGetResultColumnValues(rdbPredicates, columns, "bundleAbility", bundleAbilityNames); +} + +int32_t PkgDbHelper::QueryAllSize(std::vector &allBundleAbility) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"bundleAbility"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + return QueryAndGetResultColumnValues(rdbPredicates, columns, "bundleAbility", allBundleAbility); +} + +int32_t PkgDbHelper::QueryAndGetResultColumnValues(const RdbPredicates &rdbPredicates, + const std::vector &columns, const std::string &columnName, std::vector &columnValues) +{ + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return ret; + } + auto resultSet = rightDatabase_->Query(rdbPredicates, columns); + if (resultSet == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query error"); + (void)rightDatabase_->RollBack(); + return PKG_RDB_EXECUTE_FAILTURE; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + return ret; + } + int32_t rowCount = 0; + int32_t columnIndex = 0; + if (resultSet->GetRowCount(rowCount) != E_OK || resultSet->GetColumnIndex(columnName, columnIndex) != E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "get table info failed"); + return PKG_RDB_EXECUTE_FAILTURE; + } + bool endFlag = false; + for (int32_t i = 0; (i < rowCount) && !endFlag; i++) { + if (resultSet->GoToRow(i) != E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "GoToRow %{public}d", i); + return PKG_RDB_EXECUTE_FAILTURE; + } + std::string tempStr; + if (resultSet->GetString(columnIndex, tempStr) == E_OK) { + columnValues.push_back(tempStr); + } + resultSet->IsEnded(endFlag); + } + int32_t position = 0; + resultSet->GetRowIndex(position); + resultSet->IsEnded(endFlag); + EDM_LOGI(MODULE_PKG_MGR, "idx=%{public}d rows=%{public}d pos=%{public}d ret=%{public}zu end=%{public}s", + columnIndex, rowCount, position, columnValues.size(), (endFlag ? "yes" : "no")); + return columnValues.size(); +} + +std::string PkgDbHelper::QueryBundleInfoNames(const std::string &driverInfo) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"bundleAbility"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + rdbPredicates.EqualTo("driverInfo", driverInfo)->Distinct(); + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return nullptr; + } + auto resultSet = rightDatabase_->Query(rdbPredicates, columns); + if (resultSet == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query error"); + (void)rightDatabase_->RollBack(); + return nullptr; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + return nullptr; + } + std::string s; + resultSet->GetString(0, s); + return s; +} +} // namespace USB +} // namespace OHOS diff --git a/test/unittest/drivers_pkg_manager_test/src/drv_bundle_callback_test.cpp b/test/unittest/drivers_pkg_manager_test/src/drv_bundle_callback_test.cpp index 8c99d6d06e345664711c4d2c75d5ea11fe33befa..1d1b06aa28bdbe920467a02a6fb9e9ab9b94e41f 100644 --- a/test/unittest/drivers_pkg_manager_test/src/drv_bundle_callback_test.cpp +++ b/test/unittest/drivers_pkg_manager_test/src/drv_bundle_callback_test.cpp @@ -44,8 +44,7 @@ HWTEST_F(DrvBundleStateCallbackTest, DrvBundleCallback_CheckPermissio_Test, Test HWTEST_F(DrvBundleStateCallbackTest, DrvBundleCallback_GetAllInfos_Test, TestSize.Level1) { - std::map drvInfos_; - bool ret = drvbundleInstance.GetAllDriverInfos(drvInfos_); + bool ret = drvbundleInstance.GetAllDriverInfos(); EXPECT_EQ(true, ret); cout << "Ptr DrvBundleCallback_GetAllInfos_Test" << endl; } diff --git a/utils/include/hilog_wrapper.h b/utils/include/hilog_wrapper.h index 58ed4dd881375c7b65aaea4c10f5f221a3492395..2167ff6cbede678c3740da29d0177eb3b549b70d 100644 --- a/utils/include/hilog_wrapper.h +++ b/utils/include/hilog_wrapper.h @@ -73,6 +73,17 @@ enum UsbMgrDomainId { EDM_BUTT, }; +enum PkgErrCode { + PKG_OK = 0, + PKG_FAILURE = -1, + PKG_RDB_EXECUTE_FAILTURE = -2, + PKG_RDB_NO_INIT = -3, + PKG_RDB_EMPTY = -4, + PKG_PERMISSION_DENIED = -5, + PKG_NOP = -6, + PKG_OVERFLOW = -7, +}; + constexpr OHOS::HiviewDFX::HiLogLabel EDM_MGR_LABEL[EDM_MODULE_BUTT] = { {LOG_CORE, EDM_FRAMEWORK_DOMAIN, "EdmFwk" }, {LOG_CORE, EDM_SERVICE_DOMAIN, "EdmService" },