diff --git a/frameworks/js/napi/application/BUILD.gn b/frameworks/js/napi/application/BUILD.gn index 6e38f0181c5b7a6ec7ee6b60e1abb1a4356a493e..08b55e3f0185f874014262adf4b1fcd5647766e2 100644 --- a/frameworks/js/napi/application/BUILD.gn +++ b/frameworks/js/napi/application/BUILD.gn @@ -40,6 +40,7 @@ ohos_shared_library("application_napi") { configs = [ "${ability_runtime_services_path}/common:common_config" ] deps = [ + "${ability_runtime_innerkits_path}/app_manager:app_manager", "${ability_runtime_innerkits_path}/error_utils:ability_runtime_error_util", "${ability_runtime_innerkits_path}/napi_base_context:napi_base_context", "${ability_runtime_innerkits_path}/runtime:runtime", diff --git a/frameworks/js/napi/application/js_application.cpp b/frameworks/js/napi/application/js_application.cpp index bb81f8aa6250601d3662e9500f5e5be373db639e..06c192aa02eaa0fd911f8bddaf8f5af61201879e 100644 --- a/frameworks/js/napi/application/js_application.cpp +++ b/frameworks/js/napi/application/js_application.cpp @@ -17,6 +17,7 @@ #include "ability_runtime_error_util.h" #include "accesstoken_kit.h" +#include "app_mgr_client.h" #include "context_impl.h" #include "hilog_tag_wrapper.h" #include "js_application_context_utils.h" @@ -24,6 +25,7 @@ #include "js_runtime_utils.h" #include "js_context_utils.h" #include "napi_base_context.h" +#include "singleton.h" namespace OHOS { namespace AbilityRuntime { @@ -400,6 +402,83 @@ napi_value JsApplication::CreateJsContext(napi_env env, const std::shared_ptr(ERR_OK); + NapiAsyncTask::ExecuteCallback execute = [isInsertToHead, errCode]() { + auto appMgrClient = DelayedSingleton::GetInstance(); + if (appMgrClient == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "Null appMgrClient"); + *errCode = static_cast(AbilityErrorCode::ERROR_CODE_INNER); + } + *errCode = appMgrClient->PromoteCurrentToCandidateMasterProcess(isInsertToHead); + }; + NapiAsyncTask::CompleteCallback complete = [errCode](napi_env env, NapiAsyncTask& task, int32_t status) { + if (*errCode == ERR_OK) { + TAG_LOGD(AAFwkTag::APPKIT, "promote to standby master process success"); + task.ResolveWithNoError(env, CreateJsUndefined(env)); + return ; + } + task.Reject(env, CreateJsErrorByNativeErr(env, *errCode)); + }; + napi_value result = nullptr; + NapiAsyncTask::ScheduleHighQos("JsApplication::OnPromoteCurrentToCandidateMasterProcess", + env, CreateAsyncTaskWithLastParam(env, nullptr, std::move(execute), std::move(complete), &result)); + return result; +} + +napi_value JsApplication::DemoteCurrentFromCandidateMasterProcess(napi_env env, napi_callback_info info) +{ + TAG_LOGD(AAFwkTag::APPKIT, "called"); + GET_NAPI_INFO_AND_CALL(env, info, JsApplication, OnDemoteCurrentFromCandidateMasterProcess); +} + +napi_value JsApplication::OnDemoteCurrentFromCandidateMasterProcess(napi_env env, NapiCallbackInfo& info) +{ + auto errCode = std::make_shared(ERR_OK); + NapiAsyncTask::ExecuteCallback execute = [errCode]() { + auto appMgrClient = DelayedSingleton::GetInstance(); + if (appMgrClient == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "Null appMgrClient"); + *errCode = static_cast(AbilityErrorCode::ERROR_CODE_INNER); + } + *errCode = appMgrClient->DemoteCurrentFromCandidateMasterProcess(); + }; + NapiAsyncTask::CompleteCallback complete = [errCode](napi_env env, NapiAsyncTask& task, int32_t status) { + if (*errCode == ERR_OK) { + TAG_LOGD(AAFwkTag::APPKIT, "demote to standby master process success"); + task.ResolveWithNoError(env, CreateJsUndefined(env)); + return ; + } + task.Reject(env, CreateJsErrorByNativeErr(env, *errCode)); + }; + napi_value result = nullptr; + NapiAsyncTask::ScheduleHighQos("JsApplication::OnDemoteCurrentFromCandidateMasterProcess", + env, CreateAsyncTaskWithLastParam(env, nullptr, std::move(execute), std::move(complete), &result)); + return result; +} + napi_value ApplicationInit(napi_env env, napi_value exportObj) { TAG_LOGD(AAFwkTag::APPKIT, "Called"); @@ -423,6 +502,13 @@ napi_value ApplicationInit(napi_env env, napi_value exportObj) BindNativeFunction(env, exportObj, "createPluginModuleContext", moduleName, JsApplication::CreatePluginModuleContext); + + BindNativeFunction(env, exportObj, "promoteCurrentToCandidateMasterProcess", moduleName, + JsApplication::PromoteCurrentToCandidateMasterProcess); + + BindNativeFunction(env, exportObj, "demoteCurrentFromCandidateMasterProcess", moduleName, + JsApplication::DemoteCurrentFromCandidateMasterProcess); + return CreateJsUndefined(env); } } // namespace AbilityRuntime diff --git a/frameworks/js/napi/application/js_application.h b/frameworks/js/napi/application/js_application.h index 06785115152aac8e699f765c9bafea47d108ebe0..13164547bbfc36d27469f6df4112aed4f4ce9b1d 100644 --- a/frameworks/js/napi/application/js_application.h +++ b/frameworks/js/napi/application/js_application.h @@ -35,6 +35,8 @@ public: static napi_value CreateBundleContext(napi_env env, napi_callback_info info); static napi_value GetApplicationContext(napi_env env, napi_callback_info info); static napi_value CreatePluginModuleContext(napi_env env, napi_callback_info info); + static napi_value PromoteCurrentToCandidateMasterProcess(napi_env env, napi_callback_info info); + static napi_value DemoteCurrentFromCandidateMasterProcess(napi_env env, napi_callback_info info); private: napi_value OnCreateModuleContext(napi_env env, NapiCallbackInfo &info); @@ -46,6 +48,8 @@ private: bool CheckCallerIsSystemApp(); bool CheckCallerPermission(const std::string &permission); napi_value OnGetApplicationContext(napi_env env, NapiCallbackInfo &info); + napi_value OnPromoteCurrentToCandidateMasterProcess(napi_env env, NapiCallbackInfo& info); + napi_value OnDemoteCurrentFromCandidateMasterProcess(napi_env env, NapiCallbackInfo& info); }; napi_value ApplicationInit(napi_env env, napi_value exportObj); diff --git a/frameworks/native/ability/native/ability_business_error/ability_business_error.cpp b/frameworks/native/ability/native/ability_business_error/ability_business_error.cpp index 96355dcd6c5453e3128e8e9972ceb84d64788307..2cca9a6cdcc73bac32a105398638da497d67b625 100644 --- a/frameworks/native/ability/native/ability_business_error/ability_business_error.cpp +++ b/frameworks/native/ability/native/ability_business_error/ability_business_error.cpp @@ -124,6 +124,13 @@ constexpr const char* ERROR_MSG_NOT_IN_KIOSK_MODE = "The current application is not in the kiosk mode. Exit is not allowed."; constexpr const char* ERROR_MSG_APP_NOT_IN_FOCUS = "The current ability is not foreground."; +constexpr const char* ERROR_MSG_NOT_ISOLATION_PROCESS = "Current process is not running a component configured " + "with \"isolationProcess\", and cannot be set as a candidate master process."; +constexpr const char* ERROR_MSG_ALREADY_MASTER_PROCESS = + "Current process is already a master process, revocation is not supported."; +constexpr const char* ERROR_MSG_NOT_CANDIDATE_MASTER_PROCESS = + "Current process is not a candidate master process, no need to revocation."; + // follow ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST of appexecfwk_errors.h in bundle_framework constexpr int32_t ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST = 8521220; @@ -214,6 +221,9 @@ static std::unordered_map ERR_CODE_MAP = { { AbilityErrorCode::ERROR_CODE_ALREADY_IN_KIOSK_MODE, ERROR_MSG_ALREADY_IN_KIOSK_MODE}, { AbilityErrorCode::ERROR_CODE_NOT_IN_KIOSK_MODE, ERROR_MSG_NOT_IN_KIOSK_MODE}, { AbilityErrorCode::ERROR_CODE_APP_NOT_IN_FOCUS, ERROR_MSG_APP_NOT_IN_FOCUS}, + { AbilityErrorCode::ERROR_CODE_NOT_ISOLATION_PROCESS, ERROR_MSG_NOT_ISOLATION_PROCESS}, + { AbilityErrorCode::ERROR_CODE_ALREADY_MASTER_PROCESS, ERROR_MSG_ALREADY_MASTER_PROCESS}, + { AbilityErrorCode::ERROR_CODE_NOT_CANDIDATE_MASTER_PROCESS, ERROR_MSG_NOT_CANDIDATE_MASTER_PROCESS }, }; static std::unordered_map INNER_TO_JS_ERROR_CODE_MAP { @@ -307,6 +317,9 @@ static std::unordered_map INNER_TO_JS_ERROR_CODE_MAP {ERR_ALREADY_IN_KIOSK_MODE, AbilityErrorCode::ERROR_CODE_ALREADY_IN_KIOSK_MODE}, {ERR_NOT_IN_KIOSK_MODE, AbilityErrorCode::ERROR_CODE_NOT_IN_KIOSK_MODE}, {ERR_APP_NOT_IN_FOCUS, AbilityErrorCode::ERROR_CODE_APP_NOT_IN_FOCUS}, + {ERR_NOT_ISOLATION_PROCESS, AbilityErrorCode::ERROR_CODE_NOT_ISOLATION_PROCESS}, + {ERR_ALREADY_MASTER_PROCESS, AbilityErrorCode::ERROR_CODE_ALREADY_MASTER_PROCESS}, + {ERR_NOT_CANDIDATE_MASTER_PROCESS, AbilityErrorCode::ERROR_CODE_NOT_CANDIDATE_MASTER_PROCESS}, }; } diff --git a/interfaces/inner_api/ability_manager/include/ability_manager_errors.h b/interfaces/inner_api/ability_manager/include/ability_manager_errors.h index 87c485af117203e6366924447b492201ad0ec5fc..b6c37b54754cff1591bae61400acb27f0ed25370 100644 --- a/interfaces/inner_api/ability_manager/include/ability_manager_errors.h +++ b/interfaces/inner_api/ability_manager/include/ability_manager_errors.h @@ -903,6 +903,21 @@ enum { */ ERR_SA_INTERCEPTOR_READ_PARAMS_FAILED = 2097361, + /* + * Result(2097364) Current process is not running a component configured with "isolationProcess". + */ + ERR_NOT_ISOLATION_PROCESS = 2097364, + + /* + * Result(2097365) Current process is already a master process, revocation is not supported. + */ + ERR_ALREADY_MASTER_PROCESS = 2097365, + + /* + * Result(2097366) Current process is not a candidate master process, no need to revocation. + */ + ERR_NOT_CANDIDATE_MASTER_PROCESS = 2097366, + /** * Native error(3000000) for target bundle not exist. */ diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h index 485f59efb020e9a625bd74c3d4a90008aa8f717a..a764ccb3358777edce85f575e782e9ba1b6b58d4 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h @@ -987,6 +987,21 @@ public: virtual AppMgrResultCode IsAppRunningByBundleNameAndUserId(const std::string &bundleName, int32_t userId, bool &isRunning); + /** + * Elevate the current process to be a candidate master process. + * + * @param isInsertToHead Whether inset current process to the head of candidate master process list. + * @return Return ERR_OK if success, others fail. + */ + int32_t PromoteCurrentToCandidateMasterProcess(bool isInsertToHead); + + /** + * Revoke current process as a candidate master process. + * + * @return Return ERR_OK if success, others fail. + */ + int32_t DemoteCurrentFromCandidateMasterProcess(); + private: void SetServiceManager(std::unique_ptr serviceMgr); /** diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h index 78c33ca8ff10759c433cbe893e184c8a80e51de8..fe712b24851225e56d82373e9ea77b6883ddb166 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h @@ -1001,6 +1001,16 @@ public: { return 0; } + + virtual int32_t PromoteCurrentToCandidateMasterProcess(bool isInsertToHead) + { + return 0; + } + + virtual int32_t DemoteCurrentFromCandidateMasterProcess() + { + return 0; + } }; } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h index 8b68417f09702db03bafb15e596f5cc060269ee9..e11dac6b00e878347ef2b6ede47602e1e11f1fcd 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_ipc_interface_code.h @@ -132,6 +132,8 @@ enum class AppMgrInterfaceCode { UNREGISTER_NATIVE_CHILD_EXIT_NOTIFY = 106, UPDATE_CONFIGURATION_POLICY = 107, DUMP_CJHEAP_MEMORY_PROCESS = 108, + PROMOTE_CURRENT_TO_CANDIDATE_MASTER_PROCESS = 109, + DEMOTE_CURRENT_FROM_CANDIDATE_MASTER_PROCESS = 110, }; } // AppExecFwk } // OHOS diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h index 9473dcfbe3e6e6c119c2d2b73f9438b3ff93d31f..b9988bfb9dc62f979f291faecb3fbcc054090741 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h @@ -847,6 +847,21 @@ public: * @return Returns ERR_OK on success, others on failure. */ int32_t LaunchAbility(sptr token) override; + +/** + * Elevate the current process to be a candidate master process. + * + * @param isInsertToHead Whether inset current process to the head of candidate master process list. + * @return Return ERR_OK if success, others fail. + */ + int32_t PromoteCurrentToCandidateMasterProcess(bool isInsertToHead) override; + + /** + * Revoke current process as a candidate master process. + * + * @return Return ERR_OK if success, others fail. + */ + int32_t DemoteCurrentFromCandidateMasterProcess() override; private: bool SendTransactCmd(AppMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply); diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h index ca9c4a8c4c273436483433c59b886d52f29f8b10..bc71a74bf66b6889d9f8ca5e1f0d47c0d384a0de 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h @@ -197,6 +197,8 @@ private: int32_t HandleUpdateProcessMemoryState(MessageParcel &data, MessageParcel &reply); int32_t HandleGetKilledProcessInfo(MessageParcel &data, MessageParcel &reply); int32_t HandleLaunchAbility(MessageParcel &data, MessageParcel &reply); + int32_t HandlePromoteCurrentToCandidateMasterProcess(MessageParcel &data, MessageParcel &reply); + int32_t HandleDemoteCurrentFromCandidateMasterProcess(MessageParcel &data, MessageParcel &reply); DISALLOW_COPY_AND_MOVE(AppMgrStub); }; } // namespace AppExecFwk diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp index 03b60f7458542478b48da29c35a452b2fb8b0cb3..bb49f3dad0fbc5a37fafe21397d3c78e2b0aa03d 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp @@ -1592,5 +1592,30 @@ AppMgrResultCode AppMgrClient::IsAppRunningByBundleNameAndUserId(const std::stri } return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED; } + +int32_t AppMgrClient::PromoteCurrentToCandidateMasterProcess(bool isInsertToHead) +{ + TAG_LOGI(AAFwkTag::APPMGR, "Called"); + sptr service = iface_cast(mgrHolder_->GetRemoteObject()); + if (service == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr."); + return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED; + } + return service->PromoteCurrentToCandidateMasterProcess(isInsertToHead); +} + +int32_t AppMgrClient::DemoteCurrentFromCandidateMasterProcess() +{ + TAG_LOGI(AAFwkTag::APPMGR, "Called"); + sptr service = iface_cast(mgrHolder_->GetRemoteObject()); + if (service == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr."); + return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED; + } + return service->DemoteCurrentFromCandidateMasterProcess(); +} + + + } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp index 8464da2409b920e861b7b31ca2b4f57f583399c9..40b64da0006b9cf5ec9b80085aead359ba1bfaa7 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp @@ -2424,5 +2424,38 @@ int32_t AppMgrProxy::LaunchAbility(sptr token) PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::LAUNCH_ABILITY, data, reply, option); return reply.ReadInt32(); } + +int32_t AppMgrProxy::PromoteCurrentToCandidateMasterProcess(bool isInsertToHead) +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + MessageParcel data; + if (!WriteInterfaceToken(data)) { + TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed."); + return ERR_INVALID_DATA; + } + PARCEL_UTIL_WRITE_RET_INT(data, Bool, isInsertToHead); + + MessageParcel reply; + MessageOption option; + + PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::PROMOTE_CURRENT_TO_CANDIDATE_MASTER_PROCESS, data, reply, option); + return reply.ReadInt32(); +} + +int32_t AppMgrProxy::DemoteCurrentFromCandidateMasterProcess() +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!WriteInterfaceToken(data)) { + TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed."); + return ERR_INVALID_DATA; + } + + PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::DEMOTE_CURRENT_FROM_CANDIDATE_MASTER_PROCESS, data, reply, option); + return reply.ReadInt32(); +} + } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp index 262597ebb037a36fb7a39a7b371acb1966b24dbb..7e29aaca058978e0eae7a617b2322caa6e8624c3 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp @@ -400,6 +400,10 @@ int32_t AppMgrStub::OnRemoteRequestInnerEighth(uint32_t code, MessageParcel &dat return HandleLaunchAbility(data, reply); case static_cast(AppMgrInterfaceCode::UPDATE_CONFIGURATION_POLICY): return HandleUpdateConfigurationForBackgroundApp(data, reply); + case static_cast(AppMgrInterfaceCode::PROMOTE_CURRENT_TO_CANDIDATE_MASTER_PROCESS): + return HandlePromoteCurrentToCandidateMasterProcess(data, reply); + case static_cast(AppMgrInterfaceCode::DEMOTE_CURRENT_FROM_CANDIDATE_MASTER_PROCESS): + return HandleDemoteCurrentFromCandidateMasterProcess(data, reply); } return INVALID_FD; } @@ -2010,5 +2014,30 @@ int32_t AppMgrStub::HandleLaunchAbility(MessageParcel &data, MessageParcel &repl } return NO_ERROR; } + +int32_t AppMgrStub::HandlePromoteCurrentToCandidateMasterProcess(MessageParcel &data, MessageParcel &reply) +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + bool isInsertToHead = data.ReadBool(); + auto ret = PromoteCurrentToCandidateMasterProcess(isInsertToHead); + if (!reply.WriteInt32(ret)) { + TAG_LOGE(AAFwkTag::APPMGR, "Write ret error."); + return IPC_STUB_ERR; + } + return NO_ERROR; +} + +int32_t AppMgrStub::HandleDemoteCurrentFromCandidateMasterProcess(MessageParcel &data, MessageParcel &reply) +{ + TAG_LOGD(AAFwkTag::APPMGR, "call"); + int32_t ret = DemoteCurrentFromCandidateMasterProcess(); + if (!reply.WriteInt32(ret)) { + TAG_LOGE(AAFwkTag::APPMGR, "Write ret error."); + return IPC_STUB_ERR; + } + + return NO_ERROR; +} + } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/kits/native/ability/native/ability_business_error/ability_business_error.h b/interfaces/kits/native/ability/native/ability_business_error/ability_business_error.h index ef04055073dc69c62532f86b22e7b289ed97b630..4db6cf767e6266b3cbe7813c7760611cda833fec 100644 --- a/interfaces/kits/native/ability/native/ability_business_error/ability_business_error.h +++ b/interfaces/kits/native/ability/native/ability_business_error/ability_business_error.h @@ -261,6 +261,12 @@ enum class AbilityErrorCode { ERROR_CODE_NOT_IN_KIOSK_MODE = 16000112, ERROR_CODE_APP_NOT_IN_FOCUS = 16000113, + + ERROR_CODE_NOT_ISOLATION_PROCESS = 16000115, + + ERROR_CODE_ALREADY_MASTER_PROCESS = 16000116, + + ERROR_CODE_NOT_CANDIDATE_MASTER_PROCESS = 16000117, // target bundle is not in u1 ERROR_CODE_NO_U1 = 16000204, diff --git a/services/appmgr/include/app_mgr_service.h b/services/appmgr/include/app_mgr_service.h index 2777380081bd7ef9e3c54453aecd2aa966117714..d787e0bee264567dd23c9f8c041377b5fd880cb2 100644 --- a/services/appmgr/include/app_mgr_service.h +++ b/services/appmgr/include/app_mgr_service.h @@ -791,6 +791,21 @@ public: int32_t SetProcessCacheEnable(int32_t pid, bool enable) override; + /** + * Elevate the current process to be a candidate master process. + * + * @param isInsertToHead Whether inset current process to the head of candidate master process list. + * @return Return ERR_OK if success, others fail. + */ + int32_t PromoteCurrentToCandidateMasterProcess(bool isInsertToHead) override; + + /** + * Revoke current process as a candidate master process. + * + * @return Return ERR_OK if success, others fail. + */ + int32_t DemoteCurrentFromCandidateMasterProcess() override; + /** * set browser channel for caller */ diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index 43ea4ea946a0adc54ea4da7a46a0fc7b615e5676..f49c65482a059066bc509043f862a34884fc8a9c 100644 --- a/services/appmgr/include/app_mgr_service_inner.h +++ b/services/appmgr/include/app_mgr_service_inner.h @@ -1297,6 +1297,21 @@ public: virtual int32_t UnregisterNativeChildExitNotify(const sptr &callback); + /** + * Elevate the current process to be a candidate master process. + * + * @param isInsertToHead Whether inset current process to the head of candidate master process list. + * @return Return ERR_OK if success, others fail. + */ + int32_t PromoteCurrentToCandidateMasterProcess(bool isInsertToHead); + + /** + * Revoke current process as a candidate master process. + * + * @return Return ERR_OK if success, others fail. + */ + int32_t DemoteCurrentFromCandidateMasterProcess(); + /** * To clear the process by ability token. * diff --git a/services/appmgr/include/app_running_manager.h b/services/appmgr/include/app_running_manager.h index 128af80f880ec5d19cb90a55619f7e8608e86a47..4ed1444c1185b7ad187cc42d4a335f038f97a3a1 100644 --- a/services/appmgr/include/app_running_manager.h +++ b/services/appmgr/include/app_running_manager.h @@ -102,6 +102,21 @@ public: bool CheckAppRunningRecordIsExist(const std::string &bundleName, const std::string &abilityName); #endif + /** + * CheckMasterProcessAppRunningRecordIsExist, Get master process record by application name and ability information. + * + * @param appName, the application name. + * @param abilityInfo, the ability information. + * @param uid, the process uid. + * + * @return process record. + */ + std::shared_ptr FindMasterProcessAppRunningRecord(const std::string &appName, + const AppExecFwk::AbilityInfo &abilityInfo, const int uid); + + bool CheckMasterProcessAppRunningRecordIsExist(const std::string &appName, + const AppExecFwk::AbilityInfo &abilityInfo, const int uid); + /** * Check whether the process of the application exists. * @@ -438,6 +453,8 @@ private: void RemoveTimeoutDeadAppRecord(); void ExecuteConfigurationTask(const BackgroundAppInfo& info, const int32_t userId); bool UpdateConfiguration(std::shared_ptr &appRecord, Rosen::ConfigMode configMode); + bool IsSameAbilityType( + const std::shared_ptr &appRecord, const AppExecFwk::AbilityInfo &abilityInfo); private: std::mutex runningRecordMapMutex_; std::map> appRunningRecordMap_; diff --git a/services/appmgr/include/app_running_record.h b/services/appmgr/include/app_running_record.h index 15ced1407713bda81b64ca55ae91ece2464659f4..77f0c47535bc9adfb35e934f6eada57b80422adb 100644 --- a/services/appmgr/include/app_running_record.h +++ b/services/appmgr/include/app_running_record.h @@ -1098,6 +1098,26 @@ public: std::optional IsSupportMultiProcessDeviceFeature() const; void SetSupportMultiProcessDeviceFeature(bool support); + inline void SetMasterProcess(bool isMasterProcess) + { + isMasterProcess_ = isMasterProcess; + } + + inline bool GetIsMasterProcess() const + { + return isMasterProcess_; + } + + inline void SetTimeStamp(int64_t timeStamp) + { + timeStamp_ = timeStamp; + } + + inline int64_t GetTimeStamp() const + { + return timeStamp_; + } + private: /** * SearchTheModuleInfoNeedToUpdated, Get an uninitialized abilityStage data. @@ -1278,6 +1298,9 @@ private: std::shared_ptr startupTaskData_ = nullptr; ffrt::mutex startupTaskDataLock_; mutable ffrt::mutex killReasonLock_; + + bool isMasterProcess_ = false; // Only MasterProcess can be keepalive + int64_t timeStamp_ = 0; // the flag of BackUpMainControlProcess }; } // namespace AppExecFwk diff --git a/services/appmgr/src/app_mgr_service.cpp b/services/appmgr/src/app_mgr_service.cpp index b4b8c50a40893255d42ca9309ca670632caa6a22..43d3c630963e1ae7e997647a930fd6606260224b 100644 --- a/services/appmgr/src/app_mgr_service.cpp +++ b/services/appmgr/src/app_mgr_service.cpp @@ -1904,5 +1904,25 @@ int32_t AppMgrService::LaunchAbility(sptr token) } return appMgrServiceInner_->LaunchAbility(token); } + +int32_t AppMgrService::PromoteCurrentToCandidateMasterProcess(bool isInsertToHead) +{ + TAG_LOGD(AAFwkTag::APPMGR, "call"); + if (!IsReady()) { + TAG_LOGE(AAFwkTag::APPMGR, "not ready"); + return AAFwk::ERR_NULL_APP_MGR_SERVICE_INNER; + } + return appMgrServiceInner_->PromoteCurrentToCandidateMasterProcess(isInsertToHead); +} + +int32_t AppMgrService::DemoteCurrentFromCandidateMasterProcess() +{ + TAG_LOGD(AAFwkTag::APPMGR, "called"); + if (!IsReady()) { + TAG_LOGE(AAFwkTag::APPMGR, "not ready"); + return AAFwk::ERR_NULL_APP_MGR_SERVICE_INNER; + } + return appMgrServiceInner_->DemoteCurrentFromCandidateMasterProcess(); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 90206fe1dff944e40fa506bdef313b7ffaf69564..d096db2f29af72a414d3fd4c555ead06c08de468 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -466,29 +466,36 @@ void AppMgrServiceInner::StartSpecifiedProcess(const AAFwk::Want &want, const Ap MakeProcessName(abilityInfoPtr, appInfo, hapModuleInfo, appIndex, "", processName, false); TAG_LOGD(AAFwkTag::APPMGR, "processName = %{public}s", processName.c_str()); auto instanceKey = want.GetStringParam(Want::APP_INSTANCE_KEY); + auto masterAppRecord = appRunningManager_->FindMasterProcessAppRunningRecord(appInfo->name, abilityInfo, + appInfo->uid); std::string customProcessFlag = ""; - if (AAFwk::UIExtensionUtils::IsUIExtension(abilityInfo.extensionAbilityType)) { - customProcessFlag = customProcess; - } else { - customProcessFlag = abilityInfo.process; - } - auto mainAppRecord = appRunningManager_->CheckAppRunningRecordIsExist(appInfo->name, processName, appInfo->uid, - bundleInfo, "", nullptr, instanceKey, customProcessFlag); - if (mainAppRecord != nullptr) { - TAG_LOGI(AAFwkTag::APPMGR, "main process exists."); - mainAppRecord->SetScheduleNewProcessRequestState(requestId, want, hapModuleInfo.moduleName); - auto moduleRecord = mainAppRecord->GetModuleRecordByModuleName(appInfo->bundleName, hapModuleInfo.moduleName); + if (masterAppRecord == nullptr) { + if (AAFwk::UIExtensionUtils::IsUIExtension(abilityInfo.extensionAbilityType)) { + customProcessFlag = customProcess; + } else { + customProcessFlag = abilityInfo.process; + } + masterAppRecord = appRunningManager_->CheckAppRunningRecordIsExist(appInfo->name, processName, appInfo->uid, + bundleInfo, "", nullptr, instanceKey, customProcessFlag); + if(masterAppRecord != nullptr){ + masterAppRecord->SetMasterProcess(true); + } + } + if (masterAppRecord != nullptr) { + TAG_LOGI(AAFwkTag::APPMGR, "master process exists: %{public}s.", masterAppRecord->GetProcessName().c_str()); + masterAppRecord->SetScheduleNewProcessRequestState(requestId, want, hapModuleInfo.moduleName); + auto moduleRecord = masterAppRecord->GetModuleRecordByModuleName(appInfo->bundleName, hapModuleInfo.moduleName); if (!moduleRecord) { TAG_LOGI(AAFwkTag::APPMGR, "module record is nullptr, add modules"); std::vector hapModules = { hapModuleInfo }; - mainAppRecord->AddModules(appInfo, hapModules); - if (mainAppRecord->GetApplicationClient() != nullptr) { - mainAppRecord->AddAbilityStageBySpecifiedProcess(appInfo->bundleName); + masterAppRecord->AddModules(appInfo, hapModules); + if (masterAppRecord->GetApplicationClient() != nullptr) { + masterAppRecord->AddAbilityStageBySpecifiedProcess(appInfo->bundleName); } return; } TAG_LOGD(AAFwkTag::APPMGR, "schedule new process request."); - mainAppRecord->ScheduleNewProcessRequest(want, hapModuleInfo.moduleName); + masterAppRecord->ScheduleNewProcessRequest(want, hapModuleInfo.moduleName); return; } std::shared_ptr appRecord = nullptr; @@ -511,6 +518,7 @@ void AppMgrServiceInner::StartSpecifiedProcess(const AAFwk::Want &want, const Ap TAG_LOGE(AAFwkTag::APPMGR, "create new appRecord failed"); return; } + newAppRecord->SetMasterProcess(true); newAppRecord->SetScheduleNewProcessRequestState(requestId, want, hapModuleInfo.moduleName); bool appExistFlag = appRunningManager_->IsAppExist(bundleInfo.applicationInfo.accessTokenId); auto ret = StartProcess(abilityInfo.applicationName, processName, 0, newAppRecord, @@ -855,6 +863,15 @@ void AppMgrServiceInner::LoadAbility(std::shared_ptr abilityInfo, s } appRecord = CreateAppRunningRecord(loadParam, appInfo, abilityInfo, processName, bundleInfo, hapModuleInfo, want, isKia); + + if (abilityInfo->isolationProcess) { + bool isMasterProcessExist = appRunningManager_->CheckMasterProcessAppRunningRecordIsExist( + appInfo->name, *abilityInfo, appInfo->uid); + if (!isMasterProcessExist) { + appRecord->SetMasterProcess(true); + } + } + if (appRecord != nullptr) { appRecord->SetExtensionSandBoxFlag(isExtensionSandBox); } @@ -1133,7 +1150,6 @@ void AppMgrServiceInner::LoadAbilityNoAppRecord(const std::shared_ptrSetMainProcess(false); TAG_LOGI(AAFwkTag::APPMGR, "%{public}s will not alive", hapModuleInfo.process.c_str()); } - uint32_t startFlags = (want == nullptr) ? 0 : AppspawnUtil::BuildStartFlags(*want, *abilityInfo); int32_t bundleIndex = 0; if (want != nullptr) { @@ -10074,5 +10090,74 @@ bool AppMgrServiceInner::WrapBindInfo(std::shared_ptr &want, std::s bindInfo.processType = appRecord->GetProcessType(); return true; } + +int32_t AppMgrServiceInner::PromoteCurrentToCandidateMasterProcess(bool isInsertToHead) +{ + TAG_LOGI(AAFwkTag::APPMGR, "call"); + HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); + + if (!appRunningManager_) { + TAG_LOGE(AAFwkTag::APPMGR, "appRunningManager_ null"); + return AAFwk::ERR_NOT_ISOLATION_PROCESS; + } + + auto callerPid = IPCSkeleton::GetCallingPid(); + auto appRecord = GetAppRunningRecordByPid(callerPid); + if (!appRecord) { + TAG_LOGE(AAFwkTag::APPMGR, "no appRecord, callerPid:%{public}d", callerPid); + return AAFwk::ERR_NOT_ISOLATION_PROCESS; + } + + if (!AAFwk::AppUtils::GetInstance().IsStartSpecifiedProcess()) { + TAG_LOGE(AAFwkTag::APPMGR, "Capability not support"); + return AAFwk::ERR_CAPABILITY_NOT_SUPPORT; + } + + if (appRecord->GetSpecifiedProcessFlag() == "" && + !appRecord->GetIsMasterProcess()) { + TAG_LOGE(AAFwkTag::APPMGR, "Current process is not running a component " + "configured with \"isolationProcess\"."); + return AAFwk::ERR_NOT_ISOLATION_PROCESS; + } + + auto now = std::chrono::steady_clock::now().time_since_epoch(); + int64_t timeStamp = std::chrono::duration_cast(now).count(); + if(!isInsertToHead){ + timeStamp = -timeStamp; + } + appRecord->SetTimeStamp(timeStamp); + return ERR_OK; +} + +int32_t AppMgrServiceInner::DemoteCurrentFromCandidateMasterProcess() +{ + TAG_LOGD(AAFwkTag::APPMGR, "call"); + int32_t pid = IPCSkeleton::GetCallingPid(); + auto appRecord = GetAppRunningRecordByPid(pid); + if (appRecord == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "no appRecord"); + return AAFwk::ERR_NOT_CANDIDATE_MASTER_PROCESS; + } + + if (!AAFwk::AppUtils::GetInstance().IsStartSpecifiedProcess()) { + TAG_LOGE(AAFwkTag::APPMGR, "Capability not support"); + return AAFwk::ERR_CAPABILITY_NOT_SUPPORT; + } + + if (appRecord->GetIsMasterProcess()) { + TAG_LOGE(AAFwkTag::APPMGR, "Current process is already a master process"); + return AAFwk::ERR_ALREADY_MASTER_PROCESS; + } + + if (appRecord->GetTimeStamp() == 0) { + TAG_LOGE(AAFwkTag::APPMGR, "Current process is not a candidate master process"); + return AAFwk::ERR_NOT_CANDIDATE_MASTER_PROCESS; + } + + appRecord->SetTimeStamp(0); + + return ERR_OK; +} + } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/app_running_manager.cpp b/services/appmgr/src/app_running_manager.cpp index 3e7319ad46d1d52a3c2973399c2f3f35f61a5560..a3a4697686570a4b1e8f16a54d4047c689518163 100644 --- a/services/appmgr/src/app_running_manager.cpp +++ b/services/appmgr/src/app_running_manager.cpp @@ -255,6 +255,90 @@ bool AppRunningManager::CheckAppRunningRecordIsExist(const std::string &bundleNa } #endif +bool AppRunningManager::IsSameAbilityType(const std::shared_ptr &appRecord, + const AppExecFwk::AbilityInfo &abilityInfo) +{ + if (appRecord == nullptr) { + return false; + } + bool isUIAbility = (abilityInfo.type == AppExecFwk::AbilityType::PAGE); + bool isUIExtension = (abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::SYS_COMMON_UI); + if (((appRecord->GetProcessType() == ProcessType::NORMAL || + appRecord->GetExtensionType() == AppExecFwk::ExtensionAbilityType::SERVICE || + appRecord->GetExtensionType() == AppExecFwk::ExtensionAbilityType::DATASHARE) && isUIAbility)|| + (appRecord->GetExtensionType() == AppExecFwk::ExtensionAbilityType::SYS_COMMON_UI && isUIExtension)) { + return true; + } + return false; +} + +std::shared_ptr AppRunningManager::FindMasterProcessAppRunningRecord( + const std::string &appName, const AppExecFwk::AbilityInfo &abilityInfo, const int uid) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + TAG_LOGD(AAFwkTag::APPMGR, "uid: %{public}d: appName: %{public}s", uid, appName.c_str()); + auto appRunningMap = GetAppRunningRecordMap(); + int64_t maxTimeStamp = INT64_MIN; + int64_t minAppRecordId = INT32_MAX; + std::shared_ptr maxAppRecord; + std::shared_ptr minAppRecord; + bool isUIAbility = (abilityInfo.type == AppExecFwk::AbilityType::PAGE); + bool isUIExtension = (abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::SYS_COMMON_UI); + for (const auto &item : appRunningMap) { + const auto &appRecord = item.second; + if (!(appRecord && appRecord->GetUid() == uid)) { + continue; + } + + if (appRecord->GetIsMasterProcess() && IsSameAbilityType(appRecord, abilityInfo)) { + return appRecord; + } + + if (appRecord->GetTimeStamp() != 0 && maxTimeStamp < appRecord->GetTimeStamp() && + IsSameAbilityType(appRecord, abilityInfo)) { + maxTimeStamp = appRecord->GetTimeStamp(); + maxAppRecord = appRecord; + } + + if ((appRecord->GetExtensionType() == AppExecFwk::ExtensionAbilityType::SYS_COMMON_UI && isUIExtension) && + appRecord->GetRecordId() < minAppRecordId) { + minAppRecordId = appRecord->GetRecordId(); + minAppRecord = appRecord; + } + } + + if (maxAppRecord != nullptr) { + maxAppRecord->SetMasterProcess(true); + maxAppRecord->SetTimeStamp(0); + return maxAppRecord; + } + + if (minAppRecord != nullptr) { + minAppRecord->SetMasterProcess(true); + return minAppRecord; + } + + return nullptr; +} + +bool AppRunningManager::CheckMasterProcessAppRunningRecordIsExist( + const std::string &appName, const AppExecFwk::AbilityInfo &abilityInfo, const int uid) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + TAG_LOGD(AAFwkTag::APPMGR, "uid: %{public}d: appName: %{public}s", uid, appName.c_str()); + auto appRunningMap = GetAppRunningRecordMap(); + bool isUIAbility = (abilityInfo.type == AppExecFwk::AbilityType::PAGE); + bool isUIExtension = (abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::SYS_COMMON_UI); + for (const auto &item : appRunningMap) { + const auto &appRecord = item.second; + if (appRecord && appRecord->GetUid() == uid && appRecord->GetIsMasterProcess() && + IsSameAbilityType(appRecord, abilityInfo)) { + return true; + } + } + return false; +} + bool AppRunningManager::IsAppExist(uint32_t accessTokenId) { std::lock_guard guard(runningRecordMapMutex_); diff --git a/test/unittest/app_mgr_service_inner_eighth_test/BUILD.gn b/test/unittest/app_mgr_service_inner_eighth_test/BUILD.gn index 579917b5a5dde43b1a94e1ee0464ad78ed05c8d5..e4a0184befbdb101e8eeb626155aa7184797a93c 100644 --- a/test/unittest/app_mgr_service_inner_eighth_test/BUILD.gn +++ b/test/unittest/app_mgr_service_inner_eighth_test/BUILD.gn @@ -85,6 +85,7 @@ ohos_unittest("app_mgr_service_inner_eighth_test") { "mock/src/mock_permission_verification.cpp", "mock/src/mock_remote_client_manager.cpp", "mock/src/mock_user_record_manager.cpp", + "mock/src/mock_app_utils.cpp", ] configs = [] diff --git a/test/unittest/app_mgr_service_inner_eighth_test/app_mgr_service_inner_eighth_test.cpp b/test/unittest/app_mgr_service_inner_eighth_test/app_mgr_service_inner_eighth_test.cpp index a76ce33ce28c21a19d21319feeb2f433c99a6332..6e4823de345f707ca6d711abc162c2b2e49878d8 100644 --- a/test/unittest/app_mgr_service_inner_eighth_test/app_mgr_service_inner_eighth_test.cpp +++ b/test/unittest/app_mgr_service_inner_eighth_test/app_mgr_service_inner_eighth_test.cpp @@ -2682,5 +2682,146 @@ HWTEST_F(AppMgrServiceInnerEighthTest, SetPreloadDebugApp_0100, TestSize.Level1) EXPECT_EQ(debugApp, true); TAG_LOGI(AAFwkTag::TEST, "SetPreloadDebugApp_0100 end"); } + +/** + * @tc.name: PromoteCurrentToCandidateMasterProcess_0100 + * @tc.desc: test SetPreloadDebugApp + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerEighthTest, PromoteCurrentToCandidateMasterProcess_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "PromoteCurrentToCandidateMasterProcess_0100 start"); + auto appMgrServiceInner = std::make_shared(); + ASSERT_NE(appMgrServiceInner, nullptr); + AAFwk::MyStatus::GetInstance().isStartSpecifiedProcess_ = true; + std::shared_ptr info1 = nullptr; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = std::make_shared(info1, 0, ""); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetMasterProcess(true); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetTimeStamp(0); + int32_t res = appMgrServiceInner->PromoteCurrentToCandidateMasterProcess(true); + EXPECT_EQ(res, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "PromoteCurrentToCandidateMasterProcess_0100 end"); +} + +/** + * @tc.name: PromoteCurrentToCandidateMasterProcess_0200 + * @tc.desc: test SetPreloadDebugApp + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerEighthTest, PromoteCurrentToCandidateMasterProcess_0200, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "PromoteCurrentToCandidateMasterProcess_0200 start"); + auto appMgrServiceInner = std::make_shared(); + ASSERT_NE(appMgrServiceInner, nullptr); + AAFwk::MyStatus::GetInstance().isStartSpecifiedProcess_ = true; + std::shared_ptr info1 = nullptr; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = std::make_shared(info1, 0, ""); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetMasterProcess(false); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetTimeStamp(1); + int32_t res = appMgrServiceInner->PromoteCurrentToCandidateMasterProcess(true); + EXPECT_EQ(res, AAFwk::ERR_NOT_ISOLATION_PROCESS); + TAG_LOGI(AAFwkTag::TEST, "PromoteCurrentToCandidateMasterProcess_0200 end"); +} + +/** + * @tc.name: PromoteCurrentToCandidateMasterProcess_0300 + * @tc.desc: test SetPreloadDebugApp + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerEighthTest, PromoteCurrentToCandidateMasterProcess_0300, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "PromoteCurrentToCandidateMasterProcess_0300 start"); + auto appMgrServiceInner = std::make_shared(); + ASSERT_NE(appMgrServiceInner, nullptr); + AAFwk::MyStatus::GetInstance().isStartSpecifiedProcess_ = false; + std::shared_ptr info1 = nullptr; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = std::make_shared(info1, 0, ""); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetMasterProcess(false); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetTimeStamp(0); + int32_t res = appMgrServiceInner->PromoteCurrentToCandidateMasterProcess(true); + EXPECT_EQ(res, AAFwk::ERR_CAPABILITY_NOT_SUPPORT); + TAG_LOGI(AAFwkTag::TEST, "PromoteCurrentToCandidateMasterProcess_0300 end"); +} + +/** + * @tc.name: DemoteCurrentFromCandidateMasterProcess_0100 + * @tc.desc: test SetPreloadDebugApp + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerEighthTest, DemoteCurrentFromCandidateMasterProcess_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "DemoteCurrentFromCandidateMasterProcess_0100 start"); + auto appMgrServiceInner = std::make_shared(); + ASSERT_NE(appMgrServiceInner, nullptr); + AAFwk::MyStatus::GetInstance().isStartSpecifiedProcess_ = true; + std::shared_ptr info1 = nullptr; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = std::make_shared(info1, 0, ""); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetMasterProcess(false); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetTimeStamp(1); + int32_t res = appMgrServiceInner->DemoteCurrentFromCandidateMasterProcess(); + EXPECT_EQ(res, ERR_OK); + TAG_LOGI(AAFwkTag::TEST, "DemoteCurrentFromCandidateMasterProcess_0100 end"); +} + + +/** + * @tc.name: DemoteCurrentFromCandidateMasterProcess_0200 + * @tc.desc: test SetPreloadDebugApp + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerEighthTest, DemoteCurrentFromCandidateMasterProcess_0200, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "DemoteCurrentFromCandidateMasterProcess_0200 start"); + auto appMgrServiceInner = std::make_shared(); + ASSERT_NE(appMgrServiceInner, nullptr); + AAFwk::MyStatus::GetInstance().isStartSpecifiedProcess_ = true; + std::shared_ptr info1 = nullptr; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = std::make_shared(info1, 0, ""); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetMasterProcess(true); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetTimeStamp(0); + int32_t res = appMgrServiceInner->DemoteCurrentFromCandidateMasterProcess(); + EXPECT_EQ(res, AAFwk::ERR_ALREADY_MASTER_PROCESS); + TAG_LOGI(AAFwkTag::TEST, "DemoteCurrentFromCandidateMasterProcess_0200 end"); +} + +/** + * @tc.name: DemoteCurrentFromCandidateMasterProcess_0300 + * @tc.desc: test SetPreloadDebugApp + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerEighthTest, DemoteCurrentFromCandidateMasterProcess_0300, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "DemoteCurrentFromCandidateMasterProcess_0300 start"); + auto appMgrServiceInner = std::make_shared(); + ASSERT_NE(appMgrServiceInner, nullptr); + AAFwk::MyStatus::GetInstance().isStartSpecifiedProcess_ = false; + std::shared_ptr info1 = nullptr; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = std::make_shared(info1, 0, ""); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetMasterProcess(false); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetTimeStamp(0); + int32_t res = appMgrServiceInner->DemoteCurrentFromCandidateMasterProcess(); + EXPECT_EQ(res, AAFwk::ERR_CAPABILITY_NOT_SUPPORT); + TAG_LOGI(AAFwkTag::TEST, "DemoteCurrentFromCandidateMasterProcess_0300 end"); +} + +/** + * @tc.name: DemoteCurrentFromCandidateMasterProcess_0400 + * @tc.desc: test SetPreloadDebugApp + * @tc.type: FUNC + */ +HWTEST_F(AppMgrServiceInnerEighthTest, DemoteCurrentFromCandidateMasterProcess_0400, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "DemoteCurrentFromCandidateMasterProcess_0400 start"); + auto appMgrServiceInner = std::make_shared(); + ASSERT_NE(appMgrServiceInner, nullptr); + AAFwk::MyStatus::GetInstance().isStartSpecifiedProcess_ = true; + std::shared_ptr info1 = nullptr; + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_ = std::make_shared(info1, 0, ""); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetMasterProcess(false); + AAFwk::MyStatus::GetInstance().getAppRunningRecordByPid_->SetTimeStamp(0); + int32_t res = appMgrServiceInner->DemoteCurrentFromCandidateMasterProcess(); + EXPECT_EQ(res, AAFwk::ERR_NOT_CANDIDATE_MASTER_PROCESS); + TAG_LOGI(AAFwkTag::TEST, "DemoteCurrentFromCandidateMasterProcess_0400 end"); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/app_mgr_service_inner_eighth_test/mock/include/mock_my_status.h b/test/unittest/app_mgr_service_inner_eighth_test/mock/include/mock_my_status.h index 5436fc4dbf6298e3764c906f08f13655d3075f9b..f300dd1e78f1a7cdfcb6be28b8231a1de6b02008 100755 --- a/test/unittest/app_mgr_service_inner_eighth_test/mock/include/mock_my_status.h +++ b/test/unittest/app_mgr_service_inner_eighth_test/mock/include/mock_my_status.h @@ -137,6 +137,7 @@ public: ErrCode handleMemorySizeInSufficent_ = ERR_OK; ErrCode handleRequireBigMemoryOptimization_ = ERR_OK; ErrCode handleNoRequireBigMemoryOptimization_ = ERR_OK; + bool isStartSpecifiedProcess_ = true; private: MyStatus() = default; }; diff --git a/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_utils.cpp b/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03c93b31267533e632b8a43710d7811d20a7fbc9 --- /dev/null +++ b/test/unittest/app_mgr_service_inner_eighth_test/mock/src/mock_app_utils.cpp @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "app_utils.h" +#include "mock_my_status.h" + + +namespace OHOS { +namespace AAFwk { + +AppUtils::~AppUtils() {} + +AppUtils::AppUtils() {} + +AppUtils &AppUtils::GetInstance() +{ + static AppUtils utils; + return utils; +} + +bool AppUtils::IsLauncher(const std::string &bundleName) const +{ + return true; +} + +bool AppUtils::IsLauncherAbility(const std::string &abilityName) const +{ + return true; +} + +bool AppUtils::IsInheritWindowSplitScreenMode() +{ + return true; +} + +bool AppUtils::IsSupportAncoApp() +{ + return true; +} + +int32_t AppUtils::GetTimeoutUnitTimeRatio() +{ + return 0; +} + +bool AppUtils::IsSelectorDialogDefaultPossion() +{ + return true; +} + +bool AppUtils::IsStartSpecifiedProcess() +{ + return AAFwk::MyStatus::GetInstance().isStartSpecifiedProcess_; +} + +bool AppUtils::IsUseMultiRenderProcess() +{ + return true; +} + +bool AppUtils::IsLimitMaximumOfRenderProcess() +{ + return true; +} + +bool AppUtils::IsGrantPersistUriPermission() +{ + return true; +} + +bool AppUtils::IsStartOptionsWithAnimation() +{ + return true; +} + +bool AppUtils::IsMultiProcessModel() +{ + return true; +} + +bool AppUtils::IsStartOptionsWithProcessOptions() +{ + return true; +} + +bool AppUtils::EnableMoveUIAbilityToBackgroundApi() +{ + return true; +} + +bool AppUtils::IsLaunchEmbededUIAbility() +{ + return true; +} + +bool AppUtils::IsSupportNativeChildProcess() +{ + return true; +} + +bool AppUtils::IsAllowResidentInExtremeMemory(const std::string& bundleName, const std::string& abilityName) +{ + return true; +} + +bool AppUtils::IsBigMemoryUnrelatedKeepAliveProc(const std::string &bundleName) +{ + return true; +} + +bool AppUtils::IsRequireBigMemoryProcess(const std::string &bundleName) +{ + return true; +} + +void AppUtils::LoadProcessProhibitedFromRestarting() +{ +} + +void AppUtils::LoadRequireBigMemoryApp() +{ +} + +void AppUtils::LoadResidentProcessInExtremeMemory() +{ +} + +bool AppUtils::IsAllowNativeChildProcess(const std::string &appIdentifier) +{ + return true; +} + +void AppUtils::LoadAllowNativeChildProcessApps() +{ +} + +int32_t AppUtils::GetLimitMaximumExtensionsPerProc() +{ + return 0; +} + +int32_t AppUtils::GetLimitMaximumExtensionsPerDevice() +{ + return 0; +} + +std::string AppUtils::GetCacheExtensionTypeList() +{ + return ""; +} + +bool AppUtils::IsAllowStartAbilityWithoutCallerToken(const std::string& bundleName, const std::string& abilityName) +{ + return true; +} + +void AppUtils::LoadStartAbilityWithoutCallerToken() +{ +} + +std::string AppUtils::GetBrokerDelegateBundleName() +{ + return ""; +} + +int32_t AppUtils::GetCollaboratorBrokerUID() +{ + return 0; +} + +int32_t AppUtils::GetCollaboratorBrokerReserveUID() +{ + return 0; +} + +int32_t AppUtils::MaxChildProcess() +{ + return 0; +} + +bool AppUtils::IsSupportMultiInstance() +{ + return true; +} + +std::string AppUtils::GetMigrateClientBundleName() +{ + return ""; +} + +bool AppUtils::IsConnectSupportCrossUser() +{ + return true; +} + +bool AppUtils::IsPrepareTerminateEnabled() +{ + return true; +} + +bool AppUtils::IsSystemReasonMessage(const std::string &reasonMessage) +{ + return true; +} + +bool AppUtils::IsCacheAbilityEnabled() +{ + return true; +} + +void AppUtils::LoadCacheAbilityList() +{ +} + +bool AppUtils::IsCacheExtensionAbilityByList(const std::string& bundleName, const std::string& abilityName) +{ + return true; +} + +} // namespace AAFwk +} // namespace OHOS