diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_state_callback_host.h b/interfaces/inner_api/app_manager/include/appmgr/app_state_callback_host.h index 64d8e70eae..b80157a978 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_state_callback_host.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_state_callback_host.h @@ -62,12 +62,19 @@ public: * @param bundleInfos resident process bundle infos. */ virtual void NotifyStartResidentProcess(std::vector &bundleInfos) override; + + /** + * @brief Notify abilityms app process OnRemoteDied + * @param abilityTokens abilities in died process. + */ + virtual void OnAppRemoteDied(const std::vector> &abilityTokens) override; private: int32_t HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply); int32_t HandleOnAbilityRequestDone(MessageParcel &data, MessageParcel &reply); int32_t HandleNotifyConfigurationChange(MessageParcel &data, MessageParcel &reply); int32_t HandleNotifyStartResidentProcess(MessageParcel &data, MessageParcel &reply); + int32_t HandleOnAppRemoteDied(MessageParcel &data, MessageParcel &reply); using AppStateCallbackFunc = int32_t (AppStateCallbackHost::*)(MessageParcel &data, MessageParcel &reply); std::map memberFuncMap_; diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_state_callback_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/app_state_callback_proxy.h index b464a65a54..1995ea5668 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_state_callback_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_state_callback_proxy.h @@ -57,6 +57,12 @@ public: */ virtual void NotifyStartResidentProcess(std::vector &bundleInfos) override; + /** + * @brief Notify abilityms app process OnRemoteDied + * @param abilityTokens abilities in died process. + */ + virtual void OnAppRemoteDied(const std::vector> &abilityTokens) override; + private: bool WriteInterfaceToken(MessageParcel &data); static inline BrokerDelegator delegator_; diff --git a/interfaces/inner_api/app_manager/include/appmgr/iapp_state_callback.h b/interfaces/inner_api/app_manager/include/appmgr/iapp_state_callback.h index 938104b90f..767fd95b98 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/iapp_state_callback.h +++ b/interfaces/inner_api/app_manager/include/appmgr/iapp_state_callback.h @@ -63,11 +63,18 @@ public: */ virtual void NotifyStartResidentProcess(std::vector &bundleInfos) {} + /** + * @brief Notify abilityms app process OnRemoteDied + * @param abilityTokens abilities in died process. + */ + virtual void OnAppRemoteDied(const std::vector> &abilityTokens) {} + enum class Message { TRANSACT_ON_APP_STATE_CHANGED = 0, TRANSACT_ON_ABILITY_REQUEST_DONE, TRANSACT_ON_NOTIFY_CONFIG_CHANGE, - TRANSACT_ON_NOTIFY_START_RESIDENT_PROCESS + TRANSACT_ON_NOTIFY_START_RESIDENT_PROCESS, + TRANSACT_ON_APP_REMOTE_DIED }; }; } // namespace AppExecFwk diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_state_callback_host.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_state_callback_host.cpp index 42779c87e0..e71dbf28c5 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_state_callback_host.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_state_callback_host.cpp @@ -38,6 +38,8 @@ AppStateCallbackHost::AppStateCallbackHost() &AppStateCallbackHost::HandleNotifyConfigurationChange; memberFuncMap_[static_cast(IAppStateCallback::Message::TRANSACT_ON_NOTIFY_START_RESIDENT_PROCESS)] = &AppStateCallbackHost::HandleNotifyStartResidentProcess; + memberFuncMap_[static_cast(IAppStateCallback::Message::TRANSACT_ON_APP_REMOTE_DIED)] = + &AppStateCallbackHost::HandleOnAppRemoteDied; } AppStateCallbackHost::~AppStateCallbackHost() @@ -87,6 +89,11 @@ void AppStateCallbackHost::NotifyStartResidentProcess(std::vector> &abilityTokens) +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); +} + int32_t AppStateCallbackHost::HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply) { HITRACE_METER(HITRACE_TAG_APP); @@ -143,5 +150,25 @@ int32_t AppStateCallbackHost::HandleNotifyStartResidentProcess(MessageParcel &da NotifyStartResidentProcess(bundleInfos); return NO_ERROR; } + +int32_t AppStateCallbackHost::HandleOnAppRemoteDied(MessageParcel &data, MessageParcel &reply) +{ + std::vector> abilityTokens; + int32_t infoSize = data.ReadInt32(); + if (infoSize > CYCLE_LIMIT) { + TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large"); + return ERR_INVALID_VALUE; + } + for (int32_t i = 0; i < infoSize; i++) { + sptr obj = data.ReadRemoteObject(); + if (!obj) { + TAG_LOGE(AAFwkTag::APPMGR, "Read token failed."); + return ERR_INVALID_VALUE; + } + abilityTokens.emplace_back(obj); + } + OnAppRemoteDied(abilityTokens); + return NO_ERROR; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_state_callback_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_state_callback_proxy.cpp index 25f37a9e6d..1636c1c45e 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_state_callback_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_state_callback_proxy.cpp @@ -138,6 +138,35 @@ void AppStateCallbackProxy::NotifyStartResidentProcess(std::vector> &abilityTokens) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!WriteInterfaceToken(data)) { + TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed"); + return; + } + + if (!data.WriteInt32(abilityTokens.size())) { + TAG_LOGE(AAFwkTag::APPMGR, "write token size failed."); + return; + } + + for (auto &token : abilityTokens) { + if (!data.WriteRemoteObject(token.GetRefPtr())) { + TAG_LOGE(AAFwkTag::APPMGR, "write token failed"); + return; + } + } + auto ret = SendTransactCmd( + static_cast(IAppStateCallback::Message::TRANSACT_ON_APP_REMOTE_DIED), + data, reply, option); + if (ret != NO_ERROR) { + TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret); + } +} + int32_t AppStateCallbackProxy::SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { diff --git a/services/abilitymgr/abilitymgr.gni b/services/abilitymgr/abilitymgr.gni index 15bae44f83..9607736fe5 100644 --- a/services/abilitymgr/abilitymgr.gni +++ b/services/abilitymgr/abilitymgr.gni @@ -14,7 +14,6 @@ import("//foundation/ability/ability_runtime/ability_runtime.gni") abilityms_files = [ - "src/ability_app_state_observer.cpp", "src/preload_uiext_state_observer.cpp", "src/ability_background_connection.cpp", "src/ability_connect_manager.cpp", diff --git a/services/abilitymgr/include/ability_app_state_observer.h b/services/abilitymgr/include/ability_app_state_observer.h deleted file mode 100644 index 0b8d83e142..0000000000 --- a/services/abilitymgr/include/ability_app_state_observer.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef OHOS_ABILITY_RUNTIME_ABILITY_APP_STATE_OBSERVER_H -#define OHOS_ABILITY_RUNTIME_ABILITY_APP_STATE_OBSERVER_H - -#include - -#include "application_state_observer_stub.h" - -namespace OHOS { -namespace AAFwk { -class AbilityRecord; -class AbilityAppStateObserver : public AppExecFwk::ApplicationStateObserverStub { -public: - AbilityAppStateObserver(std::shared_ptr abilityRecord); - virtual void OnProcessDied(const AppExecFwk::ProcessData &processData) override; - -private: - std::weak_ptr abilityRecord_; -}; -} // namespace AAFwk -} // namespace OHOS -#endif // OHOS_ABILITY_RUNTIME_ABILITY_APP_STATE_OBSERVER_H \ No newline at end of file diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index 9167d95ccb..f40d4aab1c 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -1751,6 +1751,8 @@ protected: void NotifyStartResidentProcess(std::vector &bundleInfos) override; + void OnAppRemoteDied(const std::vector> &abilityTokens) override; + private: int TerminateAbilityWithFlag(const sptr &token, int resultCode = DEFAULT_INVAL_VALUE, const Want *resultWant = nullptr, bool flag = true); diff --git a/services/abilitymgr/include/ability_record.h b/services/abilitymgr/include/ability_record.h index af71fc858a..be3bf4e39a 100644 --- a/services/abilitymgr/include/ability_record.h +++ b/services/abilitymgr/include/ability_record.h @@ -57,7 +57,6 @@ class ConnectionRecord; class Mission; class MissionList; class CallContainer; -class AbilityAppStateObserver; constexpr const char* ABILITY_TOKEN_NAME = "AbilityToken"; constexpr const char* LAUNCHER_BUNDLE_NAME = "com.ohos.launcher"; @@ -1001,7 +1000,6 @@ private: */ void GetAbilityTypeString(std::string &typeStr); void OnSchedulerDied(const wptr &remote); - void RemoveAppStateObserver(bool force = false); void GrantUriPermission(Want &want, std::string targetBundleName, bool isSandboxApp, uint32_t tokenId); void GrantDmsUriPermission(Want &want, std::string targetBundleName); bool IsDmsCall(Want &want); @@ -1177,7 +1175,6 @@ private: // scene session sptr sessionInfo_ = nullptr; mutable ffrt::mutex sessionLock_; - sptr abilityAppStateObserver_; std::map abilityWindowStateMap_; sptr uiExtRequestSessionInfo_ = nullptr; diff --git a/services/abilitymgr/include/app_scheduler.h b/services/abilitymgr/include/app_scheduler.h index 760e9c1295..db716ce3a9 100644 --- a/services/abilitymgr/include/app_scheduler.h +++ b/services/abilitymgr/include/app_scheduler.h @@ -93,6 +93,8 @@ public: virtual void NotifyConfigurationChange(const AppExecFwk::Configuration &config, int32_t userId) {} virtual void NotifyStartResidentProcess(std::vector &bundleInfos) {} + + virtual void OnAppRemoteDied(const std::vector> &abilityTokens) {} }; class StartSpecifiedAbilityResponse : public AppExecFwk::StartSpecifiedAbilityResponseStub { @@ -461,6 +463,8 @@ protected: */ virtual void NotifyStartResidentProcess(std::vector &bundleInfos) override; + virtual void OnAppRemoteDied(const std::vector> &abilityTokens) override; + private: std::mutex lock_; bool isInit_ {false}; diff --git a/services/abilitymgr/src/ability_app_state_observer.cpp b/services/abilitymgr/src/ability_app_state_observer.cpp deleted file mode 100644 index 866af1664c..0000000000 --- a/services/abilitymgr/src/ability_app_state_observer.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "ability_app_state_observer.h" -#include "ability_record.h" -#include "hilog_tag_wrapper.h" -#include "hilog_wrapper.h" - -namespace OHOS { -namespace AAFwk { -AbilityAppStateObserver::AbilityAppStateObserver(std::shared_ptr abilityRecord) - : abilityRecord_(abilityRecord) {} -void AbilityAppStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData) -{ - auto abilityRecord = abilityRecord_.lock(); - if (abilityRecord) { - const auto &abilityInfo = abilityRecord->GetAbilityInfo(); - if (abilityInfo.bundleName == processData.bundleName && - processData.processType == AppExecFwk::ProcessType::NORMAL && - abilityInfo.type == AppExecFwk::AbilityType::PAGE) { - abilityRecord->OnProcessDied(); - } - if (abilityRecord->IsSceneBoard() && abilityRecord->GetPid() == processData.pid) { - abilityRecord->OnProcessDied(); - } - } else { - TAG_LOGW(AAFwkTag::ABILITYMGR, "AbilityRecord null"); - } -} -} // namespace AAFwk -} // namespace OHOS \ No newline at end of file diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 61590f5f8b..43e199e200 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -9869,6 +9869,20 @@ void AbilityManagerService::NotifyStartResidentProcess(std::vector> &abilityTokens) +{ + std::shared_ptr abilityRecord; + for (auto &token : abilityTokens) { + abilityRecord = Token::GetAbilityRecordByToken(token); + if (abilityRecord == nullptr) { + continue; + } + TAG_LOGI(AAFwkTag::ABILITYMGR, "App OnRemoteDied, ability is %{public}s, app is %{public}s", + abilityRecord->GetAbilityInfo().name.c_str(), abilityRecord->GetAbilityInfo().bundleName.c_str()); + abilityRecord->OnProcessDied(); + } +} + int32_t AbilityManagerService::OpenFile(const Uri& uri, uint32_t flag) { auto accessTokenId = IPCSkeleton::GetCallingTokenID(); diff --git a/services/abilitymgr/src/ability_record.cpp b/services/abilitymgr/src/ability_record.cpp index 334f75b873..820462fe99 100644 --- a/services/abilitymgr/src/ability_record.cpp +++ b/services/abilitymgr/src/ability_record.cpp @@ -21,7 +21,6 @@ #include #include "constants.h" -#include "ability_app_state_observer.h" #include "ability_event_handler.h" #include "ability_manager_service.h" #include "ability_resident_process_rdb.h" @@ -246,7 +245,6 @@ AbilityRecord::~AbilityRecord() } } want_.CloseAllFd(); - RemoveAppStateObserver(true); } std::shared_ptr AbilityRecord::CreateAbilityRecord(const AbilityRequest &abilityRequest) @@ -273,30 +271,9 @@ std::shared_ptr AbilityRecord::CreateAbilityRecord(const AbilityR abilityRecord->collaboratorType_ = abilityRequest.collaboratorType; abilityRecord->missionAffinity_ = abilityRequest.want.GetStringParam(PARAM_MISSION_AFFINITY_KEY); - // Before the ability attaches - abilityRecord->abilityAppStateObserver_ = sptr( - new AbilityAppStateObserver(abilityRecord)); - DelayedSingleton::GetInstance()->RegisterApplicationStateObserver( - abilityRecord->abilityAppStateObserver_, {abilityRequest.abilityInfo.bundleName}); return abilityRecord; } -void AbilityRecord::RemoveAppStateObserver(bool force) -{ - if (!force && IsSceneBoard()) { - TAG_LOGI(AAFwkTag::ABILITYMGR, "Special ability no need to RemoveAppStateObserver."); - return; - } - auto handler = DelayedSingleton::GetInstance()->GetTaskHandler(); - if (handler && abilityAppStateObserver_) { - handler->SubmitTask([appStateObserver = abilityAppStateObserver_]() { - DelayedSingleton::GetInstance()->UnregisterApplicationStateObserver( - appStateObserver); - }); - abilityAppStateObserver_ = nullptr; - } -} - bool AbilityRecord::Init() { lifecycleDeal_ = std::make_unique(); @@ -1424,7 +1401,6 @@ void AbilityRecord::SetScheduler(const sptr &scheduler) } }); } - RemoveAppStateObserver(); isReady_ = true; scheduler_ = scheduler; lifecycleDeal_->SetScheduler(scheduler); @@ -2390,7 +2366,10 @@ void AbilityRecord::OnSchedulerDied(const wptr &remote) void AbilityRecord::OnProcessDied() { std::lock_guard guard(lock_); - RemoveAppStateObserver(true); + if (!IsSceneBoard() && scheduler_ != nullptr) { + TAG_LOGD(AAFwkTag::ABILITYMGR, "OnProcessDied: '%{public}s', attached.", abilityInfo_.name.c_str()); + return; + } isWindowAttached_ = false; auto handler = DelayedSingleton::GetInstance()->GetTaskHandler(); diff --git a/services/abilitymgr/src/app_scheduler.cpp b/services/abilitymgr/src/app_scheduler.cpp index d7b3fb298a..a5de63c336 100644 --- a/services/abilitymgr/src/app_scheduler.cpp +++ b/services/abilitymgr/src/app_scheduler.cpp @@ -239,6 +239,13 @@ void AppScheduler::NotifyStartResidentProcess(std::vectorNotifyStartResidentProcess(bundleInfos); } +void AppScheduler::OnAppRemoteDied(const std::vector> &abilityTokens) +{ + auto callback = callback_.lock(); + CHECK_POINTER(callback); + callback->OnAppRemoteDied(abilityTokens); +} + int AppScheduler::KillApplication(const std::string &bundleName) { TAG_LOGI(AAFwkTag::ABILITYMGR, "[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__); diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 8ece2189a0..37da9e6ccc 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -3024,6 +3024,15 @@ void AppMgrServiceInner::OnRemoteDied(const wptr &remote, bool is return; } + std::vector> abilityTokens; + for (const auto &token : appRecord->GetAbilities()) { + abilityTokens.emplace_back(token.first); + } + for (const auto &callback : appStateCallbacks_) { + if (callback != nullptr) { + callback->OnAppRemoteDied(abilityTokens); + } + } ClearData(appRecord); }