1 /*
2 * Copyright (c) 2023-2024 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 "cjson_check.h"
20 #include "cjson_serializer.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "edm_constants.h"
24 #include "edm_ipc_interface_code.h"
25 #include "map_string_serializer.h"
26 #include "want.h"
27 #include "plugin_manager.h"
28
29 static constexpr int32_t SET_POLICY_PARAM_NUM = 3;
30 static constexpr int32_t SET_POLICY_APPID_INDEX = 0;
31 static constexpr int32_t SET_POLICY_POLICY_NAME_INDEX = 1;
32 static constexpr int32_t SET_POLICY_POLICY_VALUE_INDEX = 2;
33
34 namespace OHOS {
35 namespace EDM {
36 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(std::make_shared<SetBrowserPoliciesPlugin>());
37 const char* const BROWSER_POLICY_CHANGED_EVENT = "com.ohos.edm.browserpolicychanged";
38
SetBrowserPoliciesPlugin()39 SetBrowserPoliciesPlugin::SetBrowserPoliciesPlugin()
40 {
41 policyCode_ = EdmInterfaceCode::SET_BROWSER_POLICIES;
42 policyName_ = "set_browser_policies";
43 permissionMap_.insert(std::make_pair(
44 FuncOperateType::SET, IPlugin::PolicyPermissionConfig("ohos.permission.ENTERPRISE_SET_BROWSER_POLICY",
45 IPlugin::PermissionType::SUPER_DEVICE_ADMIN, IPlugin::ApiType::PUBLIC)));
46 permissionMap_.insert(std::make_pair(
47 FuncOperateType::GET, IPlugin::PolicyPermissionConfig("", IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
48 IPlugin::ApiType::PUBLIC)));
49 needSave_ = true;
50 }
51
OnHandlePolicy(std::uint32_t funcCode,MessageParcel & data,MessageParcel & reply,HandlePolicyData & policyData,int32_t userId)52 ErrCode SetBrowserPoliciesPlugin::OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply,
53 HandlePolicyData &policyData, int32_t userId)
54 {
55 EDMLOGD("SetBrowserPoliciesPlugin OnHandlePolicy.");
56 std::string beforeHandle = policyData.policyData;
57 std::string afterHandle;
58 ErrCode errCode = ERR_OK;
59 int32_t type = data.ReadInt32();
60 if (type == EdmConstants::SET_POLICY_TYPE) {
61 std::vector<std::string> params;
62 data.ReadStringVector(¶ms);
63 if (params.size() < SET_POLICY_PARAM_NUM) {
64 EDMLOGD("SetBrowserPolicyPlugin param invalid.");
65 return EdmReturnErrCode::PARAM_ERROR;
66 }
67 std::string appid = params[SET_POLICY_APPID_INDEX];
68 std::string policyName = params[SET_POLICY_POLICY_NAME_INDEX];
69 std::string policyValue = params[SET_POLICY_POLICY_VALUE_INDEX];
70 if (appid.empty()) {
71 EDMLOGD("SetBrowserPolicyPlugin param invalid.");
72 return EdmReturnErrCode::PARAM_ERROR;
73 }
74
75 if (beforeHandle.empty()) {
76 beforeHandle = "{}";
77 }
78
79 if (policyName.empty()) {
80 errCode = SetRootPolicy(beforeHandle, appid, policyValue, afterHandle);
81 } else {
82 errCode = SetPolicy(beforeHandle, appid, policyName, policyValue, afterHandle);
83 }
84 } else if (type == EdmConstants::SET_POLICIES_TYPE) {
85 auto serializer_ = MapStringSerializer::GetInstance();
86 std::map<std::string, std::string> policies;
87 std::map<std::string, std::string> policyDataMap;
88 serializer_->GetPolicy(data, policies);
89 serializer_->Deserialize(beforeHandle, policyDataMap);
90 errCode = SetPolicies(policies, policyDataMap);
91 serializer_->Serialize(policyDataMap, afterHandle);
92 } else {
93 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
94 }
95
96 if (errCode != ERR_OK) {
97 return errCode;
98 }
99
100 policyData.isChanged = afterHandle != beforeHandle;
101 if (policyData.isChanged) {
102 policyData.policyData = afterHandle;
103 }
104
105 return errCode;
106 }
107
SetPolicies(std::map<std::string,std::string> & policies,std::map<std::string,std::string> & currentData)108 ErrCode SetBrowserPoliciesPlugin::SetPolicies(std::map<std::string, std::string> &policies,
109 std::map<std::string, std::string> ¤tData)
110 {
111 EDMLOGD("SetBrowserPoliciesPlugin OnSetPolicy.");
112 if (policies.empty()) {
113 EDMLOGD("SetBrowserPoliciesPlugin policies is empty.");
114 return EdmReturnErrCode::PARAM_ERROR;
115 }
116 auto iter = policies.begin();
117 if (iter->second.empty()) {
118 currentData.erase(iter->first);
119 } else {
120 currentData[iter->first] = iter->second;
121 }
122 return ERR_OK;
123 }
124
SetRootPolicy(const std::string policyData,std::string appid,std::string policyValue,std::string & afterHandle)125 ErrCode SetBrowserPoliciesPlugin::SetRootPolicy(const std::string policyData, std::string appid,
126 std::string policyValue, std::string &afterHandle)
127 {
128 cJSON* policies = nullptr;
129 auto serializer_ = CjsonSerializer::GetInstance();
130 if (!serializer_->Deserialize(policyData, policies)) {
131 EDMLOGD("SetBrowserPolicyPlugin parse policies error!");
132 return EdmReturnErrCode::PARAM_ERROR;
133 }
134 cJSON_DeleteItemFromObject(policies, appid.c_str());
135 if (!policyValue.empty()) {
136 cJSON* value = nullptr;
137 if (!serializer_->Deserialize(policyValue, value)) {
138 EDMLOGD("SetBrowserPolicyPlugin parse policyValue error!");
139 cJSON_Delete(policies);
140 return EdmReturnErrCode::PARAM_ERROR;
141 }
142 std::string valueString;
143 serializer_->Serialize(value, valueString);
144 cJSON_Delete(value);
145 cJSON_AddStringToObject(policies, appid.c_str(), valueString.c_str());
146 }
147 serializer_->Serialize(policies, afterHandle);
148 cJSON_Delete(policies);
149 return ERR_OK;
150 }
151
SetPolicy(const std::string policyData,std::string appid,std::string policyName,std::string policyValue,std::string & afterHandle)152 ErrCode SetBrowserPoliciesPlugin::SetPolicy(const std::string policyData, std::string appid, std::string policyName,
153 std::string policyValue, std::string &afterHandle)
154 {
155 cJSON* policies = nullptr;
156 auto serializer_ = CjsonSerializer::GetInstance();
157 if (!serializer_->Deserialize(policyData, policies)) {
158 EDMLOGE("SetBrowserPolicyPlugin parse policies error!");
159 return EdmReturnErrCode::PARAM_ERROR;
160 }
161
162 cJSON* beforeParsedPolicy = cJSON_GetObjectItem(policies, appid.c_str());
163 cJSON* policy = nullptr;
164 if (beforeParsedPolicy == nullptr) {
165 cJSON* appidObj = nullptr;
166 CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(appidObj, EdmReturnErrCode::SYSTEM_ABNORMALLY, policies);
167 if (!cJSON_AddItemToObject(policies, appid.c_str(), appidObj)) {
168 EDMLOGE("SetBrowserPolicyPlugin AddItemToObject appid error!");
169 cJSON_Delete(policies);
170 cJSON_Delete(appidObj);
171 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
172 }
173 CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(policy, EdmReturnErrCode::SYSTEM_ABNORMALLY, policies);
174 } else {
175 std::string beforeParsedPolicyString = cJSON_GetStringValue(beforeParsedPolicy);
176 if (!serializer_->Deserialize(beforeParsedPolicyString, policy)) {
177 EDMLOGE("SetBrowserPolicyPlugin parse policies error!");
178 cJSON_Delete(policies);
179 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
180 }
181 }
182 cJSON_DeleteItemFromObject(policy, policyName.c_str());
183 ErrCode ret = SetPolicyValue(policy, policyName, policyValue);
184 if (ret != ERR_OK) {
185 cJSON_Delete(policies);
186 cJSON_Delete(policy);
187 return ret;
188 }
189 std::string policyString;
190 serializer_->Serialize(policy, policyString);
191 cJSON_ReplaceItemInObject(policies, appid.c_str(), cJSON_CreateString(policyString.c_str()));
192 serializer_->Serialize(policies, afterHandle);
193 cJSON_Delete(policies);
194 cJSON_Delete(policy);
195 return ERR_OK;
196 }
197
SetPolicyValue(cJSON * policy,std::string policyName,std::string policyValue)198 ErrCode SetBrowserPoliciesPlugin::SetPolicyValue(cJSON* policy, std::string policyName,
199 std::string policyValue)
200 {
201 if (!policyValue.empty()) {
202 cJSON* value = nullptr;
203 if (!CjsonSerializer::GetInstance()->Deserialize(policyValue, value)) {
204 EDMLOGE("SetBrowserPolicyPlugin parse policyValue error!");
205 return EdmReturnErrCode::PARAM_ERROR;
206 }
207 if (!cJSON_AddItemToObject(policy, policyName.c_str(), value)) {
208 EDMLOGE("SetBrowserPolicyPlugin AddItemToObject value error!");
209 cJSON_Delete(value);
210 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
211 }
212 }
213 return ERR_OK;
214 }
215
OnHandlePolicyDone(std::uint32_t funcCode,const std::string & adminName,bool isGlobalChanged,int32_t userId)216 void SetBrowserPoliciesPlugin::OnHandlePolicyDone(std::uint32_t funcCode, const std::string &adminName,
217 bool isGlobalChanged, int32_t userId)
218 {
219 if (!isGlobalChanged) {
220 return;
221 }
222 NotifyBrowserPolicyChanged();
223 }
224
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)225 ErrCode SetBrowserPoliciesPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
226 int32_t userId)
227 {
228 auto mapStringSerilizer_ = MapStringSerializer::GetInstance();
229 std::map<std::string, std::string> policies;
230 mapStringSerilizer_->Deserialize(policyData, policies);
231 std::string appId = data.ReadString();
232 if (appId.empty() && FAILED(BundleManagerUtils::GetAppIdByCallingUid(appId))) {
233 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
234 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
235 }
236 reply.WriteInt32(ERR_OK);
237 std::string policy;
238 if (policies.count(appId) > 0) {
239 policy = policies[appId];
240 }
241 reply.WriteString(policy);
242 return ERR_OK;
243 }
244
NotifyBrowserPolicyChanged()245 void SetBrowserPoliciesPlugin::NotifyBrowserPolicyChanged()
246 {
247 EDMLOGD("SetBrowserPoliciesPlugin NotifyBrowserPolicyChanged.");
248 AAFwk::Want want;
249 want.SetAction(BROWSER_POLICY_CHANGED_EVENT);
250 EventFwk::CommonEventData eventData;
251 eventData.SetWant(want);
252 if (!EventFwk::CommonEventManager::PublishCommonEvent(eventData)) {
253 EDMLOGE("NotifyBrowserPolicyChanged failed.");
254 }
255 }
256 } // namespace EDM
257 } // namespace OHOS
258