From bbbcbce1db9a97aef90dc7896c690ea5a5236131 Mon Sep 17 00:00:00 2001 From: chennian Date: Wed, 20 Jul 2022 22:41:50 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E9=9C=80=E6=B1=82=20Signed-off-by:chennian?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chennian --- BUILD.gn | 1 + frameworks/privacy/BUILD.gn | 2 - services/privacymanager/BUILD.gn | 8 +- services/privacymanager/dialog_ui/BUILD.gn | 27 +++++ .../privacymanager/dialog_ui/common/.gitkeep | 0 .../privacymanager/dialog_ui/i18n/en-US.json | 7 ++ .../privacymanager/dialog_ui/i18n/zh-CN.json | 7 ++ .../dialog_ui/pages/index/index.css | 86 ++++++++++++++ .../dialog_ui/pages/index/index.hml | 43 +++++++ .../dialog_ui/pages/index/index.js | 38 ++++++ .../sensitive/camera_float_window_listener.h | 34 ++++++ .../include/sensitive/global_switch_manager.h | 50 ++++++++ .../camera_float_window_listener.cpp | 36 ++++++ .../src/sensitive/global_switch_manager.cpp | 112 ++++++++++++++++++ services/privacymanager/test/BUILD.gn | 20 ++++ .../BUILD.gn | 39 ++++++ .../camera_float_window_listener_test.cpp | 46 +++++++ .../camera_float_window_listener_test.h | 37 ++++++ 18 files changed, 590 insertions(+), 3 deletions(-) create mode 100644 services/privacymanager/dialog_ui/BUILD.gn create mode 100644 services/privacymanager/dialog_ui/common/.gitkeep create mode 100644 services/privacymanager/dialog_ui/i18n/en-US.json create mode 100644 services/privacymanager/dialog_ui/i18n/zh-CN.json create mode 100644 services/privacymanager/dialog_ui/pages/index/index.css create mode 100644 services/privacymanager/dialog_ui/pages/index/index.hml create mode 100644 services/privacymanager/dialog_ui/pages/index/index.js create mode 100644 services/privacymanager/include/sensitive/camera_float_window_listener.h create mode 100644 services/privacymanager/include/sensitive/global_switch_manager.h create mode 100644 services/privacymanager/src/sensitive/camera_float_window_listener.cpp create mode 100644 services/privacymanager/src/sensitive/global_switch_manager.cpp create mode 100644 services/privacymanager/test/BUILD.gn create mode 100644 services/privacymanager/test/camera_float_window_listener_test/BUILD.gn create mode 100644 services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.cpp create mode 100644 services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.h diff --git a/BUILD.gn b/BUILD.gn index d52623233..d5d4c44e6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -60,6 +60,7 @@ group("accesstoken_build_module_test") { "//base/security/access_token/interfaces/innerkits/privacy/test:unittest", "//base/security/access_token/interfaces/innerkits/token_setproc/test:unittest", "//base/security/access_token/services/accesstokenmanager/test:unittest", + "//base/security/access_token/services/privacymanager/test:unittest", ] } if (token_sync_enable == true) { diff --git a/frameworks/privacy/BUILD.gn b/frameworks/privacy/BUILD.gn index e0106a79d..0104023ca 100644 --- a/frameworks/privacy/BUILD.gn +++ b/frameworks/privacy/BUILD.gn @@ -34,6 +34,4 @@ ohos_shared_library("privacy_communication_adapter_cxx") { ] deps = [ "//utils/native/base:utils" ] - - external_deps = [ "ipc:ipc_core" ] } diff --git a/services/privacymanager/BUILD.gn b/services/privacymanager/BUILD.gn index 5a9801289..8e89d880e 100644 --- a/services/privacymanager/BUILD.gn +++ b/services/privacymanager/BUILD.gn @@ -28,6 +28,7 @@ ohos_shared_library("privacy_manager_service") { "include/common", "include/database", "include/record", + "include/sensitive", "include/service", "//base/security/access_token/frameworks/privacy/include", "//base/security/access_token/frameworks/common/include", @@ -50,6 +51,7 @@ ohos_shared_library("privacy_manager_service") { "src/record/permission_record_repository.cpp", "src/record/permission_visitor.cpp", "src/record/permission_visitor_repository.cpp", + "src/sensitive/global_switch_manager.cpp", "src/service/privacy_manager_service.cpp", "src/service/privacy_manager_stub.cpp", ] @@ -61,14 +63,18 @@ ohos_shared_library("privacy_manager_service") { "//base/security/access_token/frameworks/privacy:privacy_communication_adapter_cxx", "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", "//base/security/access_token/services/common/database:accesstoken_database_cxx", + "//base/security/access_token/services/privacymanager/dialog_ui:dialog_js_files_etc", "//base/security/access_token/services/privacymanager:privacy.rc", + "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", "//third_party/sqlite:sqlite", "//utils/native/base:utils", ] external_deps = [ + "ability_base:want", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "hiviewdfx_hilog_native:libhilog", - "init:libbegetutil", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", diff --git a/services/privacymanager/dialog_ui/BUILD.gn b/services/privacymanager/dialog_ui/BUILD.gn new file mode 100644 index 000000000..0668d5645 --- /dev/null +++ b/services/privacymanager/dialog_ui/BUILD.gn @@ -0,0 +1,27 @@ +# 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/notification/distributed_notification_service/notification.gni") +import("//foundation/arkui/ace_engine/build/ace_gen_sa_dialog_js.gni") + +gen_sa_dialog_js("dialog_js_files_etc") { + project_path = "//base/security/access_token/services/privacymanager/dialog_ui/" + i18n_files = [ + "i18n/en-US.json", + "i18n/zh-CN.json", + ] + common_files = [] + dialog_name = "global_switch_dialog" + subsystem_name = "security" + part_name = "access_token" +} diff --git a/services/privacymanager/dialog_ui/common/.gitkeep b/services/privacymanager/dialog_ui/common/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/services/privacymanager/dialog_ui/i18n/en-US.json b/services/privacymanager/dialog_ui/i18n/en-US.json new file mode 100644 index 000000000..c5ec0165b --- /dev/null +++ b/services/privacymanager/dialog_ui/i18n/en-US.json @@ -0,0 +1,7 @@ +{ + "message":{ + "request": "Allowed to send notification?", + "allowButton": "Allow", + "cancelButton": "Cancel" + } +} diff --git a/services/privacymanager/dialog_ui/i18n/zh-CN.json b/services/privacymanager/dialog_ui/i18n/zh-CN.json new file mode 100644 index 000000000..e0d706325 --- /dev/null +++ b/services/privacymanager/dialog_ui/i18n/zh-CN.json @@ -0,0 +1,7 @@ +{ + "message": { + "request": "正在申请开启本机摄像头功能", + "allowButton": "开启", + "cancelButton": "拒绝" + } +} diff --git a/services/privacymanager/dialog_ui/pages/index/index.css b/services/privacymanager/dialog_ui/pages/index/index.css new file mode 100644 index 000000000..727c1ea71 --- /dev/null +++ b/services/privacymanager/dialog_ui/pages/index/index.css @@ -0,0 +1,86 @@ +/* + * 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. + */ + + .container { + height: 100%; + width: 100%; + position: relative; + display: flex; + justify-content: center; + align-items: center; +} + +.global-dialog { + width: 397vp; + height: 117vp; + border-radius: 32vp; + align-items: center; + background-color: rgba(255, 255, 255, 1); + display: flex; + flex-direction: column; + box-shadow: 2vp 2vp 2vp #888888; +} + +.title-bar { + width: 100%; + height: 34vp; + font-size: 14fp; + font-weight: 400; + word-break: break-word; + text-color: #182431; + opacity: 1; + text-align: center; + margin-top: 24vp; + font-family: 'HarmonyHeiTi'; +} + +.btn-group { + align-items: center; + flex-direction: row; + display: flex; + justify-content: center; +} + +.btn-inner { + width: 116vp; + height: 40vp; + margin: 16vp 4vp 16vp 4.5vp; + align-items: center; +} + +.btn { + width: 100vp; + height: 22vp; + font-size: 16fp; + background-color: rgba(255, 255, 255, 1); + text-color: #0d81f2; + line-height: 22vp; + font-weight: 500; + margin: 9vp 8vp; +} + +.split { + width: 0.5vp; + height: 24vp; + background-color: rgba(0, 0, 0, 0.2); + align-self: center; +} + +.center { + position: relative; + display: flex; + justify-content: center; + align-items: center; +} diff --git a/services/privacymanager/dialog_ui/pages/index/index.hml b/services/privacymanager/dialog_ui/pages/index/index.hml new file mode 100644 index 000000000..c0d544e8c --- /dev/null +++ b/services/privacymanager/dialog_ui/pages/index/index.hml @@ -0,0 +1,43 @@ + + +
+ + + + {{ prompt }} + + + + +
+ +
+
+ +
+
+ +
+ +
+
+
+
+
diff --git a/services/privacymanager/dialog_ui/pages/index/index.js b/services/privacymanager/dialog_ui/pages/index/index.js new file mode 100644 index 000000000..e5983709c --- /dev/null +++ b/services/privacymanager/dialog_ui/pages/index/index.js @@ -0,0 +1,38 @@ +/* + * 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 router from '@ohos.router' + +export default { + data: { + prompt: '', + allowButton: '', + cancelButton: '', + }, + onInit() { + console.info('getParams: ' + JSON.stringify(router.getParams())); + this.prompt = router.getParams()['bundleName'] + this.$t("message.request") + '?' + this.allowButton = this.$t('message.allowButton'); + this.cancelButton = this.$t('message.cancelButton'); + }, + onAllow() { + console.info('clicked allow'); + callNativeHandler("EVENT_ALLOW", "allow"); + }, + onCancel() { + console.info('clicked cancel'); + callNativeHandler("EVENT_CANCEL", "cancel"); + }, +} diff --git a/services/privacymanager/include/sensitive/camera_float_window_listener.h b/services/privacymanager/include/sensitive/camera_float_window_listener.h new file mode 100644 index 000000000..97ab6177b --- /dev/null +++ b/services/privacymanager/include/sensitive/camera_float_window_listener.h @@ -0,0 +1,34 @@ +/* + * 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 CAMERA_FLOAT_WINDOW_LISTENER_H +#define CAMERA_FLOAT_WINDOW_LISTENER_H + +#include "window_manager.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +class CameraFloatWindowChangeListener : public ICameraFloatWindowChangeListener { +public: + void OnCameraFloatWindowChange(uint32_t accessTokenId, bool isShowing) override; +private: + uint32_t tokenId_ = 0; + bool isShow_ = false; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // CAMERA_FLOAT_WINDOW_LISTENER_H diff --git a/services/privacymanager/include/sensitive/global_switch_manager.h b/services/privacymanager/include/sensitive/global_switch_manager.h new file mode 100644 index 000000000..cff446ab5 --- /dev/null +++ b/services/privacymanager/include/sensitive/global_switch_manager.h @@ -0,0 +1,50 @@ +/* + * 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_MANAGER_H +#define GLOBAL_SWITCH_MANAGER_H + +#include +#include +#include + +namespace OHOS { +namespace Security { +namespace AccessToken { +class GlobalSwitchManager final { +public: + enum ResourceType { + CAMERA = 0, + MICROPHONE, + }; + + static GlobalSwitchManager& GetInstance(); + virtual ~GlobalSwitchManager(); + + void InitGlobalSwitchStatus(); + void SetGlobalSwitchStatus(const ResourceType& type, bool switchStatus); + bool GetGlobalSwitchStatus(const ResourceType& type); + void RegisterGlobalSwitchChangeListen(const ResourceType& type); + void UnRegisterGlobalSwitchChangeListen(const ResourceType& type); + bool ShowDialog(const ResourceType& type); +private: + std::map switchStatusMap_; + std::mutex mutex_; + int32_t dialogId_ = -1; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // GLOBAL_SWITCH_MANAGER_H diff --git a/services/privacymanager/src/sensitive/camera_float_window_listener.cpp b/services/privacymanager/src/sensitive/camera_float_window_listener.cpp new file mode 100644 index 000000000..d3c994297 --- /dev/null +++ b/services/privacymanager/src/sensitive/camera_float_window_listener.cpp @@ -0,0 +1,36 @@ +/* + * 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 "camera_float_window_listener.h" +#include "accesstoken_log.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "GlobalSwitchManager" +}; +} + +void CameraFloatWindowChangeListener::OnCameraFloatWindowChange(uint32_t accessTokenId, bool isShowing) +{ + ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(tokenId=%{public}d, isShow=%{public}d)", accessTokenId, isShowing); + + +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/src/sensitive/global_switch_manager.cpp b/services/privacymanager/src/sensitive/global_switch_manager.cpp new file mode 100644 index 000000000..b69f524b1 --- /dev/null +++ b/services/privacymanager/src/sensitive/global_switch_manager.cpp @@ -0,0 +1,112 @@ +/* + * 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 "global_switch_manager.h" +#include "accesstoken_log.h" +#include "ui_service_mgr_client.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "GlobalSwitchManager" +}; +} + +GlobalSwitchManager& GlobalSwitchManager::GetInstance() +{ + static GlobalSwitchManager instance; + return instance; +} + +GlobalSwitchManager::~GlobalSwitchManager() +{ +} + +void GlobalSwitchManager::InitGlobalSwitchStatus() +{ + // TODO:从服务获取camera和microphone的全局开关状态 +} + +void GlobalSwitchManager::SetGlobalSwitchStatus(const ResourceType& type, bool switchStatus) +{ + if (switchStatusMap_.count(type) == 0) { + return; + } + + // TODO:调用服务的mute接口 + switchStatusMap_[type] = switchStatus; +} + +bool GlobalSwitchManager::GetGlobalSwitchStatus(const ResourceType& type) +{ + if (switchStatusMap_.count(type) == 0) { + return true; + } + return switchStatusMap_[type]; +} + +void GlobalSwitchManager::RegisterGlobalSwitchChangeListen(const ResourceType& type) +{ + // TODO:全局开关的注册监听,依赖于服务 +} + +void GlobalSwitchManager::UnRegisterGlobalSwitchChangeListen(const ResourceType& type) +{ + // TODO:全局开关的去注册监听,依赖于服务 +} + +bool GlobalSwitchManager::ShowDialog(const ResourceType& type) +{ + if (dialogId_ >= 0) { + ACCESSTOKEN_LOG_INFO(LABEL, "dialog is already showing"); + return true; + } + + // ACE + const int32_t ACE_X = 50; + const int32_t ACE_Y = 300; + const int32_t ACE_WIDTH = 580; + const int32_t ACE_HEIGHT = 520; + const std::string EVENT_CONFIRM = "EVENT_ALLOW"; + const std::string EVENT_CANCEL = "EVENT_CANCEL"; + //const std::string EVENT_CONFIRM_CODE = "0"; + //const std::string EVENT_CANCEL_CODE = "1"; + //const std::string EVENT_INIT_CODE = "2"; + + const std::string params = std::string("{\"requestNotification\":\"Allowed to send notification?\", ") + + std::string("\"allowButton\":\"Allow\", \"cancelButton\":\"Cancel\"") + std::string("\"}"); + int32_t errCode = Ace::UIServiceMgrClient::GetInstance()->ShowDialog( + "global_switch_dialog", + params, + OHOS::Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, + ACE_X, + ACE_Y, + ACE_WIDTH, + ACE_HEIGHT, + [this](int32_t id, const std::string& event, const std::string& params) { + ACCESSTOKEN_LOG_INFO(LABEL, "Dialog callback: %{public}s, %{public}s", event.c_str(), params.c_str()); + if (event == "EVENT_CANCEL") { + Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); + dialogId_ = -1; + } + }, &dialogId_); + ACCESSTOKEN_LOG_INFO(LABEL, "Show dialog errCode %{public}d, dialogId=%{public}d", errCode, dialogId_); + return !errCode; +} +} // 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 new file mode 100644 index 000000000..a3228c9ca --- /dev/null +++ b/services/privacymanager/test/BUILD.gn @@ -0,0 +1,20 @@ +# 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") + +group("unittest") { + testonly = true + deps = [ "camera_float_window_listener_test:camera_float_window_listener_test" ] +} diff --git a/services/privacymanager/test/camera_float_window_listener_test/BUILD.gn b/services/privacymanager/test/camera_float_window_listener_test/BUILD.gn new file mode 100644 index 000000000..f42c7ab69 --- /dev/null +++ b/services/privacymanager/test/camera_float_window_listener_test/BUILD.gn @@ -0,0 +1,39 @@ +# 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("camera_float_window_listener_test") { + subsystem_name = "security" + part_name = "access_token" + module_out_path = part_name + "/" + part_name + + include_dirs = [ + "//base/security/access_token/services/privacymanager/include/sensitive", + "//third_party/googletest/include", + ] + + sources = [ "camera_float_window_listener_test.cpp" ] + + deps = [ + "//base/security/access_token/services/privacymanager:privacy_manager_service", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] +} + +group("unittest") { + testonly = true + deps = [ ":camera_float_window_listener_test" ] +} diff --git a/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.cpp b/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.cpp new file mode 100644 index 000000000..ade16e9e7 --- /dev/null +++ b/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.cpp @@ -0,0 +1,46 @@ +/* + * 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 "camera_float_window_listener_test.h" +#include "global_switch_manager.h" + +using namespace testing::ext; +using namespace OHOS::Security::AccessToken; + +void CameraFloatWindowListenerTest::SetUpTestCase() +{} + +void CameraFloatWindowListenerTest::TearDownTestCase() +{ +} + +void CameraFloatWindowListenerTest::SetUp() +{ +} + +void CameraFloatWindowListenerTest::TearDown() +{ +} + +/** + * @tc.name: CameraFloatWindowListenerTest_001 + * @tc.desc: cannot AddPermissionUsedRecord with invalid tokenID and permission. + * @tc.type: FUNC + * @tc.require:AR000GK6TD===== + */ +HWTEST_F(CameraFloatWindowListenerTest, CameraFloatWindowListenerTest_001, TestSize.Level1) +{ + GlobalSwitchManager::GetInstance().ShowDialog(GlobalSwitchManager::CAMERA); +} diff --git a/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.h b/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.h new file mode 100644 index 000000000..863a76a50 --- /dev/null +++ b/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.h @@ -0,0 +1,37 @@ +/* + * 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 CAMERA_FLOAT_WINDOW_LISTENER_TEST_H +#define CAMERA_FLOAT_WINDOW_LISTENER_TEST_H + +#include + +namespace OHOS { +namespace Security { +namespace AccessToken { +class CameraFloatWindowListenerTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // CAMERA_FLOAT_WINDOW_LISTENER_TEST_H -- Gitee From 51eb319226a5c51d863d694cbdd3db0b2cf90e79 Mon Sep 17 00:00:00 2001 From: chennian Date: Wed, 20 Jul 2022 22:48:19 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E9=9C=80=E6=B1=82=20Signed-off-by:chennian?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chennian --- services/privacymanager/BUILD.gn | 2 +- services/privacymanager/test/BUILD.gn | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/services/privacymanager/BUILD.gn b/services/privacymanager/BUILD.gn index 8e89d880e..acdd52a92 100644 --- a/services/privacymanager/BUILD.gn +++ b/services/privacymanager/BUILD.gn @@ -63,8 +63,8 @@ ohos_shared_library("privacy_manager_service") { "//base/security/access_token/frameworks/privacy:privacy_communication_adapter_cxx", "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", "//base/security/access_token/services/common/database:accesstoken_database_cxx", - "//base/security/access_token/services/privacymanager/dialog_ui:dialog_js_files_etc", "//base/security/access_token/services/privacymanager:privacy.rc", + "//base/security/access_token/services/privacymanager/dialog_ui:dialog_js_files_etc", "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", "//third_party/sqlite:sqlite", "//utils/native/base:utils", diff --git a/services/privacymanager/test/BUILD.gn b/services/privacymanager/test/BUILD.gn index a3228c9ca..a4d504d4a 100644 --- a/services/privacymanager/test/BUILD.gn +++ b/services/privacymanager/test/BUILD.gn @@ -16,5 +16,6 @@ import("//build/test.gni") group("unittest") { testonly = true - deps = [ "camera_float_window_listener_test:camera_float_window_listener_test" ] + deps = + [ "camera_float_window_listener_test:camera_float_window_listener_test" ] } -- Gitee From 5aa6c73bb970fde7ff44dd966d215c6c9b962103 Mon Sep 17 00:00:00 2001 From: chennian Date: Fri, 22 Jul 2022 18:13:56 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E9=9A=90=E7=A7=81=E9=9C=80=E6=B1=82?= =?UTF-8?q?=E5=BC=80=E5=8F=91=20Signed-off-by:chennian?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chennian --- ...h => application_status_change_callback.h} | 31 ++------ ... => camera_float_window_change_callback.h} | 8 +-- .../sensitive/global_switch_change_callback.h | 34 +++++++++ .../include/sensitive/resource_manager.h | 70 +++++++++++++++++++ .../application_status_change_callback.cpp | 43 ++++++++++++ ...> camera_float_window_change_callback.cpp} | 2 +- .../global_switch_change_callback.cpp | 30 ++++++++ ...witch_manager.cpp => resource_manager.cpp} | 65 +++++++++++------ 8 files changed, 234 insertions(+), 49 deletions(-) rename services/privacymanager/include/sensitive/{global_switch_manager.h => application_status_change_callback.h} (48%) rename services/privacymanager/include/sensitive/{camera_float_window_listener.h => camera_float_window_change_callback.h} (83%) create mode 100644 services/privacymanager/include/sensitive/global_switch_change_callback.h create mode 100644 services/privacymanager/include/sensitive/resource_manager.h create mode 100644 services/privacymanager/src/sensitive/application_status_change_callback.cpp rename services/privacymanager/src/sensitive/{camera_float_window_listener.cpp => camera_float_window_change_callback.cpp} (93%) create mode 100644 services/privacymanager/src/sensitive/global_switch_change_callback.cpp rename services/privacymanager/src/sensitive/{global_switch_manager.cpp => resource_manager.cpp} (66%) diff --git a/services/privacymanager/include/sensitive/global_switch_manager.h b/services/privacymanager/include/sensitive/application_status_change_callback.h similarity index 48% rename from services/privacymanager/include/sensitive/global_switch_manager.h rename to services/privacymanager/include/sensitive/application_status_change_callback.h index cff446ab5..d96da2418 100644 --- a/services/privacymanager/include/sensitive/global_switch_manager.h +++ b/services/privacymanager/include/sensitive/application_status_change_callback.h @@ -13,38 +13,21 @@ * limitations under the License. */ -#ifndef GLOBAL_SWITCH_MANAGER_H -#define GLOBAL_SWITCH_MANAGER_H +#ifndef APPLICATION_STATUS_CHANGE_CALLBACK_H +#define APPLICATION_STATUS_CHANGE_CALLBACK_H -#include -#include -#include +#include "iapplication_state_observer.h" namespace OHOS { namespace Security { namespace AccessToken { -class GlobalSwitchManager final { +class ApplicationStatusChangeCallback : public IApplicationStateObserver { public: - enum ResourceType { - CAMERA = 0, - MICROPHONE, - }; - - static GlobalSwitchManager& GetInstance(); - virtual ~GlobalSwitchManager(); - - void InitGlobalSwitchStatus(); - void SetGlobalSwitchStatus(const ResourceType& type, bool switchStatus); - bool GetGlobalSwitchStatus(const ResourceType& type); - void RegisterGlobalSwitchChangeListen(const ResourceType& type); - void UnRegisterGlobalSwitchChangeListen(const ResourceType& type); - bool ShowDialog(const ResourceType& type); + void OnForegroundApplicationChange(const AppStateData& appStateData) override; + void OnProcessDied(const ProcesData& processData) override; private: - std::map switchStatusMap_; - std::mutex mutex_; - int32_t dialogId_ = -1; }; } // namespace AccessToken } // namespace Security } // namespace OHOS -#endif // GLOBAL_SWITCH_MANAGER_H +#endif // APPLICATION_STATUS_CHANGE_CALLBACK_H diff --git a/services/privacymanager/include/sensitive/camera_float_window_listener.h b/services/privacymanager/include/sensitive/camera_float_window_change_callback.h similarity index 83% rename from services/privacymanager/include/sensitive/camera_float_window_listener.h rename to services/privacymanager/include/sensitive/camera_float_window_change_callback.h index 97ab6177b..d53d0b09e 100644 --- a/services/privacymanager/include/sensitive/camera_float_window_listener.h +++ b/services/privacymanager/include/sensitive/camera_float_window_change_callback.h @@ -13,15 +13,15 @@ * limitations under the License. */ -#ifndef CAMERA_FLOAT_WINDOW_LISTENER_H -#define CAMERA_FLOAT_WINDOW_LISTENER_H +#ifndef CAMERA_FLOAT_WINDOW_CALLBACK_H +#define CAMERA_FLOAT_WINDOW_CALLBACK_H #include "window_manager.h" namespace OHOS { namespace Security { namespace AccessToken { -class CameraFloatWindowChangeListener : public ICameraFloatWindowChangeListener { +class CameraFloatWindowChangeCallback : public ICameraFloatWindowChangeListener { public: void OnCameraFloatWindowChange(uint32_t accessTokenId, bool isShowing) override; private: @@ -31,4 +31,4 @@ private: } // namespace AccessToken } // namespace Security } // namespace OHOS -#endif // CAMERA_FLOAT_WINDOW_LISTENER_H +#endif // CAMERA_FLOAT_WINDOW_CALLBACK_H diff --git a/services/privacymanager/include/sensitive/global_switch_change_callback.h b/services/privacymanager/include/sensitive/global_switch_change_callback.h new file mode 100644 index 000000000..c4f4343aa --- /dev/null +++ b/services/privacymanager/include/sensitive/global_switch_change_callback.h @@ -0,0 +1,34 @@ +/* + * 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 + +namespace OHOS { +namespace Security { +namespace AccessToken { +class CameraSwitchChangeCallback { +public: + +}; + +class AudioSwitchChangeCallback { +public: + +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // GLOBAL_SWITCH_CHANGE_CALLBACK_H diff --git a/services/privacymanager/include/sensitive/resource_manager.h b/services/privacymanager/include/sensitive/resource_manager.h new file mode 100644 index 000000000..566bf6047 --- /dev/null +++ b/services/privacymanager/include/sensitive/resource_manager.h @@ -0,0 +1,70 @@ +/* + * 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 RESOURCE_MANAGER_H +#define RESOURCE_MANAGER_H + +#include +#include +#include + +namespace OHOS { +namespace Security { +namespace AccessToken { +enum AppStatus { + APP_CREATE = 0, + APP_DIE, + APP_FOREGROUND, + APP_BACKGROUND, +}; +enum ResourceType { + CAMERA = 0, + MICROPHONE, +}; +typedef void (*OnAppStatusChangeCallback)(uint32_t tokenId, AppStatus status); +typedef void (*OnCameraFloatWindowChangeCallback)(uint32_t tokenId, bool isShowing); +class ResourceManager final { +public: + static ResourceManager& GetInstance(); + virtual ~ResourceManager(); + + void Init(); + bool GetGlobalSwitchStatus(const ResourceType& type); + bool IsAllowedRequest(AccessTokenID tokenId, const ResourceType& type); + + // 前后台监听 + void RegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback); + void UnRegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback); + + // camera float window + void RegisterCameraFloatWindowChangeCallback(OnCameraFloatWindowChangeCallback callback); + void UnRegisterGlobalSwitchChangeListen(); + + bool ShowDialog(const ResourceType& type); +public: + void NotifyAppStatusChange(uint32_t tokenId, AppStatus status); + void NotifyFloatWindowChange(uint32_t tokenId, bool isShowing); + void NotifyGlobalSwitchChange(const ResourceType& type, bool switch); + +private: + std::map switchStatusMap_; + std::map appStatusCallbackMap_; + OnCameraFloatWindowChangeCallback camera_FloatWindowCallback_; + int32_t dialogId_ = -1; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // RESOURCE_MANAGER_H diff --git a/services/privacymanager/src/sensitive/application_status_change_callback.cpp b/services/privacymanager/src/sensitive/application_status_change_callback.cpp new file mode 100644 index 000000000..e2bd6c197 --- /dev/null +++ b/services/privacymanager/src/sensitive/application_status_change_callback.cpp @@ -0,0 +1,43 @@ +/* + * 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 "application_status_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, "ApplicationStatusChangeCallback" +}; +} + +void ApplicationStatusChangeCallback::OnForegroundApplicationChange(const AppStateData& appStateData) + ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(bundleName=%{public}s, uid=%{public}d, state=%{public}d)", + appStateData.bundleName.c_str(), appStateData.uid, appStateData.state); + + +} + +void ApplicationStatusChangeCallback::OnProcessDied(const ProcesData& processData) + ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(bundleName=%{public}s, uid=%{public}d, pid=%{public}d)", + processData.bundleName.c_str(), processData.uid, processData.pid); + + +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/src/sensitive/camera_float_window_listener.cpp b/services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp similarity index 93% rename from services/privacymanager/src/sensitive/camera_float_window_listener.cpp rename to services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp index d3c994297..4ad2097fc 100644 --- a/services/privacymanager/src/sensitive/camera_float_window_listener.cpp +++ b/services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp @@ -21,7 +21,7 @@ namespace Security { namespace AccessToken { namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_PRIVACY, "GlobalSwitchManager" + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "CameraFloatWindowChangeListener" }; } diff --git a/services/privacymanager/src/sensitive/global_switch_change_callback.cpp b/services/privacymanager/src/sensitive/global_switch_change_callback.cpp new file mode 100644 index 000000000..833f4f639 --- /dev/null +++ b/services/privacymanager/src/sensitive/global_switch_change_callback.cpp @@ -0,0 +1,30 @@ +/* + * 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 "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, "AudioSwitchChangeCallback" +}; +} + +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/services/privacymanager/src/sensitive/global_switch_manager.cpp b/services/privacymanager/src/sensitive/resource_manager.cpp similarity index 66% rename from services/privacymanager/src/sensitive/global_switch_manager.cpp rename to services/privacymanager/src/sensitive/resource_manager.cpp index b69f524b1..60b2ffbb2 100644 --- a/services/privacymanager/src/sensitive/global_switch_manager.cpp +++ b/services/privacymanager/src/sensitive/resource_manager.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "global_switch_manager.h" +#include "resource_manager.h" #include "accesstoken_log.h" #include "ui_service_mgr_client.h" @@ -22,36 +22,30 @@ namespace Security { namespace AccessToken { namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_PRIVACY, "GlobalSwitchManager" + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "ResourceManager" }; } -GlobalSwitchManager& GlobalSwitchManager::GetInstance() +ResourceManager& ResourceManager::GetInstance() { - static GlobalSwitchManager instance; + static ResourceManager instance; return instance; } -GlobalSwitchManager::~GlobalSwitchManager() +ResourceManager::~ResourceManager() { } -void GlobalSwitchManager::InitGlobalSwitchStatus() +void ResourceManager::Init() { // TODO:从服务获取camera和microphone的全局开关状态 -} - -void GlobalSwitchManager::SetGlobalSwitchStatus(const ResourceType& type, bool switchStatus) -{ - if (switchStatusMap_.count(type) == 0) { - return; - } + switchStatusMap_[CAMERA] = false; + switchStatusMap_[MICROPHONE] = false; - // TODO:调用服务的mute接口 - switchStatusMap_[type] = switchStatus; + // TODO:注册camera和microphone的全局开关变化监听 } -bool GlobalSwitchManager::GetGlobalSwitchStatus(const ResourceType& type) +bool ResourceManager::GetGlobalSwitch(const ResourceType& type) { if (switchStatusMap_.count(type) == 0) { return true; @@ -59,17 +53,32 @@ bool GlobalSwitchManager::GetGlobalSwitchStatus(const ResourceType& type) return switchStatusMap_[type]; } -void GlobalSwitchManager::RegisterGlobalSwitchChangeListen(const ResourceType& type) +bool ResourceManager::IsAllowedRequest(AccessTokenID tokenId, const ResourceType& type) { - // TODO:全局开关的注册监听,依赖于服务 + return true; } -void GlobalSwitchManager::UnRegisterGlobalSwitchChangeListen(const ResourceType& type) +void ResourceManager::RegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback) { // TODO:全局开关的去注册监听,依赖于服务 } -bool GlobalSwitchManager::ShowDialog(const ResourceType& type) +void ResourceManager::UnRegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback) +{ + // TODO:全局开关的去注册监听,依赖于服务 +} + +void ResourceManager::RegisterCameraFloatWindowChangeCallback(OnCameraFloatWindowChangeCallback callback) +{ + +} + +void ResourceManager::UnRegisterGlobalSwitchChangeListen() +{ + +} + +bool ResourceManager::ShowDialog(const ResourceType& type) { if (dialogId_ >= 0) { ACCESSTOKEN_LOG_INFO(LABEL, "dialog is already showing"); @@ -107,6 +116,22 @@ bool GlobalSwitchManager::ShowDialog(const ResourceType& type) ACCESSTOKEN_LOG_INFO(LABEL, "Show dialog errCode %{public}d, dialogId=%{public}d", errCode, dialogId_); return !errCode; } + +void ResourceManager::NotifyAppStatusChange(uint32_t tokenId, AppStatus status) +{ + +} + +void ResourceManager::NotifyFloatWindowChange(uint32_t tokenId, bool isShowing) +{ + +} + +void ResourceManager::NotifyGlobalSwitchChange(const ResourceType& type, bool switch) +{ + +} + } // namespace AccessToken } // namespace Security } // namespace OHOS \ No newline at end of file -- Gitee From 08b829c089d4de684b1722bcfccadc3dc026d220 Mon Sep 17 00:00:00 2001 From: chennian Date: Sun, 24 Jul 2022 12:04:19 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E9=9A=90=E7=A7=81=E9=9C=80=E6=B1=82?= =?UTF-8?q?=E5=BC=80=E5=8F=91=20Signed-off-by:chennian?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chennian --- .../application_status_change_callback.h | 3 +- .../camera_float_window_change_callback.h | 3 +- .../include/sensitive/resource_manager.h | 18 ++++++-- .../application_status_change_callback.cpp | 13 +++++- .../camera_float_window_change_callback.cpp | 9 ++-- .../src/sensitive/resource_manager.cpp | 42 ++++++++++++++--- .../src/service/privacy_manager_service.cpp | 2 + .../BUILD.gn | 17 ++++++- .../camera_float_window_listener_test.cpp | 46 +++++++++++++++++-- 9 files changed, 128 insertions(+), 25 deletions(-) diff --git a/services/privacymanager/include/sensitive/application_status_change_callback.h b/services/privacymanager/include/sensitive/application_status_change_callback.h index d96da2418..b8d0be571 100644 --- a/services/privacymanager/include/sensitive/application_status_change_callback.h +++ b/services/privacymanager/include/sensitive/application_status_change_callback.h @@ -25,7 +25,8 @@ class ApplicationStatusChangeCallback : public IApplicationStateObserver { public: void OnForegroundApplicationChange(const AppStateData& appStateData) override; void OnProcessDied(const ProcesData& processData) override; -private: + uint32_t tokenId_ = 0; + int32_t state = 0; }; } // namespace AccessToken } // namespace Security diff --git a/services/privacymanager/include/sensitive/camera_float_window_change_callback.h b/services/privacymanager/include/sensitive/camera_float_window_change_callback.h index d53d0b09e..54ff119c0 100644 --- a/services/privacymanager/include/sensitive/camera_float_window_change_callback.h +++ b/services/privacymanager/include/sensitive/camera_float_window_change_callback.h @@ -21,10 +21,9 @@ namespace OHOS { namespace Security { namespace AccessToken { -class CameraFloatWindowChangeCallback : public ICameraFloatWindowChangeListener { +class CameraFloatWindowChangeCallback : public Rosen::ICameraFloatWindowChangedListener { public: void OnCameraFloatWindowChange(uint32_t accessTokenId, bool isShowing) override; -private: uint32_t tokenId_ = 0; bool isShow_ = false; }; diff --git a/services/privacymanager/include/sensitive/resource_manager.h b/services/privacymanager/include/sensitive/resource_manager.h index 566bf6047..30cb861bc 100644 --- a/services/privacymanager/include/sensitive/resource_manager.h +++ b/services/privacymanager/include/sensitive/resource_manager.h @@ -19,6 +19,7 @@ #include #include #include +#include "camera_float_window_change_callback.h" namespace OHOS { namespace Security { @@ -35,14 +36,16 @@ enum ResourceType { }; typedef void (*OnAppStatusChangeCallback)(uint32_t tokenId, AppStatus status); typedef void (*OnCameraFloatWindowChangeCallback)(uint32_t tokenId, bool isShowing); +using CameraFloatWindowChangeCallbackMap = + std::map>; class ResourceManager final { public: static ResourceManager& GetInstance(); virtual ~ResourceManager(); void Init(); - bool GetGlobalSwitchStatus(const ResourceType& type); - bool IsAllowedRequest(AccessTokenID tokenId, const ResourceType& type); + bool GetGlobalSwitch(const ResourceType& type); + bool IsAllowedRequest(uint32_t tokenId, const ResourceType& type); // 前后台监听 void RegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback); @@ -50,18 +53,23 @@ public: // camera float window void RegisterCameraFloatWindowChangeCallback(OnCameraFloatWindowChangeCallback callback); - void UnRegisterGlobalSwitchChangeListen(); + void UnRegisterCameraFloatWindowChangeCallback(OnCameraFloatWindowChangeCallback callback); bool ShowDialog(const ResourceType& type); public: void NotifyAppStatusChange(uint32_t tokenId, AppStatus status); void NotifyFloatWindowChange(uint32_t tokenId, bool isShowing); - void NotifyGlobalSwitchChange(const ResourceType& type, bool switch); + void NotifyGlobalSwitchChange(const ResourceType& type, bool status); private: + std::mutex switchStatusMutex_; std::map switchStatusMap_; + + std::mutex appStatusMutex_; std::map appStatusCallbackMap_; - OnCameraFloatWindowChangeCallback camera_FloatWindowCallback_; + + std::mutex cameraFloatWindowMutex_; + CameraFloatWindowChangeCallbackMap cameraFloatWindowCallbacks_; int32_t dialogId_ = -1; }; } // namespace AccessToken diff --git a/services/privacymanager/src/sensitive/application_status_change_callback.cpp b/services/privacymanager/src/sensitive/application_status_change_callback.cpp index e2bd6c197..c58f894b0 100644 --- a/services/privacymanager/src/sensitive/application_status_change_callback.cpp +++ b/services/privacymanager/src/sensitive/application_status_change_callback.cpp @@ -15,6 +15,7 @@ #include "application_status_change_callback.h" #include "accesstoken_log.h" +#include "resource_manager.h" namespace OHOS { namespace Security { @@ -25,18 +26,26 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { }; } +AppStatus ApplicationStatusChangeCallback::GetAppStatue(int state) +{ + // TODOҪstate + return APP_FOREGROUND; +} + void ApplicationStatusChangeCallback::OnForegroundApplicationChange(const AppStateData& appStateData) ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(bundleName=%{public}s, uid=%{public}d, state=%{public}d)", appStateData.bundleName.c_str(), appStateData.uid, appStateData.state); - + uint32_t tokenId = 0; // TODOݱȡtokenId + ResourceManager::GetInstance().NotifyAppStatusChange(tokenId, GetAppStatue(appStateData.state)); } void ApplicationStatusChangeCallback::OnProcessDied(const ProcesData& processData) ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(bundleName=%{public}s, uid=%{public}d, pid=%{public}d)", processData.bundleName.c_str(), processData.uid, processData.pid); - + uint32_t tokenId = 0; // TODOݱȡtokenId + ResourceManager::GetInstance().NotifyAppStatusChange(tokenId, APP_DIE); } } // namespace AccessToken } // namespace Security diff --git a/services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp b/services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp index 4ad2097fc..c8a915bce 100644 --- a/services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp +++ b/services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp @@ -13,23 +13,24 @@ * limitations under the License. */ -#include "camera_float_window_listener.h" +#include "camera_float_window_change_callback.h" #include "accesstoken_log.h" +#include "resource_manager.h" namespace OHOS { namespace Security { namespace AccessToken { namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { - LOG_CORE, SECURITY_DOMAIN_PRIVACY, "CameraFloatWindowChangeListener" + LOG_CORE, SECURITY_DOMAIN_PRIVACY, "CameraFloatWindowChangeCallback" }; } -void CameraFloatWindowChangeListener::OnCameraFloatWindowChange(uint32_t accessTokenId, bool isShowing) +void CameraFloatWindowChangeCallback::OnCameraFloatWindowChange(uint32_t accessTokenId, bool isShowing) { ACCESSTOKEN_LOG_INFO(LABEL, "OnChange(tokenId=%{public}d, isShow=%{public}d)", accessTokenId, isShowing); - + ResourceManager::GetInstance().NotifyFloatWindowChange(accessTokenId, isShowing); } } // namespace AccessToken } // namespace Security diff --git a/services/privacymanager/src/sensitive/resource_manager.cpp b/services/privacymanager/src/sensitive/resource_manager.cpp index 60b2ffbb2..f57dcbf37 100644 --- a/services/privacymanager/src/sensitive/resource_manager.cpp +++ b/services/privacymanager/src/sensitive/resource_manager.cpp @@ -16,6 +16,7 @@ #include "resource_manager.h" #include "accesstoken_log.h" #include "ui_service_mgr_client.h" +#include "window_manager.h" namespace OHOS { namespace Security { @@ -25,7 +26,7 @@ static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_PRIVACY, "ResourceManager" }; } - +using namespace OHOS::Rosen; ResourceManager& ResourceManager::GetInstance() { static ResourceManager instance; @@ -53,29 +54,58 @@ bool ResourceManager::GetGlobalSwitch(const ResourceType& type) return switchStatusMap_[type]; } -bool ResourceManager::IsAllowedRequest(AccessTokenID tokenId, const ResourceType& type) +bool ResourceManager::IsAllowedRequest(uint32_t tokenId, const ResourceType& type) { return true; } void ResourceManager::RegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback) { - // TODO:全局开关的去注册监听,依赖于服务 + // TODO:依赖于支持监听指定应用,6.2交付 } void ResourceManager::UnRegisterAppStatusChangeCallback(uint32_t tokenId, OnAppStatusChangeCallback callback) { - // TODO:全局开关的去注册监听,依赖于服务 + // TODO:依赖于支持监听指定应用,6.2交付 } void ResourceManager::RegisterCameraFloatWindowChangeCallback(OnCameraFloatWindowChangeCallback callback) { + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback could not be null."); + return; + } + + std::lock_guard lock(cameraFloatWindowMutex_); + if (cameraFloatWindowCallbacks_.find(callback) != cameraFloatWindowCallbacks_.end()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback is already registered."); + return; + } + OHOS::sptr listener(new CameraFloatWindowChangeCallback()); + if (listener == nullptr) { + return; + } + cameraFloatWindowCallbacks_[callback] = listener; + WindowManager::GetInstance().RegisterCameraFloatWindowChangedListener(listener); } -void ResourceManager::UnRegisterGlobalSwitchChangeListen() +void ResourceManager::UnRegisterCameraFloatWindowChangeCallback(OnCameraFloatWindowChangeCallback callback) { + if (callback == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback could not be null."); + return; + } + std::lock_guard lock(cameraFloatWindowMutex_); + auto iter = cameraFloatWindowCallbacks_.find(callback); + if (iter == cameraFloatWindowCallbacks_.end()) { + ACCESSTOKEN_LOG_ERROR(LABEL, "callback is not found."); + return; + } + + WindowManager::GetInstance().UnregisterCameraFloatWindowChangedListener(iter->second); + cameraFloatWindowCallbacks_.erase(iter); } bool ResourceManager::ShowDialog(const ResourceType& type) @@ -127,7 +157,7 @@ void ResourceManager::NotifyFloatWindowChange(uint32_t tokenId, bool isShowing) } -void ResourceManager::NotifyGlobalSwitchChange(const ResourceType& type, bool switch) +void ResourceManager::NotifyGlobalSwitchChange(const ResourceType& type, bool status) { } diff --git a/services/privacymanager/src/service/privacy_manager_service.cpp b/services/privacymanager/src/service/privacy_manager_service.cpp index 1f9cecd49..9e11fb03b 100644 --- a/services/privacymanager/src/service/privacy_manager_service.cpp +++ b/services/privacymanager/src/service/privacy_manager_service.cpp @@ -20,6 +20,7 @@ #include "constant.h" #include "ipc_skeleton.h" #include "permission_record_manager.h" +#include "resource_manager.h" namespace OHOS { namespace Security { @@ -122,6 +123,7 @@ std::string PrivacyManagerService::DumpRecordInfo(const std::string& bundleName, { ACCESSTOKEN_LOG_DEBUG(LABEL, "Entry, bundleName: %{public}s, permissionName: %{public}s", bundleName.c_str(), permissionName.c_str()); + ResourceManager::GetInstance().ShowDialog(CAMERA); return PermissionRecordManager::GetInstance().DumpRecordInfo(bundleName, permissionName); } diff --git a/services/privacymanager/test/camera_float_window_listener_test/BUILD.gn b/services/privacymanager/test/camera_float_window_listener_test/BUILD.gn index f42c7ab69..02e67348d 100644 --- a/services/privacymanager/test/camera_float_window_listener_test/BUILD.gn +++ b/services/privacymanager/test/camera_float_window_listener_test/BUILD.gn @@ -20,17 +20,30 @@ ohos_unittest("camera_float_window_listener_test") { module_out_path = part_name + "/" + part_name include_dirs = [ + "//base/security/access_token/interfaces/innerkits/token_setproc/include", "//base/security/access_token/services/privacymanager/include/sensitive", + "//foundation/window/window_manager/wm/include", "//third_party/googletest/include", ] - sources = [ "camera_float_window_listener_test.cpp" ] + sources = [ + "camera_float_window_listener_test.cpp", + "//base/security/access_token/services/privacymanager/src/sensitive/camera_float_window_change_callback.cpp", + "//base/security/access_token/services/privacymanager/src/sensitive/resource_manager.cpp", + ] deps = [ - "//base/security/access_token/services/privacymanager:privacy_manager_service", + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", "//third_party/googletest:gtest_main", "//utils/native/base:utils", ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "window_manager:libwm" + ] + } group("unittest") { diff --git a/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.cpp b/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.cpp index ade16e9e7..2fa41fa6c 100644 --- a/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.cpp +++ b/services/privacymanager/test/camera_float_window_listener_test/camera_float_window_listener_test.cpp @@ -14,11 +14,19 @@ */ #include "camera_float_window_listener_test.h" -#include "global_switch_manager.h" +#include "resource_manager.h" +#include "token_setproc.h" +#include "window.h" +#include "window_scene.h" +#include "wm_common.h" using namespace testing::ext; using namespace OHOS::Security::AccessToken; +static const TEST_OK = 0; +static uint32_t g_selfTokenId = 0; +static uint32_t g_changeTokenId = 0; +static bool g_isShowing = false; void CameraFloatWindowListenerTest::SetUpTestCase() {} @@ -28,19 +36,51 @@ void CameraFloatWindowListenerTest::TearDownTestCase() void CameraFloatWindowListenerTest::SetUp() { + g_selfTokenId = GetSelfTokenID(); + GTEST_LOG_(INFO) << " test process tokenId :" << g_selfTokenId; } void CameraFloatWindowListenerTest::TearDown() { } +static void OnChangeCameraFloatWindow(uint32_t tokenId, bool isShowing) +{ + GTEST_LOG_(INFO) << " OnChangeCameraFloatWindow tokenId:" << tokenId << ", isShowing: " << isShowing; + g_changeTokenId = tokenId; + g_isShowing = isShowing; +} /** * @tc.name: CameraFloatWindowListenerTest_001 * @tc.desc: cannot AddPermissionUsedRecord with invalid tokenID and permission. * @tc.type: FUNC - * @tc.require:AR000GK6TD===== + * @tc.require: */ HWTEST_F(CameraFloatWindowListenerTest, CameraFloatWindowListenerTest_001, TestSize.Level1) { - GlobalSwitchManager::GetInstance().ShowDialog(GlobalSwitchManager::CAMERA); + GlobalSwitchManager::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); + + ASSERT_EQ(TEST_OK, scene->GoForeground()); + ASSERT_EQ(TEST_OK, fltWin->Show()); + + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(g_selfTokenId, g_changeTokenId); + ASSERT_EQ(true, g_isShowing); + + ASSERT_EQ(TEST_OK, fltWin->Hide()); + + usleep(500000); // 500000us = 0.5s + ASSERT_EQ(g_selfTokenId, g_changeTokenId); + ASSERT_EQ(false, g_isShowing); + + ASSERT_EQ(TEST_OK, fltWin->Destroy()); + ASSERT_EQ(TEST_OK, scene->GoDestroy()); + + GlobalSwitchManager::GetInstance().UnRegisterCameraFloatWindowChangeCallback(OnChangeCameraFloatWindow); } -- Gitee From 0a139ec58b5537039355ac720fdd1d8b2c8c5573 Mon Sep 17 00:00:00 2001 From: chennian Date: Sun, 24 Jul 2022 12:12:47 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E9=9A=90=E7=A7=81=E9=9C=80=E6=B1=82?= =?UTF-8?q?=E5=BC=80=E5=8F=91=20Signed-off-by:chennian?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chennian --- services/privacymanager/BUILD.gn | 110 ++++++++++++++++--------------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/services/privacymanager/BUILD.gn b/services/privacymanager/BUILD.gn index acdd52a92..3d0ceee2a 100644 --- a/services/privacymanager/BUILD.gn +++ b/services/privacymanager/BUILD.gn @@ -20,63 +20,65 @@ ohos_prebuilt_etc("privacy.rc") { part_name = "access_token" } -ohos_shared_library("privacy_manager_service") { - subsystem_name = "security" - part_name = "access_token" +if (is_standard_system) { + ohos_shared_library("privacy_manager_service") { + subsystem_name = "security" + part_name = "access_token" - include_dirs = [ - "include/common", - "include/database", - "include/record", - "include/sensitive", - "include/service", - "//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", - "//third_party/json/include", - ] + include_dirs = [ + "include/common", + "include/database", + "include/record", + "include/sensitive", + "include/service", + "//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", + "//third_party/json/include", + ] - sources = [ - "src/common/constant.cpp", - "src/common/time_util.cpp", - "src/common/to_string.cpp", - "src/database/data_translator.cpp", - "src/database/permission_used_record_db.cpp", - "src/record/on_permission_used_record_callback_proxy.cpp", - "src/record/on_permission_used_record_callback_stub.cpp", - "src/record/permission_record.cpp", - "src/record/permission_record_manager.cpp", - "src/record/permission_record_repository.cpp", - "src/record/permission_visitor.cpp", - "src/record/permission_visitor_repository.cpp", - "src/sensitive/global_switch_manager.cpp", - "src/service/privacy_manager_service.cpp", - "src/service/privacy_manager_stub.cpp", - ] + sources = [ + "src/common/constant.cpp", + "src/common/time_util.cpp", + "src/common/to_string.cpp", + "src/database/data_translator.cpp", + "src/database/permission_used_record_db.cpp", + "src/record/on_permission_used_record_callback_proxy.cpp", + "src/record/on_permission_used_record_callback_stub.cpp", + "src/record/permission_record.cpp", + "src/record/permission_record_manager.cpp", + "src/record/permission_record_repository.cpp", + "src/record/permission_visitor.cpp", + "src/record/permission_visitor_repository.cpp", + "src/sensitive/global_switch_manager.cpp", + "src/service/privacy_manager_service.cpp", + "src/service/privacy_manager_stub.cpp", + ] - cflags_cc = [ "-DHILOG_ENABLE" ] + cflags_cc = [ "-DHILOG_ENABLE" ] - deps = [ - "//base/security/access_token/frameworks/common:accesstoken_common_cxx", - "//base/security/access_token/frameworks/privacy:privacy_communication_adapter_cxx", - "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", - "//base/security/access_token/services/common/database:accesstoken_database_cxx", - "//base/security/access_token/services/privacymanager:privacy.rc", - "//base/security/access_token/services/privacymanager/dialog_ui:dialog_js_files_etc", - "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", - "//third_party/sqlite:sqlite", - "//utils/native/base:utils", - ] + deps = [ + "//base/security/access_token/frameworks/common:accesstoken_common_cxx", + "//base/security/access_token/frameworks/privacy:privacy_communication_adapter_cxx", + "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", + "//base/security/access_token/services/common/database:accesstoken_database_cxx", + "//base/security/access_token/services/privacymanager:privacy.rc", + "//base/security/access_token/services/privacymanager/dialog_ui:dialog_js_files_etc", + "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", + "//third_party/sqlite:sqlite", + "//utils/native/base:utils", + ] - external_deps = [ - "ability_base:want", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] + external_deps = [ + "ability_base:want", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] + } } -- Gitee