1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "ability_connect_manager.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include <regex> 19eace7efcSopenharmony_ci 20eace7efcSopenharmony_ci#include "ability_manager_service.h" 21eace7efcSopenharmony_ci#include "ability_permission_util.h" 22eace7efcSopenharmony_ci#include "ability_resident_process_rdb.h" 23eace7efcSopenharmony_ci#include "appfreeze_manager.h" 24eace7efcSopenharmony_ci#include "app_exit_reason_data_manager.h" 25eace7efcSopenharmony_ci#include "assert_fault_callback_death_mgr.h" 26eace7efcSopenharmony_ci#include "global_constant.h" 27eace7efcSopenharmony_ci#include "hitrace_meter.h" 28eace7efcSopenharmony_ci#include "int_wrapper.h" 29eace7efcSopenharmony_ci#include "multi_instance_utils.h" 30eace7efcSopenharmony_ci#include "res_sched_util.h" 31eace7efcSopenharmony_ci#include "session/host/include/zidl/session_interface.h" 32eace7efcSopenharmony_ci#include "startup_util.h" 33eace7efcSopenharmony_ci#include "ui_extension_utils.h" 34eace7efcSopenharmony_ci#include "ui_service_extension_connection_constants.h" 35eace7efcSopenharmony_ci#include "cache_extension_utils.h" 36eace7efcSopenharmony_ci#include "datetime_ex.h" 37eace7efcSopenharmony_ci#include "init_reboot.h" 38eace7efcSopenharmony_ci 39eace7efcSopenharmony_cinamespace OHOS { 40eace7efcSopenharmony_cinamespace AAFwk { 41eace7efcSopenharmony_cinamespace { 42eace7efcSopenharmony_ciconstexpr char EVENT_KEY_UID[] = "UID"; 43eace7efcSopenharmony_ciconstexpr char EVENT_KEY_PID[] = "PID"; 44eace7efcSopenharmony_ciconstexpr char EVENT_KEY_MESSAGE[] = "MSG"; 45eace7efcSopenharmony_ciconstexpr char EVENT_KEY_PACKAGE_NAME[] = "PACKAGE_NAME"; 46eace7efcSopenharmony_ciconstexpr char EVENT_KEY_PROCESS_NAME[] = "PROCESS_NAME"; 47eace7efcSopenharmony_ciconst std::string DEBUG_APP = "debugApp"; 48eace7efcSopenharmony_ciconst std::string FRS_APP_INDEX = "ohos.extra.param.key.frs_index"; 49eace7efcSopenharmony_ciconst std::string FRS_BUNDLE_NAME = "com.ohos.formrenderservice"; 50eace7efcSopenharmony_ciconst std::string UIEXTENSION_ABILITY_ID = "ability.want.params.uiExtensionAbilityId"; 51eace7efcSopenharmony_ciconst std::string UIEXTENSION_ROOT_HOST_PID = "ability.want.params.uiExtensionRootHostPid"; 52eace7efcSopenharmony_ciconst std::string MAX_UINT64_VALUE = "18446744073709551615"; 53eace7efcSopenharmony_ciconst std::string IS_PRELOAD_UIEXTENSION_ABILITY = "ability.want.params.is_preload_uiextension_ability"; 54eace7efcSopenharmony_ciconst std::string SEPARATOR = ":"; 55eace7efcSopenharmony_ci#ifdef SUPPORT_ASAN 56eace7efcSopenharmony_ciconst int LOAD_TIMEOUT_MULTIPLE = 150; 57eace7efcSopenharmony_ciconst int CONNECT_TIMEOUT_MULTIPLE = 45; 58eace7efcSopenharmony_ciconst int COMMAND_TIMEOUT_MULTIPLE = 75; 59eace7efcSopenharmony_ciconst int COMMAND_WINDOW_TIMEOUT_MULTIPLE = 75; 60eace7efcSopenharmony_ci#else 61eace7efcSopenharmony_ciconst int LOAD_TIMEOUT_MULTIPLE = 10; 62eace7efcSopenharmony_ciconst int CONNECT_TIMEOUT_MULTIPLE = 10; 63eace7efcSopenharmony_ciconst int COMMAND_TIMEOUT_MULTIPLE = 5; 64eace7efcSopenharmony_ciconst int COMMAND_WINDOW_TIMEOUT_MULTIPLE = 5; 65eace7efcSopenharmony_ci#endif 66eace7efcSopenharmony_ciconst int32_t AUTO_DISCONNECT_INFINITY = -1; 67eace7efcSopenharmony_ciconstexpr const char* FROZEN_WHITE_DIALOG = "com.huawei.hmos.huaweicast"; 68eace7efcSopenharmony_ciconstexpr char BUNDLE_NAME_DIALOG[] = "com.ohos.amsdialog"; 69eace7efcSopenharmony_ciconstexpr char ABILITY_NAME_ASSERT_FAULT_DIALOG[] = "AssertFaultDialog"; 70eace7efcSopenharmony_ciconstexpr const char* WANT_PARAMS_APP_RESTART_FLAG = "ohos.aafwk.app.restart"; 71eace7efcSopenharmony_ci 72eace7efcSopenharmony_ciconst std::string XIAOYI_BUNDLE_NAME = "com.huawei.hmos.vassistant"; 73eace7efcSopenharmony_ci 74eace7efcSopenharmony_cibool IsSpecialAbility(const AppExecFwk::AbilityInfo &abilityInfo) 75eace7efcSopenharmony_ci{ 76eace7efcSopenharmony_ci std::vector<std::pair<std::string, std::string>> trustAbilities{ 77eace7efcSopenharmony_ci { AbilityConfig::SCENEBOARD_BUNDLE_NAME, AbilityConfig::SCENEBOARD_ABILITY_NAME }, 78eace7efcSopenharmony_ci { AbilityConfig::SYSTEM_UI_BUNDLE_NAME, AbilityConfig::SYSTEM_UI_ABILITY_NAME }, 79eace7efcSopenharmony_ci { AbilityConfig::LAUNCHER_BUNDLE_NAME, AbilityConfig::LAUNCHER_ABILITY_NAME } 80eace7efcSopenharmony_ci }; 81eace7efcSopenharmony_ci for (const auto &pair : trustAbilities) { 82eace7efcSopenharmony_ci if (pair.first == abilityInfo.bundleName && pair.second == abilityInfo.name) { 83eace7efcSopenharmony_ci return true; 84eace7efcSopenharmony_ci } 85eace7efcSopenharmony_ci } 86eace7efcSopenharmony_ci return false; 87eace7efcSopenharmony_ci} 88eace7efcSopenharmony_ci} 89eace7efcSopenharmony_ci 90eace7efcSopenharmony_ciAbilityConnectManager::AbilityConnectManager(int userId) : userId_(userId) 91eace7efcSopenharmony_ci{ 92eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_ = std::make_unique<AbilityRuntime::ExtensionRecordManager>(userId); 93eace7efcSopenharmony_ci} 94eace7efcSopenharmony_ci 95eace7efcSopenharmony_ciAbilityConnectManager::~AbilityConnectManager() 96eace7efcSopenharmony_ci{} 97eace7efcSopenharmony_ci 98eace7efcSopenharmony_ciint AbilityConnectManager::StartAbility(const AbilityRequest &abilityRequest) 99eace7efcSopenharmony_ci{ 100eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 101eace7efcSopenharmony_ci return StartAbilityLocked(abilityRequest); 102eace7efcSopenharmony_ci} 103eace7efcSopenharmony_ci 104eace7efcSopenharmony_ciint AbilityConnectManager::TerminateAbility(const sptr<IRemoteObject> &token) 105eace7efcSopenharmony_ci{ 106eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 107eace7efcSopenharmony_ci return TerminateAbilityInner(token); 108eace7efcSopenharmony_ci} 109eace7efcSopenharmony_ci 110eace7efcSopenharmony_ciint AbilityConnectManager::TerminateAbilityInner(const sptr<IRemoteObject> &token) 111eace7efcSopenharmony_ci{ 112eace7efcSopenharmony_ci auto abilityRecord = GetExtensionByTokenFromServiceMap(token); 113eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 114eace7efcSopenharmony_ci abilityRecord = GetExtensionByTokenFromAbilityCache(token); 115eace7efcSopenharmony_ci } 116eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 117eace7efcSopenharmony_ci std::string element = abilityRecord->GetURI(); 118eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Terminate ability, ability is %{public}s.", element.c_str()); 119eace7efcSopenharmony_ci if (IsUIExtensionAbility(abilityRecord)) { 120eace7efcSopenharmony_ci if (!abilityRecord->IsConnectListEmpty()) { 121eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "exist connection, don't terminate"); 122eace7efcSopenharmony_ci return ERR_OK; 123eace7efcSopenharmony_ci } else if (abilityRecord->IsAbilityState(AbilityState::FOREGROUND) || 124eace7efcSopenharmony_ci abilityRecord->IsAbilityState(AbilityState::FOREGROUNDING) || 125eace7efcSopenharmony_ci abilityRecord->IsAbilityState(AbilityState::BACKGROUNDING)) { 126eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "current ability is active"); 127eace7efcSopenharmony_ci DoBackgroundAbilityWindow(abilityRecord, abilityRecord->GetSessionInfo()); 128eace7efcSopenharmony_ci MoveToTerminatingMap(abilityRecord); 129eace7efcSopenharmony_ci return ERR_OK; 130eace7efcSopenharmony_ci } 131eace7efcSopenharmony_ci } 132eace7efcSopenharmony_ci MoveToTerminatingMap(abilityRecord); 133eace7efcSopenharmony_ci return TerminateAbilityLocked(token); 134eace7efcSopenharmony_ci} 135eace7efcSopenharmony_ci 136eace7efcSopenharmony_ciint AbilityConnectManager::StopServiceAbility(const AbilityRequest &abilityRequest) 137eace7efcSopenharmony_ci{ 138eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "call"); 139eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 140eace7efcSopenharmony_ci return StopServiceAbilityLocked(abilityRequest); 141eace7efcSopenharmony_ci} 142eace7efcSopenharmony_ci 143eace7efcSopenharmony_ciint AbilityConnectManager::StartAbilityLocked(const AbilityRequest &abilityRequest) 144eace7efcSopenharmony_ci{ 145eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 146eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "ability_name:%{public}s", abilityRequest.want.GetElement().GetURI().c_str()); 147eace7efcSopenharmony_ci 148eace7efcSopenharmony_ci int32_t ret = AbilityPermissionUtil::GetInstance().CheckMultiInstanceKeyForExtension(abilityRequest); 149eace7efcSopenharmony_ci if (ret != ERR_OK) { 150eace7efcSopenharmony_ci // Do not distinguishing specific error codes 151eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 152eace7efcSopenharmony_ci } 153eace7efcSopenharmony_ci 154eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> targetService; 155eace7efcSopenharmony_ci bool isLoadedAbility = false; 156eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRequest.abilityInfo.extensionAbilityType)) { 157eace7efcSopenharmony_ci auto callerAbilityRecord = AAFwk::Token::GetAbilityRecordByToken(abilityRequest.callerToken); 158eace7efcSopenharmony_ci if (callerAbilityRecord == nullptr) { 159eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "null callerAbilityRecord"); 160eace7efcSopenharmony_ci return ERR_NULL_OBJECT; 161eace7efcSopenharmony_ci } 162eace7efcSopenharmony_ci std::string hostBundleName = callerAbilityRecord->GetAbilityInfo().bundleName; 163eace7efcSopenharmony_ci ret = GetOrCreateExtensionRecord(abilityRequest, false, hostBundleName, targetService, isLoadedAbility); 164eace7efcSopenharmony_ci if (ret != ERR_OK) { 165eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "fail, ret: %{public}d", ret); 166eace7efcSopenharmony_ci return ret; 167eace7efcSopenharmony_ci } 168eace7efcSopenharmony_ci } else { 169eace7efcSopenharmony_ci GetOrCreateServiceRecord(abilityRequest, false, targetService, isLoadedAbility); 170eace7efcSopenharmony_ci } 171eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(targetService, ERR_INVALID_VALUE); 172eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "StartAbility:%{public}s", targetService->GetURI().c_str()); 173eace7efcSopenharmony_ci 174eace7efcSopenharmony_ci targetService->AddCallerRecord(abilityRequest.callerToken, abilityRequest.requestCode, abilityRequest.want); 175eace7efcSopenharmony_ci 176eace7efcSopenharmony_ci targetService->SetLaunchReason(LaunchReason::LAUNCHREASON_START_EXTENSION); 177eace7efcSopenharmony_ci 178eace7efcSopenharmony_ci targetService->DoBackgroundAbilityWindowDelayed(false); 179eace7efcSopenharmony_ci 180eace7efcSopenharmony_ci targetService->SetSessionInfo(abilityRequest.sessionInfo); 181eace7efcSopenharmony_ci 182eace7efcSopenharmony_ci if (IsUIExtensionAbility(targetService) && abilityRequest.sessionInfo && abilityRequest.sessionInfo->sessionToken) { 183eace7efcSopenharmony_ci auto &remoteObj = abilityRequest.sessionInfo->sessionToken; 184eace7efcSopenharmony_ci { 185eace7efcSopenharmony_ci std::lock_guard guard(uiExtensionMapMutex_); 186eace7efcSopenharmony_ci uiExtensionMap_[remoteObj] = UIExtWindowMapValType(targetService, abilityRequest.sessionInfo); 187eace7efcSopenharmony_ci } 188eace7efcSopenharmony_ci AddUIExtWindowDeathRecipient(remoteObj); 189eace7efcSopenharmony_ci } 190eace7efcSopenharmony_ci 191eace7efcSopenharmony_ci ret = ReportXiaoYiToRSSIfNeeded(abilityRequest.abilityInfo); 192eace7efcSopenharmony_ci if (ret != ERR_OK) { 193eace7efcSopenharmony_ci return ret; 194eace7efcSopenharmony_ci } 195eace7efcSopenharmony_ci 196eace7efcSopenharmony_ci if (!isLoadedAbility) { 197eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Target service has not been loaded."); 198eace7efcSopenharmony_ci SetLastExitReason(abilityRequest, targetService); 199eace7efcSopenharmony_ci if (IsUIExtensionAbility(targetService)) { 200eace7efcSopenharmony_ci targetService->SetLaunchReason(LaunchReason::LAUNCHREASON_START_ABILITY); 201eace7efcSopenharmony_ci } 202eace7efcSopenharmony_ci targetService->GrantUriPermissionForServiceExtension(); 203eace7efcSopenharmony_ci LoadAbility(targetService); 204eace7efcSopenharmony_ci } else if (targetService->IsAbilityState(AbilityState::ACTIVE) && !IsUIExtensionAbility(targetService)) { 205eace7efcSopenharmony_ci // It may have been started through connect 206eace7efcSopenharmony_ci targetService->SetWant(abilityRequest.want); 207eace7efcSopenharmony_ci targetService->GrantUriPermissionForServiceExtension(); 208eace7efcSopenharmony_ci CommandAbility(targetService); 209eace7efcSopenharmony_ci } else if (IsUIExtensionAbility(targetService)) { 210eace7efcSopenharmony_ci DoForegroundUIExtension(targetService, abilityRequest); 211eace7efcSopenharmony_ci } else { 212eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "targetService activating"); 213eace7efcSopenharmony_ci EnqueueStartServiceReq(abilityRequest); 214eace7efcSopenharmony_ci return ERR_OK; 215eace7efcSopenharmony_ci } 216eace7efcSopenharmony_ci return ERR_OK; 217eace7efcSopenharmony_ci} 218eace7efcSopenharmony_ci 219eace7efcSopenharmony_civoid AbilityConnectManager::SetLastExitReason( 220eace7efcSopenharmony_ci const AbilityRequest &abilityRequest, std::shared_ptr<AbilityRecord> &targetRecord) 221eace7efcSopenharmony_ci{ 222eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); 223eace7efcSopenharmony_ci if (targetRecord == nullptr || !UIExtensionUtils::IsUIExtension(abilityRequest.abilityInfo.extensionAbilityType)) { 224eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Failed to set UIExtensionAbility last exit reason."); 225eace7efcSopenharmony_ci return; 226eace7efcSopenharmony_ci } 227eace7efcSopenharmony_ci auto appExitReasonDataMgr = DelayedSingleton<AbilityRuntime::AppExitReasonDataManager>::GetInstance(); 228eace7efcSopenharmony_ci if (appExitReasonDataMgr == nullptr) { 229eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "null appExitReasonDataMgr"); 230eace7efcSopenharmony_ci return; 231eace7efcSopenharmony_ci } 232eace7efcSopenharmony_ci 233eace7efcSopenharmony_ci ExitReason exitReason = { REASON_UNKNOWN, "" }; 234eace7efcSopenharmony_ci const std::string keyEx = targetRecord->GetAbilityInfo().bundleName + SEPARATOR + 235eace7efcSopenharmony_ci targetRecord->GetAbilityInfo().moduleName + SEPARATOR + 236eace7efcSopenharmony_ci targetRecord->GetAbilityInfo().name; 237eace7efcSopenharmony_ci if (!appExitReasonDataMgr->GetUIExtensionAbilityExitReason(keyEx, exitReason)) { 238eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "There is no record of UIExtensionAbility's last exit reason in the database."); 239eace7efcSopenharmony_ci return; 240eace7efcSopenharmony_ci } 241eace7efcSopenharmony_ci targetRecord->SetLastExitReason(exitReason); 242eace7efcSopenharmony_ci} 243eace7efcSopenharmony_ci 244eace7efcSopenharmony_civoid AbilityConnectManager::DoForegroundUIExtension(std::shared_ptr<AbilityRecord> abilityRecord, 245eace7efcSopenharmony_ci const AbilityRequest &abilityRequest) 246eace7efcSopenharmony_ci{ 247eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 248eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 249eace7efcSopenharmony_ci CHECK_POINTER(abilityRequest.sessionInfo); 250eace7efcSopenharmony_ci auto abilitystateStr = abilityRecord->ConvertAbilityState(abilityRecord->GetAbilityState()); 251eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, 252eace7efcSopenharmony_ci "foreground ability: %{public}s, persistentId: %{public}d, abilityState: %{public}s", 253eace7efcSopenharmony_ci abilityRecord->GetURI().c_str(), abilityRequest.sessionInfo->persistentId, abilitystateStr.c_str()); 254eace7efcSopenharmony_ci if (abilityRecord->IsReady() && !abilityRecord->IsAbilityState(AbilityState::INACTIVATING) && 255eace7efcSopenharmony_ci !abilityRecord->IsAbilityState(AbilityState::FOREGROUNDING) && 256eace7efcSopenharmony_ci !abilityRecord->IsAbilityState(AbilityState::BACKGROUNDING) && 257eace7efcSopenharmony_ci abilityRecord->IsAbilityWindowReady()) { 258eace7efcSopenharmony_ci if (abilityRecord->IsAbilityState(AbilityState::FOREGROUND)) { 259eace7efcSopenharmony_ci abilityRecord->SetWant(abilityRequest.want); 260eace7efcSopenharmony_ci CommandAbilityWindow(abilityRecord, abilityRequest.sessionInfo, WIN_CMD_FOREGROUND); 261eace7efcSopenharmony_ci return; 262eace7efcSopenharmony_ci } else { 263eace7efcSopenharmony_ci abilityRecord->SetWant(abilityRequest.want); 264eace7efcSopenharmony_ci abilityRecord->PostUIExtensionAbilityTimeoutTask(AbilityManagerService::FOREGROUND_TIMEOUT_MSG); 265eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->MoveToForeground(abilityRecord->GetToken()); 266eace7efcSopenharmony_ci return; 267eace7efcSopenharmony_ci } 268eace7efcSopenharmony_ci } 269eace7efcSopenharmony_ci EnqueueStartServiceReq(abilityRequest, abilityRecord->GetURI()); 270eace7efcSopenharmony_ci} 271eace7efcSopenharmony_ci 272eace7efcSopenharmony_civoid AbilityConnectManager::EnqueueStartServiceReq(const AbilityRequest &abilityRequest, const std::string &serviceUri) 273eace7efcSopenharmony_ci{ 274eace7efcSopenharmony_ci std::lock_guard guard(startServiceReqListLock_); 275eace7efcSopenharmony_ci auto abilityUri = abilityRequest.want.GetElement().GetURI(); 276eace7efcSopenharmony_ci if (!serviceUri.empty()) { 277eace7efcSopenharmony_ci abilityUri = serviceUri; 278eace7efcSopenharmony_ci } 279eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "abilityUri: %{public}s", abilityUri.c_str()); 280eace7efcSopenharmony_ci auto reqListIt = startServiceReqList_.find(abilityUri); 281eace7efcSopenharmony_ci if (reqListIt != startServiceReqList_.end()) { 282eace7efcSopenharmony_ci reqListIt->second->push_back(abilityRequest); 283eace7efcSopenharmony_ci } else { 284eace7efcSopenharmony_ci auto reqList = std::make_shared<std::list<AbilityRequest>>(); 285eace7efcSopenharmony_ci reqList->push_back(abilityRequest); 286eace7efcSopenharmony_ci startServiceReqList_.emplace(abilityUri, reqList); 287eace7efcSopenharmony_ci 288eace7efcSopenharmony_ci CHECK_POINTER(taskHandler_); 289eace7efcSopenharmony_ci auto callback = [abilityUri, connectManager = shared_from_this()]() { 290eace7efcSopenharmony_ci std::lock_guard guard{connectManager->startServiceReqListLock_}; 291eace7efcSopenharmony_ci auto exist = connectManager->startServiceReqList_.erase(abilityUri); 292eace7efcSopenharmony_ci if (exist) { 293eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "Target service %{public}s start timeout", abilityUri.c_str()); 294eace7efcSopenharmony_ci } 295eace7efcSopenharmony_ci }; 296eace7efcSopenharmony_ci 297eace7efcSopenharmony_ci int connectTimeout = 298eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetAppStartTimeoutTime() * CONNECT_TIMEOUT_MULTIPLE; 299eace7efcSopenharmony_ci taskHandler_->SubmitTask(callback, std::string("start_service_timeout:") + abilityUri, 300eace7efcSopenharmony_ci connectTimeout); 301eace7efcSopenharmony_ci } 302eace7efcSopenharmony_ci} 303eace7efcSopenharmony_ci 304eace7efcSopenharmony_ciint AbilityConnectManager::TerminateAbilityLocked(const sptr<IRemoteObject> &token) 305eace7efcSopenharmony_ci{ 306eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 307eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); 308eace7efcSopenharmony_ci auto abilityRecord = GetExtensionByTokenFromTerminatingMap(token); 309eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 310eace7efcSopenharmony_ci 311eace7efcSopenharmony_ci if (abilityRecord->IsTerminating()) { 312eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Ability is on terminating."); 313eace7efcSopenharmony_ci return ERR_OK; 314eace7efcSopenharmony_ci } 315eace7efcSopenharmony_ci 316eace7efcSopenharmony_ci if (!abilityRecord->GetConnectRecordList().empty()) { 317eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "target service connected"); 318eace7efcSopenharmony_ci auto connectRecordList = abilityRecord->GetConnectRecordList(); 319eace7efcSopenharmony_ci HandleTerminateDisconnectTask(connectRecordList); 320eace7efcSopenharmony_ci } 321eace7efcSopenharmony_ci 322eace7efcSopenharmony_ci auto timeoutTask = [abilityRecord, connectManager = shared_from_this()]() { 323eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "disconnect timeout"); 324eace7efcSopenharmony_ci connectManager->HandleStopTimeoutTask(abilityRecord); 325eace7efcSopenharmony_ci }; 326eace7efcSopenharmony_ci abilityRecord->Terminate(timeoutTask); 327eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) { 328eace7efcSopenharmony_ci AddUIExtensionAbilityRecordToTerminatedList(abilityRecord); 329eace7efcSopenharmony_ci } else { 330eace7efcSopenharmony_ci RemoveUIExtensionAbilityRecord(abilityRecord); 331eace7efcSopenharmony_ci } 332eace7efcSopenharmony_ci 333eace7efcSopenharmony_ci return ERR_OK; 334eace7efcSopenharmony_ci} 335eace7efcSopenharmony_ci 336eace7efcSopenharmony_ciint AbilityConnectManager::StopServiceAbilityLocked(const AbilityRequest &abilityRequest) 337eace7efcSopenharmony_ci{ 338eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 339eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "call"); 340eace7efcSopenharmony_ci AppExecFwk::ElementName element(abilityRequest.abilityInfo.deviceId, GenerateBundleName(abilityRequest), 341eace7efcSopenharmony_ci abilityRequest.abilityInfo.name, abilityRequest.abilityInfo.moduleName); 342eace7efcSopenharmony_ci std::string serviceKey = element.GetURI(); 343eace7efcSopenharmony_ci if (FRS_BUNDLE_NAME == abilityRequest.abilityInfo.bundleName) { 344eace7efcSopenharmony_ci serviceKey = serviceKey + std::to_string(abilityRequest.want.GetIntParam(FRS_APP_INDEX, 0)); 345eace7efcSopenharmony_ci } 346eace7efcSopenharmony_ci auto abilityRecord = GetServiceRecordByElementName(serviceKey); 347eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 348eace7efcSopenharmony_ci abilityRecord = AbilityCacheManager::GetInstance().Get(abilityRequest); 349eace7efcSopenharmony_ci AddToServiceMap(serviceKey, abilityRecord); 350eace7efcSopenharmony_ci } 351eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 352eace7efcSopenharmony_ci 353eace7efcSopenharmony_ci if (abilityRecord->IsTerminating()) { 354eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "ability terminating"); 355eace7efcSopenharmony_ci return ERR_OK; 356eace7efcSopenharmony_ci } 357eace7efcSopenharmony_ci 358eace7efcSopenharmony_ci if (!abilityRecord->GetConnectRecordList().empty()) { 359eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "post disconnect task"); 360eace7efcSopenharmony_ci auto connectRecordList = abilityRecord->GetConnectRecordList(); 361eace7efcSopenharmony_ci HandleTerminateDisconnectTask(connectRecordList); 362eace7efcSopenharmony_ci } 363eace7efcSopenharmony_ci 364eace7efcSopenharmony_ci TerminateRecord(abilityRecord); 365eace7efcSopenharmony_ci EventInfo eventInfo = BuildEventInfo(abilityRecord); 366eace7efcSopenharmony_ci EventReport::SendStopServiceEvent(EventName::STOP_SERVICE, eventInfo); 367eace7efcSopenharmony_ci return ERR_OK; 368eace7efcSopenharmony_ci} 369eace7efcSopenharmony_ci 370eace7efcSopenharmony_ciint32_t AbilityConnectManager::GetOrCreateExtensionRecord(const AbilityRequest &abilityRequest, bool isCreatedByConnect, 371eace7efcSopenharmony_ci const std::string &hostBundleName, std::shared_ptr<AbilityRecord> &extensionRecord, bool &isLoaded) 372eace7efcSopenharmony_ci{ 373eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 374eace7efcSopenharmony_ci AppExecFwk::ElementName element(abilityRequest.abilityInfo.deviceId, abilityRequest.abilityInfo.bundleName, 375eace7efcSopenharmony_ci abilityRequest.abilityInfo.name, abilityRequest.abilityInfo.moduleName); 376eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, ERR_NULL_OBJECT); 377eace7efcSopenharmony_ci if (uiExtensionAbilityRecordMgr_->IsBelongToManager(abilityRequest.abilityInfo)) { 378eace7efcSopenharmony_ci int32_t ret = uiExtensionAbilityRecordMgr_->GetOrCreateExtensionRecord( 379eace7efcSopenharmony_ci abilityRequest, hostBundleName, extensionRecord, isLoaded); 380eace7efcSopenharmony_ci if (ret != ERR_OK) { 381eace7efcSopenharmony_ci return ret; 382eace7efcSopenharmony_ci } 383eace7efcSopenharmony_ci extensionRecord->SetCreateByConnectMode(isCreatedByConnect); 384eace7efcSopenharmony_ci std::string extensionRecordKey = element.GetURI() + std::to_string(extensionRecord->GetUIExtensionAbilityId()); 385eace7efcSopenharmony_ci extensionRecord->SetURI(extensionRecordKey); 386eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Service map add, hostBundleName:%{public}s, key: %{public}s", 387eace7efcSopenharmony_ci hostBundleName.c_str(), extensionRecordKey.c_str()); 388eace7efcSopenharmony_ci AddToServiceMap(extensionRecordKey, extensionRecord); 389eace7efcSopenharmony_ci if (IsAbilityNeedKeepAlive(extensionRecord)) { 390eace7efcSopenharmony_ci extensionRecord->SetRestartTime(abilityRequest.restartTime); 391eace7efcSopenharmony_ci extensionRecord->SetRestartCount(abilityRequest.restartCount); 392eace7efcSopenharmony_ci } 393eace7efcSopenharmony_ci return ERR_OK; 394eace7efcSopenharmony_ci } 395eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 396eace7efcSopenharmony_ci} 397eace7efcSopenharmony_ci 398eace7efcSopenharmony_civoid AbilityConnectManager::GetOrCreateServiceRecord(const AbilityRequest &abilityRequest, 399eace7efcSopenharmony_ci const bool isCreatedByConnect, std::shared_ptr<AbilityRecord> &targetService, bool &isLoadedAbility) 400eace7efcSopenharmony_ci{ 401eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 402eace7efcSopenharmony_ci // lifecycle is not complete when window extension is reused 403eace7efcSopenharmony_ci bool noReuse = UIExtensionUtils::IsWindowExtension(abilityRequest.abilityInfo.extensionAbilityType); 404eace7efcSopenharmony_ci AppExecFwk::ElementName element(abilityRequest.abilityInfo.deviceId, GenerateBundleName(abilityRequest), 405eace7efcSopenharmony_ci abilityRequest.abilityInfo.name, abilityRequest.abilityInfo.moduleName); 406eace7efcSopenharmony_ci std::string serviceKey = element.GetURI(); 407eace7efcSopenharmony_ci if (FRS_BUNDLE_NAME == abilityRequest.abilityInfo.bundleName) { 408eace7efcSopenharmony_ci serviceKey = element.GetURI() + std::to_string(abilityRequest.want.GetIntParam(FRS_APP_INDEX, 0)); 409eace7efcSopenharmony_ci } 410eace7efcSopenharmony_ci { 411eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 412eace7efcSopenharmony_ci auto serviceMapIter = serviceMap_.find(serviceKey); 413eace7efcSopenharmony_ci targetService = serviceMapIter == serviceMap_.end() ? nullptr : serviceMapIter->second; 414eace7efcSopenharmony_ci } 415eace7efcSopenharmony_ci if (targetService == nullptr && 416eace7efcSopenharmony_ci CacheExtensionUtils::IsCacheExtensionType(abilityRequest.abilityInfo.extensionAbilityType)) { 417eace7efcSopenharmony_ci targetService = AbilityCacheManager::GetInstance().Get(abilityRequest); 418eace7efcSopenharmony_ci if (targetService != nullptr) { 419eace7efcSopenharmony_ci AddToServiceMap(serviceKey, targetService); 420eace7efcSopenharmony_ci } 421eace7efcSopenharmony_ci } 422eace7efcSopenharmony_ci if (noReuse && targetService) { 423eace7efcSopenharmony_ci if (IsSpecialAbility(abilityRequest.abilityInfo)) { 424eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "removing ability: %{public}s", element.GetURI().c_str()); 425eace7efcSopenharmony_ci } 426eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 427eace7efcSopenharmony_ci serviceMap_.erase(serviceKey); 428eace7efcSopenharmony_ci } 429eace7efcSopenharmony_ci isLoadedAbility = true; 430eace7efcSopenharmony_ci if (noReuse || targetService == nullptr) { 431eace7efcSopenharmony_ci targetService = AbilityRecord::CreateAbilityRecord(abilityRequest); 432eace7efcSopenharmony_ci CHECK_POINTER(targetService); 433eace7efcSopenharmony_ci targetService->SetOwnerMissionUserId(userId_); 434eace7efcSopenharmony_ci if (isCreatedByConnect) { 435eace7efcSopenharmony_ci targetService->SetCreateByConnectMode(); 436eace7efcSopenharmony_ci } 437eace7efcSopenharmony_ci if (abilityRequest.abilityInfo.name == AbilityConfig::LAUNCHER_ABILITY_NAME) { 438eace7efcSopenharmony_ci targetService->SetLauncherRoot(); 439eace7efcSopenharmony_ci targetService->SetRestartTime(abilityRequest.restartTime); 440eace7efcSopenharmony_ci targetService->SetRestartCount(abilityRequest.restartCount); 441eace7efcSopenharmony_ci } else if (IsAbilityNeedKeepAlive(targetService)) { 442eace7efcSopenharmony_ci targetService->SetRestartTime(abilityRequest.restartTime); 443eace7efcSopenharmony_ci targetService->SetRestartCount(abilityRequest.restartCount); 444eace7efcSopenharmony_ci } 445eace7efcSopenharmony_ci if (MultiInstanceUtils::IsMultiInstanceApp(abilityRequest.appInfo)) { 446eace7efcSopenharmony_ci targetService->SetInstanceKey(MultiInstanceUtils::GetValidExtensionInstanceKey(abilityRequest)); 447eace7efcSopenharmony_ci } 448eace7efcSopenharmony_ci AddToServiceMap(serviceKey, targetService); 449eace7efcSopenharmony_ci isLoadedAbility = false; 450eace7efcSopenharmony_ci } 451eace7efcSopenharmony_ci} 452eace7efcSopenharmony_ci 453eace7efcSopenharmony_civoid AbilityConnectManager::GetConnectRecordListFromMap( 454eace7efcSopenharmony_ci const sptr<IAbilityConnection> &connect, std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList) 455eace7efcSopenharmony_ci{ 456eace7efcSopenharmony_ci std::lock_guard lock(connectMapMutex_); 457eace7efcSopenharmony_ci auto connectMapIter = connectMap_.find(connect->AsObject()); 458eace7efcSopenharmony_ci if (connectMapIter != connectMap_.end()) { 459eace7efcSopenharmony_ci connectRecordList = connectMapIter->second; 460eace7efcSopenharmony_ci } 461eace7efcSopenharmony_ci} 462eace7efcSopenharmony_ci 463eace7efcSopenharmony_ciint32_t AbilityConnectManager::GetOrCreateTargetServiceRecord( 464eace7efcSopenharmony_ci const AbilityRequest &abilityRequest, const sptr<UIExtensionAbilityConnectInfo> &connectInfo, 465eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> &targetService, bool &isLoadedAbility) 466eace7efcSopenharmony_ci{ 467eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRequest.abilityInfo.extensionAbilityType) && connectInfo != nullptr) { 468eace7efcSopenharmony_ci int32_t ret = GetOrCreateExtensionRecord( 469eace7efcSopenharmony_ci abilityRequest, true, connectInfo->hostBundleName, targetService, isLoadedAbility); 470eace7efcSopenharmony_ci if (ret != ERR_OK || targetService == nullptr) { 471eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "GetOrCreateExtensionRecord fail"); 472eace7efcSopenharmony_ci return ERR_NULL_OBJECT; 473eace7efcSopenharmony_ci } 474eace7efcSopenharmony_ci connectInfo->uiExtensionAbilityId = targetService->GetUIExtensionAbilityId(); 475eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "UIExtensionAbility id %{public}d.", connectInfo->uiExtensionAbilityId); 476eace7efcSopenharmony_ci } else { 477eace7efcSopenharmony_ci GetOrCreateServiceRecord(abilityRequest, true, targetService, isLoadedAbility); 478eace7efcSopenharmony_ci } 479eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(targetService, ERR_INVALID_VALUE); 480eace7efcSopenharmony_ci return ERR_OK; 481eace7efcSopenharmony_ci} 482eace7efcSopenharmony_ci 483eace7efcSopenharmony_ciint AbilityConnectManager::PreloadUIExtensionAbilityLocked(const AbilityRequest &abilityRequest, 484eace7efcSopenharmony_ci std::string &hostBundleName) 485eace7efcSopenharmony_ci{ 486eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 487eace7efcSopenharmony_ci return PreloadUIExtensionAbilityInner(abilityRequest, hostBundleName); 488eace7efcSopenharmony_ci} 489eace7efcSopenharmony_ci 490eace7efcSopenharmony_ciint AbilityConnectManager::PreloadUIExtensionAbilityInner(const AbilityRequest &abilityRequest, 491eace7efcSopenharmony_ci std::string &hostBundleName) 492eace7efcSopenharmony_ci{ 493eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "call"); 494eace7efcSopenharmony_ci if (!UIExtensionUtils::IsUIExtension(abilityRequest.abilityInfo.extensionAbilityType)) { 495eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "can't preload non-uiextension type"); 496eace7efcSopenharmony_ci return ERR_WRONG_INTERFACE_CALL; 497eace7efcSopenharmony_ci } 498eace7efcSopenharmony_ci int32_t ret = AbilityPermissionUtil::GetInstance().CheckMultiInstanceKeyForExtension(abilityRequest); 499eace7efcSopenharmony_ci if (ret != ERR_OK) { 500eace7efcSopenharmony_ci // Do not distinguishing specific error codes 501eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 502eace7efcSopenharmony_ci } 503eace7efcSopenharmony_ci std::shared_ptr<ExtensionRecord> extensionRecord = nullptr; 504eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, ERR_NULL_OBJECT); 505eace7efcSopenharmony_ci int32_t extensionRecordId = INVALID_EXTENSION_RECORD_ID; 506eace7efcSopenharmony_ci ret = uiExtensionAbilityRecordMgr_->CreateExtensionRecord(abilityRequest, hostBundleName, 507eace7efcSopenharmony_ci extensionRecord, extensionRecordId); 508eace7efcSopenharmony_ci if (ret != ERR_OK) { 509eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "CreateExtensionRecord ERR"); 510eace7efcSopenharmony_ci return ret; 511eace7efcSopenharmony_ci } 512eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(extensionRecord, ERR_NULL_OBJECT); 513eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> targetService = extensionRecord->abilityRecord_; 514eace7efcSopenharmony_ci AppExecFwk::ElementName element(abilityRequest.abilityInfo.deviceId, abilityRequest.abilityInfo.bundleName, 515eace7efcSopenharmony_ci abilityRequest.abilityInfo.name, abilityRequest.abilityInfo.moduleName); 516eace7efcSopenharmony_ci std::string extensionRecordKey = element.GetURI() + std::to_string(targetService->GetUIExtensionAbilityId()); 517eace7efcSopenharmony_ci targetService->SetURI(extensionRecordKey); 518eace7efcSopenharmony_ci AddToServiceMap(extensionRecordKey, targetService); 519eace7efcSopenharmony_ci LoadAbility(targetService); 520eace7efcSopenharmony_ci return ERR_OK; 521eace7efcSopenharmony_ci} 522eace7efcSopenharmony_ci 523eace7efcSopenharmony_ciint AbilityConnectManager::UnloadUIExtensionAbility(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord, 524eace7efcSopenharmony_ci std::string &hostBundleName) 525eace7efcSopenharmony_ci{ 526eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "call"); 527eace7efcSopenharmony_ci //Get preLoadUIExtensionInfo 528eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 529eace7efcSopenharmony_ci auto preLoadUIExtensionInfo = std::make_tuple(abilityRecord->GetWant().GetElement().GetAbilityName(), 530eace7efcSopenharmony_ci abilityRecord->GetWant().GetElement().GetBundleName(), 531eace7efcSopenharmony_ci abilityRecord->GetWant().GetElement().GetModuleName(), hostBundleName); 532eace7efcSopenharmony_ci //delete preLoadUIExtensionMap 533eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, ERR_NULL_OBJECT); 534eace7efcSopenharmony_ci auto extensionRecordId = abilityRecord->GetUIExtensionAbilityId(); 535eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_->RemovePreloadUIExtensionRecordById(preLoadUIExtensionInfo, extensionRecordId); 536eace7efcSopenharmony_ci //terminate preload uiextension 537eace7efcSopenharmony_ci auto token = abilityRecord->GetToken(); 538eace7efcSopenharmony_ci auto result = TerminateAbilityInner(token); 539eace7efcSopenharmony_ci if (result != ERR_OK) { 540eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "terminate error"); 541eace7efcSopenharmony_ci return result; 542eace7efcSopenharmony_ci } 543eace7efcSopenharmony_ci return ERR_OK; 544eace7efcSopenharmony_ci} 545eace7efcSopenharmony_ci 546eace7efcSopenharmony_civoid AbilityConnectManager::ReportEventToRSS(const AppExecFwk::AbilityInfo &abilityInfo, 547eace7efcSopenharmony_ci const std::shared_ptr<AbilityRecord> abilityRecord, sptr<IRemoteObject> callerToken) 548eace7efcSopenharmony_ci{ 549eace7efcSopenharmony_ci if (taskHandler_ == nullptr) { 550eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "taskHandler_ null"); 551eace7efcSopenharmony_ci return; 552eace7efcSopenharmony_ci } 553eace7efcSopenharmony_ci 554eace7efcSopenharmony_ci std::string reason = ResSchedUtil::GetInstance().GetThawReasonByAbilityType(abilityInfo); 555eace7efcSopenharmony_ci const int32_t uid = abilityInfo.applicationInfo.uid; 556eace7efcSopenharmony_ci const std::string bundleName = abilityInfo.applicationInfo.bundleName; 557eace7efcSopenharmony_ci const int32_t pid = (abilityRecord->GetPid() > 0) ? abilityRecord->GetPid() : -1; 558eace7efcSopenharmony_ci auto callerAbility = Token::GetAbilityRecordByToken(callerToken); 559eace7efcSopenharmony_ci const int32_t callerPid = (callerAbility != nullptr) ? callerAbility->GetPid() : IPCSkeleton::GetCallingPid(); 560eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "%{public}d_%{public}s_%{public}d reason=%{public}s callerPid=%{public}d", uid, 561eace7efcSopenharmony_ci bundleName.c_str(), pid, reason.c_str(), callerPid); 562eace7efcSopenharmony_ci taskHandler_->SubmitTask([uid, bundleName, reason, pid, callerPid]() { 563eace7efcSopenharmony_ci ResSchedUtil::GetInstance().ReportEventToRSS(uid, bundleName, reason, pid, callerPid); 564eace7efcSopenharmony_ci }); 565eace7efcSopenharmony_ci} 566eace7efcSopenharmony_ci 567eace7efcSopenharmony_ciint AbilityConnectManager::ConnectAbilityLocked(const AbilityRequest &abilityRequest, 568eace7efcSopenharmony_ci const sptr<IAbilityConnection> &connect, const sptr<IRemoteObject> &callerToken, sptr<SessionInfo> sessionInfo, 569eace7efcSopenharmony_ci sptr<UIExtensionAbilityConnectInfo> connectInfo) 570eace7efcSopenharmony_ci{ 571eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 572eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(connect, ERR_INVALID_VALUE); 573eace7efcSopenharmony_ci auto connectObject = connect->AsObject(); 574eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 575eace7efcSopenharmony_ci 576eace7efcSopenharmony_ci // 1. get target service ability record, and check whether it has been loaded. 577eace7efcSopenharmony_ci int32_t ret = AbilityPermissionUtil::GetInstance().CheckMultiInstanceKeyForExtension(abilityRequest); 578eace7efcSopenharmony_ci if (ret != ERR_OK) { 579eace7efcSopenharmony_ci // Do not distinguishing specific error codes 580eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 581eace7efcSopenharmony_ci } 582eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> targetService; 583eace7efcSopenharmony_ci bool isLoadedAbility = false; 584eace7efcSopenharmony_ci ret = GetOrCreateTargetServiceRecord(abilityRequest, connectInfo, targetService, isLoadedAbility); 585eace7efcSopenharmony_ci if (ret != ERR_OK) { 586eace7efcSopenharmony_ci return ret; 587eace7efcSopenharmony_ci } 588eace7efcSopenharmony_ci if (abilityRequest.abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::REMOTE_NOTIFICATION) { 589eace7efcSopenharmony_ci ReportEventToRSS(abilityRequest.abilityInfo, targetService, callerToken); 590eace7efcSopenharmony_ci } 591eace7efcSopenharmony_ci // 2. get target connectRecordList, and check whether this callback has been connected. 592eace7efcSopenharmony_ci ConnectListType connectRecordList; 593eace7efcSopenharmony_ci GetConnectRecordListFromMap(connect, connectRecordList); 594eace7efcSopenharmony_ci bool isCallbackConnected = !connectRecordList.empty(); 595eace7efcSopenharmony_ci // 3. If this service ability and callback has been connected, There is no need to connect repeatedly 596eace7efcSopenharmony_ci if (isLoadedAbility && (isCallbackConnected) && IsAbilityConnected(targetService, connectRecordList)) { 597eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "Service/callback connected"); 598eace7efcSopenharmony_ci return ERR_OK; 599eace7efcSopenharmony_ci } 600eace7efcSopenharmony_ci 601eace7efcSopenharmony_ci // 4. Other cases , need to connect the service ability 602eace7efcSopenharmony_ci auto connectRecord = ConnectionRecord::CreateConnectionRecord(callerToken, targetService, connect); 603eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(connectRecord, ERR_INVALID_VALUE); 604eace7efcSopenharmony_ci connectRecord->AttachCallerInfo(); 605eace7efcSopenharmony_ci connectRecord->SetConnectState(ConnectionState::CONNECTING); 606eace7efcSopenharmony_ci if (targetService->GetAbilityInfo().extensionAbilityType == AppExecFwk::ExtensionAbilityType::UI_SERVICE) { 607eace7efcSopenharmony_ci connectRecord->SetConnectWant(abilityRequest.want); 608eace7efcSopenharmony_ci } 609eace7efcSopenharmony_ci targetService->AddConnectRecordToList(connectRecord); 610eace7efcSopenharmony_ci targetService->SetSessionInfo(sessionInfo); 611eace7efcSopenharmony_ci connectRecordList.push_back(connectRecord); 612eace7efcSopenharmony_ci AddConnectObjectToMap(connectObject, connectRecordList, isCallbackConnected); 613eace7efcSopenharmony_ci targetService->SetLaunchReason(LaunchReason::LAUNCHREASON_CONNECT_EXTENSION); 614eace7efcSopenharmony_ci 615eace7efcSopenharmony_ci if (UIExtensionUtils::IsWindowExtension(targetService->GetAbilityInfo().extensionAbilityType) 616eace7efcSopenharmony_ci && abilityRequest.sessionInfo) { 617eace7efcSopenharmony_ci std::lock_guard guard(windowExtensionMapMutex_); 618eace7efcSopenharmony_ci windowExtensionMap_.emplace(connectObject, 619eace7efcSopenharmony_ci WindowExtMapValType(targetService->GetApplicationInfo().accessTokenId, abilityRequest.sessionInfo)); 620eace7efcSopenharmony_ci } 621eace7efcSopenharmony_ci 622eace7efcSopenharmony_ci auto &abilityInfo = abilityRequest.abilityInfo; 623eace7efcSopenharmony_ci ret = ReportXiaoYiToRSSIfNeeded(abilityInfo); 624eace7efcSopenharmony_ci if (ret != ERR_OK) { 625eace7efcSopenharmony_ci return ret; 626eace7efcSopenharmony_ci } 627eace7efcSopenharmony_ci 628eace7efcSopenharmony_ci if (!isLoadedAbility) { 629eace7efcSopenharmony_ci LoadAbility(targetService); 630eace7efcSopenharmony_ci } else if (targetService->IsAbilityState(AbilityState::ACTIVE)) { 631eace7efcSopenharmony_ci targetService->SetWant(abilityRequest.want); 632eace7efcSopenharmony_ci HandleActiveAbility(targetService, connectRecord); 633eace7efcSopenharmony_ci } else { 634eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "TargetService activing"); 635eace7efcSopenharmony_ci targetService->SaveConnectWant(abilityRequest.want); 636eace7efcSopenharmony_ci } 637eace7efcSopenharmony_ci return ret; 638eace7efcSopenharmony_ci} 639eace7efcSopenharmony_ci 640eace7efcSopenharmony_civoid AbilityConnectManager::HandleActiveAbility(std::shared_ptr<AbilityRecord> &targetService, 641eace7efcSopenharmony_ci std::shared_ptr<ConnectionRecord> &connectRecord) 642eace7efcSopenharmony_ci{ 643eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s called.", __func__); 644eace7efcSopenharmony_ci if (targetService == nullptr) { 645eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "null targetService"); 646eace7efcSopenharmony_ci return; 647eace7efcSopenharmony_ci } 648eace7efcSopenharmony_ci AppExecFwk::ExtensionAbilityType extType = targetService->GetAbilityInfo().extensionAbilityType; 649eace7efcSopenharmony_ci bool isAbilityUIServiceExt = (extType == AppExecFwk::ExtensionAbilityType::UI_SERVICE); 650eace7efcSopenharmony_ci if (isAbilityUIServiceExt) { 651eace7efcSopenharmony_ci if (connectRecord != nullptr) { 652eace7efcSopenharmony_ci Want want = connectRecord->GetConnectWant(); 653eace7efcSopenharmony_ci int connectRecordId = connectRecord->GetRecordId(); 654eace7efcSopenharmony_ci ConnectUIServiceExtAbility(targetService, connectRecordId, want); 655eace7efcSopenharmony_ci } 656eace7efcSopenharmony_ci targetService->RemoveSignatureInfo(); 657eace7efcSopenharmony_ci return; 658eace7efcSopenharmony_ci } 659eace7efcSopenharmony_ci 660eace7efcSopenharmony_ci if (targetService->GetConnectRecordList().size() > 1) { 661eace7efcSopenharmony_ci targetService->RemoveSignatureInfo(); 662eace7efcSopenharmony_ci if (taskHandler_ != nullptr && targetService->GetConnRemoteObject()) { 663eace7efcSopenharmony_ci auto task = [connectRecord]() { connectRecord->CompleteConnect(ERR_OK); }; 664eace7efcSopenharmony_ci taskHandler_->SubmitTask(task, TaskQoS::USER_INTERACTIVE); 665eace7efcSopenharmony_ci } else { 666eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "connecting"); 667eace7efcSopenharmony_ci } 668eace7efcSopenharmony_ci } else { 669eace7efcSopenharmony_ci ConnectAbility(targetService); 670eace7efcSopenharmony_ci } 671eace7efcSopenharmony_ci} 672eace7efcSopenharmony_ci 673eace7efcSopenharmony_ciint AbilityConnectManager::DisconnectAbilityLocked(const sptr<IAbilityConnection> &connect) 674eace7efcSopenharmony_ci{ 675eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 676eace7efcSopenharmony_ci return DisconnectAbilityLocked(connect, false); 677eace7efcSopenharmony_ci} 678eace7efcSopenharmony_ci 679eace7efcSopenharmony_ciint AbilityConnectManager::DisconnectAbilityLocked(const sptr<IAbilityConnection> &connect, bool callerDied) 680eace7efcSopenharmony_ci{ 681eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 682eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "call"); 683eace7efcSopenharmony_ci 684eace7efcSopenharmony_ci // 1. check whether callback was connected. 685eace7efcSopenharmony_ci ConnectListType connectRecordList; 686eace7efcSopenharmony_ci GetConnectRecordListFromMap(connect, connectRecordList); 687eace7efcSopenharmony_ci if (connectRecordList.empty()) { 688eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "recordList empty"); 689eace7efcSopenharmony_ci return CONNECTION_NOT_EXIST; 690eace7efcSopenharmony_ci } 691eace7efcSopenharmony_ci 692eace7efcSopenharmony_ci // 2. schedule disconnect to target service 693eace7efcSopenharmony_ci int result = ERR_OK; 694eace7efcSopenharmony_ci ConnectListType list; 695eace7efcSopenharmony_ci for (auto &connectRecord : connectRecordList) { 696eace7efcSopenharmony_ci if (connectRecord) { 697eace7efcSopenharmony_ci auto abilityRecord = connectRecord->GetAbilityRecord(); 698eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 699eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "abilityName: %{public}s, bundleName: %{public}s", 700eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().name.c_str(), abilityRecord->GetAbilityInfo().bundleName.c_str()); 701eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().type == AbilityType::EXTENSION) { 702eace7efcSopenharmony_ci RemoveExtensionDelayDisconnectTask(connectRecord); 703eace7efcSopenharmony_ci } 704eace7efcSopenharmony_ci if (connectRecord->GetCallerTokenId() != IPCSkeleton::GetCallingTokenID() && 705eace7efcSopenharmony_ci static_cast<uint32_t>(IPCSkeleton::GetSelfTokenID() != IPCSkeleton::GetCallingTokenID())) { 706eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "inconsistent caller"); 707eace7efcSopenharmony_ci continue; 708eace7efcSopenharmony_ci } 709eace7efcSopenharmony_ci 710eace7efcSopenharmony_ci result = DisconnectRecordNormal(list, connectRecord, callerDied); 711eace7efcSopenharmony_ci if (result != ERR_OK && callerDied) { 712eace7efcSopenharmony_ci DisconnectRecordForce(list, connectRecord); 713eace7efcSopenharmony_ci result = ERR_OK; 714eace7efcSopenharmony_ci } 715eace7efcSopenharmony_ci 716eace7efcSopenharmony_ci if (result != ERR_OK) { 717eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "fail , ret = %{public}d", result); 718eace7efcSopenharmony_ci break; 719eace7efcSopenharmony_ci } else { 720eace7efcSopenharmony_ci EventInfo eventInfo = BuildEventInfo(abilityRecord); 721eace7efcSopenharmony_ci EventReport::SendDisconnectServiceEvent(EventName::DISCONNECT_SERVICE, eventInfo); 722eace7efcSopenharmony_ci } 723eace7efcSopenharmony_ci } 724eace7efcSopenharmony_ci } 725eace7efcSopenharmony_ci for (auto&& connectRecord : list) { 726eace7efcSopenharmony_ci RemoveConnectionRecordFromMap(connectRecord); 727eace7efcSopenharmony_ci } 728eace7efcSopenharmony_ci 729eace7efcSopenharmony_ci return result; 730eace7efcSopenharmony_ci} 731eace7efcSopenharmony_ci 732eace7efcSopenharmony_civoid AbilityConnectManager::TerminateRecord(std::shared_ptr<AbilityRecord> abilityRecord) 733eace7efcSopenharmony_ci{ 734eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "call"); 735eace7efcSopenharmony_ci if (!GetExtensionByIdFromServiceMap(abilityRecord->GetRecordId()) && 736eace7efcSopenharmony_ci !AbilityCacheManager::GetInstance().FindRecordByToken(abilityRecord->GetToken())) { 737eace7efcSopenharmony_ci return; 738eace7efcSopenharmony_ci } 739eace7efcSopenharmony_ci auto timeoutTask = [abilityRecord, connectManager = shared_from_this()]() { 740eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "disconnect timeout"); 741eace7efcSopenharmony_ci connectManager->HandleStopTimeoutTask(abilityRecord); 742eace7efcSopenharmony_ci }; 743eace7efcSopenharmony_ci 744eace7efcSopenharmony_ci MoveToTerminatingMap(abilityRecord); 745eace7efcSopenharmony_ci abilityRecord->Terminate(timeoutTask); 746eace7efcSopenharmony_ci} 747eace7efcSopenharmony_ci 748eace7efcSopenharmony_ciint AbilityConnectManager::DisconnectRecordNormal(ConnectListType &list, 749eace7efcSopenharmony_ci std::shared_ptr<ConnectionRecord> connectRecord, bool callerDied) const 750eace7efcSopenharmony_ci{ 751eace7efcSopenharmony_ci auto result = connectRecord->DisconnectAbility(); 752eace7efcSopenharmony_ci if (result != ERR_OK) { 753eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "fail:%{public}d", result); 754eace7efcSopenharmony_ci return result; 755eace7efcSopenharmony_ci } 756eace7efcSopenharmony_ci 757eace7efcSopenharmony_ci if (connectRecord->GetConnectState() == ConnectionState::DISCONNECTED) { 758eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "DisconnectRecordNormal disconnect record:%{public}d", 759eace7efcSopenharmony_ci connectRecord->GetRecordId()); 760eace7efcSopenharmony_ci connectRecord->CompleteDisconnect(ERR_OK, callerDied); 761eace7efcSopenharmony_ci list.emplace_back(connectRecord); 762eace7efcSopenharmony_ci } 763eace7efcSopenharmony_ci return ERR_OK; 764eace7efcSopenharmony_ci} 765eace7efcSopenharmony_ci 766eace7efcSopenharmony_civoid AbilityConnectManager::DisconnectRecordForce(ConnectListType &list, 767eace7efcSopenharmony_ci std::shared_ptr<ConnectionRecord> connectRecord) 768eace7efcSopenharmony_ci{ 769eace7efcSopenharmony_ci auto abilityRecord = connectRecord->GetAbilityRecord(); 770eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 771eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityRecord"); 772eace7efcSopenharmony_ci return; 773eace7efcSopenharmony_ci } 774eace7efcSopenharmony_ci abilityRecord->RemoveConnectRecordFromList(connectRecord); 775eace7efcSopenharmony_ci connectRecord->CompleteDisconnect(ERR_OK, true); 776eace7efcSopenharmony_ci list.emplace_back(connectRecord); 777eace7efcSopenharmony_ci bool isUIService = (abilityRecord->GetAbilityInfo().extensionAbilityType == 778eace7efcSopenharmony_ci AppExecFwk::ExtensionAbilityType::UI_SERVICE); 779eace7efcSopenharmony_ci if (abilityRecord->IsConnectListEmpty() && abilityRecord->GetStartId() == 0 && !isUIService) { 780eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "force terminate ability record state: %{public}d", 781eace7efcSopenharmony_ci abilityRecord->GetAbilityState()); 782eace7efcSopenharmony_ci TerminateRecord(abilityRecord); 783eace7efcSopenharmony_ci } 784eace7efcSopenharmony_ci} 785eace7efcSopenharmony_ci 786eace7efcSopenharmony_ciint AbilityConnectManager::AttachAbilityThreadLocked( 787eace7efcSopenharmony_ci const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token) 788eace7efcSopenharmony_ci{ 789eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 790eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 791eace7efcSopenharmony_ci auto abilityRecord = GetExtensionByTokenFromServiceMap(token); 792eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 793eace7efcSopenharmony_ci abilityRecord = GetExtensionByTokenFromTerminatingMap(token); 794eace7efcSopenharmony_ci if (abilityRecord != nullptr) { 795eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "Ability:%{public}s, user:%{public}d", 796eace7efcSopenharmony_ci abilityRecord->GetURI().c_str(), userId_); 797eace7efcSopenharmony_ci } 798eace7efcSopenharmony_ci auto tmpRecord = Token::GetAbilityRecordByToken(token); 799eace7efcSopenharmony_ci if (tmpRecord && tmpRecord != abilityRecord) { 800eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "Token:%{public}s, user:%{public}d", 801eace7efcSopenharmony_ci tmpRecord->GetURI().c_str(), userId_); 802eace7efcSopenharmony_ci } 803eace7efcSopenharmony_ci if (!IsUIExtensionAbility(abilityRecord)) { 804eace7efcSopenharmony_ci abilityRecord = nullptr; 805eace7efcSopenharmony_ci } 806eace7efcSopenharmony_ci } 807eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 808eace7efcSopenharmony_ci std::string element = abilityRecord->GetURI(); 809eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "ability:%{public}s", element.c_str()); 810eace7efcSopenharmony_ci if (taskHandler_ != nullptr) { 811eace7efcSopenharmony_ci int recordId = abilityRecord->GetRecordId(); 812eace7efcSopenharmony_ci std::string taskName = std::string("LoadTimeout_") + std::to_string(recordId); 813eace7efcSopenharmony_ci taskHandler_->CancelTask(taskName); 814eace7efcSopenharmony_ci } 815eace7efcSopenharmony_ci if (eventHandler_) { 816eace7efcSopenharmony_ci abilityRecord->RemoveLoadTimeoutTask(); 817eace7efcSopenharmony_ci } 818eace7efcSopenharmony_ci if (abilityRecord->IsSceneBoard()) { 819eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "attach Ability: %{public}s", element.c_str()); 820eace7efcSopenharmony_ci sceneBoardTokenId_ = abilityRecord->GetAbilityInfo().applicationInfo.accessTokenId; 821eace7efcSopenharmony_ci } 822eace7efcSopenharmony_ci abilityRecord->SetScheduler(scheduler); 823eace7efcSopenharmony_ci abilityRecord->RemoveSpecifiedWantParam(UIEXTENSION_ABILITY_ID); 824eace7efcSopenharmony_ci abilityRecord->RemoveSpecifiedWantParam(UIEXTENSION_ROOT_HOST_PID); 825eace7efcSopenharmony_ci if (IsUIExtensionAbility(abilityRecord) && !abilityRecord->IsCreateByConnect() 826eace7efcSopenharmony_ci && !abilityRecord->GetWant().GetBoolParam(IS_PRELOAD_UIEXTENSION_ABILITY, false)) { 827eace7efcSopenharmony_ci abilityRecord->PostUIExtensionAbilityTimeoutTask(AbilityManagerService::FOREGROUND_TIMEOUT_MSG); 828eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->MoveToForeground(token); 829eace7efcSopenharmony_ci } else { 830eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Inactivate"); 831eace7efcSopenharmony_ci abilityRecord->Inactivate(); 832eace7efcSopenharmony_ci } 833eace7efcSopenharmony_ci return ERR_OK; 834eace7efcSopenharmony_ci} 835eace7efcSopenharmony_ci 836eace7efcSopenharmony_civoid AbilityConnectManager::OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state) 837eace7efcSopenharmony_ci{ 838eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "state: %{public}d", state); 839eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 840eace7efcSopenharmony_ci AppAbilityState abilityState = DelayedSingleton<AppScheduler>::GetInstance()->ConvertToAppAbilityState(state); 841eace7efcSopenharmony_ci if (abilityState == AppAbilityState::ABILITY_STATE_FOREGROUND) { 842eace7efcSopenharmony_ci auto abilityRecord = GetExtensionByTokenFromServiceMap(token); 843eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 844eace7efcSopenharmony_ci if (!IsUIExtensionAbility(abilityRecord)) { 845eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "Not ui extension"); 846eace7efcSopenharmony_ci return; 847eace7efcSopenharmony_ci } 848eace7efcSopenharmony_ci if (abilityRecord->IsAbilityState(AbilityState::FOREGROUNDING)) { 849eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "abilityRecord foregrounding"); 850eace7efcSopenharmony_ci return; 851eace7efcSopenharmony_ci } 852eace7efcSopenharmony_ci std::string element = abilityRecord->GetURI(); 853eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Ability is %{public}s, start to foreground.", element.c_str()); 854eace7efcSopenharmony_ci abilityRecord->GrantUriPermissionForUIExtension(); 855eace7efcSopenharmony_ci abilityRecord->ForegroundUIExtensionAbility(); 856eace7efcSopenharmony_ci } 857eace7efcSopenharmony_ci} 858eace7efcSopenharmony_ci 859eace7efcSopenharmony_civoid AbilityConnectManager::OnAppStateChanged(const AppInfo &info) 860eace7efcSopenharmony_ci{ 861eace7efcSopenharmony_ci auto serviceMap = GetServiceMap(); 862eace7efcSopenharmony_ci std::for_each(serviceMap.begin(), serviceMap.end(), [&info](ServiceMapType::reference service) { 863eace7efcSopenharmony_ci if (service.second && (info.processName == service.second->GetAbilityInfo().process || 864eace7efcSopenharmony_ci info.processName == service.second->GetApplicationInfo().bundleName)) { 865eace7efcSopenharmony_ci auto appName = service.second->GetApplicationInfo().name; 866eace7efcSopenharmony_ci auto uid = service.second->GetAbilityInfo().applicationInfo.uid; 867eace7efcSopenharmony_ci auto isExist = [&appName, &uid]( 868eace7efcSopenharmony_ci const AppData &appData) { return appData.appName == appName && appData.uid == uid; }; 869eace7efcSopenharmony_ci auto iter = std::find_if(info.appData.begin(), info.appData.end(), isExist); 870eace7efcSopenharmony_ci if (iter != info.appData.end()) { 871eace7efcSopenharmony_ci service.second->SetAppState(info.state); 872eace7efcSopenharmony_ci } 873eace7efcSopenharmony_ci } 874eace7efcSopenharmony_ci }); 875eace7efcSopenharmony_ci 876eace7efcSopenharmony_ci auto cacheAbilityList = AbilityCacheManager::GetInstance().GetAbilityList(); 877eace7efcSopenharmony_ci std::for_each(cacheAbilityList.begin(), cacheAbilityList.end(), [&info](std::shared_ptr<AbilityRecord> &service) { 878eace7efcSopenharmony_ci if (service && (info.processName == service->GetAbilityInfo().process || 879eace7efcSopenharmony_ci info.processName == service->GetApplicationInfo().bundleName)) { 880eace7efcSopenharmony_ci auto appName = service->GetApplicationInfo().name; 881eace7efcSopenharmony_ci auto uid = service->GetAbilityInfo().applicationInfo.uid; 882eace7efcSopenharmony_ci auto isExist = [&appName, &uid](const AppData &appData) { 883eace7efcSopenharmony_ci return appData.appName == appName && appData.uid == uid; 884eace7efcSopenharmony_ci }; 885eace7efcSopenharmony_ci auto iter = std::find_if(info.appData.begin(), info.appData.end(), isExist); 886eace7efcSopenharmony_ci if (iter != info.appData.end()) { 887eace7efcSopenharmony_ci service->SetAppState(info.state); 888eace7efcSopenharmony_ci } 889eace7efcSopenharmony_ci } 890eace7efcSopenharmony_ci }); 891eace7efcSopenharmony_ci} 892eace7efcSopenharmony_ci 893eace7efcSopenharmony_ciint AbilityConnectManager::AbilityTransitionDone(const sptr<IRemoteObject> &token, int state) 894eace7efcSopenharmony_ci{ 895eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 896eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 897eace7efcSopenharmony_ci int targetState = AbilityRecord::ConvertLifeCycleToAbilityState(static_cast<AbilityLifeCycleState>(state)); 898eace7efcSopenharmony_ci std::string abilityState = AbilityRecord::ConvertAbilityState(static_cast<AbilityState>(targetState)); 899eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord; 900eace7efcSopenharmony_ci if (targetState == AbilityState::INACTIVE) { 901eace7efcSopenharmony_ci abilityRecord = GetExtensionByTokenFromServiceMap(token); 902eace7efcSopenharmony_ci } else if (targetState == AbilityState::FOREGROUND || targetState == AbilityState::BACKGROUND) { 903eace7efcSopenharmony_ci abilityRecord = GetExtensionByTokenFromServiceMap(token); 904eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 905eace7efcSopenharmony_ci abilityRecord = GetExtensionByTokenFromTerminatingMap(token); 906eace7efcSopenharmony_ci } 907eace7efcSopenharmony_ci } else if (targetState == AbilityState::INITIAL) { 908eace7efcSopenharmony_ci abilityRecord = GetExtensionByTokenFromTerminatingMap(token); 909eace7efcSopenharmony_ci } 910eace7efcSopenharmony_ci 911eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 912eace7efcSopenharmony_ci std::string element = abilityRecord->GetURI(); 913eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s called, ability:%{public}s, state:%{public}s", 914eace7efcSopenharmony_ci __func__, element.c_str(), abilityState.c_str()); 915eace7efcSopenharmony_ci 916eace7efcSopenharmony_ci switch (targetState) { 917eace7efcSopenharmony_ci case AbilityState::INACTIVE: { 918eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().type == AbilityType::SERVICE) { 919eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->UpdateAbilityState( 920eace7efcSopenharmony_ci token, AppExecFwk::AbilityState::ABILITY_STATE_CREATE); 921eace7efcSopenharmony_ci } else { 922eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->UpdateExtensionState( 923eace7efcSopenharmony_ci token, AppExecFwk::ExtensionState::EXTENSION_STATE_CREATE); 924eace7efcSopenharmony_ci auto preloadTask = [owner = weak_from_this(), abilityRecord] { 925eace7efcSopenharmony_ci auto acm = owner.lock(); 926eace7efcSopenharmony_ci if (acm == nullptr) { 927eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "null AbilityConnectManager"); 928eace7efcSopenharmony_ci return; 929eace7efcSopenharmony_ci } 930eace7efcSopenharmony_ci acm->ProcessPreload(abilityRecord); 931eace7efcSopenharmony_ci }; 932eace7efcSopenharmony_ci if (taskHandler_ != nullptr) { 933eace7efcSopenharmony_ci taskHandler_->SubmitTask(preloadTask); 934eace7efcSopenharmony_ci } 935eace7efcSopenharmony_ci } 936eace7efcSopenharmony_ci return DispatchInactive(abilityRecord, state); 937eace7efcSopenharmony_ci } 938eace7efcSopenharmony_ci case AbilityState::FOREGROUND: { 939eace7efcSopenharmony_ci abilityRecord->RemoveSignatureInfo(); 940eace7efcSopenharmony_ci return DispatchForeground(abilityRecord); 941eace7efcSopenharmony_ci } 942eace7efcSopenharmony_ci case AbilityState::BACKGROUND: { 943eace7efcSopenharmony_ci return DispatchBackground(abilityRecord); 944eace7efcSopenharmony_ci } 945eace7efcSopenharmony_ci case AbilityState::INITIAL: { 946eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().type == AbilityType::SERVICE) { 947eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->UpdateAbilityState( 948eace7efcSopenharmony_ci token, AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED); 949eace7efcSopenharmony_ci } else { 950eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->UpdateExtensionState( 951eace7efcSopenharmony_ci token, AppExecFwk::ExtensionState::EXTENSION_STATE_TERMINATED); 952eace7efcSopenharmony_ci } 953eace7efcSopenharmony_ci return DispatchTerminate(abilityRecord); 954eace7efcSopenharmony_ci } 955eace7efcSopenharmony_ci default: { 956eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "not support transiting state: %{public}d", state); 957eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 958eace7efcSopenharmony_ci } 959eace7efcSopenharmony_ci } 960eace7efcSopenharmony_ci} 961eace7efcSopenharmony_ci 962eace7efcSopenharmony_ciint AbilityConnectManager::AbilityWindowConfigTransactionDone(const sptr<IRemoteObject> &token, 963eace7efcSopenharmony_ci const WindowConfig &windowConfig) 964eace7efcSopenharmony_ci{ 965eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> guard(serialMutex_); 966eace7efcSopenharmony_ci auto abilityRecord = Token::GetAbilityRecordByToken(token); 967eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 968eace7efcSopenharmony_ci abilityRecord->SaveAbilityWindowConfig(windowConfig); 969eace7efcSopenharmony_ci return ERR_OK; 970eace7efcSopenharmony_ci} 971eace7efcSopenharmony_ci 972eace7efcSopenharmony_civoid AbilityConnectManager::ProcessPreload(const std::shared_ptr<AbilityRecord> &record) const 973eace7efcSopenharmony_ci{ 974eace7efcSopenharmony_ci auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); 975eace7efcSopenharmony_ci CHECK_POINTER(record); 976eace7efcSopenharmony_ci CHECK_POINTER(bundleMgrHelper); 977eace7efcSopenharmony_ci auto abilityInfo = record->GetAbilityInfo(); 978eace7efcSopenharmony_ci Want want; 979eace7efcSopenharmony_ci want.SetElementName(abilityInfo.deviceId, abilityInfo.bundleName, abilityInfo.name, abilityInfo.moduleName); 980eace7efcSopenharmony_ci auto uid = record->GetUid(); 981eace7efcSopenharmony_ci want.SetParam("uid", uid); 982eace7efcSopenharmony_ci bundleMgrHelper->ProcessPreload(want); 983eace7efcSopenharmony_ci} 984eace7efcSopenharmony_ci 985eace7efcSopenharmony_ciint AbilityConnectManager::ScheduleConnectAbilityDoneLocked( 986eace7efcSopenharmony_ci const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &remoteObject) 987eace7efcSopenharmony_ci{ 988eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 989eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 990eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(token, ERR_INVALID_VALUE); 991eace7efcSopenharmony_ci 992eace7efcSopenharmony_ci auto abilityRecord = Token::GetAbilityRecordByToken(token); 993eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 994eace7efcSopenharmony_ci 995eace7efcSopenharmony_ci std::string element = abilityRecord->GetURI(); 996eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s called, Connect ability done, ability: %{public}s.", 997eace7efcSopenharmony_ci __func__, element.c_str()); 998eace7efcSopenharmony_ci 999eace7efcSopenharmony_ci if ((!abilityRecord->IsAbilityState(AbilityState::INACTIVE)) && 1000eace7efcSopenharmony_ci (!abilityRecord->IsAbilityState(AbilityState::ACTIVE))) { 1001eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "ability not inactive, state: %{public}d", 1002eace7efcSopenharmony_ci abilityRecord->GetAbilityState()); 1003eace7efcSopenharmony_ci return INVALID_CONNECTION_STATE; 1004eace7efcSopenharmony_ci } 1005eace7efcSopenharmony_ci abilityRecord->RemoveConnectWant(); 1006eace7efcSopenharmony_ci abilityRecord->RemoveSignatureInfo(); 1007eace7efcSopenharmony_ci 1008eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().type == AbilityType::SERVICE) { 1009eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->UpdateAbilityState( 1010eace7efcSopenharmony_ci token, AppExecFwk::AbilityState::ABILITY_STATE_CONNECTED); 1011eace7efcSopenharmony_ci } else { 1012eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->UpdateExtensionState( 1013eace7efcSopenharmony_ci token, AppExecFwk::ExtensionState::EXTENSION_STATE_CONNECTED); 1014eace7efcSopenharmony_ci } 1015eace7efcSopenharmony_ci EventInfo eventInfo = BuildEventInfo(abilityRecord); 1016eace7efcSopenharmony_ci eventInfo.userId = userId_; 1017eace7efcSopenharmony_ci eventInfo.bundleName = abilityRecord->GetAbilityInfo().bundleName; 1018eace7efcSopenharmony_ci eventInfo.moduleName = abilityRecord->GetAbilityInfo().moduleName; 1019eace7efcSopenharmony_ci eventInfo.abilityName = abilityRecord->GetAbilityInfo().name; 1020eace7efcSopenharmony_ci EventReport::SendConnectServiceEvent(EventName::CONNECT_SERVICE, eventInfo); 1021eace7efcSopenharmony_ci 1022eace7efcSopenharmony_ci abilityRecord->SetConnRemoteObject(remoteObject); 1023eace7efcSopenharmony_ci // There may be multiple callers waiting for the connection result 1024eace7efcSopenharmony_ci auto connectRecordList = abilityRecord->GetConnectRecordList(); 1025eace7efcSopenharmony_ci for (auto &connectRecord : connectRecordList) { 1026eace7efcSopenharmony_ci connectRecord->ScheduleConnectAbilityDone(); 1027eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().type == AbilityType::EXTENSION && 1028eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().extensionAbilityType != AppExecFwk::ExtensionAbilityType::SERVICE) { 1029eace7efcSopenharmony_ci PostExtensionDelayDisconnectTask(connectRecord); 1030eace7efcSopenharmony_ci } 1031eace7efcSopenharmony_ci } 1032eace7efcSopenharmony_ci CompleteStartServiceReq(abilityRecord->GetURI()); 1033eace7efcSopenharmony_ci ResSchedUtil::GetInstance().ReportLoadingEventToRss(LoadingStage::CONNECT_END, abilityRecord->GetPid(), 1034eace7efcSopenharmony_ci abilityRecord->GetUid()); 1035eace7efcSopenharmony_ci return ERR_OK; 1036eace7efcSopenharmony_ci} 1037eace7efcSopenharmony_ci 1038eace7efcSopenharmony_civoid AbilityConnectManager::ProcessEliminateAbilityRecord(std::shared_ptr<AbilityRecord> eliminateRecord) 1039eace7efcSopenharmony_ci{ 1040eace7efcSopenharmony_ci CHECK_POINTER(eliminateRecord); 1041eace7efcSopenharmony_ci std::string eliminateKey = eliminateRecord->GetURI(); 1042eace7efcSopenharmony_ci if (FRS_BUNDLE_NAME == eliminateRecord->GetAbilityInfo().bundleName) { 1043eace7efcSopenharmony_ci eliminateKey = eliminateKey + 1044eace7efcSopenharmony_ci std::to_string(eliminateRecord->GetWant().GetIntParam(FRS_APP_INDEX, 0)); 1045eace7efcSopenharmony_ci } 1046eace7efcSopenharmony_ci AddToServiceMap(eliminateKey, eliminateRecord); 1047eace7efcSopenharmony_ci TerminateRecord(eliminateRecord); 1048eace7efcSopenharmony_ci} 1049eace7efcSopenharmony_ci 1050eace7efcSopenharmony_civoid AbilityConnectManager::TerminateOrCacheAbility(std::shared_ptr<AbilityRecord> abilityRecord) 1051eace7efcSopenharmony_ci{ 1052eace7efcSopenharmony_ci RemoveUIExtensionAbilityRecord(abilityRecord); 1053eace7efcSopenharmony_ci if (abilityRecord->IsSceneBoard()) { 1054eace7efcSopenharmony_ci return; 1055eace7efcSopenharmony_ci } 1056eace7efcSopenharmony_ci if (IsCacheExtensionAbilityType(abilityRecord)) { 1057eace7efcSopenharmony_ci std::string serviceKey = abilityRecord->GetURI(); 1058eace7efcSopenharmony_ci auto abilityInfo = abilityRecord->GetAbilityInfo(); 1059eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Cache the ability, service:%{public}s, extension type %{public}d", 1060eace7efcSopenharmony_ci serviceKey.c_str(), abilityInfo.extensionAbilityType); 1061eace7efcSopenharmony_ci if (FRS_BUNDLE_NAME == abilityInfo.bundleName) { 1062eace7efcSopenharmony_ci AppExecFwk::ElementName elementName(abilityInfo.deviceId, abilityInfo.bundleName, abilityInfo.name, 1063eace7efcSopenharmony_ci abilityInfo.moduleName); 1064eace7efcSopenharmony_ci serviceKey = elementName.GetURI() + 1065eace7efcSopenharmony_ci std::to_string(abilityRecord->GetWant().GetIntParam(FRS_APP_INDEX, 0)); 1066eace7efcSopenharmony_ci } 1067eace7efcSopenharmony_ci { 1068eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 1069eace7efcSopenharmony_ci serviceMap_.erase(serviceKey); 1070eace7efcSopenharmony_ci } 1071eace7efcSopenharmony_ci auto eliminateRecord = AbilityCacheManager::GetInstance().Put(abilityRecord); 1072eace7efcSopenharmony_ci if (eliminateRecord != nullptr) { 1073eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Terminate the eliminated ability, service:%{public}s.", 1074eace7efcSopenharmony_ci eliminateRecord->GetURI().c_str()); 1075eace7efcSopenharmony_ci ProcessEliminateAbilityRecord(eliminateRecord); 1076eace7efcSopenharmony_ci } 1077eace7efcSopenharmony_ci return; 1078eace7efcSopenharmony_ci } 1079eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Terminate the ability, service:%{public}s, extension type %{public}d", 1080eace7efcSopenharmony_ci abilityRecord->GetURI().c_str(), abilityRecord->GetAbilityInfo().extensionAbilityType); 1081eace7efcSopenharmony_ci TerminateRecord(abilityRecord); 1082eace7efcSopenharmony_ci} 1083eace7efcSopenharmony_ci 1084eace7efcSopenharmony_ciint AbilityConnectManager::ScheduleDisconnectAbilityDoneLocked(const sptr<IRemoteObject> &token) 1085eace7efcSopenharmony_ci{ 1086eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1087eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1088eace7efcSopenharmony_ci auto abilityRecord = GetExtensionByTokenFromServiceMap(token); 1089eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, CONNECTION_NOT_EXIST); 1090eace7efcSopenharmony_ci 1091eace7efcSopenharmony_ci auto connect = abilityRecord->GetDisconnectingRecord(); 1092eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(connect, CONNECTION_NOT_EXIST); 1093eace7efcSopenharmony_ci 1094eace7efcSopenharmony_ci if (!abilityRecord->IsAbilityState(AbilityState::ACTIVE)) { 1095eace7efcSopenharmony_ci if (IsUIExtensionAbility(abilityRecord) && (abilityRecord->IsForeground() || 1096eace7efcSopenharmony_ci abilityRecord->IsAbilityState(AbilityState::BACKGROUND) || 1097eace7efcSopenharmony_ci abilityRecord->IsAbilityState(AbilityState::BACKGROUNDING))) { 1098eace7efcSopenharmony_ci // uiextension ability support connect and start, so the ability state maybe others 1099eace7efcSopenharmony_ci TAG_LOGI( 1100eace7efcSopenharmony_ci AAFwkTag::ABILITYMGR, "disconnect when ability state: %{public}d", abilityRecord->GetAbilityState()); 1101eace7efcSopenharmony_ci } else { 1102eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "ability not active, state: %{public}d", 1103eace7efcSopenharmony_ci abilityRecord->GetAbilityState()); 1104eace7efcSopenharmony_ci return INVALID_CONNECTION_STATE; 1105eace7efcSopenharmony_ci } 1106eace7efcSopenharmony_ci } 1107eace7efcSopenharmony_ci 1108eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().type == AbilityType::SERVICE) { 1109eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->UpdateAbilityState( 1110eace7efcSopenharmony_ci token, AppExecFwk::AbilityState::ABILITY_STATE_DISCONNECTED); 1111eace7efcSopenharmony_ci } else { 1112eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->UpdateExtensionState( 1113eace7efcSopenharmony_ci token, AppExecFwk::ExtensionState::EXTENSION_STATE_DISCONNECTED); 1114eace7efcSopenharmony_ci } 1115eace7efcSopenharmony_ci 1116eace7efcSopenharmony_ci std::string element = abilityRecord->GetURI(); 1117eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "ScheduleDisconnectAbilityDoneLocked called, service:%{public}s.", 1118eace7efcSopenharmony_ci element.c_str()); 1119eace7efcSopenharmony_ci 1120eace7efcSopenharmony_ci // complete disconnect and remove record from conn map 1121eace7efcSopenharmony_ci connect->ScheduleDisconnectAbilityDone(); 1122eace7efcSopenharmony_ci abilityRecord->RemoveConnectRecordFromList(connect); 1123eace7efcSopenharmony_ci if (abilityRecord->IsConnectListEmpty() && abilityRecord->GetStartId() == 0) { 1124eace7efcSopenharmony_ci if (IsUIExtensionAbility(abilityRecord) && CheckUIExtensionAbilitySessionExist(abilityRecord)) { 1125eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "exist ui extension component, don't terminate when disconnect"); 1126eace7efcSopenharmony_ci } else if (abilityRecord->GetAbilityInfo().extensionAbilityType == 1127eace7efcSopenharmony_ci AppExecFwk::ExtensionAbilityType::UI_SERVICE) { 1128eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "don't terminate uiservice"); 1129eace7efcSopenharmony_ci } else { 1130eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, 1131eace7efcSopenharmony_ci "Service ability has no any connection, and not started, need terminate or cache."); 1132eace7efcSopenharmony_ci TerminateOrCacheAbility(abilityRecord); 1133eace7efcSopenharmony_ci } 1134eace7efcSopenharmony_ci } 1135eace7efcSopenharmony_ci RemoveConnectionRecordFromMap(connect); 1136eace7efcSopenharmony_ci 1137eace7efcSopenharmony_ci return ERR_OK; 1138eace7efcSopenharmony_ci} 1139eace7efcSopenharmony_ci 1140eace7efcSopenharmony_ciint AbilityConnectManager::ScheduleCommandAbilityDoneLocked(const sptr<IRemoteObject> &token) 1141eace7efcSopenharmony_ci{ 1142eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1143eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1144eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(token, ERR_INVALID_VALUE); 1145eace7efcSopenharmony_ci auto abilityRecord = Token::GetAbilityRecordByToken(token); 1146eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 1147eace7efcSopenharmony_ci std::string element = abilityRecord->GetURI(); 1148eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s called, Ability: %{public}s", __func__, element.c_str()); 1149eace7efcSopenharmony_ci 1150eace7efcSopenharmony_ci if ((!abilityRecord->IsAbilityState(AbilityState::INACTIVE)) && 1151eace7efcSopenharmony_ci (!abilityRecord->IsAbilityState(AbilityState::ACTIVE))) { 1152eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "ability not inactive, state: %{public}d", 1153eace7efcSopenharmony_ci abilityRecord->GetAbilityState()); 1154eace7efcSopenharmony_ci return INVALID_CONNECTION_STATE; 1155eace7efcSopenharmony_ci } 1156eace7efcSopenharmony_ci EventInfo eventInfo = BuildEventInfo(abilityRecord); 1157eace7efcSopenharmony_ci EventReport::SendStartServiceEvent(EventName::START_SERVICE, eventInfo); 1158eace7efcSopenharmony_ci abilityRecord->RemoveSignatureInfo(); 1159eace7efcSopenharmony_ci // complete command and pop waiting start ability from queue. 1160eace7efcSopenharmony_ci CompleteCommandAbility(abilityRecord); 1161eace7efcSopenharmony_ci 1162eace7efcSopenharmony_ci return ERR_OK; 1163eace7efcSopenharmony_ci} 1164eace7efcSopenharmony_ci 1165eace7efcSopenharmony_ciint AbilityConnectManager::ScheduleCommandAbilityWindowDone( 1166eace7efcSopenharmony_ci const sptr<IRemoteObject> &token, 1167eace7efcSopenharmony_ci const sptr<SessionInfo> &sessionInfo, 1168eace7efcSopenharmony_ci WindowCommand winCmd, 1169eace7efcSopenharmony_ci AbilityCommand abilityCmd) 1170eace7efcSopenharmony_ci{ 1171eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1172eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1173eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(token, ERR_INVALID_VALUE); 1174eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(sessionInfo, ERR_INVALID_VALUE); 1175eace7efcSopenharmony_ci auto abilityRecord = Token::GetAbilityRecordByToken(token); 1176eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 1177eace7efcSopenharmony_ci std::string element = abilityRecord->GetURI(); 1178eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, 1179eace7efcSopenharmony_ci "ability:%{public}s, persistentId:%{private}d, winCmd:%{public}d, abilityCmd:%{public}d", element.c_str(), 1180eace7efcSopenharmony_ci sessionInfo->persistentId, winCmd, abilityCmd); 1181eace7efcSopenharmony_ci 1182eace7efcSopenharmony_ci // Only foreground mode need cancel, cause only foreground CommandAbilityWindow post timeout task. 1183eace7efcSopenharmony_ci if (taskHandler_ && winCmd == WIN_CMD_FOREGROUND) { 1184eace7efcSopenharmony_ci int recordId = abilityRecord->GetRecordId(); 1185eace7efcSopenharmony_ci std::string taskName = std::string("CommandWindowTimeout_") + std::to_string(recordId) + std::string("_") + 1186eace7efcSopenharmony_ci std::to_string(sessionInfo->persistentId) + std::string("_") + std::to_string(winCmd); 1187eace7efcSopenharmony_ci taskHandler_->CancelTask(taskName); 1188eace7efcSopenharmony_ci } 1189eace7efcSopenharmony_ci 1190eace7efcSopenharmony_ci if (winCmd == WIN_CMD_DESTROY) { 1191eace7efcSopenharmony_ci HandleCommandDestroy(sessionInfo); 1192eace7efcSopenharmony_ci } 1193eace7efcSopenharmony_ci 1194eace7efcSopenharmony_ci abilityRecord->SetAbilityWindowState(sessionInfo, winCmd, true); 1195eace7efcSopenharmony_ci 1196eace7efcSopenharmony_ci CompleteStartServiceReq(element); 1197eace7efcSopenharmony_ci return ERR_OK; 1198eace7efcSopenharmony_ci} 1199eace7efcSopenharmony_ci 1200eace7efcSopenharmony_civoid AbilityConnectManager::HandleCommandDestroy(const sptr<SessionInfo> &sessionInfo) 1201eace7efcSopenharmony_ci{ 1202eace7efcSopenharmony_ci if (sessionInfo == nullptr) { 1203eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "null sessionInfo"); 1204eace7efcSopenharmony_ci return; 1205eace7efcSopenharmony_ci } 1206eace7efcSopenharmony_ci if (sessionInfo->sessionToken) { 1207eace7efcSopenharmony_ci RemoveUIExtWindowDeathRecipient(sessionInfo->sessionToken); 1208eace7efcSopenharmony_ci size_t ret = 0; 1209eace7efcSopenharmony_ci { 1210eace7efcSopenharmony_ci std::lock_guard guard(uiExtensionMapMutex_); 1211eace7efcSopenharmony_ci ret = uiExtensionMap_.erase(sessionInfo->sessionToken); 1212eace7efcSopenharmony_ci } 1213eace7efcSopenharmony_ci if (ret > 0) { 1214eace7efcSopenharmony_ci return; 1215eace7efcSopenharmony_ci } 1216eace7efcSopenharmony_ci 1217eace7efcSopenharmony_ci std::lock_guard guard(windowExtensionMapMutex_); 1218eace7efcSopenharmony_ci for (auto& item : windowExtensionMap_) { 1219eace7efcSopenharmony_ci auto sessionInfoVal = item.second.second; 1220eace7efcSopenharmony_ci if (sessionInfoVal && sessionInfoVal->callerToken == sessionInfo->sessionToken) { 1221eace7efcSopenharmony_ci windowExtensionMap_.erase(item.first); 1222eace7efcSopenharmony_ci break; 1223eace7efcSopenharmony_ci } 1224eace7efcSopenharmony_ci } 1225eace7efcSopenharmony_ci } 1226eace7efcSopenharmony_ci} 1227eace7efcSopenharmony_ci 1228eace7efcSopenharmony_civoid AbilityConnectManager::CompleteCommandAbility(std::shared_ptr<AbilityRecord> abilityRecord) 1229eace7efcSopenharmony_ci{ 1230eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1231eace7efcSopenharmony_ci if (taskHandler_) { 1232eace7efcSopenharmony_ci int recordId = abilityRecord->GetRecordId(); 1233eace7efcSopenharmony_ci std::string taskName = std::string("CommandTimeout_") + std::to_string(recordId) + std::string("_") + 1234eace7efcSopenharmony_ci std::to_string(abilityRecord->GetStartId()); 1235eace7efcSopenharmony_ci taskHandler_->CancelTask(taskName); 1236eace7efcSopenharmony_ci } 1237eace7efcSopenharmony_ci 1238eace7efcSopenharmony_ci abilityRecord->SetAbilityState(AbilityState::ACTIVE); 1239eace7efcSopenharmony_ci 1240eace7efcSopenharmony_ci // manage queued request 1241eace7efcSopenharmony_ci CompleteStartServiceReq(abilityRecord->GetURI()); 1242eace7efcSopenharmony_ci if (abilityRecord->NeedConnectAfterCommand()) { 1243eace7efcSopenharmony_ci abilityRecord->UpdateConnectWant(); 1244eace7efcSopenharmony_ci ConnectAbility(abilityRecord); 1245eace7efcSopenharmony_ci } 1246eace7efcSopenharmony_ci} 1247eace7efcSopenharmony_ci 1248eace7efcSopenharmony_civoid AbilityConnectManager::CompleteStartServiceReq(const std::string &serviceUri) 1249eace7efcSopenharmony_ci{ 1250eace7efcSopenharmony_ci std::shared_ptr<std::list<OHOS::AAFwk::AbilityRequest>> reqList; 1251eace7efcSopenharmony_ci { 1252eace7efcSopenharmony_ci std::lock_guard guard(startServiceReqListLock_); 1253eace7efcSopenharmony_ci auto it = startServiceReqList_.find(serviceUri); 1254eace7efcSopenharmony_ci if (it != startServiceReqList_.end()) { 1255eace7efcSopenharmony_ci reqList = it->second; 1256eace7efcSopenharmony_ci startServiceReqList_.erase(it); 1257eace7efcSopenharmony_ci if (taskHandler_) { 1258eace7efcSopenharmony_ci taskHandler_->CancelTask(std::string("start_service_timeout:") + serviceUri); 1259eace7efcSopenharmony_ci } 1260eace7efcSopenharmony_ci } 1261eace7efcSopenharmony_ci } 1262eace7efcSopenharmony_ci 1263eace7efcSopenharmony_ci if (reqList) { 1264eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "target service activating: %{public}zu, uri: %{public}s", reqList->size(), 1265eace7efcSopenharmony_ci serviceUri.c_str()); 1266eace7efcSopenharmony_ci for (const auto &req: *reqList) { 1267eace7efcSopenharmony_ci StartAbilityLocked(req); 1268eace7efcSopenharmony_ci } 1269eace7efcSopenharmony_ci } 1270eace7efcSopenharmony_ci} 1271eace7efcSopenharmony_ci 1272eace7efcSopenharmony_cistd::shared_ptr<AbilityRecord> AbilityConnectManager::GetServiceRecordByElementName(const std::string &element) 1273eace7efcSopenharmony_ci{ 1274eace7efcSopenharmony_ci std::lock_guard guard(serviceMapMutex_); 1275eace7efcSopenharmony_ci auto mapIter = serviceMap_.find(element); 1276eace7efcSopenharmony_ci if (mapIter != serviceMap_.end()) { 1277eace7efcSopenharmony_ci return mapIter->second; 1278eace7efcSopenharmony_ci } 1279eace7efcSopenharmony_ci return nullptr; 1280eace7efcSopenharmony_ci} 1281eace7efcSopenharmony_ci 1282eace7efcSopenharmony_cistd::shared_ptr<AbilityRecord> AbilityConnectManager::GetExtensionByTokenFromServiceMap( 1283eace7efcSopenharmony_ci const sptr<IRemoteObject> &token) 1284eace7efcSopenharmony_ci{ 1285eace7efcSopenharmony_ci auto IsMatch = [token](auto service) { 1286eace7efcSopenharmony_ci if (!service.second) { 1287eace7efcSopenharmony_ci return false; 1288eace7efcSopenharmony_ci } 1289eace7efcSopenharmony_ci sptr<IRemoteObject> srcToken = service.second->GetToken(); 1290eace7efcSopenharmony_ci return srcToken == token; 1291eace7efcSopenharmony_ci }; 1292eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 1293eace7efcSopenharmony_ci auto serviceRecord = std::find_if(serviceMap_.begin(), serviceMap_.end(), IsMatch); 1294eace7efcSopenharmony_ci if (serviceRecord != serviceMap_.end()) { 1295eace7efcSopenharmony_ci return serviceRecord->second; 1296eace7efcSopenharmony_ci } 1297eace7efcSopenharmony_ci return nullptr; 1298eace7efcSopenharmony_ci} 1299eace7efcSopenharmony_ci 1300eace7efcSopenharmony_cistd::shared_ptr<AbilityRecord> AbilityConnectManager::GetExtensionByTokenFromAbilityCache( 1301eace7efcSopenharmony_ci const sptr<IRemoteObject> &token) 1302eace7efcSopenharmony_ci{ 1303eace7efcSopenharmony_ci return AbilityCacheManager::GetInstance().FindRecordByToken(token); 1304eace7efcSopenharmony_ci} 1305eace7efcSopenharmony_ci 1306eace7efcSopenharmony_cistd::shared_ptr<AbilityRecord> AbilityConnectManager::GetExtensionByIdFromServiceMap( 1307eace7efcSopenharmony_ci const int64_t &abilityRecordId) 1308eace7efcSopenharmony_ci{ 1309eace7efcSopenharmony_ci auto IsMatch = [abilityRecordId](auto &service) { 1310eace7efcSopenharmony_ci if (!service.second) { 1311eace7efcSopenharmony_ci return false; 1312eace7efcSopenharmony_ci } 1313eace7efcSopenharmony_ci return service.second->GetAbilityRecordId() == abilityRecordId; 1314eace7efcSopenharmony_ci }; 1315eace7efcSopenharmony_ci 1316eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 1317eace7efcSopenharmony_ci auto serviceRecord = std::find_if(serviceMap_.begin(), serviceMap_.end(), IsMatch); 1318eace7efcSopenharmony_ci if (serviceRecord != serviceMap_.end()) { 1319eace7efcSopenharmony_ci return serviceRecord->second; 1320eace7efcSopenharmony_ci } 1321eace7efcSopenharmony_ci return nullptr; 1322eace7efcSopenharmony_ci} 1323eace7efcSopenharmony_ci 1324eace7efcSopenharmony_cistd::shared_ptr<AbilityRecord> AbilityConnectManager::GetExtensionByIdFromTerminatingMap( 1325eace7efcSopenharmony_ci const int64_t &abilityRecordId) 1326eace7efcSopenharmony_ci{ 1327eace7efcSopenharmony_ci auto IsMatch = [abilityRecordId](auto &extensionRecord) { 1328eace7efcSopenharmony_ci if (extensionRecord == nullptr) { 1329eace7efcSopenharmony_ci return false; 1330eace7efcSopenharmony_ci } 1331eace7efcSopenharmony_ci return extensionRecord->GetAbilityRecordId() == abilityRecordId; 1332eace7efcSopenharmony_ci }; 1333eace7efcSopenharmony_ci 1334eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 1335eace7efcSopenharmony_ci auto extensionRecord = std::find_if(terminatingExtensionList_.begin(), terminatingExtensionList_.end(), IsMatch); 1336eace7efcSopenharmony_ci if (extensionRecord != terminatingExtensionList_.end()) { 1337eace7efcSopenharmony_ci return *extensionRecord; 1338eace7efcSopenharmony_ci } 1339eace7efcSopenharmony_ci return nullptr; 1340eace7efcSopenharmony_ci} 1341eace7efcSopenharmony_ci 1342eace7efcSopenharmony_cistd::shared_ptr<AbilityRecord> AbilityConnectManager::GetUIExtensionBySessionInfo( 1343eace7efcSopenharmony_ci const sptr<SessionInfo> &sessionInfo) 1344eace7efcSopenharmony_ci{ 1345eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(sessionInfo, nullptr); 1346eace7efcSopenharmony_ci auto sessionToken = iface_cast<Rosen::ISession>(sessionInfo->sessionToken); 1347eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(sessionToken, nullptr); 1348eace7efcSopenharmony_ci 1349eace7efcSopenharmony_ci std::lock_guard guard(uiExtensionMapMutex_); 1350eace7efcSopenharmony_ci auto it = uiExtensionMap_.find(sessionToken->AsObject()); 1351eace7efcSopenharmony_ci if (it != uiExtensionMap_.end()) { 1352eace7efcSopenharmony_ci auto abilityRecord = it->second.first.lock(); 1353eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 1354eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "abilityRecord null"); 1355eace7efcSopenharmony_ci RemoveUIExtWindowDeathRecipient(sessionToken->AsObject()); 1356eace7efcSopenharmony_ci uiExtensionMap_.erase(it); 1357eace7efcSopenharmony_ci return nullptr; 1358eace7efcSopenharmony_ci } 1359eace7efcSopenharmony_ci auto savedSessionInfo = it->second.second; 1360eace7efcSopenharmony_ci if (!savedSessionInfo || savedSessionInfo->sessionToken != sessionInfo->sessionToken 1361eace7efcSopenharmony_ci || savedSessionInfo->callerToken != sessionInfo->callerToken) { 1362eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "inconsistent sessionInfo"); 1363eace7efcSopenharmony_ci return nullptr; 1364eace7efcSopenharmony_ci } 1365eace7efcSopenharmony_ci return abilityRecord; 1366eace7efcSopenharmony_ci } else { 1367eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "UIExtension not found"); 1368eace7efcSopenharmony_ci } 1369eace7efcSopenharmony_ci return nullptr; 1370eace7efcSopenharmony_ci} 1371eace7efcSopenharmony_ci 1372eace7efcSopenharmony_cistd::shared_ptr<AbilityRecord> AbilityConnectManager::GetExtensionByTokenFromTerminatingMap( 1373eace7efcSopenharmony_ci const sptr<IRemoteObject> &token) 1374eace7efcSopenharmony_ci{ 1375eace7efcSopenharmony_ci auto IsMatch = [token](auto& extensionRecord) { 1376eace7efcSopenharmony_ci if (extensionRecord == nullptr) { 1377eace7efcSopenharmony_ci return false; 1378eace7efcSopenharmony_ci } 1379eace7efcSopenharmony_ci auto terminatingToken = extensionRecord->GetToken(); 1380eace7efcSopenharmony_ci if (terminatingToken != nullptr) { 1381eace7efcSopenharmony_ci return terminatingToken->AsObject() == token; 1382eace7efcSopenharmony_ci } 1383eace7efcSopenharmony_ci return false; 1384eace7efcSopenharmony_ci }; 1385eace7efcSopenharmony_ci 1386eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 1387eace7efcSopenharmony_ci auto terminatingExtensionRecord = 1388eace7efcSopenharmony_ci std::find_if(terminatingExtensionList_.begin(), terminatingExtensionList_.end(), IsMatch); 1389eace7efcSopenharmony_ci if (terminatingExtensionRecord != terminatingExtensionList_.end()) { 1390eace7efcSopenharmony_ci return *terminatingExtensionRecord; 1391eace7efcSopenharmony_ci } 1392eace7efcSopenharmony_ci return nullptr; 1393eace7efcSopenharmony_ci} 1394eace7efcSopenharmony_ci 1395eace7efcSopenharmony_cistd::list<std::shared_ptr<ConnectionRecord>> AbilityConnectManager::GetConnectRecordListByCallback( 1396eace7efcSopenharmony_ci sptr<IAbilityConnection> callback) 1397eace7efcSopenharmony_ci{ 1398eace7efcSopenharmony_ci std::lock_guard guard(connectMapMutex_); 1399eace7efcSopenharmony_ci std::list<std::shared_ptr<ConnectionRecord>> connectList; 1400eace7efcSopenharmony_ci auto connectMapIter = connectMap_.find(callback->AsObject()); 1401eace7efcSopenharmony_ci if (connectMapIter != connectMap_.end()) { 1402eace7efcSopenharmony_ci connectList = connectMapIter->second; 1403eace7efcSopenharmony_ci } 1404eace7efcSopenharmony_ci return connectList; 1405eace7efcSopenharmony_ci} 1406eace7efcSopenharmony_ci 1407eace7efcSopenharmony_civoid AbilityConnectManager::LoadAbility(const std::shared_ptr<AbilityRecord> &abilityRecord) 1408eace7efcSopenharmony_ci{ 1409eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1410eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1411eace7efcSopenharmony_ci abilityRecord->SetStartTime(); 1412eace7efcSopenharmony_ci 1413eace7efcSopenharmony_ci if (!abilityRecord->CanRestartRootLauncher()) { 1414eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "CanRestartRootLauncher fail"); 1415eace7efcSopenharmony_ci RemoveServiceAbility(abilityRecord); 1416eace7efcSopenharmony_ci return; 1417eace7efcSopenharmony_ci } 1418eace7efcSopenharmony_ci if (!abilityRecord->IsDebugApp()) { 1419eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "IsDebug is false, here is not debug app"); 1420eace7efcSopenharmony_ci PostTimeOutTask(abilityRecord, AbilityManagerService::LOAD_TIMEOUT_MSG); 1421eace7efcSopenharmony_ci } 1422eace7efcSopenharmony_ci sptr<Token> token = abilityRecord->GetToken(); 1423eace7efcSopenharmony_ci sptr<Token> perToken = nullptr; 1424eace7efcSopenharmony_ci if (abilityRecord->IsCreateByConnect()) { 1425eace7efcSopenharmony_ci perToken = iface_cast<Token>(abilityRecord->GetConnectingRecord()->GetToken()); 1426eace7efcSopenharmony_ci } else { 1427eace7efcSopenharmony_ci auto callerList = abilityRecord->GetCallerRecordList(); 1428eace7efcSopenharmony_ci if (!callerList.empty() && callerList.back()) { 1429eace7efcSopenharmony_ci auto caller = callerList.back()->GetCaller(); 1430eace7efcSopenharmony_ci if (caller) { 1431eace7efcSopenharmony_ci perToken = caller->GetToken(); 1432eace7efcSopenharmony_ci } 1433eace7efcSopenharmony_ci } 1434eace7efcSopenharmony_ci } 1435eace7efcSopenharmony_ci 1436eace7efcSopenharmony_ci UpdateUIExtensionInfo(abilityRecord); 1437eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->LoadAbility( 1438eace7efcSopenharmony_ci token, perToken, abilityRecord->GetAbilityInfo(), abilityRecord->GetApplicationInfo(), 1439eace7efcSopenharmony_ci abilityRecord->GetWant(), abilityRecord->GetRecordId(), abilityRecord->GetInstanceKey()); 1440eace7efcSopenharmony_ci abilityRecord->SetLoadState(AbilityLoadState::LOADING); 1441eace7efcSopenharmony_ci} 1442eace7efcSopenharmony_ci 1443eace7efcSopenharmony_civoid AbilityConnectManager::PostRestartResidentTask(const AbilityRequest &abilityRequest) 1444eace7efcSopenharmony_ci{ 1445eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "PostRestartResidentTask start"); 1446eace7efcSopenharmony_ci CHECK_POINTER(taskHandler_); 1447eace7efcSopenharmony_ci std::string taskName = std::string("RestartResident_") + std::string(abilityRequest.abilityInfo.name); 1448eace7efcSopenharmony_ci auto task = [abilityRequest, connectManager = shared_from_this()]() { 1449eace7efcSopenharmony_ci CHECK_POINTER(connectManager); 1450eace7efcSopenharmony_ci connectManager->HandleRestartResidentTask(abilityRequest); 1451eace7efcSopenharmony_ci }; 1452eace7efcSopenharmony_ci int restartIntervalTime = 0; 1453eace7efcSopenharmony_ci auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance(); 1454eace7efcSopenharmony_ci if (abilityMgr) { 1455eace7efcSopenharmony_ci restartIntervalTime = AmsConfigurationParameter::GetInstance().GetRestartIntervalTime(); 1456eace7efcSopenharmony_ci } 1457eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "PostRestartResidentTask, time:%{public}d", restartIntervalTime); 1458eace7efcSopenharmony_ci taskHandler_->SubmitTask(task, taskName, restartIntervalTime); 1459eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "PostRestartResidentTask end"); 1460eace7efcSopenharmony_ci} 1461eace7efcSopenharmony_ci 1462eace7efcSopenharmony_civoid AbilityConnectManager::HandleRestartResidentTask(const AbilityRequest &abilityRequest) 1463eace7efcSopenharmony_ci{ 1464eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "HandleRestartResidentTask start"); 1465eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1466eace7efcSopenharmony_ci auto findRestartResidentTask = [abilityRequest](const AbilityRequest &requestInfo) { 1467eace7efcSopenharmony_ci return (requestInfo.want.GetElement().GetBundleName() == abilityRequest.want.GetElement().GetBundleName() && 1468eace7efcSopenharmony_ci requestInfo.want.GetElement().GetModuleName() == abilityRequest.want.GetElement().GetModuleName() && 1469eace7efcSopenharmony_ci requestInfo.want.GetElement().GetAbilityName() == abilityRequest.want.GetElement().GetAbilityName()); 1470eace7efcSopenharmony_ci }; 1471eace7efcSopenharmony_ci auto findIter = find_if(restartResidentTaskList_.begin(), restartResidentTaskList_.end(), findRestartResidentTask); 1472eace7efcSopenharmony_ci if (findIter != restartResidentTaskList_.end()) { 1473eace7efcSopenharmony_ci restartResidentTaskList_.erase(findIter); 1474eace7efcSopenharmony_ci } 1475eace7efcSopenharmony_ci StartAbilityLocked(abilityRequest); 1476eace7efcSopenharmony_ci} 1477eace7efcSopenharmony_ci 1478eace7efcSopenharmony_civoid AbilityConnectManager::PostTimeOutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, uint32_t messageId) 1479eace7efcSopenharmony_ci{ 1480eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1481eace7efcSopenharmony_ci int connectRecordId = 0; 1482eace7efcSopenharmony_ci if (messageId == AbilityConnectManager::CONNECT_TIMEOUT_MSG) { 1483eace7efcSopenharmony_ci auto connectRecord = abilityRecord->GetConnectingRecord(); 1484eace7efcSopenharmony_ci CHECK_POINTER(connectRecord); 1485eace7efcSopenharmony_ci connectRecordId = connectRecord->GetRecordId(); 1486eace7efcSopenharmony_ci } 1487eace7efcSopenharmony_ci PostTimeOutTask(abilityRecord, connectRecordId, messageId); 1488eace7efcSopenharmony_ci} 1489eace7efcSopenharmony_ci 1490eace7efcSopenharmony_civoid AbilityConnectManager::PostTimeOutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, 1491eace7efcSopenharmony_ci int connectRecordId, uint32_t messageId) 1492eace7efcSopenharmony_ci{ 1493eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1494eace7efcSopenharmony_ci CHECK_POINTER(taskHandler_); 1495eace7efcSopenharmony_ci 1496eace7efcSopenharmony_ci std::string taskName; 1497eace7efcSopenharmony_ci int32_t delayTime = 0; 1498eace7efcSopenharmony_ci if (messageId == AbilityManagerService::LOAD_TIMEOUT_MSG) { 1499eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) { 1500eace7efcSopenharmony_ci return abilityRecord->PostUIExtensionAbilityTimeoutTask(messageId); 1501eace7efcSopenharmony_ci } 1502eace7efcSopenharmony_ci // first load ability, There is at most one connect record. 1503eace7efcSopenharmony_ci int recordId = abilityRecord->GetRecordId(); 1504eace7efcSopenharmony_ci taskName = std::string("LoadTimeout_") + std::to_string(recordId); 1505eace7efcSopenharmony_ci delayTime = AmsConfigurationParameter::GetInstance().GetAppStartTimeoutTime() * LOAD_TIMEOUT_MULTIPLE; 1506eace7efcSopenharmony_ci } else if (messageId == AbilityConnectManager::CONNECT_TIMEOUT_MSG) { 1507eace7efcSopenharmony_ci taskName = std::string("ConnectTimeout_") + std::to_string(connectRecordId); 1508eace7efcSopenharmony_ci delayTime = AmsConfigurationParameter::GetInstance().GetAppStartTimeoutTime() * CONNECT_TIMEOUT_MULTIPLE; 1509eace7efcSopenharmony_ci ResSchedUtil::GetInstance().ReportLoadingEventToRss(LoadingStage::CONNECT_BEGIN, abilityRecord->GetPid(), 1510eace7efcSopenharmony_ci abilityRecord->GetUid(), delayTime); 1511eace7efcSopenharmony_ci } else { 1512eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "messageId error"); 1513eace7efcSopenharmony_ci return; 1514eace7efcSopenharmony_ci } 1515eace7efcSopenharmony_ci 1516eace7efcSopenharmony_ci // check libc.hook_mode 1517eace7efcSopenharmony_ci const int bufferLen = 128; 1518eace7efcSopenharmony_ci char paramOutBuf[bufferLen] = {0}; 1519eace7efcSopenharmony_ci const char *hook_mode = "startup:"; 1520eace7efcSopenharmony_ci int ret = GetParameter("libc.hook_mode", "", paramOutBuf, bufferLen - 1); 1521eace7efcSopenharmony_ci if (ret > 0 && strncmp(paramOutBuf, hook_mode, strlen(hook_mode)) == 0) { 1522eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Hook_mode: no timeoutTask"); 1523eace7efcSopenharmony_ci return; 1524eace7efcSopenharmony_ci } 1525eace7efcSopenharmony_ci 1526eace7efcSopenharmony_ci auto timeoutTask = [abilityRecord, connectManager = shared_from_this(), messageId]() { 1527eace7efcSopenharmony_ci if (messageId == AbilityManagerService::LOAD_TIMEOUT_MSG) { 1528eace7efcSopenharmony_ci connectManager->HandleStartTimeoutTask(abilityRecord); 1529eace7efcSopenharmony_ci } else if (messageId == AbilityConnectManager::CONNECT_TIMEOUT_MSG) { 1530eace7efcSopenharmony_ci connectManager->HandleConnectTimeoutTask(abilityRecord); 1531eace7efcSopenharmony_ci } 1532eace7efcSopenharmony_ci }; 1533eace7efcSopenharmony_ci taskHandler_->SubmitTask(timeoutTask, taskName, delayTime); 1534eace7efcSopenharmony_ci} 1535eace7efcSopenharmony_ci 1536eace7efcSopenharmony_civoid AbilityConnectManager::HandleStartTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord) 1537eace7efcSopenharmony_ci{ 1538eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "load timeout"); 1539eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1540eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1541eace7efcSopenharmony_ci abilityRecord->SetLoadState(AbilityLoadState::FAILED); 1542eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) { 1543eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "consume session timeout, Uri: %{public}s", abilityRecord->GetURI().c_str()); 1544eace7efcSopenharmony_ci if (uiExtensionAbilityRecordMgr_ != nullptr && IsCallerValid(abilityRecord)) { 1545eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "start load timeout"); 1546eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_->LoadTimeout(abilityRecord->GetUIExtensionAbilityId()); 1547eace7efcSopenharmony_ci } 1548eace7efcSopenharmony_ci } 1549eace7efcSopenharmony_ci auto connectingList = abilityRecord->GetConnectingRecordList(); 1550eace7efcSopenharmony_ci for (auto &connectRecord : connectingList) { 1551eace7efcSopenharmony_ci if (connectRecord == nullptr) { 1552eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "connectRecord null"); 1553eace7efcSopenharmony_ci continue; 1554eace7efcSopenharmony_ci } 1555eace7efcSopenharmony_ci connectRecord->CompleteDisconnect(ERR_OK, false, true); 1556eace7efcSopenharmony_ci abilityRecord->RemoveConnectRecordFromList(connectRecord); 1557eace7efcSopenharmony_ci RemoveConnectionRecordFromMap(connectRecord); 1558eace7efcSopenharmony_ci } 1559eace7efcSopenharmony_ci 1560eace7efcSopenharmony_ci if (GetExtensionByTokenFromServiceMap(abilityRecord->GetToken()) == nullptr) { 1561eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "timeout ability record not exist"); 1562eace7efcSopenharmony_ci return; 1563eace7efcSopenharmony_ci } 1564eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "AbilityUri:%{public}s,user:%{public}d", abilityRecord->GetURI().c_str(), userId_); 1565eace7efcSopenharmony_ci MoveToTerminatingMap(abilityRecord); 1566eace7efcSopenharmony_ci RemoveServiceAbility(abilityRecord); 1567eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->AttachTimeOut(abilityRecord->GetToken()); 1568eace7efcSopenharmony_ci if (abilityRecord->IsSceneBoard()) { 1569eace7efcSopenharmony_ci if (DelayedSingleton<AbilityManagerService>::GetInstance()->GetUserId() == userId_) { 1570eace7efcSopenharmony_ci RestartAbility(abilityRecord, userId_); 1571eace7efcSopenharmony_ci } 1572eace7efcSopenharmony_ci PrintTimeOutLog(abilityRecord, AbilityManagerService::LOAD_TIMEOUT_MSG); 1573eace7efcSopenharmony_ci return; 1574eace7efcSopenharmony_ci } 1575eace7efcSopenharmony_ci if (IsAbilityNeedKeepAlive(abilityRecord)) { 1576eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "load timeout"); 1577eace7efcSopenharmony_ci RestartAbility(abilityRecord, userId_); 1578eace7efcSopenharmony_ci } 1579eace7efcSopenharmony_ci} 1580eace7efcSopenharmony_ci 1581eace7efcSopenharmony_civoid AbilityConnectManager::HandleCommandTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord) 1582eace7efcSopenharmony_ci{ 1583eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1584eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().name == AbilityConfig::LAUNCHER_ABILITY_NAME) { 1585eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Handle root launcher command timeout."); 1586eace7efcSopenharmony_ci // terminate the timeout root launcher. 1587eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->AttachTimeOut(abilityRecord->GetToken()); 1588eace7efcSopenharmony_ci } 1589eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "HandleCommandTimeoutTask end"); 1590eace7efcSopenharmony_ci} 1591eace7efcSopenharmony_ci 1592eace7efcSopenharmony_civoid AbilityConnectManager::HandleConnectTimeoutTask(std::shared_ptr<AbilityRecord> abilityRecord) 1593eace7efcSopenharmony_ci{ 1594eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "connect timeout"); 1595eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1596eace7efcSopenharmony_ci auto connectList = abilityRecord->GetConnectRecordList(); 1597eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1598eace7efcSopenharmony_ci for (const auto &connectRecord : connectList) { 1599eace7efcSopenharmony_ci RemoveExtensionDelayDisconnectTask(connectRecord); 1600eace7efcSopenharmony_ci connectRecord->CancelConnectTimeoutTask(); 1601eace7efcSopenharmony_ci connectRecord->CompleteDisconnect(ERR_OK, false, true); 1602eace7efcSopenharmony_ci abilityRecord->RemoveConnectRecordFromList(connectRecord); 1603eace7efcSopenharmony_ci RemoveConnectionRecordFromMap(connectRecord); 1604eace7efcSopenharmony_ci } 1605eace7efcSopenharmony_ci 1606eace7efcSopenharmony_ci if (IsSpecialAbility(abilityRecord->GetAbilityInfo()) || abilityRecord->GetStartId() != 0) { 1607eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "no need terminate"); 1608eace7efcSopenharmony_ci return; 1609eace7efcSopenharmony_ci } 1610eace7efcSopenharmony_ci 1611eace7efcSopenharmony_ci TerminateRecord(abilityRecord); 1612eace7efcSopenharmony_ci} 1613eace7efcSopenharmony_ci 1614eace7efcSopenharmony_civoid AbilityConnectManager::HandleCommandWindowTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord, 1615eace7efcSopenharmony_ci const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd) 1616eace7efcSopenharmony_ci{ 1617eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "start"); 1618eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1619eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1620eace7efcSopenharmony_ci abilityRecord->SetAbilityWindowState(sessionInfo, winCmd, true); 1621eace7efcSopenharmony_ci // manage queued request 1622eace7efcSopenharmony_ci CompleteStartServiceReq(abilityRecord->GetURI()); 1623eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "end"); 1624eace7efcSopenharmony_ci} 1625eace7efcSopenharmony_ci 1626eace7efcSopenharmony_civoid AbilityConnectManager::HandleStopTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord) 1627eace7efcSopenharmony_ci{ 1628eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Complete stop ability timeout start."); 1629eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1630eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1631eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) { 1632eace7efcSopenharmony_ci if (uiExtensionAbilityRecordMgr_ != nullptr && IsCallerValid(abilityRecord)) { 1633eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "start terminate timeout"); 1634eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_->TerminateTimeout(abilityRecord->GetUIExtensionAbilityId()); 1635eace7efcSopenharmony_ci } 1636eace7efcSopenharmony_ci PrintTimeOutLog(abilityRecord, AbilityManagerService::TERMINATE_TIMEOUT_MSG); 1637eace7efcSopenharmony_ci } 1638eace7efcSopenharmony_ci TerminateDone(abilityRecord); 1639eace7efcSopenharmony_ci} 1640eace7efcSopenharmony_ci 1641eace7efcSopenharmony_civoid AbilityConnectManager::HandleTerminateDisconnectTask(const ConnectListType& connectlist) 1642eace7efcSopenharmony_ci{ 1643eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Disconnect ability when terminate."); 1644eace7efcSopenharmony_ci for (auto& connectRecord : connectlist) { 1645eace7efcSopenharmony_ci if (!connectRecord) { 1646eace7efcSopenharmony_ci continue; 1647eace7efcSopenharmony_ci } 1648eace7efcSopenharmony_ci auto targetService = connectRecord->GetAbilityRecord(); 1649eace7efcSopenharmony_ci if (targetService) { 1650eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "record complete disconnect. recordId:%{public}d", 1651eace7efcSopenharmony_ci connectRecord->GetRecordId()); 1652eace7efcSopenharmony_ci connectRecord->CompleteDisconnect(ERR_OK, false, true); 1653eace7efcSopenharmony_ci targetService->RemoveConnectRecordFromList(connectRecord); 1654eace7efcSopenharmony_ci RemoveConnectionRecordFromMap(connectRecord); 1655eace7efcSopenharmony_ci }; 1656eace7efcSopenharmony_ci } 1657eace7efcSopenharmony_ci} 1658eace7efcSopenharmony_ci 1659eace7efcSopenharmony_ciint AbilityConnectManager::DispatchInactive(const std::shared_ptr<AbilityRecord> &abilityRecord, int state) 1660eace7efcSopenharmony_ci{ 1661eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "DispatchInactive call"); 1662eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 1663eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(eventHandler_, ERR_INVALID_VALUE); 1664eace7efcSopenharmony_ci if (!abilityRecord->IsAbilityState(AbilityState::INACTIVATING)) { 1665eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, 1666eace7efcSopenharmony_ci "error. expect %{public}d, actual %{public}d callback %{public}d", 1667eace7efcSopenharmony_ci AbilityState::INACTIVATING, abilityRecord->GetAbilityState(), state); 1668eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 1669eace7efcSopenharmony_ci } 1670eace7efcSopenharmony_ci eventHandler_->RemoveEvent(AbilityManagerService::INACTIVE_TIMEOUT_MSG, abilityRecord->GetAbilityRecordId()); 1671eace7efcSopenharmony_ci 1672eace7efcSopenharmony_ci // complete inactive 1673eace7efcSopenharmony_ci abilityRecord->SetAbilityState(AbilityState::INACTIVE); 1674eace7efcSopenharmony_ci if (abilityRecord->IsCreateByConnect()) { 1675eace7efcSopenharmony_ci ConnectAbility(abilityRecord); 1676eace7efcSopenharmony_ci } else if (abilityRecord->GetWant().GetBoolParam(IS_PRELOAD_UIEXTENSION_ABILITY, false)) { 1677eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "IS_PRELOAD_UIEXTENSION_ABILITY"); 1678eace7efcSopenharmony_ci auto ret = uiExtensionAbilityRecordMgr_->AddPreloadUIExtensionRecord(abilityRecord); 1679eace7efcSopenharmony_ci if (ret != ERR_OK) { 1680eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "AddPreloadUIExtensionRecord error"); 1681eace7efcSopenharmony_ci return ret; 1682eace7efcSopenharmony_ci } 1683eace7efcSopenharmony_ci return ERR_OK; 1684eace7efcSopenharmony_ci } else { 1685eace7efcSopenharmony_ci CommandAbility(abilityRecord); 1686eace7efcSopenharmony_ci if (abilityRecord->GetConnectRecordList().size() > 0) { 1687eace7efcSopenharmony_ci // It means someone called connectAbility when service was loading 1688eace7efcSopenharmony_ci abilityRecord->UpdateConnectWant(); 1689eace7efcSopenharmony_ci ConnectAbility(abilityRecord); 1690eace7efcSopenharmony_ci } 1691eace7efcSopenharmony_ci } 1692eace7efcSopenharmony_ci 1693eace7efcSopenharmony_ci return ERR_OK; 1694eace7efcSopenharmony_ci} 1695eace7efcSopenharmony_ci 1696eace7efcSopenharmony_ciint AbilityConnectManager::DispatchForeground(const std::shared_ptr<AbilityRecord> &abilityRecord) 1697eace7efcSopenharmony_ci{ 1698eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 1699eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(taskHandler_, ERR_INVALID_VALUE); 1700eace7efcSopenharmony_ci // remove foreground timeout task. 1701eace7efcSopenharmony_ci abilityRecord->RemoveForegroundTimeoutTask(); 1702eace7efcSopenharmony_ci auto self(shared_from_this()); 1703eace7efcSopenharmony_ci auto task = [self, abilityRecord]() { self->CompleteForeground(abilityRecord); }; 1704eace7efcSopenharmony_ci taskHandler_->SubmitTask(task, TaskQoS::USER_INTERACTIVE); 1705eace7efcSopenharmony_ci 1706eace7efcSopenharmony_ci return ERR_OK; 1707eace7efcSopenharmony_ci} 1708eace7efcSopenharmony_ci 1709eace7efcSopenharmony_ciint AbilityConnectManager::DispatchBackground(const std::shared_ptr<AbilityRecord> &abilityRecord) 1710eace7efcSopenharmony_ci{ 1711eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 1712eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(taskHandler_, ERR_INVALID_VALUE); 1713eace7efcSopenharmony_ci // remove background timeout task. 1714eace7efcSopenharmony_ci taskHandler_->CancelTask("background_" + std::to_string(abilityRecord->GetAbilityRecordId())); 1715eace7efcSopenharmony_ci 1716eace7efcSopenharmony_ci auto self(shared_from_this()); 1717eace7efcSopenharmony_ci auto task = [self, abilityRecord]() { self->CompleteBackground(abilityRecord); }; 1718eace7efcSopenharmony_ci taskHandler_->SubmitTask(task, TaskQoS::USER_INTERACTIVE); 1719eace7efcSopenharmony_ci 1720eace7efcSopenharmony_ci return ERR_OK; 1721eace7efcSopenharmony_ci} 1722eace7efcSopenharmony_ci 1723eace7efcSopenharmony_ciint AbilityConnectManager::DispatchTerminate(const std::shared_ptr<AbilityRecord> &abilityRecord) 1724eace7efcSopenharmony_ci{ 1725eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); 1726eace7efcSopenharmony_ci // remove terminate timeout task 1727eace7efcSopenharmony_ci if (taskHandler_ != nullptr) { 1728eace7efcSopenharmony_ci taskHandler_->CancelTask("terminate_" + std::to_string(abilityRecord->GetAbilityRecordId())); 1729eace7efcSopenharmony_ci } 1730eace7efcSopenharmony_ci // complete terminate 1731eace7efcSopenharmony_ci TerminateDone(abilityRecord); 1732eace7efcSopenharmony_ci return ERR_OK; 1733eace7efcSopenharmony_ci} 1734eace7efcSopenharmony_ci 1735eace7efcSopenharmony_civoid AbilityConnectManager::ConnectAbility(const std::shared_ptr<AbilityRecord> &abilityRecord) 1736eace7efcSopenharmony_ci{ 1737eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1738eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1739eace7efcSopenharmony_ci AppExecFwk::ExtensionAbilityType extType = abilityRecord->GetAbilityInfo().extensionAbilityType; 1740eace7efcSopenharmony_ci if (extType == AppExecFwk::ExtensionAbilityType::UI_SERVICE) { 1741eace7efcSopenharmony_ci ResumeConnectAbility(abilityRecord); 1742eace7efcSopenharmony_ci } else { 1743eace7efcSopenharmony_ci PostTimeOutTask(abilityRecord, AbilityConnectManager::CONNECT_TIMEOUT_MSG); 1744eace7efcSopenharmony_ci abilityRecord->ConnectAbility(); 1745eace7efcSopenharmony_ci } 1746eace7efcSopenharmony_ci} 1747eace7efcSopenharmony_ci 1748eace7efcSopenharmony_civoid AbilityConnectManager::ConnectUIServiceExtAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, 1749eace7efcSopenharmony_ci int connectRecordId, const Want &want) 1750eace7efcSopenharmony_ci{ 1751eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1752eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1753eace7efcSopenharmony_ci PostTimeOutTask(abilityRecord, connectRecordId, AbilityConnectManager::CONNECT_TIMEOUT_MSG); 1754eace7efcSopenharmony_ci abilityRecord->ConnectAbilityWithWant(want); 1755eace7efcSopenharmony_ci} 1756eace7efcSopenharmony_ci 1757eace7efcSopenharmony_civoid AbilityConnectManager::ResumeConnectAbility(const std::shared_ptr<AbilityRecord> &abilityRecord) 1758eace7efcSopenharmony_ci{ 1759eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "ResumeConnectAbility"); 1760eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1761eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1762eace7efcSopenharmony_ci std::list<std::shared_ptr<ConnectionRecord>> connectingList = abilityRecord->GetConnectingRecordList(); 1763eace7efcSopenharmony_ci for (auto &connectRecord : connectingList) { 1764eace7efcSopenharmony_ci if (connectRecord == nullptr) { 1765eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "connectRecord null"); 1766eace7efcSopenharmony_ci continue; 1767eace7efcSopenharmony_ci } 1768eace7efcSopenharmony_ci int connectRecordId = connectRecord->GetRecordId(); 1769eace7efcSopenharmony_ci PostTimeOutTask(abilityRecord, connectRecordId, AbilityConnectManager::CONNECT_TIMEOUT_MSG); 1770eace7efcSopenharmony_ci abilityRecord->ConnectAbilityWithWant(connectRecord->GetConnectWant()); 1771eace7efcSopenharmony_ci } 1772eace7efcSopenharmony_ci} 1773eace7efcSopenharmony_ci 1774eace7efcSopenharmony_civoid AbilityConnectManager::CommandAbility(const std::shared_ptr<AbilityRecord> &abilityRecord) 1775eace7efcSopenharmony_ci{ 1776eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1777eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1778eace7efcSopenharmony_ci if (taskHandler_ != nullptr) { 1779eace7efcSopenharmony_ci // first connect ability, There is at most one connect record. 1780eace7efcSopenharmony_ci int recordId = abilityRecord->GetRecordId(); 1781eace7efcSopenharmony_ci abilityRecord->AddStartId(); 1782eace7efcSopenharmony_ci std::string taskName = std::string("CommandTimeout_") + std::to_string(recordId) + std::string("_") + 1783eace7efcSopenharmony_ci std::to_string(abilityRecord->GetStartId()); 1784eace7efcSopenharmony_ci auto timeoutTask = [abilityRecord, connectManager = shared_from_this()]() { 1785eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "command ability timeout. %{public}s", 1786eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().name.c_str()); 1787eace7efcSopenharmony_ci connectManager->HandleCommandTimeoutTask(abilityRecord); 1788eace7efcSopenharmony_ci }; 1789eace7efcSopenharmony_ci int commandTimeout = 1790eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetAppStartTimeoutTime() * COMMAND_TIMEOUT_MULTIPLE; 1791eace7efcSopenharmony_ci taskHandler_->SubmitTask(timeoutTask, taskName, commandTimeout); 1792eace7efcSopenharmony_ci // scheduling command ability 1793eace7efcSopenharmony_ci abilityRecord->CommandAbility(); 1794eace7efcSopenharmony_ci } 1795eace7efcSopenharmony_ci} 1796eace7efcSopenharmony_ci 1797eace7efcSopenharmony_civoid AbilityConnectManager::CommandAbilityWindow(const std::shared_ptr<AbilityRecord> &abilityRecord, 1798eace7efcSopenharmony_ci const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd) 1799eace7efcSopenharmony_ci{ 1800eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1801eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1802eace7efcSopenharmony_ci CHECK_POINTER(sessionInfo); 1803eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "ability: %{public}s, persistentId: %{private}d, wincmd: %{public}d", 1804eace7efcSopenharmony_ci abilityRecord->GetURI().c_str(), sessionInfo->persistentId, winCmd); 1805eace7efcSopenharmony_ci abilityRecord->SetAbilityWindowState(sessionInfo, winCmd, false); 1806eace7efcSopenharmony_ci if (taskHandler_ != nullptr) { 1807eace7efcSopenharmony_ci int recordId = abilityRecord->GetRecordId(); 1808eace7efcSopenharmony_ci std::string taskName = std::string("CommandWindowTimeout_") + std::to_string(recordId) + std::string("_") + 1809eace7efcSopenharmony_ci std::to_string(sessionInfo->persistentId) + std::string("_") + std::to_string(winCmd); 1810eace7efcSopenharmony_ci auto timeoutTask = [abilityRecord, sessionInfo, winCmd, connectManager = shared_from_this()]() { 1811eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "command window timeout. %{public}s", 1812eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().name.c_str()); 1813eace7efcSopenharmony_ci connectManager->HandleCommandWindowTimeoutTask(abilityRecord, sessionInfo, winCmd); 1814eace7efcSopenharmony_ci }; 1815eace7efcSopenharmony_ci int commandWindowTimeout = 1816eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetAppStartTimeoutTime() * COMMAND_WINDOW_TIMEOUT_MULTIPLE; 1817eace7efcSopenharmony_ci taskHandler_->SubmitTask(timeoutTask, taskName, commandWindowTimeout); 1818eace7efcSopenharmony_ci // scheduling command ability 1819eace7efcSopenharmony_ci abilityRecord->CommandAbilityWindow(sessionInfo, winCmd); 1820eace7efcSopenharmony_ci } 1821eace7efcSopenharmony_ci} 1822eace7efcSopenharmony_ci 1823eace7efcSopenharmony_civoid AbilityConnectManager::BackgroundAbilityWindowLocked(const std::shared_ptr<AbilityRecord> &abilityRecord, 1824eace7efcSopenharmony_ci const sptr<SessionInfo> &sessionInfo) 1825eace7efcSopenharmony_ci{ 1826eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1827eace7efcSopenharmony_ci DoBackgroundAbilityWindow(abilityRecord, sessionInfo); 1828eace7efcSopenharmony_ci} 1829eace7efcSopenharmony_ci 1830eace7efcSopenharmony_civoid AbilityConnectManager::DoBackgroundAbilityWindow(const std::shared_ptr<AbilityRecord> &abilityRecord, 1831eace7efcSopenharmony_ci const sptr<SessionInfo> &sessionInfo) 1832eace7efcSopenharmony_ci{ 1833eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1834eace7efcSopenharmony_ci CHECK_POINTER(sessionInfo); 1835eace7efcSopenharmony_ci auto abilitystateStr = abilityRecord->ConvertAbilityState(abilityRecord->GetAbilityState()); 1836eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, 1837eace7efcSopenharmony_ci "ability:%{public}s, persistentId:%{public}d, abilityState:%{public}s", 1838eace7efcSopenharmony_ci abilityRecord->GetURI().c_str(), sessionInfo->persistentId, abilitystateStr.c_str()); 1839eace7efcSopenharmony_ci if (abilityRecord->IsAbilityState(AbilityState::FOREGROUND)) { 1840eace7efcSopenharmony_ci MoveToBackground(abilityRecord); 1841eace7efcSopenharmony_ci } else if (abilityRecord->IsAbilityState(AbilityState::INITIAL) || 1842eace7efcSopenharmony_ci abilityRecord->IsAbilityState(AbilityState::FOREGROUNDING)) { 1843eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "exist initial or foregrounding task"); 1844eace7efcSopenharmony_ci abilityRecord->DoBackgroundAbilityWindowDelayed(true); 1845eace7efcSopenharmony_ci } else if (!abilityRecord->IsAbilityState(AbilityState::BACKGROUNDING)) { 1846eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "invalid ability state"); 1847eace7efcSopenharmony_ci } 1848eace7efcSopenharmony_ci} 1849eace7efcSopenharmony_ci 1850eace7efcSopenharmony_civoid AbilityConnectManager::TerminateAbilityWindowLocked(const std::shared_ptr<AbilityRecord> &abilityRecord, 1851eace7efcSopenharmony_ci const sptr<SessionInfo> &sessionInfo) 1852eace7efcSopenharmony_ci{ 1853eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1854eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1855eace7efcSopenharmony_ci CHECK_POINTER(sessionInfo); 1856eace7efcSopenharmony_ci auto abilitystateStr = abilityRecord->ConvertAbilityState(abilityRecord->GetAbilityState()); 1857eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, 1858eace7efcSopenharmony_ci "ability:%{public}s, persistentId:%{public}d, abilityState:%{public}s", 1859eace7efcSopenharmony_ci abilityRecord->GetURI().c_str(), sessionInfo->persistentId, abilitystateStr.c_str()); 1860eace7efcSopenharmony_ci EventInfo eventInfo; 1861eace7efcSopenharmony_ci eventInfo.bundleName = abilityRecord->GetAbilityInfo().bundleName; 1862eace7efcSopenharmony_ci eventInfo.abilityName = abilityRecord->GetAbilityInfo().name; 1863eace7efcSopenharmony_ci EventReport::SendAbilityEvent(EventName::TERMINATE_ABILITY, HiSysEventType::BEHAVIOR, eventInfo); 1864eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 1865eace7efcSopenharmony_ci eventInfo.errCode = TerminateAbilityInner(abilityRecord->GetToken()); 1866eace7efcSopenharmony_ci if (eventInfo.errCode != ERR_OK) { 1867eace7efcSopenharmony_ci EventReport::SendAbilityEvent(EventName::TERMINATE_ABILITY_ERROR, HiSysEventType::FAULT, eventInfo); 1868eace7efcSopenharmony_ci } 1869eace7efcSopenharmony_ci} 1870eace7efcSopenharmony_ci 1871eace7efcSopenharmony_civoid AbilityConnectManager::TerminateDone(const std::shared_ptr<AbilityRecord> &abilityRecord) 1872eace7efcSopenharmony_ci{ 1873eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 1874eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1875eace7efcSopenharmony_ci if (!abilityRecord->IsAbilityState(AbilityState::TERMINATING)) { 1876eace7efcSopenharmony_ci std::string expect = AbilityRecord::ConvertAbilityState(AbilityState::TERMINATING); 1877eace7efcSopenharmony_ci std::string actual = AbilityRecord::ConvertAbilityState(abilityRecord->GetAbilityState()); 1878eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, 1879eace7efcSopenharmony_ci "error. expect %{public}s, actual %{public}s", expect.c_str(), actual.c_str()); 1880eace7efcSopenharmony_ci return; 1881eace7efcSopenharmony_ci } 1882eace7efcSopenharmony_ci IN_PROCESS_CALL_WITHOUT_RET(abilityRecord->RevokeUriPermission()); 1883eace7efcSopenharmony_ci abilityRecord->RemoveAbilityDeathRecipient(); 1884eace7efcSopenharmony_ci if (abilityRecord->IsSceneBoard()) { 1885eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "scb exit, kill processes"); 1886eace7efcSopenharmony_ci KillProcessesByUserId(); 1887eace7efcSopenharmony_ci } 1888eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->TerminateAbility(abilityRecord->GetToken(), false); 1889eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) { 1890eace7efcSopenharmony_ci RemoveUIExtensionAbilityRecord(abilityRecord); 1891eace7efcSopenharmony_ci } 1892eace7efcSopenharmony_ci RemoveServiceAbility(abilityRecord); 1893eace7efcSopenharmony_ci} 1894eace7efcSopenharmony_ci 1895eace7efcSopenharmony_cibool AbilityConnectManager::IsAbilityConnected(const std::shared_ptr<AbilityRecord> &abilityRecord, 1896eace7efcSopenharmony_ci const std::list<std::shared_ptr<ConnectionRecord>> &connectRecordList) 1897eace7efcSopenharmony_ci{ 1898eace7efcSopenharmony_ci auto isMatch = [abilityRecord](auto connectRecord) -> bool { 1899eace7efcSopenharmony_ci if (abilityRecord == nullptr || connectRecord == nullptr) { 1900eace7efcSopenharmony_ci return false; 1901eace7efcSopenharmony_ci } 1902eace7efcSopenharmony_ci if (abilityRecord != connectRecord->GetAbilityRecord()) { 1903eace7efcSopenharmony_ci return false; 1904eace7efcSopenharmony_ci } 1905eace7efcSopenharmony_ci return true; 1906eace7efcSopenharmony_ci }; 1907eace7efcSopenharmony_ci return std::any_of(connectRecordList.begin(), connectRecordList.end(), isMatch); 1908eace7efcSopenharmony_ci} 1909eace7efcSopenharmony_ci 1910eace7efcSopenharmony_civoid AbilityConnectManager::RemoveConnectionRecordFromMap(std::shared_ptr<ConnectionRecord> connection) 1911eace7efcSopenharmony_ci{ 1912eace7efcSopenharmony_ci std::lock_guard lock(connectMapMutex_); 1913eace7efcSopenharmony_ci for (auto &connectCallback : connectMap_) { 1914eace7efcSopenharmony_ci auto &connectList = connectCallback.second; 1915eace7efcSopenharmony_ci auto connectRecord = std::find(connectList.begin(), connectList.end(), connection); 1916eace7efcSopenharmony_ci if (connectRecord != connectList.end()) { 1917eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "connrecord(%{public}d)", (*connectRecord)->GetRecordId()); 1918eace7efcSopenharmony_ci connectList.remove(connection); 1919eace7efcSopenharmony_ci if (connectList.empty()) { 1920eace7efcSopenharmony_ci RemoveConnectDeathRecipient(connectCallback.first); 1921eace7efcSopenharmony_ci connectMap_.erase(connectCallback.first); 1922eace7efcSopenharmony_ci } 1923eace7efcSopenharmony_ci return; 1924eace7efcSopenharmony_ci } 1925eace7efcSopenharmony_ci } 1926eace7efcSopenharmony_ci} 1927eace7efcSopenharmony_ci 1928eace7efcSopenharmony_civoid AbilityConnectManager::RemoveServiceAbility(const std::shared_ptr<AbilityRecord> &abilityRecord) 1929eace7efcSopenharmony_ci{ 1930eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 1931eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Remove service(%{public}s) from terminating map.", abilityRecord->GetURI().c_str()); 1932eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 1933eace7efcSopenharmony_ci terminatingExtensionList_.remove(abilityRecord); 1934eace7efcSopenharmony_ci} 1935eace7efcSopenharmony_ci 1936eace7efcSopenharmony_civoid AbilityConnectManager::AddConnectDeathRecipient(sptr<IRemoteObject> connectObject) 1937eace7efcSopenharmony_ci{ 1938eace7efcSopenharmony_ci CHECK_POINTER(connectObject); 1939eace7efcSopenharmony_ci { 1940eace7efcSopenharmony_ci std::lock_guard guard(recipientMapMutex_); 1941eace7efcSopenharmony_ci auto it = recipientMap_.find(connectObject); 1942eace7efcSopenharmony_ci if (it != recipientMap_.end()) { 1943eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "recipient added before"); 1944eace7efcSopenharmony_ci return; 1945eace7efcSopenharmony_ci } 1946eace7efcSopenharmony_ci } 1947eace7efcSopenharmony_ci 1948eace7efcSopenharmony_ci std::weak_ptr<AbilityConnectManager> thisWeakPtr(shared_from_this()); 1949eace7efcSopenharmony_ci sptr<IRemoteObject::DeathRecipient> deathRecipient = 1950eace7efcSopenharmony_ci new AbilityConnectCallbackRecipient([thisWeakPtr](const wptr<IRemoteObject> &remote) { 1951eace7efcSopenharmony_ci auto abilityConnectManager = thisWeakPtr.lock(); 1952eace7efcSopenharmony_ci if (abilityConnectManager) { 1953eace7efcSopenharmony_ci abilityConnectManager->OnCallBackDied(remote); 1954eace7efcSopenharmony_ci } 1955eace7efcSopenharmony_ci }); 1956eace7efcSopenharmony_ci if (!connectObject->AddDeathRecipient(deathRecipient)) { 1957eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "AddDeathRecipient fail"); 1958eace7efcSopenharmony_ci return; 1959eace7efcSopenharmony_ci } 1960eace7efcSopenharmony_ci std::lock_guard guard(recipientMapMutex_); 1961eace7efcSopenharmony_ci recipientMap_.emplace(connectObject, deathRecipient); 1962eace7efcSopenharmony_ci} 1963eace7efcSopenharmony_ci 1964eace7efcSopenharmony_civoid AbilityConnectManager::RemoveConnectDeathRecipient(sptr<IRemoteObject> connectObject) 1965eace7efcSopenharmony_ci{ 1966eace7efcSopenharmony_ci CHECK_POINTER(connectObject); 1967eace7efcSopenharmony_ci sptr<IRemoteObject::DeathRecipient> deathRecipient; 1968eace7efcSopenharmony_ci { 1969eace7efcSopenharmony_ci std::lock_guard guard(recipientMapMutex_); 1970eace7efcSopenharmony_ci auto it = recipientMap_.find(connectObject); 1971eace7efcSopenharmony_ci if (it == recipientMap_.end()) { 1972eace7efcSopenharmony_ci return; 1973eace7efcSopenharmony_ci } 1974eace7efcSopenharmony_ci deathRecipient = it->second; 1975eace7efcSopenharmony_ci recipientMap_.erase(it); 1976eace7efcSopenharmony_ci } 1977eace7efcSopenharmony_ci 1978eace7efcSopenharmony_ci connectObject->RemoveDeathRecipient(deathRecipient); 1979eace7efcSopenharmony_ci} 1980eace7efcSopenharmony_ci 1981eace7efcSopenharmony_civoid AbilityConnectManager::OnCallBackDied(const wptr<IRemoteObject> &remote) 1982eace7efcSopenharmony_ci{ 1983eace7efcSopenharmony_ci auto object = remote.promote(); 1984eace7efcSopenharmony_ci CHECK_POINTER(object); 1985eace7efcSopenharmony_ci if (taskHandler_) { 1986eace7efcSopenharmony_ci auto task = [object, connectManager = shared_from_this()]() { connectManager->HandleCallBackDiedTask(object); }; 1987eace7efcSopenharmony_ci taskHandler_->SubmitTask(task, TASK_ON_CALLBACK_DIED); 1988eace7efcSopenharmony_ci } 1989eace7efcSopenharmony_ci} 1990eace7efcSopenharmony_ci 1991eace7efcSopenharmony_civoid AbilityConnectManager::HandleCallBackDiedTask(const sptr<IRemoteObject> &connect) 1992eace7efcSopenharmony_ci{ 1993eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); 1994eace7efcSopenharmony_ci CHECK_POINTER(connect); 1995eace7efcSopenharmony_ci { 1996eace7efcSopenharmony_ci std::lock_guard guard(windowExtensionMapMutex_); 1997eace7efcSopenharmony_ci auto item = windowExtensionMap_.find(connect); 1998eace7efcSopenharmony_ci if (item != windowExtensionMap_.end()) { 1999eace7efcSopenharmony_ci windowExtensionMap_.erase(item); 2000eace7efcSopenharmony_ci } 2001eace7efcSopenharmony_ci } 2002eace7efcSopenharmony_ci 2003eace7efcSopenharmony_ci { 2004eace7efcSopenharmony_ci std::lock_guard guard(connectMapMutex_); 2005eace7efcSopenharmony_ci auto it = connectMap_.find(connect); 2006eace7efcSopenharmony_ci if (it != connectMap_.end()) { 2007eace7efcSopenharmony_ci ConnectListType connectRecordList = it->second; 2008eace7efcSopenharmony_ci for (auto &connRecord : connectRecordList) { 2009eace7efcSopenharmony_ci connRecord->ClearConnCallBack(); 2010eace7efcSopenharmony_ci } 2011eace7efcSopenharmony_ci } else { 2012eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "not find"); 2013eace7efcSopenharmony_ci return; 2014eace7efcSopenharmony_ci } 2015eace7efcSopenharmony_ci } 2016eace7efcSopenharmony_ci 2017eace7efcSopenharmony_ci sptr<IAbilityConnection> object = iface_cast<IAbilityConnection>(connect); 2018eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 2019eace7efcSopenharmony_ci DisconnectAbilityLocked(object, true); 2020eace7efcSopenharmony_ci} 2021eace7efcSopenharmony_ci 2022eace7efcSopenharmony_ciint32_t AbilityConnectManager::GetActiveUIExtensionList( 2023eace7efcSopenharmony_ci const int32_t pid, std::vector<std::string> &extensionList) 2024eace7efcSopenharmony_ci{ 2025eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, ERR_NULL_OBJECT); 2026eace7efcSopenharmony_ci return uiExtensionAbilityRecordMgr_->GetActiveUIExtensionList(pid, extensionList); 2027eace7efcSopenharmony_ci} 2028eace7efcSopenharmony_ci 2029eace7efcSopenharmony_ciint32_t AbilityConnectManager::GetActiveUIExtensionList( 2030eace7efcSopenharmony_ci const std::string &bundleName, std::vector<std::string> &extensionList) 2031eace7efcSopenharmony_ci{ 2032eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, ERR_NULL_OBJECT); 2033eace7efcSopenharmony_ci return uiExtensionAbilityRecordMgr_->GetActiveUIExtensionList(bundleName, extensionList); 2034eace7efcSopenharmony_ci} 2035eace7efcSopenharmony_ci 2036eace7efcSopenharmony_civoid AbilityConnectManager::OnAbilityDied(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId) 2037eace7efcSopenharmony_ci{ 2038eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2039eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "on ability died: %{public}s", abilityRecord->GetURI().c_str()); 2040eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().type != AbilityType::SERVICE && 2041eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().type != AbilityType::EXTENSION) { 2042eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "type not service"); 2043eace7efcSopenharmony_ci return; 2044eace7efcSopenharmony_ci } 2045eace7efcSopenharmony_ci if (eventHandler_ && abilityRecord->GetAbilityState() == AbilityState::INITIAL) { 2046eace7efcSopenharmony_ci abilityRecord->RemoveLoadTimeoutTask(); 2047eace7efcSopenharmony_ci } 2048eace7efcSopenharmony_ci if (eventHandler_ && abilityRecord->GetAbilityState() == AbilityState::FOREGROUNDING) { 2049eace7efcSopenharmony_ci abilityRecord->RemoveForegroundTimeoutTask(); 2050eace7efcSopenharmony_ci } 2051eace7efcSopenharmony_ci if (taskHandler_ && abilityRecord->GetAbilityState() == AbilityState::BACKGROUNDING) { 2052eace7efcSopenharmony_ci taskHandler_->CancelTask("background_" + std::to_string(abilityRecord->GetAbilityRecordId())); 2053eace7efcSopenharmony_ci } 2054eace7efcSopenharmony_ci if (taskHandler_ && abilityRecord->GetAbilityState() == AbilityState::TERMINATING) { 2055eace7efcSopenharmony_ci taskHandler_->CancelTask("terminate_" + std::to_string(abilityRecord->GetAbilityRecordId())); 2056eace7efcSopenharmony_ci } 2057eace7efcSopenharmony_ci if (taskHandler_) { 2058eace7efcSopenharmony_ci auto task = [abilityRecord, connectManager = shared_from_this(), currentUserId]() { 2059eace7efcSopenharmony_ci connectManager->HandleAbilityDiedTask(abilityRecord, currentUserId); 2060eace7efcSopenharmony_ci }; 2061eace7efcSopenharmony_ci taskHandler_->SubmitTask(task, TASK_ON_ABILITY_DIED); 2062eace7efcSopenharmony_ci } 2063eace7efcSopenharmony_ci} 2064eace7efcSopenharmony_ci 2065eace7efcSopenharmony_civoid AbilityConnectManager::OnTimeOut(uint32_t msgId, int64_t abilityRecordId, bool isHalf) 2066eace7efcSopenharmony_ci{ 2067eace7efcSopenharmony_ci auto abilityRecord = GetExtensionByIdFromServiceMap(abilityRecordId); 2068eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 2069eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityRecord"); 2070eace7efcSopenharmony_ci return; 2071eace7efcSopenharmony_ci } 2072eace7efcSopenharmony_ci PrintTimeOutLog(abilityRecord, msgId, isHalf); 2073eace7efcSopenharmony_ci if (isHalf) { 2074eace7efcSopenharmony_ci return; 2075eace7efcSopenharmony_ci } 2076eace7efcSopenharmony_ci switch (msgId) { 2077eace7efcSopenharmony_ci case AbilityManagerService::LOAD_TIMEOUT_MSG: 2078eace7efcSopenharmony_ci HandleStartTimeoutTask(abilityRecord); 2079eace7efcSopenharmony_ci break; 2080eace7efcSopenharmony_ci case AbilityManagerService::INACTIVE_TIMEOUT_MSG: 2081eace7efcSopenharmony_ci HandleInactiveTimeout(abilityRecord); 2082eace7efcSopenharmony_ci break; 2083eace7efcSopenharmony_ci case AbilityManagerService::FOREGROUND_TIMEOUT_MSG: 2084eace7efcSopenharmony_ci HandleForegroundTimeoutTask(abilityRecord); 2085eace7efcSopenharmony_ci break; 2086eace7efcSopenharmony_ci default: 2087eace7efcSopenharmony_ci break; 2088eace7efcSopenharmony_ci } 2089eace7efcSopenharmony_ci} 2090eace7efcSopenharmony_ci 2091eace7efcSopenharmony_civoid AbilityConnectManager::HandleInactiveTimeout(const std::shared_ptr<AbilityRecord> &ability) 2092eace7efcSopenharmony_ci{ 2093eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "HandleInactiveTimeout start"); 2094eace7efcSopenharmony_ci CHECK_POINTER(ability); 2095eace7efcSopenharmony_ci if (ability->GetAbilityInfo().name == AbilityConfig::LAUNCHER_ABILITY_NAME) { 2096eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Handle root launcher inactive timeout."); 2097eace7efcSopenharmony_ci // terminate the timeout root launcher. 2098eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->AttachTimeOut(ability->GetToken()); 2099eace7efcSopenharmony_ci } 2100eace7efcSopenharmony_ci if (ability->GetAbilityInfo().name == AbilityConfig::CALLUI_ABILITY_NAME && ability->GetStartId() == 0) { 2101eace7efcSopenharmony_ci HandleConnectTimeoutTask(ability); 2102eace7efcSopenharmony_ci EventInfo eventInfo; 2103eace7efcSopenharmony_ci eventInfo.userId = userId_; 2104eace7efcSopenharmony_ci eventInfo.bundleName = ability->GetAbilityInfo().bundleName; 2105eace7efcSopenharmony_ci eventInfo.moduleName = ability->GetAbilityInfo().moduleName; 2106eace7efcSopenharmony_ci eventInfo.abilityName = ability->GetAbilityInfo().name; 2107eace7efcSopenharmony_ci eventInfo.abilityName = ability->GetAbilityInfo().name; 2108eace7efcSopenharmony_ci eventInfo.errCode = CONNECTION_TIMEOUT; 2109eace7efcSopenharmony_ci EventReport::SendExtensionEvent(EventName::CONNECT_SERVICE_ERROR, HiSysEventType::FAULT, eventInfo); 2110eace7efcSopenharmony_ci } 2111eace7efcSopenharmony_ci 2112eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "HandleInactiveTimeout end"); 2113eace7efcSopenharmony_ci} 2114eace7efcSopenharmony_ci 2115eace7efcSopenharmony_cibool AbilityConnectManager::IsAbilityNeedKeepAlive(const std::shared_ptr<AbilityRecord> &abilityRecord) 2116eace7efcSopenharmony_ci{ 2117eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 2118eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, false); 2119eace7efcSopenharmony_ci const auto &abilityInfo = abilityRecord->GetAbilityInfo(); 2120eace7efcSopenharmony_ci if (IsSpecialAbility(abilityInfo)) { 2121eace7efcSopenharmony_ci return true; 2122eace7efcSopenharmony_ci } 2123eace7efcSopenharmony_ci 2124eace7efcSopenharmony_ci return abilityRecord->IsKeepAliveBundle(); 2125eace7efcSopenharmony_ci} 2126eace7efcSopenharmony_ci 2127eace7efcSopenharmony_civoid AbilityConnectManager::ClearPreloadUIExtensionRecord(const std::shared_ptr<AbilityRecord> &abilityRecord) 2128eace7efcSopenharmony_ci{ 2129eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); 2130eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2131eace7efcSopenharmony_ci auto extensionRecordId = abilityRecord->GetUIExtensionAbilityId(); 2132eace7efcSopenharmony_ci std::string hostBundleName; 2133eace7efcSopenharmony_ci auto ret = uiExtensionAbilityRecordMgr_->GetHostBundleNameForExtensionId(extensionRecordId, hostBundleName); 2134eace7efcSopenharmony_ci if (ret != ERR_OK) { 2135eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "GetHostBundleNameForExtensionId fail"); 2136eace7efcSopenharmony_ci return; 2137eace7efcSopenharmony_ci } 2138eace7efcSopenharmony_ci auto extensionRecordMapKey = std::make_tuple(abilityRecord->GetWant().GetElement().GetAbilityName(), 2139eace7efcSopenharmony_ci abilityRecord->GetWant().GetElement().GetBundleName(), 2140eace7efcSopenharmony_ci abilityRecord->GetWant().GetElement().GetModuleName(), hostBundleName); 2141eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_->RemovePreloadUIExtensionRecordById(extensionRecordMapKey, extensionRecordId); 2142eace7efcSopenharmony_ci} 2143eace7efcSopenharmony_ci 2144eace7efcSopenharmony_civoid AbilityConnectManager::KeepAbilityAlive(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId) 2145eace7efcSopenharmony_ci{ 2146eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2147eace7efcSopenharmony_ci auto abilityInfo = abilityRecord->GetAbilityInfo(); 2148eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "restart ability, bundleName: %{public}s, abilityName: %{public}s", 2149eace7efcSopenharmony_ci abilityInfo.bundleName.c_str(), abilityInfo.name.c_str()); 2150eace7efcSopenharmony_ci auto token = abilityRecord->GetToken(); 2151eace7efcSopenharmony_ci if ((IsLauncher(abilityRecord) || abilityRecord->IsSceneBoard()) && token != nullptr) { 2152eace7efcSopenharmony_ci IN_PROCESS_CALL_WITHOUT_RET(DelayedSingleton<AppScheduler>::GetInstance()->ClearProcessByToken( 2153eace7efcSopenharmony_ci token->AsObject())); 2154eace7efcSopenharmony_ci if (abilityRecord->IsSceneBoard() && currentUserId != userId_) { 2155eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "not current user's SCB, clear user and not restart"); 2156eace7efcSopenharmony_ci KillProcessesByUserId(); 2157eace7efcSopenharmony_ci return; 2158eace7efcSopenharmony_ci } 2159eace7efcSopenharmony_ci } 2160eace7efcSopenharmony_ci 2161eace7efcSopenharmony_ci if (userId_ != USER_ID_NO_HEAD && userId_ != currentUserId) { 2162eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "Not current user's ability"); 2163eace7efcSopenharmony_ci return; 2164eace7efcSopenharmony_ci } 2165eace7efcSopenharmony_ci 2166eace7efcSopenharmony_ci if (abilityRecord->IsSceneBoard() && AmsConfigurationParameter::GetInstance().IsSupportSCBCrashReboot()) { 2167eace7efcSopenharmony_ci static int sceneBoardCrashCount = 0; 2168eace7efcSopenharmony_ci static int64_t tickCount = GetTickCount(); 2169eace7efcSopenharmony_ci int64_t tickNow = GetTickCount(); 2170eace7efcSopenharmony_ci const int64_t maxTime = 240000; // 240000 4min 2171eace7efcSopenharmony_ci const int maxCount = 4; // 4: crash happened 4 times during 4 mins 2172eace7efcSopenharmony_ci if (tickNow - tickCount > maxTime) { 2173eace7efcSopenharmony_ci sceneBoardCrashCount = 0; 2174eace7efcSopenharmony_ci tickCount = tickNow; 2175eace7efcSopenharmony_ci } 2176eace7efcSopenharmony_ci ++sceneBoardCrashCount; 2177eace7efcSopenharmony_ci if (sceneBoardCrashCount >= maxCount) { 2178eace7efcSopenharmony_ci std::string reason = "SceneBoard exits " + std::to_string(sceneBoardCrashCount) + 2179eace7efcSopenharmony_ci "times in " + std::to_string(maxTime) + "ms"; 2180eace7efcSopenharmony_ci DoRebootExt("panic", reason.c_str()); 2181eace7efcSopenharmony_ci } 2182eace7efcSopenharmony_ci } 2183eace7efcSopenharmony_ci 2184eace7efcSopenharmony_ci if (DelayedSingleton<AppScheduler>::GetInstance()->IsKilledForUpgradeWeb(abilityInfo.bundleName)) { 2185eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "bundle killed"); 2186eace7efcSopenharmony_ci return; 2187eace7efcSopenharmony_ci } 2188eace7efcSopenharmony_ci if (DelayedSingleton<AppScheduler>::GetInstance()->IsMemorySizeSufficent() || 2189eace7efcSopenharmony_ci IsLauncher(abilityRecord) || abilityRecord->IsSceneBoard() || 2190eace7efcSopenharmony_ci AppUtils::GetInstance().IsAllowResidentInExtremeMemory(abilityInfo.bundleName, abilityInfo.name)) { 2191eace7efcSopenharmony_ci RestartAbility(abilityRecord, currentUserId); 2192eace7efcSopenharmony_ci } 2193eace7efcSopenharmony_ci} 2194eace7efcSopenharmony_ci 2195eace7efcSopenharmony_civoid AbilityConnectManager::HandleAbilityDiedTask( 2196eace7efcSopenharmony_ci const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId) 2197eace7efcSopenharmony_ci{ 2198eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); 2199eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 2200eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2201eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "ability died: %{public}s", abilityRecord->GetURI().c_str()); 2202eace7efcSopenharmony_ci abilityRecord->SetConnRemoteObject(nullptr); 2203eace7efcSopenharmony_ci ConnectListType connlist = abilityRecord->GetConnectRecordList(); 2204eace7efcSopenharmony_ci for (auto &connectRecord : connlist) { 2205eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "record complete disconnect. recordId:%{public}d", 2206eace7efcSopenharmony_ci connectRecord->GetRecordId()); 2207eace7efcSopenharmony_ci RemoveExtensionDelayDisconnectTask(connectRecord); 2208eace7efcSopenharmony_ci connectRecord->CompleteDisconnect(ERR_OK, false, true); 2209eace7efcSopenharmony_ci abilityRecord->RemoveConnectRecordFromList(connectRecord); 2210eace7efcSopenharmony_ci RemoveConnectionRecordFromMap(connectRecord); 2211eace7efcSopenharmony_ci } 2212eace7efcSopenharmony_ci 2213eace7efcSopenharmony_ci if (IsUIExtensionAbility(abilityRecord)) { 2214eace7efcSopenharmony_ci HandleUIExtensionDied(abilityRecord); 2215eace7efcSopenharmony_ci } 2216eace7efcSopenharmony_ci 2217eace7efcSopenharmony_ci std::string serviceKey = GetServiceKey(abilityRecord); 2218eace7efcSopenharmony_ci if (GetServiceRecordByElementName(serviceKey) == nullptr) { 2219eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "%{public}s ability record is not exist in service map.", serviceKey.c_str()); 2220eace7efcSopenharmony_ci return; 2221eace7efcSopenharmony_ci } 2222eace7efcSopenharmony_ci 2223eace7efcSopenharmony_ci bool isRemove = false; 2224eace7efcSopenharmony_ci if (IsCacheExtensionAbilityType(abilityRecord) && 2225eace7efcSopenharmony_ci AbilityCacheManager::GetInstance().FindRecordByToken(abilityRecord->GetToken()) != nullptr) { 2226eace7efcSopenharmony_ci AbilityCacheManager::GetInstance().Remove(abilityRecord); 2227eace7efcSopenharmony_ci MoveToTerminatingMap(abilityRecord); 2228eace7efcSopenharmony_ci RemoveServiceAbility(abilityRecord); 2229eace7efcSopenharmony_ci isRemove = true; 2230eace7efcSopenharmony_ci } else if (GetExtensionByIdFromServiceMap(abilityRecord->GetAbilityRecordId()) != nullptr) { 2231eace7efcSopenharmony_ci MoveToTerminatingMap(abilityRecord); 2232eace7efcSopenharmony_ci RemoveServiceAbility(abilityRecord); 2233eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) { 2234eace7efcSopenharmony_ci RemoveUIExtensionAbilityRecord(abilityRecord); 2235eace7efcSopenharmony_ci } 2236eace7efcSopenharmony_ci isRemove = true; 2237eace7efcSopenharmony_ci } 2238eace7efcSopenharmony_ci 2239eace7efcSopenharmony_ci if (IsAbilityNeedKeepAlive(abilityRecord)) { 2240eace7efcSopenharmony_ci KeepAbilityAlive(abilityRecord, currentUserId); 2241eace7efcSopenharmony_ci } else { 2242eace7efcSopenharmony_ci if (isRemove) { 2243eace7efcSopenharmony_ci HandleNotifyAssertFaultDialogDied(abilityRecord); 2244eace7efcSopenharmony_ci } 2245eace7efcSopenharmony_ci } 2246eace7efcSopenharmony_ci} 2247eace7efcSopenharmony_ci 2248eace7efcSopenharmony_cistatic bool CheckIsNumString(const std::string &numStr) 2249eace7efcSopenharmony_ci{ 2250eace7efcSopenharmony_ci const std::regex regexJsperf(R"(^\d*)"); 2251eace7efcSopenharmony_ci std::match_results<std::string::const_iterator> matchResults; 2252eace7efcSopenharmony_ci if (numStr.empty() || !std::regex_match(numStr, matchResults, regexJsperf)) { 2253eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "error, %{public}s", numStr.c_str()); 2254eace7efcSopenharmony_ci return false; 2255eace7efcSopenharmony_ci } 2256eace7efcSopenharmony_ci if (MAX_UINT64_VALUE.length() < numStr.length() || 2257eace7efcSopenharmony_ci (MAX_UINT64_VALUE.length() == numStr.length() && MAX_UINT64_VALUE.compare(numStr) < 0)) { 2258eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "error, %{public}s", numStr.c_str()); 2259eace7efcSopenharmony_ci return false; 2260eace7efcSopenharmony_ci } 2261eace7efcSopenharmony_ci 2262eace7efcSopenharmony_ci return true; 2263eace7efcSopenharmony_ci} 2264eace7efcSopenharmony_ci 2265eace7efcSopenharmony_civoid AbilityConnectManager::HandleNotifyAssertFaultDialogDied(const std::shared_ptr<AbilityRecord> &abilityRecord) 2266eace7efcSopenharmony_ci{ 2267eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); 2268eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2269eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().name != ABILITY_NAME_ASSERT_FAULT_DIALOG || 2270eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().bundleName != BUNDLE_NAME_DIALOG) { 2271eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "not assert fault dialog"); 2272eace7efcSopenharmony_ci return; 2273eace7efcSopenharmony_ci } 2274eace7efcSopenharmony_ci 2275eace7efcSopenharmony_ci auto want = abilityRecord->GetWant(); 2276eace7efcSopenharmony_ci auto assertSessionStr = want.GetStringParam(Want::PARAM_ASSERT_FAULT_SESSION_ID); 2277eace7efcSopenharmony_ci if (!CheckIsNumString(assertSessionStr)) { 2278eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "assertSessionStr not number"); 2279eace7efcSopenharmony_ci return; 2280eace7efcSopenharmony_ci } 2281eace7efcSopenharmony_ci 2282eace7efcSopenharmony_ci auto callbackDeathMgr = DelayedSingleton<AbilityRuntime::AssertFaultCallbackDeathMgr>::GetInstance(); 2283eace7efcSopenharmony_ci if (callbackDeathMgr == nullptr) { 2284eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "null callbackDeathMgr"); 2285eace7efcSopenharmony_ci return; 2286eace7efcSopenharmony_ci } 2287eace7efcSopenharmony_ci callbackDeathMgr->CallAssertFaultCallback(std::stoull(assertSessionStr)); 2288eace7efcSopenharmony_ci} 2289eace7efcSopenharmony_ci 2290eace7efcSopenharmony_civoid AbilityConnectManager::CloseAssertDialog(const std::string &assertSessionId) 2291eace7efcSopenharmony_ci{ 2292eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Called"); 2293eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord = nullptr; 2294eace7efcSopenharmony_ci { 2295eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 2296eace7efcSopenharmony_ci for (const auto &item : serviceMap_) { 2297eace7efcSopenharmony_ci if (item.second == nullptr) { 2298eace7efcSopenharmony_ci continue; 2299eace7efcSopenharmony_ci } 2300eace7efcSopenharmony_ci 2301eace7efcSopenharmony_ci auto assertSessionStr = item.second->GetWant().GetStringParam(Want::PARAM_ASSERT_FAULT_SESSION_ID); 2302eace7efcSopenharmony_ci if (assertSessionStr == assertSessionId) { 2303eace7efcSopenharmony_ci abilityRecord = item.second; 2304eace7efcSopenharmony_ci serviceMap_.erase(item.first); 2305eace7efcSopenharmony_ci break; 2306eace7efcSopenharmony_ci } 2307eace7efcSopenharmony_ci } 2308eace7efcSopenharmony_ci } 2309eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 2310eace7efcSopenharmony_ci abilityRecord = AbilityCacheManager::GetInstance().FindRecordBySessionId(assertSessionId); 2311eace7efcSopenharmony_ci AbilityCacheManager::GetInstance().Remove(abilityRecord); 2312eace7efcSopenharmony_ci } 2313eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 2314eace7efcSopenharmony_ci return; 2315eace7efcSopenharmony_ci } 2316eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Terminate assert fault dialog"); 2317eace7efcSopenharmony_ci terminatingExtensionList_.push_back(abilityRecord); 2318eace7efcSopenharmony_ci sptr<IRemoteObject> token = abilityRecord->GetToken(); 2319eace7efcSopenharmony_ci if (token != nullptr) { 2320eace7efcSopenharmony_ci std::lock_guard lock(serialMutex_); 2321eace7efcSopenharmony_ci TerminateAbilityLocked(token); 2322eace7efcSopenharmony_ci } 2323eace7efcSopenharmony_ci} 2324eace7efcSopenharmony_ci 2325eace7efcSopenharmony_civoid AbilityConnectManager::HandleUIExtensionDied(const std::shared_ptr<AbilityRecord> &abilityRecord) 2326eace7efcSopenharmony_ci{ 2327eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); 2328eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2329eace7efcSopenharmony_ci std::lock_guard guard(uiExtensionMapMutex_); 2330eace7efcSopenharmony_ci for (auto it = uiExtensionMap_.begin(); it != uiExtensionMap_.end();) { 2331eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> uiExtAbility = it->second.first.lock(); 2332eace7efcSopenharmony_ci if (uiExtAbility == nullptr) { 2333eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "uiExtAbility null"); 2334eace7efcSopenharmony_ci RemoveUIExtWindowDeathRecipient(it->first); 2335eace7efcSopenharmony_ci it = uiExtensionMap_.erase(it); 2336eace7efcSopenharmony_ci continue; 2337eace7efcSopenharmony_ci } 2338eace7efcSopenharmony_ci 2339eace7efcSopenharmony_ci if (abilityRecord == uiExtAbility) { 2340eace7efcSopenharmony_ci sptr<Rosen::ISession> sessionProxy = iface_cast<Rosen::ISession>(it->first); 2341eace7efcSopenharmony_ci if (sessionProxy) { 2342eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "start NotifyExtensionDied"); 2343eace7efcSopenharmony_ci sessionProxy->NotifyExtensionDied(); 2344eace7efcSopenharmony_ci } 2345eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::UI_EXT, "uiExtAbility died"); 2346eace7efcSopenharmony_ci RemoveUIExtWindowDeathRecipient(it->first); 2347eace7efcSopenharmony_ci it = uiExtensionMap_.erase(it); 2348eace7efcSopenharmony_ci continue; 2349eace7efcSopenharmony_ci } 2350eace7efcSopenharmony_ci ++it; 2351eace7efcSopenharmony_ci } 2352eace7efcSopenharmony_ci} 2353eace7efcSopenharmony_ci 2354eace7efcSopenharmony_civoid AbilityConnectManager::RestartAbility(const std::shared_ptr<AbilityRecord> &abilityRecord, int32_t currentUserId) 2355eace7efcSopenharmony_ci{ 2356eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "restart ability: %{public}s", abilityRecord->GetURI().c_str()); 2357eace7efcSopenharmony_ci AbilityRequest requestInfo; 2358eace7efcSopenharmony_ci requestInfo.want = abilityRecord->GetWant(); 2359eace7efcSopenharmony_ci requestInfo.abilityInfo = abilityRecord->GetAbilityInfo(); 2360eace7efcSopenharmony_ci requestInfo.appInfo = abilityRecord->GetApplicationInfo(); 2361eace7efcSopenharmony_ci requestInfo.restartTime = abilityRecord->GetRestartTime(); 2362eace7efcSopenharmony_ci requestInfo.restart = true; 2363eace7efcSopenharmony_ci requestInfo.uid = abilityRecord->GetUid(); 2364eace7efcSopenharmony_ci abilityRecord->SetRestarting(true); 2365eace7efcSopenharmony_ci ResidentAbilityInfoGuard residentAbilityInfoGuard; 2366eace7efcSopenharmony_ci if (abilityRecord->IsKeepAliveBundle()) { 2367eace7efcSopenharmony_ci residentAbilityInfoGuard.SetResidentAbilityInfo(requestInfo.abilityInfo.bundleName, 2368eace7efcSopenharmony_ci requestInfo.abilityInfo.name, userId_); 2369eace7efcSopenharmony_ci } 2370eace7efcSopenharmony_ci 2371eace7efcSopenharmony_ci if (AppUtils::GetInstance().IsLauncherAbility(abilityRecord->GetAbilityInfo().name)) { 2372eace7efcSopenharmony_ci if (currentUserId != userId_) { 2373eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "delay restart root launcher until switch user"); 2374eace7efcSopenharmony_ci return; 2375eace7efcSopenharmony_ci } 2376eace7efcSopenharmony_ci if (abilityRecord->IsSceneBoard()) { 2377eace7efcSopenharmony_ci requestInfo.want.SetParam("ohos.app.recovery", true); 2378eace7efcSopenharmony_ci DelayedSingleton<AbilityManagerService>::GetInstance()->EnableListForSCBRecovery(userId_); 2379eace7efcSopenharmony_ci } 2380eace7efcSopenharmony_ci requestInfo.restartCount = abilityRecord->GetRestartCount(); 2381eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "restart root launcher, number:%{public}d", requestInfo.restartCount); 2382eace7efcSopenharmony_ci StartAbilityLocked(requestInfo); 2383eace7efcSopenharmony_ci return; 2384eace7efcSopenharmony_ci } 2385eace7efcSopenharmony_ci 2386eace7efcSopenharmony_ci requestInfo.want.SetParam(WANT_PARAMS_APP_RESTART_FLAG, true); 2387eace7efcSopenharmony_ci 2388eace7efcSopenharmony_ci // restart other resident ability 2389eace7efcSopenharmony_ci if (abilityRecord->CanRestartResident()) { 2390eace7efcSopenharmony_ci requestInfo.restartCount = abilityRecord->GetRestartCount(); 2391eace7efcSopenharmony_ci requestInfo.restartTime = AbilityUtil::SystemTimeMillis(); 2392eace7efcSopenharmony_ci StartAbilityLocked(requestInfo); 2393eace7efcSopenharmony_ci } else { 2394eace7efcSopenharmony_ci auto findRestartResidentTask = [requestInfo](const AbilityRequest &abilityRequest) { 2395eace7efcSopenharmony_ci return (requestInfo.want.GetElement().GetBundleName() == abilityRequest.want.GetElement().GetBundleName() && 2396eace7efcSopenharmony_ci requestInfo.want.GetElement().GetModuleName() == abilityRequest.want.GetElement().GetModuleName() && 2397eace7efcSopenharmony_ci requestInfo.want.GetElement().GetAbilityName() == abilityRequest.want.GetElement().GetAbilityName()); 2398eace7efcSopenharmony_ci }; 2399eace7efcSopenharmony_ci auto findIter = find_if(restartResidentTaskList_.begin(), restartResidentTaskList_.end(), 2400eace7efcSopenharmony_ci findRestartResidentTask); 2401eace7efcSopenharmony_ci if (findIter != restartResidentTaskList_.end()) { 2402eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "restart task registered"); 2403eace7efcSopenharmony_ci return; 2404eace7efcSopenharmony_ci } 2405eace7efcSopenharmony_ci restartResidentTaskList_.emplace_back(requestInfo); 2406eace7efcSopenharmony_ci PostRestartResidentTask(requestInfo); 2407eace7efcSopenharmony_ci } 2408eace7efcSopenharmony_ci} 2409eace7efcSopenharmony_ci 2410eace7efcSopenharmony_cistd::string AbilityConnectManager::GetServiceKey(const std::shared_ptr<AbilityRecord> &service) 2411eace7efcSopenharmony_ci{ 2412eace7efcSopenharmony_ci std::string serviceKey = service->GetURI(); 2413eace7efcSopenharmony_ci if (FRS_BUNDLE_NAME == service->GetAbilityInfo().bundleName) { 2414eace7efcSopenharmony_ci serviceKey = serviceKey + std::to_string(service->GetWant().GetIntParam(FRS_APP_INDEX, 0)); 2415eace7efcSopenharmony_ci } 2416eace7efcSopenharmony_ci return serviceKey; 2417eace7efcSopenharmony_ci} 2418eace7efcSopenharmony_ci 2419eace7efcSopenharmony_civoid AbilityConnectManager::DumpState(std::vector<std::string> &info, bool isClient, const std::string &args) 2420eace7efcSopenharmony_ci{ 2421eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "args:%{public}s", args.c_str()); 2422eace7efcSopenharmony_ci auto serviceMapBack = GetServiceMap(); 2423eace7efcSopenharmony_ci auto cacheList = AbilityCacheManager::GetInstance().GetAbilityList(); 2424eace7efcSopenharmony_ci if (!args.empty()) { 2425eace7efcSopenharmony_ci auto it = std::find_if(serviceMapBack.begin(), serviceMapBack.end(), [&args](const auto &service) { 2426eace7efcSopenharmony_ci return service.first.compare(args) == 0; 2427eace7efcSopenharmony_ci }); 2428eace7efcSopenharmony_ci if (it != serviceMapBack.end()) { 2429eace7efcSopenharmony_ci info.emplace_back("uri [ " + it->first + " ]"); 2430eace7efcSopenharmony_ci if (it->second != nullptr) { 2431eace7efcSopenharmony_ci it->second->DumpService(info, isClient); 2432eace7efcSopenharmony_ci } 2433eace7efcSopenharmony_ci } else { 2434eace7efcSopenharmony_ci info.emplace_back(args + ": Nothing to dump from serviceMap."); 2435eace7efcSopenharmony_ci } 2436eace7efcSopenharmony_ci 2437eace7efcSopenharmony_ci std::string serviceKey; 2438eace7efcSopenharmony_ci auto iter = std::find_if(cacheList.begin(), cacheList.end(), [&args, &serviceKey, this](const auto &service) { 2439eace7efcSopenharmony_ci serviceKey = GetServiceKey(service); 2440eace7efcSopenharmony_ci return serviceKey.compare(args) == 0; 2441eace7efcSopenharmony_ci }); 2442eace7efcSopenharmony_ci if (iter != cacheList.end()) { 2443eace7efcSopenharmony_ci info.emplace_back("uri [ " + serviceKey + " ]"); 2444eace7efcSopenharmony_ci if (*iter != nullptr) { 2445eace7efcSopenharmony_ci (*iter)->DumpService(info, isClient); 2446eace7efcSopenharmony_ci } 2447eace7efcSopenharmony_ci } else { 2448eace7efcSopenharmony_ci info.emplace_back(args + ": Nothing to dump from lru cache."); 2449eace7efcSopenharmony_ci } 2450eace7efcSopenharmony_ci } else { 2451eace7efcSopenharmony_ci info.emplace_back(" ExtensionRecords:"); 2452eace7efcSopenharmony_ci for (auto &&service : serviceMapBack) { 2453eace7efcSopenharmony_ci info.emplace_back(" uri [" + service.first + "]"); 2454eace7efcSopenharmony_ci if (service.second != nullptr) { 2455eace7efcSopenharmony_ci service.second->DumpService(info, isClient); 2456eace7efcSopenharmony_ci } 2457eace7efcSopenharmony_ci } 2458eace7efcSopenharmony_ci for (auto &&service : cacheList) { 2459eace7efcSopenharmony_ci std::string serviceKey = GetServiceKey(service); 2460eace7efcSopenharmony_ci info.emplace_back(" uri [" + serviceKey + "]"); 2461eace7efcSopenharmony_ci if (service != nullptr) { 2462eace7efcSopenharmony_ci service->DumpService(info, isClient); 2463eace7efcSopenharmony_ci } 2464eace7efcSopenharmony_ci } 2465eace7efcSopenharmony_ci } 2466eace7efcSopenharmony_ci} 2467eace7efcSopenharmony_ci 2468eace7efcSopenharmony_civoid AbilityConnectManager::DumpStateByUri(std::vector<std::string> &info, bool isClient, const std::string &args, 2469eace7efcSopenharmony_ci std::vector<std::string> ¶ms) 2470eace7efcSopenharmony_ci{ 2471eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "args:%{public}s, params size: %{public}zu", args.c_str(), params.size()); 2472eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> extensionAbilityRecord = nullptr; 2473eace7efcSopenharmony_ci { 2474eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 2475eace7efcSopenharmony_ci auto it = std::find_if(serviceMap_.begin(), serviceMap_.end(), [&args](const auto &service) { 2476eace7efcSopenharmony_ci return service.first.compare(args) == 0; 2477eace7efcSopenharmony_ci }); 2478eace7efcSopenharmony_ci if (it != serviceMap_.end()) { 2479eace7efcSopenharmony_ci info.emplace_back("uri [ " + it->first + " ]"); 2480eace7efcSopenharmony_ci extensionAbilityRecord = it->second; 2481eace7efcSopenharmony_ci } else { 2482eace7efcSopenharmony_ci info.emplace_back(args + ": Nothing to dump from serviceMap."); 2483eace7efcSopenharmony_ci } 2484eace7efcSopenharmony_ci } 2485eace7efcSopenharmony_ci if (extensionAbilityRecord != nullptr) { 2486eace7efcSopenharmony_ci extensionAbilityRecord->DumpService(info, params, isClient); 2487eace7efcSopenharmony_ci return; 2488eace7efcSopenharmony_ci } 2489eace7efcSopenharmony_ci extensionAbilityRecord = AbilityCacheManager::GetInstance().FindRecordByServiceKey(args); 2490eace7efcSopenharmony_ci if (extensionAbilityRecord != nullptr) { 2491eace7efcSopenharmony_ci info.emplace_back("uri [ " + args + " ]"); 2492eace7efcSopenharmony_ci extensionAbilityRecord->DumpService(info, params, isClient); 2493eace7efcSopenharmony_ci } else { 2494eace7efcSopenharmony_ci info.emplace_back(args + ": Nothing to dump from lru cache."); 2495eace7efcSopenharmony_ci } 2496eace7efcSopenharmony_ci} 2497eace7efcSopenharmony_ci 2498eace7efcSopenharmony_civoid AbilityConnectManager::GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info, 2499eace7efcSopenharmony_ci const int32_t userId, bool isPerm) 2500eace7efcSopenharmony_ci{ 2501eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 2502eace7efcSopenharmony_ci auto serviceMapBack = GetServiceMap(); 2503eace7efcSopenharmony_ci auto queryInfo = [&](ServiceMapType::reference service) { 2504eace7efcSopenharmony_ci if (static_cast<int>(info.size()) >= upperLimit) { 2505eace7efcSopenharmony_ci return; 2506eace7efcSopenharmony_ci } 2507eace7efcSopenharmony_ci auto abilityRecord = service.second; 2508eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2509eace7efcSopenharmony_ci 2510eace7efcSopenharmony_ci if (isPerm) { 2511eace7efcSopenharmony_ci GetExtensionRunningInfo(abilityRecord, userId, info); 2512eace7efcSopenharmony_ci } else { 2513eace7efcSopenharmony_ci auto callingTokenId = IPCSkeleton::GetCallingTokenID(); 2514eace7efcSopenharmony_ci auto tokenID = abilityRecord->GetApplicationInfo().accessTokenId; 2515eace7efcSopenharmony_ci if (callingTokenId == tokenID) { 2516eace7efcSopenharmony_ci GetExtensionRunningInfo(abilityRecord, userId, info); 2517eace7efcSopenharmony_ci } 2518eace7efcSopenharmony_ci } 2519eace7efcSopenharmony_ci }; 2520eace7efcSopenharmony_ci std::for_each(serviceMapBack.begin(), serviceMapBack.end(), queryInfo); 2521eace7efcSopenharmony_ci 2522eace7efcSopenharmony_ci auto cacheAbilityList = AbilityCacheManager::GetInstance().GetAbilityList(); 2523eace7efcSopenharmony_ci auto queryInfoForCache = [&](std::shared_ptr<AbilityRecord> &service) { 2524eace7efcSopenharmony_ci if (static_cast<int>(info.size()) >= upperLimit) { 2525eace7efcSopenharmony_ci return; 2526eace7efcSopenharmony_ci } 2527eace7efcSopenharmony_ci CHECK_POINTER(service); 2528eace7efcSopenharmony_ci 2529eace7efcSopenharmony_ci if (isPerm) { 2530eace7efcSopenharmony_ci GetExtensionRunningInfo(service, userId, info); 2531eace7efcSopenharmony_ci } else { 2532eace7efcSopenharmony_ci auto callingTokenId = IPCSkeleton::GetCallingTokenID(); 2533eace7efcSopenharmony_ci auto tokenID = service->GetApplicationInfo().accessTokenId; 2534eace7efcSopenharmony_ci if (callingTokenId == tokenID) { 2535eace7efcSopenharmony_ci GetExtensionRunningInfo(service, userId, info); 2536eace7efcSopenharmony_ci } 2537eace7efcSopenharmony_ci } 2538eace7efcSopenharmony_ci }; 2539eace7efcSopenharmony_ci std::for_each(cacheAbilityList.begin(), cacheAbilityList.end(), queryInfoForCache); 2540eace7efcSopenharmony_ci} 2541eace7efcSopenharmony_ci 2542eace7efcSopenharmony_civoid AbilityConnectManager::GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info, bool isPerm) 2543eace7efcSopenharmony_ci{ 2544eace7efcSopenharmony_ci auto serviceMapBack = GetServiceMap(); 2545eace7efcSopenharmony_ci auto queryInfo = [&info, isPerm](ServiceMapType::reference service) { 2546eace7efcSopenharmony_ci auto abilityRecord = service.second; 2547eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2548eace7efcSopenharmony_ci 2549eace7efcSopenharmony_ci if (isPerm) { 2550eace7efcSopenharmony_ci DelayedSingleton<AbilityManagerService>::GetInstance()->GetAbilityRunningInfo(info, abilityRecord); 2551eace7efcSopenharmony_ci } else { 2552eace7efcSopenharmony_ci auto callingTokenId = IPCSkeleton::GetCallingTokenID(); 2553eace7efcSopenharmony_ci auto tokenID = abilityRecord->GetApplicationInfo().accessTokenId; 2554eace7efcSopenharmony_ci if (callingTokenId == tokenID) { 2555eace7efcSopenharmony_ci DelayedSingleton<AbilityManagerService>::GetInstance()->GetAbilityRunningInfo(info, abilityRecord); 2556eace7efcSopenharmony_ci } 2557eace7efcSopenharmony_ci } 2558eace7efcSopenharmony_ci }; 2559eace7efcSopenharmony_ci 2560eace7efcSopenharmony_ci std::for_each(serviceMapBack.begin(), serviceMapBack.end(), queryInfo); 2561eace7efcSopenharmony_ci} 2562eace7efcSopenharmony_ci 2563eace7efcSopenharmony_civoid AbilityConnectManager::GetExtensionRunningInfo(std::shared_ptr<AbilityRecord> &abilityRecord, 2564eace7efcSopenharmony_ci const int32_t userId, std::vector<ExtensionRunningInfo> &info) 2565eace7efcSopenharmony_ci{ 2566eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 2567eace7efcSopenharmony_ci ExtensionRunningInfo extensionInfo; 2568eace7efcSopenharmony_ci AppExecFwk::RunningProcessInfo processInfo; 2569eace7efcSopenharmony_ci extensionInfo.extension = abilityRecord->GetElementName(); 2570eace7efcSopenharmony_ci extensionInfo.type = abilityRecord->GetAbilityInfo().extensionAbilityType; 2571eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()-> 2572eace7efcSopenharmony_ci GetRunningProcessInfoByToken(abilityRecord->GetToken(), processInfo); 2573eace7efcSopenharmony_ci extensionInfo.pid = processInfo.pid_; 2574eace7efcSopenharmony_ci extensionInfo.uid = processInfo.uid_; 2575eace7efcSopenharmony_ci extensionInfo.processName = processInfo.processName_; 2576eace7efcSopenharmony_ci extensionInfo.startTime = abilityRecord->GetStartTime(); 2577eace7efcSopenharmony_ci ConnectListType connectRecordList = abilityRecord->GetConnectRecordList(); 2578eace7efcSopenharmony_ci for (auto &connectRecord : connectRecordList) { 2579eace7efcSopenharmony_ci if (connectRecord == nullptr) { 2580eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "connectRecord is nullptr."); 2581eace7efcSopenharmony_ci continue; 2582eace7efcSopenharmony_ci } 2583eace7efcSopenharmony_ci auto callerAbilityRecord = Token::GetAbilityRecordByToken(connectRecord->GetToken()); 2584eace7efcSopenharmony_ci if (callerAbilityRecord == nullptr) { 2585eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "callerAbilityRecord is nullptr."); 2586eace7efcSopenharmony_ci continue; 2587eace7efcSopenharmony_ci } 2588eace7efcSopenharmony_ci std::string package = callerAbilityRecord->GetAbilityInfo().bundleName; 2589eace7efcSopenharmony_ci extensionInfo.clientPackage.emplace_back(package); 2590eace7efcSopenharmony_ci } 2591eace7efcSopenharmony_ci info.emplace_back(extensionInfo); 2592eace7efcSopenharmony_ci} 2593eace7efcSopenharmony_ci 2594eace7efcSopenharmony_civoid AbilityConnectManager::PauseExtensions() 2595eace7efcSopenharmony_ci{ 2596eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "begin."); 2597eace7efcSopenharmony_ci std::vector<sptr<IRemoteObject>> needTerminatedTokens; 2598eace7efcSopenharmony_ci { 2599eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 2600eace7efcSopenharmony_ci for (auto it = serviceMap_.begin(); it != serviceMap_.end();) { 2601eace7efcSopenharmony_ci auto targetExtension = it->second; 2602eace7efcSopenharmony_ci if (targetExtension != nullptr && targetExtension->GetAbilityInfo().type == AbilityType::EXTENSION && 2603eace7efcSopenharmony_ci (IsLauncher(targetExtension) || targetExtension->IsSceneBoard() || 2604eace7efcSopenharmony_ci (targetExtension->GetKeepAlive() && userId_ != USER_ID_NO_HEAD))) { 2605eace7efcSopenharmony_ci terminatingExtensionList_.push_back(it->second); 2606eace7efcSopenharmony_ci it = serviceMap_.erase(it); 2607eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "terminate ability:%{public}s", 2608eace7efcSopenharmony_ci targetExtension->GetAbilityInfo().name.c_str()); 2609eace7efcSopenharmony_ci needTerminatedTokens.push_back(targetExtension->GetToken()); 2610eace7efcSopenharmony_ci } else { 2611eace7efcSopenharmony_ci ++it; 2612eace7efcSopenharmony_ci } 2613eace7efcSopenharmony_ci } 2614eace7efcSopenharmony_ci } 2615eace7efcSopenharmony_ci 2616eace7efcSopenharmony_ci for (const auto &token : needTerminatedTokens) { 2617eace7efcSopenharmony_ci std::lock_guard lock(serialMutex_); 2618eace7efcSopenharmony_ci TerminateAbilityLocked(token); 2619eace7efcSopenharmony_ci } 2620eace7efcSopenharmony_ci} 2621eace7efcSopenharmony_ci 2622eace7efcSopenharmony_civoid AbilityConnectManager::RemoveLauncherDeathRecipient() 2623eace7efcSopenharmony_ci{ 2624eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "call"); 2625eace7efcSopenharmony_ci { 2626eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 2627eace7efcSopenharmony_ci for (auto it = serviceMap_.begin(); it != serviceMap_.end(); ++it) { 2628eace7efcSopenharmony_ci auto targetExtension = it->second; 2629eace7efcSopenharmony_ci if (targetExtension != nullptr && targetExtension->GetAbilityInfo().type == AbilityType::EXTENSION && 2630eace7efcSopenharmony_ci (IsLauncher(targetExtension) || targetExtension->IsSceneBoard())) { 2631eace7efcSopenharmony_ci targetExtension->RemoveAbilityDeathRecipient(); 2632eace7efcSopenharmony_ci return; 2633eace7efcSopenharmony_ci } 2634eace7efcSopenharmony_ci } 2635eace7efcSopenharmony_ci } 2636eace7efcSopenharmony_ci AbilityCacheManager::GetInstance().RemoveLauncherDeathRecipient(); 2637eace7efcSopenharmony_ci} 2638eace7efcSopenharmony_ci 2639eace7efcSopenharmony_cibool AbilityConnectManager::IsLauncher(std::shared_ptr<AbilityRecord> serviceExtension) const 2640eace7efcSopenharmony_ci{ 2641eace7efcSopenharmony_ci if (serviceExtension == nullptr) { 2642eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "param null"); 2643eace7efcSopenharmony_ci return false; 2644eace7efcSopenharmony_ci } 2645eace7efcSopenharmony_ci return serviceExtension->GetAbilityInfo().name == AbilityConfig::LAUNCHER_ABILITY_NAME && 2646eace7efcSopenharmony_ci serviceExtension->GetAbilityInfo().bundleName == AbilityConfig::LAUNCHER_BUNDLE_NAME; 2647eace7efcSopenharmony_ci} 2648eace7efcSopenharmony_ci 2649eace7efcSopenharmony_civoid AbilityConnectManager::KillProcessesByUserId() const 2650eace7efcSopenharmony_ci{ 2651eace7efcSopenharmony_ci auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance(); 2652eace7efcSopenharmony_ci if (appScheduler == nullptr) { 2653eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "appScheduler null"); 2654eace7efcSopenharmony_ci return; 2655eace7efcSopenharmony_ci } 2656eace7efcSopenharmony_ci IN_PROCESS_CALL_WITHOUT_RET(appScheduler->KillProcessesByUserId(userId_)); 2657eace7efcSopenharmony_ci} 2658eace7efcSopenharmony_ci 2659eace7efcSopenharmony_civoid AbilityConnectManager::MoveToBackground(const std::shared_ptr<AbilityRecord> &abilityRecord) 2660eace7efcSopenharmony_ci{ 2661eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 2662eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 2663eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityRecord"); 2664eace7efcSopenharmony_ci return; 2665eace7efcSopenharmony_ci } 2666eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Move the ui extension ability to background, ability:%{public}s.", 2667eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().name.c_str()); 2668eace7efcSopenharmony_ci abilityRecord->SetIsNewWant(false); 2669eace7efcSopenharmony_ci 2670eace7efcSopenharmony_ci auto self(weak_from_this()); 2671eace7efcSopenharmony_ci auto task = [abilityRecord, self]() { 2672eace7efcSopenharmony_ci auto selfObj = self.lock(); 2673eace7efcSopenharmony_ci if (selfObj == nullptr) { 2674eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "mgr invalid"); 2675eace7efcSopenharmony_ci return; 2676eace7efcSopenharmony_ci } 2677eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2678eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType) && 2679eace7efcSopenharmony_ci selfObj->uiExtensionAbilityRecordMgr_ != nullptr && selfObj->IsCallerValid(abilityRecord)) { 2680eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Start background timeout."); 2681eace7efcSopenharmony_ci selfObj->uiExtensionAbilityRecordMgr_->BackgroundTimeout(abilityRecord->GetUIExtensionAbilityId()); 2682eace7efcSopenharmony_ci } 2683eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "move timeout"); 2684eace7efcSopenharmony_ci selfObj->PrintTimeOutLog(abilityRecord, AbilityManagerService::BACKGROUND_TIMEOUT_MSG); 2685eace7efcSopenharmony_ci selfObj->CompleteBackground(abilityRecord); 2686eace7efcSopenharmony_ci }; 2687eace7efcSopenharmony_ci abilityRecord->BackgroundAbility(task); 2688eace7efcSopenharmony_ci} 2689eace7efcSopenharmony_ci 2690eace7efcSopenharmony_civoid AbilityConnectManager::CompleteForeground(const std::shared_ptr<AbilityRecord> &abilityRecord) 2691eace7efcSopenharmony_ci{ 2692eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 2693eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 2694eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityRecord null"); 2695eace7efcSopenharmony_ci return; 2696eace7efcSopenharmony_ci } 2697eace7efcSopenharmony_ci if (abilityRecord->GetAbilityState() != AbilityState::FOREGROUNDING) { 2698eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "ability state: %{public}d, not complete foreground", 2699eace7efcSopenharmony_ci abilityRecord->GetAbilityState()); 2700eace7efcSopenharmony_ci return; 2701eace7efcSopenharmony_ci } 2702eace7efcSopenharmony_ci 2703eace7efcSopenharmony_ci abilityRecord->SetAbilityState(AbilityState::FOREGROUND); 2704eace7efcSopenharmony_ci if (abilityRecord->BackgroundAbilityWindowDelayed()) { 2705eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "response background request"); 2706eace7efcSopenharmony_ci abilityRecord->DoBackgroundAbilityWindowDelayed(false); 2707eace7efcSopenharmony_ci DoBackgroundAbilityWindow(abilityRecord, abilityRecord->GetSessionInfo()); 2708eace7efcSopenharmony_ci } 2709eace7efcSopenharmony_ci CompleteStartServiceReq(abilityRecord->GetURI()); 2710eace7efcSopenharmony_ci} 2711eace7efcSopenharmony_ci 2712eace7efcSopenharmony_civoid AbilityConnectManager::HandleForegroundTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord) 2713eace7efcSopenharmony_ci{ 2714eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 2715eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 2716eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityRecord null"); 2717eace7efcSopenharmony_ci return; 2718eace7efcSopenharmony_ci } 2719eace7efcSopenharmony_ci if (UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType) && 2720eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_ != nullptr && IsCallerValid(abilityRecord)) { 2721eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "start foreground timeout"); 2722eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_->ForegroundTimeout(abilityRecord->GetUIExtensionAbilityId()); 2723eace7efcSopenharmony_ci } 2724eace7efcSopenharmony_ci abilityRecord->SetAbilityState(AbilityState::BACKGROUND); 2725eace7efcSopenharmony_ci abilityRecord->DoBackgroundAbilityWindowDelayed(false); 2726eace7efcSopenharmony_ci CompleteStartServiceReq(abilityRecord->GetURI()); 2727eace7efcSopenharmony_ci} 2728eace7efcSopenharmony_ci 2729eace7efcSopenharmony_civoid AbilityConnectManager::CompleteBackground(const std::shared_ptr<AbilityRecord> &abilityRecord) 2730eace7efcSopenharmony_ci{ 2731eace7efcSopenharmony_ci std::lock_guard lock(serialMutex_); 2732eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 2733eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityRecord null"); 2734eace7efcSopenharmony_ci return; 2735eace7efcSopenharmony_ci } 2736eace7efcSopenharmony_ci if (abilityRecord->GetAbilityState() != AbilityState::BACKGROUNDING) { 2737eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "ability state: %{public}d, not complete background.", 2738eace7efcSopenharmony_ci abilityRecord->GetAbilityState()); 2739eace7efcSopenharmony_ci return; 2740eace7efcSopenharmony_ci } 2741eace7efcSopenharmony_ci abilityRecord->SetAbilityState(AbilityState::BACKGROUND); 2742eace7efcSopenharmony_ci // send application state to AppMS. 2743eace7efcSopenharmony_ci // notify AppMS to update application state. 2744eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->MoveToBackground(abilityRecord->GetToken()); 2745eace7efcSopenharmony_ci CompleteStartServiceReq(abilityRecord->GetURI()); 2746eace7efcSopenharmony_ci // Abilities ahead of the one started were put in terminate list, we need to terminate them. 2747eace7efcSopenharmony_ci TerminateAbilityLocked(abilityRecord->GetToken()); 2748eace7efcSopenharmony_ci} 2749eace7efcSopenharmony_ci 2750eace7efcSopenharmony_civoid AbilityConnectManager::PrintTimeOutLog(const std::shared_ptr<AbilityRecord> &ability, uint32_t msgId, bool isHalf) 2751eace7efcSopenharmony_ci{ 2752eace7efcSopenharmony_ci CHECK_POINTER(ability); 2753eace7efcSopenharmony_ci AppExecFwk::RunningProcessInfo processInfo = {}; 2754eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByToken(ability->GetToken(), processInfo); 2755eace7efcSopenharmony_ci if (processInfo.pid_ == 0) { 2756eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "ability %{public}s pid invalid", ability->GetURI().c_str()); 2757eace7efcSopenharmony_ci return; 2758eace7efcSopenharmony_ci } 2759eace7efcSopenharmony_ci int typeId = AppExecFwk::AppfreezeManager::TypeAttribute::NORMAL_TIMEOUT; 2760eace7efcSopenharmony_ci std::string msgContent = "ability:" + ability->GetAbilityInfo().name + " "; 2761eace7efcSopenharmony_ci switch (msgId) { 2762eace7efcSopenharmony_ci case AbilityManagerService::LOAD_TIMEOUT_MSG: 2763eace7efcSopenharmony_ci msgContent += "load timeout"; 2764eace7efcSopenharmony_ci typeId = AppExecFwk::AppfreezeManager::TypeAttribute::CRITICAL_TIMEOUT; 2765eace7efcSopenharmony_ci break; 2766eace7efcSopenharmony_ci case AbilityManagerService::ACTIVE_TIMEOUT_MSG: 2767eace7efcSopenharmony_ci msgContent += "active timeout"; 2768eace7efcSopenharmony_ci break; 2769eace7efcSopenharmony_ci case AbilityManagerService::INACTIVE_TIMEOUT_MSG: 2770eace7efcSopenharmony_ci msgContent += "inactive timeout"; 2771eace7efcSopenharmony_ci break; 2772eace7efcSopenharmony_ci case AbilityManagerService::FOREGROUND_TIMEOUT_MSG: 2773eace7efcSopenharmony_ci msgContent += "foreground timeout"; 2774eace7efcSopenharmony_ci typeId = AppExecFwk::AppfreezeManager::TypeAttribute::CRITICAL_TIMEOUT; 2775eace7efcSopenharmony_ci break; 2776eace7efcSopenharmony_ci case AbilityManagerService::BACKGROUND_TIMEOUT_MSG: 2777eace7efcSopenharmony_ci msgContent += "background timeout"; 2778eace7efcSopenharmony_ci break; 2779eace7efcSopenharmony_ci case AbilityManagerService::TERMINATE_TIMEOUT_MSG: 2780eace7efcSopenharmony_ci msgContent += "terminate timeout"; 2781eace7efcSopenharmony_ci break; 2782eace7efcSopenharmony_ci default: 2783eace7efcSopenharmony_ci return; 2784eace7efcSopenharmony_ci } 2785eace7efcSopenharmony_ci 2786eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, 2787eace7efcSopenharmony_ci "LIFECYCLE_TIMEOUT: uid: %{public}d, pid: %{public}d, bundleName: %{public}s, abilityName: %{public}s," 2788eace7efcSopenharmony_ci "msg: %{public}s", processInfo.uid_, processInfo.pid_, ability->GetAbilityInfo().bundleName.c_str(), 2789eace7efcSopenharmony_ci ability->GetAbilityInfo().name.c_str(), msgContent.c_str()); 2790eace7efcSopenharmony_ci std::string eventName = isHalf ? 2791eace7efcSopenharmony_ci AppExecFwk::AppFreezeType::LIFECYCLE_HALF_TIMEOUT : AppExecFwk::AppFreezeType::LIFECYCLE_TIMEOUT; 2792eace7efcSopenharmony_ci AppExecFwk::AppfreezeManager::ParamInfo info = { 2793eace7efcSopenharmony_ci .typeId = typeId, 2794eace7efcSopenharmony_ci .pid = processInfo.pid_, 2795eace7efcSopenharmony_ci .eventName = eventName, 2796eace7efcSopenharmony_ci .bundleName = ability->GetAbilityInfo().bundleName, 2797eace7efcSopenharmony_ci .msg = msgContent 2798eace7efcSopenharmony_ci }; 2799eace7efcSopenharmony_ci AppExecFwk::AppfreezeManager::GetInstance()->LifecycleTimeoutHandle(info); 2800eace7efcSopenharmony_ci} 2801eace7efcSopenharmony_ci 2802eace7efcSopenharmony_civoid AbilityConnectManager::MoveToTerminatingMap(const std::shared_ptr<AbilityRecord>& abilityRecord) 2803eace7efcSopenharmony_ci{ 2804eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2805eace7efcSopenharmony_ci auto& abilityInfo = abilityRecord->GetAbilityInfo(); 2806eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 2807eace7efcSopenharmony_ci terminatingExtensionList_.push_back(abilityRecord); 2808eace7efcSopenharmony_ci std::string serviceKey = abilityRecord->GetURI(); 2809eace7efcSopenharmony_ci if (FRS_BUNDLE_NAME == abilityInfo.bundleName) { 2810eace7efcSopenharmony_ci AppExecFwk::ElementName element(abilityInfo.deviceId, abilityInfo.bundleName, abilityInfo.name, 2811eace7efcSopenharmony_ci abilityInfo.moduleName); 2812eace7efcSopenharmony_ci serviceKey = element.GetURI() + std::to_string(abilityRecord->GetWant().GetIntParam(FRS_APP_INDEX, 0)); 2813eace7efcSopenharmony_ci } 2814eace7efcSopenharmony_ci if (serviceMap_.erase(serviceKey) == 0) { 2815eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "Unknown: %{public}s", serviceKey.c_str()); 2816eace7efcSopenharmony_ci } 2817eace7efcSopenharmony_ci AbilityCacheManager::GetInstance().Remove(abilityRecord); 2818eace7efcSopenharmony_ci if (IsSpecialAbility(abilityRecord->GetAbilityInfo())) { 2819eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "moving ability: %{public}s", abilityRecord->GetURI().c_str()); 2820eace7efcSopenharmony_ci } 2821eace7efcSopenharmony_ci} 2822eace7efcSopenharmony_ci 2823eace7efcSopenharmony_civoid AbilityConnectManager::AddUIExtWindowDeathRecipient(const sptr<IRemoteObject> &session) 2824eace7efcSopenharmony_ci{ 2825eace7efcSopenharmony_ci CHECK_POINTER(session); 2826eace7efcSopenharmony_ci std::lock_guard lock(uiExtRecipientMapMutex_); 2827eace7efcSopenharmony_ci auto it = uiExtRecipientMap_.find(session); 2828eace7efcSopenharmony_ci if (it != uiExtRecipientMap_.end()) { 2829eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "recipient added before"); 2830eace7efcSopenharmony_ci return; 2831eace7efcSopenharmony_ci } else { 2832eace7efcSopenharmony_ci std::weak_ptr<AbilityConnectManager> thisWeakPtr(shared_from_this()); 2833eace7efcSopenharmony_ci sptr<IRemoteObject::DeathRecipient> deathRecipient = 2834eace7efcSopenharmony_ci new AbilityConnectCallbackRecipient([thisWeakPtr](const wptr<IRemoteObject> &remote) { 2835eace7efcSopenharmony_ci auto abilityConnectManager = thisWeakPtr.lock(); 2836eace7efcSopenharmony_ci if (abilityConnectManager) { 2837eace7efcSopenharmony_ci abilityConnectManager->OnUIExtWindowDied(remote); 2838eace7efcSopenharmony_ci } 2839eace7efcSopenharmony_ci }); 2840eace7efcSopenharmony_ci if (!session->AddDeathRecipient(deathRecipient)) { 2841eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "AddDeathRecipient fail"); 2842eace7efcSopenharmony_ci } 2843eace7efcSopenharmony_ci uiExtRecipientMap_.emplace(session, deathRecipient); 2844eace7efcSopenharmony_ci } 2845eace7efcSopenharmony_ci} 2846eace7efcSopenharmony_ci 2847eace7efcSopenharmony_civoid AbilityConnectManager::RemoveUIExtWindowDeathRecipient(const sptr<IRemoteObject> &session) 2848eace7efcSopenharmony_ci{ 2849eace7efcSopenharmony_ci CHECK_POINTER(session); 2850eace7efcSopenharmony_ci std::lock_guard lock(uiExtRecipientMapMutex_); 2851eace7efcSopenharmony_ci auto it = uiExtRecipientMap_.find(session); 2852eace7efcSopenharmony_ci if (it != uiExtRecipientMap_.end() && it->first != nullptr) { 2853eace7efcSopenharmony_ci it->first->RemoveDeathRecipient(it->second); 2854eace7efcSopenharmony_ci uiExtRecipientMap_.erase(it); 2855eace7efcSopenharmony_ci return; 2856eace7efcSopenharmony_ci } 2857eace7efcSopenharmony_ci} 2858eace7efcSopenharmony_ci 2859eace7efcSopenharmony_civoid AbilityConnectManager::OnUIExtWindowDied(const wptr<IRemoteObject> &remote) 2860eace7efcSopenharmony_ci{ 2861eace7efcSopenharmony_ci auto object = remote.promote(); 2862eace7efcSopenharmony_ci CHECK_POINTER(object); 2863eace7efcSopenharmony_ci if (taskHandler_) { 2864eace7efcSopenharmony_ci auto task = [object, connectManager = shared_from_this()]() { 2865eace7efcSopenharmony_ci connectManager->HandleUIExtWindowDiedTask(object); 2866eace7efcSopenharmony_ci }; 2867eace7efcSopenharmony_ci taskHandler_->SubmitTask(task); 2868eace7efcSopenharmony_ci } 2869eace7efcSopenharmony_ci} 2870eace7efcSopenharmony_ci 2871eace7efcSopenharmony_civoid AbilityConnectManager::HandleUIExtWindowDiedTask(const sptr<IRemoteObject> &remote) 2872eace7efcSopenharmony_ci{ 2873eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "call."); 2874eace7efcSopenharmony_ci CHECK_POINTER(remote); 2875eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord; 2876eace7efcSopenharmony_ci sptr<SessionInfo> sessionInfo; 2877eace7efcSopenharmony_ci { 2878eace7efcSopenharmony_ci std::lock_guard guard(uiExtensionMapMutex_); 2879eace7efcSopenharmony_ci auto it = uiExtensionMap_.find(remote); 2880eace7efcSopenharmony_ci if (it != uiExtensionMap_.end()) { 2881eace7efcSopenharmony_ci abilityRecord = it->second.first.lock(); 2882eace7efcSopenharmony_ci sessionInfo = it->second.second; 2883eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::UI_EXT, "uiExtAbility caller died"); 2884eace7efcSopenharmony_ci uiExtensionMap_.erase(it); 2885eace7efcSopenharmony_ci } else { 2886eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "not find"); 2887eace7efcSopenharmony_ci return; 2888eace7efcSopenharmony_ci } 2889eace7efcSopenharmony_ci } 2890eace7efcSopenharmony_ci 2891eace7efcSopenharmony_ci if (abilityRecord) { 2892eace7efcSopenharmony_ci TerminateAbilityWindowLocked(abilityRecord, sessionInfo); 2893eace7efcSopenharmony_ci } else { 2894eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "abilityRecord null"); 2895eace7efcSopenharmony_ci } 2896eace7efcSopenharmony_ci RemoveUIExtWindowDeathRecipient(remote); 2897eace7efcSopenharmony_ci} 2898eace7efcSopenharmony_ci 2899eace7efcSopenharmony_cibool AbilityConnectManager::IsUIExtensionFocused(uint32_t uiExtensionTokenId, const sptr<IRemoteObject>& focusToken) 2900eace7efcSopenharmony_ci{ 2901eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); 2902eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, false); 2903eace7efcSopenharmony_ci std::lock_guard guard(uiExtensionMapMutex_); 2904eace7efcSopenharmony_ci for (auto& item: uiExtensionMap_) { 2905eace7efcSopenharmony_ci auto uiExtension = item.second.first.lock(); 2906eace7efcSopenharmony_ci auto sessionInfo = item.second.second; 2907eace7efcSopenharmony_ci if (uiExtension && uiExtension->GetApplicationInfo().accessTokenId == uiExtensionTokenId) { 2908eace7efcSopenharmony_ci if (sessionInfo && uiExtensionAbilityRecordMgr_->IsFocused( 2909eace7efcSopenharmony_ci uiExtension->GetUIExtensionAbilityId(), sessionInfo->callerToken, focusToken)) { 2910eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Focused"); 2911eace7efcSopenharmony_ci return true; 2912eace7efcSopenharmony_ci } 2913eace7efcSopenharmony_ci if (sessionInfo && sessionInfo->callerToken == focusToken) { 2914eace7efcSopenharmony_ci return true; 2915eace7efcSopenharmony_ci } 2916eace7efcSopenharmony_ci } 2917eace7efcSopenharmony_ci } 2918eace7efcSopenharmony_ci return false; 2919eace7efcSopenharmony_ci} 2920eace7efcSopenharmony_ci 2921eace7efcSopenharmony_cisptr<IRemoteObject> AbilityConnectManager::GetUIExtensionSourceToken(const sptr<IRemoteObject> &token) 2922eace7efcSopenharmony_ci{ 2923eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Called"); 2924eace7efcSopenharmony_ci std::lock_guard guard(uiExtensionMapMutex_); 2925eace7efcSopenharmony_ci for (auto &item : uiExtensionMap_) { 2926eace7efcSopenharmony_ci auto sessionInfo = item.second.second; 2927eace7efcSopenharmony_ci auto uiExtension = item.second.first.lock(); 2928eace7efcSopenharmony_ci if (sessionInfo != nullptr && uiExtension != nullptr && uiExtension->GetToken() != nullptr && 2929eace7efcSopenharmony_ci uiExtension->GetToken()->AsObject() == token) { 2930eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "The source token found."); 2931eace7efcSopenharmony_ci return sessionInfo->callerToken; 2932eace7efcSopenharmony_ci } 2933eace7efcSopenharmony_ci } 2934eace7efcSopenharmony_ci return nullptr; 2935eace7efcSopenharmony_ci} 2936eace7efcSopenharmony_ci 2937eace7efcSopenharmony_civoid AbilityConnectManager::GetUIExtensionCallerTokenList(const std::shared_ptr<AbilityRecord> &abilityRecord, 2938eace7efcSopenharmony_ci std::list<sptr<IRemoteObject>> &callerList) 2939eace7efcSopenharmony_ci{ 2940eace7efcSopenharmony_ci CHECK_POINTER(uiExtensionAbilityRecordMgr_); 2941eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_->GetCallerTokenList(abilityRecord, callerList); 2942eace7efcSopenharmony_ci} 2943eace7efcSopenharmony_ci 2944eace7efcSopenharmony_cibool AbilityConnectManager::IsWindowExtensionFocused(uint32_t extensionTokenId, const sptr<IRemoteObject>& focusToken) 2945eace7efcSopenharmony_ci{ 2946eace7efcSopenharmony_ci std::lock_guard guard(windowExtensionMapMutex_); 2947eace7efcSopenharmony_ci for (auto& item: windowExtensionMap_) { 2948eace7efcSopenharmony_ci uint32_t windowExtTokenId = item.second.first; 2949eace7efcSopenharmony_ci auto sessionInfo = item.second.second; 2950eace7efcSopenharmony_ci if (windowExtTokenId == extensionTokenId && sessionInfo && sessionInfo->callerToken == focusToken) { 2951eace7efcSopenharmony_ci return true; 2952eace7efcSopenharmony_ci } 2953eace7efcSopenharmony_ci } 2954eace7efcSopenharmony_ci return false; 2955eace7efcSopenharmony_ci} 2956eace7efcSopenharmony_ci 2957eace7efcSopenharmony_civoid AbilityConnectManager::HandleProcessFrozen(const std::vector<int32_t> &pidList, int32_t uid) 2958eace7efcSopenharmony_ci{ 2959eace7efcSopenharmony_ci auto taskHandler = taskHandler_; 2960eace7efcSopenharmony_ci if (!taskHandler) { 2961eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "taskHandler null"); 2962eace7efcSopenharmony_ci return; 2963eace7efcSopenharmony_ci } 2964eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "uid:%{public}d", uid); 2965eace7efcSopenharmony_ci std::unordered_set<int32_t> pidSet(pidList.begin(), pidList.end()); 2966eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 2967eace7efcSopenharmony_ci auto weakThis = weak_from_this(); 2968eace7efcSopenharmony_ci for (auto [key, abilityRecord] : serviceMap_) { 2969eace7efcSopenharmony_ci if (abilityRecord && abilityRecord->GetUid() == uid && 2970eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().extensionAbilityType == AppExecFwk::ExtensionAbilityType::SERVICE && 2971eace7efcSopenharmony_ci pidSet.count(abilityRecord->GetPid()) > 0 && 2972eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().bundleName != FROZEN_WHITE_DIALOG && 2973eace7efcSopenharmony_ci abilityRecord->IsConnectListEmpty() && 2974eace7efcSopenharmony_ci !abilityRecord->GetKeepAlive()) { 2975eace7efcSopenharmony_ci taskHandler->SubmitTask([weakThis, record = abilityRecord]() { 2976eace7efcSopenharmony_ci auto connectManager = weakThis.lock(); 2977eace7efcSopenharmony_ci if (record && connectManager) { 2978eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "terminateRecord:%{public}s", 2979eace7efcSopenharmony_ci record->GetAbilityInfo().bundleName.c_str()); 2980eace7efcSopenharmony_ci connectManager->TerminateRecord(record); 2981eace7efcSopenharmony_ci } else { 2982eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "connectManager null"); 2983eace7efcSopenharmony_ci } 2984eace7efcSopenharmony_ci }); 2985eace7efcSopenharmony_ci } 2986eace7efcSopenharmony_ci } 2987eace7efcSopenharmony_ci} 2988eace7efcSopenharmony_ci 2989eace7efcSopenharmony_civoid AbilityConnectManager::PostExtensionDelayDisconnectTask(const std::shared_ptr<ConnectionRecord> &connectRecord) 2990eace7efcSopenharmony_ci{ 2991eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "call"); 2992eace7efcSopenharmony_ci CHECK_POINTER(taskHandler_); 2993eace7efcSopenharmony_ci CHECK_POINTER(connectRecord); 2994eace7efcSopenharmony_ci int32_t recordId = connectRecord->GetRecordId(); 2995eace7efcSopenharmony_ci std::string taskName = std::string("DelayDisconnectTask_") + std::to_string(recordId); 2996eace7efcSopenharmony_ci 2997eace7efcSopenharmony_ci auto abilityRecord = connectRecord->GetAbilityRecord(); 2998eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 2999eace7efcSopenharmony_ci auto typeName = abilityRecord->GetAbilityInfo().extensionTypeName; 3000eace7efcSopenharmony_ci int32_t delayTime = DelayedSingleton<ExtensionConfig>::GetInstance()->GetExtensionAutoDisconnectTime(typeName); 3001eace7efcSopenharmony_ci if (delayTime == AUTO_DISCONNECT_INFINITY) { 3002eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "This extension needn't auto disconnect."); 3003eace7efcSopenharmony_ci return; 3004eace7efcSopenharmony_ci } 3005eace7efcSopenharmony_ci 3006eace7efcSopenharmony_ci auto task = [connectRecord, self = weak_from_this()]() { 3007eace7efcSopenharmony_ci auto selfObj = self.lock(); 3008eace7efcSopenharmony_ci if (selfObj == nullptr) { 3009eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "mgr invalid"); 3010eace7efcSopenharmony_ci return; 3011eace7efcSopenharmony_ci } 3012eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "auto disconnect the Extension's connection"); 3013eace7efcSopenharmony_ci selfObj->HandleExtensionDisconnectTask(connectRecord); 3014eace7efcSopenharmony_ci }; 3015eace7efcSopenharmony_ci taskHandler_->SubmitTask(task, taskName, delayTime); 3016eace7efcSopenharmony_ci} 3017eace7efcSopenharmony_ci 3018eace7efcSopenharmony_civoid AbilityConnectManager::RemoveExtensionDelayDisconnectTask(const std::shared_ptr<ConnectionRecord> &connectRecord) 3019eace7efcSopenharmony_ci{ 3020eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "call"); 3021eace7efcSopenharmony_ci CHECK_POINTER(taskHandler_); 3022eace7efcSopenharmony_ci CHECK_POINTER(connectRecord); 3023eace7efcSopenharmony_ci int32_t recordId = connectRecord->GetRecordId(); 3024eace7efcSopenharmony_ci std::string taskName = std::string("DelayDisconnectTask_") + std::to_string(recordId); 3025eace7efcSopenharmony_ci taskHandler_->CancelTask(taskName); 3026eace7efcSopenharmony_ci} 3027eace7efcSopenharmony_ci 3028eace7efcSopenharmony_civoid AbilityConnectManager::HandleExtensionDisconnectTask(const std::shared_ptr<ConnectionRecord> &connectRecord) 3029eace7efcSopenharmony_ci{ 3030eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "call"); 3031eace7efcSopenharmony_ci std::lock_guard guard(serialMutex_); 3032eace7efcSopenharmony_ci CHECK_POINTER(connectRecord); 3033eace7efcSopenharmony_ci int result = connectRecord->DisconnectAbility(); 3034eace7efcSopenharmony_ci if (result != ERR_OK) { 3035eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "error, ret: %{public}d", result); 3036eace7efcSopenharmony_ci } 3037eace7efcSopenharmony_ci if (connectRecord->GetConnectState() == ConnectionState::DISCONNECTED) { 3038eace7efcSopenharmony_ci connectRecord->CompleteDisconnect(ERR_OK, false); 3039eace7efcSopenharmony_ci RemoveConnectionRecordFromMap(connectRecord); 3040eace7efcSopenharmony_ci } 3041eace7efcSopenharmony_ci} 3042eace7efcSopenharmony_ci 3043eace7efcSopenharmony_cibool AbilityConnectManager::IsUIExtensionAbility(const std::shared_ptr<AbilityRecord> &abilityRecord) 3044eace7efcSopenharmony_ci{ 3045eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, false); 3046eace7efcSopenharmony_ci return UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType); 3047eace7efcSopenharmony_ci} 3048eace7efcSopenharmony_ci 3049eace7efcSopenharmony_cibool AbilityConnectManager::IsCacheExtensionAbilityType(const std::shared_ptr<AbilityRecord> &abilityRecord) 3050eace7efcSopenharmony_ci{ 3051eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, false); 3052eace7efcSopenharmony_ci return CacheExtensionUtils::IsCacheExtensionType(abilityRecord->GetAbilityInfo().extensionAbilityType); 3053eace7efcSopenharmony_ci} 3054eace7efcSopenharmony_ci 3055eace7efcSopenharmony_cibool AbilityConnectManager::CheckUIExtensionAbilitySessionExist( 3056eace7efcSopenharmony_ci const std::shared_ptr<AbilityRecord> &abilityRecord) 3057eace7efcSopenharmony_ci{ 3058eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, false); 3059eace7efcSopenharmony_ci std::lock_guard guard(uiExtensionMapMutex_); 3060eace7efcSopenharmony_ci for (auto it = uiExtensionMap_.begin(); it != uiExtensionMap_.end(); ++it) { 3061eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> uiExtAbility = it->second.first.lock(); 3062eace7efcSopenharmony_ci if (abilityRecord == uiExtAbility) { 3063eace7efcSopenharmony_ci return true; 3064eace7efcSopenharmony_ci } 3065eace7efcSopenharmony_ci } 3066eace7efcSopenharmony_ci 3067eace7efcSopenharmony_ci return false; 3068eace7efcSopenharmony_ci} 3069eace7efcSopenharmony_ci 3070eace7efcSopenharmony_civoid AbilityConnectManager::RemoveUIExtensionAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord) 3071eace7efcSopenharmony_ci{ 3072eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 3073eace7efcSopenharmony_ci CHECK_POINTER(uiExtensionAbilityRecordMgr_); 3074eace7efcSopenharmony_ci if (abilityRecord->GetWant().GetBoolParam(IS_PRELOAD_UIEXTENSION_ABILITY, false)) { 3075eace7efcSopenharmony_ci ClearPreloadUIExtensionRecord(abilityRecord); 3076eace7efcSopenharmony_ci } 3077eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_->RemoveExtensionRecord(abilityRecord->GetUIExtensionAbilityId()); 3078eace7efcSopenharmony_ci} 3079eace7efcSopenharmony_ci 3080eace7efcSopenharmony_civoid AbilityConnectManager::AddUIExtensionAbilityRecordToTerminatedList( 3081eace7efcSopenharmony_ci const std::shared_ptr<AbilityRecord> &abilityRecord) 3082eace7efcSopenharmony_ci{ 3083eace7efcSopenharmony_ci CHECK_POINTER(abilityRecord); 3084eace7efcSopenharmony_ci CHECK_POINTER(uiExtensionAbilityRecordMgr_); 3085eace7efcSopenharmony_ci uiExtensionAbilityRecordMgr_->AddExtensionRecordToTerminatedList(abilityRecord->GetUIExtensionAbilityId()); 3086eace7efcSopenharmony_ci} 3087eace7efcSopenharmony_ci 3088eace7efcSopenharmony_cibool AbilityConnectManager::IsCallerValid(const std::shared_ptr<AbilityRecord> &abilityRecord) 3089eace7efcSopenharmony_ci{ 3090eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN_LOG(abilityRecord, false, "Invalid caller for UIExtension"); 3091eace7efcSopenharmony_ci auto sessionInfo = abilityRecord->GetSessionInfo(); 3092eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN_LOG(sessionInfo, false, "Invalid caller for UIExtension"); 3093eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN_LOG(sessionInfo->sessionToken, false, "Invalid caller for UIExtension"); 3094eace7efcSopenharmony_ci std::lock_guard lock(uiExtRecipientMapMutex_); 3095eace7efcSopenharmony_ci if (uiExtRecipientMap_.find(sessionInfo->sessionToken) == uiExtRecipientMap_.end()) { 3096eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "invalid caller for UIExtension"); 3097eace7efcSopenharmony_ci return false; 3098eace7efcSopenharmony_ci } 3099eace7efcSopenharmony_ci 3100eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "The caller survival."); 3101eace7efcSopenharmony_ci return true; 3102eace7efcSopenharmony_ci} 3103eace7efcSopenharmony_ci 3104eace7efcSopenharmony_cistd::shared_ptr<AAFwk::AbilityRecord> AbilityConnectManager::GetUIExtensionRootHostInfo(const sptr<IRemoteObject> token) 3105eace7efcSopenharmony_ci{ 3106eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(token, nullptr); 3107eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, nullptr); 3108eace7efcSopenharmony_ci return uiExtensionAbilityRecordMgr_->GetUIExtensionRootHostInfo(token); 3109eace7efcSopenharmony_ci} 3110eace7efcSopenharmony_ci 3111eace7efcSopenharmony_ciint32_t AbilityConnectManager::GetUIExtensionSessionInfo(const sptr<IRemoteObject> token, 3112eace7efcSopenharmony_ci UIExtensionSessionInfo &uiExtensionSessionInfo) 3113eace7efcSopenharmony_ci{ 3114eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(token, ERR_NULL_OBJECT); 3115eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, ERR_NULL_OBJECT); 3116eace7efcSopenharmony_ci return uiExtensionAbilityRecordMgr_->GetUIExtensionSessionInfo(token, uiExtensionSessionInfo); 3117eace7efcSopenharmony_ci} 3118eace7efcSopenharmony_ci 3119eace7efcSopenharmony_civoid AbilityConnectManager::SignRestartAppFlag(int32_t uid) 3120eace7efcSopenharmony_ci{ 3121eace7efcSopenharmony_ci { 3122eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 3123eace7efcSopenharmony_ci for (auto &[key, abilityRecord] : serviceMap_) { 3124eace7efcSopenharmony_ci if (abilityRecord == nullptr || abilityRecord->GetUid() != uid) { 3125eace7efcSopenharmony_ci continue; 3126eace7efcSopenharmony_ci } 3127eace7efcSopenharmony_ci abilityRecord->SetRestartAppFlag(true); 3128eace7efcSopenharmony_ci } 3129eace7efcSopenharmony_ci } 3130eace7efcSopenharmony_ci AbilityCacheManager::GetInstance().SignRestartAppFlag(uid); 3131eace7efcSopenharmony_ci} 3132eace7efcSopenharmony_ci 3133eace7efcSopenharmony_cibool AbilityConnectManager::AddToServiceMap(const std::string &key, std::shared_ptr<AbilityRecord> abilityRecord) 3134eace7efcSopenharmony_ci{ 3135eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 3136eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 3137eace7efcSopenharmony_ci return false; 3138eace7efcSopenharmony_ci } 3139eace7efcSopenharmony_ci auto insert = serviceMap_.emplace(key, abilityRecord); 3140eace7efcSopenharmony_ci if (!insert.second) { 3141eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "record exist: %{public}s", key.c_str()); 3142eace7efcSopenharmony_ci } 3143eace7efcSopenharmony_ci return insert.second; 3144eace7efcSopenharmony_ci} 3145eace7efcSopenharmony_ci 3146eace7efcSopenharmony_ciAbilityConnectManager::ServiceMapType AbilityConnectManager::GetServiceMap() 3147eace7efcSopenharmony_ci{ 3148eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 3149eace7efcSopenharmony_ci return serviceMap_; 3150eace7efcSopenharmony_ci} 3151eace7efcSopenharmony_ci 3152eace7efcSopenharmony_civoid AbilityConnectManager::AddConnectObjectToMap(sptr<IRemoteObject> connectObject, 3153eace7efcSopenharmony_ci const ConnectListType &connectRecordList, bool updateOnly) 3154eace7efcSopenharmony_ci{ 3155eace7efcSopenharmony_ci if (!updateOnly) { 3156eace7efcSopenharmony_ci AddConnectDeathRecipient(connectObject); 3157eace7efcSopenharmony_ci } 3158eace7efcSopenharmony_ci std::lock_guard guard(connectMapMutex_); 3159eace7efcSopenharmony_ci connectMap_[connectObject] = connectRecordList; 3160eace7efcSopenharmony_ci} 3161eace7efcSopenharmony_ci 3162eace7efcSopenharmony_ciEventInfo AbilityConnectManager::BuildEventInfo(const std::shared_ptr<AbilityRecord> &abilityRecord) 3163eace7efcSopenharmony_ci{ 3164eace7efcSopenharmony_ci EventInfo eventInfo; 3165eace7efcSopenharmony_ci if (abilityRecord == nullptr) { 3166eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityRecord null"); 3167eace7efcSopenharmony_ci return eventInfo; 3168eace7efcSopenharmony_ci } 3169eace7efcSopenharmony_ci AppExecFwk::RunningProcessInfo processInfo; 3170eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByToken(abilityRecord->GetToken(), processInfo); 3171eace7efcSopenharmony_ci eventInfo.pid = processInfo.pid_; 3172eace7efcSopenharmony_ci eventInfo.processName = processInfo.processName_; 3173eace7efcSopenharmony_ci eventInfo.time = std::chrono::duration_cast<std::chrono::milliseconds>( 3174eace7efcSopenharmony_ci std::chrono::system_clock::now().time_since_epoch()).count(); 3175eace7efcSopenharmony_ci auto callerPid = abilityRecord->GetWant().GetIntParam(Want::PARAM_RESV_CALLER_PID, -1); 3176eace7efcSopenharmony_ci eventInfo.callerPid = callerPid == -1 ? IPCSkeleton::GetCallingPid() : callerPid; 3177eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByPid(eventInfo.callerPid, processInfo); 3178eace7efcSopenharmony_ci eventInfo.callerPid = processInfo.pid_; 3179eace7efcSopenharmony_ci eventInfo.callerProcessName = processInfo.processName_; 3180eace7efcSopenharmony_ci if (!abilityRecord->IsCreateByConnect()) { 3181eace7efcSopenharmony_ci auto abilityInfo = abilityRecord->GetAbilityInfo(); 3182eace7efcSopenharmony_ci eventInfo.extensionType = static_cast<int32_t>(abilityInfo.extensionAbilityType); 3183eace7efcSopenharmony_ci eventInfo.userId = userId_; 3184eace7efcSopenharmony_ci eventInfo.bundleName = abilityInfo.bundleName; 3185eace7efcSopenharmony_ci eventInfo.moduleName = abilityInfo.moduleName; 3186eace7efcSopenharmony_ci eventInfo.abilityName = abilityInfo.name; 3187eace7efcSopenharmony_ci } 3188eace7efcSopenharmony_ci return eventInfo; 3189eace7efcSopenharmony_ci} 3190eace7efcSopenharmony_ci 3191eace7efcSopenharmony_civoid AbilityConnectManager::UpdateUIExtensionInfo(const std::shared_ptr<AbilityRecord> &abilityRecord) 3192eace7efcSopenharmony_ci{ 3193eace7efcSopenharmony_ci if (abilityRecord == nullptr || 3194eace7efcSopenharmony_ci !UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) { 3195eace7efcSopenharmony_ci return; 3196eace7efcSopenharmony_ci } 3197eace7efcSopenharmony_ci 3198eace7efcSopenharmony_ci WantParams wantParams; 3199eace7efcSopenharmony_ci auto uiExtensionAbilityId = abilityRecord->GetUIExtensionAbilityId(); 3200eace7efcSopenharmony_ci wantParams.SetParam(UIEXTENSION_ABILITY_ID, AAFwk::Integer::Box(uiExtensionAbilityId)); 3201eace7efcSopenharmony_ci auto rootHostRecord = GetUIExtensionRootHostInfo(abilityRecord->GetToken()); 3202eace7efcSopenharmony_ci if (rootHostRecord != nullptr) { 3203eace7efcSopenharmony_ci auto rootHostPid = rootHostRecord->GetPid(); 3204eace7efcSopenharmony_ci wantParams.SetParam(UIEXTENSION_ROOT_HOST_PID, AAFwk::Integer::Box(rootHostPid)); 3205eace7efcSopenharmony_ci } 3206eace7efcSopenharmony_ci if (abilityRecord->GetWant().GetBoolParam(IS_PRELOAD_UIEXTENSION_ABILITY, false)) { 3207eace7efcSopenharmony_ci // Applicable only to preloadUIExtension scenarios 3208eace7efcSopenharmony_ci auto rootHostPid = IPCSkeleton::GetCallingPid(); 3209eace7efcSopenharmony_ci wantParams.SetParam(UIEXTENSION_ROOT_HOST_PID, AAFwk::Integer::Box(rootHostPid)); 3210eace7efcSopenharmony_ci } 3211eace7efcSopenharmony_ci abilityRecord->UpdateUIExtensionInfo(wantParams); 3212eace7efcSopenharmony_ci} 3213eace7efcSopenharmony_ci 3214eace7efcSopenharmony_cistd::string AbilityConnectManager::GenerateBundleName(const AbilityRequest &abilityRequest) const 3215eace7efcSopenharmony_ci{ 3216eace7efcSopenharmony_ci auto bundleName = abilityRequest.abilityInfo.bundleName; 3217eace7efcSopenharmony_ci if (MultiInstanceUtils::IsMultiInstanceApp(abilityRequest.appInfo)) { 3218eace7efcSopenharmony_ci bundleName = bundleName + '-' + MultiInstanceUtils::GetValidExtensionInstanceKey(abilityRequest); 3219eace7efcSopenharmony_ci return bundleName; 3220eace7efcSopenharmony_ci } 3221eace7efcSopenharmony_ci if (AbilityRuntime::StartupUtil::IsSupportAppClone(abilityRequest.abilityInfo.extensionAbilityType)) { 3222eace7efcSopenharmony_ci auto appCloneIndex = abilityRequest.want.GetIntParam(Want::PARAM_APP_CLONE_INDEX_KEY, 0); 3223eace7efcSopenharmony_ci if (appCloneIndex > 0) { 3224eace7efcSopenharmony_ci bundleName = std::to_string(appCloneIndex) + bundleName; 3225eace7efcSopenharmony_ci } 3226eace7efcSopenharmony_ci } 3227eace7efcSopenharmony_ci return bundleName; 3228eace7efcSopenharmony_ci} 3229eace7efcSopenharmony_ci 3230eace7efcSopenharmony_ciint32_t AbilityConnectManager::ReportXiaoYiToRSSIfNeeded(const AppExecFwk::AbilityInfo &abilityInfo) 3231eace7efcSopenharmony_ci{ 3232eace7efcSopenharmony_ci if (abilityInfo.type != AppExecFwk::AbilityType::EXTENSION || 3233eace7efcSopenharmony_ci abilityInfo.bundleName != XIAOYI_BUNDLE_NAME) { 3234eace7efcSopenharmony_ci return ERR_OK; 3235eace7efcSopenharmony_ci } 3236eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, 3237eace7efcSopenharmony_ci "bundleName is extension, abilityName:%{public}s", 3238eace7efcSopenharmony_ci abilityInfo.name.c_str()); 3239eace7efcSopenharmony_ci auto ret = ReportAbilityStartInfoToRSS(abilityInfo); 3240eace7efcSopenharmony_ci if (ret != ERR_OK) { 3241eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "fail, ret:%{public}d", ret); 3242eace7efcSopenharmony_ci return ret; 3243eace7efcSopenharmony_ci } 3244eace7efcSopenharmony_ci return ERR_OK; 3245eace7efcSopenharmony_ci} 3246eace7efcSopenharmony_ci 3247eace7efcSopenharmony_ciint32_t AbilityConnectManager::ReportAbilityStartInfoToRSS(const AppExecFwk::AbilityInfo &abilityInfo) 3248eace7efcSopenharmony_ci{ 3249eace7efcSopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 3250eace7efcSopenharmony_ci std::vector<AppExecFwk::RunningProcessInfo> runningProcessInfos; 3251eace7efcSopenharmony_ci auto ret = IN_PROCESS_CALL(DelayedSingleton<AppScheduler>::GetInstance()->GetProcessRunningInfos( 3252eace7efcSopenharmony_ci runningProcessInfos)); 3253eace7efcSopenharmony_ci if (ret != ERR_OK) { 3254eace7efcSopenharmony_ci return ret; 3255eace7efcSopenharmony_ci } 3256eace7efcSopenharmony_ci bool isColdStart = true; 3257eace7efcSopenharmony_ci int32_t pid = 0; 3258eace7efcSopenharmony_ci for (auto const &info : runningProcessInfos) { 3259eace7efcSopenharmony_ci if (info.uid_ == abilityInfo.applicationInfo.uid) { 3260eace7efcSopenharmony_ci isColdStart = false; 3261eace7efcSopenharmony_ci pid = info.pid_; 3262eace7efcSopenharmony_ci break; 3263eace7efcSopenharmony_ci } 3264eace7efcSopenharmony_ci } 3265eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "ReportAbilityStartInfoToRSS, abilityName:%{public}s", abilityInfo.name.c_str()); 3266eace7efcSopenharmony_ci ResSchedUtil::GetInstance().ReportAbilityStartInfoToRSS(abilityInfo, pid, isColdStart); 3267eace7efcSopenharmony_ci return ERR_OK; 3268eace7efcSopenharmony_ci} 3269eace7efcSopenharmony_ci 3270eace7efcSopenharmony_civoid AbilityConnectManager::UninstallApp(const std::string &bundleName) 3271eace7efcSopenharmony_ci{ 3272eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 3273eace7efcSopenharmony_ci for (const auto &[key, abilityRecord]: serviceMap_) { 3274eace7efcSopenharmony_ci if (abilityRecord && abilityRecord->GetAbilityInfo().bundleName == bundleName) { 3275eace7efcSopenharmony_ci abilityRecord->SetKeepAliveBundle(false); 3276eace7efcSopenharmony_ci } 3277eace7efcSopenharmony_ci } 3278eace7efcSopenharmony_ci} 3279eace7efcSopenharmony_ci 3280eace7efcSopenharmony_ciint32_t AbilityConnectManager::UpdateKeepAliveEnableState(const std::string &bundleName, 3281eace7efcSopenharmony_ci const std::string &moduleName, const std::string &mainElement, bool updateEnable) 3282eace7efcSopenharmony_ci{ 3283eace7efcSopenharmony_ci std::lock_guard lock(serviceMapMutex_); 3284eace7efcSopenharmony_ci for (const auto &[key, abilityRecord]: serviceMap_) { 3285eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(abilityRecord, ERR_NULL_OBJECT); 3286eace7efcSopenharmony_ci if (abilityRecord->GetAbilityInfo().bundleName == bundleName && 3287eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().name == mainElement && 3288eace7efcSopenharmony_ci abilityRecord->GetAbilityInfo().moduleName == moduleName) { 3289eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, 3290eace7efcSopenharmony_ci "update keepAlive,bundle:%{public}s,module:%{public}s,ability:%{public}s,enable:%{public}d", 3291eace7efcSopenharmony_ci bundleName.c_str(), moduleName.c_str(), mainElement.c_str(), updateEnable); 3292eace7efcSopenharmony_ci abilityRecord->SetKeepAliveBundle(updateEnable); 3293eace7efcSopenharmony_ci return ERR_OK; 3294eace7efcSopenharmony_ci } 3295eace7efcSopenharmony_ci } 3296eace7efcSopenharmony_ci return ERR_OK; 3297eace7efcSopenharmony_ci} 3298eace7efcSopenharmony_ci} // namespace AAFwk 3299eace7efcSopenharmony_ci} // namespace OHOS 3300