diff --git a/frameworks/native/ability/native/ui_extension_ability/js_ui_extension.cpp b/frameworks/native/ability/native/ui_extension_ability/js_ui_extension.cpp index 5fdfe486f0..6b02fff834 100755 --- a/frameworks/native/ability/native/ui_extension_ability/js_ui_extension.cpp +++ b/frameworks/native/ability/native/ui_extension_ability/js_ui_extension.cpp @@ -428,8 +428,11 @@ bool JsUIExtension::ForegroundWindowWithInsightIntent(const AAFwk::Want &want, auto context = GetContext(); InsightIntentExecutorInfo executorInfo; - executorInfo.hapPath = context->GetAbilityInfo()->hapPath; - executorInfo.windowMode = context->GetAbilityInfo()->compileMode == AppExecFwk::CompileMode::ES_MODULE; + std::shared_ptr abilityInfo = context->GetAbilityInfo(); + if (abilityInfo != nullptr) { + executorInfo.hapPath = abilityInfo->hapPath; + executorInfo.windowMode = abilityInfo->compileMode == AppExecFwk::CompileMode::ES_MODULE; + } executorInfo.token = context->GetToken(); executorInfo.pageLoader = contentSessions_[sessionInfo->uiExtensionComponentId]; executorInfo.executeParam = std::make_shared(); diff --git a/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_base.cpp b/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_base.cpp index a01adbe20f..95fef9c8d3 100644 --- a/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_base.cpp +++ b/frameworks/native/ability/native/ui_extension_ability/js_ui_extension_base.cpp @@ -259,8 +259,11 @@ bool JsUIExtensionBase::ForegroundWindowWithInsightIntent(const AAFwk::Want &wan }); InsightIntentExecutorInfo executorInfo; - executorInfo.hapPath = context_->GetAbilityInfo()->hapPath; - executorInfo.windowMode = context_->GetAbilityInfo()->compileMode == AppExecFwk::CompileMode::ES_MODULE; + std::shared_ptr abilityInfo = context_->GetAbilityInfo(); + if (abilityInfo != nullptr) { + executorInfo.hapPath = abilityInfo->hapPath; + executorInfo.windowMode = abilityInfo->compileMode == AppExecFwk::CompileMode::ES_MODULE; + } executorInfo.token = context_->GetToken(); executorInfo.pageLoader = contentSessions_[sessionInfo->uiExtensionComponentId]; executorInfo.executeParam = std::make_shared(); diff --git a/services/abilitymgr/abilitymgr.gni b/services/abilitymgr/abilitymgr.gni index 64fab24186..47fe019d3d 100644 --- a/services/abilitymgr/abilitymgr.gni +++ b/services/abilitymgr/abilitymgr.gni @@ -63,7 +63,9 @@ abilityms_files = [ # new ability manager service here "src/task_data_persistence_mgr.cpp", - "src/ui_extension_ability_connect_manager.cpp", + "src/extension_record_manager.cpp", + "src/extension_record.cpp", + "src/ui_extension_record.cpp", "src/start_options.cpp", "src/stop_user_callback_proxy.cpp", "src/stop_user_callback_stub.cpp", diff --git a/services/abilitymgr/include/ability_connect_manager.h b/services/abilitymgr/include/ability_connect_manager.h index ed4bbd31a4..1421f22f8c 100644 --- a/services/abilitymgr/include/ability_connect_manager.h +++ b/services/abilitymgr/include/ability_connect_manager.h @@ -32,7 +32,7 @@ #include "connection_record.h" #include "element_name.h" #include "ui_extension_ability_connect_info.h" -#include "ui_extension_ability_connect_manager.h" +#include "extension_record_manager.h" #include "want.h" #include "iremote_object.h" #include "nocopyable.h" @@ -41,7 +41,7 @@ namespace OHOS { namespace AAFwk { using OHOS::AppExecFwk::AbilityType; using UIExtensionAbilityConnectInfo = AbilityRuntime::UIExtensionAbilityConnectInfo; -using UIExtensionAbilityConnectManager = AbilityRuntime::UIExtensionAbilityConnectManager; +using UIExtensionAbilityConnectManager = AbilityRuntime::ExtensionRecordManager; /** * @class AbilityConnectManager @@ -502,12 +502,12 @@ private: bool IsLauncher(std::shared_ptr serviceExtension) const; bool IsSceneBoard(std::shared_ptr serviceExtension) const; void KillProcessesByUserId() const; - inline bool IsUIExtensionAbility(const std::shared_ptr abilityRecord); + inline bool IsUIExtensionAbility(const std::shared_ptr &abilityRecord); inline bool CheckUIExtensionAbilityLoaded(const AbilityRequest &abilityRequest); - inline bool CheckUIExtensionAbilitySessionExistLocked(const std::shared_ptr abilityRecord); - inline int32_t AddUIExtensionAbilityRecord(const std::shared_ptr abilityRecord, - const std::string hostBundleName, const int32_t inputId) const; - inline void RemoveUIExtensionAbilityRecord(const std::shared_ptr abilityRecord); + inline bool CheckUIExtensionAbilitySessionExistLocked(const std::shared_ptr &abilityRecord); + inline int32_t AddUIExtensionAbilityRecord(const std::shared_ptr &abilityRecord, + const std::string &hostBundleName, const int32_t inputId) const; + inline void RemoveUIExtensionAbilityRecord(const std::shared_ptr &abilityRecord); private: const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask"; diff --git a/services/abilitymgr/include/ui_extension_ability_record.h b/services/abilitymgr/include/extension_record.h similarity index 54% rename from services/abilitymgr/include/ui_extension_ability_record.h rename to services/abilitymgr/include/extension_record.h index dd5e11b912..303ff2fc16 100644 --- a/services/abilitymgr/include/ui_extension_ability_record.h +++ b/services/abilitymgr/include/extension_record.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_RECORD_H -#define OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_RECORD_H +#ifndef OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_H +#define OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_H #include #include @@ -25,19 +25,28 @@ namespace OHOS { namespace AbilityRuntime { -class UIExtensionAbilityRecord : public std::enable_shared_from_this { +class ExtensionRecord : public std::enable_shared_from_this { public: - UIExtensionAbilityRecord(const std::shared_ptr abilityRecord, std::string hostBundleName, - int32_t uiExtensionAbilityId) - : abilityRecord_(abilityRecord), hostBundleName_(hostBundleName), uiExtensionAbilityId_(uiExtensionAbilityId) - {} + ExtensionRecord(const std::shared_ptr &abilityRecord, + const std::string &hostBundleName, int32_t extensionRecordId); - virtual ~UIExtensionAbilityRecord() = default; + virtual ~ExtensionRecord(); + + sptr GetCallToken() const; + + sptr GetRootCallerToken() const; + + void SetRootCallerToken(sptr &rootCallerToken); + + virtual bool ContinueToGetCallerToken(); std::shared_ptr abilityRecord_ = nullptr; - std::string hostBundleName_ = ""; - int32_t uiExtensionAbilityId_ = 0; + std::string hostBundleName_; + int32_t extensionRecordId_ = 0; + +private: + sptr rootCallerToken_ = nullptr; }; } // namespace AbilityRuntime } // namespace OHOS -#endif // OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_RECORD_H +#endif // OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_H diff --git a/services/abilitymgr/include/extension_record_manager.h b/services/abilitymgr/include/extension_record_manager.h new file mode 100644 index 0000000000..910ae2ea84 --- /dev/null +++ b/services/abilitymgr/include/extension_record_manager.h @@ -0,0 +1,88 @@ +/* + * 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_EXTENSION_RECORD_MANAGER_H +#define OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_MANAGER_H + +#include +#include +#include +#include + +#include "extension_record.h" + +namespace OHOS { +namespace AbilityRuntime { +constexpr int32_t INVALID_EXTENSION_RECORD_ID = 0; + +class ExtensionRecordManager : public std::enable_shared_from_this { +public: + using ExtensionAbilityRecordMap = std::map>; + + explicit ExtensionRecordManager(const int32_t userId); + virtual ~ExtensionRecordManager(); + + /** + * @brief Generate extension record id, if input id didn't exist, return it, else assign one. + * + * @param extensionRecordId Input extension record id. + * @return int32_t Generated extension record id. + */ + int32_t GenerateExtensionRecordId(const int32_t extensionRecordId); + + /** + * @brief Add extension record by id, if record exist, replace it. + * + * @param extensionRecordId extension record id. + * @param record extension record. + */ + void AddExtensionRecord(const int32_t extensionRecordId, const std::shared_ptr &record); + + /** + * @brief Remove extension record by id + * + * @param extensionRecordId extension record id. + */ + void RemoveExtensionRecord(const int32_t extensionRecordId); + + /** + * @brief Check if host bundleName matched to stored record by specified id. + * + * @param extensionRecordId extension record id. + * @param hostBundleName bundleName of target extension. + * @return true Matched. + * @return false Not Match. + */ + bool CheckExtensionLoaded(const int32_t extensionRecordId, const std::string &hostBundleName); + + static bool IsBelongToManager(const AppExecFwk::AbilityInfo &abilityInfo); + + bool IsFocused(int32_t extensionRecordId, const sptr& focusToken); + + int32_t CreateExtensionRecord(const std::shared_ptr &abilityRecord, + const std::string &hostBundleName, int32_t &extensionRecordId); + +private: + int32_t userId_; + static std::atomic_int32_t extensionRecordId_; + std::mutex mutex_; + std::set extensionRecordIdSet_; + ExtensionAbilityRecordMap extensionRecords_; + + sptr GetRootCallerTokenLocked(int32_t extensionRecordId); +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_MANAGER_H diff --git a/services/abilitymgr/include/ui_extension_ability_connect_manager.h b/services/abilitymgr/include/ui_extension_ability_connect_manager.h deleted file mode 100644 index 2b6e8d0061..0000000000 --- a/services/abilitymgr/include/ui_extension_ability_connect_manager.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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_UI_EXTENSION_ABILITY_CONNECT_MANAGER_H -#define OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_CONNECT_MANAGER_H - -#include -#include -#include -#include - -#include "ui_extension_ability_record.h" - -namespace OHOS { -namespace AbilityRuntime { -constexpr int32_t INVALID_UI_EXTENSION_ABILITY_ID = 0; - -class UIExtensionAbilityConnectManager : public std::enable_shared_from_this { -public: - using UIExtensionAbilityRecordMap = std::map>; - - explicit UIExtensionAbilityConnectManager(const int32_t userId); - virtual ~UIExtensionAbilityConnectManager(); - - /** - * @brief Generate uiextensionability id, if input id didn't exist, return it, else assign one. - * - * @param uiExtensionAbilityId Input uiextensionability id. - * @return int32_t Generated uiextensionability id. - */ - int32_t GenerateUIExtensionAbilityId(const int32_t uiExtensionAbilityId); - - /** - * @brief Add uiextensionability record by id, if record exist, replace it. - * - * @param uiExtensionAbilityId uiextensionability id. - * @param record uiextensionability record. - */ - void AddUIExtensionAbilityRecord(const int32_t uiExtensionAbilityId, - const std::shared_ptr record); - - /** - * @brief Remove uiextensionability record by id - * - * @param uiExtensionAbilityId uiextensionability id. - */ - void RemoveUIExtensionAbilityRecord(const int32_t uiExtensionAbilityId); - - /** - * @brief Check if host bundleName matched to stored record by specified id. - * - * @param uiExtensionAbilityId uiextensionability id. - * @param hostBundleName bundleName of target uiextensionability. - * @return true Matched. - * @return false Not Match. - */ - bool CheckUIExtensionAbilityLoaded(const int32_t uiExtensionAbilityId, const std::string hostBundleName); - -private: - int32_t userId_; - static std::atomic_int32_t uiExtensionAbilityId_; - std::mutex mutex_; - std::set uiExtensionAbilityIdSet_; - UIExtensionAbilityRecordMap uiExtensionAbilityRecords_; -}; -} // namespace AbilityRuntime -} // namespace OHOS -#endif // OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_CONNECT_MANAGER_H diff --git a/services/abilitymgr/include/ui_extension_record.h b/services/abilitymgr/include/ui_extension_record.h new file mode 100644 index 0000000000..1a4c2e85d6 --- /dev/null +++ b/services/abilitymgr/include/ui_extension_record.h @@ -0,0 +1,39 @@ +/* + * 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_UI_EXTENSION_RECORD_H +#define OHOS_ABILITY_RUNTIME_UI_EXTENSION_RECORD_H + +#include +#include +#include +#include + +#include "extension_record.h" + +namespace OHOS { +namespace AbilityRuntime { +class UIExtensionRecord : public ExtensionRecord { +public: + UIExtensionRecord(const std::shared_ptr &abilityRecord, + const std::string &hostBundleName, int32_t extensionRecordId); + + ~UIExtensionRecord() override; + + bool ContinueToGetCallerToken() override; +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_UI_EXTENSION_RECORD_H diff --git a/services/abilitymgr/src/ability_connect_manager.cpp b/services/abilitymgr/src/ability_connect_manager.cpp index ecf02f3834..7e980b5158 100644 --- a/services/abilitymgr/src/ability_connect_manager.cpp +++ b/services/abilitymgr/src/ability_connect_manager.cpp @@ -30,7 +30,7 @@ #include "mock_session_manager_service.h" #include "parameter.h" #include "session/host/include/zidl/session_interface.h" -#include "ui_extension_ability_record.h" +#include "extension_record.h" #include "ui_extension_utils.h" namespace OHOS { @@ -69,7 +69,7 @@ const std::unordered_set FROZEN_WHITE_LIST { AbilityConnectManager::AbilityConnectManager(int userId) : userId_(userId) { - uiExtensionAbilityRecordMgr_ = std::make_unique(userId); + uiExtensionAbilityRecordMgr_ = std::make_unique(userId); } AbilityConnectManager::~AbilityConnectManager() @@ -136,7 +136,7 @@ int AbilityConnectManager::StartAbilityLocked(const AbilityRequest &abilityReque if (IsUIExtensionAbility(targetService) && abilityRequest.sessionInfo != nullptr) { std::string hostBundleName = abilityRequest.abilityInfo.bundleName; int32_t inputId = abilityRequest.sessionInfo->want.GetIntParam(UIEXTENSION_ABILITY_ID, - INVALID_UI_EXTENSION_ABILITY_ID); + INVALID_EXTENSION_RECORD_ID); auto uiExtensionAbilityId = AddUIExtensionAbilityRecord(targetService, hostBundleName, inputId); HILOG_DEBUG("UIExtensionAbility id %{public}d.", uiExtensionAbilityId); } @@ -723,7 +723,7 @@ int AbilityConnectManager::ScheduleDisconnectAbilityDoneLocked(const sptrRemoveConnectRecordFromList(connect); if (abilityRecord->IsConnectListEmpty() && abilityRecord->GetStartId() == 0) { if (IsUIExtensionAbility(abilityRecord) && CheckUIExtensionAbilitySessionExistLocked(abilityRecord)) { - HILOG_INFO("There exist ui extension component, don't terminate when disconnet."); + HILOG_INFO("There exist ui extension component, don't terminate when disconnect."); } else { HILOG_INFO("Service ability has no any connection, and not started, need terminate."); TerminateRecord(abilityRecord); @@ -2190,12 +2190,15 @@ void AbilityConnectManager::HandleUIExtWindowDiedTask(const sptr bool AbilityConnectManager::IsUIExtensionFocused(uint32_t uiExtensionTokenId, const sptr& focusToken) { + HILOG_DEBUG("called, id: %{public}u", uiExtensionTokenId); + CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, false); std::lock_guard guard(Lock_); for (auto& item: uiExtensionMap_) { auto uiExtension = item.second.first.lock(); auto sessionInfo = item.second.second; if (uiExtension && uiExtension->GetApplicationInfo().accessTokenId == uiExtensionTokenId - && sessionInfo && sessionInfo->callerToken == focusToken) { + && uiExtensionAbilityRecordMgr_->IsFocused(uiExtension->GetUIExtensionAbilityId(), focusToken)) { + HILOG_INFO("id: %{public}u, isFocused.", uiExtensionTokenId); return true; } } @@ -2302,7 +2305,7 @@ void AbilityConnectManager::HandleExtensionDisconnectTask(const std::shared_ptr< } } -bool AbilityConnectManager::IsUIExtensionAbility(const std::shared_ptr abilityRecord) +bool AbilityConnectManager::IsUIExtensionAbility(const std::shared_ptr &abilityRecord) { CHECK_POINTER_AND_RETURN(abilityRecord, false); return UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType); @@ -2314,20 +2317,20 @@ bool AbilityConnectManager::CheckUIExtensionAbilityLoaded(const AbilityRequest & CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, false); int32_t uiExtensionAbilityId = abilityRequest.sessionInfo->want.GetIntParam(UIEXTENSION_ABILITY_ID, - INVALID_UI_EXTENSION_ABILITY_ID); - if (uiExtensionAbilityId == INVALID_UI_EXTENSION_ABILITY_ID) { + INVALID_EXTENSION_RECORD_ID); + if (uiExtensionAbilityId == INVALID_EXTENSION_RECORD_ID) { HILOG_DEBUG("Didn't carry uiextension ability id when start."); return true; } - auto ret = uiExtensionAbilityRecordMgr_->CheckUIExtensionAbilityLoaded( + auto ret = uiExtensionAbilityRecordMgr_->CheckExtensionLoaded( uiExtensionAbilityId, abilityRequest.abilityInfo.bundleName); HILOG_DEBUG("UIExtensionAbility loaded status: %{public}s.", ret ? "true" : "false"); return ret; } bool AbilityConnectManager::CheckUIExtensionAbilitySessionExistLocked( - const std::shared_ptr abilityRecord) + const std::shared_ptr &abilityRecord) { CHECK_POINTER_AND_RETURN(abilityRecord, false); @@ -2342,26 +2345,21 @@ bool AbilityConnectManager::CheckUIExtensionAbilitySessionExistLocked( return false; } -int32_t AbilityConnectManager::AddUIExtensionAbilityRecord(const std::shared_ptr abilityRecord, - const std::string hostBundleName, const int32_t inputId) const +int32_t AbilityConnectManager::AddUIExtensionAbilityRecord(const std::shared_ptr &abilityRecord, + const std::string &hostBundleName, const int32_t inputId) const { - CHECK_POINTER_AND_RETURN(abilityRecord, INVALID_UI_EXTENSION_ABILITY_ID); - CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, INVALID_UI_EXTENSION_ABILITY_ID); - - auto uiExtensionAbilityId = uiExtensionAbilityRecordMgr_->GenerateUIExtensionAbilityId(inputId); - HILOG_DEBUG("Generated id is %{public}d.", uiExtensionAbilityId); - abilityRecord->SetUIExtensionAbilityId(uiExtensionAbilityId); - auto uiExtensionAbilityRecord = std::make_shared(abilityRecord, - hostBundleName, uiExtensionAbilityId); - uiExtensionAbilityRecordMgr_->AddUIExtensionAbilityRecord(uiExtensionAbilityId, uiExtensionAbilityRecord); + CHECK_POINTER_AND_RETURN(abilityRecord, INVALID_EXTENSION_RECORD_ID); + CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, INVALID_EXTENSION_RECORD_ID); + int32_t uiExtensionAbilityId = inputId; + uiExtensionAbilityRecordMgr_->CreateExtensionRecord(abilityRecord, hostBundleName, uiExtensionAbilityId); return uiExtensionAbilityId; } -void AbilityConnectManager::RemoveUIExtensionAbilityRecord(const std::shared_ptr abilityRecord) +void AbilityConnectManager::RemoveUIExtensionAbilityRecord(const std::shared_ptr &abilityRecord) { CHECK_POINTER(abilityRecord); CHECK_POINTER(uiExtensionAbilityRecordMgr_); - uiExtensionAbilityRecordMgr_->RemoveUIExtensionAbilityRecord(abilityRecord->GetUIExtensionAbilityId()); + uiExtensionAbilityRecordMgr_->RemoveExtensionRecord(abilityRecord->GetUIExtensionAbilityId()); } } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 05543a3bcd..3003c015a7 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -75,7 +75,7 @@ #include "string_wrapper.h" #include "system_ability_definition.h" #include "system_ability_token_callback.h" -#include "ui_extension_ability_connect_manager.h" +#include "extension_record_manager.h" #include "ui_extension_utils.h" #include "uri_permission_manager_client.h" #include "view_data.h" diff --git a/services/abilitymgr/src/extension_record.cpp b/services/abilitymgr/src/extension_record.cpp new file mode 100644 index 0000000000..1eb2f682f0 --- /dev/null +++ b/services/abilitymgr/src/extension_record.cpp @@ -0,0 +1,52 @@ +/* + * 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 "extension_record.h" +#include "ability_util.h" +#include "errors.h" + +namespace OHOS { +namespace AbilityRuntime { +ExtensionRecord::ExtensionRecord(const std::shared_ptr &abilityRecord, + const std::string &hostBundleName, int32_t extensionRecordId) + : abilityRecord_(abilityRecord), hostBundleName_(hostBundleName), extensionRecordId_(extensionRecordId) +{} + +ExtensionRecord::~ExtensionRecord() = default; + +sptr ExtensionRecord::GetCallToken() const +{ + CHECK_POINTER_AND_RETURN(abilityRecord_, nullptr); + auto sessionInfo = abilityRecord_->GetSessionInfo(); + CHECK_POINTER_AND_RETURN(sessionInfo, nullptr); + return sessionInfo->callerToken; +} + +sptr ExtensionRecord::GetRootCallerToken() const +{ + return rootCallerToken_; +} + +void ExtensionRecord::SetRootCallerToken(sptr &rootCallerToken) +{ + rootCallerToken_ = rootCallerToken; +} + +bool ExtensionRecord::ContinueToGetCallerToken() +{ + return false; +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/services/abilitymgr/src/extension_record_manager.cpp b/services/abilitymgr/src/extension_record_manager.cpp new file mode 100644 index 0000000000..5d9e912660 --- /dev/null +++ b/services/abilitymgr/src/extension_record_manager.cpp @@ -0,0 +1,164 @@ +/* + * 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 "extension_record_manager.h" + +#include "ability_util.h" +#include "hilog_wrapper.h" +#include "ui_extension_utils.h" +#include "ui_extension_record.h" + +namespace OHOS { +namespace AbilityRuntime { +std::atomic_int32_t ExtensionRecordManager::extensionRecordId_ = INVALID_EXTENSION_RECORD_ID; + +ExtensionRecordManager::ExtensionRecordManager(const int32_t userId) : userId_(userId) +{ + HILOG_DEBUG("constructor."); +} + +ExtensionRecordManager::~ExtensionRecordManager() +{ + HILOG_INFO("deconstructor."); +} + +int32_t ExtensionRecordManager::GenerateExtensionRecordId(const int32_t extensionRecordId) +{ + HILOG_DEBUG("Input id is %{public}d.", extensionRecordId); + std::lock_guard lock(mutex_); + if (extensionRecordId != INVALID_EXTENSION_RECORD_ID && + !extensionRecordIdSet_.count(extensionRecordId)) { + extensionRecordIdSet_.insert(extensionRecordId); + extensionRecordId_ = extensionRecordId; + return extensionRecordId_; + } + + if (extensionRecordId == INVALID_EXTENSION_RECORD_ID) { + ++extensionRecordId_; + } + + while (extensionRecordIdSet_.count(extensionRecordId_)) { + extensionRecordId_++; + } + + return extensionRecordId_; +} + +void ExtensionRecordManager::AddExtensionRecord(const int32_t extensionRecordId, + const std::shared_ptr &record) +{ + HILOG_DEBUG("extensionRecordId %{public}d.", extensionRecordId); + std::lock_guard lock(mutex_); + extensionRecords_.emplace(extensionRecordId, record); +} + +void ExtensionRecordManager::RemoveExtensionRecord(const int32_t extensionRecordId) +{ + HILOG_DEBUG("extensionRecordId %{public}d.", extensionRecordId); + std::lock_guard lock(mutex_); + extensionRecords_.erase(extensionRecordId); +} + +bool ExtensionRecordManager::CheckExtensionLoaded(const int32_t extensionRecordId, + const std::string &hostBundleName) +{ + HILOG_DEBUG("extensionRecordId %{public}d.", extensionRecordId); + std::lock_guard lock(mutex_); + // find target record firstly + auto it = extensionRecords_.find(extensionRecordId); + if (it != extensionRecords_.end() && it->second != nullptr) { + // check bundleName + HILOG_DEBUG("Stored host bundleName: %{public}s, input bundleName is %{public}s.", + it->second->hostBundleName_.c_str(), hostBundleName.c_str()); + if (it->second->hostBundleName_ == hostBundleName) { + return true; + } + } + HILOG_DEBUG("Not found stored id %{public}d.", extensionRecordId); + return false; +} + +bool ExtensionRecordManager::IsBelongToManager(const AppExecFwk::AbilityInfo &abilityInfo) +{ + // only support UIExtension now + return AAFwk::UIExtensionUtils::IsUIExtension(abilityInfo.extensionAbilityType); +} + +bool ExtensionRecordManager::IsFocused(int32_t extensionRecordId, const sptr& focusToken) +{ + std::lock_guard lock(mutex_); + sptr rootCallerToken = GetRootCallerTokenLocked(extensionRecordId); + bool isFocused = rootCallerToken == focusToken; + HILOG_DEBUG("id: %{public}d isFocused: %{public}d.", extensionRecordId, isFocused); + return isFocused; +} + +sptr ExtensionRecordManager::GetRootCallerTokenLocked(int32_t extensionRecordId) +{ + auto it = extensionRecords_.find(extensionRecordId); + if (it != extensionRecords_.end() && it->second != nullptr) { + sptr rootCallerToken = it->second->GetRootCallerToken(); + if (rootCallerToken != nullptr) { + return rootCallerToken; + } + if (!it->second->ContinueToGetCallerToken()) { + return it->second->GetCallToken(); + } + auto callerToken = it->second->GetCallToken(); + if (callerToken == nullptr) { + HILOG_ERROR("callerToken is null, id: %{public}d.", extensionRecordId); + return nullptr; + } + auto callerAbilityRecord = AAFwk::Token::GetAbilityRecordByToken(callerToken); + if (callerAbilityRecord == nullptr) { + HILOG_ERROR("callerAbilityRecord is null, id: %{public}d.", extensionRecordId); + return nullptr; + } + if (callerAbilityRecord->GetUIExtensionAbilityId() == INVALID_EXTENSION_RECORD_ID) { + HILOG_DEBUG("update rootCallerToken, id: %{public}d.", extensionRecordId); + it->second->SetRootCallerToken(callerToken); + return callerToken; + } + rootCallerToken = GetRootCallerTokenLocked(callerAbilityRecord->GetUIExtensionAbilityId()); + HILOG_DEBUG("update rootCallerToken, id: %{public}d.", extensionRecordId); + it->second->SetRootCallerToken(rootCallerToken); + return rootCallerToken; + } + HILOG_ERROR("Not found id %{public}d.", extensionRecordId); + return nullptr; +} + +int32_t ExtensionRecordManager::CreateExtensionRecord(const std::shared_ptr &abilityRecord, + const std::string &hostBundleName, int32_t &extensionRecordId) +{ + // factory pattern with ability request + if (abilityRecord == nullptr) { + HILOG_ERROR("abilityRecord is null"); + return ERR_NULL_OBJECT; + } + extensionRecordId = GenerateExtensionRecordId(extensionRecordId); + if (AAFwk::UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) { + std::shared_ptr extensionRecord = std::make_shared(abilityRecord, + hostBundleName, extensionRecordId); + std::lock_guard lock(mutex_); + HILOG_DEBUG("add UIExtension, id %{public}d.", extensionRecordId); + extensionRecords_[extensionRecordId] = extensionRecord; + abilityRecord->SetUIExtensionAbilityId(extensionRecordId); + return ERR_OK; + } + return ERR_INVALID_VALUE; +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/services/abilitymgr/src/ui_extension_ability_connect_manager.cpp b/services/abilitymgr/src/ui_extension_ability_connect_manager.cpp deleted file mode 100644 index 57b2d6f25c..0000000000 --- a/services/abilitymgr/src/ui_extension_ability_connect_manager.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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 "ui_extension_ability_connect_manager.h" - -#include "hilog_wrapper.h" - -namespace OHOS { -namespace AbilityRuntime { -std::atomic_int32_t UIExtensionAbilityConnectManager::uiExtensionAbilityId_ = INVALID_UI_EXTENSION_ABILITY_ID; - -UIExtensionAbilityConnectManager::UIExtensionAbilityConnectManager(const int userId) : userId_(userId) -{ - HILOG_DEBUG("constructor."); -} - -UIExtensionAbilityConnectManager::~UIExtensionAbilityConnectManager() -{ - HILOG_INFO("deconstructor."); -} - -int32_t UIExtensionAbilityConnectManager::GenerateUIExtensionAbilityId(const int32_t uiExtensionAbilityId) -{ - HILOG_DEBUG("Input id is %{public}d.", uiExtensionAbilityId); - std::lock_guard lock(mutex_); - if (uiExtensionAbilityId != INVALID_UI_EXTENSION_ABILITY_ID && - !uiExtensionAbilityIdSet_.count(uiExtensionAbilityId)) { - uiExtensionAbilityIdSet_.insert(uiExtensionAbilityId); - uiExtensionAbilityId_ = uiExtensionAbilityId; - return uiExtensionAbilityId_; - } - - if (uiExtensionAbilityId == INVALID_UI_EXTENSION_ABILITY_ID) { - ++uiExtensionAbilityId_; - } - - while (uiExtensionAbilityIdSet_.count(uiExtensionAbilityId_)) { - uiExtensionAbilityId_++; - } - - return uiExtensionAbilityId_; -} - -void UIExtensionAbilityConnectManager::AddUIExtensionAbilityRecord(const int32_t uiExtensionAbilityId, - const std::shared_ptr record) -{ - HILOG_DEBUG("UIExtensionAbilityId %{public}d.", uiExtensionAbilityId); - std::lock_guard lock(mutex_); - uiExtensionAbilityRecords_.emplace(uiExtensionAbilityId, record); -} - -void UIExtensionAbilityConnectManager::RemoveUIExtensionAbilityRecord(const int32_t uiExtensionAbilityId) -{ - HILOG_DEBUG("UIExtensionAbilityId %{public}d.", uiExtensionAbilityId); - std::lock_guard lock(mutex_); - uiExtensionAbilityRecords_.erase(uiExtensionAbilityId); -} - -bool UIExtensionAbilityConnectManager::CheckUIExtensionAbilityLoaded(const int32_t uiExtensionAbilityId, - const std::string hostBundleName) -{ - HILOG_DEBUG("UIExtensionAbilityId %{public}d.", uiExtensionAbilityId); - std::lock_guard lock(mutex_); - // find target record firstly - auto it = uiExtensionAbilityRecords_.find(uiExtensionAbilityId); - if (it != uiExtensionAbilityRecords_.end() && it->second != nullptr) { - // check bundlename - HILOG_DEBUG("Stored host bundleName: %{public}s, input bundleName is %{public}s.", - it->second->hostBundleName_.c_str(), hostBundleName.c_str()); - if (it->second->hostBundleName_ == hostBundleName) { - return true; - } - } - HILOG_DEBUG("Not found stored id %{public}d.", uiExtensionAbilityId); - return false; -} -} // namespace AbilityRuntime -} // namespace OHOS diff --git a/services/abilitymgr/src/ui_extension_record.cpp b/services/abilitymgr/src/ui_extension_record.cpp new file mode 100644 index 0000000000..6a577f8eec --- /dev/null +++ b/services/abilitymgr/src/ui_extension_record.cpp @@ -0,0 +1,33 @@ +/* + * 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 "ui_extension_record.h" +#include "ability_util.h" + +namespace OHOS { +namespace AbilityRuntime { +UIExtensionRecord::UIExtensionRecord(const std::shared_ptr &abilityRecord, + const std::string &hostBundleName, int32_t extensionRecordId) + : ExtensionRecord(abilityRecord, hostBundleName, extensionRecordId) +{} + +UIExtensionRecord::~UIExtensionRecord() = default; + +bool UIExtensionRecord::ContinueToGetCallerToken() +{ + return true; +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 35f6c2ffe3..4705b4d954 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -139,6 +139,8 @@ ohos_source_set("abilityms_test_source") { "${ability_runtime_services_path}/abilitymgr/src/data_ability_record.cpp", "${ability_runtime_services_path}/abilitymgr/src/dialog_session_record.cpp", "${ability_runtime_services_path}/abilitymgr/src/dlp_state_item.cpp", + "${ability_runtime_services_path}/abilitymgr/src/extension_record.cpp", + "${ability_runtime_services_path}/abilitymgr/src/extension_record_manager.cpp", "${ability_runtime_services_path}/abilitymgr/src/free_install_manager.cpp", "${ability_runtime_services_path}/abilitymgr/src/insight_intent_execute_manager.cpp", "${ability_runtime_services_path}/abilitymgr/src/insight_intent_profile.cpp", @@ -154,6 +156,7 @@ ohos_source_set("abilityms_test_source") { "${ability_runtime_services_path}/abilitymgr/src/start_ability_handler.cpp", "${ability_runtime_services_path}/abilitymgr/src/start_ability_handler/start_ability_sandbox_savefile.cpp", "${ability_runtime_services_path}/abilitymgr/src/task_data_persistence_mgr.cpp", + "${ability_runtime_services_path}/abilitymgr/src/ui_extension_record.cpp", "${ability_runtime_services_path}/abilitymgr/src/user_controller.cpp", "${ability_runtime_services_path}/abilitymgr/src/user_event_handler.cpp", "${ability_runtime_services_path}/abilitymgr/src/want_receiver_proxy.cpp", diff --git a/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp b/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp index e096e98aee..6bfb8cd530 100644 --- a/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp +++ b/test/unittest/ability_connect_manager_test/ability_connect_manager_test.cpp @@ -3082,14 +3082,71 @@ HWTEST_F(AbilityConnectManagerTest, IsUIExtensionFocused_001, TestSize.Level1) bool isFocused = connectManager->IsUIExtensionFocused( serviceRecord_->GetApplicationInfo().accessTokenId, serviceRecord1_->GetToken()); EXPECT_EQ(isFocused, false); + connectManager.reset(); +} - sptr sessionInfo = new (std::nothrow) SessionInfo(); - sessionInfo->callerToken = serviceRecord1_->GetToken(); +/* + * Feature: AbilityConnectManager + * Function: IsUIExtensionFocused + * SubFunction: IsUIExtensionFocused + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Verify AbilityConnectManager IsUIExtensionFocused + */ +HWTEST_F(AbilityConnectManagerTest, IsUIExtensionFocused_002, TestSize.Level1) +{ + std::shared_ptr connectManager = std::make_shared(3); + ASSERT_NE(connectManager, nullptr); + connectManager->uiExtensionMap_.clear(); + + std::string device = "device"; + std::string abilityName = "uiExtensionUserAbility"; + std::string appName = "uiExtensionUser"; + std::string bundleName = "com.ix.uiExtensionUser"; + std::string moduleName = "entry"; + auto request = GenerateAbilityRequest(device, abilityName, appName, bundleName, moduleName); + auto uiExtensionUser = AbilityRecord::CreateAbilityRecord(request); + EXPECT_NE(uiExtensionUser, nullptr); + + std::string abilityName1 = "uiExtensionAbility1"; + std::string appName1 = "uiExtensionProvider1"; + std::string bundleName1 = "com.ix.uiExtensionProvider1"; + std::string moduleName1 = "entry"; + auto request1 = GenerateAbilityRequest(device, abilityName1, appName1, bundleName1, moduleName1); + auto uiExtension1 = AbilityRecord::CreateAbilityRecord(request1); + EXPECT_NE(uiExtension1, nullptr); + uiExtension1->abilityInfo_.extensionAbilityType = ExtensionAbilityType::SYS_COMMON_UI; + sptr sessionInfo1 = new (std::nothrow) SessionInfo(); + sessionInfo1->callerToken = uiExtensionUser->GetToken(); + uiExtension1->sessionInfo_ = sessionInfo1; connectManager->uiExtensionMap_.emplace( - callbackA_->AsObject(), AbilityConnectManager::UIExtWindowMapValType(serviceRecord_, sessionInfo)); - isFocused = connectManager->IsUIExtensionFocused( - serviceRecord_->GetApplicationInfo().accessTokenId, serviceRecord1_->GetToken()); - EXPECT_EQ(isFocused, true); + callbackA_->AsObject(), AbilityConnectManager::UIExtWindowMapValType(uiExtension1, sessionInfo1)); + int32_t extensionId1 = 1; + int32_t ret = connectManager->uiExtensionAbilityRecordMgr_->CreateExtensionRecord(uiExtension1, "", extensionId1); + EXPECT_EQ(ret, ERR_OK); + bool isFocused1 = connectManager->IsUIExtensionFocused( + uiExtension1->GetApplicationInfo().accessTokenId, uiExtensionUser->GetToken()); + EXPECT_EQ(isFocused1, true); + + std::string abilityName2 = "uiExtensionAbility2"; + std::string appName2 = "uiExtensionProvider2"; + std::string bundleName2 = "com.ix.uiExtensionProvider2"; + std::string moduleName2 = "entry"; + auto request2 = GenerateAbilityRequest(device, abilityName2, appName2, bundleName2, moduleName2); + auto uiExtension2 = AbilityRecord::CreateAbilityRecord(request2); + EXPECT_NE(uiExtension2, nullptr); + uiExtension2->abilityInfo_.extensionAbilityType = ExtensionAbilityType::SYS_COMMON_UI; + sptr sessionInfo2 = new (std::nothrow) SessionInfo(); + sessionInfo2->callerToken = uiExtension1->GetToken(); + uiExtension2->sessionInfo_ = sessionInfo2; + connectManager->uiExtensionMap_.emplace( + callbackA_->AsObject(), AbilityConnectManager::UIExtWindowMapValType(uiExtension2, sessionInfo2)); + int32_t extensionId2 = 2; + ret = connectManager->uiExtensionAbilityRecordMgr_->CreateExtensionRecord(uiExtension2, "", extensionId2); + EXPECT_EQ(ret, ERR_OK); + bool isFocused2 = connectManager->IsUIExtensionFocused( + uiExtension2->GetApplicationInfo().accessTokenId, uiExtensionUser->GetToken()); + EXPECT_EQ(isFocused2, true); connectManager.reset(); }