• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     GetValueIfFindKey<std::string>(jsonObject,
105         jsonObjectEnd,
106         Constants::BUNDLE_NAME,
107         bundleName,
108         JsonType::STRING,
109         false,
110         parseResult,
111         ArrayType::NOT_ARRAY);
112     elementName.SetBundleName(bundleName);
113     std::string moduleName;
114     GetValueIfFindKey<std::string>(jsonObject,
115         jsonObjectEnd,
116         Constants::MODULE_NAME,
117         moduleName,
118         JsonType::STRING,
119         false,
120         parseResult,
121         ArrayType::NOT_ARRAY);
122     elementName.SetModuleName(moduleName);
123     std::string abilityName;
124     GetValueIfFindKey<std::string>(jsonObject,
125         jsonObjectEnd,
126         Constants::ABILITY_NAME,
127         abilityName,
128         JsonType::STRING,
129         false,
130         parseResult,
131         ArrayType::NOT_ARRAY);
132     elementName.SetAbilityName(abilityName);
133     std::string deviceId;
134     GetValueIfFindKey<std::string>(jsonObject,
135         jsonObjectEnd,
136         DEVICE_ID,
137         deviceId,
138         JsonType::STRING,
139         false,
140         parseResult,
141         ArrayType::NOT_ARRAY);
142     elementName.SetDeviceID(deviceId);
143     if (parseResult != ERR_OK) {
144         APP_LOGE("read elementName error : %{public}d", parseResult);
145     }
146 }
147 
to_json(nlohmann::json & jsonObject,const DisposedRule & disposedRule)148 void to_json(nlohmann::json &jsonObject, const DisposedRule &disposedRule)
149 {
150     std::string wantString;
151     if (disposedRule.want != nullptr) {
152         wantString = disposedRule.want->ToString();
153     }
154     jsonObject = nlohmann::json {
155         {WANT, wantString},
156         {COMPONENT_TYPE, disposedRule.componentType},
157         {DISPOSED_TYPE, disposedRule.disposedType},
158         {CONTROL_TYPE, disposedRule.controlType},
159         {ELEMENT_LIST, disposedRule.elementList},
160         {PRIORITY, disposedRule.priority},
161         {IS_EDM, disposedRule.isEdm},
162     };
163 }
164 
from_json(const nlohmann::json & jsonObject,DisposedRule & disposedRule)165 void from_json(const nlohmann::json &jsonObject, DisposedRule &disposedRule)
166 {
167     const auto &jsonObjectEnd = jsonObject.end();
168     int32_t parseResult = ERR_OK;
169     std::string wantString;
170     GetValueIfFindKey<std::string>(jsonObject,
171         jsonObjectEnd,
172         WANT,
173         wantString,
174         JsonType::STRING,
175         false,
176         parseResult,
177         ArrayType::NOT_ARRAY);
178     disposedRule.want.reset(AAFwk::Want::FromString(wantString));
179     GetValueIfFindKey<ComponentType>(jsonObject,
180         jsonObjectEnd,
181         COMPONENT_TYPE,
182         disposedRule.componentType,
183         JsonType::NUMBER,
184         false,
185         parseResult,
186         ArrayType::NOT_ARRAY);
187     GetValueIfFindKey<DisposedType>(jsonObject,
188         jsonObjectEnd,
189         DISPOSED_TYPE,
190         disposedRule.disposedType,
191         JsonType::NUMBER,
192         false,
193         parseResult,
194         ArrayType::NOT_ARRAY);
195     GetValueIfFindKey<ControlType>(jsonObject,
196         jsonObjectEnd,
197         CONTROL_TYPE,
198         disposedRule.controlType,
199         JsonType::NUMBER,
200         false,
201         parseResult,
202         ArrayType::NOT_ARRAY);
203     GetValueIfFindKey<std::vector<ElementName>>(jsonObject,
204         jsonObjectEnd,
205         ELEMENT_LIST,
206         disposedRule.elementList,
207         JsonType::ARRAY,
208         false,
209         parseResult,
210         ArrayType::OBJECT);
211     GetValueIfFindKey<int32_t>(jsonObject,
212         jsonObjectEnd,
213         PRIORITY,
214         disposedRule.priority,
215         JsonType::NUMBER,
216         false,
217         parseResult,
218         ArrayType::NOT_ARRAY);
219     GetValueIfFindKey<bool>(jsonObject,
220         jsonObjectEnd,
221         IS_EDM,
222         disposedRule.isEdm,
223         JsonType::BOOLEAN,
224         false,
225         parseResult,
226         ArrayType::NOT_ARRAY);
227     if (parseResult != ERR_OK) {
228         APP_LOGE("read disposedRule error : %{public}d", parseResult);
229     }
230 }
231 
ToString() const232 std::string DisposedRule::ToString() const
233 {
234     nlohmann::json jsonObject;
235     to_json(jsonObject, *this);
236     return jsonObject.dump();
237 }
238 
FromString(const std::string & ruleString,DisposedRule & rule)239 bool DisposedRule::FromString(const std::string &ruleString, DisposedRule &rule)
240 {
241     APP_LOGD("FromString %{public}s", ruleString.c_str());
242     nlohmann::json jsonObject = nlohmann::json::parse(ruleString, nullptr, false);
243     if (jsonObject.is_discarded()) {
244         APP_LOGE("failed parse ruleString: %{public}s", ruleString.c_str());
245         return false;
246     }
247     from_json(jsonObject, rule);
248     return true;
249 }
250 
ReadFromParcel(Parcel & parcel)251 bool UninstallDisposedRule::ReadFromParcel(Parcel &parcel)
252 {
253     want.reset(parcel.ReadParcelable<AAFwk::Want>());
254     uninstallComponentType = static_cast<UninstallComponentType>(parcel.ReadInt32());
255     priority = parcel.ReadInt32();
256     return true;
257 }
258 
Marshalling(Parcel & parcel) const259 bool UninstallDisposedRule::Marshalling(Parcel &parcel) const
260 {
261     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, want.get());
262     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(uninstallComponentType));
263     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
264 
265     return true;
266 }
267 
Unmarshalling(Parcel & parcel)268 UninstallDisposedRule *UninstallDisposedRule::Unmarshalling(Parcel &parcel)
269 {
270     UninstallDisposedRule *info = new (std::nothrow) UninstallDisposedRule();
271     if (info && !info->ReadFromParcel(parcel)) {
272         APP_LOGW("read from parcel failed");
273         delete info;
274         info = nullptr;
275     }
276     return info;
277 }
278 
to_json(nlohmann::json & jsonObject,const UninstallDisposedRule & uninstallDisposedRule)279 void to_json(nlohmann::json &jsonObject, const UninstallDisposedRule &uninstallDisposedRule)
280 {
281     std::string wantString = "";
282     if (uninstallDisposedRule.want != nullptr) {
283         wantString = uninstallDisposedRule.want->ToString();
284     }
285     jsonObject = nlohmann::json {
286         {WANT, wantString},
287         {UNINSTALL_COMPONENT_TYPE, uninstallDisposedRule.uninstallComponentType},
288         {PRIORITY, uninstallDisposedRule.priority},
289     };
290 }
291 
from_json(const nlohmann::json & jsonObject,UninstallDisposedRule & uninstallDisposedRule)292 void from_json(const nlohmann::json &jsonObject, UninstallDisposedRule &uninstallDisposedRule)
293 {
294     const auto &jsonObjectEnd = jsonObject.end();
295     int32_t parseResult = ERR_OK;
296     std::string wantString;
297     GetValueIfFindKey<std::string>(jsonObject,
298         jsonObjectEnd,
299         WANT,
300         wantString,
301         JsonType::STRING,
302         false,
303         parseResult,
304         ArrayType::NOT_ARRAY);
305     uninstallDisposedRule.want.reset(AAFwk::Want::FromString(wantString));
306     GetValueIfFindKey<UninstallComponentType>(jsonObject,
307         jsonObjectEnd,
308         UNINSTALL_COMPONENT_TYPE,
309         uninstallDisposedRule.uninstallComponentType,
310         JsonType::NUMBER,
311         false,
312         parseResult,
313         ArrayType::NOT_ARRAY);
314     GetValueIfFindKey<int32_t>(jsonObject,
315         jsonObjectEnd,
316         PRIORITY,
317         uninstallDisposedRule.priority,
318         JsonType::NUMBER,
319         false,
320         parseResult,
321         ArrayType::NOT_ARRAY);
322     if (parseResult != ERR_OK) {
323         APP_LOGE("read uninstallDisposedRule error : %{public}d", parseResult);
324     }
325 }
326 
ToString() const327 std::string UninstallDisposedRule::ToString() const
328 {
329     nlohmann::json jsonObject;
330     to_json(jsonObject, *this);
331     return jsonObject.dump();
332 }
333 
FromString(const std::string & ruleString,UninstallDisposedRule & rule)334 bool UninstallDisposedRule::FromString(const std::string &ruleString, UninstallDisposedRule &rule)
335 {
336     APP_LOGD("FromString %{public}s", ruleString.c_str());
337     nlohmann::json jsonObject = nlohmann::json::parse(ruleString, nullptr, false);
338     if (jsonObject.is_discarded()) {
339         APP_LOGE("failed parse ruleString: %{public}s", ruleString.c_str());
340         return false;
341     }
342     from_json(jsonObject, rule);
343     return true;
344 }
345 }  // namespace AppExecFwk
346 }  // namespace OHOS
347