diff --git a/services/bundlemgr/include/free_install/bms_ecological_rule_mgr_service_client.h b/services/bundlemgr/include/free_install/bms_ecological_rule_mgr_service_client.h index 67b4d11c94c7dab6f0355b945414fec42db68032..9e63154b5bf76bc4f5537f55c248eba94f5e8636 100644 --- a/services/bundlemgr/include/free_install/bms_ecological_rule_mgr_service_client.h +++ b/services/bundlemgr/include/free_install/bms_ecological_rule_mgr_service_client.h @@ -18,6 +18,7 @@ #include #include "iremote_proxy.h" #include "bms_ecological_rule_mgr_service_interface.h" +#include "singleton.h" namespace OHOS { namespace AppExecFwk { @@ -49,23 +50,21 @@ private: static inline BrokerDelegator delegator_; }; -class BmsEcologicalRuleMgrServiceClient : public RefBase { +class BmsEcologicalRuleMgrServiceClient : public DelayedSingleton { public: - DISALLOW_COPY_AND_MOVE(BmsEcologicalRuleMgrServiceClient); - static sptr GetInstance(); + BmsEcologicalRuleMgrServiceClient(); + ~BmsEcologicalRuleMgrServiceClient(); + void OnRemoteSaDied(const wptr &object); int32_t QueryFreeInstallExperience(const Want &want, const BmsCallerInfo &callerInfo, BmsExperienceRule &rule); private: - BmsEcologicalRuleMgrServiceClient() {}; - ~BmsEcologicalRuleMgrServiceClient(); - static sptr ConnectService(); - static bool CheckConnectService(); + sptr ConnectService(); + bool CheckConnectService(); - static mutex instanceLock_; - static sptr instance_; - static sptr bmsEcologicalRuleMgrServiceProxy_; - static sptr deathRecipient_; + std::mutex proxyMutex_; + sptr bmsEcologicalRuleMgrServiceProxy_; + sptr deathRecipient_; static string ERMS_ORIGINAL_TARGET; }; diff --git a/services/bundlemgr/src/free_install/bms_ecological_rule_mgr_service_client.cpp b/services/bundlemgr/src/free_install/bms_ecological_rule_mgr_service_client.cpp index 96383b84e476d277742c0e6ec1e98591f1e31461..3425d72a9a455ff44ce4465b7a4fcf73c964b117 100644 --- a/services/bundlemgr/src/free_install/bms_ecological_rule_mgr_service_client.cpp +++ b/services/bundlemgr/src/free_install/bms_ecological_rule_mgr_service_client.cpp @@ -25,15 +25,14 @@ using namespace std::chrono; static inline const std::u16string ERMS_INTERFACE_TOKEN = u"ohos.cloud.ecologicalrulemgrservice.IEcologicalRuleMgrService"; -std::mutex BmsEcologicalRuleMgrServiceClient::instanceLock_; -sptr BmsEcologicalRuleMgrServiceClient::instance_; -sptr BmsEcologicalRuleMgrServiceClient::bmsEcologicalRuleMgrServiceProxy_; -sptr BmsEcologicalRuleMgrServiceClient::deathRecipient_; - std::string BmsEcologicalRuleMgrServiceClient::ERMS_ORIGINAL_TARGET = "ecological_experience_original_target"; +BmsEcologicalRuleMgrServiceClient::BmsEcologicalRuleMgrServiceClient() +{} + BmsEcologicalRuleMgrServiceClient::~BmsEcologicalRuleMgrServiceClient() { + std::lock_guard lock(proxyMutex_); if (bmsEcologicalRuleMgrServiceProxy_ != nullptr) { auto remoteObj = bmsEcologicalRuleMgrServiceProxy_->AsObject(); if (remoteObj != nullptr) { @@ -42,17 +41,6 @@ BmsEcologicalRuleMgrServiceClient::~BmsEcologicalRuleMgrServiceClient() } } -sptr BmsEcologicalRuleMgrServiceClient::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard autoLock(instanceLock_); - if (instance_ == nullptr) { - instance_ = (new (std::nothrow) BmsEcologicalRuleMgrServiceClient); - } - } - return instance_; -} - sptr BmsEcologicalRuleMgrServiceClient::ConnectService() { sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); @@ -80,6 +68,7 @@ sptr BmsEcologicalRuleMgrServiceClient::ConnectSer bool BmsEcologicalRuleMgrServiceClient::CheckConnectService() { + std::lock_guard lock(proxyMutex_); if (bmsEcologicalRuleMgrServiceProxy_ == nullptr) { LOG_W(BMS_TAG_DEFAULT, "redo ConnectService"); bmsEcologicalRuleMgrServiceProxy_ = ConnectService(); @@ -93,6 +82,7 @@ bool BmsEcologicalRuleMgrServiceClient::CheckConnectService() void BmsEcologicalRuleMgrServiceClient::OnRemoteSaDied(const wptr &object) { + std::lock_guard lock(proxyMutex_); bmsEcologicalRuleMgrServiceProxy_ = ConnectService(); } @@ -106,6 +96,7 @@ int32_t BmsEcologicalRuleMgrServiceClient::QueryFreeInstallExperience(const OHOS LOG_W(BMS_TAG_DEFAULT, "check Connect SA Failed"); return OHOS::AppExecFwk::IBmsEcologicalRuleMgrService::ErrCode::ERR_FAILED; } + std::lock_guard lock(proxyMutex_); int32_t res = bmsEcologicalRuleMgrServiceProxy_->QueryFreeInstallExperience(want, callerInfo, rule); if (rule.replaceWant != nullptr) { rule.replaceWant->SetParam(ERMS_ORIGINAL_TARGET, want.ToString()); @@ -117,6 +108,7 @@ int32_t BmsEcologicalRuleMgrServiceClient::QueryFreeInstallExperience(const OHOS void BmsEcologicalRuleMgrServiceDeathRecipient::OnRemoteDied(const wptr &object) { + LOG_I(BMS_TAG_DEFAULT, "OnRemoteDied"); BmsEcologicalRuleMgrServiceClient::GetInstance()->OnRemoteSaDied(object); } diff --git a/services/bundlemgr/src/free_install/bundle_connect_ability_mgr.cpp b/services/bundlemgr/src/free_install/bundle_connect_ability_mgr.cpp index 15c8a7e88452429abe95fb6d31257b68bde9c05e..6a7f68e2d7c54a4b0c1bada2290058e6db8608c1 100644 --- a/services/bundlemgr/src/free_install/bundle_connect_ability_mgr.cpp +++ b/services/bundlemgr/src/free_install/bundle_connect_ability_mgr.cpp @@ -1225,13 +1225,7 @@ void BundleConnectAbilityMgr::UpgradeAtomicService(const Want &want, int32_t use bool BundleConnectAbilityMgr::CheckEcologicalRule(const Want &want, ErmsCallerInfo &callerInfo, BmsExperienceRule &rule) { - sptr instance_ = - BmsEcologicalRuleMgrServiceClient::GetInstance(); - if (instance_ == nullptr) { - LOG_E(BMS_TAG_DEFAULT, "Failed to get instance from erms"); - return false; - } - int ret = instance_->QueryFreeInstallExperience(want, callerInfo, rule); + int ret = BmsEcologicalRuleMgrServiceClient::GetInstance()->QueryFreeInstallExperience(want, callerInfo, rule); if (ret != ERR_OK) { LOG_E(BMS_TAG_DEFAULT, "Failed to query free install experience from erms"); return false;