diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index 0597162fb6043f1f692445f90c38d6d681d26edc..51f1a782e5e086b84932c294610770463b791b3c 100644 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -71,6 +71,9 @@ namespace AppExecFwk { using PermissionKit = OHOS::Security::Permission::PermissionKit; using PermissionState = OHOS::Security::Permission::PermissionState; +static OHOS::sptr bundleMgr_ = nullptr; +static std::mutex bundleMgrMutex_; + // REGISTER_AA(Ability) const std::string Ability::SYSTEM_UI("com.ohos.systemui"); const std::string Ability::STATUS_BAR("com.ohos.systemui.statusbar.MainAbility"); @@ -3089,24 +3092,27 @@ std::string Ability::GetErrorMsg(const ErrCode errorCode) */ sptr Ability::GetBundleMgr() { - HILOG_INFO("%{public}s called.", __func__); - if (iBundleMgr_ == nullptr) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (remoteObject == nullptr) { - HILOG_ERROR("%{public}s error, failed to get bundle manager service.", __func__); - return nullptr; - } - - iBundleMgr_ = iface_cast(remoteObject); - if (iBundleMgr_ == nullptr) { - HILOG_ERROR("%{public}s error, failed to get bundle manager service", __func__); - return nullptr; + if (bundleMgr_ == nullptr) { + std::lock_guard lock(bundleMgrMutex_); + if (bundleMgr_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbilityManager is null"); + return nullptr; + } + auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrSa == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbility is null"); + return nullptr; + } + auto bundleMgr = OHOS::iface_cast(bundleMgrSa); + if (bundleMgr == nullptr) { + HILOG_ERROR("GetBundleMgr iface_cast get null"); + } + bundleMgr_ = bundleMgr; } } - - return iBundleMgr_; + return bundleMgr_; } /** @@ -3116,8 +3122,7 @@ sptr Ability::GetBundleMgr() void Ability::SetBundleManager(const sptr &bundleManager) { HILOG_INFO("%{public}s called.", __func__); - - iBundleMgr_ = bundleManager; + bundleMgr_ = bundleManager; } #ifdef SUPPORT_GRAPHICS diff --git a/frameworks/kits/appkit/native/app/include/sys_mgr_client.h b/frameworks/kits/appkit/native/app/include/sys_mgr_client.h index e5b7be41f9c19e5b679a733e6f83508497c1ace8..2f8103d516f0502aa63c4c4e9e046b6f36abfdd1 100644 --- a/frameworks/kits/appkit/native/app/include/sys_mgr_client.h +++ b/frameworks/kits/appkit/native/app/include/sys_mgr_client.h @@ -56,9 +56,9 @@ public: void UnregisterSystemAbility(const int32_t systemAbilityId); private: - OHOS::sptr abilityManager_; - std::mutex saMutex_; std::unordered_map> servicesMap_; + static sptr remoteObject_; + static std::mutex remoteObjectMutex_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/kits/appkit/native/app/src/sys_mgr_client.cpp b/frameworks/kits/appkit/native/app/src/sys_mgr_client.cpp index c3c371f720df8d298db9b062433ba24e571d2e28..fc903fb93760db09fc5f3befe8477a268f841a87 100644 --- a/frameworks/kits/appkit/native/app/src/sys_mgr_client.cpp +++ b/frameworks/kits/appkit/native/app/src/sys_mgr_client.cpp @@ -23,7 +23,10 @@ namespace OHOS { namespace AppExecFwk { -SysMrgClient::SysMrgClient() : abilityManager_(nullptr) +OHOS::sptr SysMrgClient::remoteObject_ = nullptr; +std::mutex SysMrgClient::remoteObjectMutex_; + +SysMrgClient::SysMrgClient() {} SysMrgClient::~SysMrgClient() @@ -37,18 +40,22 @@ SysMrgClient::~SysMrgClient() */ sptr SysMrgClient::GetSystemAbility(const int32_t systemAbilityId) { - // use single instance of abilityManager_ - if (abilityManager_ == nullptr) { - std::lock_guard lock(saMutex_); - if (abilityManager_ == nullptr) { - abilityManager_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (abilityManager_ == nullptr) { - HILOG_ERROR("fail to GetSystemAbility abilityManager_ == nullptr."); + if (remoteObject_ == nullptr) { + std::lock_guard lock(remoteObjectMutex_); + if (remoteObject_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetSystemAbilityManager for %{public}d failed.", systemAbilityId); + return nullptr; + } + remoteObject_ = systemAbilityManager->GetSystemAbility(systemAbilityId); + if (remoteObject_ == nullptr) { + HILOG_ERROR("GetSystemAbility for %{public}d failed.", systemAbilityId); return nullptr; } } } - return abilityManager_->GetSystemAbility(systemAbilityId); + return remoteObject_; } /** diff --git a/services/abilitymgr/include/sa_mgr_client.h b/services/abilitymgr/include/sa_mgr_client.h index dfc80b191347a2786c8e18707df18d3ba4589d42..8477f66046502b6ba874f612cc37c4e994c242d2 100644 --- a/services/abilitymgr/include/sa_mgr_client.h +++ b/services/abilitymgr/include/sa_mgr_client.h @@ -38,9 +38,9 @@ public: void RegisterSystemAbility(const int32_t systemAbilityId, sptr broker); private: - OHOS::sptr saMgr_; - std::mutex saMutex_; std::unordered_map> servicesMap_; + static OHOS::sptr remoteObject_; + static std::mutex saMutex_; }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/sa_mgr_client.cpp b/services/abilitymgr/src/sa_mgr_client.cpp index d155c1ae26078872b1cc8e8813de693e669ebade..0d0019c9f27d1e49c805298eef4239543ce0307d 100644 --- a/services/abilitymgr/src/sa_mgr_client.cpp +++ b/services/abilitymgr/src/sa_mgr_client.cpp @@ -23,7 +23,10 @@ namespace OHOS { namespace AAFwk { -SaMgrClient::SaMgrClient() : saMgr_(nullptr) +OHOS::sptr SaMgrClient::remoteObject_ = nullptr; +std::mutex SaMgrClient::saMutex_; + +SaMgrClient::SaMgrClient() {} SaMgrClient::~SaMgrClient() @@ -31,18 +34,22 @@ SaMgrClient::~SaMgrClient() sptr SaMgrClient::GetSystemAbility(const int32_t systemAbilityId) { - // use single instance of saMgr_ - if (saMgr_ == nullptr) { + if (remoteObject_ == nullptr) { std::lock_guard lock(saMutex_); - if (saMgr_ == nullptr) { - saMgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (saMgr_ == nullptr) { - HILOG_ERROR("Fail to get registry."); + if (remoteObject_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetSystemAbilityManager for %{public}d failed.", systemAbilityId); + return nullptr; + } + remoteObject_ = systemAbilityManager->GetSystemAbility(systemAbilityId); + if (remoteObject_ == nullptr) { + HILOG_ERROR("GetSystemAbility for %{public}d failed.", systemAbilityId); return nullptr; } } } - return saMgr_->GetSystemAbility(systemAbilityId); + return remoteObject_; } void SaMgrClient::RegisterSystemAbility( diff --git a/services/appmgr/include/remote_client_manager.h b/services/appmgr/include/remote_client_manager.h index 2372b5cf28207f235bb29eeb18c204d72a7f1079..4aef9a4f5ea6423fdcc9f8b8cf836f1f07768d86 100644 --- a/services/appmgr/include/remote_client_manager.h +++ b/services/appmgr/include/remote_client_manager.h @@ -61,8 +61,9 @@ public: private: std::shared_ptr appSpawnClient_; - sptr bundleManager_; std::shared_ptr nwebSpawnClient_; + static sptr bundleManager_; + static std::mutex bundleMgrMutex_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/remote_client_manager.cpp b/services/appmgr/src/remote_client_manager.cpp index a1b5cda7a178c3c4c2c7889ea9261f5ef599af49..6280ff4049176e8950fa49b72045790fd934ffa1 100644 --- a/services/appmgr/src/remote_client_manager.cpp +++ b/services/appmgr/src/remote_client_manager.cpp @@ -22,6 +22,9 @@ namespace OHOS { namespace AppExecFwk { +sptr RemoteClientManager::bundleManager_ = nullptr; +std::mutex RemoteClientManager::bundleMgrMutex_; + RemoteClientManager::RemoteClientManager() : appSpawnClient_(std::make_shared()), nwebSpawnClient_(std::make_shared(true)) {} @@ -45,12 +48,23 @@ void RemoteClientManager::SetSpawnClient(const std::shared_ptr & sptr RemoteClientManager::GetBundleManager() { if (bundleManager_ == nullptr) { - sptr systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemManager != nullptr) { - bundleManager_ = - iface_cast(systemManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID)); - } else { - HILOG_ERROR("AppMgrServiceInner::GetBundleManager fail to get SAMGR"); + std::lock_guard lock(bundleMgrMutex_); + if (bundleManager_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbilityManager is null"); + return nullptr; + } + auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrSa == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbility is null"); + return nullptr; + } + auto bundleMgr = OHOS::iface_cast(bundleMgrSa); + if (bundleMgr == nullptr) { + HILOG_ERROR("GetBundleMgr iface_cast get null"); + } + bundleManager_ = bundleMgr; } } return bundleManager_; diff --git a/services/formmgr/include/form_bms_helper.h b/services/formmgr/include/form_bms_helper.h index 4e5523ba3bc681a495af091c8a76ca6f767a5ad7..bb34553e1e63c790f3b6d67c0bbe254d5105d20a 100644 --- a/services/formmgr/include/form_bms_helper.h +++ b/services/formmgr/include/form_bms_helper.h @@ -71,7 +71,8 @@ private: std::string GenerateModuleKey(const std::string &bundleName, const std::string &moduleName) const; private: - sptr iBundleMgr_ = nullptr; + static sptr bundleMgr_; + static std::mutex bundleMgrMutex_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/formmgr/src/form_bms_helper.cpp b/services/formmgr/src/form_bms_helper.cpp index de4de2ed6fb9af86280cf3d93dfa8a9583611a65..25979a87967a43be9285971cd8cd014b0a04e328 100644 --- a/services/formmgr/src/form_bms_helper.cpp +++ b/services/formmgr/src/form_bms_helper.cpp @@ -24,6 +24,9 @@ namespace OHOS { namespace AppExecFwk { +sptr FormBmsHelper::bundleMgr_ = nullptr; +std::mutex FormBmsHelper::bundleMgrMutex_; + FormBmsHelper::FormBmsHelper() {} @@ -36,24 +39,27 @@ FormBmsHelper::~FormBmsHelper() */ sptr FormBmsHelper::GetBundleMgr() { - HILOG_INFO("%{public}s called.", __func__); - - if (iBundleMgr_ == nullptr) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (remoteObject == nullptr) { - HILOG_ERROR("%{public}s error, failed to get bundle manager service.", __func__); - return nullptr; - } - - iBundleMgr_ = iface_cast(remoteObject); - if (iBundleMgr_ == nullptr) { - HILOG_ERROR("%{public}s error, failed to get bundle manager service", __func__); - return nullptr; + if (bundleMgr_ == nullptr) { + std::lock_guard lock(bundleMgrMutex_); + if (bundleMgr_ == nullptr) { + auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbilityManager is null"); + return nullptr; + } + auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrSa == nullptr) { + HILOG_ERROR("GetBundleMgr GetSystemAbility is null"); + return nullptr; + } + auto bundleMgr = OHOS::iface_cast(bundleMgrSa); + if (bundleMgr == nullptr) { + HILOG_ERROR("GetBundleMgr iface_cast get null"); + } + bundleMgr_ = bundleMgr; } } - return iBundleMgr_; + return bundleMgr_; } /** @@ -63,8 +69,7 @@ sptr FormBmsHelper::GetBundleMgr() void FormBmsHelper::SetBundleManager(const sptr &bundleManager) { HILOG_INFO("%{public}s called.", __func__); - - iBundleMgr_ = bundleManager; + bundleMgr_ = bundleManager; } /** * @brief Notify module removable.