From c7b200fd90a8fd7d1a1083de7f1002a8296cb93d Mon Sep 17 00:00:00 2001 From: yuwenze Date: Tue, 29 Oct 2024 10:52:54 +0800 Subject: [PATCH] restartApp supports multi-instance Signed-off-by: yuwenze Change-Id: I0c86a42d316a68fd7a57b4c511c8c856b0a8867f --- .../include/appmgr/app_mgr_interface.h | 12 +-- .../appmgr/app_mgr_ipc_interface_code.h | 2 +- .../include/appmgr/app_mgr_proxy.h | 6 +- .../app_manager/include/appmgr/app_mgr_stub.h | 2 +- .../include/appmgr/running_process_info.h | 2 + .../app_manager/src/appmgr/app_mgr_proxy.cpp | 24 +++--- .../app_manager/src/appmgr/app_mgr_stub.cpp | 24 +++--- .../src/appmgr/running_process_info.cpp | 6 ++ .../include/ability_cache_manager.h | 3 +- .../include/ability_connect_manager.h | 2 +- .../include/ability_manager_service.h | 5 +- .../abilitymgr/include/mission/mission_list.h | 2 +- .../include/mission/mission_list_manager.h | 2 +- .../mission/mission_list_manager_interface.h | 2 +- .../abilitymgr/include/restart_app_manager.h | 30 +++++++- .../ui_ability_lifecycle_manager.h | 2 +- .../abilitymgr/include/utils/want_utils.h | 8 -- .../abilitymgr/src/ability_cache_manager.cpp | 5 +- .../src/ability_connect_manager.cpp | 7 +- .../src/ability_manager_service.cpp | 75 ++++++++++--------- .../abilitymgr/src/mission/mission_list.cpp | 6 +- .../src/mission/mission_list_manager.cpp | 8 +- .../abilitymgr/src/restart_app_manager.cpp | 23 ++---- .../ui_ability_lifecycle_manager.cpp | 5 +- services/abilitymgr/src/utils/want_utils.cpp | 18 ----- services/appmgr/include/app_mgr_service.h | 6 +- .../appmgr/include/app_mgr_service_inner.h | 6 +- services/appmgr/include/app_running_manager.h | 4 +- services/appmgr/src/app_mgr_service.cpp | 15 ++-- services/appmgr/src/app_mgr_service_inner.cpp | 55 +++++++++----- services/appmgr/src/app_running_manager.cpp | 51 +++++++++++-- .../abilityappmgrapprunningmanager_fuzzer.cpp | 2 +- .../abilitycachemanagera_fuzzer.cpp | 2 +- .../abilitymgrrestartappmanager_fuzzer.cpp | 8 +- .../uiabilitylifecyclemanagera_fuzzer.cpp | 2 +- .../ability_cache_manager_test.cpp | 2 +- .../ability_connect_manager_test.cpp | 2 +- .../ability_manager_service_third_test.cpp | 6 +- .../app_mgr_proxy_test/app_mgr_proxy_test.cpp | 24 +----- .../app_running_manager_second_test.cpp | 6 +- .../mission_list_manager_second_test.cpp | 6 +- .../mission_list_test/mission_list_test.cpp | 8 +- .../restart_app_manager_test.cpp | 21 ++---- 43 files changed, 265 insertions(+), 242 deletions(-) diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h index 9a416c3cea..42811570bc 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h @@ -730,10 +730,11 @@ public: /** * @brief mark a process which is going restart. * @param uid the uid of the process. + * @param instanceKey the instance key of the process. * * @return Returns ERR_OK on success, others on failure. */ - virtual int32_t SignRestartAppFlag(int32_t uid) + virtual int32_t SignRestartAppFlag(int32_t uid, const std::string &instanceKey) { return 0; } @@ -855,13 +856,8 @@ public: */ virtual int32_t GetSupportedProcessCachePids(const std::string &bundleName, std::vector &pidList) = 0; - /** - * Get appIndex of pid. - * @param pid The pid. - * @param appIndex appIndex of pid. - * @return Returns ERR_OK on success, others on failure. - */ - virtual int32_t GetAppIndexByPid(pid_t pid, int32_t &appIndex) + virtual int32_t KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack, + const std::string& reason) { return 0; } diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h index 19a1cd5da5..ec3ad9c7bd 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h @@ -117,7 +117,7 @@ enum class AppMgrInterfaceCode { CHECK_IS_KIA_PROCESS = 91, GET_All_RUNNING_INSTANCE_KEYS_BY_BUNDLENAME = 92, GET_All_RUNNING_INSTANCE_KEYS_BY_SELF = 93, - GET_APP_INDEX_BY_PID = 94, + KILL_APP_SELF_WITH_INSTANCE_KEY = 94, }; } // AppExecFwk } // OHOS diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h index 077c726d8b..32514a0f41 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h @@ -641,10 +641,11 @@ public: /** * @brief mark a process which is going restart. * @param uid the uid of the process. + * @param instanceKey the instance key of the process. * * @return Returns ERR_OK on success, others on failure. */ - int32_t SignRestartAppFlag(int32_t uid) override; + int32_t SignRestartAppFlag(int32_t uid, const std::string &instanceKey) override; /** * Get appRunningUniqueId by pid. @@ -740,7 +741,8 @@ public: virtual int32_t GetSupportedProcessCachePids(const std::string &bundleName, std::vector &pidList) override; - virtual int32_t GetAppIndexByPid(pid_t pid, int32_t &appIndex) override; + virtual int32_t KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack, + const std::string& reason) override; private: bool SendTransactCmd(AppMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply); bool WriteInterfaceToken(MessageParcel &data); diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h index ab649fc316..d8cd5acd4b 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h @@ -158,7 +158,7 @@ private: int32_t HandleNotifyProcessDependedOnWeb(MessageParcel &data, MessageParcel &reply); int32_t HandleKillProcessDependedOnWeb(MessageParcel &data, MessageParcel &reply); int32_t HandleRestartResidentProcessDependedOnWeb(MessageParcel &data, MessageParcel &reply); - int32_t HandleGetAppIndexByPid(MessageParcel &data, MessageParcel &reply); + int32_t HandleKillAppSelfWithInstanceKey(MessageParcel &data, MessageParcel &reply); int32_t OnRemoteRequestInner(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); int32_t OnRemoteRequestInnerFirst(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); diff --git a/interfaces/inner_api/app_manager/include/appmgr/running_process_info.h b/interfaces/inner_api/app_manager/include/appmgr/running_process_info.h index d82daa6019..6491c385ab 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/running_process_info.h +++ b/interfaces/inner_api/app_manager/include/appmgr/running_process_info.h @@ -65,6 +65,8 @@ struct RunningProcessInfo : public Parcelable { bool isStrictMode = false; std::int32_t bundleType = 0; std::int32_t appCloneIndex = -1; + std::string instanceKey = ""; + AppExecFwk::MultiAppModeType appMode = AppExecFwk::MultiAppModeType::UNSPECIFIED; bool ReadFromParcel(Parcel &parcel); virtual bool Marshalling(Parcel &parcel) const override; diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp index 5e7d204dc1..d5e986eded 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp @@ -1750,7 +1750,7 @@ int32_t AppMgrProxy::UpdateRenderState(pid_t renderPid, int32_t state) return reply.ReadInt32(); } -int32_t AppMgrProxy::SignRestartAppFlag(int32_t uid) +int32_t AppMgrProxy::SignRestartAppFlag(int32_t uid, const std::string &instanceKey) { TAG_LOGD(AAFwkTag::APPMGR, "called"); MessageParcel data; @@ -1761,6 +1761,7 @@ int32_t AppMgrProxy::SignRestartAppFlag(int32_t uid) return IPC_PROXY_ERR; } PARCEL_UTIL_WRITE_RET_INT(data, Int32, uid); + PARCEL_UTIL_WRITE_RET_INT(data, String, instanceKey); PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::SIGN_RESTART_APP_FLAG, data, reply, option); return reply.ReadInt32(); @@ -2073,25 +2074,22 @@ int32_t AppMgrProxy::CheckIsKiaProcess(pid_t pid, bool &isKia) return ERR_OK; } -int32_t AppMgrProxy::GetAppIndexByPid(pid_t pid, int32_t &appIndex) +int32_t AppMgrProxy::KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack, + const std::string& reason) { MessageParcel data; - MessageParcel reply; - MessageOption option(MessageOption::TF_SYNC); if (!WriteInterfaceToken(data)) { TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed."); return ERR_INVALID_VALUE; } - PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid); + PARCEL_UTIL_WRITE_RET_INT(data, String, instanceKey); + PARCEL_UTIL_WRITE_RET_INT(data, Bool, clearPageStack); + PARCEL_UTIL_WRITE_RET_INT(data, String, reason); - PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_APP_INDEX_BY_PID, data, reply, option); - int32_t ret = reply.ReadInt32(); - if (ret != ERR_OK) { - TAG_LOGE(AAFwkTag::APPMGR, "failed,ret=%{public}d.", ret); - return ret; - } - appIndex = reply.ReadInt32(); - return ERR_OK; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::KILL_APP_SELF_WITH_INSTANCE_KEY, data, reply, option); + return reply.ReadInt32(); } } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp index fca79aa583..b1c4ef4d76 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp @@ -345,8 +345,8 @@ int32_t AppMgrStub::OnRemoteRequestInnerSeventh(uint32_t code, MessageParcel &da return HandleRegisterKiaInterceptor(data, reply); case static_cast(AppMgrInterfaceCode::CHECK_IS_KIA_PROCESS): return HandleCheckIsKiaProcess(data, reply); - case static_cast(AppMgrInterfaceCode::GET_APP_INDEX_BY_PID): - return HandleGetAppIndexByPid(data, reply); + case static_cast(AppMgrInterfaceCode::KILL_APP_SELF_WITH_INSTANCE_KEY): + return HandleKillAppSelfWithInstanceKey(data, reply); } return INVALID_FD; } @@ -1468,7 +1468,8 @@ int32_t AppMgrStub::HandleSignRestartAppFlag(MessageParcel &data, MessageParcel { TAG_LOGD(AAFwkTag::APPMGR, "called"); auto uid = data.ReadInt32(); - auto ret = SignRestartAppFlag(uid); + auto instanceKey = data.ReadString(); + auto ret = SignRestartAppFlag(uid, instanceKey); if (!reply.WriteInt32(ret)) { TAG_LOGE(AAFwkTag::APPMGR, "Write ret error."); return IPC_STUB_ERR; @@ -1691,19 +1692,16 @@ int32_t AppMgrStub::HandleCheckIsKiaProcess(MessageParcel &data, MessageParcel & return NO_ERROR; } -int32_t AppMgrStub::HandleGetAppIndexByPid(MessageParcel &data, MessageParcel &reply) +int32_t AppMgrStub::HandleKillAppSelfWithInstanceKey(MessageParcel &data, MessageParcel &reply) { TAG_LOGD(AAFwkTag::APPMGR, "call"); - auto pid = data.ReadInt32(); - int32_t appIndex = -1; - int32_t result = GetAppIndexByPid(pid, appIndex); + auto instanceKey = data.ReadString(); + auto clearPageStack = data.ReadBool(); + auto reason = data.ReadString(); + auto result = KillAppSelfWithInstanceKey(instanceKey, clearPageStack, reason); if (!reply.WriteInt32(result)) { - TAG_LOGE(AAFwkTag::APPMGR, "fail to write GetAppIndexByPid result."); - return IPC_STUB_ERR; - } - if (!reply.WriteInt32(appIndex)) { - TAG_LOGE(AAFwkTag::APPMGR, "fail to write appIndex."); - return IPC_STUB_ERR; + TAG_LOGE(AAFwkTag::APPMGR, "fail to write result."); + return ERR_INVALID_VALUE; } return NO_ERROR; } diff --git a/interfaces/inner_api/app_manager/src/appmgr/running_process_info.cpp b/interfaces/inner_api/app_manager/src/appmgr/running_process_info.cpp index a4bce0c84f..83bd203b2b 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/running_process_info.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/running_process_info.cpp @@ -61,6 +61,10 @@ bool RunningProcessInfo::ReadFromParcel(Parcel &parcel) READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, extensionType); extensionType_ = static_cast(extensionType); appCloneIndex = parcel.ReadInt32(); + instanceKey = Str16ToStr8(parcel.ReadString16()); + int32_t appModeType; + READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appModeType); + appMode = static_cast(appModeType); return true; } @@ -95,6 +99,8 @@ bool RunningProcessInfo::Marshalling(Parcel &parcel) const WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast(processType_)); WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast(extensionType_)); WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appCloneIndex); + WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(instanceKey)); + WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast(appMode)); return true; } } // namespace AppExecFwk diff --git a/services/abilitymgr/include/ability_cache_manager.h b/services/abilitymgr/include/ability_cache_manager.h index c0120a3756..3910b35d1b 100644 --- a/services/abilitymgr/include/ability_cache_manager.h +++ b/services/abilitymgr/include/ability_cache_manager.h @@ -102,8 +102,9 @@ public: /** * Sign the restart flag by uid of ability from ability cache manager. * @param uid the ability uid to be searched in cache manager. + * @param instanceKey the instance key of the process. */ - void SignRestartAppFlag(int32_t uid); + void SignRestartAppFlag(int32_t uid, const std::string &instanceKey); /** * Delete the invalid ability by bundleName from ability cache manager. diff --git a/services/abilitymgr/include/ability_connect_manager.h b/services/abilitymgr/include/ability_connect_manager.h index 1b244daad7..af5db74d20 100644 --- a/services/abilitymgr/include/ability_connect_manager.h +++ b/services/abilitymgr/include/ability_connect_manager.h @@ -325,7 +325,7 @@ public: void CloseAssertDialog(const std::string &assertSessionId); - void SignRestartAppFlag(int32_t uid); + void SignRestartAppFlag(int32_t uid, const std::string &instanceKey); std::shared_ptr GetUIExtensionRootHostInfo(const sptr token); void UninstallApp(const std::string &bundleName); diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index e158c0550c..a6972d2053 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -2255,8 +2255,9 @@ private: void WaitBootAnimationStart(); - int32_t SignRestartAppFlag(int32_t userId, int32_t uid, bool isAppRecovery = false); - int32_t CheckRestartAppWant(const AAFwk::Want &want, int32_t appIndex); + int32_t SignRestartAppFlag(int32_t userId, int32_t uid, const std::string &instanceKey, + AppExecFwk::MultiAppModeType type, bool isAppRecovery = false); + int32_t CheckRestartAppWant(const AAFwk::Want &want, int32_t appIndex, int32_t userId); int StartUIAbilityForOptionWrap(const Want &want, const StartOptions &options, sptr callerToken, bool isPendingWantCaller, int32_t userId, int requestCode, uint32_t callerTokenId = 0, bool isImplicit = false, diff --git a/services/abilitymgr/include/mission/mission_list.h b/services/abilitymgr/include/mission/mission_list.h index 4d02f643af..6d08981a86 100644 --- a/services/abilitymgr/include/mission/mission_list.h +++ b/services/abilitymgr/include/mission/mission_list.h @@ -222,7 +222,7 @@ public: int32_t GetMissionCount() const; void GetActiveAbilityList(int32_t uid, std::vector &abilityList, int32_t pid = NO_PID); - void SignRestartAppFlag(int32_t uid); + void SignRestartAppFlag(int32_t uid, const std::string &instanceKey); private: std::string GetTypeName(); diff --git a/services/abilitymgr/include/mission/mission_list_manager.h b/services/abilitymgr/include/mission/mission_list_manager.h index 0df3158396..8802dbe851 100644 --- a/services/abilitymgr/include/mission/mission_list_manager.h +++ b/services/abilitymgr/include/mission/mission_list_manager.h @@ -365,7 +365,7 @@ public: bool IsAbilityStarted(AbilityRequest &abilityRequest, std::shared_ptr &targetRecord) override; - void SignRestartAppFlag(int32_t uid) override; + void SignRestartAppFlag(int32_t uid, const std::string &instanceKey) override; void SetAnimationFlag(bool IsAnimationEnabled); #ifdef SUPPORT_SCREEN diff --git a/services/abilitymgr/include/mission/mission_list_manager_interface.h b/services/abilitymgr/include/mission/mission_list_manager_interface.h index fa76426494..8e49dc6f95 100644 --- a/services/abilitymgr/include/mission/mission_list_manager_interface.h +++ b/services/abilitymgr/include/mission/mission_list_manager_interface.h @@ -113,7 +113,7 @@ public: const AAFwk::ContinueState &state) = 0; virtual bool IsAbilityStarted(AbilityRequest &abilityRequest, std::shared_ptr &targetRecord) = 0; - virtual void SignRestartAppFlag(int32_t uid) = 0; + virtual void SignRestartAppFlag(int32_t uid, const std::string &instanceKey) = 0; #ifdef SUPPORT_SCREEN public: virtual int SetMissionLabel(const sptr &abilityToken, const std::string &label) = 0; diff --git a/services/abilitymgr/include/restart_app_manager.h b/services/abilitymgr/include/restart_app_manager.h index 6dbca4219e..72fe75c28c 100644 --- a/services/abilitymgr/include/restart_app_manager.h +++ b/services/abilitymgr/include/restart_app_manager.h @@ -24,25 +24,47 @@ namespace OHOS { namespace AAFwk { +typedef struct RestartAppKey { + int32_t uid = 0; + std::string instanceKey = ""; + + RestartAppKey(std::string callingInstanceKey, int32_t callingUid) + { + instanceKey = callingInstanceKey; + uid = callingUid; + } + bool operator ==(const struct RestartAppKey &other) const + { + return instanceKey == other.instanceKey && uid == other.uid; + } +} RestartAppKeyType; + +struct RestartAppKeyHash { + size_t operator()(const RestartAppKeyType &p) const + { + return std::hash()(p.instanceKey) ^ std::hash()(p.uid); + } +}; + /** * @class RestartAppManager * RestartAppManager provides a facility for managing restart app history. */ class RestartAppManager { public: + using RestartAppMapType = std::unordered_map; virtual ~RestartAppManager() = default; static RestartAppManager &GetInstance(); - bool IsRestartAppFrequent(int32_t uid, time_t time); - void AddRestartAppHistory(int32_t uid, time_t time); - bool IsForegroundToRestartApp() const; + bool IsRestartAppFrequent(const RestartAppKeyType &key, time_t time); + void AddRestartAppHistory(const RestartAppKeyType &key, time_t time); private: RestartAppManager() = default; DISALLOW_COPY_AND_MOVE(RestartAppManager); ffrt::mutex restartAppMapLock_; - std::unordered_map restartAppHistory_; // RestartAppKey:time + RestartAppMapType restartAppHistory_; // RestartAppKey:time }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h b/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h index 92ccf55f2d..2e14804194 100644 --- a/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h +++ b/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h @@ -56,7 +56,7 @@ public: } }; - void SignRestartAppFlag(int32_t uid, bool isAppRecovery = false); + void SignRestartAppFlag(int32_t uid, const std::string &instanceKey, bool isAppRecovery = false); /** * StartUIAbility with request. diff --git a/services/abilitymgr/include/utils/want_utils.h b/services/abilitymgr/include/utils/want_utils.h index 04213bd6c0..2904422f29 100644 --- a/services/abilitymgr/include/utils/want_utils.h +++ b/services/abilitymgr/include/utils/want_utils.h @@ -54,14 +54,6 @@ public: * @return Flag if the want contains atomic service url. */ static bool IsAtomicServiceUrl(const Want& want); - - /** - * Get app index. - * - * @param want The want. - * @return AppIndex if the want contains app index. - */ - static int32_t GetAppIndex(const Want& want); }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/ability_cache_manager.cpp b/services/abilitymgr/src/ability_cache_manager.cpp index b0fbd2af87..acf05753d8 100644 --- a/services/abilitymgr/src/ability_cache_manager.cpp +++ b/services/abilitymgr/src/ability_cache_manager.cpp @@ -284,13 +284,14 @@ void AbilityCacheManager::RemoveLauncherDeathRecipient() } } -void AbilityCacheManager::SignRestartAppFlag(int32_t uid) +void AbilityCacheManager::SignRestartAppFlag(int32_t uid, const std::string &instanceKey) { std::lock_guard lock(mutex_); auto it = devRecLru_.begin(); while (it != devRecLru_.end()) { auto abilityRecord = *it; - if (abilityRecord != nullptr && abilityRecord->GetUid() == uid) { + if (abilityRecord != nullptr && abilityRecord->GetUid() == uid && + abilityRecord->GetInstanceKey() == instanceKey) { abilityRecord->SetRestartAppFlag(true); } it++; diff --git a/services/abilitymgr/src/ability_connect_manager.cpp b/services/abilitymgr/src/ability_connect_manager.cpp index d0b33ac5fb..1db4d8a164 100644 --- a/services/abilitymgr/src/ability_connect_manager.cpp +++ b/services/abilitymgr/src/ability_connect_manager.cpp @@ -3116,18 +3116,19 @@ int32_t AbilityConnectManager::GetUIExtensionSessionInfo(const sptrGetUIExtensionSessionInfo(token, uiExtensionSessionInfo); } -void AbilityConnectManager::SignRestartAppFlag(int32_t uid) +void AbilityConnectManager::SignRestartAppFlag(int32_t uid, const std::string &instanceKey) { { std::lock_guard lock(serviceMapMutex_); for (auto &[key, abilityRecord] : serviceMap_) { - if (abilityRecord == nullptr || abilityRecord->GetUid() != uid) { + if (abilityRecord == nullptr || abilityRecord->GetUid() != uid || + abilityRecord->GetInstanceKey() != instanceKey) { continue; } abilityRecord->SetRestartAppFlag(true); } } - AbilityCacheManager::GetInstance().SignRestartAppFlag(uid); + AbilityCacheManager::GetInstance().SignRestartAppFlag(uid, instanceKey); } bool AbilityConnectManager::AddToServiceMap(const std::string &key, std::shared_ptr abilityRecord) diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index dbdd9d2689..4de1eb0499 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -11252,55 +11252,50 @@ int32_t AbilityManagerService::GetUIExtensionSessionInfo(const sptr::GetInstance()->GetRunningProcessInfoByPid(callerPid, processInfo); + int32_t callerUid = IPCSkeleton::GetCallingUid(); + int32_t userId = callerUid / BASE_USER_RANGE; + auto result = CheckRestartAppWant(want, processInfo.appCloneIndex, userId); if (result != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITYMGR, "checkRestartAppWant error"); return result; } - - int32_t callerUid = IPCSkeleton::GetCallingUid(); - int64_t now = time(nullptr); - if (!isAppRecovery && RestartAppManager::GetInstance().IsRestartAppFrequent(callerUid, now)) { - return AAFwk::ERR_RESTART_APP_FREQUENT; - } - - bool isForegroundToRestartApp = RestartAppManager::GetInstance().IsForegroundToRestartApp(); - if (!isForegroundToRestartApp) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "restartApp, isForegroundToRestartApp failed"); + if (!processInfo.isFocused && !processInfo.isAbilityForegrounding) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "restartApp, is not foreground"); return AAFwk::NOT_TOP_ABILITY; } - int32_t userId = IPCSkeleton::GetCallingUid() / BASE_USER_RANGE; - result = SignRestartAppFlag(userId, callerUid, isAppRecovery); + RestartAppKeyType key(processInfo.instanceKey, callerUid); + int64_t now = time(nullptr); + if (!isAppRecovery && RestartAppManager::GetInstance().IsRestartAppFrequent(key, now)) { + return AAFwk::ERR_RESTART_APP_FREQUENT; + } + + result = SignRestartAppFlag(userId, callerUid, processInfo.instanceKey, processInfo.appMode, isAppRecovery); if (result != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITYMGR, "signRestartAppFlag error"); return result; } - result = DelayedSingleton::GetInstance()->KillApplicationSelf(false, "RestartApp"); - if (result != ERR_OK) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "killApplicationSelf error"); - return result; - } - TAG_LOGD(AAFwkTag::ABILITYMGR, "restartApp, without checkCallAbilityPermission."); - (const_cast(want)).SetParam(AAFwk::Want::PARAM_APP_CLONE_INDEX_KEY, appIndex); - result = StartAbilityWrap(want, nullptr, - DEFAULT_INVAL_VALUE, false, DEFAULT_INVAL_VALUE, false, 0, isForegroundToRestartApp); + (const_cast(want)).SetParam(AAFwk::Want::PARAM_APP_CLONE_INDEX_KEY, processInfo.appCloneIndex); + (const_cast(want)).SetParam(AAFwk::Want::APP_INSTANCE_KEY, processInfo.instanceKey); + (const_cast(want)).RemoveParam(Want::CREATE_APP_INSTANCE_KEY); + result = StartAbilityWrap(want, nullptr, DEFAULT_INVAL_VALUE, false, DEFAULT_INVAL_VALUE, false, 0, true); if (result != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITYMGR, "startAbility error"); return result; } if (!isAppRecovery) { - RestartAppManager::GetInstance().AddRestartAppHistory(callerUid, now); + RestartAppManager::GetInstance().AddRestartAppHistory(key, now); } return result; } -int32_t AbilityManagerService::CheckRestartAppWant(const AAFwk::Want &want, int32_t appIndex) +int32_t AbilityManagerService::CheckRestartAppWant(const AAFwk::Want &want, int32_t appIndex, int32_t userId) { std::string bundleName = want.GetElement().GetBundleName(); - auto userId = IPCSkeleton::GetCallingUid() / BASE_USER_RANGE; if (!CheckCallingTokenId(bundleName, userId, appIndex)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "no itself called, no allowed"); return AAFwk::ERR_RESTART_APP_INCORRECT_ABILITY; @@ -11320,34 +11315,40 @@ int32_t AbilityManagerService::CheckRestartAppWant(const AAFwk::Want &want, int3 return ERR_OK; } -int32_t AbilityManagerService::SignRestartAppFlag(int32_t userId, int32_t uid, bool isAppRecovery) +int32_t AbilityManagerService::SignRestartAppFlag(int32_t userId, int32_t uid, const std::string &instanceKey, + AppExecFwk::MultiAppModeType type, bool isAppRecovery) { auto appMgr = AppMgrUtil::GetAppMgr(); if (appMgr == nullptr) { TAG_LOGW(AAFwkTag::ABILITYMGR, "AppMgrUtil::GetAppMgr failed"); return ERR_INVALID_VALUE; } - auto ret = IN_PROCESS_CALL(appMgr->SignRestartAppFlag(uid)); + auto ret = IN_PROCESS_CALL(appMgr->SignRestartAppFlag(uid, instanceKey)); if (ret != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITYMGR, "appMgr signRestartAppFlag error"); return ret; } auto connectManager = GetConnectManagerByUserId(userId); - connectManager->SignRestartAppFlag(uid); + connectManager->SignRestartAppFlag(uid, instanceKey); if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) { auto uiAbilityManager = GetUIAbilityManagerByUserId(userId); CHECK_POINTER_AND_RETURN(uiAbilityManager, ERR_INVALID_VALUE); - uiAbilityManager->SignRestartAppFlag(uid, isAppRecovery); - return ERR_OK; + uiAbilityManager->SignRestartAppFlag(uid, instanceKey, isAppRecovery); + } else { + auto missionListManager = GetMissionListManagerByUserId(userId); + if (missionListManager == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "missionListManager null userId:%{public}d", userId); + return ERR_INVALID_VALUE; + } + missionListManager->SignRestartAppFlag(uid, instanceKey); } - auto missionListManager = GetMissionListManagerByUserId(userId); - if (missionListManager == nullptr) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "missionListManager null userId:%{public}d", userId); - return ERR_INVALID_VALUE; + + if (type == AppExecFwk::MultiAppModeType::MULTI_INSTANCE) { + return appMgr->KillAppSelfWithInstanceKey(instanceKey, false, "RestartInstance"); + } else { + return DelayedSingleton::GetInstance()->KillApplicationSelf(false, "RestartApp"); } - missionListManager->SignRestartAppFlag(uid); - return ERR_OK; } bool AbilityManagerService::IsEmbeddedOpenAllowed(sptr callerToken, const std::string &appId) diff --git a/services/abilitymgr/src/mission/mission_list.cpp b/services/abilitymgr/src/mission/mission_list.cpp index 3b517c1643..19d34bddc1 100644 --- a/services/abilitymgr/src/mission/mission_list.cpp +++ b/services/abilitymgr/src/mission/mission_list.cpp @@ -468,7 +468,7 @@ void MissionList::GetActiveAbilityList(int32_t uid, std::vector &ab } } -void MissionList::SignRestartAppFlag(int32_t uid) +void MissionList::SignRestartAppFlag(int32_t uid, const std::string &instanceKey) { for (auto it = missions_.begin(); it != missions_.end();) { auto mission = *it; @@ -485,6 +485,10 @@ void MissionList::SignRestartAppFlag(int32_t uid) it++; continue; } + if (abilityRecord->GetInstanceKey() != instanceKey) { + it++; + continue; + } abilityRecord->SetRestartAppFlag(true); it = missions_.erase(it); } diff --git a/services/abilitymgr/src/mission/mission_list_manager.cpp b/services/abilitymgr/src/mission/mission_list_manager.cpp index f2a4535a9f..c2077b2304 100644 --- a/services/abilitymgr/src/mission/mission_list_manager.cpp +++ b/services/abilitymgr/src/mission/mission_list_manager.cpp @@ -4272,20 +4272,20 @@ void MissionListManager::SendKeyEvent(const AbilityRequest &abilityRequest) EventReport::SendKeyEvent(EventName::START_PRIVATE_ABILITY, HiSysEventType::BEHAVIOR, eventInfo); } -void MissionListManager::SignRestartAppFlag(int32_t uid) +void MissionListManager::SignRestartAppFlag(int32_t uid, const std::string &instanceKey) { std::lock_guard guard(managerLock_); for (const auto& missionList : currentMissionLists_) { if (!missionList) { continue; } - missionList->SignRestartAppFlag(uid); + missionList->SignRestartAppFlag(uid, instanceKey); } if (defaultStandardList_) { - defaultStandardList_->SignRestartAppFlag(uid); + defaultStandardList_->SignRestartAppFlag(uid, instanceKey); } if (defaultSingleList_) { - defaultSingleList_->SignRestartAppFlag(uid); + defaultSingleList_->SignRestartAppFlag(uid, instanceKey); } } diff --git a/services/abilitymgr/src/restart_app_manager.cpp b/services/abilitymgr/src/restart_app_manager.cpp index 0f500ab943..1a750d0bdc 100644 --- a/services/abilitymgr/src/restart_app_manager.cpp +++ b/services/abilitymgr/src/restart_app_manager.cpp @@ -28,11 +28,11 @@ RestartAppManager &RestartAppManager::GetInstance() return instance; } -bool RestartAppManager::IsRestartAppFrequent(int32_t uid, time_t time) +bool RestartAppManager::IsRestartAppFrequent(const RestartAppKeyType &key, time_t time) { std::lock_guard lock(restartAppMapLock_); constexpr int64_t MIN_RESTART_TIME = 10; - auto it = restartAppHistory_.find(uid); + auto it = restartAppHistory_.find(key); if ((it != restartAppHistory_.end()) && (it->second + MIN_RESTART_TIME > time)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "restart too frequently. try again at least 10s later"); return true; @@ -40,24 +40,11 @@ bool RestartAppManager::IsRestartAppFrequent(int32_t uid, time_t time) return false; } -void RestartAppManager::AddRestartAppHistory(int32_t uid, time_t time) +void RestartAppManager::AddRestartAppHistory(const RestartAppKeyType &key, time_t time) { std::lock_guard lock(restartAppMapLock_); - TAG_LOGD(AAFwkTag::ABILITYMGR, "Refresh history, uid=%{public}d", uid); - restartAppHistory_[uid] = time; -} - -bool RestartAppManager::IsForegroundToRestartApp() const -{ - TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); - auto callerPid = IPCSkeleton::GetCallingPid(); - AppExecFwk::RunningProcessInfo processInfo; - DelayedSingleton::GetInstance()->GetRunningProcessInfoByPid(callerPid, processInfo); - if (processInfo.isFocused || processInfo.isAbilityForegrounding) { - return true; - } - TAG_LOGE(AAFwkTag::ABILITYMGR, "IsForegroundToRestartApp, state not foreground"); - return false; + TAG_LOGD(AAFwkTag::ABILITYMGR, "Refresh uid=%{public}d, instanceKey:%{public}s", key.uid, key.instanceKey.c_str()); + restartAppHistory_[key] = time; } } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp index 9801b2adc4..10ca8a906b 100644 --- a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp +++ b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp @@ -2652,12 +2652,13 @@ int32_t UIAbilityLifecycleManager::UpdateSessionInfoBySCB(std::list return ERR_OK; } -void UIAbilityLifecycleManager::SignRestartAppFlag(int32_t uid, bool isAppRecovery) +void UIAbilityLifecycleManager::SignRestartAppFlag(int32_t uid, const std::string &instanceKey, bool isAppRecovery) { std::lock_guard guard(sessionLock_); auto tempSessionAbilityMap = sessionAbilityMap_; for (auto &[sessionId, abilityRecord] : tempSessionAbilityMap) { - if (abilityRecord == nullptr || abilityRecord->GetUid() != uid) { + if (abilityRecord == nullptr || abilityRecord->GetUid() != uid || + abilityRecord->GetInstanceKey() != instanceKey) { continue; } abilityRecord->SetRestartAppFlag(true); diff --git a/services/abilitymgr/src/utils/want_utils.cpp b/services/abilitymgr/src/utils/want_utils.cpp index a9efde11b1..fc595aaa28 100644 --- a/services/abilitymgr/src/utils/want_utils.cpp +++ b/services/abilitymgr/src/utils/want_utils.cpp @@ -81,23 +81,5 @@ bool WantUtils::IsAtomicServiceUrl(const Want& want) } return isAtomicServiceShortUrl; } - -int32_t WantUtils::GetAppIndex(const Want& want) -{ - int32_t appIndex = want.GetIntParam(AAFwk::Want::PARAM_APP_CLONE_INDEX_KEY, -1); - if (appIndex == -1) { - auto appMgr = AppMgrUtil::GetAppMgr(); - if (appMgr == nullptr) { - TAG_LOGW(AAFwkTag::ABILITYMGR, "AppMgrUtil::GetAppMgr failed"); - return appIndex; - } - auto callingPid = IPCSkeleton::GetCallingPid(); - auto ret = IN_PROCESS_CALL(appMgr->GetAppIndexByPid(callingPid, appIndex)); - if (ret != ERR_OK) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "appMgr GetAppIndexByPid error"); - } - } - return appIndex; -} } // namespace AAFwk } // namespace OHOS diff --git a/services/appmgr/include/app_mgr_service.h b/services/appmgr/include/app_mgr_service.h index dceebc9061..e00177205e 100644 --- a/services/appmgr/include/app_mgr_service.h +++ b/services/appmgr/include/app_mgr_service.h @@ -683,10 +683,11 @@ public: /** * @brief mark a process which is going restart. * @param uid the uid of the process. + * @param instanceKey the instance key of the process. * * @return Returns ERR_OK on success, others on failure. */ - int32_t SignRestartAppFlag(int32_t uid) override; + int32_t SignRestartAppFlag(int32_t uid, const std::string &instanceKey) override; /** * Get appRunningUniqueId by pid. @@ -772,7 +773,8 @@ public: */ virtual void RestartResidentProcessDependedOnWeb() override; - int32_t GetAppIndexByPid(pid_t pid, int32_t &appIndex) override; + virtual int32_t KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack, + const std::string& reason) override; private: /** * Init, Initialize application services. diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index e9365e7051..f6cea3f222 100644 --- a/services/appmgr/include/app_mgr_service_inner.h +++ b/services/appmgr/include/app_mgr_service_inner.h @@ -314,6 +314,8 @@ public: virtual int32_t KillApplicationSelf(const bool clearPageStack = false, const std::string& reason = "KillApplicationSelf"); + int32_t KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack, const std::string& reason); + /** * KillApplicationByUserId, kill the application by user ID. * @@ -1237,9 +1239,7 @@ public: /** * Mark processes of the uid as the app is going to be restarted. */ - int32_t SignRestartAppFlag(int32_t uid); - - int32_t GetAppIndexByPid(pid_t pid, int32_t &appIndex) const; + int32_t SignRestartAppFlag(int32_t uid, const std::string &instanceKey); /** * Set application assertion pause state. diff --git a/services/appmgr/include/app_running_manager.h b/services/appmgr/include/app_running_manager.h index f50a6b3b30..67f83b0892 100644 --- a/services/appmgr/include/app_running_manager.h +++ b/services/appmgr/include/app_running_manager.h @@ -255,6 +255,8 @@ public: bool ProcessExitByBundleNameAndUid( const std::string &bundleName, const int uid, std::list &pids, const bool clearPageStack = false); + bool ProcessExitByTokenIdAndInstance(uint32_t accessTokenId, const std::string &instanceKey, std::list &pids, + bool clearPageStack); bool GetPidsByUserId(int32_t userId, std::list &pids); void PrepareTerminate(const sptr &token, bool clearMissionFlag = false); @@ -312,7 +314,7 @@ public: */ int32_t GetAllAppRunningRecordCountByBundleName(const std::string &bundleName); - int32_t SignRestartAppFlag(int32_t uid); + int32_t SignRestartAppFlag(int32_t uid, const std::string &instanceKey); int32_t GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId); diff --git a/services/appmgr/src/app_mgr_service.cpp b/services/appmgr/src/app_mgr_service.cpp index aaa4de696f..ec54c182d5 100644 --- a/services/appmgr/src/app_mgr_service.cpp +++ b/services/appmgr/src/app_mgr_service.cpp @@ -1474,7 +1474,7 @@ int32_t AppMgrService::UpdateRenderState(pid_t renderPid, int32_t state) return appMgrServiceInner_->UpdateRenderState(renderPid, state); } -int32_t AppMgrService::SignRestartAppFlag(int32_t uid) +int32_t AppMgrService::SignRestartAppFlag(int32_t uid, const std::string &instanceKey) { if (!IsReady()) { TAG_LOGE(AAFwkTag::APPMGR, "not ready"); @@ -1486,7 +1486,7 @@ int32_t AppMgrService::SignRestartAppFlag(int32_t uid) TAG_LOGE(AAFwkTag::APPMGR, "verification failed"); return ERR_PERMISSION_DENIED; } - return appMgrServiceInner_->SignRestartAppFlag(uid); + return appMgrServiceInner_->SignRestartAppFlag(uid, instanceKey); } int32_t AppMgrService::GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId) @@ -1667,19 +1667,14 @@ int32_t AppMgrService::CheckIsKiaProcess(pid_t pid, bool &isKia) return appMgrServiceInner_->CheckIsKiaProcess(pid, isKia); } -int32_t AppMgrService::GetAppIndexByPid(pid_t pid, int32_t &appIndex) +int32_t AppMgrService::KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack, + const std::string& reason) { - bool isCallingPermission = - AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_PROCESS); - if (!isCallingPermission) { - TAG_LOGE(AAFwkTag::APPMGR, "verification failed"); - return ERR_PERMISSION_DENIED; - } if (!appMgrServiceInner_) { TAG_LOGE(AAFwkTag::APPMGR, "appMgrServiceInner_ is nullptr"); return ERR_INVALID_VALUE; } - return appMgrServiceInner_->GetAppIndexByPid(pid, appIndex); + return appMgrServiceInner_->KillAppSelfWithInstanceKey(instanceKey, clearPageStack, reason); } } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 5c57ddaccf..b756070bec 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -1584,15 +1584,47 @@ int32_t AppMgrServiceInner::KillApplicationSelf(const bool clearPageStack, const TAG_LOGI(AAFwkTag::APPMGR, "remote process exited successs"); return ERR_OK; } + int32_t result = ERR_OK; for (auto iter = pids.begin(); iter != pids.end(); ++iter) { - auto result = KillProcessByPid(*iter, reason); - if (result < 0) { + auto singleRet = KillProcessByPid(*iter, reason); + if (singleRet < 0) { TAG_LOGE(AAFwkTag::APPMGR, "killApplication fail for bundleName:%{public}s pid:%{public}d", bundleName.c_str(), *iter); - return result; + result = singleRet; } } - return ERR_OK; + return result; +} + +int32_t AppMgrServiceInner::KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack, + const std::string& reason) +{ + if (!appRunningManager_) { + TAG_LOGE(AAFwkTag::APPMGR, "appRunningManager_ null"); + return ERR_NO_INIT; + } + + int64_t startTime = SystemTimeMillisecond(); + auto callingTokenId = IPCSkeleton::GetCallingTokenID(); + TAG_LOGI(AAFwkTag::APPMGR, "callingTokenId value: %{public}d", callingTokenId); + std::list pids; + if (!appRunningManager_->ProcessExitByTokenIdAndInstance(callingTokenId, instanceKey, pids, clearPageStack)) { + TAG_LOGI(AAFwkTag::APPMGR, "not exist"); + return ERR_OK; + } + if (WaitForRemoteProcessExit(pids, startTime)) { + TAG_LOGI(AAFwkTag::APPMGR, "remote process exited success"); + return ERR_OK; + } + int32_t result = ERR_OK; + for (auto iter = pids.begin(); iter != pids.end(); ++iter) { + auto singleRet = KillProcessByPid(*iter, reason); + if (singleRet < 0) { + TAG_LOGE(AAFwkTag::APPMGR, "Failed to kill pid:%{public}d", *iter); + result = singleRet; + } + } + return result; } int32_t AppMgrServiceInner::KillApplicationByBundleName( @@ -7506,25 +7538,14 @@ int32_t AppMgrServiceInner::UpdateRenderState(pid_t renderPid, int32_t state) renderRecord, state); } -int32_t AppMgrServiceInner::SignRestartAppFlag(int32_t uid) +int32_t AppMgrServiceInner::SignRestartAppFlag(int32_t uid, const std::string &instanceKey) { TAG_LOGD(AAFwkTag::APPMGR, "call."); if (!appRunningManager_) { TAG_LOGE(AAFwkTag::APPMGR, "appRunningManager_ null"); return ERR_NO_INIT; } - return appRunningManager_->SignRestartAppFlag(uid); -} - -int32_t AppMgrServiceInner::GetAppIndexByPid(pid_t pid, int32_t &appIndex) const -{ - auto appRecord = GetAppRunningRecordByPid(pid); - if (appRecord == nullptr) { - TAG_LOGE(AAFwkTag::APPMGR, "no appRecord, pid:%{public}d", pid); - return ERR_INVALID_VALUE; - } - appIndex = appRecord->GetAppIndex(); - return ERR_OK; + return appRunningManager_->SignRestartAppFlag(uid, instanceKey); } int32_t AppMgrServiceInner::GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId) diff --git a/services/appmgr/src/app_running_manager.cpp b/services/appmgr/src/app_running_manager.cpp index ee3684363c..58610e135c 100644 --- a/services/appmgr/src/app_running_manager.cpp +++ b/services/appmgr/src/app_running_manager.cpp @@ -375,6 +375,46 @@ bool AppRunningManager::ProcessExitByBundleNameAndUid( return (pids.empty() ? false : true); } +bool AppRunningManager::ProcessExitByTokenIdAndInstance(uint32_t accessTokenId, const std::string &instanceKey, + std::list &pids, bool clearPageStack) +{ + auto appRunningMap = GetAppRunningRecordMap(); + for (const auto &item : appRunningMap) { + const auto &appRecord = item.second; + if (appRecord == nullptr) { + continue; + } + auto appInfo = appRecord->GetApplicationInfo(); + if (appInfo == nullptr) { + continue; + } + if (appInfo->multiAppMode.multiAppModeType != MultiAppModeType::MULTI_INSTANCE) { + TAG_LOGI(AAFwkTag::APPMGR, "not multi-instance"); + } + if (appInfo->accessTokenId != accessTokenId) { + continue; + } + if (appRecord->GetInstanceKey() != instanceKey) { + continue; + } + if (appRecord->GetPriorityObject() == nullptr) { + continue; + } + pid_t pid = appRecord->GetPriorityObject()->GetPid(); + if (pid <= 0) { + continue; + } + pids.push_back(pid); + if (clearPageStack) { + appRecord->ScheduleClearPageStack(); + } + appRecord->SetKilling(); + appRecord->ScheduleProcessSecurityExit(); + } + + return pids.empty() ? false : true; +} + bool AppRunningManager::GetPidsByBundleNameUserIdAndAppIndex(const std::string &bundleName, const int userId, const int appIndex, std::list &pids) { @@ -760,11 +800,10 @@ int32_t AppRunningManager::AssignRunningProcessInfoByAppRecord( auto appInfo = appRecord->GetApplicationInfo(); if (appInfo) { info.bundleType = static_cast(appInfo->bundleType); + info.appMode = appInfo->multiAppMode.multiAppModeType; } - if (appInfo && (static_cast(appInfo->multiAppMode.multiAppModeType) == - static_cast(MultiAppModeType::APP_CLONE))) { - info.appCloneIndex = appRecord->GetAppIndex(); - } + info.appCloneIndex = appRecord->GetAppIndex(); + info.instanceKey = appRecord->GetInstanceKey(); return ERR_OK; } @@ -1389,13 +1428,13 @@ std::shared_ptr AppRunningManager::OnChildProcessRemoteDied( return nullptr; } -int32_t AppRunningManager::SignRestartAppFlag(int32_t uid) +int32_t AppRunningManager::SignRestartAppFlag(int32_t uid, const std::string &instanceKey) { TAG_LOGD(AAFwkTag::APPMGR, "called"); std::lock_guard guard(runningRecordMapMutex_); for (const auto &item : appRunningRecordMap_) { const auto &appRecord = item.second; - if (appRecord == nullptr || appRecord->GetUid() != uid) { + if (appRecord == nullptr || appRecord->GetUid() != uid || appRecord->GetInstanceKey() != instanceKey) { continue; } TAG_LOGD(AAFwkTag::APPMGR, "sign"); diff --git a/test/fuzztest/abilityappmgrapprunningmanager_fuzzer/abilityappmgrapprunningmanager_fuzzer.cpp b/test/fuzztest/abilityappmgrapprunningmanager_fuzzer/abilityappmgrapprunningmanager_fuzzer.cpp index 513db584f5..03cf7ab495 100644 --- a/test/fuzztest/abilityappmgrapprunningmanager_fuzzer/abilityappmgrapprunningmanager_fuzzer.cpp +++ b/test/fuzztest/abilityappmgrapprunningmanager_fuzzer/abilityappmgrapprunningmanager_fuzzer.cpp @@ -132,7 +132,7 @@ void DoSomethingInterestingWithMyAPIaddb(const char* data, size_t size) manager->OnChildProcessRemoteDied(remote); manager->GetAllAppRunningRecordCountByBundleName(jsonStr); auto uid = static_cast(GetU32Data(data)); - manager->SignRestartAppFlag(uid); + manager->SignRestartAppFlag(uid, jsonStr); manager->GetAppRunningUniqueIdByPid(pidApps, jsonStr); std::vector hostPids; manager->GetAllUIExtensionRootHostPid(pidApps, hostPids); diff --git a/test/fuzztest/abilitycachemanagera_fuzzer/abilitycachemanagera_fuzzer.cpp b/test/fuzztest/abilitycachemanagera_fuzzer/abilitycachemanagera_fuzzer.cpp index b6a6f6427f..9971937d18 100755 --- a/test/fuzztest/abilitycachemanagera_fuzzer/abilitycachemanagera_fuzzer.cpp +++ b/test/fuzztest/abilitycachemanagera_fuzzer/abilitycachemanagera_fuzzer.cpp @@ -111,7 +111,7 @@ void AbilityCacheManagerFuzztest1(bool boolParam, std::string &stringParam, int3 mgr.FindRecordBySessionId(stringParam); mgr.FindRecordByServiceKey(stringParam); mgr.RemoveLauncherDeathRecipient(); - mgr.SignRestartAppFlag(int32Param); + mgr.SignRestartAppFlag(int32Param, stringParam); mgr.DeleteInvalidServiceRecord(stringParam); } diff --git a/test/fuzztest/abilitymgrrestartappmanager_fuzzer/abilitymgrrestartappmanager_fuzzer.cpp b/test/fuzztest/abilitymgrrestartappmanager_fuzzer/abilitymgrrestartappmanager_fuzzer.cpp index 891a596d9c..8ab6a7c8a6 100644 --- a/test/fuzztest/abilitymgrrestartappmanager_fuzzer/abilitymgrrestartappmanager_fuzzer.cpp +++ b/test/fuzztest/abilitymgrrestartappmanager_fuzzer/abilitymgrrestartappmanager_fuzzer.cpp @@ -52,11 +52,11 @@ bool DoSomethingInterestingWithMyAPI(const char *data, size_t size) std::shared_ptr restartAppManager = std::make_shared(); std::string stringParam(data, size); int32_t uid = static_cast(GetU32Data(data)); + RestartAppKeyType key(stringParam, uid); time_t currentTime = static_cast(GetU32Data(data)); - restartAppManager-> GetInstance(); - restartAppManager-> IsRestartAppFrequent(uid, currentTime); - restartAppManager-> AddRestartAppHistory(uid, currentTime); - restartAppManager-> IsForegroundToRestartApp(); + restartAppManager->GetInstance(); + restartAppManager->IsRestartAppFrequent(key, currentTime); + restartAppManager->AddRestartAppHistory(key, currentTime); return true; } } // namespace OHOS diff --git a/test/fuzztest/uiabilitylifecyclemanagera_fuzzer/uiabilitylifecyclemanagera_fuzzer.cpp b/test/fuzztest/uiabilitylifecyclemanagera_fuzzer/uiabilitylifecyclemanagera_fuzzer.cpp index e984f9edc6..056ff9abff 100644 --- a/test/fuzztest/uiabilitylifecyclemanagera_fuzzer/uiabilitylifecyclemanagera_fuzzer.cpp +++ b/test/fuzztest/uiabilitylifecyclemanagera_fuzzer/uiabilitylifecyclemanagera_fuzzer.cpp @@ -106,7 +106,7 @@ bool DoSomethingInterestingWithMyAPI(const char *data, size_t size) auto uIAbilityLifecycleManager = std::make_shared(userId); std::string strParam(data, size); int32_t uid = static_cast(GetU32Data(data)); - uIAbilityLifecycleManager->SignRestartAppFlag(uid); + uIAbilityLifecycleManager->SignRestartAppFlag(uid, strParam); AbilityRequest abilityRequest; sptr sessionInfo; uint32_t sceneFlag = GetU32Data(data); diff --git a/test/unittest/ability_cache_manager_test/ability_cache_manager_test.cpp b/test/unittest/ability_cache_manager_test/ability_cache_manager_test.cpp index b3c09e0877..00d46cef84 100644 --- a/test/unittest/ability_cache_manager_test/ability_cache_manager_test.cpp +++ b/test/unittest/ability_cache_manager_test/ability_cache_manager_test.cpp @@ -624,7 +624,7 @@ HWTEST_F(AbilityCacheManagerTest, AbilityCacheManagerSignRestartAppFlag_001, Tes int recId = abilityRecord_->GetRecordId(); std::shared_ptr rec = OHOS::AAFwk::AbilityCacheManager::GetInstance().Put(abilityRecord_); EXPECT_EQ(rec, nullptr); - OHOS::AAFwk::AbilityCacheManager::GetInstance().SignRestartAppFlag(applicationInfo.uid); + OHOS::AAFwk::AbilityCacheManager::GetInstance().SignRestartAppFlag(applicationInfo.uid, ""); AbilityRequest abilityRequest; abilityRequest.abilityInfo = abilityInfo; abilityRequest.appInfo = applicationInfo; diff --git a/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp b/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp index 6b78d528d8..4793aaa04f 100644 --- a/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp +++ b/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp @@ -3246,7 +3246,7 @@ HWTEST_F(AbilityConnectManagerTest, AAFwk_AbilityMS_SignRestartAppFlag_001, Test abilityRecord2->abilityInfo_.bundleName = "errTestBundleName"; connectManager->serviceMap_.emplace("second", abilityRecord2); int32_t uid = 100; - connectManager->SignRestartAppFlag(uid); + connectManager->SignRestartAppFlag(uid, ""); } /* diff --git a/test/unittest/ability_manager_service_third_test/ability_manager_service_third_test.cpp b/test/unittest/ability_manager_service_third_test/ability_manager_service_third_test.cpp index 943d84e3ce..fb118d6579 100644 --- a/test/unittest/ability_manager_service_third_test/ability_manager_service_third_test.cpp +++ b/test/unittest/ability_manager_service_third_test/ability_manager_service_third_test.cpp @@ -1096,7 +1096,8 @@ HWTEST_F(AbilityManagerServiceThirdTest, CheckRestartAppWant_001, TestSize.Level auto abilityMs = std::make_shared(); EXPECT_NE(abilityMs, nullptr); AAFwk::Want want; - int32_t res = abilityMs->CheckRestartAppWant(want, 0); + int32_t userId = 100; + int32_t res = abilityMs->CheckRestartAppWant(want, 0, userId); EXPECT_EQ(res, AAFwk::ERR_RESTART_APP_INCORRECT_ABILITY); } @@ -2468,7 +2469,8 @@ HWTEST_F(AbilityManagerServiceThirdTest, SignRestartAppFlag_001, TestSize.Level1 auto abilityMs_ = std::make_shared(); EXPECT_NE(abilityMs_, nullptr); - abilityMs_->SignRestartAppFlag(USER_ID_U100, 1); + int32_t uid = 100; + abilityMs_->SignRestartAppFlag(USER_ID_U100, uid, "", AppExecFwk::MultiAppModeType::UNSPECIFIED, 1); } /* diff --git a/test/unittest/app_mgr_proxy_test/app_mgr_proxy_test.cpp b/test/unittest/app_mgr_proxy_test/app_mgr_proxy_test.cpp index 40702770cb..6c20e61589 100644 --- a/test/unittest/app_mgr_proxy_test/app_mgr_proxy_test.cpp +++ b/test/unittest/app_mgr_proxy_test/app_mgr_proxy_test.cpp @@ -555,7 +555,7 @@ HWTEST_F(AppMgrProxyTest, SignRestartAppFlag_0100, TestSize.Level1) { EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)).Times(1); int32_t uid = 0; - auto res = appMgrProxy_->SignRestartAppFlag(uid); + auto res = appMgrProxy_->SignRestartAppFlag(uid, ""); EXPECT_EQ(res, NO_ERROR); } @@ -761,27 +761,5 @@ HWTEST_F(AppMgrProxyTest, GetSupportedProcessCachePids_001, TestSize.Level0) TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); } - -/** - * @tc.name: GetAppIndexByPid_001 - * @tc.desc: Get app index of pid. - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(AppMgrProxyTest, GetAppIndexByPid_001, TestSize.Level1) -{ - TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); - - EXPECT_CALL(*mockAppMgrService_, SendRequest(_, _, _, _)) - .Times(1) - .WillOnce(Invoke(mockAppMgrService_.GetRefPtr(), &MockAppMgrService::InvokeSendRequest)); - - pid_t pid = 1; - int32_t appIndex = -1; - appMgrProxy_->GetAppIndexByPid(pid, appIndex); - EXPECT_EQ(mockAppMgrService_->code_, static_cast(AppMgrInterfaceCode::GET_APP_INDEX_BY_PID)); - - TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); -} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp b/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp index b44471f4a2..5c116dbb1f 100644 --- a/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp +++ b/test/unittest/app_running_manager_second_test/app_running_manager_second_test.cpp @@ -1545,7 +1545,7 @@ HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_SignRestartAppFlag_0100, * @tc.steps: step2. Initialize AppRunningManager instance * @tc.expected: expect step1 first focused true */ - auto ret = appRunningManager->SignRestartAppFlag(0); + auto ret = appRunningManager->SignRestartAppFlag(0, ""); EXPECT_EQ(ret, ERR_INVALID_VALUE); TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_SignRestartAppFlag_0100 end"); } @@ -1572,7 +1572,7 @@ HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_SignRestartAppFlag_0200, * @tc.steps: step2. Initialize AppRunningManager instance * @tc.expected: expect step1 different bundle unfocused true */ - auto ret = appRunningManager->SignRestartAppFlag(0); + auto ret = appRunningManager->SignRestartAppFlag(0, ""); EXPECT_EQ(ret, ERR_INVALID_VALUE); TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_SignRestartAppFlag_0200 end"); } @@ -1600,7 +1600,7 @@ HWTEST_F(AppRunningManagerSecondTest, AppRunningManager_SignRestartAppFlag_0300, * @tc.steps: step2. Initialize AppRunningManager instance * @tc.expected: expect step2 focused false */ - auto ret = appRunningManager->SignRestartAppFlag(0); + auto ret = appRunningManager->SignRestartAppFlag(0, ""); EXPECT_EQ(ret, ERR_OK); EXPECT_TRUE(record->GetRestartAppFlag()); TAG_LOGI(AAFwkTag::TEST, "AppRunningManager_SignRestartAppFlag_0300 end"); diff --git a/test/unittest/mission_list_manager_second_test/mission_list_manager_second_test.cpp b/test/unittest/mission_list_manager_second_test/mission_list_manager_second_test.cpp index 1a331a6cfb..505971ce15 100644 --- a/test/unittest/mission_list_manager_second_test/mission_list_manager_second_test.cpp +++ b/test/unittest/mission_list_manager_second_test/mission_list_manager_second_test.cpp @@ -162,17 +162,17 @@ HWTEST_F(MissionListManagerSecondTest, SignRestartAppFlag_001, TestSize.Level1) missionList->missions_.push_front(mission); missionListManager->currentMissionLists_.push_front(missionList); missionListManager->currentMissionLists_.push_front(missionList2); - missionListManager->SignRestartAppFlag(uid); + missionListManager->SignRestartAppFlag(uid, ""); auto result = missionListManager->currentMissionLists_.size(); EXPECT_EQ(2, result); missionListManager->defaultStandardList_ = missionList; - missionListManager->SignRestartAppFlag(uid); + missionListManager->SignRestartAppFlag(uid, ""); result = missionListManager->defaultStandardList_->missions_.size(); EXPECT_EQ(0, result); missionListManager->defaultSingleList_ = missionList; - missionListManager->SignRestartAppFlag(uid); + missionListManager->SignRestartAppFlag(uid, ""); result = missionListManager->defaultSingleList_->missions_.size(); EXPECT_EQ(0, result); } diff --git a/test/unittest/mission_list_test/mission_list_test.cpp b/test/unittest/mission_list_test/mission_list_test.cpp index 1e9e2ecd92..988fad8a12 100644 --- a/test/unittest/mission_list_test/mission_list_test.cpp +++ b/test/unittest/mission_list_test/mission_list_test.cpp @@ -1096,24 +1096,24 @@ HWTEST_F(MissionListTest, SignRestartAppFlag_0100, TestSize.Level1) missionList->missions_.push_back(nullptr); std::string bundleName("testbundlename"); int32_t userId = 100; - missionList->SignRestartAppFlag(userId); + missionList->SignRestartAppFlag(userId, ""); EXPECT_EQ(*missionList->missions_.begin(), nullptr); missionList->missions_.clear(); missionList->missions_.push_back(mission); - missionList->SignRestartAppFlag(userId); + missionList->SignRestartAppFlag(userId, ""); EXPECT_NE(*missionList->missions_.begin(), nullptr); EXPECT_EQ((*missionList->missions_.begin())->GetAbilityRecord(), nullptr); missionList->missions_.clear(); mission = std::make_shared(1, abilityRecord, "bundle"); missionList->missions_.push_back(mission); - missionList->SignRestartAppFlag(userId); + missionList->SignRestartAppFlag(userId, ""); EXPECT_NE(*missionList->missions_.begin(), nullptr); auto ai = (*missionList->missions_.begin())->GetAbilityRecord(); EXPECT_NE(ai->GetApplicationInfo().bundleName, bundleName); - missionList->SignRestartAppFlag(0); + missionList->SignRestartAppFlag(0, ""); EXPECT_EQ(*missionList->missions_.begin(), nullptr); } } // namespace AAFwk diff --git a/test/unittest/restart_app_manager_test/restart_app_manager_test.cpp b/test/unittest/restart_app_manager_test/restart_app_manager_test.cpp index d0563a701e..b6637b00d9 100644 --- a/test/unittest/restart_app_manager_test/restart_app_manager_test.cpp +++ b/test/unittest/restart_app_manager_test/restart_app_manager_test.cpp @@ -50,9 +50,10 @@ void RestartAppManagerTest::SetUp() HWTEST_F(RestartAppManagerTest, IsRestartAppFrequent_001, TestSize.Level1) { RestartAppManager &instance = RestartAppManager::GetInstance(); + RestartAppKeyType key("", 123); time_t time = 0; - instance.restartAppHistory_[1] = time; - auto res = instance.IsRestartAppFrequent(1, time); + instance.restartAppHistory_[key] = time; + auto res = instance.IsRestartAppFrequent(key, time); EXPECT_EQ(res, true); } @@ -65,21 +66,9 @@ HWTEST_F(RestartAppManagerTest, IsRestartAppFrequent_001, TestSize.Level1) HWTEST_F(RestartAppManagerTest, IsRestartAppFrequent_002, TestSize.Level1) { RestartAppManager &instance = RestartAppManager::GetInstance(); + RestartAppKeyType key("", 123); time_t time = 20; - auto res = instance.IsRestartAppFrequent(1, time); - EXPECT_EQ(res, false); -} - -/** - * @tc.number: IsForegroundToRestartApp_001 - * @tc.name: IsForegroundToRestartApp - * @tc.desc: Test whether IsForegroundToRestartApp is called normally. - * @tc.type: FUNC - */ -HWTEST_F(RestartAppManagerTest, IsForegroundToRestartApp_001, TestSize.Level1) -{ - RestartAppManager &instance = RestartAppManager::GetInstance(); - auto res = instance.IsForegroundToRestartApp(); + auto res = instance.IsRestartAppFrequent(key, time); EXPECT_EQ(res, false); } } // namespace AAFwk