From 0813821d371835457df410b207f55517f2c54319 Mon Sep 17 00:00:00 2001 From: y1585740638 Date: Tue, 20 Sep 2022 16:31:34 +0800 Subject: [PATCH 1/3] Signed-off-by: y1585740638 Change-Id: Id92d449c08d24b921b1258c60c313f359258444b --- services/privacymanager/test/BUILD.gn | 84 +++++++ .../sensitive_resource_manager_test.cpp | 205 ++++++++++++++++++ 2 files changed, 289 insertions(+) create mode 100644 services/privacymanager/test/BUILD.gn create mode 100644 services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp diff --git a/services/privacymanager/test/BUILD.gn b/services/privacymanager/test/BUILD.gn new file mode 100644 index 000000000..ad89c019a --- /dev/null +++ b/services/privacymanager/test/BUILD.gn @@ -0,0 +1,84 @@ +# Copyright (c) 2022 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("//base/security/access_token/access_token.gni") +import("//build/test.gni") + +ohos_unittest("libsensitive_resource_manager_test") { + subsystem_name = "security" + part_name = "access_token" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "//base/security/access_token/frameworks/privacy/include", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//base/security/access_token/interfaces/innerkits/privacy/include", + "//base/security/access_token/services/common/database/include", + "//base/global/resource_management/interfaces/inner_api/include", + "//base/security/access_token/services/privacymanager/include/sensitive", + "//base/security/access_token/frameworks/common/include", + "//base/security/access_token/interfaces/innerkits/token_setproc/include", + "//foundation/ability/ability_runtime/interfaces/kits/native/ability/ability_runtime", + "//foundation/arkui/napi", + "//foundation/multimedia/audio_framework/interfaces/inner_api/native/audiomanager/include", + "//foundation/window/window_manager/wm/include", + "//third_party/googletest/include", + ] + + sources = [ + "//base/security/access_token/services/privacymanager/src/sensitive/application_status_change_callback.cpp", + "//base/security/access_token/services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp", + "//base/security/access_token/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp", + "unittest/sensitive_resource_manager_test.cpp", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + if (dlp_permission_enable == true) { + cflags_cc += [ "-DSUPPORT_SANDBOX_APP" ] + } + + configs = [ "//base/security/access_token/config:coverage_flags" ] + + deps = [ + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", + "//base/security/access_token/services/privacymanager:privacymanager", + "//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", + "//foundation/ability/ability_runtime/services/abilitymgr:abilityms_target", + "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", + "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", + "//utils/native/base:utils", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:ability_context_native", + "ability_runtime:ability_manager", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "multimedia_audio_framework:audio_client", + "samgr:samgr_proxy", + "window_manager:libwm", + ] +} + +group("unittest") { + testonly = true + deps = [ ":libsensitive_resource_manager_test" ] +} diff --git a/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp new file mode 100644 index 000000000..3cf0685b6 --- /dev/null +++ b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2022 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 "ability_context_impl.h" +#include "accesstoken_kit.h" +#include "audio_system_manager.h" +#include "sensitive_resource_manager.h" +#include "token_setproc.h" +#include "window.h" +#include "window_scene.h" +#include "wm_common.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Rosen; +using namespace OHOS::Security::AccessToken; +using namespace OHOS::AudioStandard; + +class SensitiveResourceManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + static sptr CreateWindowScene(); + static sptr CreateAppFloatingWindow(WindowType type, Rect rect); + static Rect GetRectWithVpr(int32_t x, int32_t y, uint32_t w, uint32_t h); + + static inline float virtualPixelRatio_ = 1.0; + static inline std::shared_ptr abilityContext_ = nullptr; +}; + +void SensitiveResourceManagerTest::SetUpTestCase() +{ +} + +void SensitiveResourceManagerTest::TearDownTestCase() +{ +} + +void SensitiveResourceManagerTest::SetUp() +{ + SensitiveResourceManager::GetInstance().Init(); +} + +void SensitiveResourceManagerTest::TearDown() +{ +} + +sptr SensitiveResourceManagerTest::CreateWindowScene() +{ + sptr listener = nullptr; + abilityContext_ = std::make_shared(); + + sptr scene = new WindowScene(); + scene->Init(0, abilityContext_, listener); + return scene; +} + +sptr SensitiveResourceManagerTest::CreateAppFloatingWindow(WindowType type, Rect rect) +{ + sptr option = new WindowOption(); + option->SetWindowType(type); + option->SetWindowRect(rect); + + static int cnt = 0; + std::string winName = "FloatingWindowTest" + std::to_string(cnt++); + + return Window::Create(winName, option, abilityContext_); +} + +inline Rect SensitiveResourceManagerTest::GetRectWithVpr(int32_t x, int32_t y, uint32_t w, uint32_t h) +{ + auto vpr = virtualPixelRatio_; + return {x, y, static_cast(w * vpr), static_cast(h * vpr)}; +} + +static bool g_isShowing = false; +static void OnChangeCameraFloatWindow(bool isShowing) +{ + GTEST_LOG_(INFO) << " OnChangeCameraFloatWindow isShowing:" << isShowing; + g_isShowing = isShowing; +} + +static void ResetEnv() +{ + g_isShowing = false; +} + +/** + * @tc.name: RegisterCameraFloatWindowChangeCallbackTest_001 + * @tc.desc: Verify the RegisterCameraFloatWindowChangeCallback with vaild callback. + * @tc.type: FUNC + * @tc.require: issueI5RWX5 + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterCameraFloatWindowChangeCallbackTest_001, TestSize.Level1) +{ + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); + sptr scene = CreateWindowScene(); + ASSERT_NE(nullptr, scene); + + Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600); + sptr fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect); + ASSERT_NE(nullptr, fltWin); + + ResetEnv(); + + ASSERT_EQ(WMError::WM_OK, scene->GoForeground()); + ASSERT_EQ(WMError::WM_OK, fltWin->Show()); + + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, g_isShowing); + + ResetEnv(); + + ASSERT_EQ(WMError::WM_OK, fltWin->Hide()); + + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, g_isShowing); + + ResetEnv(); + + ASSERT_EQ(WMError::WM_OK, fltWin->Destroy()); + ASSERT_EQ(WMError::WM_OK, scene->GoDestroy()); + + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, g_isShowing); + + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); +} + +/** + * @tc.name: RegisterCameraFloatWindowChangeCallbackTest_002 + * @tc.desc: Verify the RegisterCameraFloatWindowChangeCallback abnormal branch callback is nullptr. + * @tc.type: FUNC + * @tc.require: issueI5RWX5 + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterCameraFloatWindowChangeCallbackTest_002, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(nullptr)); +} + +/** + * @tc.name: RegisterCameraFloatWindowChangeCallbackTest_003 + * @tc.desc: Verify the RegisterCameraFloatWindowChangeCallback abnormal branch callback is already registered. + * @tc.type: FUNC + * @tc.require: issueI5RWX5 + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterCameraFloatWindowChangeCallbackTest_003, TestSize.Level1) +{ + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); + + SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow); +} + +/** + * @tc.name: UnRegisterCameraFloatWindowChangeCallbackTest_001 + * @tc.desc: Verify the UnRegisterCameraFloatWindowChangeCallback with vaild callback. + * @tc.type: FUNC + * @tc.require: issueI5RWX5 + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterCameraFloatWindowChangeCallbackTest_001, TestSize.Level1) +{ + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); +} + +/** + * @tc.name: UnRegisterCameraFloatWindowChangeCallbackTest_002 + * @tc.desc: Verify the UnRegisterCameraFloatWindowChangeCallback abnormal branch callback is nullptr. + * @tc.type: FUNC + * @tc.require: issueI5RWX5 + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterCameraFloatWindowChangeCallbackTest_002, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(nullptr)); +} + +/** + * @tc.name: UnRegisterCameraFloatWindowChangeCallbackTest_003 + * @tc.desc: Verify the UnRegisterCameraFloatWindowChangeCallback abnormal branch callback is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWX5 + */ +HWTEST_F(SensitiveResourceManagerTest, UnRegisterCameraFloatWindowChangeCallbackTest_003, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); +} \ No newline at end of file -- Gitee From 31907569cf20b7e798c6a2fe320409be0a2f8ed2 Mon Sep 17 00:00:00 2001 From: y1585740638 Date: Tue, 20 Sep 2022 16:35:48 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=9B=B8=E6=9C=BA=E6=82=AC=E6=B5=AE?= =?UTF-8?q?=E7=AA=97=E7=9B=91=E5=90=AC=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: y1585740638 Change-Id: Id1df43b04d0f377cf81c0ded0e656dece50b430d --- BUILD.gn | 1 + services/privacymanager/test/BUILD.gn | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/BUILD.gn b/BUILD.gn index 9e318da81..f423d80e3 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -28,6 +28,7 @@ group("accesstoken_build_module_test") { "//base/security/access_token/interfaces/innerkits/token_setproc/test:unittest", "//base/security/access_token/services/accesstokenmanager/test:unittest", "//base/security/access_token/services/common/database/test:unittest", + "//base/security/access_token/services/privacymanager/test:unittest", ] } if (token_sync_enable == true) { diff --git a/services/privacymanager/test/BUILD.gn b/services/privacymanager/test/BUILD.gn index ad89c019a..48e215dcb 100644 --- a/services/privacymanager/test/BUILD.gn +++ b/services/privacymanager/test/BUILD.gn @@ -34,6 +34,7 @@ ohos_unittest("libsensitive_resource_manager_test") { "//foundation/multimedia/audio_framework/interfaces/inner_api/native/audiomanager/include", "//foundation/window/window_manager/wm/include", "//third_party/googletest/include", + "//commonlibrary/c_utils/base/include", ] sources = [ @@ -59,10 +60,10 @@ ohos_unittest("libsensitive_resource_manager_test") { "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", "//third_party/googletest:gtest_main", "//third_party/libuv:uv", - "//utils/native/base:utils", ] external_deps = [ + "ability_base:base", "ability_base:want", "ability_runtime:ability_context_native", "ability_runtime:ability_manager", @@ -70,6 +71,7 @@ ohos_unittest("libsensitive_resource_manager_test") { "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", + "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_audio_framework:audio_client", -- Gitee From 73cdd8e0da1ed95fa0e80ac243064a3b079a81f0 Mon Sep 17 00:00:00 2001 From: y1585740638 Date: Wed, 21 Sep 2022 10:00:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E5=8F=8A=E9=BA=A6=E5=85=8B=E9=A3=8E=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: y1585740638 Change-Id: Ice41b6877361cf758892cb7994377272725a3604 --- services/privacymanager/BUILD.gn | 1 + .../mic_global_switch_change_callback.h | 41 +++ .../sensitive/sensitive_resource_manager.h | 14 +- .../mic_global_switch_change_callback.cpp | 48 ++++ .../sensitive/sensitive_resource_manager.cpp | 100 ++++++++ services/privacymanager/test/BUILD.gn | 2 +- .../sensitive_resource_manager_test.cpp | 234 ++++++++++++------ 7 files changed, 356 insertions(+), 84 deletions(-) create mode 100644 services/privacymanager/include/sensitive/mic_global_switch_change_callback.h create mode 100644 services/privacymanager/src/sensitive/mic_global_switch_change_callback.cpp diff --git a/services/privacymanager/BUILD.gn b/services/privacymanager/BUILD.gn index 4f5321129..5061892c0 100644 --- a/services/privacymanager/BUILD.gn +++ b/services/privacymanager/BUILD.gn @@ -67,6 +67,7 @@ if (is_standard_system && ability_base_enable == true) { "src/record/permission_record_repository.cpp", "src/record/permission_used_record_cache.cpp", "src/sensitive/application_status_change_callback.cpp", + "src/sensitive/mic_global_switch_change_callback.cpp", "src/sensitive/sensitive_resource_manager.cpp", "src/service/privacy_manager_service.cpp", "src/service/privacy_manager_stub.cpp", diff --git a/services/privacymanager/include/sensitive/mic_global_switch_change_callback.h b/services/privacymanager/include/sensitive/mic_global_switch_change_callback.h new file mode 100644 index 000000000..bc34e01b0 --- /dev/null +++ b/services/privacymanager/include/sensitive/mic_global_switch_change_callback.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022 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 GLOBAL_SWITCH_CHANGE_CALLBACK_H +#define GLOBAL_SWITCH_CHANGE_CALLBACK_H + +#include "audio_routing_manager.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +typedef void (*OnMicGlobalSwitchChangeCallback)(bool switchStatus); +class MicGlobalSwitchChangeCallback : public AudioStandard::AudioManagerMicStateChangeCallback { +public: + MicGlobalSwitchChangeCallback() = default; + virtual ~MicGlobalSwitchChangeCallback() {} + void OnMicStateUpdated(const AudioStandard::MicStateChangeEvent &micStateChangeEvent) override; + + void SetCallback(OnMicGlobalSwitchChangeCallback callback); + OnMicGlobalSwitchChangeCallback GetCallback() const; + +private: + OnMicGlobalSwitchChangeCallback callback_ = nullptr; +}; + +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // GLOBAL_SWITCH_CHANGE_CALLBACK_H \ No newline at end of file diff --git a/services/privacymanager/include/sensitive/sensitive_resource_manager.h b/services/privacymanager/include/sensitive/sensitive_resource_manager.h index 6dc2c8d5a..8afd08fce 100644 --- a/services/privacymanager/include/sensitive/sensitive_resource_manager.h +++ b/services/privacymanager/include/sensitive/sensitive_resource_manager.h @@ -21,6 +21,7 @@ #include #include "app_mgr_proxy.h" #include "application_status_change_callback.h" +#include "mic_global_switch_change_callback.h" #include "safe_map.h" namespace OHOS { @@ -49,11 +50,18 @@ public: bool GetAppStatus(const std::string& pkgName, int32_t& status); bool GetGlobalSwitch(const ResourceType type); void SetGlobalSwitch(const ResourceType type, bool switchStatus); + void SetFlowWindowStatus(bool isshow); + bool IsFlowWindowShow(); + + void ShowDialog(const ResourceType& type); // register and unregister app status change callback bool RegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback); bool UnRegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback); - + + // mic global switch + bool RegisterMicGlobalSwitchChangeCallback(OnMicGlobalSwitchChangeCallback callback); + bool UnRegisterMicGlobalSwitchChangeCallback(OnMicGlobalSwitchChangeCallback callback); private: bool InitProxy(); OHOS::sptr GetAppManagerProxy(); @@ -62,6 +70,10 @@ private: std::mutex appStatusMutex_; std::vector> appStateCallbacks_; SafeMap switchStatusMap_; + std::mutex floatwindowStatusMutex_; + bool floatwindowStatus_ = false; + std::mutex MicGlobalSwitchMutex_; + std::vector> MicGlobalSwitchCallbacks_; std::mutex mutex_; sptr appMgrProxy_; }; diff --git a/services/privacymanager/src/sensitive/mic_global_switch_change_callback.cpp b/services/privacymanager/src/sensitive/mic_global_switch_change_callback.cpp new file mode 100644 index 000000000..4496a5606 --- /dev/null +++ b/services/privacymanager/src/sensitive/mic_global_switch_change_callback.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 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 "mic_global_switch_change_callback.h" +#include "accesstoken_log.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "GlobalSwitchChangeCallback" +}; +} + +void MicGlobalSwitchChangeCallback::OnMicStateUpdated(const AudioStandard::MicStateChangeEvent &micStateChangeEvent) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(MicGlobalStatus=%{public}d)", micStateChangeEvent.mute); + + if (callback_ != nullptr) { + callback_(micStateChangeEvent.mute); + } +} + +void MicGlobalSwitchChangeCallback::SetCallback(OnMicGlobalSwitchChangeCallback callback) +{ + callback_ = callback; +} + +OnMicGlobalSwitchChangeCallback MicGlobalSwitchChangeCallback::GetCallback() const +{ + return callback_; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp b/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp index 46aea1b19..ba6c3e2c2 100644 --- a/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp +++ b/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp @@ -33,6 +33,13 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { static const size_t MAX_CALLBACK_SIZE = 200; } +using namespace OHOS::Rosen; +using namespace OHOS::AudioStandard; + +static const std::string PERMISSION_MANAGER_BUNDLE_NAME = "com.ohos.permissionmanager"; +static const std::string PERMISSION_MANAGER_DIALOG_ABILITY = "com.ohos.permissionmanager.GlobalExtAbility"; +static const std::string RESOURCE_KEY = "ohos.sensitive.resource"; + SensitiveResourceManager& SensitiveResourceManager::GetInstance() { static SensitiveResourceManager instance; @@ -130,6 +137,46 @@ void SensitiveResourceManager::SetGlobalSwitch(const ResourceType type, bool swi switchStatusMap_[type] = switchStatus; } +void SensitiveResourceManager::SetFlowWindowStatus(bool isshow) +{ + std::lock_guard lock(floatwindowStatusMutex_); + floatwindowStatus_ = isshow; +} + +bool SensitiveResourceManager::IsFlowWindowShow() +{ + std::lock_guard lock(floatwindowStatusMutex_); + return floatwindowStatus_; +} + +void SensitiveResourceManager::ShowDialog(const ResourceType& type) +{ + AAFwk::Want want; + want.SetElementName(PERMISSION_MANAGER_BUNDLE_NAME, PERMISSION_MANAGER_DIALOG_ABILITY); + std::string typeStr = ""; + switch (type) { + case ResourceType::CAMERA: + typeStr = "camera"; + break; + case ResourceType::MICROPHONE: + typeStr = "microphone"; + break; + default: + break; + } + if (typeStr == "") { + ACCESSTOKEN_LOG_ERROR(LABEL, "type is invalid, type:%{public}d", type); + return; + } + want.SetParam(RESOURCE_KEY, typeStr); + ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, nullptr); + if (err != ERR_OK) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Fail to StartAbility, err:%{public}d", err); + return; + } + return; +} + bool SensitiveResourceManager::RegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback) { if (callback == nullptr) { @@ -208,6 +255,59 @@ bool SensitiveResourceManager::UnRegisterAppStatusChangeCallback(uint32_t tokenI return true; } + +bool SensitiveResourceManager::RegisterMicGlobalSwitchChangeCallback(OnMicGlobalSwitchChangeCallback callback) +{ + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback could not be null."); + return false; + } + + std::lock_guard lock(MicGlobalSwitchMutex_); + auto iter = std::find_if(MicGlobalSwitchCallbacks_.begin(), MicGlobalSwitchCallbacks_.end(), + [callback](const std::shared_ptr& rec) { + return callback == rec->GetCallback(); + }); + if (iter != MicGlobalSwitchCallbacks_.end()) { + ACCESSTOKEN_LOG_WARN(LABEL, "callback is already registered"); + return true; + } + + std::shared_ptr listener(new MicGlobalSwitchChangeCallback()); + if (listener == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "new fail."); + return false; + } + listener->SetCallback(callback); + MicGlobalSwitchCallbacks_.emplace_back(listener); + + AudioStandard::AudioRoutingManager::GetInstance()->SetMicStateChangeCallback(listener); + ACCESSTOKEN_LOG_INFO(LABEL, "register Miceophone callback(%{public}p).", callback); + + return true; +} + +bool SensitiveResourceManager::UnRegisterMicGlobalSwitchChangeCallback(OnMicGlobalSwitchChangeCallback callback) +{ + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback could not be null."); + return false; + } + + std::lock_guard lock(MicGlobalSwitchMutex_); + auto iter = std::find_if(MicGlobalSwitchCallbacks_.begin(), MicGlobalSwitchCallbacks_.end(), + [callback](const std::shared_ptr& rec) { + return callback == rec->GetCallback(); + }); + if (iter == MicGlobalSwitchCallbacks_.end()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback is not found."); + return false; + } + + MicGlobalSwitchCallbacks_.erase(iter); + ACCESSTOKEN_LOG_INFO(LABEL, "unregister callback(%{public}p).", callback); + return true; +} } // namespace AccessToken } // namespace Security } // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/test/BUILD.gn b/services/privacymanager/test/BUILD.gn index 48e215dcb..5c4877cbd 100644 --- a/services/privacymanager/test/BUILD.gn +++ b/services/privacymanager/test/BUILD.gn @@ -39,7 +39,7 @@ ohos_unittest("libsensitive_resource_manager_test") { sources = [ "//base/security/access_token/services/privacymanager/src/sensitive/application_status_change_callback.cpp", - "//base/security/access_token/services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp", + "//base/security/access_token/services/privacymanager/src/sensitive/mic_global_switch_change_callback.cpp", "//base/security/access_token/services/privacymanager/src/sensitive/sensitive_resource_manager.cpp", "unittest/sensitive_resource_manager_test.cpp", ] diff --git a/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp index 3cf0685b6..9a20bde10 100644 --- a/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp +++ b/services/privacymanager/test/unittest/sensitive_resource_manager_test.cpp @@ -37,13 +37,6 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); - - static sptr CreateWindowScene(); - static sptr CreateAppFloatingWindow(WindowType type, Rect rect); - static Rect GetRectWithVpr(int32_t x, int32_t y, uint32_t w, uint32_t h); - - static inline float virtualPixelRatio_ = 1.0; - static inline std::shared_ptr abilityContext_ = nullptr; }; void SensitiveResourceManagerTest::SetUpTestCase() @@ -63,143 +56,220 @@ void SensitiveResourceManagerTest::TearDown() { } -sptr SensitiveResourceManagerTest::CreateWindowScene() +static bool mic_isMute = true; +static void OnChangeMicGlobalSwitch(bool isMute) { - sptr listener = nullptr; - abilityContext_ = std::make_shared(); - - sptr scene = new WindowScene(); - scene->Init(0, abilityContext_, listener); - return scene; + GTEST_LOG_(INFO) << " OnChangeMicGlobalSwitch mic_isMute_before_set: " << mic_isMute; + mic_isMute = isMute; + GTEST_LOG_(INFO) << " OnChangeMicGlobalSwitch mic_isMute_after_set: " << mic_isMute; } -sptr SensitiveResourceManagerTest::CreateAppFloatingWindow(WindowType type, Rect rect) +static void ResetEnv() { - sptr option = new WindowOption(); - option->SetWindowType(type); - option->SetWindowRect(rect); + mic_isMute = true; +} - static int cnt = 0; - std::string winName = "FloatingWindowTest" + std::to_string(cnt++); +/** + * @tc.name: GetGlobalSwitchTest_001 + * @tc.desc: Verify the GetGlobalSwitch with vaild ResourceType. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, GetGlobalSwitchTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::MICROPHONE, true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); - return Window::Create(winName, option, abilityContext_); + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::MICROPHONE, false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); } -inline Rect SensitiveResourceManagerTest::GetRectWithVpr(int32_t x, int32_t y, uint32_t w, uint32_t h) +/** + * @tc.name: GetGlobalSwitchTest_002 + * @tc.desc: Verify the GetGlobalSwitch abnormal branch ResourceType is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, GetGlobalSwitchTest_002, TestSize.Level1) { - auto vpr = virtualPixelRatio_; - return {x, y, static_cast(w * vpr), static_cast(h * vpr)}; + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::INVALID)); } -static bool g_isShowing = false; -static void OnChangeCameraFloatWindow(bool isShowing) +/** + * @tc.name: SetGlobalSwitchTest_001 + * @tc.desc: Verify the SetGlobalSwitch with vaild ResourceType. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, SetGlobalSwitchTest_001, TestSize.Level1) { - GTEST_LOG_(INFO) << " OnChangeCameraFloatWindow isShowing:" << isShowing; - g_isShowing = isShowing; + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::CAMERA, true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::CAMERA)); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::CAMERA, false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::CAMERA)); } -static void ResetEnv() +/** + * @tc.name: SetGlobalSwitchTest_002 + * @tc.desc: Verify the SetGlobalSwitch abnormal branch ResourceType is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWXA issueI5RWXF + */ +HWTEST_F(SensitiveResourceManagerTest, SetGlobalSwitchTest_002, TestSize.Level1) { - g_isShowing = false; + bool isMicrophoneMute = AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute(); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::INVALID, true); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(isMicrophoneMute, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); + + SensitiveResourceManager::GetInstance().SetGlobalSwitch(ResourceType::INVALID, false); + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(isMicrophoneMute, SensitiveResourceManager::GetInstance().GetGlobalSwitch(ResourceType::MICROPHONE)); } /** - * @tc.name: RegisterCameraFloatWindowChangeCallbackTest_001 - * @tc.desc: Verify the RegisterCameraFloatWindowChangeCallback with vaild callback. + * @tc.name: FlowWindowStatusTest_001 + * @tc.desc: Verify the SetFlowWindowStatus IsFlowWindowShow. * @tc.type: FUNC * @tc.require: issueI5RWX5 */ -HWTEST_F(SensitiveResourceManagerTest, RegisterCameraFloatWindowChangeCallbackTest_001, TestSize.Level1) +HWTEST_F(SensitiveResourceManagerTest, FlowWindowStatusTest_001, TestSize.Level1) { - ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); - sptr scene = CreateWindowScene(); - ASSERT_NE(nullptr, scene); + SensitiveResourceManager::GetInstance().SetFlowWindowStatus(true); - Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600); - sptr fltWin = CreateAppFloatingWindow(WindowType::WINDOW_TYPE_FLOAT_CAMERA, fltWindRect); - ASSERT_NE(nullptr, fltWin); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().IsFlowWindowShow()); - ResetEnv(); + SensitiveResourceManager::GetInstance().SetFlowWindowStatus(false); - ASSERT_EQ(WMError::WM_OK, scene->GoForeground()); - ASSERT_EQ(WMError::WM_OK, fltWin->Show()); + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().IsFlowWindowShow()); +} +/** + * @tc.name: RegisterMicGlobalSwitchChangeCallbackTest_001 + * @tc.desc: call RegisterMicGlobalSwitchChangeCallback once. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterMicGlobalSwitchChangeCallbackTest_001, TestSize.Level1) +{ + SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); + + bool isMute = AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute(); + + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(true); usleep(500000); // 500000us = 0.5s - ASSERT_EQ(true, g_isShowing); + ASSERT_EQ(true, mic_isMute); ResetEnv(); - ASSERT_EQ(WMError::WM_OK, fltWin->Hide()); - + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(false); usleep(500000); // 500000us = 0.5s - ASSERT_EQ(false, g_isShowing); - + ASSERT_EQ(false, mic_isMute); + ResetEnv(); + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(isMute); + SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); +} - ASSERT_EQ(WMError::WM_OK, fltWin->Destroy()); - ASSERT_EQ(WMError::WM_OK, scene->GoDestroy()); - usleep(500000); // 500000us = 0.5s - ASSERT_EQ(false, g_isShowing); +/** + * @tc.name: RegisterMicGlobalSwitchChangeCallbackTest_002 + * @tc.desc: Verify the RegisterMicGlobalSwitchChangeCallback abnormal branch callback is invalid. + * @tc.type: FUNC + * @tc.require: issueI5RWX8 + */ +HWTEST_F(SensitiveResourceManagerTest, RegisterMicGlobalSwitchChangeCallbackTest_002, TestSize.Level1) +{ + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(nullptr)); + + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); - ASSERT_EQ(true, SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); + SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); } /** - * @tc.name: RegisterCameraFloatWindowChangeCallbackTest_002 - * @tc.desc: Verify the RegisterCameraFloatWindowChangeCallback abnormal branch callback is nullptr. + * @tc.name: UnRegisterMicGlobalSwitchChangeCallbackTest_001 + * @tc.desc: Verify the UnRegisterMicGlobalSwitchChangeCallback with vaild callback. * @tc.type: FUNC - * @tc.require: issueI5RWX5 + * @tc.require: issueI5RWX8 */ -HWTEST_F(SensitiveResourceManagerTest, RegisterCameraFloatWindowChangeCallbackTest_002, TestSize.Level1) +HWTEST_F(SensitiveResourceManagerTest, UnRegisterMicGlobalSwitchChangeCallbackTest_001, TestSize.Level1) { - ASSERT_EQ(false, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(nullptr)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); + ASSERT_EQ(true, SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); } /** - * @tc.name: RegisterCameraFloatWindowChangeCallbackTest_003 - * @tc.desc: Verify the RegisterCameraFloatWindowChangeCallback abnormal branch callback is already registered. + * @tc.name: UnRegisterMicGlobalSwitchChangeCallbackTest_002 + * @tc.desc: Verify the UnRegisterMicGlobalSwitchChangeCallback abnormal branch callback is invalid. * @tc.type: FUNC - * @tc.require: issueI5RWX5 + * @tc.require: issueI5RWX8 */ -HWTEST_F(SensitiveResourceManagerTest, RegisterCameraFloatWindowChangeCallbackTest_003, TestSize.Level1) +HWTEST_F(SensitiveResourceManagerTest, UnRegisterMicGlobalSwitchChangeCallbackTest_002, TestSize.Level1) { - ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); - ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); - - SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow); -} + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(nullptr)); + ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch)); +} /** - * @tc.name: UnRegisterCameraFloatWindowChangeCallbackTest_001 - * @tc.desc: Verify the UnRegisterCameraFloatWindowChangeCallback with vaild callback. + * @tc.name: ShowDialogTest + * @tc.desc: call ShowDialog once. * @tc.type: FUNC - * @tc.require: issueI5RWX5 + * @tc.require: issueI5RWXF issueI5RWX8 */ -HWTEST_F(SensitiveResourceManagerTest, UnRegisterCameraFloatWindowChangeCallbackTest_001, TestSize.Level1) +HWTEST_F(SensitiveResourceManagerTest, ShowDialogTest, TestSize.Level1) { - ASSERT_EQ(true, SensitiveResourceManager::GetInstance().RegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); - ASSERT_EQ(true, SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); + SensitiveResourceManager::GetInstance().RegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); + + bool isMute = AudioStandard::AudioSystemManager::GetInstance()->IsMicrophoneMute(); + GTEST_LOG_(INFO) << " OnChangeMicGlobalSwitch isMute: " << isMute; + + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(true); + usleep(5000000); // 5000000us = 5s + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::MICROPHONE); + usleep(5000000); // 5000000us = 5s + ASSERT_EQ(false, mic_isMute); + + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(true); + usleep(5000000); // 5000000us = 5s + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::MICROPHONE); + usleep(5000000); // 5000000us = 5s + ASSERT_EQ(true, mic_isMute); + + GTEST_LOG_(INFO) << " OnChangeMicGlobalSwitch isMute: " << isMute; + AudioStandard::AudioSystemManager::GetInstance()->SetMicrophoneMute(isMute); + + ResetEnv(); + SensitiveResourceManager::GetInstance().UnRegisterMicGlobalSwitchChangeCallback(OnChangeMicGlobalSwitch); } /** - * @tc.name: UnRegisterCameraFloatWindowChangeCallbackTest_002 - * @tc.desc: Verify the UnRegisterCameraFloatWindowChangeCallback abnormal branch callback is nullptr. + * @tc.name: ShowDialogTest_001 + * @tc.desc: call ShowDialog once with valid ResourceType. * @tc.type: FUNC - * @tc.require: issueI5RWX5 + * @tc.require: issueI5RWXF issueI5RWX8 */ -HWTEST_F(SensitiveResourceManagerTest, UnRegisterCameraFloatWindowChangeCallbackTest_002, TestSize.Level1) +HWTEST_F(SensitiveResourceManagerTest, ShowDialogTest_001, TestSize.Level1) { - ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(nullptr)); + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::MICROPHONE); + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::CAMERA); } + /** - * @tc.name: UnRegisterCameraFloatWindowChangeCallbackTest_003 - * @tc.desc: Verify the UnRegisterCameraFloatWindowChangeCallback abnormal branch callback is invalid. + * @tc.name: ShowDialogTest_002 + * @tc.desc: call ShowDialog once with invalid ResourceType. * @tc.type: FUNC - * @tc.require: issueI5RWX5 + * @tc.require: issueI5RWXF issueI5RWX8 */ -HWTEST_F(SensitiveResourceManagerTest, UnRegisterCameraFloatWindowChangeCallbackTest_003, TestSize.Level1) +HWTEST_F(SensitiveResourceManagerTest, ShowDialogTest_002, TestSize.Level1) { - ASSERT_EQ(false, SensitiveResourceManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow)); + SensitiveResourceManager::GetInstance().ShowDialog(ResourceType::INVALID); } \ No newline at end of file -- Gitee