1 /*
2 * Copyright (c) 2024-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_query.h"
17
18 #include "bundle_manager_utils.h"
19 #include "cJSON.h"
20 #include "cjson_serializer.h"
21 #include "edm_log.h"
22
23 namespace OHOS {
24 namespace EDM {
25 const char* const EMPTY_OBJECT_STRING = "{}";
GetPolicyName()26 std::string SetBrowserPoliciesQuery::GetPolicyName()
27 {
28 return PolicyName::POLICY_SET_BROWSER_POLICIES;
29 }
30
GetPermission(IPlugin::PermissionType,const std::string & permissionTag)31 std::string SetBrowserPoliciesQuery::GetPermission(IPlugin::PermissionType, const std::string &permissionTag)
32 {
33 return "";
34 }
35
QueryPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)36 ErrCode SetBrowserPoliciesQuery::QueryPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
37 int32_t userId)
38 {
39 EDMLOGI("SetBrowserPoliciesQuery query policy");
40 std::string appId = data.ReadString();
41 if (appId.empty() && FAILED(BundleManagerUtils::GetAppIdByCallingUid(appId))) {
42 reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
43 return EdmReturnErrCode::PARAM_ERROR;
44 }
45 if (policyData.empty()) {
46 policyData = EMPTY_OBJECT_STRING;
47 }
48 cJSON *policies = nullptr;
49 auto serializer = CjsonSerializer::GetInstance();
50 if (!serializer->Deserialize(policyData, policies)) {
51 EDMLOGE("SetBrowserPoliciesQuery OnGetPolicy Deserialize error!");
52 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
53 }
54 std::string retString;
55 cJSON *policy = cJSON_GetObjectItem(policies, appId.c_str());
56 if (policy != nullptr) {
57 serializer->Serialize(policy, retString);
58 }
59 reply.WriteInt32(ERR_OK);
60 reply.WriteString(retString);
61 cJSON_Delete(policies);
62 return ERR_OK;
63 }
64 } // namespace EDM
65 } // namespace OHOS