1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "disable_hdc_plugin.h"
17
18 #include "bool_serializer.h"
19 #include "edm_constants.h"
20 #include "edm_ipc_interface_code.h"
21 #include "parameters.h"
22 #include "plugin_manager.h"
23
24 namespace OHOS {
25 namespace EDM {
26 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(DisableHdcPlugin::GetPlugin());
27 const std::string PERSIST_HDC_CONTROL = "persist.hdc.control";
28
InitPlugin(std::shared_ptr<IPluginTemplate<DisableHdcPlugin,bool>> ptr)29 void DisableHdcPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<DisableHdcPlugin, bool>> ptr)
30 {
31 EDMLOGI("DisableHdcPlugin InitPlugin...");
32 std::map<std::string, std::map<IPlugin::PermissionType, std::string>> tagPermissions;
33 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag11;
34 std::map<IPlugin::PermissionType, std::string> typePermissionsForTag12;
35 typePermissionsForTag11.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
36 "ohos.permission.ENTERPRISE_RESTRICT_POLICY");
37 typePermissionsForTag12.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
38 "ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS");
39 typePermissionsForTag12.emplace(IPlugin::PermissionType::BYOD_DEVICE_ADMIN,
40 "ohos.permission.PERSONAL_MANAGE_RESTRICTIONS");
41 tagPermissions.emplace(EdmConstants::PERMISSION_TAG_VERSION_11, typePermissionsForTag11);
42 tagPermissions.emplace(EdmConstants::PERMISSION_TAG_VERSION_12, typePermissionsForTag12);
43
44 IPlugin::PolicyPermissionConfig config = IPlugin::PolicyPermissionConfig(tagPermissions, IPlugin::ApiType::PUBLIC);
45 ptr->InitAttribute(EdmInterfaceCode::DISABLED_HDC, "disabled_hdc", config, true);
46 ptr->SetSerializer(BoolSerializer::GetInstance());
47 ptr->SetOnHandlePolicyListener(&DisableHdcPlugin::OnSetPolicy, FuncOperateType::SET);
48 ptr->SetOnAdminRemoveListener(&DisableHdcPlugin::OnAdminRemove);
49 }
50
OnSetPolicy(bool & data)51 ErrCode DisableHdcPlugin::OnSetPolicy(bool &data)
52 {
53 EDMLOGI("DisableHdcPlugin OnSetPolicy %{public}d", data);
54 std::string value = data ? "false" : "true";
55 return system::SetParameter(PERSIST_HDC_CONTROL, value) ? ERR_OK : EdmReturnErrCode::SYSTEM_ABNORMALLY;
56 }
57
OnAdminRemove(const std::string & adminName,bool & data,int32_t userId)58 ErrCode DisableHdcPlugin::OnAdminRemove(const std::string &adminName, bool &data, int32_t userId)
59 {
60 EDMLOGI("DisableHdcPlugin OnAdminRemove %{public}d...", data);
61 if (!data) {
62 return ERR_OK;
63 }
64 bool reset = false;
65 return OnSetPolicy(reset);
66 }
67
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)68 ErrCode DisableHdcPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
69 int32_t userId)
70 {
71 bool ret = system::GetBoolParameter(PERSIST_HDC_CONTROL, true);
72 reply.WriteInt32(ERR_OK);
73 reply.WriteBool(!ret);
74 return ERR_OK;
75 }
76 } // namespace EDM
77 } // namespace OHOS
78