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