1 /*
2 * Copyright (c) 2023-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 "disposed_rule.h"
17
18 #include "app_log_wrapper.h"
19 #include "json_util.h"
20 #include "nlohmann/json.hpp"
21 #include "parcel_macro.h"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const char* WANT = "want";
28 const char* COMPONENT_TYPE = "componentType";
29 const char* UNINSTALL_COMPONENT_TYPE = "uninstallComponentType";
30 const char* DISPOSED_TYPE = "disposedType";
31 const char* CONTROL_TYPE = "controlType";
32 const char* ELEMENT_LIST = "elementList";
33 const char* PRIORITY = "priority";
34 const char* DEVICE_ID = "deviceId";
35 const char* IS_EDM = "isEdm";
36 const char* APP_ID = "appId";
37 const char* APP_INDEX = "appIndex";
38 const char* DISPOSED_RULE = "disposedRule";
39 } // namespace
40
ReadFromParcel(Parcel & parcel)41 bool DisposedRule::ReadFromParcel(Parcel &parcel)
42 {
43 want.reset(parcel.ReadParcelable<AAFwk::Want>());
44 componentType = static_cast<ComponentType>(parcel.ReadInt32());
45 disposedType = static_cast<DisposedType>(parcel.ReadInt32());
46 controlType = static_cast<ControlType>(parcel.ReadInt32());
47 int32_t elementSize;
48 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, elementSize);
49 CONTAINER_SECURITY_VERIFY(parcel, elementSize, &elementList);
50 for (auto i = 0; i < elementSize; i++) {
51 std::string elementUri = Str16ToStr8(parcel.ReadString16());
52 ElementName elementName;
53 if (!elementName.ParseURI(elementUri)) {
54 APP_LOGW("parse elementName failed");
55 }
56 elementList.emplace_back(elementName);
57 }
58 priority = parcel.ReadInt32();
59 isEdm = parcel.ReadBool();
60 return true;
61 }
62
Marshalling(Parcel & parcel) const63 bool DisposedRule::Marshalling(Parcel &parcel) const
64 {
65 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, want.get());
66 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(componentType));
67 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(disposedType));
68 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(controlType));
69 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, elementList.size());
70 for (const auto &elementName: elementList) {
71 std::string elementUri = elementName.GetURI();
72 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(elementUri));
73 }
74 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
75 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isEdm);
76
77 return true;
78 }
79
Unmarshalling(Parcel & parcel)80 DisposedRule *DisposedRule::Unmarshalling(Parcel &parcel)
81 {
82 DisposedRule *info = new (std::nothrow) DisposedRule();
83 if (info && !info->ReadFromParcel(parcel)) {
84 APP_LOGW("read from parcel failed");
85 delete info;
86 info = nullptr;
87 }
88 return info;
89 }
90
to_json(nlohmann::json & jsonObject,const ElementName & elementName)91 void to_json(nlohmann::json &jsonObject, const ElementName &elementName)
92 {
93 APP_LOGD("elementName to_json bundleName %{public}s", elementName.GetBundleName().c_str());
94 jsonObject = nlohmann::json {
95 {Constants::BUNDLE_NAME, elementName.GetBundleName()},
96 {Constants::MODULE_NAME, elementName.GetModuleName()},
97 {Constants::ABILITY_NAME, elementName.GetAbilityName()},
98 {DEVICE_ID, elementName.GetDeviceID()}
99 };
100 }
101
from_json(const nlohmann::json & jsonObject,ElementName & elementName)102 void from_json(const nlohmann::json &jsonObject, ElementName &elementName)
103 {
104 const auto &jsonObjectEnd = jsonObject.end();
105 int32_t parseResult = ERR_OK;
106 std::string bundleName;
107 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
108 jsonObjectEnd,
109 Constants::BUNDLE_NAME,
110 bundleName,
111 false,
112 parseResult);
113 elementName.SetBundleName(bundleName);
114 std::string moduleName;
115 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
116 jsonObjectEnd,
117 Constants::MODULE_NAME,
118 moduleName,
119 false,
120 parseResult);
121 elementName.SetModuleName(moduleName);
122 std::string abilityName;
123 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
124 jsonObjectEnd,
125 Constants::ABILITY_NAME,
126 abilityName,
127 false,
128 parseResult);
129 elementName.SetAbilityName(abilityName);
130 std::string deviceId;
131 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
132 jsonObjectEnd,
133 DEVICE_ID,
134 deviceId,
135 false,
136 parseResult);
137 elementName.SetDeviceID(deviceId);
138 if (parseResult != ERR_OK) {
139 APP_LOGE("read elementName error : %{public}d", parseResult);
140 }
141 }
142
to_json(nlohmann::json & jsonObject,const DisposedRule & disposedRule)143 void to_json(nlohmann::json &jsonObject, const DisposedRule &disposedRule)
144 {
145 std::string wantString = "";
146 if (disposedRule.want != nullptr) {
147 wantString = disposedRule.want->ToString();
148 }
149 jsonObject = nlohmann::json {
150 {WANT, wantString},
151 {COMPONENT_TYPE, disposedRule.componentType},
152 {DISPOSED_TYPE, disposedRule.disposedType},
153 {CONTROL_TYPE, disposedRule.controlType},
154 {ELEMENT_LIST, disposedRule.elementList},
155 {PRIORITY, disposedRule.priority},
156 {IS_EDM, disposedRule.isEdm},
157 };
158 }
159
from_json(const nlohmann::json & jsonObject,DisposedRule & disposedRule)160 void from_json(const nlohmann::json &jsonObject, DisposedRule &disposedRule)
161 {
162 const auto &jsonObjectEnd = jsonObject.end();
163 int32_t parseResult = ERR_OK;
164 std::string wantString;
165 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
166 jsonObjectEnd,
167 WANT,
168 wantString,
169 false,
170 parseResult);
171 disposedRule.want.reset(AAFwk::Want::FromString(wantString));
172 GetValueIfFindKey<ComponentType>(jsonObject,
173 jsonObjectEnd,
174 COMPONENT_TYPE,
175 disposedRule.componentType,
176 JsonType::NUMBER,
177 false,
178 parseResult,
179 ArrayType::NOT_ARRAY);
180 GetValueIfFindKey<DisposedType>(jsonObject,
181 jsonObjectEnd,
182 DISPOSED_TYPE,
183 disposedRule.disposedType,
184 JsonType::NUMBER,
185 false,
186 parseResult,
187 ArrayType::NOT_ARRAY);
188 GetValueIfFindKey<ControlType>(jsonObject,
189 jsonObjectEnd,
190 CONTROL_TYPE,
191 disposedRule.controlType,
192 JsonType::NUMBER,
193 false,
194 parseResult,
195 ArrayType::NOT_ARRAY);
196 GetValueIfFindKey<std::vector<ElementName>>(jsonObject,
197 jsonObjectEnd,
198 ELEMENT_LIST,
199 disposedRule.elementList,
200 JsonType::ARRAY,
201 false,
202 parseResult,
203 ArrayType::OBJECT);
204 GetValueIfFindKey<int32_t>(jsonObject,
205 jsonObjectEnd,
206 PRIORITY,
207 disposedRule.priority,
208 JsonType::NUMBER,
209 false,
210 parseResult,
211 ArrayType::NOT_ARRAY);
212 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
213 jsonObjectEnd,
214 IS_EDM,
215 disposedRule.isEdm,
216 false,
217 parseResult);
218 if (parseResult != ERR_OK) {
219 APP_LOGE("read disposedRule error : %{public}d", parseResult);
220 }
221 }
222
ToString() const223 std::string DisposedRule::ToString() const
224 {
225 nlohmann::json jsonObject;
226 to_json(jsonObject, *this);
227 return jsonObject.dump();
228 }
229
FromString(const std::string & ruleString,DisposedRule & rule)230 bool DisposedRule::FromString(const std::string &ruleString, DisposedRule &rule)
231 {
232 APP_LOGD("FromString %{public}s", ruleString.c_str());
233 nlohmann::json jsonObject = nlohmann::json::parse(ruleString, nullptr, false);
234 if (jsonObject.is_discarded()) {
235 APP_LOGE("failed parse ruleString: %{public}s", ruleString.c_str());
236 return false;
237 }
238 from_json(jsonObject, rule);
239 return true;
240 }
241
ReadFromParcel(Parcel & parcel)242 bool UninstallDisposedRule::ReadFromParcel(Parcel &parcel)
243 {
244 want.reset(parcel.ReadParcelable<AAFwk::Want>());
245 uninstallComponentType = static_cast<UninstallComponentType>(parcel.ReadInt32());
246 priority = parcel.ReadInt32();
247 return true;
248 }
249
Marshalling(Parcel & parcel) const250 bool UninstallDisposedRule::Marshalling(Parcel &parcel) const
251 {
252 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, want.get());
253 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(uninstallComponentType));
254 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
255
256 return true;
257 }
258
Unmarshalling(Parcel & parcel)259 UninstallDisposedRule *UninstallDisposedRule::Unmarshalling(Parcel &parcel)
260 {
261 UninstallDisposedRule *info = new (std::nothrow) UninstallDisposedRule();
262 if (info && !info->ReadFromParcel(parcel)) {
263 APP_LOGW("read from parcel failed");
264 delete info;
265 info = nullptr;
266 }
267 return info;
268 }
269
to_json(nlohmann::json & jsonObject,const UninstallDisposedRule & uninstallDisposedRule)270 void to_json(nlohmann::json &jsonObject, const UninstallDisposedRule &uninstallDisposedRule)
271 {
272 std::string wantString = "";
273 if (uninstallDisposedRule.want != nullptr) {
274 wantString = uninstallDisposedRule.want->ToString();
275 }
276 jsonObject = nlohmann::json {
277 {WANT, wantString},
278 {UNINSTALL_COMPONENT_TYPE, uninstallDisposedRule.uninstallComponentType},
279 {PRIORITY, uninstallDisposedRule.priority},
280 };
281 }
282
from_json(const nlohmann::json & jsonObject,UninstallDisposedRule & uninstallDisposedRule)283 void from_json(const nlohmann::json &jsonObject, UninstallDisposedRule &uninstallDisposedRule)
284 {
285 const auto &jsonObjectEnd = jsonObject.end();
286 int32_t parseResult = ERR_OK;
287 std::string wantString;
288 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
289 jsonObjectEnd,
290 WANT,
291 wantString,
292 false,
293 parseResult);
294 uninstallDisposedRule.want.reset(AAFwk::Want::FromString(wantString));
295 GetValueIfFindKey<UninstallComponentType>(jsonObject,
296 jsonObjectEnd,
297 UNINSTALL_COMPONENT_TYPE,
298 uninstallDisposedRule.uninstallComponentType,
299 JsonType::NUMBER,
300 false,
301 parseResult,
302 ArrayType::NOT_ARRAY);
303 GetValueIfFindKey<int32_t>(jsonObject,
304 jsonObjectEnd,
305 PRIORITY,
306 uninstallDisposedRule.priority,
307 JsonType::NUMBER,
308 false,
309 parseResult,
310 ArrayType::NOT_ARRAY);
311 if (parseResult != ERR_OK) {
312 APP_LOGE("read uninstallDisposedRule error : %{public}d", parseResult);
313 }
314 }
315
ToString() const316 std::string UninstallDisposedRule::ToString() const
317 {
318 nlohmann::json jsonObject;
319 to_json(jsonObject, *this);
320 return jsonObject.dump();
321 }
322
FromString(const std::string & ruleString,UninstallDisposedRule & rule)323 bool UninstallDisposedRule::FromString(const std::string &ruleString, UninstallDisposedRule &rule)
324 {
325 APP_LOGD("FromString %{public}s", ruleString.c_str());
326 nlohmann::json jsonObject = nlohmann::json::parse(ruleString, nullptr, false);
327 if (jsonObject.is_discarded()) {
328 APP_LOGE("failed parse ruleString: %{public}s", ruleString.c_str());
329 return false;
330 }
331 from_json(jsonObject, rule);
332 return true;
333 }
334
ReadFromParcel(Parcel & parcel)335 bool DisposedRuleConfiguration::ReadFromParcel(Parcel &parcel)
336 {
337 std::unique_ptr<DisposedRule> rule(parcel.ReadParcelable<DisposedRule>());
338 if (!rule) {
339 APP_LOGE("ReadParcelable<DisposedRule> failed");
340 return false;
341 }
342 disposedRule = *rule;
343 appId = Str16ToStr8(parcel.ReadString16());
344 appIndex = parcel.ReadInt32();
345 return true;
346 }
347
Marshalling(Parcel & parcel) const348 bool DisposedRuleConfiguration::Marshalling(Parcel &parcel) const
349 {
350 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &disposedRule);
351 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(appId));
352 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex);
353 return true;
354 }
355
Unmarshalling(Parcel & parcel)356 DisposedRuleConfiguration *DisposedRuleConfiguration::Unmarshalling(Parcel &parcel)
357 {
358 DisposedRuleConfiguration *info = new (std::nothrow) DisposedRuleConfiguration();
359 if (info && !info->ReadFromParcel(parcel)) {
360 APP_LOGW("read from parcel failed");
361 delete info;
362 info = nullptr;
363 }
364 return info;
365 }
366
to_json(nlohmann::json & jsonObject,const DisposedRuleConfiguration & disposedRuleConfiguration)367 void to_json(nlohmann::json &jsonObject, const DisposedRuleConfiguration &disposedRuleConfiguration)
368 {
369 jsonObject = nlohmann::json {
370 {DISPOSED_RULE, disposedRuleConfiguration.disposedRule},
371 {APP_ID, disposedRuleConfiguration.appId},
372 {APP_INDEX, disposedRuleConfiguration.appIndex},
373 };
374 }
375
from_json(const nlohmann::json & jsonObject,DisposedRuleConfiguration & disposedRuleConfiguration)376 void from_json(const nlohmann::json &jsonObject, DisposedRuleConfiguration &disposedRuleConfiguration)
377 {
378 const auto &jsonObjectEnd = jsonObject.end();
379 int32_t parseResult = ERR_OK;
380 GetValueIfFindKey<DisposedRule>(jsonObject,
381 jsonObjectEnd,
382 DISPOSED_RULE,
383 disposedRuleConfiguration.disposedRule,
384 JsonType::OBJECT,
385 false,
386 parseResult,
387 ArrayType::NOT_ARRAY);
388 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
389 jsonObjectEnd,
390 APP_ID,
391 disposedRuleConfiguration.appId,
392 false,
393 parseResult);
394 GetValueIfFindKey<int32_t>(jsonObject,
395 jsonObjectEnd,
396 APP_INDEX,
397 disposedRuleConfiguration.appIndex,
398 JsonType::NUMBER,
399 false,
400 parseResult,
401 ArrayType::NOT_ARRAY);
402 }
403 } // namespace AppExecFwk
404 } // namespace OHOS
405