diff --git a/interfaces/inner_api/browser/include/browser_proxy.h b/interfaces/inner_api/browser/include/browser_proxy.h index 17691a045ff7bdf1d86ae7c36a17b83ff9e95d19..ccd3ada529ecf74b81c3a3f49baa422ed3ced0bb 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 0c2e42735102afd94559277d42eb85d50f62ace1..b03cb94b545c6d1d880896e314f5d7053412d168 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 5b21469ea60bcd52db517ab1e52473a40cea03d8..b43acd518987eae22875a24dc7cbae6757aee0cc 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 e5251b3ab91ae90b547f48347167221610374c96..741af34299456efe5791b3ec43a44b0efa4a0625 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 3956d686b5305c94da57b6e060beabf0307643f0..1788ef332cb147df32948eb85e96ba81fb9d60a0 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 0000000000000000000000000000000000000000..b2020b4bb6aeb26a1a5980401658b9080e30e73d --- /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 0000000000000000000000000000000000000000..8092dc7b49ff09778b3a3847896f41830bad1bb8 --- /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