diff --git a/bundle.json b/bundle.json index 5a87a4687de7b44d5d85c8a60070cc6db629e088..a33e05268a261c60778f6285e0114e294aad6267 100644 --- a/bundle.json +++ b/bundle.json @@ -16,7 +16,8 @@ "subsystem": "systemabilitymgr", "features": [ "samgr_feature_coverage", - "samgr_enable_extend_load_timeout" + "samgr_enable_extend_load_timeout", + "samgr_enable_delay_dbinder" ], "adapted_system_type": [ "standard" diff --git a/config.gni b/config.gni index 97b89d8e4ce6b3e9513e5e3bd15de56a15fbd350..de32f131a582bcd3220bde319a3c5021488dba7e 100644 --- a/config.gni +++ b/config.gni @@ -12,6 +12,9 @@ # limitations under the License. declare_args() { + # enable delay load dbinder service. + samgr_enable_delay_dbinder = true + # enable extend load sa timeout. samgr_enable_extend_load_timeout = false } diff --git a/services/samgr/native/BUILD.gn b/services/samgr/native/BUILD.gn index 5dc900c02d2742f1a01acf8c52fd57a91c127050..e9acf73f4ccf8dac4aad142e907246e7748184da 100644 --- a/services/samgr/native/BUILD.gn +++ b/services/samgr/native/BUILD.gn @@ -148,6 +148,10 @@ ohos_executable("samgr") { if (samgr_enable_extend_load_timeout) { defines += [ "SAMGR_ENABLE_EXTEND_LOAD_TIMEOUT" ] } + + if (samgr_enable_delay_dbinder) { + defines += [ "SAMGR_ENABLE_DELAY_DBINDER" ] + } part_name = "samgr" } diff --git a/services/samgr/native/include/system_ability_manager.h b/services/samgr/native/include/system_ability_manager.h index 715d708dfedd8e58ab35f1d73699a69bd63fb474..f4c672d4c3e0c5eb235f93643380f882a602399b 100644 --- a/services/samgr/native/include/system_ability_manager.h +++ b/services/samgr/native/include/system_ability_manager.h @@ -255,7 +255,9 @@ public: } } void RemoveOnDemandSaInDiedProc(std::shared_ptr& processContext); +#ifdef SAMGR_ENABLE_DELAY_DBINDER void InitDbinderService(); +#endif private: enum class AbilityState { INIT, @@ -271,7 +273,12 @@ private: OnDemandEvent event; }; - SystemAbilityManager() {} + SystemAbilityManager() + { +#ifndef SAMGR_ENABLE_DELAY_DBINDER + dBinderService_ = DBinderService::GetInstance(); +#endif + } std::string EventToJson(const OnDemandEvent& event); void DoInsertSaData(const std::u16string& name, const sptr& ability, const SAExtraProp& extraProp); int32_t StartOnDemandAbility(int32_t systemAbilityId, bool& isExist) @@ -384,9 +391,11 @@ private: sptr collectManager_; std::shared_ptr rpcCallbackImp_; +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_mutex dBinderServiceLock_; std::list distributedSaList_; bool isDbinderServiceInit_ = false; +#endif // must hold abilityMapLock_ never access other locks std::shared_mutex abilityMapLock_; diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 81145333916c3d3cad44fecc1c870fe38cefd1b1..cc4e6c15699008b77726a23a0b69907df7c1a4e7 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -193,7 +193,9 @@ void DeviceInitCallBack::OnRemoteDied() void DeviceStateCallback::OnDeviceOnline(const DmDeviceInfo& deviceInfo) { HILOGI("DeviceNetworkingCollect OnDeviceOnline size %{public}zu", deviceOnlineSet_.size()); +#ifdef SAMGR_ENABLE_DELAY_DBINDER SystemAbilityManager::GetInstance()->InitDbinderService(); +#endif { lock_guard autoLock(deviceOnlineLock_); deviceOnlineSet_.emplace(deviceInfo.networkId); diff --git a/services/samgr/native/source/system_ability_manager.cpp b/services/samgr/native/source/system_ability_manager.cpp index 113ab008ae59f20495a204e5ebbe619304dd4a2d..563ea99c6eafe429c523e41071ec7264aad3496a 100644 --- a/services/samgr/native/source/system_ability_manager.cpp +++ b/services/samgr/native/source/system_ability_manager.cpp @@ -90,6 +90,7 @@ sptr SystemAbilityManager::instance; void SystemAbilityManager::RegisterDistribute(int32_t systemAbilityId, bool isDistributed) { +#ifdef SAMGR_ENABLE_DELAY_DBINDER if (isDistributed) { std::shared_lock readLock(dBinderServiceLock_); if (dBinderService_ != nullptr) { @@ -109,8 +110,22 @@ void SystemAbilityManager::RegisterDistribute(int32_t systemAbilityId, bool isDi HILOGI("start result is %{public}s", ret ? "succeed" : "fail"); } } +#else + u16string strName = Str8ToStr16(to_string(systemAbilityId)); + if (isDistributed && dBinderService_!= nullptr) { + dBinderService_->RegisterRemoteProxy(strName, systemAbilityId); + HILOGI("AddSystemAbility RegisterRemoteProxy, SA:%{public}d", systemAbilityId); + } + if (systemAbilityId == SOFTBUS_SERVER_SA_ID) { + if (dBinderService_!= nullptr && rpcCallbackImp_!= nullptr) { + bool ret = dBinderService_->StartDBinderService(rpcCallbackImp_); + HILOGI("start result is %{public}s", ret? "succeed" : "fail"); + } + } +#endif } +#ifdef SAMGR_ENABLE_DELAY_DBINDER void SystemAbilityManager::InitDbinderService() { std::unique_lock writeLock(dBinderServiceLock_); @@ -134,6 +149,7 @@ void SystemAbilityManager::InitDbinderService() } } } +#endif void SystemAbilityManager::Init() { @@ -142,6 +158,9 @@ void SystemAbilityManager::Init() abilityStatusDeath_ = sptr(new AbilityStatusDeathRecipient()); abilityCallbackDeath_ = sptr(new AbilityCallbackDeathRecipient()); remoteCallbackDeath_ = sptr(new RemoteCallbackDeathRecipient()); +#ifndef SAMGR_ENABLE_DELAY_DBINDER + rpcCallbackImp_ = make_shared(); +#endif if (workHandler_ == nullptr) { workHandler_ = make_shared("workHandler"); @@ -990,7 +1009,9 @@ void SystemAbilityManager::NotifyRemoteSaDied(const std::u16string& name) std::u16string saName; std::string deviceId; SamgrUtil::ParseRemoteSaName(name, deviceId, saName); +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_lock readLock(dBinderServiceLock_); +#endif if (dBinderService_ != nullptr) { std::string nodeId = SamgrUtil::TransformDeviceId(deviceId, NODE_ID, false); dBinderService_->NoticeServiceDie(saName, nodeId); @@ -1001,7 +1022,9 @@ void SystemAbilityManager::NotifyRemoteSaDied(const std::u16string& name) void SystemAbilityManager::NotifyRemoteDeviceOffline(const std::string& deviceId) { +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_lock readLock(dBinderServiceLock_); +#endif if (dBinderService_ != nullptr) { dBinderService_->NoticeDeviceDie(deviceId); HILOGI("NotifyRemoteDeviceOffline, deviceId:%{public}s", AnonymizeDeviceId(deviceId).c_str()); @@ -1834,7 +1857,9 @@ sptr SystemAbilityManager::DoMakeRemoteBinder(int32_t system DeviceIdToNetworkId(networkId); #endif sptr remoteBinder = nullptr; +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_lock readLock(dBinderServiceLock_); +#endif if (dBinderService_ != nullptr) { string strName = to_string(systemAbilityId); { @@ -1856,7 +1881,9 @@ void SystemAbilityManager::NotifyRpcLoadCompleted(const std::string& srcDeviceId return; } auto notifyTask = [srcDeviceId, systemAbilityId, remoteObject, this]() { +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_lock readLock(dBinderServiceLock_); +#endif if (dBinderService_ != nullptr) { SamgrXCollie samgrXCollie("samgr--LoadSystemAbilityComplete_" + ToString(systemAbilityId)); dBinderService_->LoadSystemAbilityComplete(srcDeviceId, systemAbilityId, remoteObject); diff --git a/services/samgr/native/test/unittest/BUILD.gn b/services/samgr/native/test/unittest/BUILD.gn index a76daa2d09f4579d98f96989d3fb3abdc9f17995..15e8a301a7c53728be4c3c64a25e5d655fc12d8e 100644 --- a/services/samgr/native/test/unittest/BUILD.gn +++ b/services/samgr/native/test/unittest/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/test.gni") +import("../../../../../config.gni") import("../../../var.gni") module_output_path = "samgr/samgr" @@ -133,6 +134,10 @@ ohos_unittest("SystemAbilityMgrTest") { defines += [ "HICOLLIE_ENABLE" ] } + if (samgr_enable_delay_dbinder) { + defines += [ "SAMGR_ENABLE_DELAY_DBINDER" ] + } + if (support_common_event) { sources += [ "${samgr_services_dir}/source/collect/common_event_collect.cpp", diff --git a/services/samgr/native/test/unittest/src/system_ability_mgr_new_test.cpp b/services/samgr/native/test/unittest/src/system_ability_mgr_new_test.cpp index 7153d541c87541540f8f452c236508889c986c79..9fcf2cbadf2c8fc74cdf7a810d075ec19f347ef2 100644 --- a/services/samgr/native/test/unittest/src/system_ability_mgr_new_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_mgr_new_test.cpp @@ -332,7 +332,9 @@ HWTEST_F(SystemAbilityMgrNewTest, NotifyRpcLoadCompleted001, TestSize.Level3) { sptr saMgr = new SystemAbilityManager; InitSaMgr(saMgr); +#ifdef SAMGR_ENABLE_DELAY_DBINDER saMgr->InitDbinderService(); +#endif sptr testAbility = new TestTransactionService(); saMgr->workHandler_ = nullptr; saMgr->NotifyRpcLoadCompleted("", 1, testAbility); @@ -363,7 +365,9 @@ HWTEST_F(SystemAbilityMgrNewTest, NotifyRpcLoadCompleted003, TestSize.Level3) { sptr saMgr = new SystemAbilityManager; InitSaMgr(saMgr); +#ifdef SAMGR_ENABLE_DELAY_DBINDER saMgr->InitDbinderService(); +#endif sptr testAbility = new TestTransactionService(); saMgr->NotifyRpcLoadCompleted("", 1, testAbility); } @@ -1099,6 +1103,7 @@ HWTEST_F(SystemAbilityMgrNewTest, IsDistributedSystemAbility001, TestSize.Level2 EXPECT_FALSE(res); } +#ifdef SAMGR_ENABLE_DELAY_DBINDER HWTEST_F(SystemAbilityMgrNewTest, RegisterDistribute001, TestSize.Level2) { DTEST_LOG<<"RegisterDistribute001 BEGIN"<dBinderService_ = nullptr); DTEST_LOG<<"RegisterDistribute001 END"<