diff --git a/interfaces/inner_api/ability_manager/BUILD.gn b/interfaces/inner_api/ability_manager/BUILD.gn index 5b4fd3db703a2cfe9b2c7922a2ef8d86640be80e..8dfbd55d98b94d3108f0bad2c87d52333f2db5a1 100644 --- a/interfaces/inner_api/ability_manager/BUILD.gn +++ b/interfaces/inner_api/ability_manager/BUILD.gn @@ -103,6 +103,7 @@ ohos_shared_library("ability_manager") { "${ability_runtime_services_path}/abilitymgr/src/start_options.cpp", "${ability_runtime_services_path}/abilitymgr/src/system_ability_token_callback_stub.cpp", "${ability_runtime_services_path}/abilitymgr/src/ui_extension_ability_connect_info.cpp", + "${ability_runtime_services_path}/abilitymgr/src/ui_extension_host_info.cpp", "${ability_runtime_services_path}/abilitymgr/src/user_callback_proxy.cpp", "${ability_runtime_services_path}/abilitymgr/src/user_callback_stub.cpp", "${ability_runtime_services_path}/abilitymgr/src/want_receiver_stub.cpp", 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 938cbc32dd3446881d2e825324a76b938e53a6d3..cefa9f33072f2ef6404ff054ae0b3ff09da5aea1 100644 --- a/interfaces/inner_api/ability_manager/include/ability_manager_client.h +++ b/interfaces/inner_api/ability_manager/include/ability_manager_client.h @@ -264,7 +264,7 @@ public: sptr callerToken, int32_t userId = DEFAULT_INVAL_VALUE, AppExecFwk::ExtensionAbilityType extensionType = AppExecFwk::ExtensionAbilityType::UNSPECIFIED); - + /** * Create UIExtension with want, send want to ability manager service. * @@ -1339,6 +1339,17 @@ public: */ void UpdateSessionInfoBySCB(const std::vector &sessionInfos, int32_t userId); + /** + * @brief Get host info of root caller. + * + * @param token The ability token. + * @param hostInfo The host info of root caller. + * @param userId The user id. + * @return ErrCode Returns ERR_OK on success, others on failure. + */ + ErrCode GetUIExtensionRootHostInfo(const sptr token, UIExtensionHostInfo &hostInfo, + int32_t userId = DEFAULT_INVAL_VALUE); + private: AbilityManagerClient(); DISALLOW_COPY_AND_MOVE(AbilityManagerClient); 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 7cc27627c89a11141ed35188500892aa410ff555..1a675f33334cc2e8b4ee95ad8fbdfb7d98a77116 100644 --- a/interfaces/inner_api/ability_manager/include/ability_manager_interface.h +++ b/interfaces/inner_api/ability_manager/include/ability_manager_interface.h @@ -50,6 +50,7 @@ #include "user_callback.h" #include "system_memory_attr.h" #include "ui_extension_ability_connect_info.h" +#include "ui_extension_host_info.h" #include "ui_extension_window_command.h" #include "uri.h" #include "want.h" @@ -67,6 +68,7 @@ using AutoStartupInfo = AbilityRuntime::AutoStartupInfo; using InsightIntentExecuteParam = AppExecFwk::InsightIntentExecuteParam; using InsightIntentExecuteResult = AppExecFwk::InsightIntentExecuteResult; using UIExtensionAbilityConnectInfo = AbilityRuntime::UIExtensionAbilityConnectInfo; +using UIExtensionHostInfo = AbilityRuntime::UIExtensionHostInfo; constexpr const char* ABILITY_MANAGER_SERVICE_NAME = "AbilityManagerService"; const int DEFAULT_INVAL_VALUE = -1; const int DELAY_LOCAL_FREE_INSTALL_TIMEOUT = 40000; @@ -1093,7 +1095,7 @@ public: { return 0; } - + /** * Record the process exit reason before the process being killed. * @param pid The process id. @@ -1330,6 +1332,20 @@ public: * @param sessionInfos The vector of session info. */ virtual void UpdateSessionInfoBySCB(const std::vector &sessionInfos, int32_t userId) {} + + /** + * @brief Get host info of root caller. + * + * @param token The ability token. + * @param hostInfo The host info of root caller. + * @param userId The user id. + * @return int32_t Returns 0 on success, others on failure. + */ + virtual int32_t GetUIExtensionRootHostInfo(const sptr token, UIExtensionHostInfo &hostInfo, + int32_t userId = DEFAULT_INVAL_VALUE) + { + return 0; + } }; } // namespace AAFwk } // namespace OHOS diff --git a/interfaces/inner_api/ability_manager/include/ability_manager_ipc_interface_code.h b/interfaces/inner_api/ability_manager/include/ability_manager_ipc_interface_code.h index ccd545566b03a592d58ee7d9595477cca7ef3ecb..f734d2cff103925170993c5088e16dccdea686e3 100644 --- a/interfaces/inner_api/ability_manager/include/ability_manager_ipc_interface_code.h +++ b/interfaces/inner_api/ability_manager/include/ability_manager_ipc_interface_code.h @@ -228,7 +228,7 @@ enum class AbilityManagerInterfaceCode { // ipc id for logout user (74) LOGOUT_USER = 74, - + // Get forgeround UI abilities(75) GET_FOREGROUND_UI_ABILITIES = 75, @@ -367,6 +367,9 @@ enum class AbilityManagerInterfaceCode { // request modal UIExtension by want REQUESET_MODAL_UIEXTENSION = 1056, + // get root host info of uiextesnion + GET_UI_EXTENSION_ROOT_HOST_INFO = 1057, + // ipc id for continue ability(1101) START_CONTINUATION = 1101, diff --git a/interfaces/inner_api/ability_manager/include/ui_extension_host_info.h b/interfaces/inner_api/ability_manager/include/ui_extension_host_info.h new file mode 100755 index 0000000000000000000000000000000000000000..da9fad303747dad70b920cbed99ac0a31cdf793c --- /dev/null +++ b/interfaces/inner_api/ability_manager/include/ui_extension_host_info.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 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 + * + * 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_HOST_INFO_H +#define OHOS_ABILITY_RUNTIME_UI_EXTENSION_HOST_INFO_H + +#include "element_name.h" +#include "parcel.h" + +namespace OHOS { +namespace AbilityRuntime { +class UIExtensionHostInfo : public Parcelable { +public: + UIExtensionHostInfo() = default; + virtual ~UIExtensionHostInfo() = default; + + bool ReadFromParcel(Parcel &parcel); + bool Marshalling(Parcel &parcel) const override; + static UIExtensionHostInfo *Unmarshalling(Parcel &parcel); + + AppExecFwk::ElementName elementName_; +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_UI_EXTENSION_HOST_INFO_H diff --git a/services/abilitymgr/include/ability_connect_manager.h b/services/abilitymgr/include/ability_connect_manager.h index 793441ef43bed89ca18086c5c7529bf3637f5500..0bd89992143545b1db92d1d28dc3372de47fce61 100644 --- a/services/abilitymgr/include/ability_connect_manager.h +++ b/services/abilitymgr/include/ability_connect_manager.h @@ -260,6 +260,8 @@ public: void RemoveLauncherDeathRecipient(); + int32_t GetUIExtensionRootHostInfo(const sptr token, UIExtensionHostInfo &hostInfo); + // MSG 0 - 20 represents timeout message static constexpr uint32_t LOAD_TIMEOUT_MSG = 0; static constexpr uint32_t CONNECT_TIMEOUT_MSG = 1; diff --git a/services/abilitymgr/include/ability_manager_proxy.h b/services/abilitymgr/include/ability_manager_proxy.h index 0a2004b8dd57012dffe482dac5bdd51946ba1ca8..6eb34e02730e60a15f86abc6d24537163e039c49 100644 --- a/services/abilitymgr/include/ability_manager_proxy.h +++ b/services/abilitymgr/include/ability_manager_proxy.h @@ -1072,6 +1072,18 @@ public: * @param sessionInfos The vector of session info. */ virtual void UpdateSessionInfoBySCB(const std::vector &sessionInfos, int32_t userId) override; + + /** + * @brief Get host info of root caller. + * + * @param token The ability token. + * @param hostInfo The host info of root caller. + * @param userId The user id. + * @return int32_t Returns 0 on success, others on failure. + */ + int32_t GetUIExtensionRootHostInfo(const sptr token, UIExtensionHostInfo &hostInfo, + int32_t userId = DEFAULT_INVAL_VALUE) override; + private: template int GetParcelableInfos(MessageParcel &reply, std::vector &parcelableInfos); diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index 97c4a542698ab5a2211835b675e054e06a4c5b4a..385cb7ed2af74fed72c860fc58a7bdd93437427f 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -1402,6 +1402,17 @@ public: */ virtual void UpdateSessionInfoBySCB(const std::vector &sessionInfos, int32_t userId) override; + /** + * @brief Get host info of root caller. + * + * @param token The ability token. + * @param hostInfo The host info of root caller. + * @param userId The user id. + * @return int32_t Returns ERR_OK on success, others on failure. + */ + int32_t GetUIExtensionRootHostInfo(const sptr token, UIExtensionHostInfo &hostInfo, + int32_t userId = DEFAULT_INVAL_VALUE) override; + // MSG 0 - 20 represents timeout message static constexpr uint32_t LOAD_TIMEOUT_MSG = 0; static constexpr uint32_t ACTIVE_TIMEOUT_MSG = 1; diff --git a/services/abilitymgr/include/ability_manager_stub.h b/services/abilitymgr/include/ability_manager_stub.h index fa081f65ef1d6e7f249e5aef00f75eccbe543d95..f1f3f24e7ade7aae0293accbcd2e6712448d1e7a 100644 --- a/services/abilitymgr/include/ability_manager_stub.h +++ b/services/abilitymgr/include/ability_manager_stub.h @@ -273,6 +273,8 @@ private: int32_t StartAbilityByInsightIntentInner(MessageParcel &data, MessageParcel &reply); int32_t ExecuteInsightIntentDoneInner(MessageParcel &data, MessageParcel &reply); int32_t GetForegroundUIAbilitiesInner(MessageParcel &data, MessageParcel &reply); + + int32_t GetUIExtensionRootHostInfoInner(MessageParcel &data, MessageParcel &reply); }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/include/ability_record.h b/services/abilitymgr/include/ability_record.h index 0d72cc9bc9bc2f2445866853e8266afe7518a854..703212d184e47bd9b8341da9e92cd53a48c07a9e 100644 --- a/services/abilitymgr/include/ability_record.h +++ b/services/abilitymgr/include/ability_record.h @@ -998,6 +998,8 @@ private: void NotifyMissionBindPid(); + void DumpUIExtensionRootHostInfo(std::vector &info) const; + #ifdef SUPPORT_GRAPHICS std::shared_ptr GetWantFromMission() const; void SetShowWhenLocked(const AppExecFwk::AbilityInfo &abilityInfo, sptr &info) const; diff --git a/services/abilitymgr/include/extension_record_manager.h b/services/abilitymgr/include/extension_record_manager.h index e421ffeae083429df039a5f87fd8abff255c262a..f6fa4e719931942c02250d653d20d761a500fe01 100644 --- a/services/abilitymgr/include/extension_record_manager.h +++ b/services/abilitymgr/include/extension_record_manager.h @@ -23,6 +23,7 @@ #include "ability_record.h" #include "extension_record.h" +#include "ui_extension_host_info.h" namespace OHOS { namespace AbilityRuntime { @@ -73,6 +74,8 @@ public: std::shared_ptr GetAbilityRecordBySessionInfo(const sptr &sessionInfo); + int32_t GetUIExtensionRootHostInfo(const sptr token, UIExtensionHostInfo &hostInfo); + private: int32_t userId_; static std::atomic_int32_t extensionRecordId_; diff --git a/services/abilitymgr/src/ability_connect_manager.cpp b/services/abilitymgr/src/ability_connect_manager.cpp index 839c79bdbc2205af0bc1ff0c3a014cb1882de20d..2b480a7d8231c50d6f5e5c6efdb057cb69999354 100644 --- a/services/abilitymgr/src/ability_connect_manager.cpp +++ b/services/abilitymgr/src/ability_connect_manager.cpp @@ -2559,5 +2559,13 @@ void AbilityConnectManager::RemoveUIExtensionAbilityRecord(const std::shared_ptr CHECK_POINTER(uiExtensionAbilityRecordMgr_); uiExtensionAbilityRecordMgr_->RemoveExtensionRecord(abilityRecord->GetUIExtensionAbilityId()); } + +int32_t AbilityConnectManager::GetUIExtensionRootHostInfo(const sptr token, + UIExtensionHostInfo &hostInfo) +{ + CHECK_POINTER_AND_RETURN(token, ERR_INVALID_VALUE); + CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, ERR_INVALID_VALUE); + return uiExtensionAbilityRecordMgr_->GetUIExtensionRootHostInfo(token, hostInfo); +} } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/ability_manager_client.cpp b/services/abilitymgr/src/ability_manager_client.cpp index 67d0ed7ef9da31599edfdaf2c8df284b4b29933b..ea92c1b7f914988a32108dfadf7093f9961b69f6 100644 --- a/services/abilitymgr/src/ability_manager_client.cpp +++ b/services/abilitymgr/src/ability_manager_client.cpp @@ -1678,5 +1678,14 @@ void AbilityManagerClient::UpdateSessionInfoBySCB(const std::vector CHECK_POINTER_RETURN(abms); abms->UpdateSessionInfoBySCB(sessionInfos, userId); } + +ErrCode AbilityManagerClient::GetUIExtensionRootHostInfo(const sptr token, + UIExtensionHostInfo &hostInfo, int32_t userId) +{ + HILOG_DEBUG("Get ui extension host info."); + auto abms = GetAbilityManager(); + CHECK_POINTER_RETURN_NOT_CONNECTED(abms); + return abms->GetUIExtensionRootHostInfo(token, hostInfo, userId); +} } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/ability_manager_proxy.cpp b/services/abilitymgr/src/ability_manager_proxy.cpp index 545b9002badc6c2685c74b008fef8b903ff02af0..a04e24133eb0fe60b5d69162b91130aecf991e3b 100644 --- a/services/abilitymgr/src/ability_manager_proxy.cpp +++ b/services/abilitymgr/src/ability_manager_proxy.cpp @@ -4585,5 +4585,46 @@ int32_t AbilityManagerProxy::CancelApplicationAutoStartupByEDM(const AutoStartup } return reply.ReadInt32(); } + +int32_t AbilityManagerProxy::GetUIExtensionRootHostInfo(const sptr token, + UIExtensionHostInfo &hostInfo, int32_t userId) +{ + if (token == nullptr) { + HILOG_ERROR("Input param invalid."); + return ERR_INVALID_VALUE; + } + + MessageParcel data; + if (!WriteInterfaceToken (data)) { + HILOG_ERROR("Write remote object failed."); + return INNER_ERR; + } + + if (!data.WriteBool(true) || !data.WriteRemoteObject(token)) { + HILOG_ERROR("Write flag and token failed."); + return INNER_ERR; + } + + if (!data.WriteInt32(userId)) { + HILOG_ERROR("Write userId failed."); + return INNER_ERR; + } + + MessageParcel reply; + MessageOption option; + auto error = SendRequest(AbilityManagerInterfaceCode::GET_UI_EXTENSION_ROOT_HOST_INFO, data, reply, option); + if (error != NO_ERROR) { + HILOG_ERROR("Send request error: %{public}d", error); + return error; + } + + std::unique_ptr info(reply.ReadParcelable()); + if (info == nullptr) { + HILOG_ERROR("Get host info failed."); + return INNER_ERR; + } + hostInfo = *info; + return reply.ReadInt32(); +} } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 95e0a46a83228ad9edee179439a53e0ee855d50e..37f48f790050dbda10ba06ea70b490c81600516b 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -9366,5 +9366,33 @@ void AbilityManagerService::WaitBootAnimationStart() } // other, the animation is ready, not wait. } + +int32_t AbilityManagerService::GetUIExtensionRootHostInfo(const sptr token, + UIExtensionHostInfo &hostInfo, int32_t userId) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + HILOG_DEBUG("Get ui extension host info."); + CHECK_POINTER_AND_RETURN(token, ERR_INVALID_VALUE); + + if (!AAFwk::PermissionVerification::GetInstance()->IsSACall()) { + HILOG_ERROR("Not sa call."); + return ERR_PERMISSION_DENIED; + } + + auto validUserId = GetValidUserId(userId); + auto connectManager = GetConnectManagerByUserId(validUserId); + if (connectManager == nullptr) { + HILOG_ERROR("Connect manager is nullptr, userId: %{public}d.", validUserId); + return ERR_INVALID_VALUE; + } + + auto ret = connectManager->GetUIExtensionRootHostInfo(token, hostInfo); + if (ret != ERR_OK) { + HILOG_ERROR("Get host info failed with %{public}d.", ret); + return ret; + } + + return ERR_OK; +} } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/ability_manager_stub.cpp b/services/abilitymgr/src/ability_manager_stub.cpp index 4742115075add106d068fbd6919e1c5772062ed4..5f3f1d70ae2cdfe19b88b442859842d341409425 100644 --- a/services/abilitymgr/src/ability_manager_stub.cpp +++ b/services/abilitymgr/src/ability_manager_stub.cpp @@ -321,6 +321,8 @@ void AbilityManagerStub::ThirdStepInit() &AbilityManagerStub::PrepareTerminateAbilityBySCBInner; requestFuncMap_[static_cast(AbilityManagerInterfaceCode::REQUESET_MODAL_UIEXTENSION)] = &AbilityManagerStub::RequestModalUIExtensionInner; + requestFuncMap_[static_cast(AbilityManagerInterfaceCode::GET_UI_EXTENSION_ROOT_HOST_INFO)] = + &AbilityManagerStub::GetUIExtensionRootHostInfoInner; #ifdef SUPPORT_GRAPHICS requestFuncMap_[static_cast(AbilityManagerInterfaceCode::SET_MISSION_LABEL)] = &AbilityManagerStub::SetMissionLabelInner; @@ -2961,5 +2963,32 @@ int32_t AbilityManagerStub::UpdateSessionInfoBySCBInner(MessageParcel &data, Mes UpdateSessionInfoBySCB(sessionInfos, userId); return ERR_OK; } + +int32_t AbilityManagerStub::GetUIExtensionRootHostInfoInner(MessageParcel &data, MessageParcel &reply) +{ + sptr callerToken = nullptr; + if (data.ReadBool()) { + callerToken = data.ReadRemoteObject(); + if (callerToken == nullptr) { + HILOG_ERROR("caller token is nullptr."); + return ERR_INVALID_VALUE; + } + } + + int32_t userId = data.ReadInt32(); + UIExtensionHostInfo hostInfo; + auto result = GetUIExtensionRootHostInfo(callerToken, hostInfo, userId); + if (!reply.WriteParcelable(&hostInfo)) { + HILOG_ERROR("Write host info failed."); + return ERR_INVALID_VALUE; + } + + if (!reply.WriteInt32(result)) { + HILOG_ERROR("Write result failed."); + return ERR_INVALID_VALUE; + } + + return NO_ERROR; +} } // namespace AAFwk } // namespace OHOS \ No newline at end of file diff --git a/services/abilitymgr/src/ability_record.cpp b/services/abilitymgr/src/ability_record.cpp index e6d262f998f697de4c20ab64b729bf749b54c10c..5beaa9f1b4d3bde79a9856b89f8919457de554b2 100644 --- a/services/abilitymgr/src/ability_record.cpp +++ b/services/abilitymgr/src/ability_record.cpp @@ -42,6 +42,7 @@ #include "hilog_wrapper.h" #include "os_account_manager_wrapper.h" #include "parameters.h" +#include "ui_extension_host_info.h" #include "scene_board_judgement.h" #include "system_ability_token_callback.h" #include "ui_extension_utils.h" @@ -2060,6 +2061,34 @@ void AbilityRecord::Dump(std::vector &info) } } +void AbilityRecord::DumpUIExtensionRootHostInfo(std::vector &info) const +{ + if (!UIExtensionUtils::IsUIExtension(GetAbilityInfo().extensionAbilityType)) { + // Dump host info only for uiextension. + return; + } + + sptr token = GetToken(); + if (token == nullptr) { + HILOG_ERROR("Get token failed."); + return; + } + + UIExtensionHostInfo hostInfo; + auto ret = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetUIExtensionRootHostInfo(token, hostInfo)); + if (ret != ERR_OK) { + HILOG_ERROR("Get ui extension host info failed with %{public}d.", ret); + return; + } + + std::string dumpInfo = " root host bundle name [" + hostInfo.elementName_.GetBundleName() + "]"; + info.emplace_back(dumpInfo); + dumpInfo = " root host module name [" + hostInfo.elementName_.GetModuleName() + "]"; + info.emplace_back(dumpInfo); + dumpInfo = " root host ability name [" + hostInfo.elementName_.GetAbilityName() + "]"; + info.emplace_back(dumpInfo); +} + void AbilityRecord::DumpAbilityState( std::vector &info, bool isClient, const std::vector ¶ms) { @@ -2161,6 +2190,7 @@ void AbilityRecord::DumpService(std::vector &info, std::vector token, + UIExtensionHostInfo &hostInfo) +{ + if (token == nullptr) { + HILOG_ERROR("Input param invalid."); + return ERR_INVALID_VALUE; + } + + auto abilityRecord = AAFwk::Token::GetAbilityRecordByToken(token); + if (abilityRecord == nullptr) { + HILOG_ERROR("Get ability record failed."); + return ERR_INVALID_VALUE; + } + + if (!AAFwk::UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) { + HILOG_WARN("Not ui extension ability."); + return ERR_INVALID_VALUE; + } + + auto extensionRecordId = abilityRecord->GetUIExtensionAbilityId(); + auto rootCallerToken = GetRootCallerTokenLocked(extensionRecordId); + auto callerAbilityRecord = AAFwk::Token::GetAbilityRecordByToken(rootCallerToken); + if (callerAbilityRecord == nullptr) { + HILOG_ERROR("Get caller ability record failed, id: %{public}d.", extensionRecordId); + return ERR_INVALID_VALUE; + } + + hostInfo.elementName_ = callerAbilityRecord->GetElementName(); + HILOG_DEBUG("Root host uri: %{public}s.", hostInfo.elementName_.GetURI().c_str()); + return ERR_OK; +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/services/abilitymgr/src/ui_extension_host_info.cpp b/services/abilitymgr/src/ui_extension_host_info.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8d8a7507ff32ee1fc8db04e405df100c24809638 --- /dev/null +++ b/services/abilitymgr/src/ui_extension_host_info.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 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 + * + * 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_host_info.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace AbilityRuntime { +bool UIExtensionHostInfo::ReadFromParcel(Parcel &parcel) +{ + std::unique_ptr abilityInfo(parcel.ReadParcelable()); + if (abilityInfo == nullptr) { + HILOG_ERROR("Read host info failed."); + return false; + } + + elementName_ = *abilityInfo; + return true; +} + +UIExtensionHostInfo *UIExtensionHostInfo::Unmarshalling(Parcel &parcel) +{ + UIExtensionHostInfo *hostInfo = new (std::nothrow) UIExtensionHostInfo(); + if (hostInfo == nullptr) { + HILOG_ERROR("Create host info failed."); + return nullptr; + } + + if (!hostInfo->ReadFromParcel(parcel)) { + delete hostInfo; + hostInfo = nullptr; + } + + return hostInfo; +} + +bool UIExtensionHostInfo::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteParcelable(&elementName_)) { + HILOG_ERROR("Write element name failed."); + return false; + } + + return true; +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index fc7b430dea0e860406879672fb1587ea70343a28..5cf4fdc9de5a8f4a95314320ff9fe6b01f653ef5 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -470,6 +470,7 @@ group("unittest") { "task_data_persistence_mgr_test:unittest", "task_handler_wrap_test:unittest", "trigger_Info_test:unittest", + "ui_extension:unittest", "ui_extension_ability_test:unittest", "ui_extension_context_test:unittest", "ui_extension_utils_test:unittest", diff --git a/test/unittest/ability_manager_proxy_test/ability_manager_proxy_test.cpp b/test/unittest/ability_manager_proxy_test/ability_manager_proxy_test.cpp index fe49477ebe89990f966ebf3b641bd3d6fe2bb946..352d87ebe9343178e146a38cf03694dd2c414cd1 100644 --- a/test/unittest/ability_manager_proxy_test/ability_manager_proxy_test.cpp +++ b/test/unittest/ability_manager_proxy_test/ability_manager_proxy_test.cpp @@ -2599,5 +2599,27 @@ HWTEST_F(AbilityManagerProxyTest, QueryAllAutoStartupApplications_0100, TestSize auto res = proxy_->QueryAllAutoStartupApplications(infoList); EXPECT_EQ(res, ERR_OK); } + +/** + * @tc.name: AbilityManagerProxy_GetUIExtensionRootHostInfo_0100 + * @tc.desc: GetUIExtensionRootHostInfo + * @tc.type: FUNC + * @tc.require: issueI92G6Z + */ +HWTEST_F(AbilityManagerProxyTest, AbilityManagerProxy_GetUIExtensionRootHostInfo_0100, TestSize.Level1) +{ + HILOG_INFO("begin"); + + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(mock_.GetRefPtr(), &AbilityManagerStubMock::InvokeSendRequest)); + + auto token = sptr::MakeSptr(); + UIExtensionHostInfo hostInfo; + proxy_->GetUIExtensionRootHostInfo(token, hostInfo, USER_ID); + EXPECT_EQ(static_cast(AbilityManagerInterfaceCode::GET_UI_EXTENSION_ROOT_HOST_INFO), mock_->code_); + + HILOG_INFO("end"); +} } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/ability_manager_stub_test/ability_manager_stub_impl_mock.h b/test/unittest/ability_manager_stub_test/ability_manager_stub_impl_mock.h index 4c99256b94ccb71eca313f3397bbdeb60e176674..6c1daa08a877f557fcc831ef3dab53bc409ec463 100644 --- a/test/unittest/ability_manager_stub_test/ability_manager_stub_impl_mock.h +++ b/test/unittest/ability_manager_stub_test/ability_manager_stub_impl_mock.h @@ -449,6 +449,8 @@ public: const InsightIntentExecuteParam ¶m)); MOCK_METHOD3(ExecuteInsightIntentDone, int32_t(const sptr &token, uint64_t intentId, const InsightIntentExecuteResult &result)); + MOCK_METHOD3(GetUIExtensionRootHostInfo, int32_t(const sptr token, UIExtensionHostInfo &hostInfo, + int32_t userId)); }; } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/ability_manager_stub_test/ability_manager_stub_test.cpp b/test/unittest/ability_manager_stub_test/ability_manager_stub_test.cpp index 8b4d17a4706e053736ce9eb557c77755eb54b160..1b93982ca1b5efbb9c02bba6434ad88ade2b87e1 100644 --- a/test/unittest/ability_manager_stub_test/ability_manager_stub_test.cpp +++ b/test/unittest/ability_manager_stub_test/ability_manager_stub_test.cpp @@ -2721,5 +2721,35 @@ HWTEST_F(AbilityManagerStubTest, QueryAllAutoStartupApplicationsInner_0100, Test auto result = stub_->QueryAllAutoStartupApplicationsInner(data, reply); EXPECT_EQ(result, NO_ERROR); } + +/** + * @tc.name: GetUIExtensionRootHostInfo_0100 + * @tc.desc: GetUIExtensionRootHostInfo + * @tc.type: FUNC + * @tc.require: issueI92G6Z + */ +HWTEST_F(AbilityManagerStubTest, GetUIExtensionRootHostInfo_0100, TestSize.Level1) +{ + HILOG_INFO("begin"); + + MessageParcel data; + bool writeRet = data.WriteInterfaceToken(AbilityManagerStubImplMock::GetDescriptor()); + auto token = sptr::MakeSptr(); + auto userId = USER_ID; + writeRet &= data.WriteBool(true); + writeRet &= data.WriteRemoteObject(token); + writeRet &= data.WriteInt32(userId); + EXPECT_EQ(writeRet, true); + + EXPECT_CALL(*stub_, GetUIExtensionRootHostInfo(_, _, _)).Times(1); + + MessageParcel reply; + MessageOption option; + auto ret = stub_->OnRemoteRequest( + static_cast(AbilityManagerInterfaceCode::GET_UI_EXTENSION_ROOT_HOST_INFO), data, reply, option); + EXPECT_EQ(ret, NO_ERROR); + + HILOG_INFO("end"); +} } // namespace AAFwk } // namespace OHOS diff --git a/test/unittest/ui_extension/BUILD.gn b/test/unittest/ui_extension/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9cde1cf36938fa73c33fe543abc1d8b845f36dbc --- /dev/null +++ b/test/unittest/ui_extension/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 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 +# +# 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. + +group("unittest") { + testonly = true + + deps = [ "ui_extension_get_host_info_test:unittest" ] +} diff --git a/test/unittest/ui_extension/ui_extension_get_host_info_test/BUILD.gn b/test/unittest/ui_extension/ui_extension_get_host_info_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0b0dab275357e8a4762a521c68b14687908b7e39 --- /dev/null +++ b/test/unittest/ui_extension/ui_extension_get_host_info_test/BUILD.gn @@ -0,0 +1,67 @@ +# Copyright (c) 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 +# +# 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. + +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_unittest("ui_extension_get_host_info_test") { + module_out_path = "ability_runtime/ui_extension" + + cflags_cc = [] + + include_dirs = [ + "${ability_runtime_innerkits_path}/app_manager/include/appmgr", + "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include", + "${ability_runtime_test_path}/mock/common/include", + ] + + sources = [ + "${ability_runtime_test_path}/mock/common/src/mock_native_token.cpp", + "ui_extension_get_host_info_test.cpp", + ] + + configs = [ "${ability_runtime_services_path}/abilitymgr:abilityms_config" ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${ability_runtime_native_path}/appkit:appkit_manager_helper", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_deps_wrapper", + "ability_runtime:ability_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:libappexecfwk_common", + "c_utils:utils", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + ] +} + +group("unittest") { + testonly = true + deps = [ ":ui_extension_get_host_info_test" ] +} diff --git a/test/unittest/ui_extension/ui_extension_get_host_info_test/ui_extension_get_host_info_test.cpp b/test/unittest/ui_extension/ui_extension_get_host_info_test/ui_extension_get_host_info_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f28350862a06b4ffb1b6d56524303ef035db8df9 --- /dev/null +++ b/test/unittest/ui_extension/ui_extension_get_host_info_test/ui_extension_get_host_info_test.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (c) 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 + * + * 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 + +#include "hilog_wrapper.h" +#include "ability_manager_client.h" +#include "mock_ability_token.h" +#include "mock_native_token.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AAFwk { +class UIExtensionGetHostInfoTest : public testing::Test { +public: + + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +}; + +void UIExtensionGetHostInfoTest::SetUpTestCase(void) +{} + +void UIExtensionGetHostInfoTest::TearDownTestCase(void) +{} + +void UIExtensionGetHostInfoTest::SetUp() +{} + +void UIExtensionGetHostInfoTest::TearDown() +{} + +/** + * @tc.name: GetUIExtensionRootHostInfo_0100 + * @tc.desc: get ui extension root host info without permision. + * @tc.type: FUNC + * @tc.require: issueI8ZRAG + */ +HWTEST_F(UIExtensionGetHostInfoTest, GetUIExtensionRootHostInfo_0100, TestSize.Level1) +{ + HILOG_INFO("begin."); + sptr token = sptr::MakeSptr(); + EXPECT_NE(token, nullptr); + UIExtensionHostInfo hostInfo; + auto ret = AAFwk::AbilityManagerClient::GetInstance()->GetUIExtensionRootHostInfo(token, hostInfo); + EXPECT_EQ(ret, ERR_PERMISSION_DENIED); + HILOG_INFO("end."); +} + +/** + * @tc.name: GetUIExtensionRootHostInfo_0200 + * @tc.desc: get ui extension root host info without invalid param. + * @tc.type: FUNC + * @tc.require: issueI8ZRAG + */ +HWTEST_F(UIExtensionGetHostInfoTest, GetUIExtensionRootHostInfo_0200, TestSize.Level1) +{ + HILOG_INFO("begin."); + UIExtensionHostInfo hostInfo; + auto ret = AAFwk::AbilityManagerClient::GetInstance()->GetUIExtensionRootHostInfo(nullptr, hostInfo); + EXPECT_EQ(ret, ERR_INVALID_VALUE); + HILOG_INFO("end."); +} + +/** + * @tc.name: GetUIExtensionRootHostInfo_0300 + * @tc.desc: basic function test of get ui extension root host info. + * @tc.type: FUNC + * @tc.require: issueI8ZRAG + */ +HWTEST_F(UIExtensionGetHostInfoTest, GetUIExtensionRootHostInfo_0300, TestSize.Level1) +{ + HILOG_INFO("begin."); + auto currentID = GetSelfTokenID(); + AppExecFwk::MockNativeToken::SetNativeToken(); + + sptr token = nullptr; + auto ret = AbilityManagerClient::GetInstance()->GetTopAbility(token); + EXPECT_EQ(ret, ERR_OK); + + UIExtensionHostInfo hostInfo; + ret = AAFwk::AbilityManagerClient::GetInstance()->GetUIExtensionRootHostInfo(token, hostInfo); + // cause top ability isn't a uiextension ability. + EXPECT_EQ(ret, ERR_INVALID_VALUE); + HILOG_INFO("Get host info uri: %{public}s", hostInfo.elementName_.GetURI().c_str()); + + SetSelfTokenID(currentID); + HILOG_INFO("end."); +} + +/** + * @tc.name: GetUIExtensionRootHostInfo_0400 + * @tc.desc: basic function test of get ui extension root host info. + * @tc.type: FUNC + * @tc.require: issueI8ZRAG + */ +HWTEST_F(UIExtensionGetHostInfoTest, GetUIExtensionRootHostInfo_0400, TestSize.Level1) +{ + HILOG_INFO("begin."); + auto currentID = GetSelfTokenID(); + AppExecFwk::MockNativeToken::SetNativeToken(); + + sptr token = sptr::MakeSptr(); + EXPECT_NE(token, nullptr); + + UIExtensionHostInfo hostInfo; + auto ret = AAFwk::AbilityManagerClient::GetInstance()->GetUIExtensionRootHostInfo(token, hostInfo); + EXPECT_EQ(ret, ERR_INVALID_VALUE); + HILOG_INFO("Get host info uri: %{public}s", hostInfo.elementName_.GetURI().c_str()); + + SetSelfTokenID(currentID); + HILOG_INFO("end."); +} +} // namespace AAFwk +} // namespace OHOS