diff --git a/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp b/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp index 3ca2f7ea40..2e0b4632ce 100644 --- a/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp +++ b/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp @@ -851,13 +851,52 @@ void JsUIAbility::ExecuteInsightIntentMoveToForeground(const Want &want, } } +void JsUIAbility::ExecuteInsightIntentBackground(const Want &want, + const std::shared_ptr &executeParam, + std::unique_ptr callback) +{ + HILOG_DEBUG("called."); + if (executeParam == nullptr) { + HILOG_WARN("Intent execute param invalid."); + InsightIntentExecutorMgr::TriggerCallbackInner(std::move(callback), ERR_OK); + return; + } + + if (abilityInfo_) { + jsRuntime_.UpdateModuleNameAndAssetPath(abilityInfo_->moduleName); + } + + InsightIntentExecutorInfo executeInfo; + auto ret = GetInsightIntentExecutorInfo(want, executeParam, executeInfo); + if (!ret) { + HILOG_ERROR("Get Intent executor failed."); + InsightIntentExecutorMgr::TriggerCallbackInner(std::move(callback), + static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM)); + return; + } + + ret = DelayedSingleton::GetInstance()->ExecuteInsightIntent( + jsRuntime_, executeInfo, std::move(callback)); + if (!ret) { + // callback has removed, release in insight intent executor. + HILOG_ERROR("Execute insight intent failed."); + } +} + bool JsUIAbility::GetInsightIntentExecutorInfo(const Want &want, const std::shared_ptr &executeParam, InsightIntentExecutorInfo& executeInfo) { HILOG_DEBUG("called."); + auto context = GetAbilityContext(); - if (executeParam == nullptr || context == nullptr || abilityInfo_ == nullptr || jsWindowStageObj_ == nullptr) { + if (executeParam == nullptr || context == nullptr || abilityInfo_ == nullptr) { + HILOG_ERROR("Param invalid."); + return false; + } + + if (executeParam->executeMode_ == AppExecFwk::ExecuteMode::UI_ABILITY_FOREGROUND + && jsWindowStageObj_ == nullptr) { HILOG_ERROR("Param invalid."); return false; } @@ -868,7 +907,9 @@ bool JsUIAbility::GetInsightIntentExecutorInfo(const Want &want, executeInfo.esmodule = abilityInfo_->compileMode == AppExecFwk::CompileMode::ES_MODULE; executeInfo.windowMode = windowMode_; executeInfo.token = context->GetToken(); - executeInfo.pageLoader = jsWindowStageObj_; + if (jsWindowStageObj_ != nullptr) { + executeInfo.pageLoader = jsWindowStageObj_; + } executeInfo.executeParam = executeParam; return true; } diff --git a/frameworks/native/ability/native/ability_thread.cpp b/frameworks/native/ability/native/ability_thread.cpp index 1243b044f1..937216971f 100644 --- a/frameworks/native/ability/native/ability_thread.cpp +++ b/frameworks/native/ability/native/ability_thread.cpp @@ -264,6 +264,11 @@ void AbilityThread::CallRequest() HILOG_DEBUG("called"); } +void AbilityThread::OnExecuteIntent(const Want &want) +{ + HILOG_DEBUG("called"); +} + std::vector> AbilityThread::ExecuteBatch( const std::vector> &operations) { diff --git a/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.cpp b/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.cpp index 232d0968b9..ae81658007 100644 --- a/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.cpp +++ b/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.cpp @@ -67,7 +67,7 @@ bool InsightIntentExecutorMgr::ExecuteInsightIntent(Runtime& runtime, const Insi TriggerCallbackInner(std::move(callback), static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM)); return false; } - + HILOG_DEBUG("AddInsightIntentExecutor."); AddInsightIntentExecutor(executeParam->insightIntentId_, intentExecutor); bool isAsync = false; diff --git a/frameworks/native/ability/native/insight_intent_executor/js_insight_intent_executor.cpp b/frameworks/native/ability/native/insight_intent_executor/js_insight_intent_executor.cpp index a30a86e30a..85e277f8f0 100644 --- a/frameworks/native/ability/native/insight_intent_executor/js_insight_intent_executor.cpp +++ b/frameworks/native/ability/native/insight_intent_executor/js_insight_intent_executor.cpp @@ -132,6 +132,7 @@ bool JsInsightIntentExecutor::HandleExecuteIntent( ReplyFailedInner(); STATE_PATTERN_NAIVE_STATE_SET_AND_RETURN(State::INVALID, false); } + HILOG_DEBUG("ExecuteInsightIntentUIAbilityBackground"); successful = ExecuteInsightIntentUIAbilityBackground(name, param); break; case InsightIntentExecuteMode::UIEXTENSION_ABILITY: diff --git a/frameworks/native/ability/native/ui_ability.cpp b/frameworks/native/ability/native/ui_ability.cpp index 8925d07c7f..21eb9dab9c 100644 --- a/frameworks/native/ability/native/ui_ability.cpp +++ b/frameworks/native/ability/native/ui_ability.cpp @@ -990,6 +990,13 @@ void UIAbility::ExecuteInsightIntentMoveToForeground(const AAFwk::Want &want, { HILOG_DEBUG("called"); } + +void UIAbility::ExecuteInsightIntentBackground(const AAFwk::Want &want, + const std::shared_ptr &executeParam, + std::unique_ptr callback) +{ + HILOG_DEBUG("called"); +} #endif } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/native/ability/native/ui_ability_impl.cpp b/frameworks/native/ability/native/ui_ability_impl.cpp index 89d9135454..11c7b19963 100644 --- a/frameworks/native/ability/native/ui_ability_impl.cpp +++ b/frameworks/native/ability/native/ui_ability_impl.cpp @@ -584,7 +584,12 @@ bool UIAbilityImpl::AbilityTransaction(const AAFwk::Want &want, const AAFwk::Lif ret = false; } #ifdef SUPPORT_GRAPHICS - Background(); + if (!InsightIntentExecuteParam::IsInsightIntentExecute(want)) { + Background(); + } else { + HILOG_DEBUG("HandleExecuteInsightIntentBackground"); + HandleExecuteInsightIntentBackground(want); + } #endif break; } @@ -733,6 +738,82 @@ void UIAbilityImpl::PostForegroundInsightIntent() AbilityTransactionCallback(AAFwk::ABILITY_STATE_FOREGROUND_NEW); } } + +void UIAbilityImpl::HandleExecuteInsightIntentBackground(const AAFwk::Want &want, bool onlyExecuteIntent) +{ + HILOG_INFO("Execute insight intent in background mode."); + auto executeParam = std::make_shared(); + auto ret = InsightIntentExecuteParam::GenerateFromWant(want, *executeParam); + if (!ret && !onlyExecuteIntent) { + HILOG_ERROR("Generate execute param failed."); + Background(); + return; + } + + HILOG_DEBUG("Insight intent bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s" + "insightIntentName: %{public}s, executeMode: %{public}d, intentId: %{public}" PRIu64"", + executeParam->bundleName_.c_str(), executeParam->moduleName_.c_str(), executeParam->abilityName_.c_str(), + executeParam->insightIntentName_.c_str(), executeParam->executeMode_, executeParam->insightIntentId_); + + auto intentCb = std::make_unique(); + intentCb.reset(InsightIntentExecutorAsyncCallback::Create()); + if (intentCb == nullptr && !onlyExecuteIntent) { + HILOG_ERROR("Create async callback failed."); + Background(); + return; + } + HILOG_DEBUG("lifecycleState_: %{public}d", lifecycleState_); + if (lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL + || lifecycleState_ == AAFwk::ABILITY_STATE_STARTED_NEW) { + ExecuteInsightIntentBackgroundByColdBoot(want, executeParam, std::move(intentCb)); + } else { + ExecuteInsightIntentBackgroundAlreadyStart(want, executeParam, std::move(intentCb)); + } +} + +void UIAbilityImpl::ExecuteInsightIntentBackgroundByColdBoot(const Want &want, + const std::shared_ptr &executeParam, + std::unique_ptr callback) +{ + HILOG_DEBUG("called."); + auto asyncCallback = + [weak = weak_from_this(), intentId = executeParam->insightIntentId_](InsightIntentExecuteResult result) { + HILOG_DEBUG("Execute insight intent finshed, intentId %{public}" PRIu64"", intentId); + auto abilityImpl = weak.lock(); + if (abilityImpl == nullptr) { + HILOG_ERROR("Ability impl is nullptr."); + return; + } + abilityImpl->Background(); + abilityImpl->ExecuteInsightIntentDone(intentId, result); + }; + callback->Push(asyncCallback); + + // private function, no need check ability_ validity. + ability_->ExecuteInsightIntentBackground(want, executeParam, std::move(callback)); +} + +void UIAbilityImpl::ExecuteInsightIntentBackgroundAlreadyStart(const Want &want, + const std::shared_ptr &executeParam, + std::unique_ptr callback) +{ + HILOG_DEBUG("called."); + + auto asyncCallback = + [weak = weak_from_this(), intentId = executeParam->insightIntentId_](InsightIntentExecuteResult result) { + HILOG_DEBUG("Execute insight intent finshed, intentId %{public}" PRIu64"", intentId); + auto abilityImpl = weak.lock(); + if (abilityImpl == nullptr) { + HILOG_ERROR("Ability impl is nullptr."); + return; + } + abilityImpl->ExecuteInsightIntentDone(intentId, result); + }; + callback->Push(asyncCallback); + + // private function, no need check ability_ validity. + ability_->ExecuteInsightIntentBackground(want, executeParam, std::move(callback)); +} #endif } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/native/ability/native/ui_ability_thread.cpp b/frameworks/native/ability/native/ui_ability_thread.cpp index a0c35562cd..3a2ef565ab 100644 --- a/frameworks/native/ability/native/ui_ability_thread.cpp +++ b/frameworks/native/ability/native/ui_ability_thread.cpp @@ -644,6 +644,34 @@ void UIAbilityThread::CallRequest() HILOG_DEBUG("End."); } +void UIAbilityThread::OnExecuteIntent(const Want &want) +{ + HILOG_DEBUG("Begin."); + if (abilityImpl_ == nullptr) { + HILOG_ERROR("abilityImpl_ is nullptr."); + return; + } + + if (abilityHandler_ == nullptr) { + HILOG_ERROR("abilityHandler_ is nullptr."); + return; + } + + wptr weak = this; + auto task = [weak, want]() { + auto abilityThread = weak.promote(); + if (abilityThread == nullptr) { + HILOG_ERROR("AbilityThread is nullptr."); + return; + } + if (abilityThread->abilityImpl_ != nullptr) { + abilityThread->abilityImpl_->HandleExecuteInsightIntentBackground(want, true); + return; + } + }; + abilityHandler_->PostTask(task, "UIAbilityThread:OnExecuteIntent"); +} + void UIAbilityThread::HandlePrepareTermianteAbility() { std::unique_lock lock(mutex_); diff --git a/interfaces/inner_api/ability_manager/include/ability_scheduler_interface.h b/interfaces/inner_api/ability_manager/include/ability_scheduler_interface.h index a4a6b528bc..12e68485da 100644 --- a/interfaces/inner_api/ability_manager/include/ability_scheduler_interface.h +++ b/interfaces/inner_api/ability_manager/include/ability_scheduler_interface.h @@ -293,6 +293,8 @@ public: virtual void DumpAbilityInfo(const std::vector ¶ms, std::vector &info) = 0; + virtual void OnExecuteIntent(const Want &want) = 0; + #ifdef ABILITY_COMMAND_FOR_TEST virtual int BlockAbility() = 0; #endif @@ -391,7 +393,9 @@ public: SCHEDULE_SHARE_DATA, // ipc id for scheduling service ability to prepare terminate (30) - SCHEDULE_ABILITY_PREPARE_TERMINATE + SCHEDULE_ABILITY_PREPARE_TERMINATE, + + SCHEDULE_ONEXECUTE_INTENT, }; }; } // namespace AAFwk diff --git a/interfaces/kits/native/ability/native/ability_runtime/js_ui_ability.h b/interfaces/kits/native/ability/native/ability_runtime/js_ui_ability.h index ff54081320..1603f91d96 100644 --- a/interfaces/kits/native/ability/native/ability_runtime/js_ui_ability.h +++ b/interfaces/kits/native/ability/native/ability_runtime/js_ui_ability.h @@ -260,6 +260,17 @@ public: const std::shared_ptr &executeParam, std::unique_ptr callback) override; + /** + * @brief Execute insight intent when an ability didn't started, schedule it to background. + * + * @param want Want. + * @param executeParam insight intent execute param. + * @param callback insight intent async callback. + */ + virtual void ExecuteInsightIntentBackground(const AAFwk::Want &want, + const std::shared_ptr &executeParam, + std::unique_ptr callback) override; + protected: void DoOnForeground(const Want &want) override; void ContinuationRestore(const Want &want) override; diff --git a/interfaces/kits/native/ability/native/ability_thread.h b/interfaces/kits/native/ability/native/ability_thread.h index 8407642efa..0173d7cdca 100644 --- a/interfaces/kits/native/ability/native/ability_thread.h +++ b/interfaces/kits/native/ability/native/ability_thread.h @@ -335,6 +335,8 @@ public: */ void CallRequest() override; + void OnExecuteIntent(const Want &want) override; + /** * @brief Execute Batch * @param operations Indicates the operations diff --git a/interfaces/kits/native/ability/native/ui_ability.h b/interfaces/kits/native/ability/native/ui_ability.h index 016294aca4..28dbe15f6b 100644 --- a/interfaces/kits/native/ability/native/ui_ability.h +++ b/interfaces/kits/native/ability/native/ui_ability.h @@ -483,6 +483,17 @@ public: const std::shared_ptr &executeParam, std::unique_ptr callback); + /** + * @brief Execute insight intent when an ability didn't started, schedule it to background. + * + * @param want Want. + * @param executeParam insight intent execute param. + * @param callback insight intent async callback. + */ + virtual void ExecuteInsightIntentBackground(const AAFwk::Want &want, + const std::shared_ptr &executeParam, + std::unique_ptr callback); + protected: class UIAbilityDisplayListener : public OHOS::Rosen::DisplayManager::IDisplayListener { public: diff --git a/interfaces/kits/native/ability/native/ui_ability_impl.h b/interfaces/kits/native/ability/native/ui_ability_impl.h index 00f21298c9..b12967a4b9 100644 --- a/interfaces/kits/native/ability/native/ui_ability_impl.h +++ b/interfaces/kits/native/ability/native/ui_ability_impl.h @@ -155,6 +155,8 @@ public: */ void HandleShareData(int32_t uniqueId); + void HandleExecuteInsightIntentBackground(const AAFwk::Want &want, bool onlyExecuteIntent = false); + #ifdef SUPPORT_GRAPHICS public: @@ -226,6 +228,12 @@ private: inline void ExecuteInsightIntentMoveToForeground(const Want &want, const std::shared_ptr &executeParam, std::unique_ptr callback); + inline void ExecuteInsightIntentBackgroundByColdBoot(const Want &want, + const std::shared_ptr &executeParam, + std::unique_ptr callback); + inline void ExecuteInsightIntentBackgroundAlreadyStart(const Want &want, + const std::shared_ptr &executeParam, + std::unique_ptr callback); class WindowLifeCycleImpl : public Rosen::IWindowLifeCycle { public: diff --git a/interfaces/kits/native/ability/native/ui_ability_thread.h b/interfaces/kits/native/ability/native/ui_ability_thread.h index b31204a7d3..f1b3f5592f 100644 --- a/interfaces/kits/native/ability/native/ui_ability_thread.h +++ b/interfaces/kits/native/ability/native/ui_ability_thread.h @@ -138,6 +138,8 @@ public: */ void CallRequest() override; + void OnExecuteIntent(const Want &want) override; + private: void DumpAbilityInfoInner(const std::vector ¶ms, std::vector &info); void DumpOtherInfo(std::vector &info); diff --git a/services/abilitymgr/abilitymgr.gni b/services/abilitymgr/abilitymgr.gni index 8a258c9326..d5ae4a69f2 100644 --- a/services/abilitymgr/abilitymgr.gni +++ b/services/abilitymgr/abilitymgr.gni @@ -14,6 +14,7 @@ import("//foundation/ability/ability_runtime/ability_runtime.gni") abilityms_files = [ + "src/ability_background_connection.cpp", "src/ability_connect_manager.cpp", "src/ability_debug_deal.cpp", "src/ability_event_handler.cpp", diff --git a/services/abilitymgr/include/ability_background_connection.h b/services/abilitymgr/include/ability_background_connection.h new file mode 100644 index 0000000000..f907fdeb7c --- /dev/null +++ b/services/abilitymgr/include/ability_background_connection.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef OHOS_ABILITY_RUNTIME_ABILITY_BACKGROUND_CONNECTION_H +#define OHOS_ABILITY_RUNTIME_ABILITY_BACKGROUND_CONNECTION_H + +#include "ability_connect_callback_interface.h" +#include "iremote_object.h" +#include "iremote_stub.h" +#include "nocopyable.h" + +namespace OHOS { +namespace AAFwk { +/** + * @class AbilityBackgroundConnection + * Ability Background Connection Stub. + */ +class AbilityBackgroundConnection : public IRemoteStub { +public: + AbilityBackgroundConnection() = default; + virtual ~AbilityBackgroundConnection() = default; + + /** + * @brief OnAbilityConnectDone, AbilityMs notify caller ability the result of connect. + * @param element service ability's ElementName. + * @param remoteObject the session proxy of service ability. + * @param resultCode ERR_OK on success, others on failure. + */ + void OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override; + + /** + * OnAbilityDisconnectDone, AbilityMs notify caller ability the result of disconnect. + * + * @param element, service ability's ElementName. + * @param resultCode, ERR_OK on success, others on failure. + */ + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; + +private: + DISALLOW_COPY_AND_MOVE(AbilityBackgroundConnection); +}; +} // namespace AppExecFwk +} // namespace OHOS + +#endif // ABILITY_BACKGROUND_CONNECTION_H diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index d6c61a06a9..823168fc67 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -1363,9 +1363,18 @@ public: int32_t ExecuteIntent(uint64_t key, const sptr &callerToken, const InsightIntentExecuteParam ¶m) override; + /** + * @brief Execute intent. + * @param abilityRequest The abilityRequest. + */ + int32_t OnExecuteIntent(AbilityRequest &abilityRequest, std::shared_ptr &targetRecord); + int32_t StartAbilityWithInsightIntent(const Want &want, int32_t userId = DEFAULT_INVAL_VALUE, int requestCode = DEFAULT_INVAL_VALUE); + int32_t StartAbilityByCallWithInsightIntent(const Want &want, const sptr &callerToken, + const InsightIntentExecuteParam ¶m); + /** * @brief Check if ability controller can start. * @param want The want of ability to start. @@ -1812,6 +1821,9 @@ private: int32_t StartExtensionAbilityWithInsightIntent(const Want &want, AppExecFwk::ExtensionAbilityType extensionType = AppExecFwk::ExtensionAbilityType::UNSPECIFIED); + bool IsAbilityStarted(AbilityRequest &abilityRequest, std::shared_ptr &targetRecord, + const int32_t oriValidUserId); + constexpr static int REPOLL_TIME_MICRO_SECONDS = 1000000; constexpr static int WAITING_BOOT_ANIMATION_TIMER = 5; diff --git a/services/abilitymgr/include/ability_scheduler_proxy.h b/services/abilitymgr/include/ability_scheduler_proxy.h index c4f84b3269..103cc5b454 100644 --- a/services/abilitymgr/include/ability_scheduler_proxy.h +++ b/services/abilitymgr/include/ability_scheduler_proxy.h @@ -322,6 +322,8 @@ public: void DumpAbilityInfo(const std::vector ¶ms, std::vector &info) override; void CallRequest() override; + void OnExecuteIntent(const Want &want) override; + #ifdef ABILITY_COMMAND_FOR_TEST int BlockAbility() override; #endif diff --git a/services/abilitymgr/include/ability_scheduler_stub.h b/services/abilitymgr/include/ability_scheduler_stub.h index 6a07f8c038..e9e1cf0c06 100644 --- a/services/abilitymgr/include/ability_scheduler_stub.h +++ b/services/abilitymgr/include/ability_scheduler_stub.h @@ -66,6 +66,7 @@ private: int NotifyContinuationResultInner(MessageParcel &data, MessageParcel &reply); int DumpAbilityInfoInner(MessageParcel& data, MessageParcel& reply); int CallRequestInner(MessageParcel &data, MessageParcel &reply); + int OnExecuteIntentInner(MessageParcel &data, MessageParcel &reply); #ifdef ABILITY_COMMAND_FOR_TEST int BlockAbilityInner(MessageParcel &data, MessageParcel &reply); #endif diff --git a/services/abilitymgr/include/mission_list_manager.h b/services/abilitymgr/include/mission_list_manager.h index 3d5c1ffa56..38b313cdda 100644 --- a/services/abilitymgr/include/mission_list_manager.h +++ b/services/abilitymgr/include/mission_list_manager.h @@ -348,6 +348,8 @@ public: const AAFwk::ContinueState &state); int32_t MoveMissionToBackground(int32_t missionId); + + bool IsAbilityStarted(AbilityRequest &abilityRequest, std::shared_ptr &targetRecord); #ifdef SUPPORT_GRAPHICS public: /** diff --git a/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h b/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h index aa246150ca..4afdd06756 100644 --- a/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h +++ b/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h @@ -295,6 +295,9 @@ public: void SetDevice(std::string deviceType); + bool IsAbilityStarted(AbilityRequest &abilityRequest, std::shared_ptr &targetRecord, + const int32_t oriValidUserId); + private: std::shared_ptr GetAbilityRecordByToken(const sptr &token) const; int32_t GetPersistentIdByAbilityRequest(const AbilityRequest &abilityRequest, bool &reuse, int32_t userId) const; diff --git a/services/abilitymgr/src/ability_background_connection.cpp b/services/abilitymgr/src/ability_background_connection.cpp new file mode 100644 index 0000000000..073616e1be --- /dev/null +++ b/services/abilitymgr/src/ability_background_connection.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "ability_background_connection.h" + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace AAFwk { +void AbilityBackgroundConnection::OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) +{ + if (resultCode != ERR_OK) { + HILOG_ERROR("%{public}s, abilityName:%{public}s, resultCode:%{public}d", + __func__, element.GetAbilityName().c_str(), resultCode); + return; + } +} + +void AbilityBackgroundConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + HILOG_DEBUG("OnAbilityDisconnectDone"); +} +} // namespace AppExecFwk +} // namespace OHOS diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index d09bc6b68d..e9bed96471 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -30,6 +30,7 @@ #include #include +#include "ability_background_connection.h" #include "ability_debug_deal.h" #include "ability_info.h" #include "ability_interceptor.h" @@ -8857,8 +8858,8 @@ int32_t AbilityManagerService::ExecuteIntent(uint64_t key, const sptr &targetRecord, const int32_t oriValidUserId) +{ + if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) { + HILOG_INFO("scene board is enable"); + if (uiAbilityLifecycleManager_ == nullptr) { + return false; + } + return uiAbilityLifecycleManager_->IsAbilityStarted(abilityRequest, targetRecord, oriValidUserId); + } + + auto missionListMgr = GetListManagerByUserId(oriValidUserId); + if (missionListMgr == nullptr) { + return false; + } + return missionListMgr->IsAbilityStarted(abilityRequest, targetRecord); +} + +int32_t AbilityManagerService::OnExecuteIntent(AbilityRequest &abilityRequest, + std::shared_ptr &targetRecord) +{ + HILOG_INFO("OnExecuteIntent"); + if (targetRecord == nullptr || targetRecord->GetScheduler() == nullptr) { + return ERR_INVALID_VALUE; + } + targetRecord->GetScheduler()->OnExecuteIntent(abilityRequest.want); + + return ERR_OK; +} + int32_t AbilityManagerService::StartAbilityWithInsightIntent(const Want &want, int32_t userId, int requestCode) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); @@ -8911,6 +8942,42 @@ int32_t AbilityManagerService::StartExtensionAbilityWithInsightIntent(const Want return StartExtensionAbilityInner(want, nullptr, DEFAULT_INVAL_VALUE, extensionType, true); } +int32_t AbilityManagerService::StartAbilityByCallWithInsightIntent(const Want &want, + const sptr &callerToken, const InsightIntentExecuteParam ¶m) +{ + HILOG_INFO("call StartAbilityByCallWithInsightIntent."); + sptr connect = sptr::MakeSptr(); + if (connect == nullptr) { + HILOG_ERROR("Invalid connect."); + return ERR_INVALID_VALUE; + } + + AbilityRequest abilityRequest; + abilityRequest.callType = AbilityCallType::CALL_REQUEST_TYPE; + abilityRequest.callerUid = IPCSkeleton::GetCallingUid(); + abilityRequest.callerToken = callerToken; + abilityRequest.startSetting = nullptr; + abilityRequest.want = want; + abilityRequest.connect = connect; + int32_t result = GenerateAbilityRequest(want, -1, abilityRequest, callerToken, GetUserId()); + if (result != ERR_OK) { + HILOG_ERROR("Generate ability request error."); + return result; + } + std::shared_ptr targetRecord; + int32_t oriValidUserId = GetValidUserId(DEFAULT_INVAL_VALUE); + auto missionListMgr = GetListManagerByUserId(oriValidUserId); + if (IsAbilityStarted(abilityRequest, targetRecord, oriValidUserId)) { + HILOG_INFO("ability has already started"); + result = OnExecuteIntent(abilityRequest, targetRecord); + } else { + result = StartAbilityByCall(want, connect, callerToken); + } + + HILOG_INFO("StartAbilityByCallWithInsightIntent %{public}d", result); + return result; +} + bool AbilityManagerService::IsAbilityControllerStart(const Want &want) { auto callingUid = IPCSkeleton::GetCallingUid(); diff --git a/services/abilitymgr/src/ability_scheduler_proxy.cpp b/services/abilitymgr/src/ability_scheduler_proxy.cpp index 164f364d27..f3fb00e144 100644 --- a/services/abilitymgr/src/ability_scheduler_proxy.cpp +++ b/services/abilitymgr/src/ability_scheduler_proxy.cpp @@ -1088,6 +1088,25 @@ void AbilitySchedulerProxy::CallRequest() HILOG_INFO("AbilitySchedulerProxy::CallRequest end"); } +void AbilitySchedulerProxy::OnExecuteIntent(const Want &want) +{ + HILOG_INFO("AbilitySchedulerProxy::OnExecuteIntent start"); + + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + if (!WriteInterfaceToken(data)) { + return ; + } + data.WriteParcelable(&want); + int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_ONEXECUTE_INTENT, data, reply, option); + if (err != NO_ERROR) { + HILOG_ERROR("ScheduleAbilityTransaction fail to SendRequest. err: %{public}d", err); + } + + HILOG_INFO("AbilitySchedulerProxy::OnExecuteIntent end"); +} + #ifdef ABILITY_COMMAND_FOR_TEST int AbilitySchedulerProxy::BlockAbility() { diff --git a/services/abilitymgr/src/ability_scheduler_stub.cpp b/services/abilitymgr/src/ability_scheduler_stub.cpp index 20cf62780e..23994908be 100644 --- a/services/abilitymgr/src/ability_scheduler_stub.cpp +++ b/services/abilitymgr/src/ability_scheduler_stub.cpp @@ -64,6 +64,7 @@ AbilitySchedulerStub::AbilitySchedulerStub() requestFuncMap_[CONTINUE_ABILITY] = &AbilitySchedulerStub::ContinueAbilityInner; requestFuncMap_[DUMP_ABILITY_RUNNER_INNER] = &AbilitySchedulerStub::DumpAbilityInfoInner; requestFuncMap_[SCHEDULE_SHARE_DATA] = &AbilitySchedulerStub::ShareDataInner; + requestFuncMap_[SCHEDULE_ONEXECUTE_INTENT] = &AbilitySchedulerStub::OnExecuteIntentInner; #ifdef ABILITY_COMMAND_FOR_TEST requestFuncMap_[BLOCK_ABILITY_INNER] = &AbilitySchedulerStub::BlockAbilityInner; @@ -640,6 +641,18 @@ int AbilitySchedulerStub::CallRequestInner(MessageParcel &data, MessageParcel &r return NO_ERROR; } +int AbilitySchedulerStub::OnExecuteIntentInner(MessageParcel &data, MessageParcel &reply) +{ + HILOG_INFO("AbilitySchedulerStub::OnExecuteIntentInner start"); + std::shared_ptr want(data.ReadParcelable()); + if (want == nullptr) { + HILOG_ERROR("AbilitySchedulerStub want is nullptr"); + return ERR_INVALID_VALUE; + } + OnExecuteIntent(*want); + return NO_ERROR; +} + #ifdef ABILITY_COMMAND_FOR_TEST int AbilitySchedulerStub::BlockAbilityInner(MessageParcel &data, MessageParcel &reply) { diff --git a/services/abilitymgr/src/mission_list_manager.cpp b/services/abilitymgr/src/mission_list_manager.cpp index 75a2a510f4..efcdf14f03 100644 --- a/services/abilitymgr/src/mission_list_manager.cpp +++ b/services/abilitymgr/src/mission_list_manager.cpp @@ -3116,6 +3116,14 @@ int MissionListManager::ResolveLocked(const AbilityRequest &abilityRequest) return CallAbilityLocked(abilityRequest); } +bool MissionListManager::IsAbilityStarted(AbilityRequest &abilityRequest, + std::shared_ptr &targetRecord) +{ + std::shared_ptr targetMission; + + return HandleReusedMissionAndAbility(abilityRequest, targetMission, targetRecord); +} + int MissionListManager::CallAbilityLocked(const AbilityRequest &abilityRequest) { HILOG_INFO("call ability."); diff --git a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp index c9320f8074..26b4d7e9b6 100644 --- a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp +++ b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp @@ -588,6 +588,24 @@ int UIAbilityLifecycleManager::ResolveLocked(const AbilityRequest &abilityReques return CallAbilityLocked(abilityRequest, userId); } +bool UIAbilityLifecycleManager::IsAbilityStarted(AbilityRequest &abilityRequest, + std::shared_ptr &targetRecord, const int32_t oriValidUserId) +{ + HILOG_DEBUG("Call."); + std::lock_guard guard(sessionLock_); + bool reuse = false; + auto persistentId = GetPersistentIdByAbilityRequest(abilityRequest, reuse, oriValidUserId); + if (persistentId == 0) { + return false; + } + targetRecord = sessionAbilityMap_.at(persistentId); + if (targetRecord) { + targetRecord->AddCallerRecord(abilityRequest.callerToken, abilityRequest.requestCode); + targetRecord->SetLaunchReason(LaunchReason::LAUNCHREASON_CALL); + } + return true; +} + int UIAbilityLifecycleManager::CallAbilityLocked(const AbilityRequest &abilityRequest, int32_t userId) { HILOG_DEBUG("Call."); @@ -606,7 +624,6 @@ int UIAbilityLifecycleManager::CallAbilityLocked(const AbilityRequest &abilityRe } else { uiAbilityRecord = sessionAbilityMap_.at(persistentId); } - uiAbilityRecord->AddCallerRecord(abilityRequest.callerToken, abilityRequest.requestCode); uiAbilityRecord->SetLaunchReason(LaunchReason::LAUNCHREASON_CALL); NotifyAbilityToken(uiAbilityRecord->GetToken(), abilityRequest); diff --git a/test/fuzztest/abilityschedulerstub_fuzzer/abilityschedulerstub_fuzzer.cpp b/test/fuzztest/abilityschedulerstub_fuzzer/abilityschedulerstub_fuzzer.cpp index e66a8a648e..b41c08888e 100755 --- a/test/fuzztest/abilityschedulerstub_fuzzer/abilityschedulerstub_fuzzer.cpp +++ b/test/fuzztest/abilityschedulerstub_fuzzer/abilityschedulerstub_fuzzer.cpp @@ -146,6 +146,8 @@ public: { return; } + void OnExecuteIntent(const Want &want) override + {} }; uint32_t GetU32Data(const char* ptr) diff --git a/test/mock/frameworks_kits_ability_native_test/include/mock_ability_manager_client.h b/test/mock/frameworks_kits_ability_native_test/include/mock_ability_manager_client.h index 19d9a183ce..4242ee2544 100644 --- a/test/mock/frameworks_kits_ability_native_test/include/mock_ability_manager_client.h +++ b/test/mock/frameworks_kits_ability_native_test/include/mock_ability_manager_client.h @@ -144,6 +144,7 @@ public: { return; }; + virtual void OnExecuteIntent(const Want &want) {}; virtual void ScheduleShareData(const int32_t &uniqueId) {}; virtual bool SchedulePrepareTerminateAbility() { diff --git a/test/mock/frameworks_kits_ability_native_test/include/mock_ability_scheduler_for_observer.h b/test/mock/frameworks_kits_ability_native_test/include/mock_ability_scheduler_for_observer.h index 32fcfdd5f0..716dfc4b81 100644 --- a/test/mock/frameworks_kits_ability_native_test/include/mock_ability_scheduler_for_observer.h +++ b/test/mock/frameworks_kits_ability_native_test/include/mock_ability_scheduler_for_observer.h @@ -73,7 +73,11 @@ public: virtual void CallRequest() { return; - }; + } + virtual void OnExecuteIntent(const Want &want) + { + return; + } }; } // namespace AppExecFwk } // namespace OHOS diff --git a/test/mock/services_abilitymgr_test/libs/aakit/include/ability_scheduler.h b/test/mock/services_abilitymgr_test/libs/aakit/include/ability_scheduler.h index fb9be2a3c1..50767aef55 100644 --- a/test/mock/services_abilitymgr_test/libs/aakit/include/ability_scheduler.h +++ b/test/mock/services_abilitymgr_test/libs/aakit/include/ability_scheduler.h @@ -108,6 +108,8 @@ public: { return; }; + virtual void OnExecuteIntent(const Want &want) override + {}; #ifdef ABILITY_COMMAND_FOR_TEST virtual int BlockAbility() override { diff --git a/test/mock/services_abilitymgr_test/libs/ability_scheduler_mock/ability_scheduler_mock.h b/test/mock/services_abilitymgr_test/libs/ability_scheduler_mock/ability_scheduler_mock.h index c78b621982..a1eb358d97 100644 --- a/test/mock/services_abilitymgr_test/libs/ability_scheduler_mock/ability_scheduler_mock.h +++ b/test/mock/services_abilitymgr_test/libs/ability_scheduler_mock/ability_scheduler_mock.h @@ -152,6 +152,10 @@ public: return; } + virtual void OnExecuteIntent(const Want &want) + { + return; + } #ifdef ABILITY_COMMAND_FOR_TEST virtual int BlockAbility() { diff --git a/test/moduletest/mock/include/mock_ability_scheduler.h b/test/moduletest/mock/include/mock_ability_scheduler.h index b226233d25..a5b0e59c0b 100644 --- a/test/moduletest/mock/include/mock_ability_scheduler.h +++ b/test/moduletest/mock/include/mock_ability_scheduler.h @@ -120,6 +120,10 @@ public: { return; } + virtual void OnExecuteIntent(const Want &want) override + { + return; + } #ifdef ABILITY_COMMAND_FOR_TEST virtual int BlockAbility() override { diff --git a/test/moduletest/mock/include/mock_ability_scheduler_stub.h b/test/moduletest/mock/include/mock_ability_scheduler_stub.h index b25bf53daf..29e52b83fe 100644 --- a/test/moduletest/mock/include/mock_ability_scheduler_stub.h +++ b/test/moduletest/mock/include/mock_ability_scheduler_stub.h @@ -60,7 +60,8 @@ public: { return; } - + void OnExecuteIntent(const Want &want) override + {}; #ifdef ABILITY_COMMAND_FOR_TEST virtual int BlockAbility() override { diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index e0aab7f84c..b3d678d3e4 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -104,6 +104,7 @@ config("test_exception_config") { ohos_source_set("abilityms_test_source") { sources = [ + "${ability_runtime_services_path}/abilitymgr/src/ability_background_connection.cpp", "${ability_runtime_services_path}/abilitymgr/src/ability_bundle_event_callback.cpp", "${ability_runtime_services_path}/abilitymgr/src/ability_connect_callback_stub.cpp", "${ability_runtime_services_path}/abilitymgr/src/ability_connect_manager.cpp", diff --git a/test/unittest/ability_scheduler_stub_test/ability_schedule_stub_mock.h b/test/unittest/ability_scheduler_stub_test/ability_schedule_stub_mock.h index 2e12666c77..84bfa00393 100644 --- a/test/unittest/ability_scheduler_stub_test/ability_schedule_stub_mock.h +++ b/test/unittest/ability_scheduler_stub_test/ability_schedule_stub_mock.h @@ -151,6 +151,8 @@ public: { return; } + void OnExecuteIntent(const Want &want) override + {} #ifdef ABILITY_COMMAND_FOR_TEST int BlockAbility() override {