• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "managed_browser_policy_serializer.h"
17 
18 #include "cJSON.h"
19 #include "cjson_check.h"
20 
21 namespace OHOS {
22 namespace EDM {
23 
24 const char* const VERSION = "version";
25 const char* const POLICY_NAMES = "policyNames";
26 
Deserialize(const std::string & policy,std::map<std::string,ManagedBrowserPolicyType> & dataObj)27 bool ManagedBrowserPolicySerializer::Deserialize(const std::string &policy,
28     std::map<std::string, ManagedBrowserPolicyType> &dataObj)
29 {
30     if (policy.empty()) {
31         return true;
32     }
33     cJSON* root = cJSON_Parse(policy.c_str());
34     if (root == nullptr) {
35         return false;
36     }
37     cJSON* mapItem;
38     cJSON_ArrayForEach(mapItem, root) {
39         cJSON* version = cJSON_GetObjectItem(mapItem, VERSION);
40         cJSON* policyNames = cJSON_GetObjectItem(mapItem, POLICY_NAMES);
41         if (!cJSON_IsArray(policyNames) || !cJSON_IsNumber(version)) {
42             cJSON_Delete(root);
43             return false;
44         }
45         std::vector<std::string> policyVector;
46         cJSON* vectorItem;
47         cJSON_ArrayForEach(vectorItem, policyNames) {
48             char* policyValue = cJSON_GetStringValue(vectorItem);
49             if (policyValue == nullptr) {
50                 cJSON_Delete(root);
51                 return false;
52             }
53             policyVector.push_back(std::string(policyValue));
54         }
55         dataObj[mapItem->string].policyNames = policyVector;
56         dataObj[mapItem->string].version = version->valueint;
57     }
58     cJSON_Delete(root);
59     return true;
60 }
61 
Serialize(const std::map<std::string,ManagedBrowserPolicyType> & dataObj,std::string & policy)62 bool ManagedBrowserPolicySerializer::Serialize(const std::map<std::string, ManagedBrowserPolicyType> &dataObj,
63     std::string &policy)
64 {
65     cJSON* root = nullptr;
66     CJSON_CREATE_OBJECT_AND_CHECK(root, false);
67     for (auto& mapIt : dataObj) {
68         cJSON* policyObject = nullptr;
69         CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(policyObject, false, root);
70         cJSON_AddNumberToObject(policyObject, VERSION, mapIt.second.version);
71         cJSON* array = cJSON_CreateArray();
72         if (array == nullptr) {
73             cJSON_Delete(root);
74             cJSON_Delete(policyObject);
75             return false;
76         }
77         for (auto& vectorIt : mapIt.second.policyNames) {
78             cJSON* policyValue = cJSON_CreateString(vectorIt.c_str());
79             if (policyValue == nullptr) {
80                 cJSON_Delete(root);
81                 cJSON_Delete(policyObject);
82                 cJSON_Delete(array);
83                 return false;
84             }
85             cJSON_AddItemToArray(array, policyValue);
86         }
87         if (!cJSON_AddItemToObject(policyObject, POLICY_NAMES, array)) {
88             cJSON_Delete(root);
89             cJSON_Delete(policyObject);
90             cJSON_Delete(array);
91             return false;
92         }
93         if (!cJSON_AddItemToObject(root, mapIt.first.c_str(), policyObject)) {
94             cJSON_Delete(root);
95             cJSON_Delete(array);
96             return false;
97         }
98     }
99     char* jsonStr = cJSON_Print(root);
100     if (jsonStr == nullptr) {
101         cJSON_Delete(root);
102         return false;
103     }
104     policy = std::string(jsonStr);
105     cJSON_free(jsonStr);
106     cJSON_Delete(root);
107     return true;
108 }
109 
GetPolicy(MessageParcel & data,std::map<std::string,ManagedBrowserPolicyType> & result)110 bool ManagedBrowserPolicySerializer::GetPolicy(MessageParcel &data,
111     std::map<std::string, ManagedBrowserPolicyType> &result)
112 {
113     return true;
114 }
115 
WritePolicy(MessageParcel & reply,std::map<std::string,ManagedBrowserPolicyType> & result)116 bool ManagedBrowserPolicySerializer::WritePolicy(MessageParcel &reply,
117     std::map<std::string, ManagedBrowserPolicyType> &result)
118 {
119     return true;
120 }
121 
MergePolicy(std::vector<std::map<std::string,ManagedBrowserPolicyType>> & data,std::map<std::string,ManagedBrowserPolicyType> & result)122 bool ManagedBrowserPolicySerializer::MergePolicy(std::vector<std::map<std::string, ManagedBrowserPolicyType>> &data,
123     std::map<std::string, ManagedBrowserPolicyType> &result)
124 {
125     return true;
126 }
127 } // namespace EDM
128 } // namespace OHOS