From 94f5134ebcb69c95038f48284863baad1ce66c2a Mon Sep 17 00:00:00 2001 From: nzy Date: Sat, 21 Dec 2024 15:55:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E5=8C=96=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: nzy --- interfaces/innerkits/wm/wm_common.h | 1 + .../include/scene_session_manager.h | 1 + .../include/scene_session_manager_lite.h | 1 + .../scene_session_manager_lite_interface.h | 15 +++++++ .../zidl/scene_session_manager_lite_proxy.h | 1 + .../zidl/scene_session_manager_lite_stub.h | 1 + .../src/scene_session_manager.cpp | 29 +++++++++++++ .../src/scene_session_manager_lite.cpp | 5 +++ .../zidl/scene_session_manager_lite_proxy.cpp | 43 +++++++++++++++++++ .../zidl/scene_session_manager_lite_stub.cpp | 27 ++++++++++++ .../scene_session_manager_lite_stub_test.cpp | 25 +++++++++++ 11 files changed, 149 insertions(+) diff --git a/interfaces/innerkits/wm/wm_common.h b/interfaces/innerkits/wm/wm_common.h index bd61957bd3..06e0a8a5e2 100644 --- a/interfaces/innerkits/wm/wm_common.h +++ b/interfaces/innerkits/wm/wm_common.h @@ -306,6 +306,7 @@ inline SystemBarSettingFlag operator|(SystemBarSettingFlag lhs, SystemBarSetting enum class ControlAppType : uint8_t { CONTROL_APP_TYPE_BEGIN = 0, APP_LOCK = 1, + PARENT_CONTROL, CONTROL_APP_TYPE_END, }; diff --git a/window_scene/session_manager/include/scene_session_manager.h b/window_scene/session_manager/include/scene_session_manager.h index 2765c18c61..6aecaf8ca6 100644 --- a/window_scene/session_manager/include/scene_session_manager.h +++ b/window_scene/session_manager/include/scene_session_manager.h @@ -519,6 +519,7 @@ public: WSError NotifyAppUseControlList( ControlAppType type, int32_t userId, const std::vector& controlList); void RegisterNotifyAppUseControlListCallback(NotifyAppUseControlListFunc&& func); + WMError MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId); sptr RequestSceneSession(const SessionInfo& sessionInfo, sptr property = nullptr); void UpdateSceneSessionWant(const SessionInfo& sessionInfo); diff --git a/window_scene/session_manager/include/scene_session_manager_lite.h b/window_scene/session_manager/include/scene_session_manager_lite.h index 616ef275d5..56ef08ea49 100644 --- a/window_scene/session_manager/include/scene_session_manager_lite.h +++ b/window_scene/session_manager/include/scene_session_manager_lite.h @@ -79,6 +79,7 @@ public: WMError GetAccessibilityWindowInfo(std::vector>& infos) override; WSError NotifyAppUseControlList(ControlAppType type, int32_t userId, const std::vector& controlList) override; + WMError MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) override; }; } // namespace OHOS::Rosen diff --git a/window_scene/session_manager/include/zidl/scene_session_manager_lite_interface.h b/window_scene/session_manager/include/zidl/scene_session_manager_lite_interface.h index 494b0e5952..b1f9b0a080 100644 --- a/window_scene/session_manager/include/zidl/scene_session_manager_lite_interface.h +++ b/window_scene/session_manager/include/zidl/scene_session_manager_lite_interface.h @@ -83,6 +83,7 @@ public: TRANS_ID_GET_ROOT_MAIN_WINDOW_ID, TRANS_ID_UI_EXTENSION_CREATION_CHECK, TRANS_ID_NOTIFY_APP_USE_CONTROL_LIST, + TRANS_ID_MINIMIZE_MAIN_SESSION, }; virtual WSError SetSessionLabel(const sptr& token, const std::string& label) = 0; @@ -143,6 +144,20 @@ public: * @param windowStates Window state list */ virtual WSError GetMainWindowStatesByPid(int32_t pid, std::vector& windowStates) = 0; + + /** + * @brief Minimize All Main Sessions of An Application. + * + * This function is used to minimize the main sessions of the application with the same bundleName and appIndex. + * The invoker must be an SA or SystemApp and have the related permission. + * + * @param bundleName bundle name of the application that need to be minimized. + * @param appIndex appIndex of the target application + * @param userId User ID + * @return Successful call returns WMError: WS-OK, otherwise it indicates failure + * @permission application requires SA permission or SystemApp permission + */ + virtual WMError MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) = 0; }; } // namespace OHOS::Rosen #endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_LITE_INTERFACE_H diff --git a/window_scene/session_manager/include/zidl/scene_session_manager_lite_proxy.h b/window_scene/session_manager/include/zidl/scene_session_manager_lite_proxy.h index d29b90d53c..1f6fec739c 100644 --- a/window_scene/session_manager/include/zidl/scene_session_manager_lite_proxy.h +++ b/window_scene/session_manager/include/zidl/scene_session_manager_lite_proxy.h @@ -89,6 +89,7 @@ public: WMError GetAccessibilityWindowInfo(std::vector>& infos) override; WSError NotifyAppUseControlList(ControlAppType type, int32_t userId, const std::vector& controlList) override; + WMError MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) override; private: template diff --git a/window_scene/session_manager/include/zidl/scene_session_manager_lite_stub.h b/window_scene/session_manager/include/zidl/scene_session_manager_lite_stub.h index 7f4ab7637e..9b60d6fc5e 100644 --- a/window_scene/session_manager/include/zidl/scene_session_manager_lite_stub.h +++ b/window_scene/session_manager/include/zidl/scene_session_manager_lite_stub.h @@ -76,6 +76,7 @@ private: int HandleGetRootMainWindowId(MessageParcel& data, MessageParcel& reply); int HandleGetAccessibilityWindowInfo(MessageParcel& data, MessageParcel& reply); int HandleNotifyAppUseControlList(MessageParcel& data, MessageParcel& reply); + int HandleMinimizeMainSession(MessageParcel& data, MessageParcel& reply); int ProcessRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option); }; diff --git a/window_scene/session_manager/src/scene_session_manager.cpp b/window_scene/session_manager/src/scene_session_manager.cpp index e4db62b494..026064ad8a 100644 --- a/window_scene/session_manager/src/scene_session_manager.cpp +++ b/window_scene/session_manager/src/scene_session_manager.cpp @@ -12148,4 +12148,33 @@ void SceneSessionManager::SetIsWindowRectAutoSave(const std::string& key, bool e } } } + +WMError SceneSessionManager::MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) +{ + if (!SessionPermission::IsSystemAppCall() && !SessionPermission::IsSACalling()) { + TLOGE(WmsLogTag::WMS_LIFE, "The caller is neither a system app nor an SA."); + return WMError::WM_ERROR_INVALID_PERMISSION; + } + if ((currentUserId_ != userId && currentUserId_ != DEFAULT_USERID) || + (currentUserId_ == DEFAULT_USERID && userId != GetUserIdByUid(getuid()))) { + TLOGW(WmsLogTag::WMS_LIFE, "currentUserId_:%{public}d userId:%{public}d GetUserIdByUid:%{public}d", + currentUserId_.load(), userId, GetUserIdByUid(getuid())); + return WMError::WM_ERROR_INVALID_OPERATION; + } + const char* const where = __func__; + taskScheduler_->PostAsyncTask([this, bundleName, appIndex, where] { + std::vector> mainSessions; + GetMainSessionByBundleNameAndAppIndex(bundleName, appIndex, mainSessions); + if (mainSessions.empty()) { + TLOGNW(WmsLogTag::WMS_LIFE, "%{public}s, not found main session", where); + return; + } + for (const auto& session : mainSessions) { + session->OnSessionEvent(SessionEvent::EVENT_MINIMIZE); + TLOGNI(WmsLogTag::WMS_LIFE, "%{public}s, id:%{public}d.", where, session->GetPersistentId()); + } + mainSessions.clear(); + }, __func__); + return WMError::WM_OK; +} } // namespace OHOS::Rosen \ No newline at end of file diff --git a/window_scene/session_manager/src/scene_session_manager_lite.cpp b/window_scene/session_manager/src/scene_session_manager_lite.cpp index 32e58a0340..47bcefefbe 100644 --- a/window_scene/session_manager/src/scene_session_manager_lite.cpp +++ b/window_scene/session_manager/src/scene_session_manager_lite.cpp @@ -284,4 +284,9 @@ WSError SceneSessionManagerLite::NotifyAppUseControlList( { return SceneSessionManager::GetInstance().NotifyAppUseControlList(type, userId, controlList); } + +WMError SceneSessionManagerLite::MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) +{ + return SceneSessionManager::GetInstance().MinimizeMainSession(bundleName, appIndex, userId); +} } // namespace OHOS::Rosen diff --git a/window_scene/session_manager/src/zidl/scene_session_manager_lite_proxy.cpp b/window_scene/session_manager/src/zidl/scene_session_manager_lite_proxy.cpp index 6cb640a21d..5f508cf54a 100644 --- a/window_scene/session_manager/src/zidl/scene_session_manager_lite_proxy.cpp +++ b/window_scene/session_manager/src/zidl/scene_session_manager_lite_proxy.cpp @@ -1430,4 +1430,47 @@ WSError SceneSessionManagerLiteProxy::NotifyAppUseControlList( } return static_cast(reply.ReadInt32()); } + + +WMError SceneSessionManagerLiteProxy::MinimizeMainSession( + const std::string& bundleName, int32_t appIndex, int32_t userId) +{ + TLOGD(WmsLogTag::WMS_LIFE, "in"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + TLOGE(WmsLogTag::WMS_LIFE, "Write interfaceToken failed"); + return WMError::WM_ERROR_IPC_FAILED; + } + if (!data.WriteString(bundleName)) { + TLOGE(WmsLogTag::WMS_LIFE, "write bundleName failed."); + return WMError::WM_ERROR_IPC_FAILED; + } + if (!data.WriteInt32(appIndex)) { + TLOGE(WmsLogTag::WMS_LIFE, "write appIndex failed."); + return WMError::WM_ERROR_IPC_FAILED; + } + if (!data.WriteInt32(userId)) { + TLOGE(WmsLogTag::WMS_LIFE, "write userId failed."); + return WMError::WM_ERROR_IPC_FAILED; + } + + sptr remote = Remote(); + if (remote == nullptr) { + TLOGE(WmsLogTag::WMS_LIFE, "remote is null"); + return WMError::WM_ERROR_IPC_FAILED; + } + if (remote->SendRequest(static_cast(SceneSessionManagerLiteMessage::TRANS_ID_MINIMIZE_MAIN_SESSION), + data, reply, option) != ERR_NONE) { + TLOGE(WmsLogTag::WMS_LIFE, "SendRequest failed"); + return WMError::WM_ERROR_IPC_FAILED; + } + uint32_t ret = 0; + if (!reply.ReadUint32(ret)) { + TLOGE(WmsLogTag::WMS_LIFE, "Read ret failed"); + return WMError::WM_ERROR_IPC_FAILED; + } + return static_cast(ret); +} } // namespace OHOS::Rosen diff --git a/window_scene/session_manager/src/zidl/scene_session_manager_lite_stub.cpp b/window_scene/session_manager/src/zidl/scene_session_manager_lite_stub.cpp index d3f0ea5845..688848032c 100644 --- a/window_scene/session_manager/src/zidl/scene_session_manager_lite_stub.cpp +++ b/window_scene/session_manager/src/zidl/scene_session_manager_lite_stub.cpp @@ -129,6 +129,8 @@ int SceneSessionManagerLiteStub::ProcessRemoteRequest(uint32_t code, MessageParc return HandleGetAccessibilityWindowInfo(data, reply); case static_cast(SceneSessionManagerLiteMessage::TRANS_ID_NOTIFY_APP_USE_CONTROL_LIST): return HandleNotifyAppUseControlList(data, reply); + case static_cast(SceneSessionManagerLiteMessage::TRANS_ID_MINIMIZE_MAIN_SESSION): + return HandleMinimizeMainSession(data, reply); default: WLOGFE("Failed to find function handler!"); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -885,4 +887,29 @@ int SceneSessionManagerLiteStub::HandleNotifyAppUseControlList(MessageParcel& da reply.WriteInt32(static_cast(ret)); return ERR_NONE; } + +int SceneSessionManagerLiteStub::HandleMinimizeMainSession(MessageParcel& data, MessageParcel& reply) +{ + TLOGD(WmsLogTag::WMS_LIFE, "in"); + std::string bundleName; + if (!data.ReadString(bundleName)) { + TLOGE(WmsLogTag::WMS_LIFE, "Failed to read bundleName"); + return ERR_INVALID_DATA; + } + + int32_t appIndex = 0; + if (!data.ReadInt32(appIndex)) { + TLOGE(WmsLogTag::WMS_LIFE, "Failed to read appIndex"); + return ERR_INVALID_DATA; + } + + int32_t userId = 0; + if (!data.ReadInt32(userId)) { + TLOGE(WmsLogTag::WMS_LIFE, "Failed to read userId"); + return ERR_INVALID_DATA; + } + WMError ret = MinimizeMainSession(bundleName, appIndex, userId); + reply.WriteInt32(static_cast(ret)); + return ERR_NONE; +} } // namespace OHOS::Rosen diff --git a/window_scene/test/unittest/scene_session_manager_lite_stub_test.cpp b/window_scene/test/unittest/scene_session_manager_lite_stub_test.cpp index a06412916f..43e5658a0d 100644 --- a/window_scene/test/unittest/scene_session_manager_lite_stub_test.cpp +++ b/window_scene/test/unittest/scene_session_manager_lite_stub_test.cpp @@ -205,6 +205,10 @@ class MockSceneSessionManagerLiteStub : public SceneSessionManagerLiteStub { } WSError NotifyAppUseControlList(ControlAppType type, int32_t userId, const std::vector& controlList) override { return WSError::WS_OK; } + WMError MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) override + { + return WMError::WM_OK; + } }; class SceneSessionManagerLiteStubTest : public testing::Test { @@ -962,6 +966,27 @@ HWTEST_F(SceneSessionManagerLiteStubTest, HandleNotifyAppUseControlList, Functio SceneSessionManagerLiteStub::HandleNotifyAppUseControlList(data, reply); EXPECT_EQ(ERR_NONE, res); } + +/** + * @tc.name: HandleMinimizeMainSession + * @tc.desc: test function : HandleMinimizeMainSession + * @tc.type: FUNC + */ +HWTEST_F(SceneSessionManagerLiteStubTest, HandleMinimizeMainSession, Function | SmallTest | Level1) +{ + MessageParcel data; + MessageParcel reply; + std::string bundleName = "appbundleName"; + int32_t appIndex = 1; + int32_t userId = 1; + + data.WriteString(bundleName); + data.WriteInt32(appIndex); + data.WriteInt32(userId); + + auto res = sceneSessionManagerLiteStub_->SceneSessionManagerLiteStub::HandleMinimizeMainSession(data, reply); + EXPECT_EQ(ERR_NONE, res); +} } } } \ No newline at end of file -- Gitee From 5285349905f93b0ec79351ef547440819ce0bbfd Mon Sep 17 00:00:00 2001 From: nzy Date: Mon, 23 Dec 2024 09:05:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: nzy --- .../session_manager/src/scene_session_manager.cpp | 12 ++++++------ .../src/zidl/scene_session_manager_lite_proxy.cpp | 6 ++---- .../src/zidl/scene_session_manager_lite_stub.cpp | 2 -- .../scene_session_manager_lite_stub_test.cpp | 6 ++---- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/window_scene/session_manager/src/scene_session_manager.cpp b/window_scene/session_manager/src/scene_session_manager.cpp index 026064ad8a..fa6cb95819 100644 --- a/window_scene/session_manager/src/scene_session_manager.cpp +++ b/window_scene/session_manager/src/scene_session_manager.cpp @@ -8365,7 +8365,8 @@ WSError SceneSessionManager::NotifyAppUseControlList( TLOGW(WmsLogTag::WMS_LIFE, "The caller is not system-app, can not use system-api"); return WSError::WS_ERROR_INVALID_PERMISSION; } - if (!SessionPermission::VerifyCallingPermission(PermissionConstants::PERMISSION_WRITE_APP_LOCK)) { + if (type == ControlAppType::APP_LOCK && + !SessionPermission::VerifyCallingPermission(PermissionConstants::PERMISSION_WRITE_APP_LOCK)) { TLOGW(WmsLogTag::WMS_LIFE, "write app lock permission denied"); return WSError::WS_ERROR_INVALID_PERMISSION; } @@ -12157,23 +12158,22 @@ WMError SceneSessionManager::MinimizeMainSession(const std::string& bundleName, } if ((currentUserId_ != userId && currentUserId_ != DEFAULT_USERID) || (currentUserId_ == DEFAULT_USERID && userId != GetUserIdByUid(getuid()))) { - TLOGW(WmsLogTag::WMS_LIFE, "currentUserId_:%{public}d userId:%{public}d GetUserIdByUid:%{public}d", - currentUserId_.load(), userId, GetUserIdByUid(getuid())); - return WMError::WM_ERROR_INVALID_OPERATION; + TLOGW(WmsLogTag::WMS_LIFE, "currentUserId:%{public}d userId:%{public}d GetUserIdByUid:%{public}d", + currentUserId_.load(), userId, GetUserIdByUid(getuid())); + return WMError::WM_ERROR_INVALID_OPERATION; } const char* const where = __func__; taskScheduler_->PostAsyncTask([this, bundleName, appIndex, where] { std::vector> mainSessions; GetMainSessionByBundleNameAndAppIndex(bundleName, appIndex, mainSessions); if (mainSessions.empty()) { - TLOGNW(WmsLogTag::WMS_LIFE, "%{public}s, not found main session", where); + TLOGNW(WmsLogTag::WMS_LIFE, "%{public}s, not found any main session", where); return; } for (const auto& session : mainSessions) { session->OnSessionEvent(SessionEvent::EVENT_MINIMIZE); TLOGNI(WmsLogTag::WMS_LIFE, "%{public}s, id:%{public}d.", where, session->GetPersistentId()); } - mainSessions.clear(); }, __func__); return WMError::WM_OK; } diff --git a/window_scene/session_manager/src/zidl/scene_session_manager_lite_proxy.cpp b/window_scene/session_manager/src/zidl/scene_session_manager_lite_proxy.cpp index 5f508cf54a..55fc1574c4 100644 --- a/window_scene/session_manager/src/zidl/scene_session_manager_lite_proxy.cpp +++ b/window_scene/session_manager/src/zidl/scene_session_manager_lite_proxy.cpp @@ -1431,7 +1431,6 @@ WSError SceneSessionManagerLiteProxy::NotifyAppUseControlList( return static_cast(reply.ReadInt32()); } - WMError SceneSessionManagerLiteProxy::MinimizeMainSession( const std::string& bundleName, int32_t appIndex, int32_t userId) { @@ -1455,7 +1454,6 @@ WMError SceneSessionManagerLiteProxy::MinimizeMainSession( TLOGE(WmsLogTag::WMS_LIFE, "write userId failed."); return WMError::WM_ERROR_IPC_FAILED; } - sptr remote = Remote(); if (remote == nullptr) { TLOGE(WmsLogTag::WMS_LIFE, "remote is null"); @@ -1466,8 +1464,8 @@ WMError SceneSessionManagerLiteProxy::MinimizeMainSession( TLOGE(WmsLogTag::WMS_LIFE, "SendRequest failed"); return WMError::WM_ERROR_IPC_FAILED; } - uint32_t ret = 0; - if (!reply.ReadUint32(ret)) { + int32_t ret = 0; + if (!reply.ReadInt32(ret)) { TLOGE(WmsLogTag::WMS_LIFE, "Read ret failed"); return WMError::WM_ERROR_IPC_FAILED; } diff --git a/window_scene/session_manager/src/zidl/scene_session_manager_lite_stub.cpp b/window_scene/session_manager/src/zidl/scene_session_manager_lite_stub.cpp index 688848032c..9686ea57d1 100644 --- a/window_scene/session_manager/src/zidl/scene_session_manager_lite_stub.cpp +++ b/window_scene/session_manager/src/zidl/scene_session_manager_lite_stub.cpp @@ -896,13 +896,11 @@ int SceneSessionManagerLiteStub::HandleMinimizeMainSession(MessageParcel& data, TLOGE(WmsLogTag::WMS_LIFE, "Failed to read bundleName"); return ERR_INVALID_DATA; } - int32_t appIndex = 0; if (!data.ReadInt32(appIndex)) { TLOGE(WmsLogTag::WMS_LIFE, "Failed to read appIndex"); return ERR_INVALID_DATA; } - int32_t userId = 0; if (!data.ReadInt32(userId)) { TLOGE(WmsLogTag::WMS_LIFE, "Failed to read userId"); diff --git a/window_scene/test/unittest/scene_session_manager_lite_stub_test.cpp b/window_scene/test/unittest/scene_session_manager_lite_stub_test.cpp index 43e5658a0d..97474eeb66 100644 --- a/window_scene/test/unittest/scene_session_manager_lite_stub_test.cpp +++ b/window_scene/test/unittest/scene_session_manager_lite_stub_test.cpp @@ -205,10 +205,8 @@ class MockSceneSessionManagerLiteStub : public SceneSessionManagerLiteStub { } WSError NotifyAppUseControlList(ControlAppType type, int32_t userId, const std::vector& controlList) override { return WSError::WS_OK; } - WMError MinimizeMainSession(const std::string& bundleName, int32_t appIndex, int32_t userId) override - { - return WMError::WM_OK; - } + WMError MinimizeMainSession(const std::string& bundleName, + int32_t appIndex, int32_t userId) override { return WMError::WM_OK; } }; class SceneSessionManagerLiteStubTest : public testing::Test { -- Gitee