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::string> perms;
33     perms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_11, "ohos.permission.ENTERPRISE_RESTRICT_POLICY"));
34     perms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_12,
35         "ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS"));
36     IPlugin::PolicyPermissionConfig config = IPlugin::PolicyPermissionConfig(perms,
37         IPlugin::PermissionType::SUPER_DEVICE_ADMIN, IPlugin::ApiType::PUBLIC);
38     ptr->InitAttribute(EdmInterfaceCode::DISABLED_HDC, "disabled_hdc", config, false);
39     ptr->SetSerializer(BoolSerializer::GetInstance());
40     ptr->SetOnHandlePolicyListener(&DisableHdcPlugin::OnSetPolicy, FuncOperateType::SET);
41 }
42 
OnSetPolicy(bool & data)43 ErrCode DisableHdcPlugin::OnSetPolicy(bool &data)
44 {
45     EDMLOGI("DisableHdcPlugin OnSetPolicy %{public}d", data);
46     std::string value = data ? "false" : "true";
47     return system::SetParameter(PERSIST_HDC_CONTROL, value) ? ERR_OK : EdmReturnErrCode::SYSTEM_ABNORMALLY;
48 }
49 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)50 ErrCode DisableHdcPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
51     int32_t userId)
52 {
53     bool ret = system::GetBoolParameter(PERSIST_HDC_CONTROL, true);
54     reply.WriteInt32(ERR_OK);
55     reply.WriteBool(!ret);
56     return ERR_OK;
57 }
58 } // namespace EDM
59 } // namespace OHOS
60