diff --git a/services/appmgr/include/modal_system_app_freeze_uiextension.h b/services/appmgr/include/modal_system_app_freeze_uiextension.h index 5bdacfa7ffebff0c9c950ee0893139feb6f78288..9c4c4528fa72c0c49f8f6795f094daa831918043 100644 --- a/services/appmgr/include/modal_system_app_freeze_uiextension.h +++ b/services/appmgr/include/modal_system_app_freeze_uiextension.h @@ -34,7 +34,7 @@ namespace OHOS { namespace AppExecFwk { -constexpr const char* APP_NO_RESPONSE_ABILITY = "AppAbnormalAbility"; +constexpr const char *APP_NO_RESPONSE_ABILITY = "AppAbnormalAbility"; class ModalSystemAppFreezeUIExtension { public: @@ -47,35 +47,15 @@ public: private: bool CreateModalUIExtension(std::string pid, std::string bundleName); - AAFwk::Want CreateSystemDialogWant(std::string pid, std::string bundleName); + bool CreateSystemDialogWant(std::string pid, std::string bundleName, sptr &token, AAFwk::Want &want); private: - class AppFreezeDialogConnection : public AAFwk::AbilityConnectionStub { - public: - AppFreezeDialogConnection() = default; - virtual ~AppFreezeDialogConnection() = default; - - void SetReqeustAppFreezeDialogWant(const AAFwk::Want &want); - void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr &remoteObject, - int resultCode) override; - void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; - - private: - bool WriteWantElement(MessageParcel &data); - AAFwk::Want want_; - }; - -private: - sptr GetConnection(); - bool lastFocusStatus = false; uint64_t lastFreezeTime = 0; - sptr dialogConnectionCallback_; std::mutex appFreezeResultMutex_; - std::mutex dialogConnectionMutex_; std::string lastFreezePid; }; -} // namespace AppExecFwk -} // namespace OHOS -#endif // APP_NO_RESPONSE_DIALOG -#endif // OHOS_ABILITY_RUNTIME_MODAL_SYSTEM_APP_FREEZE_UIEXTENSION_H \ No newline at end of file +} // namespace AppExecFwk +} // namespace OHOS +#endif // APP_NO_RESPONSE_DIALOG +#endif // OHOS_ABILITY_RUNTIME_MODAL_SYSTEM_APP_FREEZE_UIEXTENSION_H \ No newline at end of file diff --git a/services/appmgr/src/modal_system_app_freeze_uiextension.cpp b/services/appmgr/src/modal_system_app_freeze_uiextension.cpp index 4e28abf4b8054f7d940f50078e2a93d031c53460..69471d8e6c12361fbff3c41cbc8f091583e45156 100644 --- a/services/appmgr/src/modal_system_app_freeze_uiextension.cpp +++ b/services/appmgr/src/modal_system_app_freeze_uiextension.cpp @@ -19,17 +19,28 @@ #include #include +#include "ability_record.h" #include "hilog_tag_wrapper.h" #include "hitrace_meter.h" #include "in_process_call_wrapper.h" #include "scene_board_judgement.h" +#include "session_manager_lite.h" +#include "window_visibility_info.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::Rosen; namespace OHOS { namespace AppExecFwk { const std::string UIEXTENSION_TYPE_KEY = "ability.want.params.uiExtensionType"; const std::string UIEXTENSION_SYS_COMMON_UI = "sysDialog/common"; const std::string APP_FREEZE_PID = "APP_FREEZE_PID"; +const std::string FREEZE_WINDOW_POSX = "FREEZE_WINDOW_POSX"; +const std::string FREEZE_WINDOW_POSY = "FREEZE_WINDOW_POSY"; +const std::string FREEZE_WINDOW_WIDTH = "FREEZE_WINDOW_WIDTH"; +const std::string FREEZE_WINDOW_HEIGHT = "FREEZE_WINDOW_HEIGHT"; const std::string START_BUNDLE_NAME = "startBundleName"; +const std::string APP_FREEZE_TOKEN = "freezeToken"; constexpr int32_t INVALID_USERID = -1; constexpr int32_t MESSAGE_PARCEL_KEY_SIZE = 3; constexpr uint32_t COMMAND_START_DIALOG = 1; @@ -43,21 +54,7 @@ ModalSystemAppFreezeUIExtension &ModalSystemAppFreezeUIExtension::GetInstance() } ModalSystemAppFreezeUIExtension::~ModalSystemAppFreezeUIExtension() -{ - dialogConnectionCallback_ = nullptr; -} - -sptr ModalSystemAppFreezeUIExtension::GetConnection() -{ - if (dialogConnectionCallback_ == nullptr) { - std::lock_guard lock(dialogConnectionMutex_); - if (dialogConnectionCallback_ == nullptr) { - dialogConnectionCallback_ = new (std::nothrow) AppFreezeDialogConnection(); - } - } - - return dialogConnectionCallback_; -} +{} void ModalSystemAppFreezeUIExtension::ProcessAppFreeze(bool focusFlag, const FaultData &faultData, std::string pid, std::string bundleName, std::function callback, bool isDialogExist) @@ -70,13 +67,14 @@ void ModalSystemAppFreezeUIExtension::ProcessAppFreeze(bool focusFlag, const Fau FaultDataType faultType = faultData.faultType; std::string name = faultData.errorObject.name; bool isAppFreezeDialog = name == AppFreezeType::THREAD_BLOCK_6S || name == AppFreezeType::APP_INPUT_BLOCK || - name == AppFreezeType::BUSSINESS_THREAD_BLOCK_6S; - uint64_t now = std::chrono::duration_cast(std::chrono::steady_clock:: - now().time_since_epoch()).count(); + name == AppFreezeType::BUSSINESS_THREAD_BLOCK_6S; + uint64_t now = + std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()) + .count(); bool timeout = now - lastFreezeTime > TIMEOUT_INTERVAL_MS; TAG_LOGI(AAFwkTag::ABILITYMGR, - "%{public}s is %{public}s.pid:%{public}s lastFreezePid:%{public}s,timeout %{public}lu", bundleName.c_str(), - focusFlag ? "focus" : "not focus", pid.c_str(), lastFreezePid.c_str(), now - lastFreezeTime); + "%{public}s is %{public}s.pid:%{public}s lastFreezePid:%{public}s", bundleName.c_str(), + focusFlag ? "focus" : "not focus", pid.c_str(), lastFreezePid.c_str()); bool isPullUpBox = isAppFreezeDialog && (pid != lastFreezePid || (pid == lastFreezePid && timeout && !isDialogExist)); bool updateTypeName = name == AppFreezeType::THREAD_BLOCK_6S || name == AppFreezeType::BUSSINESS_THREAD_BLOCK_6S; @@ -84,12 +82,7 @@ void ModalSystemAppFreezeUIExtension::ProcessAppFreeze(bool focusFlag, const Fau lastFreezeTime = now; } if (focusFlag && isPullUpBox) { - std::string appNoResponseBundleName = APP_NO_RESPONSE_BUNDLENAME; - if (appNoResponseBundleName == "com.ohos.taskmanager") { - callback(); - } else { - CreateModalUIExtension(pid, bundleName); - } + CreateModalUIExtension(pid, bundleName); } else if (callback && (faultType != FaultDataType::APP_FREEZE || !isAppFreezeDialog)) { callback(); } @@ -101,127 +94,84 @@ void ModalSystemAppFreezeUIExtension::ProcessAppFreeze(bool focusFlag, const Fau bool ModalSystemAppFreezeUIExtension::CreateModalUIExtension(std::string pid, std::string bundleName) { - TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); - AAFwk::Want want = CreateSystemDialogWant(pid, bundleName); + TAG_LOGD(AAFwkTag::ABILITYMGR, "Create Modal UIExtension Called"); + sptr token; + AAFwk::Want want; std::unique_lock lockAssertResult(appFreezeResultMutex_); - auto callback = GetConnection(); - if (callback == nullptr) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "null callback"); + if (!CreateSystemDialogWant(pid, bundleName, token, want)) { return false; } - callback->SetReqeustAppFreezeDialogWant(want); auto abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance(); if (abilityManagerClient == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityManagerClient"); return false; } - AAFwk::Want systemUIWant; - if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) { - systemUIWant.SetElementName("com.ohos.sceneboard", "com.ohos.sceneboard.systemdialog"); - } else { - systemUIWant.SetElementName("com.ohos.systemui", "com.ohos.systemui.dialog"); - } - IN_PROCESS_CALL_WITHOUT_RET(abilityManagerClient->DisconnectAbility(callback)); - auto result = IN_PROCESS_CALL(abilityManagerClient->ConnectAbility(systemUIWant, callback, INVALID_USERID)); + auto result = IN_PROCESS_CALL(abilityManagerClient->StartExtensionAbility(want, token)); if (result != ERR_OK) { - TAG_LOGE(AAFwkTag::ABILITYMGR, - "fail, result = %{public}d", result); + TAG_LOGE(AAFwkTag::ABILITYMGR, "StartExtensionAbility failed, result = %{public}d", result); return false; } lastFreezePid = pid; lastFocusStatus = true; lastFreezeTime = std::chrono::duration_cast( std::chrono::steady_clock::now().time_since_epoch()).count(); - TAG_LOGI(AAFwkTag::ABILITYMGR, - "success, result = %{public}d", result); + TAG_LOGI(AAFwkTag::ABILITYMGR, "success, result = %{public}d", result); return true; } -AAFwk::Want ModalSystemAppFreezeUIExtension::CreateSystemDialogWant(std::string pid, std::string bundleName) +bool ModalSystemAppFreezeUIExtension::CreateSystemDialogWant( + std::string pid, std::string bundleName, sptr &token, AAFwk::Want &want) { - AAFwk::Want want; want.SetElementName(APP_NO_RESPONSE_BUNDLENAME, APP_NO_RESPONSE_ABILITY); want.SetParam(UIEXTENSION_TYPE_KEY, UIEXTENSION_SYS_COMMON_UI); want.SetParam(APP_FREEZE_PID, pid); want.SetParam(START_BUNDLE_NAME, bundleName); - return want; -} -void ModalSystemAppFreezeUIExtension::AppFreezeDialogConnection::SetReqeustAppFreezeDialogWant(const AAFwk::Want &want) -{ - want_ = want; -} - -bool ModalSystemAppFreezeUIExtension::AppFreezeDialogConnection::WriteWantElement(MessageParcel &data) -{ - if (!data.WriteString16(u"bundleName")) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "write bundleName failed"); + auto sceneSessionManager = Rosen::SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy(); + if (!sceneSessionManager) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "sceneSessionManager is null proxy!"); return false; } - if (!data.WriteString16(Str8ToStr16(want_.GetElement().GetBundleName()))) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "write element bundlename failed"); + auto ret = static_cast(sceneSessionManager->GetFocusSessionToken(token)); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "Get focus session token err: %{public}d", ret); return false; } - if (!data.WriteString16(u"abilityName")) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "write abilityName failed"); + want.SetParam(APP_FREEZE_TOKEN, token); + + int32_t posX = 0; + int32_t posY = 0; + int32_t width = 10; + int32_t height = 10; + std::vector> infos; + ret = static_cast(sceneSessionManager->GetVisibilityWindowInfo(infos)); + if (ret != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "Get visibility window info err: %{public}d", ret); return false; } - if (!data.WriteString16(Str8ToStr16(want_.GetElement().GetAbilityName()))) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "write element abilityName failed"); - return false; + bool infoReady = false; + for (const auto &info : infos) { + if (info->IsFocused()) { + posX = info->rect_.posX_; + posY = info->rect_.posY_; + width = info->rect_.width_; + height = info->rect_.height_; + infoReady = true; + break; + } } - return true; -} - -void ModalSystemAppFreezeUIExtension::AppFreezeDialogConnection::OnAbilityConnectDone( - const AppExecFwk::ElementName &element, const sptr &remote, int resultCode) -{ - TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); - if (remote == nullptr) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "null remote"); - return; + if (!infoReady) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "No fucused window!"); + return false; } + want.SetParam(FREEZE_WINDOW_POSX, std::to_string(posX)); + want.SetParam(FREEZE_WINDOW_POSY, std::to_string(posY)); + want.SetParam(FREEZE_WINDOW_WIDTH, std::to_string(width)); + want.SetParam(FREEZE_WINDOW_HEIGHT, std::to_string(height)); - MessageParcel data; - MessageParcel reply; - MessageOption option; - if (!data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE)) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "write MESSAGE_PARCEL_KEY_SIZE failed"); - return; - } - if (!WriteWantElement(data)) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "write element failed"); - return; - } - if (!data.WriteString16(u"parameters")) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "write parameters failed"); - return; - } - nlohmann::json param; - param[UIEXTENSION_TYPE_KEY.c_str()] = want_.GetStringParam(UIEXTENSION_TYPE_KEY); - param[APP_FREEZE_PID.c_str()] = want_.GetStringParam(APP_FREEZE_PID); - param[START_BUNDLE_NAME.c_str()] = want_.GetStringParam(START_BUNDLE_NAME); - std::string paramStr = param.dump(); - if (!data.WriteString16(Str8ToStr16(paramStr))) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "write paramStr failed"); - return; - } - uint32_t code = !Rosen::SceneBoardJudgement::IsSceneBoardEnabled() ? - COMMAND_START_DIALOG : - AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE; - TAG_LOGI(AAFwkTag::ABILITYMGR, "show dialog"); - auto ret = remote->SendRequest(code, data, reply, option); - if (ret != ERR_OK) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "show dialog fail"); - return; - } + return true; } -void ModalSystemAppFreezeUIExtension::AppFreezeDialogConnection::OnAbilityDisconnectDone( - const AppExecFwk::ElementName &element, int resultCode) -{ - TAG_LOGI(AAFwkTag::ABILITYMGR, "call"); -} -} // namespace AppExecFwk -} // namespace OHOS -#endif // APP_NO_RESPONSE_DIALOG +} // namespace AppExecFwk +} // namespace OHOS +#endif // APP_NO_RESPONSE_DIALOG diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index c6f52ad36b0c711b864d6a2889d0a5714f770b15..f555224ee3d215a85018eaf86efa477e5fa19ebf 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -393,6 +393,7 @@ group("unittest") { "mission_listener_stub_second_test:unittest", "mission_listener_stub_test:unittest", "mission_listener_test:unittest", + "modal_system_app_freeze_uiextension_test:unittest", "modal_system_dialog_util_test:unittest", "multi_app_utils_test:unittest", "multi_instance_utils_second_test", diff --git a/test/unittest/modal_system_app_freeze_uiextension_test/BUILD.gn b/test/unittest/modal_system_app_freeze_uiextension_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..38b8475d321c44fc38ee55b00f05c62a488470ff --- /dev/null +++ b/test/unittest/modal_system_app_freeze_uiextension_test/BUILD.gn @@ -0,0 +1,104 @@ +# 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/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_unittest("modal_system_app_freeze_uiextension_test") { + module_out_path = "ability_runtime/ability_runtime/ability_call_test" + + defines = [ + "APP_NO_RESPONSE_DIALOG=1", + "APP_NO_RESPONSE_BUNDLENAME=\"com.huawei.hmos.abnormal\"", + ] + + include_dirs = [ + "${ability_runtime_test_path}/mock/common/include", + "${ability_runtime_test_path}/mock/mock_appmgr_service/include", + "${ability_runtime_test_path}/mock/mock_sa_call", + "${ability_runtime_test_path}/mock/services_appmgr_test/include", + "${ability_runtime_path}/services/abilitymgr/include", + ] + + sources = [ + "${ability_runtime_services_path}/appmgr/src/modal_system_app_freeze_uiextension.cpp", + "modal_system_app_freeze_uiextension_test.cpp", + ] + + configs = [ "${ability_runtime_services_path}/appmgr:appmgr_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/ability_manager:mission_info", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/abilitymgr:abilityms", + "${ability_runtime_services_path}/common:perm_verification", + "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/aakit:aakit_mock", + "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/appexecfwk_core:appexecfwk_appmgr_mock", + "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/appexecfwk_core:appexecfwk_bundlemgr_mock", + "${ability_runtime_native_path}/appkit:appkit_native", + "${ability_runtime_path}/utils/server/startup:startup_util", + "${ability_runtime_services_path}/appmgr:libappms", + "${ability_runtime_services_path}/common:app_util", + "${ability_runtime_services_path}/common:res_sched_util", + "${ability_runtime_services_path}/common:task_handler_wrap", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "init:libbeget_proxy", + "ipc:ipc_core", + "napi:ace_napi", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "window_manager:libwsutils", + "window_manager:scene_session", + "window_manager:session_manager_lite", + ] + + if (ability_command_for_test) { + defines += [ "ABILITY_COMMAND_FOR_TEST" ] + } + + if (ability_runtime_graphics) { + external_deps += [ + "window_manager:libwm", + "window_manager:libwsutils", + ] + } + if (ability_runtime_upms) { + deps += [ + "${ability_runtime_innerkits_path}/uri_permission:uri_permission_mgr", + ] + } +} + +group("unittest") { + testonly = true + deps = [ ":modal_system_app_freeze_uiextension_test" ] +} diff --git a/test/unittest/modal_system_app_freeze_uiextension_test/modal_system_app_freeze_uiextension_test.cpp b/test/unittest/modal_system_app_freeze_uiextension_test/modal_system_app_freeze_uiextension_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6c3a23449ec7ce50548cc27305d40abf72908ae --- /dev/null +++ b/test/unittest/modal_system_app_freeze_uiextension_test/modal_system_app_freeze_uiextension_test.cpp @@ -0,0 +1,92 @@ +/* + * 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 +#include +#include + +#include "ability_manager_service.h" +#include "ability_record.h" +#include "wm_common.h" +#define private public +#define protected public +#include "modal_system_app_freeze_uiextension.h" +#undef private +#undef protected + +using namespace OHOS::AAFwk; +using namespace OHOS::Rosen; + +namespace OHOS { +namespace AppExecFwk { +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::AAFwk; + +class ModalSystemAppFreezeUiextensionTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void ModalSystemAppFreezeUiextensionTest::SetUpTestCase(void) +{} + +void ModalSystemAppFreezeUiextensionTest::TearDownTestCase(void) +{} + +void ModalSystemAppFreezeUiextensionTest::SetUp(void) +{} + +void ModalSystemAppFreezeUiextensionTest::TearDown(void) +{} + +/** + * @tc.number: CreateModalUIExtension_001 + * @tc.name: DumpFfrt + * @tc.desc: Test whether CreateModalUIExtension is called normally. + * @tc.type: FUNC + * @tc.require: SR000GH1HL + */ +HWTEST_F(ModalSystemAppFreezeUiextensionTest, CreateModalUIExtension_001, TestSize.Level1) +{ + std::string pid = "1"; + std::string bundleName = "Test"; + bool ret = ModalSystemAppFreezeUIExtension::GetInstance().CreateModalUIExtension(pid, bundleName); + EXPECT_NE(ret, true); +} + +/** + * @tc.number: CreateSystemDialogWant_001 + * @tc.name: DumpFfrt + * @tc.desc: Test whether CreateSystemDialogWant is called normally. + * @tc.type: FUNC + * @tc.require: SR000GH1HL + */ +HWTEST_F(ModalSystemAppFreezeUiextensionTest, CreateSystemDialogWant_001, TestSize.Level1) +{ + sptr token; + AAFwk::Want want; + std::string pid = "1"; + std::string bundleName = "Test"; + bool ret = ModalSystemAppFreezeUIExtension::GetInstance().CreateSystemDialogWant(pid, bundleName, token, want); + EXPECT_NE(ret, true); +} + +} +}