1 /*
2 * Copyright (c) 2025 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 "manage_keep_alive_apps_serializer.h"
17
18 #include <map>
19 #include "cJSON.h"
20 #include "cjson_check.h"
21
22 namespace OHOS {
23 namespace EDM {
24
25 const char* const BUNDLE_NAME = "bundleName";
26 const char* const DISALLOW_MODIFY = "disallowModify";
27
SetUnionPolicyData(std::vector<ManageKeepAliveAppInfo> & data,std::vector<ManageKeepAliveAppInfo> & currentData)28 std::vector<ManageKeepAliveAppInfo> ManageKeepAliveAppsSerializer::SetUnionPolicyData(
29 std::vector<ManageKeepAliveAppInfo> &data, std::vector<ManageKeepAliveAppInfo> ¤tData)
30 {
31 std::vector<ManageKeepAliveAppInfo> mergeData;
32 std::map<std::string, ManageKeepAliveAppInfo> mergeMap;
33 for (ManageKeepAliveAppInfo &item : data) {
34 mergeMap[item.GetBundleName()] = item;
35 }
36 for (const ManageKeepAliveAppInfo &item : currentData) {
37 mergeMap[item.GetBundleName()] = item;
38 }
39 for (const auto& pair : mergeMap) {
40 mergeData.push_back(pair.second);
41 }
42 return mergeData;
43 }
44
SetDifferencePolicyData(std::vector<ManageKeepAliveAppInfo> & data,std::vector<ManageKeepAliveAppInfo> & currentData)45 std::vector<ManageKeepAliveAppInfo> ManageKeepAliveAppsSerializer::SetDifferencePolicyData(
46 std::vector<ManageKeepAliveAppInfo> &data, std::vector<ManageKeepAliveAppInfo> ¤tData)
47 {
48 std::vector<ManageKeepAliveAppInfo> mergeData;
49 std::sort(data.begin(), data.end());
50 std::sort(currentData.begin(), currentData.end());
51 std::set_difference(currentData.begin(), currentData.end(), data.begin(), data.end(), back_inserter(mergeData));
52 return mergeData;
53 }
54
SetNeedRemoveMergePolicyData(std::vector<ManageKeepAliveAppInfo> & mergeData,std::vector<ManageKeepAliveAppInfo> & data)55 std::vector<ManageKeepAliveAppInfo> ManageKeepAliveAppsSerializer::SetNeedRemoveMergePolicyData(
56 std::vector<ManageKeepAliveAppInfo> &mergeData, std::vector<ManageKeepAliveAppInfo> &data)
57 {
58 std::vector<ManageKeepAliveAppInfo> removeData;
59 for (const ManageKeepAliveAppInfo &item : data) {
60 if (std::find_if(mergeData.begin(), mergeData.end(), [&item](const ManageKeepAliveAppInfo &mergeItem) {
61 return mergeItem.GetBundleName() == item.GetBundleName();
62 }) == mergeData.end()) {
63 removeData.push_back(item);
64 }
65 }
66 return removeData;
67 }
68
SetIntersectionPolicyData(std::vector<std::string> & data,std::vector<ManageKeepAliveAppInfo> & currentData)69 std::vector<ManageKeepAliveAppInfo> ManageKeepAliveAppsSerializer::SetIntersectionPolicyData(
70 std::vector<std::string> &data, std::vector<ManageKeepAliveAppInfo> ¤tData)
71 {
72 std::vector<ManageKeepAliveAppInfo> mergeData;
73 for (const std::string &dataItem : data) {
74 for (const ManageKeepAliveAppInfo &item : currentData) {
75 if (item.GetBundleName() == dataItem) {
76 mergeData.push_back(item);
77 break;
78 }
79 }
80 }
81 return mergeData;
82 }
83
Deserialize(const std::string & policy,std::vector<ManageKeepAliveAppInfo> & dataObj)84 bool ManageKeepAliveAppsSerializer::Deserialize(const std::string &policy, std::vector<ManageKeepAliveAppInfo> &dataObj)
85 {
86 if (policy.empty()) {
87 return true;
88 }
89 cJSON* root = cJSON_Parse(policy.c_str());
90 if (root == nullptr) {
91 return false;
92 }
93 cJSON* mapItem;
94 cJSON_ArrayForEach(mapItem, root) {
95 cJSON* bundleName = cJSON_GetObjectItem(mapItem, BUNDLE_NAME);
96 cJSON* disallowModify = cJSON_GetObjectItem(mapItem, DISALLOW_MODIFY);
97 if (bundleName == nullptr && disallowModify == nullptr && cJSON_IsString(mapItem)) {
98 bundleName = mapItem;
99 } else if (!cJSON_IsString(bundleName) || !cJSON_IsBool(disallowModify)) {
100 EDMLOGI("ManageKeepAliveAppsSerializer::cJSON_GetObjectItem get error type.");
101 cJSON_Delete(root);
102 return false;
103 }
104 ManageKeepAliveAppInfo appInfo;
105 appInfo.SetBundleName(cJSON_GetStringValue(bundleName));
106 appInfo.SetDisallowModify(disallowModify == nullptr ? true : cJSON_IsTrue(disallowModify));
107 dataObj.emplace_back(appInfo);
108 }
109 cJSON_Delete(root);
110 return true;
111 }
112
Serialize(const std::vector<ManageKeepAliveAppInfo> & dataObj,std::string & policy)113 bool ManageKeepAliveAppsSerializer::Serialize(const std::vector<ManageKeepAliveAppInfo> &dataObj,
114 std::string &policy)
115 {
116 if (dataObj.empty()) {
117 return true;
118 }
119 cJSON* root = nullptr;
120 CJSON_CREATE_OBJECT_AND_CHECK(root, false);
121 for (auto& mapIt : dataObj) {
122 cJSON* policyObject = nullptr;
123 CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(policyObject, false, root);
124 cJSON_AddStringToObject(policyObject, BUNDLE_NAME, mapIt.GetBundleName().c_str());
125 cJSON_AddBoolToObject(policyObject, DISALLOW_MODIFY, mapIt.GetDisallowModify());
126 if (!cJSON_AddItemToArray(root, policyObject)) {
127 cJSON_Delete(root);
128 cJSON_Delete(policyObject);
129 return false;
130 }
131 }
132 char *cJsonStr = cJSON_Print(root);
133 if (cJsonStr != nullptr) {
134 policy = std::string(cJsonStr);
135 cJSON_free(cJsonStr);
136 }
137 cJSON_Delete(root);
138 return true;
139 }
140
GetPolicy(MessageParcel & data,std::vector<ManageKeepAliveAppInfo> & result)141 bool ManageKeepAliveAppsSerializer::GetPolicy(MessageParcel &data,
142 std::vector<ManageKeepAliveAppInfo> &result)
143 {
144 uint32_t size = data.ReadUint32();
145 if (size > EdmConstants::KEEP_ALIVE_APPS_MAX_SIZE) {
146 EDMLOGE("ManageKeepAliveAppsSerializer:GetPolicy size=[%{public}u] is too large", size);
147 return false;
148 }
149 for (uint32_t i = 0; i < size; i++) {
150 ManageKeepAliveAppInfo info;
151 if (!ManageKeepAliveAppInfo::Unmarshalling(data, info)) {
152 EDMLOGE("ManageKeepAliveAppsSerializer GetDataByParcel::read parcel fail");
153 return false;
154 }
155 result.emplace_back(info);
156 }
157 return true;
158 }
159
WritePolicy(MessageParcel & reply,std::vector<ManageKeepAliveAppInfo> & result)160 bool ManageKeepAliveAppsSerializer::WritePolicy(MessageParcel &reply,
161 std::vector<ManageKeepAliveAppInfo> &result)
162 {
163 return true;
164 }
165
MergePolicy(std::vector<std::vector<ManageKeepAliveAppInfo>> & data,std::vector<ManageKeepAliveAppInfo> & result)166 bool ManageKeepAliveAppsSerializer::MergePolicy(std::vector<std::vector<ManageKeepAliveAppInfo>> &data,
167 std::vector<ManageKeepAliveAppInfo> &result)
168 {
169 std::set<ManageKeepAliveAppInfo> stData;
170 std::map<std::string, ManageKeepAliveAppInfo> mapData;
171 for (const auto &dataItem : data) {
172 for (const ManageKeepAliveAppInfo &item : dataItem) {
173 if (mapData.find(item.GetBundleName()) == mapData.end()) {
174 mapData[item.GetBundleName()] = item;
175 stData.insert(item);
176 }
177 }
178 }
179 result.assign(stData.begin(), stData.end());
180 return true;
181 }
182
UpdateByMergePolicy(std::vector<ManageKeepAliveAppInfo> & data,std::vector<ManageKeepAliveAppInfo> & mergeData)183 bool ManageKeepAliveAppsSerializer::UpdateByMergePolicy(std::vector<ManageKeepAliveAppInfo> &data,
184 std::vector<ManageKeepAliveAppInfo> &mergeData)
185 {
186 for (ManageKeepAliveAppInfo &dataItem : data) {
187 for (const ManageKeepAliveAppInfo &mergeItem : mergeData) {
188 if (dataItem.GetBundleName() == mergeItem.GetBundleName()) {
189 dataItem.SetDisallowModify(mergeItem.GetDisallowModify());
190 break;
191 }
192 }
193 }
194 return true;
195 }
196 } // namespace EDM
197 } //