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* DISPOSED_TYPE = "disposedType";
30 const char* CONTROL_TYPE = "controlType";
31 const char* ELEMENT_LIST = "elementList";
32 const char* PRIORITY = "priority";
33 const char* DEVICE_ID = "deviceId";
34 const char* IS_EDM = "isEdm";
35 }  // namespace
36 
ReadFromParcel(Parcel & parcel)37 bool DisposedRule::ReadFromParcel(Parcel &parcel)
38 {
39     want.reset(parcel.ReadParcelable<AAFwk::Want>());
40     componentType = static_cast<ComponentType>(parcel.ReadInt32());
41     disposedType = static_cast<DisposedType>(parcel.ReadInt32());
42     controlType = static_cast<ControlType>(parcel.ReadInt32());
43     int32_t elementSize;
44     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, elementSize);
45     CONTAINER_SECURITY_VERIFY(parcel, elementSize, &elementList);
46     for (auto i = 0; i < elementSize; i++) {
47         std::string elementUri = Str16ToStr8(parcel.ReadString16());
48         ElementName elementName;
49         if (!elementName.ParseURI(elementUri)) {
50             APP_LOGW("parse elementName failed");
51         }
52         elementList.emplace_back(elementName);
53     }
54     priority = parcel.ReadInt32();
55     isEdm = parcel.ReadBool();
56     return true;
57 }
58 
Marshalling(Parcel & parcel) const59 bool DisposedRule::Marshalling(Parcel &parcel) const
60 {
61     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, want.get());
62     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(componentType));
63     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(disposedType));
64     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(controlType));
65     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, elementList.size());
66     for (const auto &elementName: elementList) {
67         std::string elementUri = elementName.GetURI();
68         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(elementUri));
69     }
70     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
71     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isEdm);
72 
73     return true;
74 }
75 
Unmarshalling(Parcel & parcel)76 DisposedRule *DisposedRule::Unmarshalling(Parcel &parcel)
77 {
78     DisposedRule *info = new (std::nothrow) DisposedRule();
79     if (info && !info->ReadFromParcel(parcel)) {
80         APP_LOGW("read from parcel failed");
81         delete info;
82         info = nullptr;
83     }
84     return info;
85 }
86 
to_json(nlohmann::json & jsonObject,const ElementName & elementName)87 void to_json(nlohmann::json &jsonObject, const ElementName &elementName)
88 {
89     APP_LOGD("elementName to_json bundleName %{public}s", elementName.GetBundleName().c_str());
90     jsonObject = nlohmann::json {
91         {Constants::BUNDLE_NAME, elementName.GetBundleName()},
92         {Constants::MODULE_NAME, elementName.GetModuleName()},
93         {Constants::ABILITY_NAME, elementName.GetAbilityName()},
94         {DEVICE_ID, elementName.GetDeviceID()}
95     };
96 }
97 
from_json(const nlohmann::json & jsonObject,ElementName & elementName)98 void from_json(const nlohmann::json &jsonObject, ElementName &elementName)
99 {
100     const auto &jsonObjectEnd = jsonObject.end();
101     int32_t parseResult = ERR_OK;
102     std::string bundleName;
103     GetValueIfFindKey<std::string>(jsonObject,
104         jsonObjectEnd,
105         Constants::BUNDLE_NAME,
106         bundleName,
107         JsonType::STRING,
108         false,
109         parseResult,
110         ArrayType::NOT_ARRAY);
111     elementName.SetBundleName(bundleName);
112     std::string moduleName;
113     GetValueIfFindKey<std::string>(jsonObject,
114         jsonObjectEnd,
115         Constants::MODULE_NAME,
116         moduleName,
117         JsonType::STRING,
118         false,
119         parseResult,
120         ArrayType::NOT_ARRAY);
121     elementName.SetModuleName(moduleName);
122     std::string abilityName;
123     GetValueIfFindKey<std::string>(jsonObject,
124         jsonObjectEnd,
125         Constants::ABILITY_NAME,
126         abilityName,
127         JsonType::STRING,
128         false,
129         parseResult,
130         ArrayType::NOT_ARRAY);
131     elementName.SetAbilityName(abilityName);
132     std::string deviceId;
133     GetValueIfFindKey<std::string>(jsonObject,
134         jsonObjectEnd,
135         DEVICE_ID,
136         deviceId,
137         JsonType::STRING,
138         false,
139         parseResult,
140         ArrayType::NOT_ARRAY);
141     elementName.SetDeviceID(deviceId);
142     if (parseResult != ERR_OK) {
143         APP_LOGE("read elementName error : %{public}d", parseResult);
144     }
145 }
146 
to_json(nlohmann::json & jsonObject,const DisposedRule & disposedRule)147 void to_json(nlohmann::json &jsonObject, const DisposedRule &disposedRule)
148 {
149     std::string wantString = disposedRule.want->ToString();
150     jsonObject = nlohmann::json {
151         {WANT, wantString},
152         {COMPONENT_TYPE, disposedRule.componentType},
153         {DISPOSED_TYPE, disposedRule.disposedType},
154         {CONTROL_TYPE, disposedRule.controlType},
155         {ELEMENT_LIST, disposedRule.elementList},
156         {PRIORITY, disposedRule.priority},
157         {IS_EDM, disposedRule.isEdm},
158     };
159 }
160 
from_json(const nlohmann::json & jsonObject,DisposedRule & disposedRule)161 void from_json(const nlohmann::json &jsonObject, DisposedRule &disposedRule)
162 {
163     const auto &jsonObjectEnd = jsonObject.end();
164     int32_t parseResult = ERR_OK;
165     std::string wantString;
166     GetValueIfFindKey<std::string>(jsonObject,
167         jsonObjectEnd,
168         WANT,
169         wantString,
170         JsonType::STRING,
171         false,
172         parseResult,
173         ArrayType::NOT_ARRAY);
174     disposedRule.want.reset(AAFwk::Want::FromString(wantString));
175     GetValueIfFindKey<ComponentType>(jsonObject,
176         jsonObjectEnd,
177         COMPONENT_TYPE,
178         disposedRule.componentType,
179         JsonType::NUMBER,
180         false,
181         parseResult,
182         ArrayType::NOT_ARRAY);
183     GetValueIfFindKey<DisposedType>(jsonObject,
184         jsonObjectEnd,
185         DISPOSED_TYPE,
186         disposedRule.disposedType,
187         JsonType::NUMBER,
188         false,
189         parseResult,
190         ArrayType::NOT_ARRAY);
191     GetValueIfFindKey<ControlType>(jsonObject,
192         jsonObjectEnd,
193         CONTROL_TYPE,
194         disposedRule.controlType,
195         JsonType::NUMBER,
196         false,
197         parseResult,
198         ArrayType::NOT_ARRAY);
199     GetValueIfFindKey<std::vector<ElementName>>(jsonObject,
200         jsonObjectEnd,
201         ELEMENT_LIST,
202         disposedRule.elementList,
203         JsonType::ARRAY,
204         false,
205         parseResult,
206         ArrayType::OBJECT);
207     GetValueIfFindKey<int32_t>(jsonObject,
208         jsonObjectEnd,
209         PRIORITY,
210         disposedRule.priority,
211         JsonType::NUMBER,
212         false,
213         parseResult,
214         ArrayType::NOT_ARRAY);
215     GetValueIfFindKey<bool>(jsonObject,
216         jsonObjectEnd,
217         IS_EDM,
218         disposedRule.isEdm,
219         JsonType::BOOLEAN,
220         false,
221         parseResult,
222         ArrayType::NOT_ARRAY);
223     if (parseResult != ERR_OK) {
224         APP_LOGE("read disposedRule error : %{public}d", parseResult);
225     }
226 }
227 
ToString() const228 std::string DisposedRule::ToString() const
229 {
230     nlohmann::json jsonObject;
231     to_json(jsonObject, *this);
232     return jsonObject.dump();
233 }
234 
FromString(const std::string & ruleString,DisposedRule & rule)235 bool DisposedRule::FromString(const std::string &ruleString, DisposedRule &rule)
236 {
237     APP_LOGD("FromString %{public}s", ruleString.c_str());
238     nlohmann::json jsonObject = nlohmann::json::parse(ruleString, nullptr, false);
239     if (jsonObject.is_discarded()) {
240         APP_LOGE("failed parse ruleString: %{public}s", ruleString.c_str());
241         return false;
242     }
243     from_json(jsonObject, rule);
244     return true;
245 }
246 }  // namespace AppExecFwk
247 }  // namespace OHOS
248