From 6c1b907686935072bda7d180a5f711b0cc23c1ff Mon Sep 17 00:00:00 2001 From: chen0088 Date: Wed, 25 Oct 2023 10:14:03 +0800 Subject: [PATCH] fix:add privacy info Signed-off-by: chen0088 --- bundle.json | 6 + common/utils/include/device_type.h | 4 +- .../include/dhardware_ipc_interface_code.h | 3 + common/utils/include/idistributed_hardware.h | 3 + .../include/idistributed_hardware_sink.h | 17 +- .../include/distributed_hardware_fwk_kit.h | 27 ++ .../include/ipc/distributed_hardware_proxy.h | 3 + .../src/distributed_hardware_fwk_kit.cpp | 36 ++ .../src/ipc/distributed_hardware_proxy.cpp | 105 +++++ interfaces/kits/napi/BUILD.gn | 61 +++ .../native_distributedhardwarefwk_js.h | 41 ++ .../src/native_distributedhardwarefwk_js.cpp | 376 ++++++++++++++++++ .../distributedhardwarefwkservice/BUILD.gn | 3 + .../componentmanager/component_manager.h | 7 +- .../componentmanager/component_privacy.h | 41 ++ .../include/distributed_hardware_service.h | 5 +- .../include/distributed_hardware_stub.h | 5 +- .../componentmanager/component_manager.cpp | 22 + .../componentmanager/component_privacy.cpp | 132 ++++++ .../src/distributed_hardware_service.cpp | 48 ++- .../src/distributed_hardware_stub.cpp | 45 +++ 21 files changed, 984 insertions(+), 6 deletions(-) create mode 100644 interfaces/kits/napi/BUILD.gn create mode 100644 interfaces/kits/napi/include/native_distributedhardwarefwk_js.h create mode 100644 interfaces/kits/napi/src/native_distributedhardwarefwk_js.cpp create mode 100644 services/distributedhardwarefwkservice/include/componentmanager/component_privacy.h create mode 100644 services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp diff --git a/bundle.json b/bundle.json index d4b2a75b..bfd24860 100644 --- a/bundle.json +++ b/bundle.json @@ -28,6 +28,11 @@ ], "deps": { "components": [ + "ability_base", + "ability_runtime", + "access_token", + "bundle_framework", + "napi", "dsoftbus", "eventhandler", "hitrace", @@ -59,6 +64,7 @@ "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhfwk_sa_profile", "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhardware.cfg", "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/inner_kits:libdhfwk_sdk", + "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/kits/napi:hardwaremanager", "//foundation/distributedhardware/distributed_hardware_fwk/av_transport/av_trans_engine/av_sender:distributed_av_sender", "//foundation/distributedhardware/distributed_hardware_fwk/av_transport/av_trans_engine/av_receiver:distributed_av_receiver", "//foundation/distributedhardware/distributed_hardware_fwk/av_transport/av_trans_handler/histreamer_ability_querier:histreamer_ability_querier" diff --git a/common/utils/include/device_type.h b/common/utils/include/device_type.h index 805b8012..f9aabce5 100644 --- a/common/utils/include/device_type.h +++ b/common/utils/include/device_type.h @@ -25,13 +25,15 @@ enum class DHType : uint32_t { UNKNOWN = 0x0, // unknown device CAMERA = 0x01, // Camera AUDIO = 0x02, // Mic - SCREEN = 0x08, // Display + SCREEN = 0x08, // Display GPS = 0x10, // GPS INPUT = 0x20, // Key board HFP = 0x40, // HFP External device A2D = 0x80, // A2DP External device VIRMODEM_MIC = 0x100, // Cellular call MIC VIRMODEM_SPEAKER = 0x200, // Cellular call Speaker + AUDIO_MIC = 0x400, // AUDIO Mic + AUDIO_SPEAKER = 0x800, // AUDIO Speaker MAX_DH = 0x80000000 }; diff --git a/common/utils/include/dhardware_ipc_interface_code.h b/common/utils/include/dhardware_ipc_interface_code.h index 4027e585..d1ef4af1 100644 --- a/common/utils/include/dhardware_ipc_interface_code.h +++ b/common/utils/include/dhardware_ipc_interface_code.h @@ -31,6 +31,9 @@ enum class DHMsgInterfaceCode : uint32_t { NOTIFY_AV_EVENT = 7, REGISTER_CTL_CEN_CALLBACK = 8, QUERY_LOCAL_SYS_SPEC = 9, + PAUSE_DISTRIBUTED_HARDWARE = 10, + RESUME_DISTRIBUTED_HARDWARE = 11, + STOP_DISTRIBUTED_HARDWARE = 12, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/utils/include/idistributed_hardware.h b/common/utils/include/idistributed_hardware.h index c919ae23..d1a602f4 100644 --- a/common/utils/include/idistributed_hardware.h +++ b/common/utils/include/idistributed_hardware.h @@ -45,6 +45,9 @@ public: virtual int32_t NotifyAVCenter(int32_t engineId, const AVTransEvent &event) = 0; virtual int32_t RegisterCtlCenterCallback(int32_t engineId, const sptr &callback) = 0; + virtual int32_t PauseDistributedHardware(DHType dhType, const std::string &networkId) = 0; + virtual int32_t ResumeDistributedHardware(DHType dhType, const std::string &networkId) = 0; + virtual int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/utils/include/idistributed_hardware_sink.h b/common/utils/include/idistributed_hardware_sink.h index 0b36e0f5..f0c6b45d 100644 --- a/common/utils/include/idistributed_hardware_sink.h +++ b/common/utils/include/idistributed_hardware_sink.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -21,17 +21,32 @@ namespace OHOS { namespace DistributedHardware { const std::string COMPONENT_LOADER_GET_SINK_HANDLER = "GetSinkHardwareHandler"; +enum class ResourceEventType : int32_t { + EVENT_TYPE_QUERY_RESOURCE = 0, + EVENT_TYPE_PULL_UP_PAGE = 1 +}; + class SubscribeCallback { public: virtual int32_t OnSubscribeCallback(const std::string &dhId, int32_t status, const std::string &data) = 0; }; +class PrivacyResourcesListener { +public: + virtual int32_t OnPrivaceResourceMessage(const ResourceEventType &type, const std::string &subType, + const std::string &networkId, bool &isSensitive, bool &isSameAccout) = 0; +}; + class IDistributedHardwareSink { public: virtual int32_t InitSink(const std::string ¶ms) = 0; virtual int32_t ReleaseSink() = 0; virtual int32_t SubscribeLocalHardware(const std::string &dhId, const std::string ¶ms) = 0; virtual int32_t UnsubscribeLocalHardware(const std::string &dhId) = 0; + virtual int32_t RegisterPrivacyResources(std::shared_ptr listener); + virtual int32_t PauseDistributedHardware(const std::string &networkId); + virtual int32_t ResumeDistributedHardware(const std::string &networkId); + virtual int32_t StopDistributedHardware(const std::string &networkId); }; extern "C" __attribute__((visibility("default"))) IDistributedHardwareSink* GetSinkHardwareHandler(); } // namespace DistributedHardware diff --git a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h index d92ccf08..1e22a572 100644 --- a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h +++ b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h @@ -130,6 +130,33 @@ public: */ API_EXPORT int32_t RegisterCtlCenterCallback(int32_t engineId, const sptr &callback); + /** + * @brief Pause distributed hardware. + * + * @param dhType distributed hardware type + * @param networkId distributed hardware networkId. + * @return Returns 0 if success. + */ + API_EXPORT int32_t PauseDistributedHardware(DHType dhType, const std::string &networkId); + + /** + * @brief Resume distributed hardware. + * + * @param dhType distributed hardware type + * @param networkId distributed hardware networkId. + * @return Returns 0 if success. + */ + API_EXPORT int32_t ResumeDistributedHardware(DHType dhType, const std::string &networkId); + + /** + * @brief Stop distributed hardware. + * + * @param dhType distributed hardware type + * @param networkId distributed hardware networkId. + * @return Returns 0 if success. + */ + API_EXPORT int32_t StopDistributedHardware(DHType dhType, const std::string &networkId); + private: /** * @brief Determine whether the topic is valid. diff --git a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h index 14c50bcd..fa9ee699 100644 --- a/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h +++ b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h @@ -43,6 +43,9 @@ public: int32_t CreateControlChannel(int32_t engineId, const std::string &peerDevId) override; int32_t NotifyAVCenter(int32_t engineId, const AVTransEvent &event) override; int32_t RegisterCtlCenterCallback(int32_t engineId, const sptr &callback) override; + int32_t PauseDistributedHardware(DHType dhType, const std::string &networkId) override; + int32_t ResumeDistributedHardware(DHType dhType, const std::string &networkId) override; + int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) override; private: static inline BrokerDelegator delegator_; diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index 3825e89f..22e20a5e 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -224,5 +224,41 @@ int32_t DistributedHardwareFwkKit::RegisterCtlCenterCallback(int32_t engineId, return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(engineId, callback); } + +int32_t DistributedHardwareFwkKit::PauseDistributedHardware(DHType dhType, const std::string &networkId) +{ + DHLOGI("Pause distributed hardware dhType %d, networkId %s", (uint32_t)dhType, networkId.c_str()); + + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->PauseDistributedHardware(dhType, networkId); +} + +int32_t DistributedHardwareFwkKit::ResumeDistributedHardware(DHType dhType, const std::string &networkId) +{ + DHLOGI("Resume distributed hardware dhType %d, networkId %s", (uint32_t)dhType, networkId.c_str()); + + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->ResumeDistributedHardware(dhType, networkId); +} + +int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const std::string &networkId) +{ + DHLOGI("Stop distributed hardware dhType %d, networkId %s", (uint32_t)dhType, networkId.c_str()); + + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->StopDistributedHardware(dhType, networkId); +} } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp index f3b7847f..8ca838e4 100644 --- a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -386,5 +386,110 @@ int32_t DistributedHardwareProxy::RegisterCtlCenterCallback(int32_t engineId, return reply.ReadInt32(); } + +int32_t DistributedHardwareProxy::PauseDistributedHardware(DHType dhType, const std::string &networkId) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + uint32_t type = static_cast(dhType); + if (!data.WriteInt32(type)) { + DHLOGE("Write type failed"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteString(networkId)) { + DHLOGE("Write networkId failed"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::PAUSE_DISTRIBUTED_HARDWARE), + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %d", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::ResumeDistributedHardware(DHType dhType, const std::string &networkId) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + uint32_t type = static_cast(dhType); + if (!data.WriteInt32(type)) { + DHLOGE("Write type failed"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteString(networkId)) { + DHLOGE("Write networkId failed"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::RESUME_DISTRIBUTED_HARDWARE), + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %d", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + + return reply.ReadInt32(); +} + +int32_t DistributedHardwareProxy::StopDistributedHardware(DHType dhType, const std::string &networkId) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; + } + int32_t type = static_cast(dhType); + if (!data.WriteInt32(type)) { + DHLOGE("Write type failed"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteString(networkId)) { + DHLOGE("Write networkId failed"); + return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest(static_cast(DHMsgInterfaceCode::STOP_DISTRIBUTED_HARDWARE), + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %d", ret); + return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; + } + + return reply.ReadInt32(); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn new file mode 100644 index 00000000..eac5395e --- /dev/null +++ b/interfaces/kits/napi/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +ohos_shared_library("hardwaremanager") { + include_dirs = [ + "include", + "${common_path}/log/include", + "${common_path}/utils/include", + "${innerkits_path}/include", + "${innerkits_path}/include/ipc", + "${services_path}/distributedhardwarefwkservice/include/componentmanager", + "${services_path}/distributedhardwarefwkservice/include/lowlatency", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${services_path}/distributedhardwarefwkservice/include/task", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${av_trans_path}/common/include", + "${utils_path}/include/log", + "//third_party/node/src", + "//third_party/json/include", + ] + + sources = [ + "src/native_distributedhardwarefwk_js.cpp", + ] + + deps = [ + "${distributedhardwarefwk_path}/interfaces/inner_kits:libdhfwk_sdk", + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"distributedhardwaremanager_js\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_base", + "ipc:ipc_core", + "napi:ace_napi", + ] + + subsystem_name = "distributedhardware" + relative_install_dir = "module" + part_name = "distributed_hardware_fwk" +} diff --git a/interfaces/kits/napi/include/native_distributedhardwarefwk_js.h b/interfaces/kits/napi/include/native_distributedhardwarefwk_js.h new file mode 100644 index 00000000..e31838e6 --- /dev/null +++ b/interfaces/kits/napi/include/native_distributedhardwarefwk_js.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DH_NATIVE_DISTRIBUTED_HARDWARE_JS_H +#define OHOS_DH_NATIVE_DISTRIBUTED_HARDWARE_JS_H + +#include + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "nlohmann/json.hpp" + +class DistributedHardwareManager { +public: + explicit DistributedHardwareManager(napi_env env, napi_value thisVar); + ~DistributedHardwareManager(); + static napi_value Init(napi_env env, napi_value exports); + static napi_value PauseDistributedHardware(napi_env env, napi_callback_info info); + static napi_value ResumeDistributedHardware(napi_env env, napi_callback_info info); + static napi_value StopDistributedHardware(napi_env env, napi_callback_info info); + static void JsObjectToString(const napi_env &env, const napi_value &object, const std::string &fieldStr, char *dest, + const int32_t destLen); + static void JsObjectToInt(const napi_env &env, const napi_value &object, const std::string &fieldStr, + int32_t &fieldRef); + +private: + static bool IsSystemApp(); +}; +#endif // OHOS_DH_NATIVE_DISTRIBUTED_HARDWARE_JS_H diff --git a/interfaces/kits/napi/src/native_distributedhardwarefwk_js.cpp b/interfaces/kits/napi/src/native_distributedhardwarefwk_js.cpp new file mode 100644 index 00000000..6baba067 --- /dev/null +++ b/interfaces/kits/napi/src/native_distributedhardwarefwk_js.cpp @@ -0,0 +1,376 @@ +/* + * Copyright (c) 2023 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 "native_distributedhardwarefwk_js.h" + +#include "device_type.h" +#include "ipc_skeleton.h" +#include "js_native_api.h" +#include "tokenid_kit.h" +#include "nlohmann/json.hpp" +#include "distributed_hardware_log.h" +#include "distributed_hardware_fwk_kit.h" + +using namespace OHOS::DistributedHardware; + +namespace { +#define GET_PARAMS(env, info, num) \ + size_t argc = num; \ + napi_value argv[num] = {nullptr}; \ + napi_value thisVar = nullptr; \ + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)) + +const int32_t DH_NAPI_ARGS_ONE = 1; +const int32_t DH_NAPI_ARGS_TWO = 2; + +enum DHBussinessErrorCode { + // Permission verify failed. + ERR_NO_PERMISSION = 201, + //The caller is not a system application. + ERR_NOT_SYSTEM_APP = 202, + // Input parameter error. + ERR_INVALID_PARAMS = 401, + // The distributed hardware is not started. + ERR_CODE_DH_NOT_START = 24200101, + // The source device is not connected. + ERR_CODE_DEVICE_NOT_CONNECT = 24200102, +}; + +const std::string ERR_MESSAGE_INVALID_PARAMS = "Input parameter error."; +const std::string ERR_MESSAGE_NO_PERMISSION = "Permission verify failed."; +const std::string ERR_MESSAGE_NOT_SYSTEM_APP = "The caller is not a system application."; +const std::string ERR_MESSAGE_DH_NOT_START = "The distributed hardware is not started."; +const std::string ERR_MESSAGE_DEVICE_NOT_CONNECT = "The source device is not connected."; + +bool CheckArgsType(napi_env env, bool assertion, const std::string ¶mName, const std::string &type) +{ + if (!(assertion)) { + std::string errMsg = ERR_MESSAGE_INVALID_PARAMS + "The type of " + paramName + + " must be " + type; + napi_throw_error(env, std::to_string(ERR_INVALID_PARAMS).c_str(), errMsg.c_str()); + return false; + } + return true; +} + +bool IsFunctionType(napi_env env, napi_value value) +{ + napi_valuetype eventHandleType = napi_undefined; + napi_typeof(env, value, &eventHandleType); + return CheckArgsType(env, eventHandleType == napi_function, "callback", "function"); +} + +napi_value GenerateBusinessError(napi_env env, int32_t err, const std::string &msg) +{ + napi_value businessError = nullptr; + NAPI_CALL(env, napi_create_object(env, &businessError)); + napi_value errorCode = nullptr; + NAPI_CALL(env, napi_create_int32(env, err, &errorCode)); + napi_value errorMessage = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), NAPI_AUTO_LENGTH, &errorMessage)); + NAPI_CALL(env, napi_set_named_property(env, businessError, "code", errorCode)); + NAPI_CALL(env, napi_set_named_property(env, businessError, "message", errorMessage)); + + return businessError; +} + +napi_value CreateErrorForCall(napi_env env, int32_t code, const std::string &errMsg, bool isAsync = true) +{ + DHLOGI("CreateErrorForCall code:%d, message:%s", code, errMsg.c_str()); + napi_value error = nullptr; + if (isAsync) { + napi_throw_error(env, std::to_string(code).c_str(), errMsg.c_str()); + } else { + error = GenerateBusinessError(env, code, errMsg); + } + return error; +} + +napi_value CreateBusinessError(napi_env env, int32_t errCode, bool isAsync = true) +{ + napi_value error = nullptr; + switch (errCode) { + case ERR_DM_NO_PERMISSION: + error = CreateErrorForCall(env, ERR_NO_PERMISSION, ERR_MESSAGE_NO_PERMISSION, isAsync); + break; + case ERR_DM_DISCOVERY_REPEATED: + error = CreateErrorForCall(env, DM_ERR_DISCOVERY_INVALID, ERR_MESSAGE_DISCOVERY_INVALID, isAsync); + break; + case ERR_DM_PUBLISH_REPEATED: + error = CreateErrorForCall(env, DM_ERR_PUBLISH_INVALID, ERR_MESSAGE_PUBLISH_INVALID, isAsync); + break; + case ERR_DM_AUTH_BUSINESS_BUSY: + error = CreateErrorForCall(env, DM_ERR_AUTHENTICALTION_INVALID, + ERR_MESSAGE_AUTHENTICALTION_INVALID, isAsync); + break; + case ERR_DM_INPUT_PARA_INVALID: + case ERR_DM_UNSUPPORTED_AUTH_TYPE: + error = CreateErrorForCall(env, ERR_INVALID_PARAMS, ERR_MESSAGE_INVALID_PARAMS, isAsync); + break; + case ERR_DM_INIT_FAILED: + error = CreateErrorForCall(env, DM_ERR_OBTAIN_SERVICE, ERR_MESSAGE_OBTAIN_SERVICE, isAsync); + break; + case ERR_NOT_SYSTEM_APP: + error = CreateErrorForCall(env, ERR_NOT_SYSTEM_APP, ERR_MESSAGE_NOT_SYSTEM_APP, isAsync); + break; + default: + error = CreateErrorForCall(env, DM_ERR_FAILED, ERR_MESSAGE_FAILED, isAsync); + break; + } + return error; +} +} // namespace + +void DistributedHardwareManager::JsObjectToString(const napi_env &env, const napi_value &object, + const std::string &fieldStr, char *dest, const int32_t destLen) +{ + bool hasProperty = false; + NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty)); + if (hasProperty) { + napi_value field = nullptr; + napi_valuetype valueType = napi_undefined; + + napi_get_named_property(env, object, fieldStr.c_str(), &field); + NAPI_CALL_RETURN_VOID(env, napi_typeof(env, field, &valueType)); + if (!CheckArgsType(env, valueType == napi_string, fieldStr.c_str(), "string")) { + return; + } + size_t result = 0; + NAPI_CALL_RETURN_VOID(env, napi_get_value_string_utf8(env, field, dest, destLen, &result)); + } else { + DHLOGE("devicemanager napi js to str no property: %s", fieldStr.c_str()); + } +} + +void DistributedHardwareManager::JsObjectToInt(const napi_env &env, const napi_value &object, + const std::string &fieldStr, int32_t &fieldRef) +{ + bool hasProperty = false; + NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty)); + if (hasProperty) { + napi_value field = nullptr; + napi_valuetype valueType = napi_undefined; + + napi_get_named_property(env, object, fieldStr.c_str(), &field); + NAPI_CALL_RETURN_VOID(env, napi_typeof(env, field, &valueType)); + if (!CheckArgsType(env, valueType == napi_number, fieldStr.c_str(), "number")) { + return; + } + napi_get_value_int32(env, field, &fieldRef); + } else { + DHLOGE("devicemanager napi js to int no property: %s", fieldStr.c_str()); + } +} + +bool DistributedHardwareManager::IsSystemApp() +{ + uint64_t tokenId = OHOS::IPCSkeleton::GetSelfTokenID(); + return OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId); +} + +napi_value DistributedHardwareManager::PauseDistributedHardware(napi_env env, napi_callback_info info) +{ + DHLOGI("PauseDistributedHardware in"); + if (!IsSystemApp()) { + // CreateBusinessError(env, ERR_NOT_SYSTEM_APP); + return nullptr; + } + napi_value result = nullptr; + size_t argc = 2; + napi_value argv[2] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NAPI_ASSERT(env, ((argc >= DH_NAPI_ARGS_ONE) && (argc <= DH_NAPI_ARGS_TWO)), "requires 1 or 2 parameter"); + + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[0], &valueType); + if (!CheckArgsType(env, valueType == napi_object, "description", "object")) { + return nullptr; + } + int32_t type = -1; + char networkId[96]; + JsObjectToInt(env, argv[0], "type", type); + DHType dhType = static_cast(type); + if (dhType == DHType::AUDIO_MIC || dhType == DHType::AUDIO_SPEAKER) { + dhType = DHType::AUDIO; + } + JsObjectToString(env, argv[0], "srcNetworkId", networkId, sizeof(networkId)); + std::shared_ptr dhFwkKit = std::make_shared(); + if (argc == DH_NAPI_ARGS_ONE) { // promise + napi_deferred deferred; + napi_value promise = 0; + napi_create_promise(env, &deferred, &promise); + int32_t ret = dhFwkKit->PauseDistributedHardware(dhType, std::string(networkId)); + if (ret != 0) { + DHLOGE("PauseDistributedHardware for DHType: %d filed", (uint32_t)dhType); + } + return promise; + } else if (argc == DH_NAPI_ARGS_TWO) { // callback + if (!IsFunctionType(env, argv[1])) { + return nullptr; + } + int32_t ret = dhFwkKit->PauseDistributedHardware(dhType, std::string(networkId)); + if (ret != 0) { + DHLOGE("PauseDistributedHardware for DHType: %d filed", (uint32_t)dhType); + } + } + napi_get_undefined(env, &result); + return result; +} + +napi_value DistributedHardwareManager::ResumeDistributedHardware(napi_env env, napi_callback_info info) +{ + DHLOGI("ResumeDistributedHardware in"); + if (!IsSystemApp()) { + // CreateBusinessError(env, ERR_NOT_SYSTEM_APP); + return nullptr; + } + napi_value result = nullptr; + size_t argc = 2; + napi_value argv[2] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NAPI_ASSERT(env, ((argc >= DH_NAPI_ARGS_ONE) && (argc <= DH_NAPI_ARGS_TWO)), "requires 1 or 2 parameter"); + + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[0], &valueType); + if (!CheckArgsType(env, valueType == napi_object, "description", "object")) { + return nullptr; + } + int32_t type = -1; + char networkId[96]; + JsObjectToInt(env, argv[0], "type", type); + DHType dhType = static_cast(type); + if (dhType == DHType::AUDIO_MIC || dhType == DHType::AUDIO_SPEAKER) { + dhType = DHType::AUDIO; + } + JsObjectToString(env, argv[0], "srcNetworkId", networkId, sizeof(networkId)); + std::shared_ptr dhFwkKit = std::make_shared(); + if (argc == DH_NAPI_ARGS_ONE) { // promise + napi_deferred deferred; + napi_value promise = 0; + napi_create_promise(env, &deferred, &promise); + int32_t ret = dhFwkKit->ResumeDistributedHardware(dhType, std::string(networkId)); + if (ret != 0) { + DHLOGE("ResumeDistributedHardware for DHType: %d filed", (uint32_t)dhType); + } + return promise; + } else if (argc == DH_NAPI_ARGS_TWO) { // callback + if (!IsFunctionType(env, argv[1])) { + return nullptr; + } + int32_t ret = dhFwkKit->ResumeDistributedHardware(dhType, std::string(networkId)); + if (ret != 0) { + DHLOGE("ResumeDistributedHardware for DHType: %d filed", (uint32_t)dhType); + } + } + napi_get_undefined(env, &result); + return result; +} + +napi_value DistributedHardwareManager::StopDistributedHardware(napi_env env, napi_callback_info info) +{ + DHLOGI("StopDistributedHardware in"); + if (!IsSystemApp()) { + // CreateBusinessError(env, ERR_NOT_SYSTEM_APP); + return nullptr; + } + napi_value result = nullptr; + size_t argc = 2; + napi_value argv[2] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NAPI_ASSERT(env, ((argc >= DH_NAPI_ARGS_ONE) && (argc <= DH_NAPI_ARGS_TWO)), "requires 1 or 2 parameter"); + + napi_valuetype valueType = napi_undefined; + napi_typeof(env, argv[0], &valueType); + if (!CheckArgsType(env, valueType == napi_object, "description", "object")) { + return nullptr; + } + int32_t type = -1; + char networkId[96]; + JsObjectToInt(env, argv[0], "type", type); + DHType dhType = static_cast(type); + if (dhType == DHType::AUDIO_MIC || dhType == DHType::AUDIO_SPEAKER) { + dhType = DHType::AUDIO; + } + JsObjectToString(env, argv[0], "srcNetworkId", networkId, sizeof(networkId)); + std::shared_ptr dhFwkKit = std::make_shared(); + if (argc == DH_NAPI_ARGS_ONE) { // promise + napi_deferred deferred; + napi_value promise = 0; + napi_create_promise(env, &deferred, &promise); + int32_t ret = dhFwkKit->StopDistributedHardware(dhType, std::string(networkId)); + if (ret != 0) { + DHLOGE("StopDistributedHardware for DHType: %d filed", (uint32_t)dhType); + } + return promise; + } else if (argc == DH_NAPI_ARGS_TWO) { // callback + if (!IsFunctionType(env, argv[1])) { + return nullptr; + } + int32_t ret = dhFwkKit->StopDistributedHardware(dhType, std::string(networkId)); + if (ret != 0) { + DHLOGE("StopDistributedHardware for DHType: %d filed", (uint32_t)dhType); + } + } + napi_get_undefined(env, &result); + return result; +} + +napi_value DistributedHardwareManager::Init(napi_env env, napi_value exports) +{ + napi_property_descriptor dhmProperties[] = { + DECLARE_NAPI_FUNCTION("pauseDistributedHardware", PauseDistributedHardware), + DECLARE_NAPI_FUNCTION("resumeDistributedHardware", ResumeDistributedHardware), + DECLARE_NAPI_FUNCTION("stopDistributedHardware", StopDistributedHardware), + }; + + DHLOGI("DistributedHardwareManager::Init is called!"); + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(dhmProperties) / sizeof(dhmProperties[0]), + dhmProperties)); + DHLOGI("All functions are configured.."); + return exports; +} + +/* + * Function registering all props and functions of ohos.distributedhardware + */ +static napi_value Export(napi_env env, napi_value exports) +{ + DHLOGI("Export is called!"); + DistributedHardwareManager::Init(env, exports); + return exports; +} + +/* + * module define + */ +static napi_module g_dhModule = {.nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Export, + .nm_modname = "distributedHardware.hardwareManager", + .nm_priv = ((void *)0), + .reserved = {0}}; + +/* + * module register + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + DHLOGI("RegisterModule is called!"); + napi_module_register(&g_dhModule); +} \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index a01a3924..b14e41a9 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -64,6 +64,7 @@ ohos_shared_library("distributedhardwarefwksvr") { "src/componentmanager/component_enable.cpp", "src/componentmanager/component_manager.cpp", "src/componentmanager/component_monitor.cpp", + "src/componentmanager/component_privacy.cpp", "src/distributed_hardware_manager.cpp", "src/distributed_hardware_manager_factory.cpp", "src/distributed_hardware_service.cpp", @@ -111,6 +112,8 @@ ohos_shared_library("distributedhardwarefwksvr") { } external_deps = [ + "ability_base:want", + "ability_runtime:ability_manager", "c_utils:utils", "config_policy:configpolicy_util", "device_manager:devicemanagersdk", diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 79171761..4e066368 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -31,6 +31,7 @@ #include "low_latency_listener.h" #include "monitor_task_timer.h" #include "version_info.h" +#include "component_privacy.h" namespace OHOS { namespace DistributedHardware { @@ -52,6 +53,7 @@ public: void DumpLoadedComps(std::set &compSourceType, std::set &compSinkType); void Recover(DHType dhType); + std::map GetDHSinkInstance(); private: enum class Action : int32_t { @@ -87,9 +89,12 @@ private: std::map compSource_; std::map compSink_; std::map compSrcSaId_; + std::map compSensitiveDesc_; std::shared_ptr compMonitorPtr_ = nullptr; sptr lowLatencyListener_ = nullptr; std::shared_ptr monitorTaskTimer_ = nullptr; + std::shared_ptr audioCompPrivacy_ = nullptr; + std::shared_ptr cameraCompPrivacy_ = nullptr; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_privacy.h b/services/distributedhardwarefwkservice/include/componentmanager/component_privacy.h new file mode 100644 index 00000000..2d39dd8c --- /dev/null +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_privacy.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_COMPONENT_PRIVACY_H +#define OHOS_DISTRIBUTED_HARDWARE_COMPONENT_PRIVACY_H + +#include "idistributed_hardware_sink.h" + +namespace OHOS { +namespace DistributedHardware { +class ComponentPrivacy : public PrivacyResourcesListener { +public: + ComponentPrivacy(); + virtual ~ComponentPrivacy(); + int32_t OnPrivaceResourceMessage(const ResourceEventType &type, const std::string &subType, + const std::string &networkId, bool &isSensitive, bool &isSameAccout) override; + void SetSensitiveResource(bool &isSensitive, bool &isSameAccout); + int32_t OnResourceInfoCallback(const std::string &subType, const std::string &networkId, + bool &isSensitive, bool &isSameAccout); + int32_t StartPrivacePage(const std::string &subType, const std::string &networkId); +private: + std::string DeviceTypeToString(uint16_t deviceTypeId); + + bool isSensitive_ = false; + bool isSameAccout_ = false; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h index 260aaebf..1ef2a454 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_service.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -47,6 +47,9 @@ public: int32_t CreateControlChannel(int32_t engineId, const std::string &peerDevId) override; int32_t NotifyAVCenter(int32_t engineId, const AVTransEvent &event) override; int32_t RegisterCtlCenterCallback(int32_t engineId, const sptr &callback) override; + int32_t PauseDistributedHardware(DHType dhType, const std::string &networkId) override; + int32_t ResumeDistributedHardware(DHType dhType, const std::string &networkId) override; + int32_t StopDistributedHardware(DHType dhType, const std::string &networkId) override; protected: void OnStart() override; diff --git a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h index 79d8a95b..898a2d3e 100644 --- a/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h +++ b/services/distributedhardwarefwkservice/include/distributed_hardware_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -37,6 +37,9 @@ private: int32_t CreateControlChannelInner(MessageParcel &data, MessageParcel &reply); int32_t NotifyAVCenterInner(MessageParcel &data, MessageParcel &reply); int32_t RegisterControlCenterCallbackInner(MessageParcel &data, MessageParcel &reply); + int32_t PauseDistributedHardwareInner(MessageParcel &data, MessageParcel &reply); + int32_t ResumeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply); + int32_t StopDistributedHardwareInner(MessageParcel &data, MessageParcel &reply); bool ValidTopic(uint32_t topic); bool ValidQueryLocalSpec(uint32_t spec); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 9270927a..4fe16b04 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -152,6 +152,7 @@ int32_t ComponentManager::UnInit() compSource_.clear(); compSink_.clear(); + compSensitiveDesc_.clear(); if (monitorTaskTimer_ != nullptr) { monitorTaskTimer_->StopTimer(); @@ -211,6 +212,14 @@ ActionResult ComponentManager::StartSink() auto params = compversion.sinkVersion; auto future = std::async(std::launch::async, [item, params]() { return item.second->InitSink(params); }); futures.emplace(item.first, future.share()); + if (audioCompPrivacy_ == nullptr && item.first == DHType::CAMERA) { + audioCompPrivacy_ = std::make_shared(); + item.second->RegisterPrivacyResources(audioCompPrivacy_); + } + if (cameraCompPrivacy_ == nullptr && item.first == DHType::AUDIO) { + cameraCompPrivacy_ = std::make_shared(); + item.second->RegisterPrivacyResources(cameraCompPrivacy_); + } } return futures; } @@ -232,6 +241,14 @@ ActionResult ComponentManager::StartSink(DHType dhType) return compSink_[dhType]->InitSink(params); }); futures.emplace(dhType, future.share()); + if (audioCompPrivacy_ == nullptr && item.first == DHType::CAMERA) { + audioCompPrivacy_ = std::make_shared(); + compSink_[dhType]->RegisterPrivacyResources(audioCompPrivacy_); + } + if (cameraCompPrivacy_ == nullptr && item.first == DHType::AUDIO) { + cameraCompPrivacy_ = std::make_shared(); + compSink_[dhType]->RegisterPrivacyResources(cameraCompPrivacy_); + } return futures; } @@ -606,5 +623,10 @@ void ComponentManager::RecoverDistributedHardware(DHType dhType) TaskExecutor::GetInstance().PushTask(task); } } + +std::map ComponentManager::GetDHSinkInstance() +{ + return compSink_; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp new file mode 100644 index 00000000..8be69669 --- /dev/null +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2023 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 "component_privacy.h" + +#include "ability_manager_client.h" +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "device_manager.h" +#include "dm_device_info.h" +#include "device_type.h" + +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "ComponentPrivacy" + +ComponentPrivacy::ComponentPrivacy() +{ + DHLOGI("ComponentPrivacy dtor."); +} + +ComponentPrivacy::~ComponentPrivacy() +{ + DHLOGI("ComponentPrivacy dtor."); +} + +int32_t ComponentPrivacy::OnPrivaceResourceMessage(const ResourceEventType &type, const std::string &subType, + const std::string &networkId, bool &isSensitive, bool &isSameAccout) +{ + DHLOGI("OnPrivaceResourceMessage start."); + int32_t ret = DH_FWK_SUCCESS; + if (type == ResourceEventType::EVENT_TYPE_QUERY_RESOURCE) { + ret = OnResourceInfoCallback(subType, networkId, isSensitive, isSameAccout); + } + if (type == ResourceEventType::EVENT_TYPE_PULL_UP_PAGE) { + ret = StartPrivacePage(subType, networkId); + } + return ret; +} + +int32_t ComponentPrivacy::OnResourceInfoCallback(const std::string &subType, const std::string &networkId, + bool &isSensitive, bool &isSameAccout) +{ + DHLOGI("OnResourceInfoCallback start."); + isSensitive = isSensitive_; + isSameAccout = isSameAccout_; + return DH_FWK_SUCCESS; +} + +int32_t ComponentPrivacy::StartPrivacePage(const std::string &subType, const std::string &networkId) +{ + DHLOGI("StartPrivacePage start."); + DmDeviceInfo deviceInfo; + DeviceManager::GetInstance().GetDeviceInfo(DH_FWK_PKG_NAME, networkId, deviceInfo); + std::string deviceName = std::string(deviceInfo.deviceName); + uint16_t deviceTypeId = deviceInfo.deviceTypeId; + std::string deviceType = DeviceTypeToString(deviceTypeId); + const std::string bundleName = "com.ohos.privacyview"; + const std::string abilityName = "PrivacyViewAbility"; + int32_t type = -1; + if (subType == "AUDIO_MIC") { + type = static_cast(DHType::AUDIO_MIC); + } else if (subType == "CAMERA") { + type = static_cast(DHType::CAMERA); + } + AAFwk::Want want; + want.SetElementName(bundleName, abilityName); + want.SetParam("type", type); + want.SetParam("srcNetworkId", networkId); + want.SetParam("deviceName", deviceName); + want.SetParam("deviceType", deviceType); + auto abilityManager = AAFwk::AbilityManagerClient::GetInstance(); + if (abilityManager == nullptr) { + DHLOGE("AbilityManagerClient is nullptr."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + int32_t result = abilityManager->StartAbility(want); + DHLOGI("performance time: StartPrivacePage result = %d", result); + return result; +} + +void ComponentPrivacy::SetSensitiveResource(bool &isSensitive, bool &isSameAccout) +{ + isSensitive_ = isSensitive; + isSameAccout_ = isSameAccout; +} + +std::string ComponentPrivacy::DeviceTypeToString(uint16_t deviceTypeId) +{ + DHLOGI("DeviceTypeToString start."); + DmDeviceType deviceType = static_cast(deviceTypeId); + switch (deviceType) { + case DmDeviceType::DEVICE_TYPE_WIFI_CAMERA: + return "camera"; + case DmDeviceType::DEVICE_TYPE_AUDIO: + return "audio"; + case DmDeviceType::DEVICE_TYPE_PC: + return "pc"; + case DmDeviceType::DEVICE_TYPE_PHONE: + return "phone"; + case DmDeviceType::DEVICE_TYPE_PAD: + return "pad"; + case DmDeviceType::DEVICE_TYPE_WATCH: + return "watch"; + case DmDeviceType::DEVICE_TYPE_CAR: + return "car"; + case DmDeviceType::DEVICE_TYPE_TV: + return "tv"; + case DmDeviceType::DEVICE_TYPE_SMART_DISPLAY: + return "display"; + case DmDeviceType::DEVICE_TYPE_2IN1: + return "2in1"; + default: + return "unknown"; + } +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index 24cb7a2e..af92834a 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -28,6 +28,7 @@ #include "access_manager.h" #include "av_trans_control_center.h" #include "capability_info_manager.h" +#include "component_manager.h" #include "dh_context.h" #include "dh_utils_tool.h" #include "dh_utils_hisysevent.h" @@ -230,5 +231,50 @@ int DistributedHardwareService::Dump(int32_t fd, const std::vector sinkMap = ComponentManager::GetInstance().GetDHSinkInstance(); + if (sinkMap.find(dhType) == sinkMap.end()) { + DHLOGE("PauseDistributedHardware for DHType: %d not init sink handler", (uint32_t)dhType); + return ERR_DH_FWK_PARA_INVALID; + } + int32_t ret = sinkMap[dhType]->PauseDistributedHardware(networkId); + if (ret != 0) { + DHLOGE("PauseDistributedHardware for DHType: %d failed, ret: %d", (uint32_t)dhType, ret); + return ret; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::ResumeDistributedHardware(DHType dhType, const std::string &networkId) +{ + std::map sinkMap = ComponentManager::GetInstance().GetDHSinkInstance(); + if (sinkMap.find(dhType) == sinkMap.end()) { + DHLOGE("ResumeDistributedHardware for DHType: %d not init sink handler", (uint32_t)dhType); + return ERR_DH_FWK_PARA_INVALID; + } + int32_t ret = sinkMap[dhType]->ResumeDistributedHardware(networkId); + if (ret != 0) { + DHLOGE("ResumeDistributedHardware for DHType: %d failed, ret: %d", (uint32_t)dhType, ret); + return ret; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareService::StopDistributedHardware(DHType dhType, const std::string &networkId) +{ + std::map sinkMap = ComponentManager::GetInstance().GetDHSinkInstance(); + if (sinkMap.find(dhType) == sinkMap.end()) { + DHLOGE("StopDistributedHardware for DHType: %d not init sink handler", (uint32_t)dhType); + return ERR_DH_FWK_PARA_INVALID; + } + int32_t ret = sinkMap[dhType]->StopDistributedHardware(networkId); + if (ret != 0) { + DHLOGE("StopDistributedHardware for DHType: %d failed, ret: %d", (uint32_t)dhType, ret); + return ret; + } + return DH_FWK_SUCCESS; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp index 6a268097..a1225339 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp @@ -63,6 +63,15 @@ int32_t DistributedHardwareStub::OnRemoteRequest(uint32_t code, MessageParcel &d case static_cast(DHMsgInterfaceCode::QUERY_LOCAL_SYS_SPEC): { return QueryLocalSysSpecInner(data, reply); } + case static_cast(DHMsgInterfaceCode::PAUSE_DISTRIBUTED_HARDWARE): { + return PauseDistributedHardwareInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::RESUME_DISTRIBUTED_HARDWARE): { + return ResumeDistributedHardwareInner(data, reply); + } + case static_cast(DHMsgInterfaceCode::STOP_DISTRIBUTED_HARDWARE): { + return StopDistributedHardwareInner(data, reply); + } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } @@ -232,5 +241,41 @@ bool DistributedHardwareStub::ValidQueryLocalSpec(uint32_t spec) } return true; } + +int32_t DistributedHardwareStub::PauseDistributedHardwareInner(MessageParcel &data, MessageParcel &reply) +{ + DHType dhType = static_cast(data.ReadInt32()); + std::string networkId = data.ReadString(); + int32_t ret = PauseDistributedHardware(dhType, networkId); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::ResumeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply) +{ + DHType dhType = static_cast(data.ReadInt32()); + std::string networkId = data.ReadString(); + int32_t ret = ResumeDistributedHardware(dhType, networkId); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t DistributedHardwareStub::StopDistributedHardwareInner(MessageParcel &data, MessageParcel &reply) +{ + DHType dhType = static_cast(data.ReadInt32()); + std::string networkId = data.ReadString(); + int32_t ret = StopDistributedHardware(dhType, networkId); + if (!reply.WriteInt32(ret)) { + DHLOGE("Write ret code failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + return DH_FWK_SUCCESS; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee