diff --git a/interfaces/innerkits/appexecfwk_core/BUILD.gn b/interfaces/innerkits/appexecfwk_core/BUILD.gn index c6ed8ee0cbf7c8b14f948e86f58e430d63d688bf..64c31b633dc46740f1c0aaa8ac3f832bf862f49b 100644 --- a/interfaces/innerkits/appexecfwk_core/BUILD.gn +++ b/interfaces/innerkits/appexecfwk_core/BUILD.gn @@ -31,6 +31,7 @@ ohos_shared_library("appexecfwk_core") { sources = [ "src/bundlemgr/bundle_installer_proxy.cpp", "src/bundlemgr/bundle_mgr_client.cpp", + "src/bundlemgr/bundle_mgr_client_impl.cpp", "src/bundlemgr/bundle_mgr_host.cpp", "src/bundlemgr/bundle_mgr_proxy.cpp", "src/bundlemgr/bundle_monitor.cpp", diff --git a/interfaces/innerkits/appexecfwk_core/include/bundlemgr/bundle_mgr_client.h b/interfaces/innerkits/appexecfwk_core/include/bundlemgr/bundle_mgr_client.h index 089dff8176f843f4ea0f79c130ed07ead32f788c..11682792ed15823df528c0fb59c04f78ebbdce24 100644 --- a/interfaces/innerkits/appexecfwk_core/include/bundlemgr/bundle_mgr_client.h +++ b/interfaces/innerkits/appexecfwk_core/include/bundlemgr/bundle_mgr_client.h @@ -16,17 +16,15 @@ #ifndef FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_H #define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_H -#include "appexecfwk_errors.h" #include "bundle_info.h" -#include "bundle_mgr_interface.h" #include "extension_ability_info.h" #include "hap_module_info.h" -#include "resource_manager.h" -#include "singleton.h" namespace OHOS { namespace AppExecFwk { -class BundleMgrClient : public DelayedSingleton { +class BundleMgrClientImpl; + +class BundleMgrClient { public: BundleMgrClient(); virtual ~BundleMgrClient(); @@ -71,18 +69,7 @@ public: std::vector &profileInfos) const; private: - ErrCode Connect(); - bool GetResProfileByMetadata(const std::vector &metadata, const std::string &metadataName, - const std ::string &resourcePath, std::vector &profileInfos) const; - std::shared_ptr InitResMgr(const std::string &resourcePath) const; - bool GetResFromResMgr(const std::string &resName, const std::shared_ptr &resMgr, - std::vector &profileInfos) const; - bool IsFileExisted(const std::string &filePath, const std::string &suffix) const; - bool TransformFileToJsonString(const std::string &resPath, std::string &profile) const; - -private: - std::mutex mutex_; - sptr bundleMgr_; + std::shared_ptr impl_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/innerkits/appexecfwk_core/include/bundlemgr/bundle_mgr_client_impl.h b/interfaces/innerkits/appexecfwk_core/include/bundlemgr/bundle_mgr_client_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..fccc388f125bcd72ddddbf17e114288afc412435 --- /dev/null +++ b/interfaces/innerkits/appexecfwk_core/include/bundlemgr/bundle_mgr_client_impl.h @@ -0,0 +1,45 @@ +#ifndef FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_IMPL_H +#define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_IMPL_H + +#include "appexecfwk_errors.h" +#include "application_info.h" +#include "bundle_info.h" +#include "bundle_mgr_interface.h" +#include "extension_ability_info.h" +#include "hap_module_info.h" +#include "resource_manager.h" + +namespace OHOS { +namespace AppExecFwk { +class BundleMgrClientImpl { +public: + BundleMgrClientImpl(); + virtual ~BundleMgrClientImpl(); + + bool GetBundleNameForUid(const int uid, std::string &bundleName); + bool GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo); + bool GetHapModuleInfo(const std::string &bundleName, const std::string &hapName, HapModuleInfo &hapModuleInfo); + bool GetResConfigFile(const HapModuleInfo &hapModuleInfo, const std::string &metadataName, + std::vector &profileInfos) const; + bool GetResConfigFile(const ExtensionAbilityInfo &extensionInfo, const std::string &metadataName, + std::vector &profileInfos) const; + bool GetResConfigFile(const AbilityInfo &abilityInfo, const std::string &metadataName, + std::vector &profileInfos) const; + +private: + ErrCode Connect(); + bool GetResProfileByMetadata(const std::vector &metadata, const std::string &metadataName, + const std ::string &resourcePath, std::vector &profileInfos) const; + std::shared_ptr InitResMgr(const std::string &resourcePath) const; + bool GetResFromResMgr(const std::string &resName, const std::shared_ptr &resMgr, + std::vector &profileInfos) const; + bool IsFileExisted(const std::string &filePath, const std::string &suffix) const; + bool TransformFileToJsonString(const std::string &resPath, std::string &profile) const; + +private: + std::mutex mutex_; + sptr bundleMgr_; +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_H \ No newline at end of file diff --git a/interfaces/innerkits/appexecfwk_core/src/bundlemgr/bundle_mgr_client.cpp b/interfaces/innerkits/appexecfwk_core/src/bundlemgr/bundle_mgr_client.cpp index c0db340c13ad7c95fdddda365eb0afb17a9fda3f..d5b91b9c1df258754c950d98311e8a3df4b99ce6 100644 --- a/interfaces/innerkits/appexecfwk_core/src/bundlemgr/bundle_mgr_client.cpp +++ b/interfaces/innerkits/appexecfwk_core/src/bundlemgr/bundle_mgr_client.cpp @@ -14,297 +14,57 @@ */ #include "bundle_mgr_client.h" -#include -#include -#include - #include "ability_info.h" #include "app_log_wrapper.h" -#include "bundle_constants.h" -#include "bundle_mgr_interface.h" -#include "iservice_registry.h" -#include "locale_config.h" -#include "nlohmann/json.hpp" -#include "system_ability_definition.h" - -using namespace OHOS::Global::Resource; -using namespace OHOS::Global::I18n; +#include "bundle_mgr_client_impl.h" +#include "extension_ability_info.h" +#include "hap_module_info.h" namespace OHOS { namespace AppExecFwk { BundleMgrClient::BundleMgrClient() { - APP_LOGI("enter"); + APP_LOGI("create BundleMgrClient"); + impl_ = std::make_shared(); } BundleMgrClient::~BundleMgrClient() { - APP_LOGI("enter"); + APP_LOGI("destory BundleMgrClient"); } bool BundleMgrClient::GetBundleNameForUid(const int uid, std::string &bundleName) { - APP_LOGI("enter"); - - ErrCode result = Connect(); - if (result != ERR_OK) { - APP_LOGE("failed to connect"); - return false; - } - - return bundleMgr_->GetBundleNameForUid(uid, bundleName); + return impl_->GetBundleNameForUid(uid, bundleName); } bool BundleMgrClient::GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo) { - APP_LOGI("enter"); - - ErrCode result = Connect(); - if (result != ERR_OK) { - APP_LOGE("failed to connect"); - return false; - } - - return bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo); + return impl_->GetBundleInfo(bundleName, flag, bundleInfo); } bool BundleMgrClient::GetHapModuleInfo(const std::string &bundleName, const std::string &hapName, HapModuleInfo &hapModuleInfo) { - ErrCode result = Connect(); - if (result != ERR_OK) { - APP_LOGE("failed to connect"); - return false; - } - - AbilityInfo info; - info.bundleName = bundleName; - info.package = hapName; - return bundleMgr_->GetHapModuleInfo(info, hapModuleInfo); + return impl_->GetHapModuleInfo(bundleName, hapName, hapModuleInfo); } bool BundleMgrClient::GetResConfigFile(const HapModuleInfo &hapModuleInfo, const std::string &metadataName, std::vector &profileInfos) const { - std::vector data = hapModuleInfo.metadata; - std::string resourcePath = hapModuleInfo.resourcePath; - if (!GetResProfileByMetadata(data, metadataName, resourcePath, profileInfos)) { - APP_LOGE("GetResProfileByMetadata failed"); - return false; - } - if (profileInfos.empty()) { - APP_LOGE("no valid file can be obtained"); - return false; - } - return true; + return impl_->GetResConfigFile(hapModuleInfo, metadataName, profileInfos); } bool BundleMgrClient::GetResConfigFile(const ExtensionAbilityInfo &extensionInfo, const std::string &metadataName, std::vector &profileInfos) const { - std::vector data = extensionInfo.metadata; - std::string resourcePath = extensionInfo.resourcePath; - if (!GetResProfileByMetadata(data, metadataName, resourcePath, profileInfos)) { - APP_LOGE("GetResProfileByMetadata failed"); - return false; - } - if (profileInfos.empty()) { - APP_LOGE("no valid file can be obtained"); - return false; - } - return true; + return impl_->GetResConfigFile(extensionInfo, metadataName, profileInfos); } bool BundleMgrClient::GetResConfigFile(const AbilityInfo &abilityInfo, const std::string &metadataName, std::vector &profileInfos) const { - std::vector data = abilityInfo.metadata; - std::string resourcePath = abilityInfo.resourcePath; - if (!GetResProfileByMetadata(data, metadataName, resourcePath, profileInfos)) { - APP_LOGE("GetResProfileByMetadata failed"); - return false; - } - if (profileInfos.empty()) { - APP_LOGE("no valid file can be obtained"); - return false; - } - return true; -} - -bool BundleMgrClient::GetResProfileByMetadata(const std::vector &metadata, const std::string &metadataName, - const std ::string &resourcePath, std::vector &profileInfos) const -{ - if (metadata.empty()) { - APP_LOGE("GetResProfileByMetadata failed due to empty metadata"); - return false; - } - if (resourcePath.empty()) { - APP_LOGE("GetResProfileByMetadata failed due to empty resourcePath"); - return false; - } - std::shared_ptr resMgr = InitResMgr(resourcePath); - if (resMgr == nullptr) { - APP_LOGE("GetResProfileByMetadata init resMgr failed"); - return false; - } - - if (metadataName.empty()) { - for_each(metadata.begin(), metadata.end(), [this, &resMgr, &profileInfos](const Metadata& data)->void { - if (!GetResFromResMgr(data.resource, resMgr, profileInfos)) { - APP_LOGW("GetResFromResMgr failed"); - } - }); - } else { - for_each(metadata.begin(), metadata.end(), - [this, &resMgr, &metadataName, &profileInfos](const Metadata& data)->void { - if ((metadataName.compare(data.name) == 0) && (!GetResFromResMgr(data.resource, resMgr, profileInfos))) { - APP_LOGW("GetResFromResMgr failed"); - } - }); - } - - return true; -} - -std::shared_ptr BundleMgrClient::InitResMgr(const std::string &resourcePath) const -{ - APP_LOGD("InitResMgr begin"); - if (resourcePath.empty()) { - APP_LOGE("InitResMgr failed due to invalid param"); - return nullptr; - } - std::shared_ptr resMgr(CreateResourceManager()); - if (!resMgr) { - APP_LOGE("InitResMgr resMgr is nullptr"); - return nullptr; - } - - std::unique_ptr resConfig(CreateResConfig()); - if (!resConfig) { - APP_LOGE("InitResMgr resConfig is nullptr"); - return nullptr; - } - resConfig->SetLocaleInfo(LocaleConfig::GetSystemLanguage().c_str(), LocaleConfig::GetSystemLocale().c_str(), - LocaleConfig::GetSystemRegion().c_str()); - resMgr->UpdateResConfig(*resConfig); - - APP_LOGD("resourcePath is %{public}s", resourcePath.c_str()); - if (!resourcePath.empty() && !resMgr->AddResource(resourcePath.c_str())) { - APP_LOGE("InitResMgr AddResource failed"); - return nullptr; - } - return resMgr; -} - -bool BundleMgrClient::GetResFromResMgr(const std::string &resName, const std::shared_ptr &resMgr, - std::vector &profileInfos) const -{ - APP_LOGD("GetResFromResMgr begin"); - if (resName.empty()) { - APP_LOGE("GetResFromResMgr res name is empty"); - return false; - } - - size_t pos = resName.rfind(Constants::PROFILE_FILE_COLON); - if ((pos == std::string::npos) || (pos == resName.length() - 1)) { - APP_LOGE("GetResFromResMgr res name is invalid"); - return false; - } - std::string profileName = resName.substr(pos + 1); - std::string resPath; - if (resMgr->GetProfileByName(profileName.c_str(), resPath) != SUCCESS) { - APP_LOGE("GetResFromResMgr profileName cannot be found"); - return false; - } - APP_LOGD("GetResFromResMgr resPath is %{public}s", resPath.c_str()); - std::string profile; - if (!TransformFileToJsonString(resPath, profile)) { - return false; - } - profileInfos.emplace_back(profile); - return true; -} - -bool BundleMgrClient::IsFileExisted(const std::string &filePath, const std::string &suffix) const -{ - if (filePath.empty()) { - APP_LOGE("the file is not existed due to empty file path"); - return false; - } - - auto position = filePath.rfind('.'); - if (position == std::string::npos) { - APP_LOGE("filePath no suffix"); - return false; - } - - std::string suffixStr = filePath.substr(position); - if (LowerStr(suffixStr) != suffix) { - APP_LOGE("file is not json"); - return false; - } - - if (access(filePath.c_str(), F_OK) != 0) { - APP_LOGE("can not access the file: %{private}s", filePath.c_str()); - return false; - } - return true; -} - -bool BundleMgrClient::TransformFileToJsonString(const std::string &resPath, std::string &profile) const -{ - if (!IsFileExisted(resPath, Constants::PROFILE_FILE_SUFFIX)) { - APP_LOGE("the file is not existed"); - return false; - } - std::fstream in; - in.open(resPath, std::ios_base::in | std::ios_base::binary); - if (!in.is_open()) { - APP_LOGE("the file cannot be open due to %{public}s", strerror(errno)); - return false; - } - in.seekg(0, std::ios::end); - size_t size = in.tellg(); - if (size == 0) { - APP_LOGE("the file is an empty file"); - in.close(); - return false; - } - in.seekg(0, std::ios::beg); - nlohmann::json profileJson = nlohmann::json::parse(in, nullptr, false); - if (profileJson.is_discarded()) { - APP_LOGE("bad profile file"); - in.close(); - return false; - } - profile = profileJson.dump(Constants::DUMP_INDENT); - in.close(); - return true; -} - -ErrCode BundleMgrClient::Connect() -{ - APP_LOGI("enter"); - - std::lock_guard lock(mutex_); - if (bundleMgr_ == nullptr) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemAbilityManager == nullptr) { - APP_LOGE("failed to get system ability manager"); - return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED; - } - - sptr remoteObject_ = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (remoteObject_ == nullptr || (bundleMgr_ = iface_cast(remoteObject_)) == nullptr) { - APP_LOGE("failed to get bundle mgr service remote object"); - return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED; - } - } - - APP_LOGI("end"); - - return ERR_OK; + return impl_->GetResConfigFile(abilityInfo, metadataName, profileInfos); } } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/innerkits/appexecfwk_core/src/bundlemgr/bundle_mgr_client_impl.cpp b/interfaces/innerkits/appexecfwk_core/src/bundlemgr/bundle_mgr_client_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38e8ecceaade7e3b4a1a893fa5283287609db8a7 --- /dev/null +++ b/interfaces/innerkits/appexecfwk_core/src/bundlemgr/bundle_mgr_client_impl.cpp @@ -0,0 +1,310 @@ +/* + * Copyright (c) 2021 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 "bundle_mgr_client_impl.h" + +#include +#include +#include + +#include "app_log_wrapper.h" +#include "bundle_constants.h" +#include "bundle_mgr_interface.h" +#include "iservice_registry.h" +#include "locale_config.h" +#include "nlohmann/json.hpp" +#include "system_ability_definition.h" + +using namespace OHOS::Global::Resource; +using namespace OHOS::Global::I18n; + +namespace OHOS { +namespace AppExecFwk { +BundleMgrClientImpl::BundleMgrClientImpl() +{ + APP_LOGI("create bundleMgrClientImpl"); + +} + +BundleMgrClientImpl::~BundleMgrClientImpl() +{ + APP_LOGI("destory bundleMgrClientImpl"); +} + +bool BundleMgrClientImpl::GetBundleNameForUid(const int uid, std::string &bundleName) +{ + APP_LOGI("enter"); + + ErrCode result = Connect(); + if (result != ERR_OK) { + APP_LOGE("failed to connect"); + return false; + } + + return bundleMgr_->GetBundleNameForUid(uid, bundleName); +} + +bool BundleMgrClientImpl::GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo) +{ + APP_LOGI("enter"); + + ErrCode result = Connect(); + if (result != ERR_OK) { + APP_LOGE("failed to connect"); + return false; + } + + return bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo); +} + +bool BundleMgrClientImpl::GetHapModuleInfo(const std::string &bundleName, const std::string &hapName, + HapModuleInfo &hapModuleInfo) +{ + ErrCode result = Connect(); + if (result != ERR_OK) { + APP_LOGE("failed to connect"); + return false; + } + + AbilityInfo info; + info.bundleName = bundleName; + info.package = hapName; + return bundleMgr_->GetHapModuleInfo(info, hapModuleInfo); +} + +bool BundleMgrClientImpl::GetResConfigFile(const HapModuleInfo &hapModuleInfo, const std::string &metadataName, + std::vector &profileInfos) const +{ + std::vector data = hapModuleInfo.metadata; + std::string resourcePath = hapModuleInfo.resourcePath; + if (!GetResProfileByMetadata(data, metadataName, resourcePath, profileInfos)) { + APP_LOGE("GetResProfileByMetadata failed"); + return false; + } + if (profileInfos.empty()) { + APP_LOGE("no valid file can be obtained"); + return false; + } + return true; +} + +bool BundleMgrClientImpl::GetResConfigFile(const ExtensionAbilityInfo &extensionInfo, const std::string &metadataName, + std::vector &profileInfos) const +{ + std::vector data = extensionInfo.metadata; + std::string resourcePath = extensionInfo.resourcePath; + if (!GetResProfileByMetadata(data, metadataName, resourcePath, profileInfos)) { + APP_LOGE("GetResProfileByMetadata failed"); + return false; + } + if (profileInfos.empty()) { + APP_LOGE("no valid file can be obtained"); + return false; + } + return true; +} + +bool BundleMgrClientImpl::GetResConfigFile(const AbilityInfo &abilityInfo, const std::string &metadataName, + std::vector &profileInfos) const +{ + std::vector data = abilityInfo.metadata; + std::string resourcePath = abilityInfo.resourcePath; + if (!GetResProfileByMetadata(data, metadataName, resourcePath, profileInfos)) { + APP_LOGE("GetResProfileByMetadata failed"); + return false; + } + if (profileInfos.empty()) { + APP_LOGE("no valid file can be obtained"); + return false; + } + return true; +} + +bool BundleMgrClientImpl::GetResProfileByMetadata(const std::vector &metadata, const std::string &metadataName, + const std ::string &resourcePath, std::vector &profileInfos) const +{ + if (metadata.empty()) { + APP_LOGE("GetResProfileByMetadata failed due to empty metadata"); + return false; + } + if (resourcePath.empty()) { + APP_LOGE("GetResProfileByMetadata failed due to empty resourcePath"); + return false; + } + std::shared_ptr resMgr = InitResMgr(resourcePath); + if (resMgr == nullptr) { + APP_LOGE("GetResProfileByMetadata init resMgr failed"); + return false; + } + + if (metadataName.empty()) { + for_each(metadata.begin(), metadata.end(), [this, &resMgr, &profileInfos](const Metadata& data)->void { + if (!GetResFromResMgr(data.resource, resMgr, profileInfos)) { + APP_LOGW("GetResFromResMgr failed"); + } + }); + } else { + for_each(metadata.begin(), metadata.end(), + [this, &resMgr, &metadataName, &profileInfos](const Metadata& data)->void { + if ((metadataName.compare(data.name) == 0) && (!GetResFromResMgr(data.resource, resMgr, profileInfos))) { + APP_LOGW("GetResFromResMgr failed"); + } + }); + } + + return true; +} + +std::shared_ptr BundleMgrClientImpl::InitResMgr(const std::string &resourcePath) const +{ + APP_LOGD("InitResMgr begin"); + if (resourcePath.empty()) { + APP_LOGE("InitResMgr failed due to invalid param"); + return nullptr; + } + std::shared_ptr resMgr(CreateResourceManager()); + if (!resMgr) { + APP_LOGE("InitResMgr resMgr is nullptr"); + return nullptr; + } + + std::unique_ptr resConfig(CreateResConfig()); + if (!resConfig) { + APP_LOGE("InitResMgr resConfig is nullptr"); + return nullptr; + } + resConfig->SetLocaleInfo(LocaleConfig::GetSystemLanguage().c_str(), LocaleConfig::GetSystemLocale().c_str(), + LocaleConfig::GetSystemRegion().c_str()); + resMgr->UpdateResConfig(*resConfig); + + APP_LOGD("resourcePath is %{public}s", resourcePath.c_str()); + if (!resourcePath.empty() && !resMgr->AddResource(resourcePath.c_str())) { + APP_LOGE("InitResMgr AddResource failed"); + return nullptr; + } + return resMgr; +} + +bool BundleMgrClientImpl::GetResFromResMgr(const std::string &resName, const std::shared_ptr &resMgr, + std::vector &profileInfos) const +{ + APP_LOGD("GetResFromResMgr begin"); + if (resName.empty()) { + APP_LOGE("GetResFromResMgr res name is empty"); + return false; + } + + size_t pos = resName.rfind(Constants::PROFILE_FILE_COLON); + if ((pos == std::string::npos) || (pos == resName.length() - 1)) { + APP_LOGE("GetResFromResMgr res name is invalid"); + return false; + } + std::string profileName = resName.substr(pos + 1); + std::string resPath; + if (resMgr->GetProfileByName(profileName.c_str(), resPath) != SUCCESS) { + APP_LOGE("GetResFromResMgr profileName cannot be found"); + return false; + } + APP_LOGD("GetResFromResMgr resPath is %{public}s", resPath.c_str()); + std::string profile; + if (!TransformFileToJsonString(resPath, profile)) { + return false; + } + profileInfos.emplace_back(profile); + return true; +} + +bool BundleMgrClientImpl::IsFileExisted(const std::string &filePath, const std::string &suffix) const +{ + if (filePath.empty()) { + APP_LOGE("the file is not existed due to empty file path"); + return false; + } + + auto position = filePath.rfind('.'); + if (position == std::string::npos) { + APP_LOGE("filePath no suffix"); + return false; + } + + std::string suffixStr = filePath.substr(position); + if (LowerStr(suffixStr) != suffix) { + APP_LOGE("file is not json"); + return false; + } + + if (access(filePath.c_str(), F_OK) != 0) { + APP_LOGE("can not access the file: %{private}s", filePath.c_str()); + return false; + } + return true; +} + +bool BundleMgrClientImpl::TransformFileToJsonString(const std::string &resPath, std::string &profile) const +{ + if (!IsFileExisted(resPath, Constants::PROFILE_FILE_SUFFIX)) { + APP_LOGE("the file is not existed"); + return false; + } + std::fstream in; + in.open(resPath, std::ios_base::in | std::ios_base::binary); + if (!in.is_open()) { + APP_LOGE("the file cannot be open due to %{public}s", strerror(errno)); + return false; + } + in.seekg(0, std::ios::end); + size_t size = in.tellg(); + if (size == 0) { + APP_LOGE("the file is an empty file"); + in.close(); + return false; + } + in.seekg(0, std::ios::beg); + nlohmann::json profileJson = nlohmann::json::parse(in, nullptr, false); + if (profileJson.is_discarded()) { + APP_LOGE("bad profile file"); + in.close(); + return false; + } + profile = profileJson.dump(Constants::DUMP_INDENT); + in.close(); + return true; +} + +ErrCode BundleMgrClientImpl::Connect() +{ + APP_LOGI("enter"); + + std::lock_guard lock(mutex_); + if (bundleMgr_ == nullptr) { + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + APP_LOGE("failed to get system ability manager"); + return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED; + } + + sptr remoteObject_ = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (remoteObject_ == nullptr || (bundleMgr_ = iface_cast(remoteObject_)) == nullptr) { + APP_LOGE("failed to get bundle mgr service remote object"); + return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED; + } + } + + APP_LOGI("end"); + + return ERR_OK; +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/tools/test/moduletest/bm/BUILD.gn b/tools/test/moduletest/bm/BUILD.gn index 29cd9282c5a60294ab51b492b1d38d368ba6b7a9..ce2c150e52aa00fedda879a54cf35b51e03f932c 100644 --- a/tools/test/moduletest/bm/BUILD.gn +++ b/tools/test/moduletest/bm/BUILD.gn @@ -63,8 +63,8 @@ ohos_moduletest("bm_command_dump_module_test") { external_deps = [ "ability_runtime:base", - "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "ces_standard:cesfwk_innerkits", "distributeddatamgr:distributeddata_inner", "hiviewdfx_hilog_native:libhilog", @@ -106,8 +106,8 @@ ohos_moduletest("bm_command_install_module_test") { external_deps = [ "ability_runtime:base", - "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "ces_standard:cesfwk_innerkits", "distributeddatamgr:distributeddata_inner", "hiviewdfx_hilog_native:libhilog", @@ -149,8 +149,8 @@ ohos_moduletest("bm_command_uninstall_module_test") { external_deps = [ "ability_runtime:base", - "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "ces_standard:cesfwk_innerkits", "distributeddatamgr:distributeddata_inner", "hiviewdfx_hilog_native:libhilog", diff --git a/tools/test/unittest/bm/BUILD.gn b/tools/test/unittest/bm/BUILD.gn index 2968bcd48bbb86e8d4f17c676c00292beeac0866..2abbb86928920fe28116e23ca4a079a54cf2b9e2 100644 --- a/tools/test/unittest/bm/BUILD.gn +++ b/tools/test/unittest/bm/BUILD.gn @@ -72,9 +72,9 @@ ohos_unittest("bm_command_dump_test") { external_deps = [ "ability_runtime:want", - "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", "appverify:libhapverify", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "ces_standard:cesfwk_innerkits", "distributeddatamgr:distributeddata_inner", "eventhandler:libeventhandler", @@ -121,9 +121,9 @@ ohos_unittest("bm_command_install_test") { external_deps = [ "ability_runtime:want", - "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", "appverify:libhapverify", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "ces_standard:cesfwk_innerkits", "distributeddatamgr:distributeddata_inner", "eventhandler:libeventhandler", @@ -170,9 +170,9 @@ ohos_unittest("bm_command_test") { external_deps = [ "ability_runtime:want", - "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", "appverify:libhapverify", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "ces_standard:cesfwk_innerkits", "distributeddatamgr:distributeddata_inner", "eventhandler:libeventhandler", @@ -219,9 +219,9 @@ ohos_unittest("bm_command_uninstall_test") { external_deps = [ "ability_runtime:want", - "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", "appverify:libhapverify", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "ces_standard:cesfwk_innerkits", "distributeddatamgr:distributeddata_inner", "eventhandler:libeventhandler",