From 0859f3ecaa14bdbdfdbe5550a5daeefc1e09dec2 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Wed, 6 Dec 2023 13:56:50 +0800 Subject: [PATCH] modify code Signed-off-by: xinking129 --- .../bundle_mgr_helper.cpp | 158 +++++++----------- .../bundle_mgr_helper.h | 4 +- 2 files changed, 60 insertions(+), 102 deletions(-) diff --git a/frameworks/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.cpp b/frameworks/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.cpp index add964941d..4dc1f21b47 100644 --- a/frameworks/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.cpp +++ b/frameworks/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.cpp @@ -26,7 +26,7 @@ BundleMgrHelper::BundleMgrHelper() {} BundleMgrHelper::~BundleMgrHelper() { - if (bundleMgr_ != nullptr && deathRecipient_ != nullptr) { + if (bundleMgr_ != nullptr && bundleMgr_->AsObject() != nullptr && deathRecipient_ != nullptr) { bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_); } } @@ -34,7 +34,7 @@ BundleMgrHelper::~BundleMgrHelper() ErrCode BundleMgrHelper::GetNameForUid(const int32_t uid, std::string &name) { HILOG_DEBUG("Called."); - if (Connect() != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; } @@ -46,8 +46,7 @@ bool BundleMgrHelper::GetBundleInfo(const std::string &bundleName, const BundleF int32_t userId) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -63,8 +62,7 @@ ErrCode BundleMgrHelper::InstallSandboxApp(const std::string &bundleName, int32_ HILOG_ERROR("The bundleName is empty."); return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR; } - ErrCode result = ConnectBundleInstaller(); - if (result != ERR_OK) { + if (ConnectBundleInstaller() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR; } @@ -79,8 +77,7 @@ ErrCode BundleMgrHelper::UninstallSandboxApp(const std::string &bundleName, int3 HILOG_ERROR("The params are invalid."); return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR; } - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR; } @@ -91,8 +88,7 @@ ErrCode BundleMgrHelper::UninstallSandboxApp(const std::string &bundleName, int3 ErrCode BundleMgrHelper::GetUninstalledBundleInfo(const std::string bundleName, BundleInfo &bundleInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; } @@ -108,8 +104,7 @@ ErrCode BundleMgrHelper::GetSandboxBundleInfo( HILOG_ERROR("The params are invalid."); return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR; } - ErrCode result = ConnectBundleInstaller(); - if (result != ERR_OK) { + if (ConnectBundleInstaller() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR; } @@ -125,8 +120,7 @@ ErrCode BundleMgrHelper::GetSandboxAbilityInfo(const Want &want, int32_t appInde HILOG_ERROR("The params are invalid."); return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR; } - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR; } @@ -142,8 +136,7 @@ ErrCode BundleMgrHelper::GetSandboxExtAbilityInfos(const Want &want, int32_t app HILOG_ERROR("The params are invalid."); return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR; } - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR; } @@ -159,8 +152,7 @@ ErrCode BundleMgrHelper::GetSandboxHapModuleInfo(const AbilityInfo &abilityInfo, HILOG_ERROR("The params are invalid."); return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR; } - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR; } @@ -168,7 +160,7 @@ ErrCode BundleMgrHelper::GetSandboxHapModuleInfo(const AbilityInfo &abilityInfo, return bundleMgr_->GetSandboxHapModuleInfo(abilityInfo, appIndex, userId, hapModuleInfo); } -ErrCode BundleMgrHelper::Connect() +sptr BundleMgrHelper::Connect() { HILOG_DEBUG("Called."); std::lock_guard lock(mutex_); @@ -177,13 +169,13 @@ ErrCode BundleMgrHelper::Connect() SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityManager == nullptr) { HILOG_ERROR("Failed to get system ability manager."); - return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED; + return nullptr; } sptr remoteObject_ = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); if (remoteObject_ == nullptr || (bundleMgr_ = iface_cast(remoteObject_)) == nullptr) { HILOG_ERROR("Failed to get bundle mgr service remote object."); - return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED; + return nullptr; } std::weak_ptr weakPtr = shared_from_this(); auto deathCallback = [weakPtr](const wptr& object) { @@ -197,21 +189,22 @@ ErrCode BundleMgrHelper::Connect() deathRecipient_ = new (std::nothrow) BundleMgrServiceDeathRecipient(deathCallback); if (deathRecipient_ == nullptr) { HILOG_ERROR("Failed to create death recipient ptr deathRecipient_!"); - return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED; + return nullptr; + } + if (bundleMgr_->AsObject() != nullptr) { + bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_); } - bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_); } - return ERR_OK; + return bundleMgr_; } -ErrCode BundleMgrHelper::ConnectBundleInstaller() +sptr BundleMgrHelper::ConnectBundleInstaller() { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); - return result; + return nullptr; } std::lock_guard lock(mutex_); @@ -219,11 +212,11 @@ ErrCode BundleMgrHelper::ConnectBundleInstaller() bundleInstaller_ = bundleMgr_->GetBundleInstaller(); if ((bundleInstaller_ == nullptr) || (bundleInstaller_->AsObject() == nullptr)) { HILOG_ERROR("Failed to get bundle installer proxy."); - return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED; + return nullptr; } } - return ERR_OK; + return bundleInstaller_; } void BundleMgrHelper::OnDeath() @@ -243,8 +236,7 @@ bool BundleMgrHelper::GetBundleInfo(const std::string &bundleName, int32_t flags BundleInfo &bundleInfo, int32_t userId) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -255,8 +247,7 @@ bool BundleMgrHelper::GetBundleInfo(const std::string &bundleName, int32_t flags bool BundleMgrHelper::GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -267,8 +258,7 @@ bool BundleMgrHelper::GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModule std::string BundleMgrHelper::GetAbilityLabel(const std::string &bundleName, const std::string &abilityName) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ""; } @@ -279,8 +269,7 @@ std::string BundleMgrHelper::GetAbilityLabel(const std::string &bundleName, cons std::string BundleMgrHelper::GetAppType(const std::string &bundleName) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ""; } @@ -292,8 +281,7 @@ ErrCode BundleMgrHelper::GetBaseSharedBundleInfos( const std::string &bundleName, std::vector &baseSharedBundleInfos) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; } @@ -304,8 +292,7 @@ ErrCode BundleMgrHelper::GetBaseSharedBundleInfos( ErrCode BundleMgrHelper::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; } @@ -316,8 +303,7 @@ ErrCode BundleMgrHelper::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleI ErrCode BundleMgrHelper::GetDependentBundleInfo(const std::string &sharedBundleName, BundleInfo &sharedBundleInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; } @@ -328,8 +314,7 @@ ErrCode BundleMgrHelper::GetDependentBundleInfo(const std::string &sharedBundleN bool BundleMgrHelper::GetGroupDir(const std::string &dataGroupId, std::string &dir) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -340,8 +325,7 @@ bool BundleMgrHelper::GetGroupDir(const std::string &dataGroupId, std::string &d sptr BundleMgrHelper::GetOverlayManagerProxy() { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return nullptr; } @@ -352,8 +336,7 @@ sptr BundleMgrHelper::GetOverlayManagerProxy() bool BundleMgrHelper::QueryAbilityInfo(const Want &want, AbilityInfo &abilityInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -364,8 +347,7 @@ bool BundleMgrHelper::QueryAbilityInfo(const Want &want, AbilityInfo &abilityInf bool BundleMgrHelper::QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -376,8 +358,7 @@ bool BundleMgrHelper::QueryAbilityInfo(const Want &want, int32_t flags, int32_t bool BundleMgrHelper::GetBundleInfos(int32_t flags, std::vector &bundleInfos, int32_t userId) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -388,8 +369,7 @@ bool BundleMgrHelper::GetBundleInfos(int32_t flags, std::vector &bun bool BundleMgrHelper::GetBundleInfos(const BundleFlag flag, std::vector &bundleInfos, int32_t userId) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -400,8 +380,7 @@ bool BundleMgrHelper::GetBundleInfos(const BundleFlag flag, std::vector BundleMgrHelper::GetQuickFixManagerProxy() { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return nullptr; } @@ -412,8 +391,7 @@ sptr BundleMgrHelper::GetQuickFixManagerProxy() bool BundleMgrHelper::ProcessPreload(const Want &want) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -424,8 +402,7 @@ bool BundleMgrHelper::ProcessPreload(const Want &want) sptr BundleMgrHelper::GetAppControlProxy() { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return nullptr; } @@ -437,8 +414,7 @@ bool BundleMgrHelper::QueryExtensionAbilityInfos(const Want &want, const int32_t std::vector &extensionInfos) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -450,8 +426,7 @@ bool BundleMgrHelper::GetApplicationInfo( const std::string &appName, const ApplicationFlag flag, const int32_t userId, ApplicationInfo &appInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -463,8 +438,7 @@ bool BundleMgrHelper::GetApplicationInfo( const std::string &appName, int32_t flags, int32_t userId, ApplicationInfo &appInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -475,8 +449,7 @@ bool BundleMgrHelper::GetApplicationInfo( bool BundleMgrHelper::UnregisterBundleEventCallback(const sptr &bundleEventCallback) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -488,8 +461,7 @@ bool BundleMgrHelper::QueryExtensionAbilityInfoByUri( const std::string &uri, int32_t userId, ExtensionAbilityInfo &extensionAbilityInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -501,8 +473,7 @@ bool BundleMgrHelper::ImplicitQueryInfoByPriority( const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -513,8 +484,7 @@ bool BundleMgrHelper::ImplicitQueryInfoByPriority( bool BundleMgrHelper::QueryAbilityInfoByUri(const std::string &abilityUri, int32_t userId, AbilityInfo &abilityInfo) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -526,8 +496,7 @@ bool BundleMgrHelper::QueryAbilityInfo( const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo, const sptr &callBack) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -538,8 +507,7 @@ bool BundleMgrHelper::QueryAbilityInfo( void BundleMgrHelper::UpgradeAtomicService(const Want &want, int32_t userId) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return; } @@ -551,8 +519,7 @@ bool BundleMgrHelper::ImplicitQueryInfos(const Want &want, int32_t flags, int32_ std::vector &abilityInfos, std::vector &extensionInfos) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -563,8 +530,7 @@ bool BundleMgrHelper::ImplicitQueryInfos(const Want &want, int32_t flags, int32_ bool BundleMgrHelper::CleanBundleDataFiles(const std::string &bundleName, const int32_t userId) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -576,8 +542,7 @@ bool BundleMgrHelper::QueryDataGroupInfos( const std::string &bundleName, int32_t userId, std::vector &infos) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -588,8 +553,7 @@ bool BundleMgrHelper::QueryDataGroupInfos( bool BundleMgrHelper::GetBundleGidsByUid(const std::string &bundleName, const int32_t &uid, std::vector &gids) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -600,8 +564,7 @@ bool BundleMgrHelper::GetBundleGidsByUid(const std::string &bundleName, const in bool BundleMgrHelper::RegisterBundleEventCallback(const sptr &bundleEventCallback) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return false; } @@ -612,8 +575,7 @@ bool BundleMgrHelper::RegisterBundleEventCallback(const sptr BundleMgrHelper::GetDefaultAppProxy() { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return nullptr; } @@ -661,8 +620,7 @@ ErrCode BundleMgrHelper::GetJsonProfile(ProfileType profileType, const std::stri const std::string &moduleName, std::string &profile, int32_t userId) { HILOG_DEBUG("Called."); - ErrCode result = Connect(); - if (result != ERR_OK) { + if (Connect() == nullptr) { HILOG_ERROR("Failed to connect."); return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; } diff --git a/interfaces/kits/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.h b/interfaces/kits/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.h index 03578ebd7d..3a464a2984 100644 --- a/interfaces/kits/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.h +++ b/interfaces/kits/native/appkit/ability_bundle_manager_helper/bundle_mgr_helper.h @@ -86,8 +86,8 @@ public: sptr GetDefaultAppProxy(); private: - ErrCode Connect(); - ErrCode ConnectBundleInstaller(); + sptr Connect(); + sptr ConnectBundleInstaller(); void OnDeath(); private: