• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "set_browser_policies_plugin.h"
17 
18 #include "bundle_manager_utils.h"
19 #include "edm_ipc_interface_code.h"
20 #include "iplugin_manager.h"
21 #include "map_string_serializer.h"
22 
23 namespace OHOS {
24 namespace EDM {
25 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(SetBrowserPoliciesPlugin::GetPlugin());
26 
InitPlugin(std::shared_ptr<IPluginTemplate<SetBrowserPoliciesPlugin,std::map<std::string,std::string>>> ptr)27 void SetBrowserPoliciesPlugin::InitPlugin(
28     std::shared_ptr<IPluginTemplate<SetBrowserPoliciesPlugin, std::map<std::string, std::string>>> ptr)
29 {
30     EDMLOGD("SetBrowserPoliciesPlugin InitPlugin.");
31     ptr->InitAttribute(EdmInterfaceCode::SET_BROWSER_POLICIES, "set_browser_policies", true);
32     ptr->InitPermission(FuncOperateType::SET, "ohos.permission.ENTERPRISE_SET_BROWSER_POLICY",
33         IPlugin::PermissionType::SUPER_DEVICE_ADMIN);
34     ptr->SetSerializer(MapStringSerializer::GetInstance());
35     ptr->SetOnHandlePolicyListener(&SetBrowserPoliciesPlugin::OnSetPolicy, FuncOperateType::SET);
36 }
37 
OnSetPolicy(std::map<std::string,std::string> & policies,std::map<std::string,std::string> & currentData,int32_t userId)38 ErrCode SetBrowserPoliciesPlugin::OnSetPolicy(std::map<std::string, std::string> &policies,
39     std::map<std::string, std::string> &currentData, int32_t userId)
40 {
41     EDMLOGD("SetBrowserPoliciesPlugin OnSetPolicy.");
42     if (policies.empty()) {
43         EDMLOGD("SetBrowserPoliciesPlugin policies is empty.");
44         return EdmReturnErrCode::PARAM_ERROR;
45     }
46     auto iter = policies.begin();
47     if (iter->second.empty()) {
48         currentData.erase(iter->first);
49     } else {
50         currentData[iter->first] = iter->second;
51     }
52     return ERR_OK;
53 }
54 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)55 ErrCode SetBrowserPoliciesPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
56     int32_t userId)
57 {
58     std::map<std::string, std::string> policies;
59     pluginInstance_->serializer_->Deserialize(policyData, policies);
60     std::string appId = data.ReadString();
61     if (appId.empty() && FAILED(BundleManagerUtils::GetAppIdByCallingUid(appId))) {
62         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
63         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
64     }
65     reply.WriteInt32(ERR_OK);
66     std::string policy;
67     if (policies.count(appId) > 0) {
68         policy = policies[appId];
69     }
70     reply.WriteString(policy);
71     return ERR_OK;
72 }
73 } // namespace EDM
74 } // namespace OHOS
75