From 9bfc1437752b73e1ad29a2b8d63939003b1f46e4 Mon Sep 17 00:00:00 2001 From: huangxiao <1286409928@qq.com> Date: Tue, 6 Feb 2024 16:52:02 +0800 Subject: [PATCH] =?UTF-8?q?[EDM]=E8=AE=BE=E7=BD=AE=E6=B5=8F=E8=A7=88?= =?UTF-8?q?=E5=99=A8=E5=8D=95=E4=B8=AA=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inner_api/browser/include/browser_proxy.h | 2 + .../inner_api/browser/src/browser_proxy.cpp | 19 ++++ .../common/include/edm_ipc_interface_code.h | 1 + .../kits/browser/include/browser_addon.h | 1 + interfaces/kits/browser/src/browser_addon.cpp | 34 ++++++ .../include/set_browser_policy_plugin.h | 37 +++++++ .../src/set_browser_policy_plugin.cpp | 102 ++++++++++++++++++ 7 files changed, 196 insertions(+) create mode 100644 services/edm_plugin/include/set_browser_policy_plugin.h create mode 100644 services/edm_plugin/src/set_browser_policy_plugin.cpp diff --git a/interfaces/inner_api/browser/include/browser_proxy.h b/interfaces/inner_api/browser/include/browser_proxy.h index 17691a045..ccd3ada52 100644 --- a/interfaces/inner_api/browser/include/browser_proxy.h +++ b/interfaces/inner_api/browser/include/browser_proxy.h @@ -26,6 +26,8 @@ public: int32_t SetPolicies(const AppExecFwk::ElementName &admin, const std::string &appId, const std::string &policies); int32_t GetPolicies(AppExecFwk::ElementName &admin, const std::string &appId, std::string &policies); int32_t GetPolicies(std::string &policies); + int32_t SetPolicy(const AppExecFwk::ElementName &admin, const std::string &appId, const std::string &policyName, + const std::string &policyValue); private: int32_t GetPolicies(AppExecFwk::ElementName *admin, const std::string &appId, std::string &policies); diff --git a/interfaces/inner_api/browser/src/browser_proxy.cpp b/interfaces/inner_api/browser/src/browser_proxy.cpp index 0c2e42735..b03cb94b5 100644 --- a/interfaces/inner_api/browser/src/browser_proxy.cpp +++ b/interfaces/inner_api/browser/src/browser_proxy.cpp @@ -102,5 +102,24 @@ int32_t BrowserProxy::GetPolicies(AppExecFwk::ElementName *admin, const std::str reply.ReadString(policies); return ERR_OK; } + +int32_t BrowserProxy::SetPolicy(const AppExecFwk::ElementName &admin, const std::string &appId, + const std::string &policyName, const std::string &policyValue) +{ + EDMLOGI("BrowserProxy::SetPolicy"); + if (appId.empty()) { + EDMLOGE("BrowserProxy::SetPolicy appId is empty"); + return EdmReturnErrCode::PARAM_ERROR; + } + MessageParcel data; + std::uint32_t funcCode = + POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_BROWSER_POLICY); + data.WriteInterfaceToken(DESCRIPTOR); + data.WriteInt32(WITHOUT_USERID); + data.WriteParcelable(&admin); + std::vector params{appId, policyName, policyValue}; + data.WriteStringVector(params); + return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data); +} } // namespace EDM } // namespace OHOS diff --git a/interfaces/inner_api/common/include/edm_ipc_interface_code.h b/interfaces/inner_api/common/include/edm_ipc_interface_code.h index 5b21469ea..b43acd518 100644 --- a/interfaces/inner_api/common/include/edm_ipc_interface_code.h +++ b/interfaces/inner_api/common/include/edm_ipc_interface_code.h @@ -83,6 +83,7 @@ enum EdmInterfaceCode : uint32_t { DISABLE_MICROPHONE = 1047, DISABLE_BLUETOOTH = 1048, FINGERPRINT_AUTH = 1049, + SET_BROWSER_POLICY = 1050, POLICY_CODE_END = 3000, }; } // namespace EDM diff --git a/interfaces/kits/browser/include/browser_addon.h b/interfaces/kits/browser/include/browser_addon.h index e5251b3ab..741af3429 100644 --- a/interfaces/kits/browser/include/browser_addon.h +++ b/interfaces/kits/browser/include/browser_addon.h @@ -37,6 +37,7 @@ public: static napi_value SetPolicies(napi_env env, napi_callback_info info); static napi_value GetPolicies(napi_env env, napi_callback_info info); + static napi_value SetPolicy(napi_env env, napi_callback_info info); private: static void NativeSetPolicies(napi_env env, void *data); diff --git a/interfaces/kits/browser/src/browser_addon.cpp b/interfaces/kits/browser/src/browser_addon.cpp index 3956d686b..1788ef332 100644 --- a/interfaces/kits/browser/src/browser_addon.cpp +++ b/interfaces/kits/browser/src/browser_addon.cpp @@ -133,6 +133,40 @@ void BrowserAddon::NativeGetPolicies(napi_env env, void *data) asyncCallbackInfo->appId, asyncCallbackInfo->stringRet); } +napi_value BrowserAddon::SetPolicy(napi_env env, napi_callback_info info) +{ + EDMLOGI("NAPI_SetPolicies called"); + size_t argc = ARGS_SIZE_FOUR; + napi_value argv[ARGS_SIZE_FOUR] = {nullptr}; + napi_value thisArg = nullptr; + void *data = nullptr; + OHOS::AppExecFwk::ElementName elementName; + std::string appId; + std::string policyName; + std::string policyValue; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data)); + ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_FOUR, "Parameter count error"); + bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object) && + MatchValueType(env, argv[ARR_INDEX_ONE], napi_string) && MatchValueType(env, argv[ARR_INDEX_TWO], napi_string) && + MatchValueType(env, argv[ARR_INDEX_THREE], napi_string); + ASSERT_AND_THROW_PARAM_ERROR(env, matchFlag, "parameter type error"); + ASSERT_AND_THROW_PARAM_ERROR(env, ParseElementName(env, elementName, argv[ARR_INDEX_ZERO]), "element name param error"); + ASSERT_AND_THROW_PARAM_ERROR(env, ParseString(env, appId, argv[ARR_INDEX_ONE]), "Parameter appId error"); + ASSERT_AND_THROW_PARAM_ERROR(env, ParseString(env, policyName, argv[ARR_INDEX_TWO]), "Parameter policyName error"); + ASSERT_AND_THROW_PARAM_ERROR(env, ParseString(env, policyValue, argv[ARR_INDEX_THREE]), "Parameter policyValue error"); + + EDMLOGD( + "EnableAdmin: elementName.bundlename %{public}s, " + "elementName.abilityname:%{public}s", + elementName.GetBundleName().c_str(), + elementName.GetAbilityName().c_str()); + int32_t retCode = BrowserProxy::GetBrowserProxy()->SetPolicy(admin, appId, policyName, policyValue); + if (FAILED(retCode)) { + napi_throw(env, CreateError(env, retCode)); + } + return nullptr; +} + static napi_module g_browserModule = { .nm_version = 1, .nm_flags = 0, diff --git a/services/edm_plugin/include/set_browser_policy_plugin.h b/services/edm_plugin/include/set_browser_policy_plugin.h new file mode 100644 index 000000000..b2020b4bb --- /dev/null +++ b/services/edm_plugin/include/set_browser_policy_plugin.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SERVICES_EDM_PLUGIN_INCLUDE_SET_BROWSER_POLICIY_PLUGIN_H +#define SERVICES_EDM_PLUGIN_INCLUDE_SET_BROWSER_POLICIY_PLUGIN_H + +#include "iplugin.h" + +namespace OHOS { +namespace EDM { + +class SetBrowserPolicyPlugin : public IPlugin { +public: + SetBrowserPolicyPlugin(); + + ErrCode OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply, + std::string &policyData, bool &isChanged, int32_t userId); + + void OnHandlePolicyDone(std::uint32_t funcCode, const std::string &adminName, bool isGlobalChanged, + int32_t userId); +private: + void NotifyBrowserPolicyChanged(); +} +} +} \ No newline at end of file diff --git a/services/edm_plugin/src/set_browser_policy_plugin.cpp b/services/edm_plugin/src/set_browser_policy_plugin.cpp new file mode 100644 index 000000000..8092dc7b4 --- /dev/null +++ b/services/edm_plugin/src/set_browser_policy_plugin.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "set_browser_policies_plugin.h" + +#include "common_event_manager.h" +#include "common_event_support.h" +#include "edm_ipc_interface_code.h" +#include "ipolicy_manager.h" +#include "iplugin_manager.h" +#include "map_string_serializer.h" +#include "want.h" + +static constexpr int32_t SET_POLICY_PARAM_NUM = 3; +static constexpr int32_t SET_POLICY_APPID_INDEX = 0; +static constexpr int32_t SET_POLICY_POLICY_NAME_INDEX = 1; +static constexpr int32_t SET_POLICY_POLICY_VALUE_INDEX = 2; + +const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(SetBrowserPolicyPlugin::GetPlugin()); +const char* const BROWSER_POLICY_CHANGED_EVENT = "com.ohos.edm.browserpolicychanged"; + +namespace OHOS { +namespace EDM { + +SetBrowserPolicyPlugin::SetBrowserPolicyPlugin() +{ + policyCode_ = EdmInterfaceCode::SET_BROWSER_POLICY; + policyName_ = "set_browser_policies"; + permission_ = "ohos.permission.ENTERPRISE_SET_BROWSER_POLICY"; + permissionType_ = IPlugin::PermissionType::SUPER_DEVICE_ADMIN; + needSave_ = true; +} + +ErrCode OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply, + std::string &policyData, bool &isChanged, int32_t userId) +{ + EDMLOGD("SetBrowserPolicyPlugin OnHandlePolicy."); + std::vector params; + data.ReadStringVector(¶ms); + if (params.size() < SET_POLICY_PARAM_NUM) + { + EDMLOGD("SetBrowserPolicyPlugin param invalid."); + return EdmReturnErrCode::PARAM_ERROR; + } + + std::map policies; + auto serializer_ = MapStringSerializer::GetInstance(); + serializer_->Deserialize(policyData, policies); + if (policies.empty()) { + EDMLOGD("SetBrowserPolicyPlugin policies is empty."); + return EdmReturnErrCode::PARAM_ERROR; + } + std::string handleData = policies[params[SET_POLICY_APPID_INDEX]]; + std::map policy; + serializer_->Deserialize(handleData, policy); + policy[params[SET_POLICY_POLICY_NAME_INDEX]] = params[SET_POLICY_POLICY_VALUE_INDEX]; + + std::string afterHandle; + serializer_->Serialize(policy, afterHandle); + isChanged = afterHandle != handleData; + if (isChanged) + { + policies[params[SET_POLICY_APPID_INDEX]] = afterHandle; + serializer_->Serialize(policies, policyData); + } + return ERR_OK; +} + +void OnHandlePolicyDone(std::uint32_t funcCode, const std::string &adminName, bool isGlobalChanged, + int32_t userId); +{ + if (!isGlobalChanged) { + return; + } + NotifyBrowserPolicyChanged(); +} + +void SetBrowserPoliciesPlugin::NotifyBrowserPolicyChanged() +{ + EDMLOGD("SetBrowserPolicyPlugin NotifyBrowserPolicyChanged."); + AAFwk::Want want; + want.SetAction(BROWSER_POLICY_CHANGED_EVENT); + EventFwk::CommonEventData eventData; + eventData.SetWant(want); + if (!EventFwk::CommonEventManager::PublishCommonEvent(eventData)) { + EDMLOGE("NotifyBrowserPolicyChanged failed."); + } +} +} +} \ No newline at end of file -- Gitee