From 4c78b2213f5063fdbb9b738920a198dd1425d33d Mon Sep 17 00:00:00 2001 From: donglin Date: Sat, 6 Jul 2024 08:25:06 +0000 Subject: [PATCH] add recordId verification Signed-off-by: donglin Change-Id: Ie08908e2782a4bfa0ed1f64dd71be13b4a50c15d --- .../napi_ability_common/js_napi_common.cpp | 6 +- .../ability_runtime/ability_context_impl.cpp | 9 ++ frameworks/native/ability/native/ability.cpp | 5 + .../native/ability/native/ability_context.cpp | 9 ++ .../ability_runtime/js_ability_context.cpp | 40 ++--- .../native/js_service_extension_context.cpp | 7 +- .../js_ui_extension_content_session.cpp | 8 +- .../js_ui_extension_context.cpp | 7 +- .../ui_extension_context.cpp | 9 ++ .../service_extension_context.cpp | 9 ++ .../include/ability_manager_client.h | 4 +- .../include/ability_manager_interface.h | 6 +- ...atomic_service_status_callback_interface.h | 8 - .../atomic_service_status_callback_proxy.h | 7 - .../ability/ability_runtime/ability_context.h | 3 + .../ability_runtime/ability_context_impl.h | 1 + .../kits/native/ability/native/ability.h | 2 + .../ability/native/fa_ability_context.h | 2 + .../ui_extension_context.h | 3 + .../service_extension_context.h | 3 + .../include/ability_manager_proxy.h | 3 +- .../include/ability_manager_service.h | 3 +- .../include/atomic_service_status_callback.h | 10 +- .../abilitymgr/include/free_install_manager.h | 31 ++-- .../include/free_install_observer_manager.h | 16 +- .../abilitymgr/src/ability_manager_client.cpp | 5 +- .../abilitymgr/src/ability_manager_proxy.cpp | 15 +- .../src/ability_manager_service.cpp | 5 +- .../abilitymgr/src/ability_manager_stub.cpp | 10 +- .../src/atomic_service_status_callback.cpp | 16 +- .../atomic_service_status_callback_proxy.cpp | 23 --- .../atomic_service_status_callback_stub.cpp | 15 -- .../abilitymgr/src/free_install_manager.cpp | 145 ++++++++---------- .../src/free_install_observer_manager.cpp | 90 ++++------- test/moduletest/ability_test/ability_test.cpp | 2 +- .../ability_manager_client_branch_test.cpp | 2 +- .../ability_manager_service_third_test.cpp | 4 +- .../free_install_manager_test.cpp | 12 +- 38 files changed, 271 insertions(+), 284 deletions(-) diff --git a/frameworks/js/napi/inner/napi_ability_common/js_napi_common.cpp b/frameworks/js/napi/inner/napi_ability_common/js_napi_common.cpp index 6fa4a8d0f0..aa88123527 100644 --- a/frameworks/js/napi/inner/napi_ability_common/js_napi_common.cpp +++ b/frameworks/js/napi/inner/napi_ability_common/js_napi_common.cpp @@ -1418,10 +1418,14 @@ void JsNapiCommon::AddFreeInstallObserver(napi_env env, const AAFwk::Want &want, { // adapter free install async return install and start result TAG_LOGD(AAFwkTag::JSNAPI, "AddFreeInstallObserver start."); + if (ability_ == nullptr) { + TAG_LOGE(AAFwkTag::JSNAPI, "the ability is nullptr"); + return; + } int ret = 0; if (freeInstallObserver_ == nullptr) { freeInstallObserver_ = new JsFreeInstallObserver(env); - ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(freeInstallObserver_); + ret = ability_->AddFreeInstallObserver(freeInstallObserver_); } if (ret != ERR_OK) { diff --git a/frameworks/native/ability/ability_runtime/ability_context_impl.cpp b/frameworks/native/ability/ability_runtime/ability_context_impl.cpp index f614012864..69663d3465 100644 --- a/frameworks/native/ability/ability_runtime/ability_context_impl.cpp +++ b/frameworks/native/ability/ability_runtime/ability_context_impl.cpp @@ -996,6 +996,15 @@ ErrCode AbilityContextImpl::ChangeAbilityVisibility(bool isShow) return err; } +ErrCode AbilityContextImpl::AddFreeInstallObserver(const sptr &observer) +{ + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(token_, observer); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::CONTEXT, "AddFreeInstallObserver error, ret: %{public}d", ret); + } + return ret; +} + ErrCode AbilityContextImpl::OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode, RuntimeTask &&task) { diff --git a/frameworks/native/ability/native/ability.cpp b/frameworks/native/ability/native/ability.cpp index 1c23b73fb5..26669aeb0a 100644 --- a/frameworks/native/ability/native/ability.cpp +++ b/frameworks/native/ability/native/ability.cpp @@ -448,6 +448,11 @@ ErrCode Ability::StartAbility(const Want &want, AbilityStartSetting abilityStart return err; } +ErrCode Ability::AddFreeInstallObserver(const sptr observer) +{ + return AbilityContext::AddFreeInstallObserver(observer); +} + std::string Ability::GetType(const Uri &uri) { return ""; diff --git a/frameworks/native/ability/native/ability_context.cpp b/frameworks/native/ability/native/ability_context.cpp index 5299d54027..12bc23b1af 100644 --- a/frameworks/native/ability/native/ability_context.cpp +++ b/frameworks/native/ability/native/ability_context.cpp @@ -329,5 +329,14 @@ sptr AbilityContext::GetSessionToken() std::lock_guard lock(sessionTokenMutex_); return sessionToken_; } + +int32_t AbilityContext::AddFreeInstallObserver(const sptr &observer) +{ + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(token_, observer); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::CONTEXT, "AddFreeInstallObserver error, ret: %{public}d", ret); + } + return ret; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/native/ability/native/ability_runtime/js_ability_context.cpp b/frameworks/native/ability/native/ability_runtime/js_ability_context.cpp index 4ca48a4e82..049ea87bc3 100644 --- a/frameworks/native/ability/native/ability_runtime/js_ability_context.cpp +++ b/frameworks/native/ability/native/ability_runtime/js_ability_context.cpp @@ -482,12 +482,10 @@ bool JsAbilityContext::CreateOpenLinkTask(const napi_env &env, const napi_value TAG_LOGW(AAFwkTag::CONTEXT, "wrap abilityResult error"); asyncTask->Reject(env, CreateJsError(env, AbilityErrorCode::ERROR_CODE_INNER)); return; + } else { + isInner ? asyncTask->Reject(env, CreateJsErrorByNativeErr(env, resultCode)) : + asyncTask->ResolveWithNoError(env, abilityResult); } - if (isInner) { - asyncTask->Reject(env, CreateJsErrorByNativeErr(env, resultCode)); - return; - } - asyncTask->ResolveWithNoError(env, abilityResult); }; curRequestCode_ = (curRequestCode_ == INT_MAX) ? 0 : (curRequestCode_ + 1); requestCode = curRequestCode_; @@ -1379,11 +1377,8 @@ napi_value JsAbilityContext::OnTerminateSelf(napi_env env, NapiCallbackInfo& inf } auto errcode = context->TerminateSelf(); - if (errcode == 0) { - task.Resolve(env, CreateJsUndefined(env)); - } else { + (errcode == 0) ? task.Resolve(env, CreateJsUndefined(env)) : task.Reject(env, CreateJsErrorByNativeErr(env, errcode)); - } }; napi_value lastParam = (info.argc > ARGC_ZERO) ? info.argv[INDEX_ZERO] : nullptr; @@ -1486,11 +1481,8 @@ napi_value JsAbilityContext::OnReportDrawnCompleted(napi_env env, NapiCallbackIn }; NapiAsyncTask::CompleteCallback complete = [innerErrorCode](napi_env env, NapiAsyncTask& task, int32_t status) { - if (*innerErrorCode == ERR_OK) { - task.Resolve(env, CreateJsUndefined(env)); - } else { + (*innerErrorCode == ERR_OK) ? task.Resolve(env, CreateJsUndefined(env)) : task.Reject(env, CreateJsErrorByNativeErr(env, *innerErrorCode)); - } }; napi_value lastParam = info.argv[INDEX_ZERO]; @@ -1569,7 +1561,12 @@ void JsAbilityContext::AddFreeInstallObserver(napi_env env, const AAFwk::Want &w int ret = 0; if (freeInstallObserver_ == nullptr) { freeInstallObserver_ = new JsFreeInstallObserver(env); - ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(freeInstallObserver_); + auto context = context_.lock(); + if (!context) { + TAG_LOGE(AAFwkTag::CONTEXT, "context is nullptr."); + return; + } + ret = context->AddFreeInstallObserver(freeInstallObserver_); } if (ret != ERR_OK) { @@ -2061,11 +2058,8 @@ napi_value JsAbilityContext::OnStartAbilityByType(napi_env env, NapiCallbackInfo } #ifdef SUPPORT_SCREEN auto errcode = context->StartAbilityByType(type, wantParam, callback); - if (errcode != 0) { - task.Reject(env, CreateJsErrorByNativeErr(env, errcode)); - } else { + (errcode != 0) ? task.Reject(env, CreateJsErrorByNativeErr(env, errcode)) : task.ResolveWithNoError(env, CreateJsUndefined(env)); - } #endif }; @@ -2136,11 +2130,8 @@ napi_value JsAbilityContext::ChangeAbilityVisibility(napi_env env, NapiCallbackI return; } auto errCode = context->ChangeAbilityVisibility(isShow); - if (errCode == 0) { - task.ResolveWithNoError(env, CreateJsUndefined(env)); - } else { + (errCode == 0) ? task.ResolveWithNoError(env, CreateJsUndefined(env)) : task.Reject(env, CreateJsErrorByNativeErr(env, errCode)); - } }; napi_value result = nullptr; @@ -2206,11 +2197,8 @@ napi_value JsAbilityContext::OpenAtomicServiceInner(napi_env env, NapiCallbackIn isInner = true; resultCode = ERR_INVALID_VALUE; } - if (isInner) { - observer->OnInstallFinished(bundleName, abilityName, startTime, resultCode); - } else { + isInner ? observer->OnInstallFinished(bundleName, abilityName, startTime, resultCode) : observer->OnInstallFinished(bundleName, abilityName, startTime, abilityResult); - } }; auto context = context_.lock(); if (context == nullptr) { diff --git a/frameworks/native/ability/native/js_service_extension_context.cpp b/frameworks/native/ability/native/js_service_extension_context.cpp index 045c034e5b..5586e4a0f7 100644 --- a/frameworks/native/ability/native/js_service_extension_context.cpp +++ b/frameworks/native/ability/native/js_service_extension_context.cpp @@ -199,7 +199,12 @@ private: int ret = 0; if (freeInstallObserver_ == nullptr) { freeInstallObserver_ = new JsFreeInstallObserver(env); - ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(freeInstallObserver_); + auto context = context_.lock(); + if (!context) { + TAG_LOGW(AAFwkTag::SERVICE_EXT, "context is released"); + return; + } + ret = context->AddFreeInstallObserver(freeInstallObserver_); } if (ret != ERR_OK) { diff --git a/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_content_session.cpp b/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_content_session.cpp index 0318240259..5d112e9eae 100644 --- a/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_content_session.cpp +++ b/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_content_session.cpp @@ -1055,7 +1055,13 @@ void JsUIExtensionContentSession::AddFreeInstallObserver(napi_env env, int ret = 0; if (freeInstallObserver_ == nullptr) { freeInstallObserver_ = new JsFreeInstallObserver(env); - ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(freeInstallObserver_); + auto context = context_.lock(); + if (!context) { + TAG_LOGE(AAFwkTag::CONTEXT, "context is nullptr."); + return; + } + ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(context->GetToken(), + freeInstallObserver_); } if (ret != ERR_OK) { diff --git a/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_context.cpp b/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_context.cpp index 2bd4d1f8d6..2c104f26a4 100755 --- a/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_context.cpp +++ b/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_context.cpp @@ -765,7 +765,12 @@ void JsUIExtensionContext::AddFreeInstallObserver(napi_env env, const AAFwk::Wan int ret = 0; if (freeInstallObserver_ == nullptr) { freeInstallObserver_ = new JsFreeInstallObserver(env); - ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(freeInstallObserver_); + auto context = context_.lock(); + if (!context) { + TAG_LOGE(AAFwkTag::CONTEXT, "context is nullptr."); + return; + } + ret = context->AddFreeInstallObserver(freeInstallObserver_); } if (ret != ERR_OK) { diff --git a/frameworks/native/ability/native/ui_extension_ability/ui_extension_context.cpp b/frameworks/native/ability/native/ui_extension_ability/ui_extension_context.cpp index d36ec87bfd..4c614cd8fa 100755 --- a/frameworks/native/ability/native/ui_extension_ability/ui_extension_context.cpp +++ b/frameworks/native/ability/native/ui_extension_ability/ui_extension_context.cpp @@ -271,5 +271,14 @@ ErrCode UIExtensionContext::OpenLink(const AAFwk::Want& want, int requestCode) TAG_LOGD(AAFwkTag::UI_EXT, "Called."); return AAFwk::AbilityManagerClient::GetInstance()->OpenLink(want, token_, -1, requestCode); } + +ErrCode UIExtensionContext::AddFreeInstallObserver(const sptr &observer) +{ + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(token_, observer); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::CONTEXT, "AddFreeInstallObserver error, ret: %{public}d", ret); + } + return ret; +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/native/appkit/ability_runtime/service_extension_context.cpp b/frameworks/native/appkit/ability_runtime/service_extension_context.cpp index d01838432c..b7bb18e449 100644 --- a/frameworks/native/appkit/ability_runtime/service_extension_context.cpp +++ b/frameworks/native/appkit/ability_runtime/service_extension_context.cpp @@ -248,5 +248,14 @@ ErrCode ServiceExtensionContext::PreStartMission(const std::string& bundleName, TAG_LOGI(AAFwkTag::APPKIT, "End."); return err; } + +ErrCode ServiceExtensionContext::AddFreeInstallObserver(const sptr &observer) +{ + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->AddFreeInstallObserver(token_, observer); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::APPKIT, "AddFreeInstallObserver error, ret: %{public}d", ret); + } + return ret; +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/interfaces/inner_api/ability_manager/include/ability_manager_client.h b/interfaces/inner_api/ability_manager/include/ability_manager_client.h index ef4ac22a9c..a8e19e2fb9 100644 --- a/interfaces/inner_api/ability_manager/include/ability_manager_client.h +++ b/interfaces/inner_api/ability_manager/include/ability_manager_client.h @@ -1181,10 +1181,12 @@ public: /** * @brief Add free install observer. * + * @param callerToken The caller ability token. * @param observer Free install observer. * @return Returns ERR_OK on success, others on failure. */ - ErrCode AddFreeInstallObserver(sptr observer); + ErrCode AddFreeInstallObserver(const sptr callToken, + const sptr observer); /** * Called to verify that the MissionId is valid. diff --git a/interfaces/inner_api/ability_manager/include/ability_manager_interface.h b/interfaces/inner_api/ability_manager/include/ability_manager_interface.h index 195438896f..f69c47c729 100644 --- a/interfaces/inner_api/ability_manager/include/ability_manager_interface.h +++ b/interfaces/inner_api/ability_manager/include/ability_manager_interface.h @@ -1150,10 +1150,12 @@ public: /** * Add free install observer. * - * @param observer, the observer of the ability to free install start. + * @param callerToken, The caller ability token. + * @param observer, The observer of the ability to free install start. * @return Returns ERR_OK on success, others on failure. */ - virtual int AddFreeInstallObserver(const sptr &observer) + virtual int AddFreeInstallObserver(const sptr &callerToken, + const sptr &observer) { return 0; } diff --git a/interfaces/inner_api/ability_manager/include/atomic_service_status_callback_interface.h b/interfaces/inner_api/ability_manager/include/atomic_service_status_callback_interface.h index f41a8b3248..2b44322052 100644 --- a/interfaces/inner_api/ability_manager/include/atomic_service_status_callback_interface.h +++ b/interfaces/inner_api/ability_manager/include/atomic_service_status_callback_interface.h @@ -49,19 +49,11 @@ public: * @param userId, user`s id. */ virtual void OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId) = 0; - - /** - * OnRemoveTimeoutTask, BMS has connected AG. - * - * @param want, the want of the ability to free install. - */ - virtual void OnRemoveTimeoutTask(const Want &want) = 0; protected: enum IAtomicServiceStatusCallbackCmd { ON_FREE_INSTALL_DONE = 0, ON_REMOTE_FREE_INSTALL_DONE, - ON_REMOVE_TIMEOUT_TASK, CMD_MAX, }; }; diff --git a/interfaces/inner_api/ability_manager/include/atomic_service_status_callback_proxy.h b/interfaces/inner_api/ability_manager/include/atomic_service_status_callback_proxy.h index 1694a06a75..cf30700726 100644 --- a/interfaces/inner_api/ability_manager/include/atomic_service_status_callback_proxy.h +++ b/interfaces/inner_api/ability_manager/include/atomic_service_status_callback_proxy.h @@ -52,13 +52,6 @@ public: */ void OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId) override; - /** - * OnRemoveTimeoutTask, BMS has connected AG. - * - * @param want, installed ability - */ - void OnRemoveTimeoutTask(const Want &want) override; - private: int32_t SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); static inline BrokerDelegator delegator_; diff --git a/interfaces/kits/native/ability/ability_runtime/ability_context.h b/interfaces/kits/native/ability/ability_runtime/ability_context.h index 1a109ed622..3264abcf20 100644 --- a/interfaces/kits/native/ability/ability_runtime/ability_context.h +++ b/interfaces/kits/native/ability/ability_runtime/ability_context.h @@ -22,6 +22,7 @@ #include "caller_callback.h" #include "configuration.h" #include "context.h" +#include "free_install_observer_interface.h" #include "iability_callback.h" #include "js_ui_extension_callback.h" #include "mission_info.h" @@ -173,6 +174,8 @@ public: virtual ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode, RuntimeTask &&task) = 0; + virtual ErrCode AddFreeInstallObserver(const sptr &observer) = 0; + virtual ErrCode ChangeAbilityVisibility(bool isShow) { return 0; } /** diff --git a/interfaces/kits/native/ability/ability_runtime/ability_context_impl.h b/interfaces/kits/native/ability/ability_runtime/ability_context_impl.h index c7ba7ea2a4..7fd01fbf22 100644 --- a/interfaces/kits/native/ability/ability_runtime/ability_context_impl.h +++ b/interfaces/kits/native/ability/ability_runtime/ability_context_impl.h @@ -222,6 +222,7 @@ public: ErrCode ChangeAbilityVisibility(bool isShow) override; ErrCode OpenLink(const AAFwk::Want& want, int requestCode) override; + ErrCode AddFreeInstallObserver(const sptr &observer) override; ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode, RuntimeTask &&task) override; diff --git a/interfaces/kits/native/ability/native/ability.h b/interfaces/kits/native/ability/native/ability.h index a8ef304d78..ffe7099905 100644 --- a/interfaces/kits/native/ability/native/ability.h +++ b/interfaces/kits/native/ability/native/ability.h @@ -32,6 +32,7 @@ #include "continuation_state.h" #include "dummy_notification_request.h" #include "fa_ability_context.h" +#include "free_install_observer_interface.h" #include "iability_callback.h" #include "want_agent.h" @@ -178,6 +179,7 @@ public: using AbilityContext::StartAbility; ErrCode StartAbility(const Want &want, AbilityStartSetting abilityStartSetting); + ErrCode AddFreeInstallObserver(const sptr observer); /** * @brief A Page or Service ability uses this method to start a specific ability. The system locates the target * ability from installed abilities based on the value of the want parameter and then starts it. You can specify diff --git a/interfaces/kits/native/ability/native/fa_ability_context.h b/interfaces/kits/native/ability/native/fa_ability_context.h index f7aa1a264a..2e122c22ba 100644 --- a/interfaces/kits/native/ability/native/fa_ability_context.h +++ b/interfaces/kits/native/ability/native/fa_ability_context.h @@ -61,6 +61,8 @@ public: */ ErrCode StartAbility(const Want &want, int requestCode, const AbilityStartSetting &abilityStartSetting) override; + ErrCode AddFreeInstallObserver(const sptr &observer); + /** * @brief Destroys the current ability. * diff --git a/interfaces/kits/native/ability/native/ui_extension_ability/ui_extension_context.h b/interfaces/kits/native/ability/native/ui_extension_ability/ui_extension_context.h index 57b91bde14..bc64e9104b 100755 --- a/interfaces/kits/native/ability/native/ui_extension_ability/ui_extension_context.h +++ b/interfaces/kits/native/ability/native/ui_extension_ability/ui_extension_context.h @@ -20,6 +20,7 @@ #include "ability_connect_callback.h" #include "extension_context.h" +#include "free_install_observer_interface.h" #include "start_options.h" #include "want.h" #ifdef SUPPORT_SCREEN @@ -149,6 +150,8 @@ public: ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode, RuntimeTask &&task); + ErrCode AddFreeInstallObserver(const sptr &observer); + void InsertResultCallbackTask(int requestCode, RuntimeTask&& task); void RemoveResultCallbackTask(int requestCode); diff --git a/interfaces/kits/native/appkit/ability_runtime/service_extension_context.h b/interfaces/kits/native/appkit/ability_runtime/service_extension_context.h index 8a2fc9cff6..63bde6d722 100644 --- a/interfaces/kits/native/appkit/ability_runtime/service_extension_context.h +++ b/interfaces/kits/native/appkit/ability_runtime/service_extension_context.h @@ -20,6 +20,7 @@ #include "ability_connect_callback.h" #include "connection_manager.h" +#include "free_install_observer_interface.h" #include "local_call_container.h" #include "start_options.h" #include "want.h" @@ -74,6 +75,8 @@ public: ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr &callback, int32_t accountId = DEFAULT_INVAL_VALUE); + ErrCode AddFreeInstallObserver(const sptr &observer); + /** * caller release by callback object * diff --git a/services/abilitymgr/include/ability_manager_proxy.h b/services/abilitymgr/include/ability_manager_proxy.h index b4925aeb37..9a41d5f147 100644 --- a/services/abilitymgr/include/ability_manager_proxy.h +++ b/services/abilitymgr/include/ability_manager_proxy.h @@ -886,7 +886,8 @@ public: * @param observer the observer of ability free install start. * @return Returns ERR_OK on success, others on failure. */ - virtual int AddFreeInstallObserver(const sptr &observer) override; + virtual int AddFreeInstallObserver(const sptr &callerToken, + const sptr &observer) override; /** * Called when client complete dump. diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index 1cb5bcb6ba..5b5738fc68 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -1300,7 +1300,8 @@ public: * @param observer the observer of ability free install start. * @return Returns ERR_OK on success, others on failure. */ - virtual int AddFreeInstallObserver(const sptr &observer) override; + virtual int AddFreeInstallObserver(const sptr &callerToken, + const sptr &observer) override; /** * Check the uid is background task uid. diff --git a/services/abilitymgr/include/atomic_service_status_callback.h b/services/abilitymgr/include/atomic_service_status_callback.h index 960f7b2aff..3480cb0d0b 100644 --- a/services/abilitymgr/include/atomic_service_status_callback.h +++ b/services/abilitymgr/include/atomic_service_status_callback.h @@ -31,7 +31,7 @@ class FreeInstallManager; */ class AtomicServiceStatusCallback : public AtomicServiceStatusCallbackStub { public: - AtomicServiceStatusCallback(const std::weak_ptr &server, bool isAsync); + AtomicServiceStatusCallback(const std::weak_ptr &server, bool isAsync, int32_t recordId); virtual ~AtomicServiceStatusCallback() = default; /** @@ -52,16 +52,10 @@ public: */ void OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId) override; - /** - * OnRemoveTimeoutTask, BMS has connected AG. - * - * @param want, installed ability - */ - void OnRemoveTimeoutTask(const Want &want) override; - private: std::weak_ptr server_; bool isAsync_; + int32_t recordId_ = -1; }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/include/free_install_manager.h b/services/abilitymgr/include/free_install_manager.h index df26dcd771..dd6148afe7 100644 --- a/services/abilitymgr/include/free_install_manager.h +++ b/services/abilitymgr/include/free_install_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -65,7 +65,7 @@ public: * @param want, installed ability. * @param userId, user`s id. */ - void OnInstallFinished(int resultCode, const Want &want, int32_t userId, bool isAsync = false); + void OnInstallFinished(int32_t recordId, int resultCode, const Want &want, int32_t userId, bool isAsync = false); /** * OnRemoteInstallFinished, DMS has finished. @@ -74,7 +74,7 @@ public: * @param want, installed ability. * @param userId, user`s id. */ - void OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId); + void OnRemoteInstallFinished(int32_t recordId, int resultCode, const Want &want, int32_t userId); /** * Start to free install. @@ -132,13 +132,8 @@ public: * @param observer, the observer of the ability to free install. * @return Returns ERR_OK on success, others on failure. */ - int AddFreeInstallObserver(const sptr &observer); - - /** - * Remove the timeout task when bms connect FA center. - * @param want, the want of the ability to free install. - */ - void OnRemoveTimeoutTask(const Want &want); + int AddFreeInstallObserver(const sptr &callerToken, + const sptr &observer); /** * Get free install task info. @@ -218,7 +213,7 @@ private: int NotifyDmsCallback(const Want &want, int resultCode); bool IsTopAbility(const sptr &callerToken); - void NotifyFreeInstallResult(const Want &want, int resultCode, bool isAsync = false); + void NotifyFreeInstallResult(int32_t recordId, const Want &want, int resultCode, bool isAsync = false); FreeInstallInfo BuildFreeInstallInfo(const Want &want, int32_t userId, int requestCode, const sptr &callerToken, bool isAsync, uint32_t specifyTokenId = 0, bool isOpenAtomicServiceShortUrl = false, std::shared_ptr originalWant = nullptr); @@ -229,21 +224,21 @@ private: void PostUpgradeAtomicServiceTask(int resultCode, const Want &want, int32_t userId); - void PostTimeoutTask(const Want &want); - void HandleTimeoutTask(const std::string &bundleName, const std::string &abilityName, const std::string &startTime); void RemoveTimeoutTask(const std::string &bundleName, const std::string &abilityName, const std::string &startTime); void StartAbilityByFreeInstall(FreeInstallInfo &info, std::string &bundleName, std::string &abilityName, std::string &startTime); - void StartAbilityByPreInstall(FreeInstallInfo &info, std::string &bundleName, std::string &abilityName, - std::string &startTime); + void StartAbilityByPreInstall(int32_t recordId, FreeInstallInfo &info, std::string &bundleName, + std::string &abilityName, std::string &startTime); int32_t UpdateElementName(Want &want, int32_t userId) const; - void HandleFreeInstallResult(FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync); - void HandleOnFreeInstallSuccess(FreeInstallInfo &freeInstallInfo, bool isAsync); - void HandleOnFreeInstallFail(FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync); + void HandleFreeInstallResult(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync); + void HandleOnFreeInstallSuccess(int32_t recordId, FreeInstallInfo &freeInstallInfo, bool isAsync); + void HandleOnFreeInstallFail(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync); void NotifySCBToHandleException(const FreeInstallInfo &info, int resultCode); void StartAbilityByConvertedWant(FreeInstallInfo &info, const std::string &startTime); void StartAbilityByOriginalWant(FreeInstallInfo &info, const std::string &startTime); + bool VerifyStartFreeInstallPermission(const sptr &callerToken); + int32_t GetRecordIdByToken(const sptr &callerToken); }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/include/free_install_observer_manager.h b/services/abilitymgr/include/free_install_observer_manager.h index 34a362c0b1..84116ea635 100644 --- a/services/abilitymgr/include/free_install_observer_manager.h +++ b/services/abilitymgr/include/free_install_observer_manager.h @@ -18,7 +18,7 @@ #include #include -#include +#include #include "cpp/mutex.h" #include "free_install_observer_interface.h" @@ -30,30 +30,28 @@ using namespace OHOS::AbilityRuntime; class FreeInstallObserverManager : public std::enable_shared_from_this { DECLARE_DELAYED_SINGLETON(FreeInstallObserverManager) public: - int32_t AddObserver(const sptr &observer); + int32_t AddObserver(int32_t recordId, const sptr &observer); int32_t RemoveObserver(const sptr &observer); - void OnInstallFinished(const std::string &bundleName, const std::string &abilityName, + void OnInstallFinished(int32_t recordId, const std::string &bundleName, const std::string &abilityName, const std::string &startTime, const int &resultCode); - void OnInstallFinishedByUrl(const std::string &startTime, const std::string &url, + void OnInstallFinishedByUrl(int32_t recordId, const std::string &startTime, const std::string &url, const int &resultCode); private: - bool ObserverExistLocked(const sptr &observer); - void OnObserverDied(const wptr &remote); - void HandleOnInstallFinished(const std::string &bundleName, const std::string &abilityName, + void HandleOnInstallFinished(int32_t recordId, const std::string &bundleName, const std::string &abilityName, const std::string &startTime, const int &resultCode); - void HandleOnInstallFinishedByUrl(const std::string &startTime, const std::string &url, + void HandleOnInstallFinishedByUrl(int32_t recordId, const std::string &startTime, const std::string &url, const int &resultCode); ffrt::mutex observerLock_; sptr deathRecipient_; - std::vector> observerList_; + std::unordered_map> observerMap_; }; class FreeInstallObserverRecipient : public IRemoteObject::DeathRecipient { diff --git a/services/abilitymgr/src/ability_manager_client.cpp b/services/abilitymgr/src/ability_manager_client.cpp index 800a510574..d3e9d90251 100644 --- a/services/abilitymgr/src/ability_manager_client.cpp +++ b/services/abilitymgr/src/ability_manager_client.cpp @@ -1513,12 +1513,13 @@ void AbilityManagerClient::HandleDlpApp(Want &want) #endif // WITH_DLP } -ErrCode AbilityManagerClient::AddFreeInstallObserver(sptr observer) +ErrCode AbilityManagerClient::AddFreeInstallObserver(const sptr callerToken, + const sptr observer) { TAG_LOGI(AAFwkTag::ABILITYMGR, "call"); auto abms = GetAbilityManager(); CHECK_POINTER_RETURN_NOT_CONNECTED(abms); - return abms->AddFreeInstallObserver(observer); + return abms->AddFreeInstallObserver(callerToken, observer); } int32_t AbilityManagerClient::IsValidMissionIds( diff --git a/services/abilitymgr/src/ability_manager_proxy.cpp b/services/abilitymgr/src/ability_manager_proxy.cpp index 1201f4cc51..f80faba115 100644 --- a/services/abilitymgr/src/ability_manager_proxy.cpp +++ b/services/abilitymgr/src/ability_manager_proxy.cpp @@ -3884,7 +3884,8 @@ int AbilityManagerProxy::FreeInstallAbilityFromRemote(const Want &want, const sp return reply.ReadInt32(); } -int AbilityManagerProxy::AddFreeInstallObserver(const sptr &observer) +int AbilityManagerProxy::AddFreeInstallObserver(const sptr &callerToken, + const sptr &observer) { MessageParcel data; MessageParcel reply; @@ -3899,6 +3900,18 @@ int AbilityManagerProxy::AddFreeInstallObserver(const sptrAsObject())) { TAG_LOGE(AAFwkTag::ABILITYMGR, "observer write failed."); return INNER_ERR; diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index fdd792135a..2c240468b4 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -9387,14 +9387,15 @@ int AbilityManagerService::CheckUIExtensionIsFocused(uint32_t uiExtensionTokenId return ERR_OK; } -int AbilityManagerService::AddFreeInstallObserver(const sptr &observer) +int AbilityManagerService::AddFreeInstallObserver(const sptr &callerToken, + const sptr &observer) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); if (freeInstallManager_ == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "freeInstallManager_ is nullptr."); return ERR_INVALID_VALUE; } - return freeInstallManager_->AddFreeInstallObserver(observer); + return freeInstallManager_->AddFreeInstallObserver(callerToken, observer); } int32_t AbilityManagerService::IsValidMissionIds( diff --git a/services/abilitymgr/src/ability_manager_stub.cpp b/services/abilitymgr/src/ability_manager_stub.cpp index b9ebdf58e4..7f0fec76aa 100644 --- a/services/abilitymgr/src/ability_manager_stub.cpp +++ b/services/abilitymgr/src/ability_manager_stub.cpp @@ -2704,13 +2704,21 @@ int AbilityManagerStub::FreeInstallAbilityFromRemoteInner(MessageParcel &data, M int AbilityManagerStub::AddFreeInstallObserverInner(MessageParcel &data, MessageParcel &reply) { + sptr callerToken = nullptr; + if (data.ReadBool()) { + callerToken = data.ReadRemoteObject(); + if (callerToken == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "caller token is nullptr."); + return ERR_INVALID_VALUE; + } + } sptr observer = iface_cast(data.ReadRemoteObject()); if (observer == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "observer is nullptr"); return ERR_INVALID_VALUE; } - int32_t result = AddFreeInstallObserver(observer); + int32_t result = AddFreeInstallObserver(callerToken, observer); if (!reply.WriteInt32(result)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "reply write failed."); return ERR_INVALID_VALUE; diff --git a/services/abilitymgr/src/atomic_service_status_callback.cpp b/services/abilitymgr/src/atomic_service_status_callback.cpp index 346dec3741..2d9d7016ca 100644 --- a/services/abilitymgr/src/atomic_service_status_callback.cpp +++ b/services/abilitymgr/src/atomic_service_status_callback.cpp @@ -21,8 +21,9 @@ namespace OHOS { namespace AAFwk { -AtomicServiceStatusCallback::AtomicServiceStatusCallback(const std::weak_ptr &server, bool isAsync) - : server_(server), isAsync_(isAsync) +AtomicServiceStatusCallback::AtomicServiceStatusCallback( + const std::weak_ptr &server, bool isAsync, int32_t recordId) + : server_(server), isAsync_(isAsync), recordId_(recordId) { } @@ -30,21 +31,14 @@ void AtomicServiceStatusCallback::OnInstallFinished(int resultCode, const Want & { auto server = server_.lock(); CHECK_POINTER(server); - server->OnInstallFinished(resultCode, want, userId, isAsync_); + server->OnInstallFinished(recordId_, resultCode, want, userId, isAsync_); } void AtomicServiceStatusCallback::OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId) { auto server = server_.lock(); CHECK_POINTER(server); - server->OnRemoteInstallFinished(resultCode, want, userId); -} - -void AtomicServiceStatusCallback::OnRemoveTimeoutTask(const Want &want) -{ - auto server = server_.lock(); - CHECK_POINTER(server); - server->OnRemoveTimeoutTask(want); + server->OnRemoteInstallFinished(recordId_, resultCode, want, userId); } } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/atomic_service_status_callback_proxy.cpp b/services/abilitymgr/src/atomic_service_status_callback_proxy.cpp index 67c076a4df..710b61b5d2 100644 --- a/services/abilitymgr/src/atomic_service_status_callback_proxy.cpp +++ b/services/abilitymgr/src/atomic_service_status_callback_proxy.cpp @@ -95,29 +95,6 @@ void AtomicServiceStatusCallbackProxy::OnRemoteInstallFinished(int resultCode, c } } -void AtomicServiceStatusCallbackProxy::OnRemoveTimeoutTask(const Want &want) -{ - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(IAtomicServiceStatusCallback::GetDescriptor())) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "Write interface token failed."); - return; - } - - if (!data.WriteParcelable(&want)) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "Write want error."); - return; - } - - int32_t error = SendTransactCmd(ON_REMOVE_TIMEOUT_TASK, data, reply, option); - if (error != NO_ERROR) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "OnFinished fail, error: %{public}d", error); - return; - } -} - int32_t AtomicServiceStatusCallbackProxy::SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { diff --git a/services/abilitymgr/src/atomic_service_status_callback_stub.cpp b/services/abilitymgr/src/atomic_service_status_callback_stub.cpp index 20c949ef46..4a364aad3e 100644 --- a/services/abilitymgr/src/atomic_service_status_callback_stub.cpp +++ b/services/abilitymgr/src/atomic_service_status_callback_stub.cpp @@ -54,18 +54,6 @@ int AtomicServiceStatusCallbackStub::OnRemoteInstallFinishedInner(MessageParcel return NO_ERROR; } -int AtomicServiceStatusCallbackStub::OnRemoveTimeoutTaskInner(MessageParcel &data, MessageParcel &reply) -{ - std::unique_ptr want(data.ReadParcelable()); - if (want == nullptr) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "AtomicServiceStatusCallbackStub want is nullptr."); - return ERR_INVALID_VALUE; - } - - OnRemoveTimeoutTask(*want); - return NO_ERROR; -} - int AtomicServiceStatusCallbackStub::OnRemoteRequest( uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { @@ -84,9 +72,6 @@ int AtomicServiceStatusCallbackStub::OnRemoteRequest( case IAtomicServiceStatusCallbackCmd::ON_REMOTE_FREE_INSTALL_DONE: return OnRemoteInstallFinishedInner(data, reply); break; - case IAtomicServiceStatusCallbackCmd::ON_REMOVE_TIMEOUT_TASK: - return OnRemoveTimeoutTaskInner(data, reply); - break; } } diff --git a/services/abilitymgr/src/free_install_manager.cpp b/services/abilitymgr/src/free_install_manager.cpp index b4e6abad92..1b1b2a731b 100644 --- a/services/abilitymgr/src/free_install_manager.cpp +++ b/services/abilitymgr/src/free_install_manager.cpp @@ -28,6 +28,8 @@ #include "hilog_wrapper.h" #include "hitrace_meter.h" #include "in_process_call_wrapper.h" +#include "permission_constants.h" +#include "start_ability_utils.h" #include "utils/app_mgr_util.h" #include "uri_utils.h" @@ -90,8 +92,7 @@ int FreeInstallManager::StartFreeInstall(const Want &want, int32_t userId, int r { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); TAG_LOGI(AAFwkTag::FREE_INSTALL, "StartFreeInstall called"); - auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); - if (!isSaCall && !IsTopAbility(callerToken)) { + if (!VerifyStartFreeInstallPermission(callerToken)) { return NOT_TOP_ABILITY; } FreeInstallInfo info = BuildFreeInstallInfo(want, userId, requestCode, callerToken, @@ -100,7 +101,8 @@ int FreeInstallManager::StartFreeInstall(const Want &want, int32_t userId, int r std::lock_guard lock(freeInstallListLock_); freeInstallList_.push_back(info); } - sptr callback = new AtomicServiceStatusCallback(weak_from_this(), isAsync); + int32_t recordId = GetRecordIdByToken(callerToken); + sptr callback = new AtomicServiceStatusCallback(weak_from_this(), isAsync, recordId); auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); CHECK_POINTER_AND_RETURN(bundleMgrHelper, GET_ABILITY_SERVICE_FAILED); AppExecFwk::AbilityInfo abilityInfo = {}; @@ -153,7 +155,8 @@ int FreeInstallManager::RemoteFreeInstall(const Want &want, int32_t userId, int std::lock_guard lock(freeInstallListLock_); freeInstallList_.push_back(info); } - sptr callback = new AtomicServiceStatusCallback(weak_from_this(), false); + int32_t recordId = GetRecordIdByToken(callerToken); + sptr callback = new AtomicServiceStatusCallback(weak_from_this(), false, recordId); int32_t callerUid = IPCSkeleton::GetCallingUid(); uint32_t accessToken = IPCSkeleton::GetCallingTokenID(); UriUtils::GetInstance().FilterUriWithPermissionDms(info.want, accessToken); @@ -261,7 +264,7 @@ int FreeInstallManager::NotifyDmsCallback(const Want &want, int resultCode) return reply.ReadInt32(); } -void FreeInstallManager::NotifyFreeInstallResult(const Want &want, int resultCode, bool isAsync) +void FreeInstallManager::NotifyFreeInstallResult(int32_t recordId, const Want &want, int resultCode, bool isAsync) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); std::lock_guard lock(freeInstallListLock_); @@ -292,12 +295,12 @@ void FreeInstallManager::NotifyFreeInstallResult(const Want &want, int resultCod } freeInstallInfo.isFreeInstallFinished = true; freeInstallInfo.resultCode = resultCode; - HandleFreeInstallResult(freeInstallInfo, resultCode, isAsync); + HandleFreeInstallResult(recordId, freeInstallInfo, resultCode, isAsync); it = freeInstallList_.erase(it); } } -void FreeInstallManager::HandleOnFreeInstallSuccess(FreeInstallInfo &freeInstallInfo, bool isAsync) +void FreeInstallManager::HandleOnFreeInstallSuccess(int32_t recordId, FreeInstallInfo &freeInstallInfo, bool isAsync) { TAG_LOGI(AAFwkTag::FREE_INSTALL, "FreeInstall success."); freeInstallInfo.isInstalled = true; @@ -307,7 +310,7 @@ void FreeInstallManager::HandleOnFreeInstallSuccess(FreeInstallInfo &freeInstall std::string bundleName = freeInstallInfo.want.GetElement().GetBundleName(); std::string abilityName = freeInstallInfo.want.GetElement().GetAbilityName(); if (freeInstallInfo.isPreStartMissionCalled) { - StartAbilityByPreInstall(freeInstallInfo, bundleName, abilityName, startTime); + StartAbilityByPreInstall(recordId, freeInstallInfo, bundleName, abilityName, startTime); return; } if (freeInstallInfo.isOpenAtomicServiceShortUrl) { @@ -320,7 +323,8 @@ void FreeInstallManager::HandleOnFreeInstallSuccess(FreeInstallInfo &freeInstall freeInstallInfo.promise->set_value(ERR_OK); } -void FreeInstallManager::HandleOnFreeInstallFail(FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync) +void FreeInstallManager::HandleOnFreeInstallFail(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, + bool isAsync) { TAG_LOGI(AAFwkTag::FREE_INSTALL, "FreeInstall failed."); freeInstallInfo.isInstalled = false; @@ -344,19 +348,20 @@ void FreeInstallManager::HandleOnFreeInstallFail(FreeInstallInfo &freeInstallInf std::string bundleName = freeInstallInfo.want.GetElement().GetBundleName(); std::string abilityName = freeInstallInfo.want.GetElement().GetAbilityName(); DelayedSingleton::GetInstance()->OnInstallFinished( - bundleName, abilityName, startTime, resultCode); + recordId, bundleName, abilityName, startTime, resultCode); return; } freeInstallInfo.promise->set_value(resultCode); } -void FreeInstallManager::HandleFreeInstallResult(FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync) +void FreeInstallManager::HandleFreeInstallResult(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, + bool isAsync) { if (resultCode == ERR_OK) { - HandleOnFreeInstallSuccess(freeInstallInfo, isAsync); + HandleOnFreeInstallSuccess(recordId, freeInstallInfo, isAsync); return; } - HandleOnFreeInstallFail(freeInstallInfo, resultCode, isAsync); + HandleOnFreeInstallFail(recordId, freeInstallInfo, resultCode, isAsync); } void FreeInstallManager::StartAbilityByFreeInstall(FreeInstallInfo &info, std::string &bundleName, @@ -374,12 +379,13 @@ void FreeInstallManager::StartAbilityByFreeInstall(FreeInstallInfo &info, std::s info.callerToken, info.userId, info.requestCode); } IPCSkeleton::SetCallingIdentity(identity); + int32_t recordId = GetRecordIdByToken(info.callerToken); TAG_LOGI(AAFwkTag::FREE_INSTALL, "The result of StartAbility is %{public}d.", result); DelayedSingleton::GetInstance()->OnInstallFinished( - bundleName, abilityName, startTime, result); + recordId, bundleName, abilityName, startTime, result); } -void FreeInstallManager::StartAbilityByPreInstall(FreeInstallInfo &info, std::string &bundleName, +void FreeInstallManager::StartAbilityByPreInstall(int32_t recordId, FreeInstallInfo &info, std::string &bundleName, std::string &abilityName, std::string &startTime) { info.want.SetFlags(info.want.GetFlags() ^ Want::FLAG_INSTALL_ON_DEMAND); @@ -395,7 +401,7 @@ void FreeInstallManager::StartAbilityByPreInstall(FreeInstallInfo &info, std::st IPCSkeleton::SetCallingIdentity(identity); TAG_LOGI(AAFwkTag::FREE_INSTALL, "The result of StartAbility is %{public}d.", result); DelayedSingleton::GetInstance()->OnInstallFinished( - bundleName, abilityName, startTime, result); + recordId, bundleName, abilityName, startTime, result); } void FreeInstallManager::StartAbilityByConvertedWant(FreeInstallInfo &info, const std::string &startTime) @@ -414,7 +420,9 @@ void FreeInstallManager::StartAbilityByConvertedWant(FreeInstallInfo &info, cons IPCSkeleton::SetCallingIdentity(identity); TAG_LOGI(AAFwkTag::FREE_INSTALL, "The result of StartAbility is %{public}d.", result); auto url = info.want.GetUriString(); - DelayedSingleton::GetInstance()->OnInstallFinishedByUrl(startTime, url, result); + int32_t recordId = GetRecordIdByToken(info.callerToken); + DelayedSingleton::GetInstance()->OnInstallFinishedByUrl(recordId, startTime, + url, result); } void FreeInstallManager::StartAbilityByOriginalWant(FreeInstallInfo &info, const std::string &startTime) @@ -426,7 +434,9 @@ void FreeInstallManager::StartAbilityByOriginalWant(FreeInstallInfo &info, const IPCSkeleton::SetCallingIdentity(identity); TAG_LOGI(AAFwkTag::FREE_INSTALL, "The result of StartAbility is %{public}d.", result); auto url = info.want.GetUriString(); - DelayedSingleton::GetInstance()->OnInstallFinishedByUrl(startTime, url, result); + int32_t recordId = GetRecordIdByToken(info.callerToken); + DelayedSingleton::GetInstance()->OnInstallFinishedByUrl(recordId, startTime, + url, result); } int32_t FreeInstallManager::UpdateElementName(Want &want, int32_t userId) const @@ -525,13 +535,14 @@ std::time_t FreeInstallManager::GetTimeStamp() return timestamp; } -void FreeInstallManager::OnInstallFinished(int resultCode, const Want &want, int32_t userId, bool isAsync) +void FreeInstallManager::OnInstallFinished(int32_t recordId, int resultCode, const Want &want, + int32_t userId, bool isAsync) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); TAG_LOGI(AAFwkTag::FREE_INSTALL, "%{public}s resultCode = %{public}d", __func__, resultCode); NotifyDmsCallback(want, resultCode); - NotifyFreeInstallResult(want, resultCode, isAsync); + NotifyFreeInstallResult(recordId, want, resultCode, isAsync); PostUpgradeAtomicServiceTask(resultCode, want, userId); } @@ -562,70 +573,24 @@ void FreeInstallManager::PostUpgradeAtomicServiceTask(int resultCode, const Want } } -void FreeInstallManager::OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId) +void FreeInstallManager::OnRemoteInstallFinished(int32_t recordId, int resultCode, const Want &want, int32_t userId) { TAG_LOGI(AAFwkTag::FREE_INSTALL, "%{public}s resultCode = %{public}d", __func__, resultCode); - NotifyFreeInstallResult(want, resultCode); + NotifyFreeInstallResult(recordId, want, resultCode); } -int FreeInstallManager::AddFreeInstallObserver(const sptr &observer) +int FreeInstallManager::AddFreeInstallObserver(const sptr &callerToken, + const sptr &observer) { TAG_LOGI(AAFwkTag::FREE_INSTALL, "Add FreeInstallObserver"); - return DelayedSingleton::GetInstance()->AddObserver(observer); -} - -void FreeInstallManager::PostTimeoutTask(const Want &want) -{ - TAG_LOGI(AAFwkTag::FREE_INSTALL, "PostTimeoutTask begin."); - std::string bundleName = want.GetElement().GetBundleName(); - std::string abilityName = want.GetElement().GetAbilityName(); - std::string startTime = want.GetStringParam(Want::PARAM_RESV_START_TIME); - auto task = [weak = weak_from_this(), bundleName, abilityName, startTime]() { - auto self = weak.lock(); - if (!self) { - TAG_LOGE(AAFwkTag::FREE_INSTALL, "this is nullptr"); - return; - } - DelayedSingleton::GetInstance()->OnInstallFinished(bundleName, abilityName, - startTime, FREE_INSTALL_TIMEOUT); - self->RemoveFreeInstallInfo(bundleName, abilityName, startTime); - }; - std::string taskName = std::string("FreeInstallTimeout_") + bundleName + std::string("_") + - abilityName + std::string("_") + startTime; - auto handler = DelayedSingleton::GetInstance()->GetTaskHandler(); - CHECK_POINTER_LOG(handler, "Fail to get AbilityTaskHandler."); - handler->SubmitTask(task, taskName, DELAY_LOCAL_FREE_INSTALL_TIMEOUT); -} - -void FreeInstallManager::RemoveTimeoutTask(const std::string &bundleName, const std::string &abilityName, - const std::string &startTime) -{ - // remove timeout task - std::string taskName = std::string("FreeInstallTimeout_") + bundleName + std::string("_") + - abilityName + std::string("_") + startTime; - TAG_LOGI(AAFwkTag::FREE_INSTALL, "RemoveTimeoutTask task name:%{public}s", taskName.c_str()); - auto handler = DelayedSingleton::GetInstance()->GetTaskHandler(); - CHECK_POINTER_LOG(handler, "Fail to get AbilityTaskHandler."); - handler->CancelTask(taskName); -} - -void FreeInstallManager::OnRemoveTimeoutTask(const Want &want) -{ - // only SA can call this interface - TAG_LOGI(AAFwkTag::FREE_INSTALL, "OnRemoveTimeoutTask begin."); - auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); - if (!isSaCall) { - TAG_LOGE(AAFwkTag::FREE_INSTALL, "Permission verification failed."); - return; + auto abilityRecord = Token::GetAbilityRecordByToken(callerToken); + if (abilityRecord != nullptr) { + return DelayedSingleton::GetInstance()->AddObserver(abilityRecord->GetRecordId(), + observer); + } else if (AAFwk::PermissionVerification::GetInstance()->IsSACall()) { + return DelayedSingleton::GetInstance()->AddObserver(-1, observer); } - std::string bundleName = want.GetElement().GetBundleName(); - std::string abilityName = want.GetElement().GetAbilityName(); - std::string startTime = want.GetStringParam(Want::PARAM_RESV_START_TIME); - if (bundleName.empty() || abilityName.empty()) { - TAG_LOGE(AAFwkTag::FREE_INSTALL, "wantBundleName or wantAbilityName is empty"); - return; - } - RemoveTimeoutTask(bundleName, abilityName, startTime); + return CHECK_PERMISSION_FAILED; } void FreeInstallManager::RemoveFreeInstallInfo(const std::string &bundleName, const std::string &abilityName, @@ -730,5 +695,31 @@ int FreeInstallManager::SetAppRunningState(Want &want) want.SetParam(KEY_IS_APP_RUNNING, isAppRunning); return ERR_OK; } + +bool FreeInstallManager::VerifyStartFreeInstallPermission(const sptr &callerToken) +{ + auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); + if (isSaCall || IsTopAbility(callerToken)) { + return true; + } + AppExecFwk::AbilityInfo callerInfo; + if (StartAbilityUtils::GetCallerAbilityInfo(callerToken, callerInfo)) { + if (callerInfo.applicationInfo.isSystemApp && AAFwk::PermissionVerification::GetInstance()-> + VerifyCallingPermission(PermissionConstants::PERMISSION_START_ABILITIES_FROM_BACKGROUND)) { + return true; + } + } + return false; +} + +int32_t FreeInstallManager::GetRecordIdByToken(const sptr &callerToken) +{ + auto abilityRecord = Token::GetAbilityRecordByToken(callerToken); + int recordId = -1; + if (abilityRecord != nullptr) { + recordId = abilityRecord->GetRecordId(); + } + return recordId; +} } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/free_install_observer_manager.cpp b/services/abilitymgr/src/free_install_observer_manager.cpp index e2838250ff..ca1410ee58 100644 --- a/services/abilitymgr/src/free_install_observer_manager.cpp +++ b/services/abilitymgr/src/free_install_observer_manager.cpp @@ -31,7 +31,7 @@ FreeInstallObserverManager::FreeInstallObserverManager() FreeInstallObserverManager::~FreeInstallObserverManager() {} -int32_t FreeInstallObserverManager::AddObserver(const sptr &observer) +int32_t FreeInstallObserverManager::AddObserver(int32_t recordId, const sptr &observer) { TAG_LOGD(AAFwkTag::FREE_INSTALL, "AddObserver begin."); if (observer == nullptr) { @@ -39,12 +39,8 @@ int32_t FreeInstallObserverManager::AddObserver(const sptr return ERR_INVALID_VALUE; } std::lock_guard lock(observerLock_); - if (ObserverExistLocked(observer)) { - TAG_LOGE(AAFwkTag::FREE_INSTALL, "Observer exist."); - return ERR_INVALID_VALUE; - } - observerList_.emplace_back(observer); - TAG_LOGD(AAFwkTag::FREE_INSTALL, "observerList_ size:%{public}zu", observerList_.size()); + observerMap_[recordId] = observer; + TAG_LOGD(AAFwkTag::FREE_INSTALL, "observerMap_ size:%{public}zu", observerMap_.size()); if (!deathRecipient_) { std::weak_ptr thisWeakPtr(shared_from_this()); @@ -74,30 +70,28 @@ int32_t FreeInstallObserverManager::RemoveObserver(const sptr lock(observerLock_); - auto it = std::find_if(observerList_.begin(), observerList_.end(), - [&observer](const sptr &item) { - return (item && item->AsObject() == observer->AsObject()); - }); - if (it != observerList_.end()) { - observerList_.erase(it); - TAG_LOGI(AAFwkTag::FREE_INSTALL, "observerList_ size:%{public}zu", observerList_.size()); - return ERR_OK; + for (auto &item : observerMap_) { + if (item.second && item.second->AsObject() == observer->AsObject()) { + observerMap_.erase(item.first); + TAG_LOGI(AAFwkTag::FREE_INSTALL, "observerMap_ size:%{public}zu", observerMap_.size()); + return ERR_OK; + } } TAG_LOGE(AAFwkTag::FREE_INSTALL, "Observer not exist or has been removed."); return ERR_INVALID_VALUE; } -void FreeInstallObserverManager::OnInstallFinished(const std::string &bundleName, const std::string &abilityName, - const std::string &startTime, const int &resultCode) +void FreeInstallObserverManager::OnInstallFinished(int32_t recordId, const std::string &bundleName, + const std::string &abilityName, const std::string &startTime, const int &resultCode) { - auto task = [weak = weak_from_this(), bundleName, abilityName, startTime, resultCode]() { + auto task = [weak = weak_from_this(), recordId, bundleName, abilityName, startTime, resultCode]() { auto self = weak.lock(); if (self == nullptr) { TAG_LOGE(AAFwkTag::FREE_INSTALL, "self is nullptr, OnInstallFinished failed."); return; } TAG_LOGI(AAFwkTag::FREE_INSTALL, "OnInstallFinished come."); - self->HandleOnInstallFinished(bundleName, abilityName, startTime, resultCode); + self->HandleOnInstallFinished(recordId, bundleName, abilityName, startTime, resultCode); }; auto handler = DelayedSingleton::GetInstance()->GetTaskHandler(); @@ -105,17 +99,17 @@ void FreeInstallObserverManager::OnInstallFinished(const std::string &bundleName handler->SubmitTask(task); } -void FreeInstallObserverManager::OnInstallFinishedByUrl(const std::string &startTime, const std::string &url, - const int &resultCode) +void FreeInstallObserverManager::OnInstallFinishedByUrl(int32_t recordId, const std::string &startTime, + const std::string &url, const int &resultCode) { - auto task = [weak = weak_from_this(), startTime, url, resultCode]() { + auto task = [weak = weak_from_this(), recordId, startTime, url, resultCode]() { auto self = weak.lock(); if (self == nullptr) { TAG_LOGE(AAFwkTag::FREE_INSTALL, "self is nullptr, OnInstallFinished failed."); return; } TAG_LOGI(AAFwkTag::FREE_INSTALL, "OnInstallFinishedByUrl come."); - self->HandleOnInstallFinishedByUrl(startTime, url, resultCode); + self->HandleOnInstallFinishedByUrl(recordId, startTime, url, resultCode); }; auto handler = DelayedSingleton::GetInstance()->GetTaskHandler(); @@ -123,44 +117,26 @@ void FreeInstallObserverManager::OnInstallFinishedByUrl(const std::string &start handler->SubmitTask(task); } -void FreeInstallObserverManager::HandleOnInstallFinished(const std::string &bundleName, const std::string &abilityName, - const std::string &startTime, const int &resultCode) +void FreeInstallObserverManager::HandleOnInstallFinished(int32_t recordId, const std::string &bundleName, + const std::string &abilityName, const std::string &startTime, const int &resultCode) { TAG_LOGD(AAFwkTag::FREE_INSTALL, "HandleOnInstallFinished begin."); - for (auto it = observerList_.begin(); it != observerList_.end(); ++it) { - if ((*it) == nullptr) { - continue; - } - (*it)->OnInstallFinished(bundleName, abilityName, startTime, resultCode); + auto iter = observerMap_.find(recordId); + if (iter != observerMap_.end() && iter->second != nullptr) { + (iter->second)->OnInstallFinished(bundleName, abilityName, startTime, resultCode); } } -void FreeInstallObserverManager::HandleOnInstallFinishedByUrl(const std::string &startTime, const std::string &url, - const int &resultCode) +void FreeInstallObserverManager::HandleOnInstallFinishedByUrl(int32_t recordId, const std::string &startTime, + const std::string &url, const int &resultCode) { TAG_LOGD(AAFwkTag::FREE_INSTALL, "HandleOnInstallFinishedByUrl begin."); - for (auto it = observerList_.begin(); it != observerList_.end(); ++it) { - if ((*it) == nullptr) { - continue; - } - (*it)->OnInstallFinishedByUrl(startTime, url, resultCode); + auto iter = observerMap_.find(recordId); + if (iter != observerMap_.end() && iter->second != nullptr) { + (iter->second)->OnInstallFinishedByUrl(startTime, url, resultCode); } } -bool FreeInstallObserverManager::ObserverExistLocked(const sptr &observer) -{ - TAG_LOGD(AAFwkTag::FREE_INSTALL, "ObserExist begin."); - if (observer == nullptr) { - TAG_LOGE(AAFwkTag::FREE_INSTALL, "The param observer is nullptr."); - return false; - } - auto it = std::find_if(observerList_.begin(), observerList_.end(), - [&observer](const sptr &item) { - return (item && item->AsObject() == observer->AsObject()); - }); - return it != observerList_.end(); -} - void FreeInstallObserverManager::OnObserverDied(const wptr &remote) { TAG_LOGI(AAFwkTag::FREE_INSTALL, "OnObserverDied begin."); @@ -172,12 +148,12 @@ void FreeInstallObserverManager::OnObserverDied(const wptr &remot remoteObj->RemoveDeathRecipient(deathRecipient_); std::lock_guard lock(observerLock_); - auto it = std::find_if(observerList_.begin(), observerList_.end(), [&remoteObj] - (const sptr item) { - return (item && item->AsObject() == remoteObj); - }); - if (it != observerList_.end()) { - observerList_.erase(it); + for (auto &item : observerMap_) { + if (item.second && item.second->AsObject() == remoteObj) { + observerMap_.erase(item.first); + TAG_LOGI(AAFwkTag::FREE_INSTALL, "observerMap_ size:%{public}zu", observerMap_.size()); + return; + } } } diff --git a/test/moduletest/ability_test/ability_test.cpp b/test/moduletest/ability_test/ability_test.cpp index cdd8ebd46e..5ff98d231e 100644 --- a/test/moduletest/ability_test/ability_test.cpp +++ b/test/moduletest/ability_test/ability_test.cpp @@ -1153,7 +1153,7 @@ HWTEST_F(AbilityTerminateTest, AaFwk_IAbilityManager_AddFreeInstallObserver_0100 OHOS::DelayedSingleton::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID); sptr abms = iface_cast(remoteObject_); EXPECT_NE(abms, nullptr); - EXPECT_EQ(0, abms->AddFreeInstallObserver(nullptr)); + EXPECT_EQ(0, abms->AddFreeInstallObserver(nullptr, nullptr)); GTEST_LOG_(INFO) << "AaFwk_IAbilityManager_AddFreeInstallObserver_0100"; } diff --git a/test/unittest/ability_manager_client_branch_test/ability_manager_client_branch_test.cpp b/test/unittest/ability_manager_client_branch_test/ability_manager_client_branch_test.cpp index 346b1cff88..32fcf89a3a 100644 --- a/test/unittest/ability_manager_client_branch_test/ability_manager_client_branch_test.cpp +++ b/test/unittest/ability_manager_client_branch_test/ability_manager_client_branch_test.cpp @@ -1378,7 +1378,7 @@ HWTEST_F(AbilityManagerClientBranchTest, StartAbilityByCall_002, TestSize.Level1 Want want; EXPECT_EQ(client_->StartAbilityByCall(want, nullptr), ERR_OK); client_->EnableRecoverAbility(nullptr); - EXPECT_EQ(client_->AddFreeInstallObserver(nullptr), ERR_OK); + EXPECT_EQ(client_->AddFreeInstallObserver(nullptr, nullptr), ERR_OK); } /** diff --git a/test/unittest/ability_manager_service_third_test/ability_manager_service_third_test.cpp b/test/unittest/ability_manager_service_third_test/ability_manager_service_third_test.cpp index 43fc226ceb..07f3a9f3b4 100644 --- a/test/unittest/ability_manager_service_third_test/ability_manager_service_third_test.cpp +++ b/test/unittest/ability_manager_service_third_test/ability_manager_service_third_test.cpp @@ -982,10 +982,10 @@ HWTEST_F(AbilityManagerServiceThirdTest, AddFreeInstallObserver_001, TestSize.Le TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceThirdTest AddFreeInstallObserver_001 start"); auto abilityMs_ = std::make_shared(); sptr observer; - EXPECT_EQ(abilityMs_->AddFreeInstallObserver(observer), ERR_INVALID_VALUE); + EXPECT_EQ(abilityMs_->AddFreeInstallObserver(nullptr, observer), ERR_INVALID_VALUE); abilityMs_->freeInstallManager_ = std::make_shared(abilityMs_); - EXPECT_EQ(abilityMs_->AddFreeInstallObserver(observer), ERR_INVALID_VALUE); + EXPECT_EQ(abilityMs_->AddFreeInstallObserver(nullptr, observer), ERR_INVALID_VALUE); TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceThirdTest AddFreeInstallObserver_001 end"); } diff --git a/test/unittest/free_install_manager_test/free_install_manager_test.cpp b/test/unittest/free_install_manager_test/free_install_manager_test.cpp index fad2764103..146d30fdbf 100644 --- a/test/unittest/free_install_manager_test/free_install_manager_test.cpp +++ b/test/unittest/free_install_manager_test/free_install_manager_test.cpp @@ -98,7 +98,7 @@ HWTEST_F(FreeInstallTest, FreeInstall_StartFreeInstall_001, TestSize.Level1) break; } } - freeInstallManager_->OnInstallFinished(0, want, userId, false); + freeInstallManager_->OnInstallFinished(-1, 0, want, userId, false); EXPECT_EQ(res, 0); } @@ -153,7 +153,7 @@ HWTEST_F(FreeInstallTest, FreeInstall_StartFreeInstall_003, TestSize.Level1) break; } } - freeInstallManager_->OnInstallFinished(1, want, userId, false); + freeInstallManager_->OnInstallFinished(-1, 1, want, userId, false); EXPECT_EQ(res, 0); } @@ -177,7 +177,7 @@ HWTEST_F(FreeInstallTest, FreeInstall_OnInstallFinished_001, TestSize.Level1) FreeInstallInfo info = freeInstallManager_->BuildFreeInstallInfo(want, userId, requestCode, nullptr, false); freeInstallManager_->freeInstallList_.resize(0); freeInstallManager_->freeInstallList_.emplace_back(info); - freeInstallManager_->OnInstallFinished(0, want, userId, false); + freeInstallManager_->OnInstallFinished(-1, 0, want, userId, false); for (auto it = freeInstallManager_->freeInstallList_.begin(); it != freeInstallManager_->freeInstallList_.end(); it++) { @@ -211,7 +211,7 @@ HWTEST_F(FreeInstallTest, FreeInstall_OnInstallFinished_002, TestSize.Level1) FreeInstallInfo info = freeInstallManager_->BuildFreeInstallInfo(want, userId, requestCode, nullptr, false); freeInstallManager_->freeInstallList_.resize(0); freeInstallManager_->freeInstallList_.emplace_back(info); - freeInstallManager_->OnInstallFinished(1, want, userId, false); + freeInstallManager_->OnInstallFinished(-1, 1, want, userId, false); for (auto it = freeInstallManager_->freeInstallList_.begin(); it != freeInstallManager_->freeInstallList_.end(); it++) { @@ -247,7 +247,7 @@ HWTEST_F(FreeInstallTest, FreeInstall_OnInstallFinished_003, TestSize.Level1) freeInstallManager_->freeInstallList_.resize(0); info.promise.reset(); freeInstallManager_->freeInstallList_.emplace_back(info); - freeInstallManager_->OnInstallFinished(0, want, userId, false); + freeInstallManager_->OnInstallFinished(-1, 0, want, userId, false); int size = freeInstallManager_->freeInstallList_.size(); EXPECT_EQ(size, 1); @@ -291,7 +291,7 @@ HWTEST_F(FreeInstallTest, FreeInstall_OnRemoteInstallFinished_001, TestSize.Leve FreeInstallInfo info = freeInstallManager_->BuildFreeInstallInfo(want, userId, requestCode, nullptr, false); freeInstallManager_->freeInstallList_.resize(0); freeInstallManager_->freeInstallList_.emplace_back(info); - freeInstallManager_->OnRemoteInstallFinished(0, want, userId); + freeInstallManager_->OnRemoteInstallFinished(-1, 0, want, userId); for (auto it = freeInstallManager_->freeInstallList_.begin(); it != freeInstallManager_->freeInstallList_.end(); it++) {