From 726c241cb229732bca48d7d024e805bcd1f5baa9 Mon Sep 17 00:00:00 2001 From: chen Date: Fri, 7 Jan 2022 14:54:23 +0800 Subject: [PATCH] add loadsystemability load fail Signed-off-by: chen --- .../system_ability_load_callback_stub.cpp | 13 ++ .../include/isystem_ability_load_callback.h | 2 + .../system_ability_load_callback_stub.h | 1 + .../system_ability_load_callback_proxy.h | 1 + .../native/include/system_ability_manager.h | 4 + .../system_ability_load_callback_proxy.cpp | 31 +++++ .../native/source/system_ability_manager.cpp | 113 ++++++++++++++---- .../test/unittest/include/ondemand_helper.h | 1 + .../unittest/include/sa_status_change_mock.h | 2 + .../test/unittest/src/ondemand_helper.cpp | 5 + .../unittest/src/sa_status_change_mock.cpp | 6 + 11 files changed, 157 insertions(+), 22 deletions(-) mode change 100644 => 100755 frameworks/native/source/system_ability_load_callback_stub.cpp mode change 100644 => 100755 interfaces/innerkits/samgr_proxy/include/isystem_ability_load_callback.h mode change 100644 => 100755 interfaces/innerkits/samgr_proxy/include/system_ability_load_callback_stub.h mode change 100644 => 100755 services/samgr/native/include/system_ability_load_callback_proxy.h mode change 100644 => 100755 services/samgr/native/source/system_ability_load_callback_proxy.cpp mode change 100644 => 100755 services/samgr/native/test/unittest/include/ondemand_helper.h mode change 100644 => 100755 services/samgr/native/test/unittest/src/ondemand_helper.cpp diff --git a/frameworks/native/source/system_ability_load_callback_stub.cpp b/frameworks/native/source/system_ability_load_callback_stub.cpp old mode 100644 new mode 100755 index 407e6404..05416258 --- a/frameworks/native/source/system_ability_load_callback_stub.cpp +++ b/frameworks/native/source/system_ability_load_callback_stub.cpp @@ -31,6 +31,8 @@ int32_t SystemAbilityLoadCallbackStub::OnRemoteRequest(uint32_t code, switch (code) { case ON_LOAD_SYSTEM_ABILITY_SUCCESS: return OnLoadSystemAbilitySuccessInner(data, reply); + case ON_LOAD_SYSTEM_ABILITY_FAIL: + return OnLoadSystemAbilityFailInner(data, reply); default: HILOGW("SystemAbilityLoadCallbackStub::OnRemoteRequest unknown request code!"); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -49,6 +51,17 @@ int32_t SystemAbilityLoadCallbackStub::OnLoadSystemAbilitySuccessInner(MessagePa return ERR_NONE; } +int32_t SystemAbilityLoadCallbackStub::OnLoadSystemAbilityFailInner(MessageParcel& data, MessageParcel& reply) +{ + int32_t systemAbilityId = data.ReadInt32(); + if (!CheckInputSystemAbilityId(systemAbilityId)) { + HILOGW("OnLoadSystemAbilityFailInner invalid systemAbilityId:%{public}d !", systemAbilityId); + return ERR_INVALID_VALUE; + } + OnLoadSystemAbilityFail(systemAbilityId); + return ERR_NONE; +} + bool SystemAbilityLoadCallbackStub::CheckInputSystemAbilityId(int32_t systemAbilityId) { return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID); diff --git a/interfaces/innerkits/samgr_proxy/include/isystem_ability_load_callback.h b/interfaces/innerkits/samgr_proxy/include/isystem_ability_load_callback.h old mode 100644 new mode 100755 index ff2e1891..01d55cac --- a/interfaces/innerkits/samgr_proxy/include/isystem_ability_load_callback.h +++ b/interfaces/innerkits/samgr_proxy/include/isystem_ability_load_callback.h @@ -25,10 +25,12 @@ class ISystemAbilityLoadCallback : public IRemoteBroker { public: virtual ~ISystemAbilityLoadCallback() = default; virtual void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) = 0; + virtual void OnLoadSystemAbilityFail(int32_t systemAbilityId) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.ISystemAbilityLoadCallback"); protected: enum { ON_LOAD_SYSTEM_ABILITY_SUCCESS = 1, + ON_LOAD_SYSTEM_ABILITY_FAIL = 2, }; }; } diff --git a/interfaces/innerkits/samgr_proxy/include/system_ability_load_callback_stub.h b/interfaces/innerkits/samgr_proxy/include/system_ability_load_callback_stub.h old mode 100644 new mode 100755 index 3f66df0c..9bf201f1 --- a/interfaces/innerkits/samgr_proxy/include/system_ability_load_callback_stub.h +++ b/interfaces/innerkits/samgr_proxy/include/system_ability_load_callback_stub.h @@ -29,6 +29,7 @@ private: static bool EnforceInterceToken(MessageParcel& data); int32_t OnLoadSystemAbilitySuccessInner(MessageParcel& data, MessageParcel& reply); + int32_t OnLoadSystemAbilityFailInner(MessageParcel& data, MessageParcel& reply); }; } #endif /* SAMGR_INTERFACES_INNERKITS_SAMGR_PROXY_INCLUDE_SYSTEM_ABILITY_LOAD_CALLBACK_STUB_H */ diff --git a/services/samgr/native/include/system_ability_load_callback_proxy.h b/services/samgr/native/include/system_ability_load_callback_proxy.h old mode 100644 new mode 100755 index 4cf905f5..2f52be48 --- a/services/samgr/native/include/system_ability_load_callback_proxy.h +++ b/services/samgr/native/include/system_ability_load_callback_proxy.h @@ -26,6 +26,7 @@ public: ~SystemAbilityLoadCallbackProxy() = default; void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override; + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; private: static inline BrokerDelegator delegator_; }; diff --git a/services/samgr/native/include/system_ability_manager.h b/services/samgr/native/include/system_ability_manager.h index 09316018..d87d2ca7 100755 --- a/services/samgr/native/include/system_ability_manager.h +++ b/services/samgr/native/include/system_ability_manager.h @@ -129,12 +129,16 @@ private: void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr& remoteObject); void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr& remoteObject, const sptr& callback); + void NotifySystemAbilityLoadFail(int32_t systemAbilityId, const sptr& callback); int32_t StartingSystemProcess(const std::u16string& name, int32_t systemAbilityId); void StartOnDemandAbility(const std::u16string& name, int32_t systemAbilityId); void StartOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, AbilityItem& abilityItem); int32_t StartDynamicSystemProcess(const std::u16string& name, int32_t systemAbilityId); void RemoveStartingAbilityCallback(AbilityItem& abilityItem, const sptr& remoteObject); void RemoveStartingAbilityCallbackLocked(std::pair, int32_t>& itemPair); + void SendCheckLoadedMsg(int32_t systemAbilityId, const std::u16string& name, + const sptr& callback); + void RemoveCheckLoadedMsg(int32_t systemAbilityId); std::u16string deviceName_; static sptr instance; diff --git a/services/samgr/native/source/system_ability_load_callback_proxy.cpp b/services/samgr/native/source/system_ability_load_callback_proxy.cpp old mode 100644 new mode 100755 index d3e9a672..193431c1 --- a/services/samgr/native/source/system_ability_load_callback_proxy.cpp +++ b/services/samgr/native/source/system_ability_load_callback_proxy.cpp @@ -61,4 +61,35 @@ void SystemAbilityLoadCallbackProxy::OnLoadSystemAbilitySuccess(int32_t systemAb return; } } + +void SystemAbilityLoadCallbackProxy::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + if (systemAbilityId <= 0) { + HILOGE("OnLoadSystemAbilityFail systemAbilityId:%{public}d invalid!", systemAbilityId); + return; + } + + sptr remote = Remote(); + if (remote == nullptr) { + HILOGE("OnLoadSystemAbilityFail Remote() return null!"); + return; + } + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("OnLoadSystemAbilityFail write interface token failed!"); + return; + } + bool ret = data.WriteInt32(systemAbilityId); + if (!ret) { + HILOGE("OnLoadSystemAbilityFail write systemAbilityId failed!"); + return; + } + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + int32_t status = remote->SendRequest(ON_LOAD_SYSTEM_ABILITY_FAIL, data, reply, option); + if (status != NO_ERROR) { + HILOGE("OnLoadSystemAbilityFail SendRequest failed, return value:%{public}d !", status); + return; + } +} } diff --git a/services/samgr/native/source/system_ability_manager.cpp b/services/samgr/native/source/system_ability_manager.cpp index 540e9e3f..fb900c9c 100755 --- a/services/samgr/native/source/system_ability_manager.cpp +++ b/services/samgr/native/source/system_ability_manager.cpp @@ -43,6 +43,7 @@ constexpr int32_t SPLIT_NAME_VECTOR_SIZE = 2; constexpr int32_t UID_ROOT = 0; constexpr int32_t UID_SYSTEM = 1000; constexpr int32_t MAX_SUBSCRIBE_COUNT = 256; +constexpr int64_t CHECK_LOADED_DELAY_TIME = 60 * 1000; // ms } std::mutex SystemAbilityManager::instanceLock; @@ -584,6 +585,7 @@ int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sp abilityMap_[systemAbilityId] = std::move(saInfo); HILOGI("insert %{public}d. size : %{public}zu", systemAbilityId, abilityMap_.size()); } + RemoveCheckLoadedMsg(systemAbilityId); if (abilityDeath_ != nullptr) { ability->AddDeathRecipient(abilityDeath_); } @@ -707,6 +709,60 @@ void SystemAbilityManager::SendSystemAbilityRemovedMsg(int32_t systemAbilityId) } } +void SystemAbilityManager::SendCheckLoadedMsg(int32_t systemAbilityId, const std::u16string& name, + const sptr& callback) +{ + if (workHandler_ == nullptr) { + HILOGE("SendCheckLoadedMsg work handler not initialized!"); + return; + } + + auto delayTask = [systemAbilityId, name, callback, this]() { + HILOGI("SendCheckLoadedMsg handle for SA : %{public}d.", systemAbilityId); + if (CheckSystemAbility(systemAbilityId) != nullptr) { + HILOGI("SendCheckLoadedMsg SA : %{public}d loaded.", systemAbilityId); + return; + } + { + lock_guard autoLock(onDemandLock_); + auto iter = startingAbilityMap_.find(systemAbilityId); + if (iter == startingAbilityMap_.end()) { + HILOGI("SendCheckLoadedMsg SA : %{public}d not in startingAbilityMap.", systemAbilityId); + return; + } + auto& abilityItem = iter->second; + for (auto& callbackItem : abilityItem.callbackList) { + if (callback->AsObject() == callbackItem.first->AsObject()) { + NotifySystemAbilityLoadFail(systemAbilityId, callbackItem.first); + RemoveStartingAbilityCallbackLocked(callbackItem); + abilityItem.callbackList.remove(callbackItem); + break; + } + } + if (abilityItem.callbackList.empty()) { + HILOGD("SendCheckLoadedMsg startingAbilityMap remove SA : %{public}d.", systemAbilityId); + startingAbilityMap_.erase(iter); + } + } + (void)GetSystemProcess(name); + }; + HILOGI("SendCheckLoadedMsg PostTask name : %{public}d!", systemAbilityId); + bool ret = workHandler_->PostTask(delayTask, ToString(systemAbilityId), CHECK_LOADED_DELAY_TIME); + if (!ret) { + HILOGW("SendCheckLoadedMsg PostTask failed!"); + } +} + +void SystemAbilityManager::RemoveCheckLoadedMsg(int32_t systemAbilityId) +{ + if (workHandler_ == nullptr) { + HILOGE("RemoveCheckLoadedMsg work handler not initialized!"); + return; + } + HILOGI("RemoveCheckLoadedMsg sa : %{public}d!", systemAbilityId); + workHandler_->RemoveTask(ToString(systemAbilityId)); +} + void SystemAbilityManager::NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr& remoteObject, const sptr& callback) { @@ -732,6 +788,16 @@ void SystemAbilityManager::NotifySystemAbilityLoaded(int32_t systemAbilityId, co startingAbilityMap_.erase(iter); } +void SystemAbilityManager::NotifySystemAbilityLoadFail(int32_t systemAbilityId, + const sptr& callback) +{ + if (callback == nullptr) { + HILOGE("NotifySystemAbilityLoadFail callback null!"); + return; + } + callback->OnLoadSystemAbilityFail(systemAbilityId); +} + int32_t SystemAbilityManager::StartDynamicSystemProcess(const std::u16string& name, int32_t systemAbilityId) { std::string strExtra = std::to_string(systemAbilityId); @@ -784,31 +850,34 @@ int32_t SystemAbilityManager::LoadSystemAbility(int32_t systemAbilityId, NotifySystemAbilityLoaded(systemAbilityId, targetObject, callback); return ERR_OK; } - + int32_t result = ERR_INVALID_VALUE; auto callingPid = IPCSkeleton::GetCallingPid(); - lock_guard autoLock(onDemandLock_); - auto& abilityItem = startingAbilityMap_[systemAbilityId]; - for (const auto& itemCallback : abilityItem.callbackList) { - if (callback->AsObject() == itemCallback.first->AsObject()) { - HILOGI("LoadSystemAbility already existed callback object systemAbilityId:%{public}d", systemAbilityId); - return ERR_OK; + { + lock_guard autoLock(onDemandLock_); + auto& abilityItem = startingAbilityMap_[systemAbilityId]; + for (const auto& itemCallback : abilityItem.callbackList) { + if (callback->AsObject() == itemCallback.first->AsObject()) { + HILOGI("LoadSystemAbility already existed callback object systemAbilityId:%{public}d", systemAbilityId); + return ERR_OK; + } } + auto& count = callbackCountMap_[callingPid]; + if (count >= MAX_SUBSCRIBE_COUNT) { + HILOGE("LoadSystemAbility pid:%{public}d overflow max callback count!", callingPid); + return ERR_PERMISSION_DENIED; + } + ++count; + abilityItem.callbackList.emplace_back(callback, callingPid); + if (abilityCallbackDeath_ != nullptr) { + bool ret = callback->AsObject()->AddDeathRecipient(abilityCallbackDeath_); + HILOGI("LoadSystemAbility systemAbilityId:%{public}d AddDeathRecipient %{public}s", + systemAbilityId, ret ? "succeed" : "failed"); + } + result = StartingSystemProcess(saProfile.process, systemAbilityId); + HILOGI("LoadSystemAbility systemAbilityId:%{public}d size : %{public}zu", + systemAbilityId, abilityItem.callbackList.size()); } - auto& count = callbackCountMap_[callingPid]; - if (count >= MAX_SUBSCRIBE_COUNT) { - HILOGE("LoadSystemAbility pid:%{public}d overflow max callback count!", callingPid); - return ERR_PERMISSION_DENIED; - } - ++count; - abilityItem.callbackList.emplace_back(callback, callingPid); - if (abilityCallbackDeath_ != nullptr) { - bool ret = callback->AsObject()->AddDeathRecipient(abilityCallbackDeath_); - HILOGI("LoadSystemAbility systemAbilityId:%{public}d AddDeathRecipient %{public}s", - systemAbilityId, ret ? "succeed" : "failed"); - } - int32_t result = StartingSystemProcess(saProfile.process, systemAbilityId); - HILOGI("LoadSystemAbility systemAbilityId:%{public}d size : %{public}zu", - systemAbilityId, abilityItem.callbackList.size()); + SendCheckLoadedMsg(systemAbilityId, saProfile.process, callback); return result; } diff --git a/services/samgr/native/test/unittest/include/ondemand_helper.h b/services/samgr/native/test/unittest/include/ondemand_helper.h old mode 100644 new mode 100755 index 8de9f8cb..8514efeb --- a/services/samgr/native/test/unittest/include/ondemand_helper.h +++ b/services/samgr/native/test/unittest/include/ondemand_helper.h @@ -31,6 +31,7 @@ protected: class OnDemandLoadCallback : public SystemAbilityLoadCallbackStub { public: void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override; + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; }; private: OnDemandHelper(); diff --git a/services/samgr/native/test/unittest/include/sa_status_change_mock.h b/services/samgr/native/test/unittest/include/sa_status_change_mock.h index 0e818f48..f2908c49 100755 --- a/services/samgr/native/test/unittest/include/sa_status_change_mock.h +++ b/services/samgr/native/test/unittest/include/sa_status_change_mock.h @@ -30,6 +30,8 @@ class SystemAbilityLoadCallbackMock : public SystemAbilityLoadCallbackStub { public: void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override; + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; + int32_t GetSystemAbilityId() const { return currSystemAbilityId; diff --git a/services/samgr/native/test/unittest/src/ondemand_helper.cpp b/services/samgr/native/test/unittest/src/ondemand_helper.cpp old mode 100644 new mode 100755 index 1f61c8e5..f79dbdc7 --- a/services/samgr/native/test/unittest/src/ondemand_helper.cpp +++ b/services/samgr/native/test/unittest/src/ondemand_helper.cpp @@ -77,6 +77,11 @@ void OnDemandHelper::OnDemandLoadCallback::OnLoadSystemAbilitySuccess(int32_t sy ((remoteObject != nullptr) ? "succeed" : "failed") << endl; OnDemandHelper::GetInstance().OnLoadSystemAbility(systemAbilityId); } + +void OnDemandHelper::OnDemandLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + cout << "OnLoadSystemAbilityFail systemAbilityId:" << systemAbilityId << endl; +} } int main(int argc, char* argv[]) diff --git a/services/samgr/native/test/unittest/src/sa_status_change_mock.cpp b/services/samgr/native/test/unittest/src/sa_status_change_mock.cpp index 2298938f..214af099 100755 --- a/services/samgr/native/test/unittest/src/sa_status_change_mock.cpp +++ b/services/samgr/native/test/unittest/src/sa_status_change_mock.cpp @@ -35,4 +35,10 @@ void SystemAbilityLoadCallbackMock::OnLoadSystemAbilitySuccess(int32_t systemAbi currSystemAbilityId = systemAbilityId; currRemoteObject = remoteObject; } + +void SystemAbilityLoadCallbackMock::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + HILOGI("OnLoadSystemAbilityFail systemAbilityId:%{public}d load fail!", systemAbilityId); + currSystemAbilityId = systemAbilityId; +} } -- Gitee