1 /*
2 * Copyright (c) 2023 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 } // namespace
37
ReadFromParcel(Parcel & parcel)38 bool DisposedRule::ReadFromParcel(Parcel &parcel)
39 {
40 want.reset(parcel.ReadParcelable<AAFwk::Want>());
41 componentType = static_cast<ComponentType>(parcel.ReadInt32());
42 disposedType = static_cast<DisposedType>(parcel.ReadInt32());
43 controlType = static_cast<ControlType>(parcel.ReadInt32());
44 int32_t elementSize;
45 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, elementSize);
46 CONTAINER_SECURITY_VERIFY(parcel, elementSize, &elementList);
47 for (auto i = 0; i < elementSize; i++) {
48 std::string elementUri = Str16ToStr8(parcel.ReadString16());
49 ElementName elementName;
50 if (!elementName.ParseURI(elementUri)) {
51 APP_LOGW("parse elementName failed");
52 }
53 elementList.emplace_back(elementName);
54 }
55 priority = parcel.ReadInt32();
56 isEdm = parcel.ReadBool();
57 return true;
58 }
59
Marshalling(Parcel & parcel) const60 bool DisposedRule::Marshalling(Parcel &parcel) const
61 {
62 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, want.get());
63 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(componentType));
64 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(disposedType));
65 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(controlType));
66 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, elementList.size());
67 for (const auto &elementName: elementList) {
68 std::string elementUri = elementName.GetURI();
69 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(elementUri));
70 }
71 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
72 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isEdm);
73
74 return true;
75 }
76
Unmarshalling(Parcel & parcel)77 DisposedRule *DisposedRule::Unmarshalling(Parcel &parcel)
78 {
79 DisposedRule *info = new (std::nothrow) DisposedRule();
80 if (info && !info->ReadFromParcel(parcel)) {
81 APP_LOGW("read from parcel failed");
82 delete info;
83 info = nullptr;
84 }
85 return info;
86 }
87
to_json(nlohmann::json & jsonObject,const ElementName & elementName)88 void to_json(nlohmann::json &jsonObject, const ElementName &elementName)
89 {
90 APP_LOGD("elementName to_json bundleName %{public}s", elementName.GetBundleName().c_str());
91 jsonObject = nlohmann::json {
92 {Constants::BUNDLE_NAME, elementName.GetBundleName()},
93 {Constants::MODULE_NAME, elementName.GetModuleName()},
94 {Constants::ABILITY_NAME, elementName.GetAbilityName()},
95 {DEVICE_ID, elementName.GetDeviceID()}
96 };
97 }
98
from_json(const nlohmann::json & jsonObject,ElementName & elementName)99 void from_json(const nlohmann::json &jsonObject, ElementName &elementName)
100 {
101 const auto &jsonObjectEnd = jsonObject.end();
102 int32_t parseResult = ERR_OK;
103 std::string bundleName;
104 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
105 jsonObjectEnd,
106 Constants::BUNDLE_NAME,
107 bundleName,
108 false,
109 parseResult);
110 elementName.SetBundleName(bundleName);
111 std::string moduleName;
112 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
113 jsonObjectEnd,
114 Constants::MODULE_NAME,
115 moduleName,
116 false,
117 parseResult);
118 elementName.SetModuleName(moduleName);
119 std::string abilityName;
120 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
121 jsonObjectEnd,
122 Constants::ABILITY_NAME,
123 abilityName,
124 false,
125 parseResult);
126 elementName.SetAbilityName(abilityName);
127 std::string deviceId;
128 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
129 jsonObjectEnd,
130 DEVICE_ID,
131 deviceId,
132 false,
133 parseResult);
134 elementName.SetDeviceID(deviceId);
135 if (parseResult != ERR_OK) {
136 APP_LOGE("read elementName error : %{public}d", parseResult);
137 }
138 }
139
to_json(nlohmann::json & jsonObject,const DisposedRule & disposedRule)140 void to_json(nlohmann::json &jsonObject, const DisposedRule &disposedRule)
141 {
142 std::string wantString = "";
143 if (disposedRule.want != nullptr) {
144 wantString = disposedRule.want->ToString();
145 }
146 jsonObject = nlohmann::json {
147 {WANT, wantString},
148 {COMPONENT_TYPE, disposedRule.componentType},
149 {DISPOSED_TYPE, disposedRule.disposedType},
150 {CONTROL_TYPE, disposedRule.controlType},
151 {ELEMENT_LIST, disposedRule.elementList},
152 {PRIORITY, disposedRule.priority},
153 {IS_EDM, disposedRule.isEdm},
154 };
155 }
156
from_json(const nlohmann::json & jsonObject,DisposedRule & disposedRule)157 void from_json(const nlohmann::json &jsonObject, DisposedRule &disposedRule)
158 {
159 const auto &jsonObjectEnd = jsonObject.end();
160 int32_t parseResult = ERR_OK;
161 std::string wantString;
162 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
163 jsonObjectEnd,
164 WANT,
165 wantString,
166 false,
167 parseResult);
168 disposedRule.want.reset(AAFwk::Want::FromString(wantString));
169 GetValueIfFindKey<ComponentType>(jsonObject,
170 jsonObjectEnd,
171 COMPONENT_TYPE,
172 disposedRule.componentType,
173 JsonType::NUMBER,
174 false,
175 parseResult,
176 ArrayType::NOT_ARRAY);
177 GetValueIfFindKey<DisposedType>(jsonObject,
178 jsonObjectEnd,
179 DISPOSED_TYPE,
180 disposedRule.disposedType,
181 JsonType::NUMBER,
182 false,
183 parseResult,
184 ArrayType::NOT_ARRAY);
185 GetValueIfFindKey<ControlType>(jsonObject,
186 jsonObjectEnd,
187 CONTROL_TYPE,
188 disposedRule.controlType,
189 JsonType::NUMBER,
190 false,
191 parseResult,
192 ArrayType::NOT_ARRAY);
193 GetValueIfFindKey<std::vector<ElementName>>(jsonObject,
194 jsonObjectEnd,
195 ELEMENT_LIST,
196 disposedRule.elementList,
197 JsonType::ARRAY,
198 false,
199 parseResult,
200 ArrayType::OBJECT);
201 GetValueIfFindKey<int32_t>(jsonObject,
202 jsonObjectEnd,
203 PRIORITY,
204 disposedRule.priority,
205 JsonType::NUMBER,
206 false,
207 parseResult,
208 ArrayType::NOT_ARRAY);
209 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
210 jsonObjectEnd,
211 IS_EDM,
212 disposedRule.isEdm,
213 false,
214 parseResult);
215 if (parseResult != ERR_OK) {
216 APP_LOGE("read disposedRule error : %{public}d", parseResult);
217 }
218 }
219
ToString() const220 std::string DisposedRule::ToString() const
221 {
222 nlohmann::json jsonObject;
223 to_json(jsonObject, *this);
224 return jsonObject.dump();
225 }
226
FromString(const std::string & ruleString,DisposedRule & rule)227 bool DisposedRule::FromString(const std::string &ruleString, DisposedRule &rule)
228 {
229 APP_LOGD("FromString %{public}s", ruleString.c_str());
230 nlohmann::json jsonObject = nlohmann::json::parse(ruleString, nullptr, false);
231 if (jsonObject.is_discarded()) {
232 APP_LOGE("failed parse ruleString: %{public}s", ruleString.c_str());
233 return false;
234 }
235 from_json(jsonObject, rule);
236 return true;
237 }
238
ReadFromParcel(Parcel & parcel)239 bool UninstallDisposedRule::ReadFromParcel(Parcel &parcel)
240 {
241 want.reset(parcel.ReadParcelable<AAFwk::Want>());
242 uninstallComponentType = static_cast<UninstallComponentType>(parcel.ReadInt32());
243 priority = parcel.ReadInt32();
244 return true;
245 }
246
Marshalling(Parcel & parcel) const247 bool UninstallDisposedRule::Marshalling(Parcel &parcel) const
248 {
249 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, want.get());
250 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(uninstallComponentType));
251 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
252
253 return true;
254 }
255
Unmarshalling(Parcel & parcel)256 UninstallDisposedRule *UninstallDisposedRule::Unmarshalling(Parcel &parcel)
257 {
258 UninstallDisposedRule *info = new (std::nothrow) UninstallDisposedRule();
259 if (info && !info->ReadFromParcel(parcel)) {
260 APP_LOGW("read from parcel failed");
261 delete info;
262 info = nullptr;
263 }
264 return info;
265 }
266
to_json(nlohmann::json & jsonObject,const UninstallDisposedRule & uninstallDisposedRule)267 void to_json(nlohmann::json &jsonObject, const UninstallDisposedRule &uninstallDisposedRule)
268 {
269 std::string wantString = "";
270 if (uninstallDisposedRule.want != nullptr) {
271 wantString = uninstallDisposedRule.want->ToString();
272 }
273 jsonObject = nlohmann::json {
274 {WANT, wantString},
275 {UNINSTALL_COMPONENT_TYPE, uninstallDisposedRule.uninstallComponentType},
276 {PRIORITY, uninstallDisposedRule.priority},
277 };
278 }
279
from_json(const nlohmann::json & jsonObject,UninstallDisposedRule & uninstallDisposedRule)280 void from_json(const nlohmann::json &jsonObject, UninstallDisposedRule &uninstallDisposedRule)
281 {
282 const auto &jsonObjectEnd = jsonObject.end();
283 int32_t parseResult = ERR_OK;
284 std::string wantString;
285 BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
286 jsonObjectEnd,
287 WANT,
288 wantString,
289 false,
290 parseResult);
291 uninstallDisposedRule.want.reset(AAFwk::Want::FromString(wantString));
292 GetValueIfFindKey<UninstallComponentType>(jsonObject,
293 jsonObjectEnd,
294 UNINSTALL_COMPONENT_TYPE,
295 uninstallDisposedRule.uninstallComponentType,
296 JsonType::NUMBER,
297 false,
298 parseResult,
299 ArrayType::NOT_ARRAY);
300 GetValueIfFindKey<int32_t>(jsonObject,
301 jsonObjectEnd,
302 PRIORITY,
303 uninstallDisposedRule.priority,
304 JsonType::NUMBER,
305 false,
306 parseResult,
307 ArrayType::NOT_ARRAY);
308 if (parseResult != ERR_OK) {
309 APP_LOGE("read uninstallDisposedRule error : %{public}d", parseResult);
310 }
311 }
312
ToString() const313 std::string UninstallDisposedRule::ToString() const
314 {
315 nlohmann::json jsonObject;
316 to_json(jsonObject, *this);
317 return jsonObject.dump();
318 }
319
FromString(const std::string & ruleString,UninstallDisposedRule & rule)320 bool UninstallDisposedRule::FromString(const std::string &ruleString, UninstallDisposedRule &rule)
321 {
322 APP_LOGD("FromString %{public}s", ruleString.c_str());
323 nlohmann::json jsonObject = nlohmann::json::parse(ruleString, nullptr, false);
324 if (jsonObject.is_discarded()) {
325 APP_LOGE("failed parse ruleString: %{public}s", ruleString.c_str());
326 return false;
327 }
328 from_json(jsonObject, rule);
329 return true;
330 }
331 } // namespace AppExecFwk
332 } // namespace OHOS
333