1 /*
2 * Copyright (c) 2021 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 "want_params.h"
17
18 #include "ability_base_log_wrapper.h"
19 #include "ohos/aafwk/base/array_wrapper.h"
20 #include "ohos/aafwk/base/base_interfaces.h"
21 #include "ohos/aafwk/base/base_object.h"
22 #include "ohos/aafwk/base/bool_wrapper.h"
23 #include "ohos/aafwk/base/byte_wrapper.h"
24 #include "ohos/aafwk/base/double_wrapper.h"
25 #include "ohos/aafwk/base/float_wrapper.h"
26 #include "ohos/aafwk/base/int_wrapper.h"
27 #include "ohos/aafwk/base/long_wrapper.h"
28 #include "ohos/aafwk/base/short_wrapper.h"
29 #include "ohos/aafwk/base/string_wrapper.h"
30 #include "ohos/aafwk/base/remote_object_wrapper.h"
31 #include "ohos/aafwk/base/zchar_wrapper.h"
32 #include "parcel.h"
33 #include "securec.h"
34 #include "string_ex.h"
35 #include "want_params_wrapper.h"
36
37 namespace OHOS {
38 namespace AAFwk {
~UnsupportedData()39 UnsupportedData::~UnsupportedData()
40 {
41 if (buffer != nullptr) {
42 delete[] buffer;
43 buffer = nullptr;
44 }
45 }
46
47 UnsupportedData::UnsupportedData() = default;
48
UnsupportedData(const UnsupportedData & other)49 UnsupportedData::UnsupportedData(const UnsupportedData &other) : key(other.key), type(other.type), size(other.size)
50 {
51 buffer = new uint8_t[size];
52 if (memcpy_s(buffer, size, other.buffer, size) != EOK) {
53 ABILITYBASE_LOGI("copy construct fail due to memcpy");
54
55 key.clear();
56 type = 0;
57 size = 0;
58 delete[] buffer;
59 buffer = nullptr;
60 }
61 }
62
UnsupportedData(UnsupportedData && other)63 UnsupportedData::UnsupportedData(UnsupportedData &&other)
64 : key(std::move(other.key)), type(other.type), size(other.size), buffer(other.buffer)
65 {
66 other.type = 0;
67 other.size = 0;
68 other.buffer = nullptr;
69 }
70
operator =(const UnsupportedData & other)71 UnsupportedData &UnsupportedData::operator=(const UnsupportedData &other)
72 {
73 if (this == &other) {
74 return *this;
75 }
76 key = other.key;
77 type = other.type;
78 size = other.size;
79 buffer = new uint8_t[size];
80 if (memcpy_s(buffer, size, other.buffer, size) != EOK) {
81 ABILITYBASE_LOGI("copy assignment fail due to memcpy");
82
83 key.clear();
84 type = 0;
85 size = 0;
86 delete[] buffer;
87 buffer = nullptr;
88 }
89 return *this;
90 }
91
operator =(UnsupportedData && other)92 UnsupportedData &UnsupportedData::operator=(UnsupportedData &&other)
93 {
94 key = std::move(other.key);
95 type = other.type;
96 size = other.size;
97 std::swap(buffer, other.buffer);
98
99 other.type = 0;
100 other.size = 0;
101 if (other.buffer) {
102 delete[] other.buffer;
103 other.buffer = nullptr;
104 }
105 return *this;
106 }
107
GetStringByType(const sptr<IInterface> iIt,int typeId)108 std::string WantParams::GetStringByType(const sptr<IInterface> iIt, int typeId)
109 {
110 if (typeId == VALUE_TYPE_BOOLEAN) {
111 return static_cast<Boolean *>(IBoolean::Query(iIt))->ToString();
112 } else if (typeId == VALUE_TYPE_BYTE) {
113 return static_cast<Byte *>(IByte::Query(iIt))->ToString();
114 } else if (typeId == VALUE_TYPE_CHAR) {
115 return static_cast<Char *>(IChar::Query(iIt))->ToString();
116 } else if (typeId == VALUE_TYPE_SHORT) {
117 return static_cast<Short *>(IShort::Query(iIt))->ToString();
118 } else if (typeId == VALUE_TYPE_INT) {
119 return static_cast<Integer *>(IInteger::Query(iIt))->ToString();
120 } else if (typeId == VALUE_TYPE_LONG) {
121 return static_cast<Long *>(ILong::Query(iIt))->ToString();
122 } else if (typeId == VALUE_TYPE_FLOAT) {
123 return static_cast<Float *>(IFloat::Query(iIt))->ToString();
124 } else if (typeId == VALUE_TYPE_DOUBLE) {
125 return static_cast<Double *>(IDouble::Query(iIt))->ToString();
126 } else if (typeId == VALUE_TYPE_STRING) {
127 return static_cast<String *>(IString::Query(iIt))->ToString();
128 } else if (typeId == VALUE_TYPE_ARRAY) {
129 return static_cast<Array *>(IArray::Query(iIt))->ToString();
130 } else if (typeId == VALUE_TYPE_WANTPARAMS) {
131 return static_cast<WantParamWrapper *>(IWantParams::Query(iIt))->ToString();
132 } else {
133 return "";
134 }
135 return "";
136 }
137 template<typename T1, typename T2, typename T3>
138 static void SetNewArray(const AAFwk::InterfaceID &id, AAFwk::IArray *orgIArray, sptr<AAFwk::IArray> &ao);
139 /**
140 * @description: A constructor used to create an IntentParams instance by using the parameters of an existing
141 * IntentParams object.
142 * @param intentParams Indicates the existing IntentParams object.
143 */
WantParams(const WantParams & wantParams)144 WantParams::WantParams(const WantParams &wantParams)
145 {
146 params_.clear();
147 NewParams(wantParams, *this);
148 }
149 // inner use function
NewParams(const WantParams & source,WantParams & dest)150 bool WantParams::NewParams(const WantParams &source, WantParams &dest)
151 {
152 // Deep copy
153 for (auto it = source.params_.begin(); it != source.params_.end(); it++) {
154 sptr<IInterface> o = it->second;
155 if (IString::Query(o) != nullptr) {
156 dest.params_[it->first] = String::Box(String::Unbox(IString::Query(o)));
157 } else if (IBoolean::Query(o) != nullptr) {
158 dest.params_[it->first] = Boolean::Box(Boolean::Unbox(IBoolean::Query(o)));
159 } else if (IByte::Query(o) != nullptr) {
160 dest.params_[it->first] = Byte::Box(Byte::Unbox(IByte::Query(o)));
161 } else if (IChar::Query(o) != nullptr) {
162 dest.params_[it->first] = Char::Box(Char::Unbox(IChar::Query(o)));
163 } else if (IShort::Query(o) != nullptr) {
164 dest.params_[it->first] = Short::Box(Short::Unbox(IShort::Query(o)));
165 } else if (IInteger::Query(o) != nullptr) {
166 dest.params_[it->first] = Integer::Box(Integer::Unbox(IInteger::Query(o)));
167 } else if (ILong::Query(o) != nullptr) {
168 dest.params_[it->first] = Long::Box(Long::Unbox(ILong::Query(o)));
169 } else if (IFloat::Query(o) != nullptr) {
170 dest.params_[it->first] = Float::Box(Float::Unbox(IFloat::Query(o)));
171 } else if (IDouble::Query(o) != nullptr) {
172 dest.params_[it->first] = Double::Box(Double::Unbox(IDouble::Query(o)));
173 } else if (IRemoteObjectWrap::Query(o) != nullptr) {
174 dest.params_[it->first] = RemoteObjectWrap::Box(RemoteObjectWrap::UnBox(IRemoteObjectWrap::Query(o)));
175 } else if (IWantParams::Query(o) != nullptr) {
176 WantParams newDest(WantParamWrapper::Unbox(IWantParams::Query(o)));
177 dest.params_[it->first] = WantParamWrapper::Box(newDest);
178 } else if (IArray::Query(o) != nullptr) {
179 sptr<IArray> destAO = nullptr;
180 if (!NewArrayData(IArray::Query(o), destAO)) {
181 continue;
182 }
183 dest.params_[it->first] = destAO;
184 }
185 }
186 return true;
187 } // namespace AAFwk
188 // inner use
NewArrayData(IArray * source,sptr<IArray> & dest)189 bool WantParams::NewArrayData(IArray *source, sptr<IArray> &dest)
190 {
191 if (Array::IsBooleanArray(source)) {
192 SetNewArray<bool, AAFwk::Boolean, AAFwk::IBoolean>(AAFwk::g_IID_IBoolean, source, dest);
193 } else if (Array::IsCharArray(source)) {
194 SetNewArray<char, AAFwk::Char, AAFwk::IChar>(AAFwk::g_IID_IChar, source, dest);
195 } else if (Array::IsByteArray(source)) {
196 SetNewArray<byte, AAFwk::Byte, AAFwk::IByte>(AAFwk::g_IID_IByte, source, dest);
197 } else if (Array::IsShortArray(source)) {
198 SetNewArray<short, AAFwk::Short, AAFwk::IShort>(AAFwk::g_IID_IShort, source, dest);
199 } else if (Array::IsIntegerArray(source)) {
200 SetNewArray<int, AAFwk::Integer, AAFwk::IInteger>(AAFwk::g_IID_IInteger, source, dest);
201 } else if (Array::IsLongArray(source)) {
202 SetNewArray<long, AAFwk::Long, AAFwk::ILong>(AAFwk::g_IID_ILong, source, dest);
203 } else if (Array::IsFloatArray(source)) {
204 SetNewArray<float, AAFwk::Float, AAFwk::IFloat>(AAFwk::g_IID_IFloat, source, dest);
205 } else if (Array::IsDoubleArray(source)) {
206 SetNewArray<double, AAFwk::Double, AAFwk::IDouble>(AAFwk::g_IID_IDouble, source, dest);
207 } else if (Array::IsStringArray(source)) {
208 SetNewArray<std::string, AAFwk::String, AAFwk::IString>(AAFwk::g_IID_IString, source, dest);
209 } else {
210 return false;
211 }
212
213 if (dest == nullptr) {
214 return false;
215 }
216
217 return true;
218 }
219 /**
220 * @description: A WantParams used to
221 *
222 * @param intentParams Indicates the existing IntentParams object.
223 */
operator =(const WantParams & other)224 WantParams &WantParams::operator=(const WantParams &other)
225 {
226 if (this != &other) {
227 params_.clear();
228 NewParams(other, *this);
229 }
230 return *this;
231 }
operator ==(const WantParams & other)232 bool WantParams::operator==(const WantParams &other)
233 {
234 if (this->params_.size() != other.params_.size()) {
235 return false;
236 }
237 for (auto itthis : this->params_) {
238 auto itother = other.params_.find(itthis.first);
239 if (itother == other.params_.end()) {
240 return false;
241 }
242 if (!CompareInterface(itother->second, itthis.second, WantParams::GetDataType(itother->second))) {
243 return false;
244 }
245 }
246 return true;
247 }
248
GetDataType(const sptr<IInterface> iIt)249 int WantParams::GetDataType(const sptr<IInterface> iIt)
250 {
251 if (iIt != nullptr && IBoolean::Query(iIt) != nullptr) {
252 return VALUE_TYPE_BOOLEAN;
253 } else if (iIt != nullptr && IByte::Query(iIt) != nullptr) {
254 return VALUE_TYPE_BYTE;
255 } else if (iIt != nullptr && IChar::Query(iIt) != nullptr) {
256 return VALUE_TYPE_CHAR;
257 } else if (iIt != nullptr && IShort::Query(iIt) != nullptr) {
258 return VALUE_TYPE_SHORT;
259 } else if (iIt != nullptr && IInteger::Query(iIt) != nullptr) {
260 return VALUE_TYPE_INT;
261 } else if (iIt != nullptr && ILong::Query(iIt) != nullptr) {
262 return VALUE_TYPE_LONG;
263 } else if (iIt != nullptr && IFloat::Query(iIt) != nullptr) {
264 return VALUE_TYPE_FLOAT;
265 } else if (iIt != nullptr && IDouble::Query(iIt) != nullptr) {
266 return VALUE_TYPE_DOUBLE;
267 } else if (iIt != nullptr && IString::Query(iIt) != nullptr) {
268 return VALUE_TYPE_STRING;
269 } else if (iIt != nullptr && IArray::Query(iIt) != nullptr) {
270 return VALUE_TYPE_ARRAY;
271 } else if (iIt != nullptr && IWantParams::Query(iIt) != nullptr) {
272 return VALUE_TYPE_WANTPARAMS;
273 }
274
275 return VALUE_TYPE_NULL;
276 }
277
GetInterfaceByType(int typeId,const std::string & value)278 sptr<IInterface> WantParams::GetInterfaceByType(int typeId, const std::string &value)
279 {
280 if (typeId == VALUE_TYPE_BOOLEAN) {
281 return Boolean::Parse(value);
282 } else if (typeId == VALUE_TYPE_BYTE) {
283 return Byte::Parse(value);
284 } else if (typeId == VALUE_TYPE_CHAR) {
285 return Char::Parse(value);
286 } else if (typeId == VALUE_TYPE_SHORT) {
287 return Short::Parse(value);
288 } else if (typeId == VALUE_TYPE_INT) {
289 return Integer::Parse(value);
290 } else if (typeId == VALUE_TYPE_LONG) {
291 return Long::Parse(value);
292 } else if (typeId == VALUE_TYPE_FLOAT) {
293 return Float::Parse(value);
294 } else if (typeId == VALUE_TYPE_DOUBLE) {
295 return Double::Parse(value);
296 } else if (typeId == VALUE_TYPE_STRING) {
297 return String::Parse(value);
298 } else if (typeId == VALUE_TYPE_ARRAY) {
299 return Array::Parse(value);
300 }
301
302 return nullptr;
303 }
304
CompareInterface(const sptr<IInterface> iIt1,const sptr<IInterface> iIt2,int typeId)305 bool WantParams::CompareInterface(const sptr<IInterface> iIt1, const sptr<IInterface> iIt2, int typeId)
306 {
307 bool flag = false;
308 switch (typeId) {
309 case VALUE_TYPE_BOOLEAN:
310 flag =
311 static_cast<Boolean *>(IBoolean::Query(iIt1))->Equals(*(static_cast<Boolean *>(IBoolean::Query(iIt1))));
312 break;
313 case VALUE_TYPE_BYTE:
314 flag = static_cast<Byte *>(IByte::Query(iIt1))->Equals(*(static_cast<Byte *>(IByte::Query(iIt1))));
315 break;
316 case VALUE_TYPE_CHAR:
317 flag = static_cast<Char *>(IChar::Query(iIt1))->Equals(*(static_cast<Char *>(IChar::Query(iIt1))));
318 break;
319 case VALUE_TYPE_SHORT:
320 flag = static_cast<Short *>(IShort::Query(iIt1))->Equals(*(static_cast<Short *>(IShort::Query(iIt1))));
321 break;
322 case VALUE_TYPE_INT:
323 flag =
324 static_cast<Integer *>(IInteger::Query(iIt1))->Equals(*(static_cast<Integer *>(IInteger::Query(iIt1))));
325 break;
326 case VALUE_TYPE_LONG:
327 flag = static_cast<Long *>(ILong::Query(iIt1))->Equals(*(static_cast<Long *>(ILong::Query(iIt1))));
328 break;
329 case VALUE_TYPE_FLOAT:
330 flag = static_cast<Float *>(IFloat::Query(iIt1))->Equals(*(static_cast<Float *>(IFloat::Query(iIt1))));
331 break;
332 case VALUE_TYPE_DOUBLE:
333 flag = static_cast<Double *>(IDouble::Query(iIt1))->Equals(*(static_cast<Double *>(IDouble::Query(iIt1))));
334 break;
335 case VALUE_TYPE_STRING:
336 flag = static_cast<String *>(IString::Query(iIt1))->Equals(*(static_cast<String *>(IString::Query(iIt1))));
337 break;
338 case VALUE_TYPE_ARRAY:
339 flag = static_cast<Array *>(IArray::Query(iIt1))->Equals(*(static_cast<Array *>(IArray::Query(iIt1))));
340 break;
341 case VALUE_TYPE_WANTPARAMS:
342 flag = static_cast<WantParamWrapper *>(IWantParams::Query(iIt1))
343 ->Equals(*(static_cast<WantParamWrapper *>(IWantParams::Query(iIt1))));
344 break;
345 default:
346 break;
347 }
348 return flag;
349 }
350
351 /**
352 * @description: Sets a parameter in key-value pair format.
353 * @param key Indicates the key matching the parameter.
354 */
SetParam(const std::string & key,IInterface * value)355 void WantParams::SetParam(const std::string &key, IInterface *value)
356 {
357 params_[key] = value;
358 }
359
360 /**
361 * @description: Obtains the parameter value based on a given key.
362 * @param key Indicates the key matching the parameter.
363 * @return Returns the value matching the given key.
364 */
GetParam(const std::string & key) const365 sptr<IInterface> WantParams::GetParam(const std::string &key) const
366 {
367 auto it = params_.find(key);
368 if (it == params_.cend()) {
369 return nullptr;
370 }
371 return it->second;
372 }
373
374 /**
375 * @description: Obtains the parameter value based on a given key.
376 * @param key Indicates the key matching the parameter.
377 * @return Returns the value matching the given key.
378 */
379
GetParams() const380 const std::map<std::string, sptr<IInterface>> &WantParams::GetParams() const
381 {
382 return params_;
383 }
384
385 /**
386 * @description: Obtains a set of the keys of all parameters.
387 * @param
388 * @return Returns a set of keys.
389 */
KeySet() const390 const std::set<std::string> WantParams::KeySet() const
391 {
392 std::set<std::string> keySet;
393 keySet.clear();
394
395 for (auto it : params_) {
396 keySet.emplace(it.first);
397 }
398
399 return keySet;
400 }
401
402 /**
403 * @description: Removes the parameter matching the given key.
404 * @param key Indicates the key matching the parameter to be removed.
405 */
Remove(const std::string & key)406 void WantParams::Remove(const std::string &key)
407 {
408 params_.erase(key);
409 }
410
411 /**
412 * @description: Checks whether the Intent contains the given key.
413 * @param key Indicates the key to check.
414 * @return Returns true if the Intent contains the key; returns false otherwise.
415 */
HasParam(const std::string & key) const416 bool WantParams::HasParam(const std::string &key) const
417 {
418 return (params_.count(key) > 0);
419 }
420
421 /**
422 * @description: Obtains the number of parameters contained in this IntentParams object.
423 * @return Returns the number of parameters.
424 */
Size() const425 int WantParams::Size() const
426 {
427 return params_.size();
428 }
429
430 /**
431 * @description: Checks whether this IntentParams object contains no parameters.
432 * @return Returns true if this object does not contain any parameters; returns false otherwise.
433 */
IsEmpty() const434 bool WantParams::IsEmpty() const
435 {
436 return (params_.size() == 0);
437 }
438
WriteToParcelString(Parcel & parcel,sptr<IInterface> & o) const439 bool WantParams::WriteToParcelString(Parcel &parcel, sptr<IInterface> &o) const
440 {
441 std::string value = String::Unbox(IString::Query(o));
442 if (!parcel.WriteInt32(VALUE_TYPE_STRING)) {
443 return false;
444 }
445 return parcel.WriteString16(Str8ToStr16(value));
446 }
447
WriteToParcelBool(Parcel & parcel,sptr<IInterface> & o) const448 bool WantParams::WriteToParcelBool(Parcel &parcel, sptr<IInterface> &o) const
449 {
450 bool value = Boolean::Unbox(IBoolean::Query(o));
451 if (!parcel.WriteInt32(VALUE_TYPE_BOOLEAN)) {
452 return false;
453 }
454 return parcel.WriteInt8(value);
455 }
WriteToParcelWantParams(Parcel & parcel,sptr<IInterface> & o) const456 bool WantParams::WriteToParcelWantParams(Parcel &parcel, sptr<IInterface> &o) const
457 {
458 WantParams value = WantParamWrapper::Unbox(IWantParams::Query(o));
459
460 auto type = value.GetParam(TYPE_PROPERTY);
461 AAFwk::IString *typeP = AAFwk::IString::Query(type);
462 if (typeP != nullptr) {
463 std::string typeValue = AAFwk::String::Unbox(typeP);
464 if (typeValue == FD) {
465 return WriteToParcelFD(parcel, value);
466 }
467 if (typeValue == REMOTE_OBJECT) {
468 return WriteToParcelRemoteObject(parcel, value);
469 }
470 }
471
472 if (!parcel.WriteInt32(VALUE_TYPE_WANTPARAMS)) {
473 return false;
474 }
475 return parcel.WriteString16(Str8ToStr16(static_cast<WantParamWrapper *>(IWantParams::Query(o))->ToString()));
476 }
477
WriteToParcelFD(Parcel & parcel,const WantParams & value) const478 bool WantParams::WriteToParcelFD(Parcel &parcel, const WantParams &value) const
479 {
480 ABILITYBASE_LOGI("%{public}s called.", __func__);
481 if (!parcel.WriteInt32(VALUE_TYPE_FD)) {
482 return false;
483 }
484
485 auto fdWrap = value.GetParam(VALUE_PROPERTY);
486 AAFwk::IInteger *fdIWrap = AAFwk::IInteger::Query(fdWrap);
487 if (fdIWrap != nullptr) {
488 int fd = AAFwk::Integer::Unbox(fdIWrap);
489 auto messageParcel = static_cast<MessageParcel*>(&parcel);
490 bool ret = messageParcel->WriteFileDescriptor(fd);
491 ABILITYBASE_LOGI("%{public}s, WriteFileDescriptor fd:%{public}d, ret:%{public}d.", __func__, fd, ret);
492 return ret;
493 }
494
495 return false;
496 }
497
WriteToParcelRemoteObject(Parcel & parcel,const WantParams & value) const498 bool WantParams::WriteToParcelRemoteObject(Parcel &parcel, const WantParams &value) const
499 {
500 ABILITYBASE_LOGI("%{public}s called.", __func__);
501 if (!parcel.WriteInt32(VALUE_TYPE_REMOTE_OBJECT)) {
502 return false;
503 }
504
505 auto remoteObjectWrap = value.GetParam(VALUE_PROPERTY);
506 AAFwk::IRemoteObjectWrap *remoteObjectIWrap = AAFwk::IRemoteObjectWrap::Query(remoteObjectWrap);
507 if (remoteObjectIWrap != nullptr) {
508 auto remoteObject = AAFwk::RemoteObjectWrap::UnBox(remoteObjectIWrap);
509 auto messageParcel = static_cast<MessageParcel*>(&parcel);
510 bool ret = messageParcel->WriteRemoteObject(remoteObject);
511 ABILITYBASE_LOGI("%{public}s, WriteRemoteObject ret:%{public}d.", __func__, ret);
512 return ret;
513 }
514 return false;
515 }
516
WriteToParcelByte(Parcel & parcel,sptr<IInterface> & o) const517 bool WantParams::WriteToParcelByte(Parcel &parcel, sptr<IInterface> &o) const
518 {
519 byte value = Byte::Unbox(IByte::Query(o));
520 if (!parcel.WriteInt32(VALUE_TYPE_BYTE)) {
521 return false;
522 }
523 return parcel.WriteInt8(value);
524 }
525
WriteToParcelChar(Parcel & parcel,sptr<IInterface> & o) const526 bool WantParams::WriteToParcelChar(Parcel &parcel, sptr<IInterface> &o) const
527 {
528 zchar value = Char::Unbox(IChar::Query(o));
529 if (!parcel.WriteInt32(VALUE_TYPE_CHAR)) {
530 return false;
531 }
532 return parcel.WriteInt32(value);
533 }
534
WriteToParcelShort(Parcel & parcel,sptr<IInterface> & o) const535 bool WantParams::WriteToParcelShort(Parcel &parcel, sptr<IInterface> &o) const
536 {
537 short value = Short::Unbox(IShort::Query(o));
538 if (!parcel.WriteInt32(VALUE_TYPE_SHORT)) {
539 return false;
540 }
541 return parcel.WriteInt16(value);
542 }
543
WriteToParcelInt(Parcel & parcel,sptr<IInterface> & o) const544 bool WantParams::WriteToParcelInt(Parcel &parcel, sptr<IInterface> &o) const
545 {
546 int value = Integer::Unbox(IInteger::Query(o));
547 if (!parcel.WriteInt32(VALUE_TYPE_INT)) {
548 return false;
549 }
550 return parcel.WriteInt32(value);
551 }
552
WriteToParcelLong(Parcel & parcel,sptr<IInterface> & o) const553 bool WantParams::WriteToParcelLong(Parcel &parcel, sptr<IInterface> &o) const
554 {
555 long value = Long::Unbox(ILong::Query(o));
556 if (!parcel.WriteInt32(VALUE_TYPE_LONG)) {
557 return false;
558 }
559 return parcel.WriteInt64(value);
560 }
561
WriteToParcelFloat(Parcel & parcel,sptr<IInterface> & o) const562 bool WantParams::WriteToParcelFloat(Parcel &parcel, sptr<IInterface> &o) const
563 {
564 float value = Float::Unbox(IFloat::Query(o));
565 if (!parcel.WriteInt32(VALUE_TYPE_FLOAT)) {
566 return false;
567 }
568 return parcel.WriteFloat(value);
569 }
570
WriteToParcelDouble(Parcel & parcel,sptr<IInterface> & o) const571 bool WantParams::WriteToParcelDouble(Parcel &parcel, sptr<IInterface> &o) const
572 {
573 double value = Double::Unbox(IDouble::Query(o));
574 if (!parcel.WriteInt32(VALUE_TYPE_DOUBLE)) {
575 return false;
576 }
577 return parcel.WriteDouble(value);
578 }
579
WriteMarshalling(Parcel & parcel,sptr<IInterface> & o) const580 bool WantParams::WriteMarshalling(Parcel &parcel, sptr<IInterface> &o) const
581 {
582 if (IString::Query(o) != nullptr) {
583 return WriteToParcelString(parcel, o);
584 } else if (IBoolean::Query(o) != nullptr) {
585 return WriteToParcelBool(parcel, o);
586 } else if (IByte::Query(o) != nullptr) {
587 return WriteToParcelByte(parcel, o);
588 } else if (IChar::Query(o) != nullptr) {
589 return WriteToParcelChar(parcel, o);
590 } else if (IShort::Query(o) != nullptr) {
591 return WriteToParcelShort(parcel, o);
592 } else if (IInteger::Query(o) != nullptr) {
593 return WriteToParcelInt(parcel, o);
594 } else if (ILong::Query(o) != nullptr) {
595 return WriteToParcelLong(parcel, o);
596 } else if (IFloat::Query(o) != nullptr) {
597 return WriteToParcelFloat(parcel, o);
598 } else if (IDouble::Query(o) != nullptr) {
599 return WriteToParcelDouble(parcel, o);
600 } else if (IWantParams::Query(o) != nullptr) {
601 return WriteToParcelWantParams(parcel, o);
602 } else {
603 IArray *ao = IArray::Query(o);
604 if (ao != nullptr) {
605 sptr<IArray> array(ao);
606 return WriteArrayToParcel(parcel, array);
607 } else {
608 return true;
609 }
610 }
611 }
612
DoMarshalling(Parcel & parcel) const613 bool WantParams::DoMarshalling(Parcel &parcel) const
614 {
615 size_t size = params_.size();
616 if (!cachedUnsupportedData_.empty()) {
617 size += cachedUnsupportedData_.size();
618 }
619
620 if (!parcel.WriteInt32(size)) {
621 return false;
622 }
623
624 auto iter = params_.cbegin();
625 while (iter != params_.cend()) {
626 std::string key = iter->first;
627 sptr<IInterface> o = iter->second;
628 if (!parcel.WriteString16(Str8ToStr16(key))) {
629 return false;
630 }
631
632 if (!WriteMarshalling(parcel, o)) {
633 return false;
634 }
635 iter++;
636 }
637
638 if (!cachedUnsupportedData_.empty()) {
639 for (const UnsupportedData &data : cachedUnsupportedData_) {
640 if (!parcel.WriteString16(data.key)) {
641 return false;
642 }
643 if (!parcel.WriteInt32(data.type)) {
644 return false;
645 }
646 if (!parcel.WriteInt32(data.size)) {
647 return false;
648 }
649 // Corresponding to Parcel#writeByteArray() in Java.
650 if (!parcel.WriteInt32(data.size)) {
651 return false;
652 }
653 if (!parcel.WriteBuffer(data.buffer, data.size)) {
654 return false;
655 }
656 }
657 }
658 return true;
659 }
660
661 /**
662 * @description: Marshals an IntentParams object into a Parcel.
663 * @param Key-value pairs in the IntentParams are marshalled separately.
664 * @return If any key-value pair fails to be marshalled, false is returned.
665 */
Marshalling(Parcel & parcel) const666 bool WantParams::Marshalling(Parcel &parcel) const
667 {
668 return DoMarshalling(parcel);
669 }
670
671 template<typename dataType, typename className>
SetArray(const InterfaceID & id,const std::vector<dataType> & value,sptr<IArray> & ao)672 static bool SetArray(const InterfaceID &id, const std::vector<dataType> &value, sptr<IArray> &ao)
673 {
674 typename std::vector<dataType>::size_type size = value.size();
675 ao = new (std::nothrow) Array(size, id);
676 if (ao != nullptr) {
677 for (typename std::vector<dataType>::size_type i = 0; i < size; i++) {
678 ao->Set(i, className::Box(value[i]));
679 }
680 return true;
681 }
682 return false;
683 }
684
685 template<typename T1, typename T2, typename T3>
FillArray(IArray * ao,std::vector<T1> & array)686 static void FillArray(IArray *ao, std::vector<T1> &array)
687 {
688 auto func = [&](IInterface *object) {
689 if (object != nullptr) {
690 T3 *value = T3::Query(object);
691 if (value != nullptr) {
692 array.push_back(T2::Unbox(value));
693 }
694 }
695 };
696 Array::ForEach(ao, func);
697 }
698 // inner use template function
699 template<typename T1, typename T2, typename T3>
SetNewArray(const AAFwk::InterfaceID & id,AAFwk::IArray * orgIArray,sptr<AAFwk::IArray> & ao)700 static void SetNewArray(const AAFwk::InterfaceID &id, AAFwk::IArray *orgIArray, sptr<AAFwk::IArray> &ao)
701 {
702 if (orgIArray == nullptr) {
703 return;
704 }
705 std::vector<T1> array;
706 auto func = [&](IInterface *object) {
707 if (object != nullptr) {
708 T3 *value = T3::Query(object);
709 if (value != nullptr) {
710 array.push_back(T2::Unbox(value));
711 }
712 }
713 };
714 Array::ForEach(orgIArray, func);
715
716 typename std::vector<T1>::size_type size = array.size();
717 if (size > 0) {
718 ao = new (std::nothrow) AAFwk::Array(size, id);
719 if (ao != nullptr) {
720 for (typename std::vector<T1>::size_type i = 0; i < size; i++) {
721 ao->Set(i, T2::Box(array[i]));
722 }
723 }
724 }
725 }
726
WriteArrayToParcelString(Parcel & parcel,IArray * ao) const727 bool WantParams::WriteArrayToParcelString(Parcel &parcel, IArray *ao) const
728 {
729 if (ao == nullptr) {
730 return false;
731 }
732
733 std::vector<std::u16string> array;
734 auto func = [&](IInterface *object) {
735 std::string s = String::Unbox(IString::Query(object));
736 array.push_back(Str8ToStr16(s));
737 };
738
739 Array::ForEach(ao, func);
740
741 if (!parcel.WriteInt32(VALUE_TYPE_STRINGARRAY)) {
742 return false;
743 }
744 return parcel.WriteString16Vector(array);
745 }
746
WriteArrayToParcelBool(Parcel & parcel,IArray * ao) const747 bool WantParams::WriteArrayToParcelBool(Parcel &parcel, IArray *ao) const
748 {
749 if (ao == nullptr) {
750 return false;
751 }
752
753 std::vector<int8_t> array;
754 std::vector<int32_t> intArray;
755 FillArray<int8_t, Boolean, IBoolean>(ao, array);
756 if (!parcel.WriteInt32(VALUE_TYPE_BOOLEANARRAY)) {
757 return false;
758 }
759
760 for (std::vector<int8_t>::size_type i = 0; i < array.size(); i++) {
761 ABILITYBASE_LOGI("%{public}s bool of array: %{public}d", __func__, array[i]);
762 intArray.push_back(array[i]);
763 }
764 return parcel.WriteInt32Vector(intArray);
765 }
766
WriteArrayToParcelByte(Parcel & parcel,IArray * ao) const767 bool WantParams::WriteArrayToParcelByte(Parcel &parcel, IArray *ao) const
768 {
769 if (ao == nullptr) {
770 return false;
771 }
772
773 std::vector<int8_t> array;
774 FillArray<int8_t, Byte, IByte>(ao, array);
775 if (!parcel.WriteInt32(VALUE_TYPE_BYTEARRAY)) {
776 return false;
777 }
778 return parcel.WriteInt8Vector(array);
779 }
780
WriteArrayToParcelChar(Parcel & parcel,IArray * ao) const781 bool WantParams::WriteArrayToParcelChar(Parcel &parcel, IArray *ao) const
782 {
783 if (ao == nullptr) {
784 return false;
785 }
786
787 std::vector<int32_t> array;
788 FillArray<int32_t, Char, IChar>(ao, array);
789 if (!parcel.WriteInt32(VALUE_TYPE_CHARARRAY)) {
790 return false;
791 }
792 return parcel.WriteInt32Vector(array);
793 }
794
WriteArrayToParcelShort(Parcel & parcel,IArray * ao) const795 bool WantParams::WriteArrayToParcelShort(Parcel &parcel, IArray *ao) const
796 {
797 if (ao == nullptr) {
798 return false;
799 }
800
801 std::vector<short> array;
802 FillArray<short, Short, IShort>(ao, array);
803 if (!parcel.WriteInt32(VALUE_TYPE_SHORTARRAY)) {
804 return false;
805 }
806 return parcel.WriteInt16Vector(array);
807 }
808
WriteArrayToParcelInt(Parcel & parcel,IArray * ao) const809 bool WantParams::WriteArrayToParcelInt(Parcel &parcel, IArray *ao) const
810 {
811 if (ao == nullptr) {
812 return false;
813 }
814
815 std::vector<int> array;
816 FillArray<int, Integer, IInteger>(ao, array);
817 if (!parcel.WriteInt32(VALUE_TYPE_INTARRAY)) {
818 return false;
819 }
820 return parcel.WriteInt32Vector(array);
821 }
822
WriteArrayToParcelLong(Parcel & parcel,IArray * ao) const823 bool WantParams::WriteArrayToParcelLong(Parcel &parcel, IArray *ao) const
824 {
825 if (ao == nullptr) {
826 return false;
827 }
828
829 std::vector<int64_t> array;
830 FillArray<int64_t, Long, ILong>(ao, array);
831 if (!parcel.WriteInt32(VALUE_TYPE_LONGARRAY)) {
832 return false;
833 }
834 return parcel.WriteInt64Vector(array);
835 }
836
WriteArrayToParcelFloat(Parcel & parcel,IArray * ao) const837 bool WantParams::WriteArrayToParcelFloat(Parcel &parcel, IArray *ao) const
838 {
839 if (ao == nullptr) {
840 return false;
841 }
842
843 std::vector<float> array;
844 FillArray<float, Float, IFloat>(ao, array);
845 if (!parcel.WriteInt32(VALUE_TYPE_FLOATARRAY)) {
846 return false;
847 }
848 return parcel.WriteFloatVector(array);
849 }
850
WriteArrayToParcelDouble(Parcel & parcel,IArray * ao) const851 bool WantParams::WriteArrayToParcelDouble(Parcel &parcel, IArray *ao) const
852 {
853 if (ao == nullptr) {
854 return false;
855 }
856
857 std::vector<double> array;
858 FillArray<double, Double, IDouble>(ao, array);
859 if (!parcel.WriteInt32(VALUE_TYPE_DOUBLEARRAY)) {
860 return false;
861 }
862 return parcel.WriteDoubleVector(array);
863 }
864
WriteArrayToParcel(Parcel & parcel,IArray * ao) const865 bool WantParams::WriteArrayToParcel(Parcel &parcel, IArray *ao) const
866 {
867 if (Array::IsStringArray(ao)) {
868 return WriteArrayToParcelString(parcel, ao);
869 } else if (Array::IsBooleanArray(ao)) {
870 return WriteArrayToParcelBool(parcel, ao);
871 } else if (Array::IsByteArray(ao)) {
872 return WriteArrayToParcelByte(parcel, ao);
873 } else if (Array::IsCharArray(ao)) {
874 return WriteArrayToParcelChar(parcel, ao);
875 } else if (Array::IsShortArray(ao)) {
876 return WriteArrayToParcelShort(parcel, ao);
877 } else if (Array::IsIntegerArray(ao)) {
878 return WriteArrayToParcelInt(parcel, ao);
879 } else if (Array::IsLongArray(ao)) {
880 return WriteArrayToParcelLong(parcel, ao);
881 } else if (Array::IsFloatArray(ao)) {
882 return WriteArrayToParcelFloat(parcel, ao);
883 } else if (Array::IsDoubleArray(ao)) {
884 return WriteArrayToParcelDouble(parcel, ao);
885 } else {
886 return true;
887 }
888 }
889
ReadFromParcelArrayString(Parcel & parcel,sptr<IArray> & ao)890 bool WantParams::ReadFromParcelArrayString(Parcel &parcel, sptr<IArray> &ao)
891 {
892 ABILITYBASE_LOGI("%{public}s called.", __func__);
893 std::vector<std::u16string> value;
894 if (!parcel.ReadString16Vector(&value)) {
895 ABILITYBASE_LOGI("%{public}s read string of array fail.", __func__);
896 return false;
897 }
898
899 std::vector<std::u16string>::size_type size = value.size();
900 ao = new (std::nothrow) Array(size, g_IID_IString);
901 if (ao != nullptr) {
902 for (std::vector<std::u16string>::size_type i = 0; i < size; i++) {
903 ao->Set(i, String::Box(Str16ToStr8(value[i])));
904 }
905 return true;
906 } else {
907 ABILITYBASE_LOGI("%{public}s create string of array fail.", __func__);
908 }
909 return false;
910 }
911
ReadFromParcelArrayBool(Parcel & parcel,sptr<IArray> & ao)912 bool WantParams::ReadFromParcelArrayBool(Parcel &parcel, sptr<IArray> &ao)
913 {
914 ABILITYBASE_LOGI("%{public}s called.", __func__);
915 std::vector<int32_t> value;
916 std::vector<int8_t> boolValue;
917 if (!parcel.ReadInt32Vector(&value)) {
918 ABILITYBASE_LOGI("%{public}s read bool of array fail.", __func__);
919 return false;
920 }
921
922 std::vector<int32_t>::size_type size = value.size();
923 for (std::vector<int32_t>::size_type i = 0; i < size; i++) {
924 boolValue.push_back(value[i]);
925 }
926 return SetArray<int8_t, Boolean>(g_IID_IBoolean, boolValue, ao);
927 }
928
ReadFromParcelArrayByte(Parcel & parcel,sptr<IArray> & ao)929 bool WantParams::ReadFromParcelArrayByte(Parcel &parcel, sptr<IArray> &ao)
930 {
931 ABILITYBASE_LOGI("%{public}s called.", __func__);
932 std::vector<int8_t> value;
933 if (!parcel.ReadInt8Vector(&value)) {
934 ABILITYBASE_LOGI("%{public}s read byte of array fail.", __func__);
935 return false;
936 }
937 return SetArray<int8_t, Byte>(g_IID_IByte, value, ao);
938 }
939
ReadFromParcelArrayChar(Parcel & parcel,sptr<IArray> & ao)940 bool WantParams::ReadFromParcelArrayChar(Parcel &parcel, sptr<IArray> &ao)
941 {
942 ABILITYBASE_LOGI("%{public}s called.", __func__);
943 std::vector<int32_t> value;
944 if (!parcel.ReadInt32Vector(&value)) {
945 ABILITYBASE_LOGI("%{public}s char bool of array fail.", __func__);
946 return false;
947 }
948 return SetArray<int32_t, Char>(g_IID_IChar, value, ao);
949 }
950
ReadFromParcelArrayShort(Parcel & parcel,sptr<IArray> & ao)951 bool WantParams::ReadFromParcelArrayShort(Parcel &parcel, sptr<IArray> &ao)
952 {
953 ABILITYBASE_LOGI("%{public}s called.", __func__);
954 std::vector<short> value;
955 if (!parcel.ReadInt16Vector(&value)) {
956 ABILITYBASE_LOGI("%{public}s read short of array fail.", __func__);
957 return false;
958 }
959 return SetArray<short, Short>(g_IID_IShort, value, ao);
960 }
961
ReadFromParcelArrayInt(Parcel & parcel,sptr<IArray> & ao)962 bool WantParams::ReadFromParcelArrayInt(Parcel &parcel, sptr<IArray> &ao)
963 {
964 ABILITYBASE_LOGI("%{public}s called.", __func__);
965 std::vector<int> value;
966 if (!parcel.ReadInt32Vector(&value)) {
967 ABILITYBASE_LOGI("%{public}s read int of array fail.", __func__);
968 return false;
969 }
970 return SetArray<int, Integer>(g_IID_IInteger, value, ao);
971 }
972
ReadFromParcelArrayLong(Parcel & parcel,sptr<IArray> & ao)973 bool WantParams::ReadFromParcelArrayLong(Parcel &parcel, sptr<IArray> &ao)
974 {
975 ABILITYBASE_LOGI("%{public}s called.", __func__);
976 std::vector<int64_t> value;
977 if (!parcel.ReadInt64Vector(&value)) {
978 ABILITYBASE_LOGI("%{public}s read long of array fail.", __func__);
979 return false;
980 }
981
982 #ifdef WANT_PARAM_USE_LONG
983 return SetArray<int64_t, Long>(g_IID_ILong, value, ao);
984 #else
985 std::vector<std::string> strList;
986 for (size_t i = 0; i < value.size(); i++) {
987 strList.push_back(std::to_string(value[i]));
988 }
989 return SetArray<std::string, String>(g_IID_IString, strList, ao);
990 #endif
991 }
992
ReadFromParcelArrayFloat(Parcel & parcel,sptr<IArray> & ao)993 bool WantParams::ReadFromParcelArrayFloat(Parcel &parcel, sptr<IArray> &ao)
994 {
995 ABILITYBASE_LOGI("%{public}s called.", __func__);
996 std::vector<float> value;
997 if (!parcel.ReadFloatVector(&value)) {
998 ABILITYBASE_LOGI("%{public}s read float of array fail.", __func__);
999 return false;
1000 }
1001 return SetArray<float, Float>(g_IID_IFloat, value, ao);
1002 }
1003
ReadFromParcelArrayDouble(Parcel & parcel,sptr<IArray> & ao)1004 bool WantParams::ReadFromParcelArrayDouble(Parcel &parcel, sptr<IArray> &ao)
1005 {
1006 ABILITYBASE_LOGI("%{public}s called.", __func__);
1007 std::vector<double> value;
1008 if (!parcel.ReadDoubleVector(&value)) {
1009 ABILITYBASE_LOGI("%{public}s read double of array fail.", __func__);
1010 return false;
1011 }
1012 return SetArray<double, Double>(g_IID_IDouble, value, ao);
1013 }
1014
ReadArrayToParcel(Parcel & parcel,int type,sptr<IArray> & ao)1015 bool WantParams::ReadArrayToParcel(Parcel &parcel, int type, sptr<IArray> &ao)
1016 {
1017 switch (type) {
1018 case VALUE_TYPE_STRINGARRAY:
1019 case VALUE_TYPE_CHARSEQUENCEARRAY:
1020 ABILITYBASE_LOGI("%{public}s type=VALUE_TYPE_STRINGARRAY|VALUE_TYPE_CHARSEQUENCEARRAY.", __func__);
1021 return ReadFromParcelArrayString(parcel, ao);
1022 case VALUE_TYPE_BOOLEANARRAY:
1023 ABILITYBASE_LOGI("%{public}s type=VALUE_TYPE_BOOLEANARRAY.", __func__);
1024 return ReadFromParcelArrayBool(parcel, ao);
1025 case VALUE_TYPE_BYTEARRAY:
1026 ABILITYBASE_LOGI("%{public}s type=VALUE_TYPE_BYTEARRAY.", __func__);
1027 return ReadFromParcelArrayByte(parcel, ao);
1028 case VALUE_TYPE_CHARARRAY:
1029 ABILITYBASE_LOGI("%{public}s type=VALUE_TYPE_CHARARRAY.", __func__);
1030 return ReadFromParcelArrayChar(parcel, ao);
1031 case VALUE_TYPE_SHORTARRAY:
1032 ABILITYBASE_LOGI("%{public}s type=VALUE_TYPE_SHORTARRAY.", __func__);
1033 return ReadFromParcelArrayShort(parcel, ao);
1034 case VALUE_TYPE_INTARRAY:
1035 ABILITYBASE_LOGI("%{public}s type=VALUE_TYPE_INTARRAY.", __func__);
1036 return ReadFromParcelArrayInt(parcel, ao);
1037 case VALUE_TYPE_LONGARRAY:
1038 ABILITYBASE_LOGI("%{public}s type=VALUE_TYPE_LONGARRAY.", __func__);
1039 return ReadFromParcelArrayLong(parcel, ao);
1040 case VALUE_TYPE_FLOATARRAY:
1041 ABILITYBASE_LOGI("%{public}s type=VALUE_TYPE_FLOATARRAY.", __func__);
1042 return ReadFromParcelArrayFloat(parcel, ao);
1043 case VALUE_TYPE_DOUBLEARRAY:
1044 ABILITYBASE_LOGI("%{public}s type=VALUE_TYPE_DOUBLEARRAY.", __func__);
1045 return ReadFromParcelArrayDouble(parcel, ao);
1046 default:
1047 ABILITYBASE_LOGI("%{public}s type=UNKNOWN, nothing to do.", __func__);
1048 break;
1049 }
1050
1051 return true;
1052 }
1053
ReadFromParcelString(Parcel & parcel,const std::string & key)1054 bool WantParams::ReadFromParcelString(Parcel &parcel, const std::string &key)
1055 {
1056 std::u16string value = parcel.ReadString16();
1057 std::string strValue(Str16ToStr8(value));
1058 ABILITYBASE_LOGI("%{public}s key=%{public}s.", __func__, key.c_str());
1059 sptr<IInterface> intf = String::Box(Str16ToStr8(value));
1060 if (intf) {
1061 SetParam(key, intf);
1062 } else {
1063 ABILITYBASE_LOGI("%{public}s read data fail: key=%{public}s", __func__, key.c_str());
1064 }
1065 return true;
1066 }
1067
ReadFromParcelBool(Parcel & parcel,const std::string & key)1068 bool WantParams::ReadFromParcelBool(Parcel &parcel, const std::string &key)
1069 {
1070 ABILITYBASE_LOGI("%{public}s key=%{public}s.", __func__, key.c_str());
1071 int8_t value;
1072 if (parcel.ReadInt8(value)) {
1073 sptr<IInterface> intf = Boolean::Box(value);
1074 if (intf) {
1075 SetParam(key, intf);
1076 } else {
1077 ABILITYBASE_LOGI("%{public}s insert param fail: key=%{public}s", __func__, key.c_str());
1078 }
1079 return true;
1080 } else {
1081 ABILITYBASE_LOGI("%{public}s read data fail: key=%{public}s", __func__, key.c_str());
1082 return false;
1083 }
1084 }
1085
ReadFromParcelInt8(Parcel & parcel,const std::string & key)1086 bool WantParams::ReadFromParcelInt8(Parcel &parcel, const std::string &key)
1087 {
1088 ABILITYBASE_LOGI("%{public}s key=%{public}s.", __func__, key.c_str());
1089 int8_t value;
1090 if (parcel.ReadInt8(value)) {
1091 sptr<IInterface> intf = Byte::Box(value);
1092 if (intf) {
1093 SetParam(key, intf);
1094 } else {
1095 ABILITYBASE_LOGI("%{public}s insert param fail: key=%{public}s", __func__, key.c_str());
1096 }
1097 return true;
1098 } else {
1099 ABILITYBASE_LOGI("%{public}s read data fail: key=%{public}s", __func__, key.c_str());
1100 return false;
1101 }
1102 }
1103
ReadFromParcelChar(Parcel & parcel,const std::string & key)1104 bool WantParams::ReadFromParcelChar(Parcel &parcel, const std::string &key)
1105 {
1106 ABILITYBASE_LOGI("%{public}s key=%{public}s.", __func__, key.c_str());
1107 int32_t value;
1108 if (parcel.ReadInt32(value)) {
1109 sptr<IInterface> intf = Char::Box(value);
1110 if (intf) {
1111 SetParam(key, intf);
1112 } else {
1113 ABILITYBASE_LOGI("%{public}s insert param fail: key=%{public}s", __func__, key.c_str());
1114 }
1115 return true;
1116 } else {
1117 ABILITYBASE_LOGI("%{public}s read data fail: key=%{public}s", __func__, key.c_str());
1118 return false;
1119 }
1120 }
1121
ReadFromParcelShort(Parcel & parcel,const std::string & key)1122 bool WantParams::ReadFromParcelShort(Parcel &parcel, const std::string &key)
1123 {
1124 ABILITYBASE_LOGI("%{public}s key=%{public}s.", __func__, key.c_str());
1125 short value;
1126 if (parcel.ReadInt16(value)) {
1127 sptr<IInterface> intf = Short::Box(value);
1128 if (intf) {
1129 SetParam(key, intf);
1130 } else {
1131 ABILITYBASE_LOGI("%{public}s insert param fail: key=%{public}s", __func__, key.c_str());
1132 }
1133 return true;
1134 } else {
1135 ABILITYBASE_LOGI("%{public}s read data fail: key=%{public}s", __func__, key.c_str());
1136 return false;
1137 }
1138 }
1139
ReadFromParcelInt(Parcel & parcel,const std::string & key)1140 bool WantParams::ReadFromParcelInt(Parcel &parcel, const std::string &key)
1141 {
1142 ABILITYBASE_LOGI("%{public}s key=%{public}s.", __func__, key.c_str());
1143 int value;
1144 if (parcel.ReadInt32(value)) {
1145 sptr<IInterface> intf = Integer::Box(value);
1146 if (intf) {
1147 SetParam(key, intf);
1148 } else {
1149 ABILITYBASE_LOGI("%{public}s insert param fail: key=%{public}s", __func__, key.c_str());
1150 }
1151 return true;
1152 } else {
1153 ABILITYBASE_LOGI("%{public}s read data fail: key=%{public}s", __func__, key.c_str());
1154 return false;
1155 }
1156 }
ReadFromParcelWantParamWrapper(Parcel & parcel,const std::string & key,int type)1157 bool WantParams::ReadFromParcelWantParamWrapper(Parcel &parcel, const std::string &key, int type)
1158 {
1159 if (type == VALUE_TYPE_FD) {
1160 return ReadFromParcelFD(parcel, key);
1161 }
1162
1163 if (type == VALUE_TYPE_REMOTE_OBJECT) {
1164 return ReadFromParcelRemoteObject(parcel, key);
1165 }
1166
1167 std::u16string value = parcel.ReadString16();
1168 sptr<IInterface> intf = WantParamWrapper::Parse(Str16ToStr8(value));
1169 if (intf) {
1170 SetParam(key, intf);
1171 }
1172 return true;
1173 }
1174
ReadFromParcelFD(Parcel & parcel,const std::string & key)1175 bool WantParams::ReadFromParcelFD(Parcel &parcel, const std::string &key)
1176 {
1177 ABILITYBASE_LOGI("%{public}s called.", __func__);
1178 auto messageParcel = static_cast<MessageParcel*>(&parcel);
1179 auto fd = messageParcel->ReadFileDescriptor();
1180 ABILITYBASE_LOGI("%{public}s fd:%{public}d.", __func__, fd);
1181 WantParams wp;
1182 wp.SetParam(TYPE_PROPERTY, String::Box(FD));
1183 wp.SetParam(VALUE_PROPERTY, Integer::Box(fd));
1184 sptr<AAFwk::IWantParams> pWantParams = AAFwk::WantParamWrapper::Box(wp);
1185 SetParam(key, pWantParams);
1186 return true;
1187 }
1188
ReadFromParcelRemoteObject(Parcel & parcel,const std::string & key)1189 bool WantParams::ReadFromParcelRemoteObject(Parcel &parcel, const std::string &key)
1190 {
1191 ABILITYBASE_LOGI("%{public}s called.", __func__);
1192 auto messageParcel = static_cast<MessageParcel*>(&parcel);
1193 auto remoteObject = messageParcel->ReadRemoteObject();
1194 WantParams wp;
1195 wp.SetParam(TYPE_PROPERTY, String::Box(REMOTE_OBJECT));
1196 wp.SetParam(VALUE_PROPERTY, RemoteObjectWrap::Box(remoteObject));
1197 sptr<AAFwk::IWantParams> pWantParams = AAFwk::WantParamWrapper::Box(wp);
1198 SetParam(key, pWantParams);
1199 return true;
1200 }
1201
ReadFromParcelLong(Parcel & parcel,const std::string & key)1202 bool WantParams::ReadFromParcelLong(Parcel &parcel, const std::string &key)
1203 {
1204 ABILITYBASE_LOGI("%{public}s key=%{public}s.", __func__, key.c_str());
1205 int64_t value;
1206 if (parcel.ReadInt64(value)) {
1207 std::string strValue(std::to_string(value));
1208 #ifdef WANT_PARAM_USE_LONG
1209 sptr<IInterface> intf = Long::Box(value);
1210 #else
1211 sptr<IInterface> intf = String::Box(std::to_string(value));
1212 #endif
1213 if (intf) {
1214 SetParam(key, intf);
1215 } else {
1216 ABILITYBASE_LOGI("%{public}s insert param fail: key=%{public}s", __func__, key.c_str());
1217 }
1218 return true;
1219 } else {
1220 ABILITYBASE_LOGI("%{public}s read data fail: key=%{public}s", __func__, key.c_str());
1221 return false;
1222 }
1223 }
1224
ReadFromParcelFloat(Parcel & parcel,const std::string & key)1225 bool WantParams::ReadFromParcelFloat(Parcel &parcel, const std::string &key)
1226 {
1227 ABILITYBASE_LOGI("%{public}s key=%{public}s.", __func__, key.c_str());
1228 float value;
1229 if (parcel.ReadFloat(value)) {
1230 sptr<IInterface> intf = Float::Box(value);
1231 if (intf) {
1232 SetParam(key, intf);
1233 } else {
1234 ABILITYBASE_LOGI("%{public}s insert param fail: key=%{public}s", __func__, key.c_str());
1235 }
1236 return true;
1237 } else {
1238 ABILITYBASE_LOGI("%{public}s read data fail: key=%{public}s", __func__, key.c_str());
1239 return false;
1240 }
1241 }
1242
ReadFromParcelDouble(Parcel & parcel,const std::string & key)1243 bool WantParams::ReadFromParcelDouble(Parcel &parcel, const std::string &key)
1244 {
1245 ABILITYBASE_LOGI("%{public}s key=%{public}s.", __func__, key.c_str());
1246 double value;
1247 if (parcel.ReadDouble(value)) {
1248 sptr<IInterface> intf = Double::Box(value);
1249 if (intf) {
1250 SetParam(key, intf);
1251 } else {
1252 ABILITYBASE_LOGI("%{public}s insert param fail: key=%{public}s", __func__, key.c_str());
1253 }
1254 return true;
1255 } else {
1256 ABILITYBASE_LOGI("%{public}s read data fail: key=%{public}s", __func__, key.c_str());
1257 return false;
1258 }
1259 }
1260
ReadUnsupportedData(Parcel & parcel,const std::string & key,int type)1261 bool WantParams::ReadUnsupportedData(Parcel &parcel, const std::string &key, int type)
1262 {
1263 int32_t bufferSize = 0;
1264 if (!parcel.ReadInt32(bufferSize)) {
1265 return false;
1266 }
1267 static constexpr int32_t maxAllowedSize = 100 * 1024 * 1024;
1268 if (bufferSize < 0 || bufferSize > maxAllowedSize) {
1269 ABILITYBASE_LOGE("%{public}s invalid size: %{public}d", __func__, bufferSize);
1270 return false;
1271 }
1272
1273 // Corresponding to Parcel#writeByteArray() in Java.
1274 int32_t length = 0;
1275 if (!parcel.ReadInt32(length)) {
1276 return false;
1277 }
1278 const uint8_t *bufferP = parcel.ReadUnpadBuffer(bufferSize);
1279 if (bufferP == nullptr) {
1280 return false;
1281 }
1282
1283 UnsupportedData data;
1284 data.key = Str8ToStr16(key);
1285 data.type = type;
1286 data.size = bufferSize;
1287 data.buffer = new (std::nothrow) uint8_t[bufferSize];
1288 if (data.buffer == nullptr) {
1289 return false;
1290 }
1291
1292 if (memcpy_s(data.buffer, bufferSize, bufferP, bufferSize) != EOK) {
1293 return false;
1294 }
1295 cachedUnsupportedData_.emplace_back(std::move(data));
1296 return true;
1297 }
1298
ReadFromParcelParam(Parcel & parcel,const std::string & key,int type)1299 bool WantParams::ReadFromParcelParam(Parcel &parcel, const std::string &key, int type)
1300 {
1301 switch (type) {
1302 case VALUE_TYPE_CHARSEQUENCE:
1303 case VALUE_TYPE_STRING:
1304 return ReadFromParcelString(parcel, key);
1305 case VALUE_TYPE_BOOLEAN:
1306 return ReadFromParcelBool(parcel, key);
1307 case VALUE_TYPE_BYTE:
1308 return ReadFromParcelInt8(parcel, key);
1309 case VALUE_TYPE_CHAR:
1310 return ReadFromParcelChar(parcel, key);
1311 case VALUE_TYPE_SHORT:
1312 return ReadFromParcelShort(parcel, key);
1313 case VALUE_TYPE_INT:
1314 return ReadFromParcelInt(parcel, key);
1315 case VALUE_TYPE_LONG:
1316 return ReadFromParcelLong(parcel, key);
1317 case VALUE_TYPE_FLOAT:
1318 return ReadFromParcelFloat(parcel, key);
1319 case VALUE_TYPE_DOUBLE:
1320 return ReadFromParcelDouble(parcel, key);
1321 case VALUE_TYPE_WANTPARAMS:
1322 case VALUE_TYPE_FD:
1323 case VALUE_TYPE_REMOTE_OBJECT:
1324 return ReadFromParcelWantParamWrapper(parcel, key, type);
1325 case VALUE_TYPE_NULL:
1326 break;
1327 case VALUE_TYPE_PARCELABLE:
1328 case VALUE_TYPE_PARCELABLEARRAY:
1329 case VALUE_TYPE_SERIALIZABLE:
1330 case VALUE_TYPE_LIST:
1331 if (!ReadUnsupportedData(parcel, key, type)) {
1332 return false;
1333 }
1334 break;
1335 default: {
1336 // handle array
1337 sptr<IArray> ao = nullptr;
1338 if (!ReadArrayToParcel(parcel, type, ao)) {
1339 return false;
1340 }
1341 sptr<IInterface> intf = ao;
1342 if (intf) {
1343 SetParam(key, intf);
1344 }
1345 break;
1346 }
1347 }
1348 return true;
1349 }
1350
ReadFromParcel(Parcel & parcel)1351 bool WantParams::ReadFromParcel(Parcel &parcel)
1352 {
1353 int32_t size;
1354 if (!parcel.ReadInt32(size)) {
1355 ABILITYBASE_LOGI("%{public}s read size fail.", __func__);
1356 return false;
1357 }
1358 ABILITYBASE_LOGI("%{public}s size=%{public}d.", __func__, size);
1359 for (int32_t i = 0; i < size; i++) {
1360 ABILITYBASE_LOGI("%{public}s get i=%{public}d", __func__, i);
1361 std::u16string key = parcel.ReadString16();
1362 int type;
1363 if (!parcel.ReadInt32(type)) {
1364 ABILITYBASE_LOGI("%{public}s read type fail.", __func__);
1365 return false;
1366 }
1367 if (!ReadFromParcelParam(parcel, Str16ToStr8(key), type)) {
1368 ABILITYBASE_LOGI("%{public}s get i=%{public}d fail.", __func__, i);
1369 return false;
1370 }
1371 }
1372 return true;
1373 }
1374
1375 /**
1376 * @description: Unmarshals an IntentParams object from a Parcel.
1377 * @param Key-value pairs in the IntentParams are unmarshalled separately.
1378 * @return If any key-value pair fails to be unmarshalled, false is returned.
1379 */
Unmarshalling(Parcel & parcel)1380 WantParams *WantParams::Unmarshalling(Parcel &parcel)
1381 {
1382 WantParams *wantParams = new (std::nothrow) WantParams();
1383 if (!wantParams->ReadFromParcel(parcel)) {
1384 delete wantParams;
1385 wantParams = nullptr;
1386 }
1387 return wantParams;
1388 }
1389
DumpInfo(int level) const1390 void WantParams::DumpInfo(int level) const
1391 {
1392 ABILITYBASE_LOGI("=======WantParams::DumpInfo level: %{public}d start=============", level);
1393 size_t params_size = params_.size();
1394 ABILITYBASE_LOGI("===WantParams::params_: count %{public}u =============", (uint32_t)params_size);
1395 int typeId = VALUE_TYPE_NULL;
1396 for (auto it : params_) {
1397 typeId = VALUE_TYPE_NULL;
1398 typeId = WantParams::GetDataType(it.second);
1399 if (typeId != VALUE_TYPE_NULL) {
1400 std::string value = WantParams::GetStringByType(it.second, typeId);
1401 ABILITYBASE_LOGI(
1402 "=WantParams::params_[%{public}s] : %{private}s =============", it.first.c_str(), value.c_str());
1403 } else {
1404 ABILITYBASE_LOGI("=WantParams::params_[%{public}s] : type error =============", it.first.c_str());
1405 }
1406 }
1407 ABILITYBASE_LOGI("=======WantParams::DumpInfo level: %{public}d end=============", level);
1408 }
1409 } // namespace AAFwk
1410 } // namespace OHOS
1411