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.h"
17
18 #include <algorithm>
19 #include <climits>
20 #include <cstdlib>
21 #include <regex>
22 #include <securec.h>
23
24 #include "base_object.h"
25 #include "bool_wrapper.h"
26 #include "zchar_wrapper.h"
27 #include "byte_wrapper.h"
28 #include "short_wrapper.h"
29 #include "int_wrapper.h"
30 #include "long_wrapper.h"
31 #include "float_wrapper.h"
32 #include "double_wrapper.h"
33 #include "string_wrapper.h"
34 #include "zchar_wrapper.h"
35 #include "array_wrapper.h"
36 #include "parcel_macro_base.h"
37 #include "remote_object_wrapper.h"
38 #include "string_ex.h"
39 #include "want_params_wrapper.h"
40
41 using namespace OHOS::AppExecFwk;
42 using OHOS::AppExecFwk::ElementName;
43
44 namespace OHOS {
45 namespace AAFwk {
46 namespace {
47 const std::regex NUMBER_REGEX("^[-+]?([0-9]+)([.]([0-9]+))?$");
48 }; // namespace
49 const std::string Want::ACTION_PLAY("action.system.play");
50 const std::string Want::ACTION_HOME("action.system.home");
51
52 const std::string Want::ENTITY_HOME("entity.system.home");
53 const std::string Want::ENTITY_VIDEO("entity.system.video");
54 const std::string Want::FLAG_HOME_INTENT_FROM_SYSTEM("flag.home.intent.from.system");
55 const std::string Want::ENTITY_MUSIC("entity.app.music");
56 const std::string Want::ENTITY_EMAIL("entity.app.email");
57 const std::string Want::ENTITY_CONTACTS("entity.app.contacts");
58 const std::string Want::ENTITY_MAPS("entity.app.maps");
59 const std::string Want::ENTITY_BROWSER("entity.app.browser");
60 const std::string Want::ENTITY_CALENDAR("entity.app.calendar");
61 const std::string Want::ENTITY_MESSAGING("entity.app.messaging");
62 const std::string Want::ENTITY_FILES("entity.app.files");
63 const std::string Want::ENTITY_GALLERY("entity.app.gallery");
64
65 const std::string Want::OCT_EQUALSTO("075"); // '='
66 const std::string Want::OCT_SEMICOLON("073"); // ';'
67 const std::string Want::MIME_TYPE("mime-type");
68 const std::string Want::WANT_HEADER("#Intent;");
69 const std::string Want::WANT_END(";end");
70
71 const std::string Want::PARAM_RESV_WINDOW_MODE("ohos.aafwk.param.windowMode");
72 const std::string Want::PARAM_RESV_DISPLAY_ID("ohos.aafwk.param.displayId");
73 const std::string Want::PARAM_RESV_CALLER_TOKEN("ohos.aafwk.param.callerToken");
74 const std::string Want::PARAM_RESV_CALLER_UID("ohos.aafwk.param.callerUid");
75 const std::string Want::PARAM_RESV_CALLER_PID("ohos.aafwk.param.callerPid");
76 const std::string Want::PARAM_RESV_FOR_RESULT("ohos.aafwk.param.startAbilityForResult");
77 const std::string Want::PARAM_RESV_CALL_TO_FOREGROUND("ohos.aafwk.param.callAbilityToForeground");
78 const std::string Want::PARAM_RESV_REQUEST_PROC_CODE("ohos.aafwk.param.requestProcCode");
79 const std::string Want::PARAM_RESV_REQUEST_TOKEN_CODE("ohos.aafwk.param.requestTokenCode");
80 const std::string Want::PARAM_RESV_ABILITY_INFO_CALLBACK("ohos.aafwk.param.abilityInfoCallback");
81 const std::string Want::PARAM_RESV_START_RECENT("ohos.aafwk.param.startRecent");
82
83 const std::string Want::PARAM_ABILITY_RECOVERY_RESTART("ohos.aafwk.param.AbilityRecoveryRestart");
84 const std::string Want::PARAM_MODULE_NAME("moduleName");
85
86 const std::string Want::PARAM_BACK_TO_OTHER_MISSION_STACK("ability.params.backToOtherMissionStack");
87
88 /**
89 * @description:Default construcotr of Want class, which is used to initialzie flags and URI.
90 * @param None
91 * @return None
92 */
Want()93 Want::Want()
94 {}
95
96 /**
97 * @description: Default deconstructor of Want class
98 * @param None
99 * @return None
100 */
~Want()101 Want::~Want()
102 {}
103
104 /**
105 * @description: Copy construcotr of Want class, which is used to initialzie flags, URI, etc.
106 * @param want the source instance of Want.
107 * @return None
108 */
Want(const Want & want)109 Want::Want(const Want &want)
110 {
111 operation_ = want.operation_;
112 parameters_ = want.parameters_;
113 }
114
operator =(const Want & want)115 Want &Want::operator=(const Want &want)
116 {
117 operation_ = want.operation_;
118 parameters_ = want.parameters_;
119 return *this;
120 }
121
122 /**
123 * @description: Obtains the description of flags in a Want.
124 * @return Returns the flag description in the Want.
125 */
GetFlags() const126 unsigned int Want::GetFlags() const
127 {
128 return operation_.GetFlags();
129 }
130
131 /**
132 * @description: Sets a flag in a Want.
133 * @param flags Indicates the flag to set.
134 * @return Returns this Want object containing the flag.
135 */
SetFlags(unsigned int flags)136 Want &Want::SetFlags(unsigned int flags)
137 {
138 operation_.SetFlags(flags);
139 return *this;
140 }
141
142 /**
143 * @description: Adds a flag to a Want.
144 * @param flags Indicates the flag to add.
145 * @return Returns the Want object with the added flag.
146 */
AddFlags(unsigned int flags)147 Want &Want::AddFlags(unsigned int flags)
148 {
149 operation_.AddFlags(flags);
150 return *this;
151 }
152
153 /**
154 * @description: Removes the description of a flag from a Want.
155 * @param flags Indicates the flag to remove.
156 * @return Removes the description of a flag from a Want.
157 */
RemoveFlags(unsigned int flags)158 void Want::RemoveFlags(unsigned int flags)
159 {
160 operation_.RemoveFlags(flags);
161 }
162
163 /**
164 * @description: Obtains the description of the ElementName object in a Want.
165 * @return Returns the ElementName description in the Want.
166 */
GetElement() const167 OHOS::AppExecFwk::ElementName Want::GetElement() const
168 {
169 return ElementName(operation_.GetDeviceId(), operation_.GetBundleName(),
170 operation_.GetAbilityName(), operation_.GetModuleName());
171 }
172
173 /**
174 * @description: Sets the bundleName and abilityName attributes for this Want object.
175 * @param bundleName Indicates the bundleName to set for the operation attribute in the Want.
176 * @param abilityName Indicates the abilityName to set for the operation attribute in the Want.
177 * @return Returns this Want object that contains the specified bundleName and abilityName attributes.
178 */
SetElementName(const std::string & bundleName,const std::string & abilityName)179 Want &Want::SetElementName(const std::string &bundleName, const std::string &abilityName)
180 {
181 operation_.SetBundleName(bundleName);
182 operation_.SetAbilityName(abilityName);
183 return *this;
184 }
185
186 /**
187 * @description: Sets the bundleName and abilityName attributes for this Want object.
188 * @param deviceId Indicates the deviceId to set for the operation attribute in the Want.
189 * @param bundleName Indicates the bundleName to set for the operation attribute in the Want.
190 * @param abilityName Indicates the abilityName to set for the operation attribute in the Want.
191 * @return Returns this Want object that contains the specified bundleName and abilityName attributes.
192 */
SetElementName(const std::string & deviceId,const std::string & bundleName,const std::string & abilityName,const std::string & moduleName)193 Want &Want::SetElementName(const std::string &deviceId, const std::string &bundleName,
194 const std::string &abilityName, const std::string &moduleName)
195 {
196 operation_.SetDeviceId(deviceId);
197 operation_.SetBundleName(bundleName);
198 operation_.SetAbilityName(abilityName);
199 operation_.SetModuleName(moduleName);
200 SetParam(PARAM_MODULE_NAME, moduleName);
201 return *this;
202 }
203
204 /**
205 * @description: Sets an ElementName object in a Want.
206 * @param element Indicates the ElementName description.
207 * @return Returns this Want object containing the ElementName
208 */
SetElement(const OHOS::AppExecFwk::ElementName & element)209 Want &Want::SetElement(const OHOS::AppExecFwk::ElementName &element)
210 {
211 operation_.SetDeviceId(element.GetDeviceID());
212 operation_.SetBundleName(element.GetBundleName());
213 operation_.SetAbilityName(element.GetAbilityName());
214 operation_.SetModuleName(element.GetModuleName());
215 SetParam(PARAM_MODULE_NAME, element.GetModuleName());
216 return *this;
217 }
218
219 /**
220 * @description: Obtains the description of all entities in a Want.
221 * @return Returns a set of entities
222 */
GetEntities() const223 const std::vector<std::string> &Want::GetEntities() const
224 {
225 return operation_.GetEntities();
226 }
227
228 /**
229 * @description: Adds the description of an entity to a Want
230 * @param entity Indicates the entity description to add
231 * @return Returns this Want object containing the entity.
232 */
AddEntity(const std::string & entity)233 Want &Want::AddEntity(const std::string &entity)
234 {
235 operation_.AddEntity(entity);
236 return *this;
237 }
238
239 /**
240 * @description: Removes the description of an entity from a Want
241 * @param entity Indicates the entity description to remove.
242 * @return void
243 */
RemoveEntity(const std::string & entity)244 void Want::RemoveEntity(const std::string &entity)
245 {
246 operation_.RemoveEntity(entity);
247 }
248
249 /**
250 * @description: Checks whether a Want contains the given entity
251 * @param entity Indicates the entity to check
252 * @return Returns true if the given entity is contained; returns false otherwise
253 */
HasEntity(const std::string & entity) const254 bool Want::HasEntity(const std::string &entity) const
255 {
256 return operation_.HasEntity(entity);
257 }
258
259 /**
260 * @description: Obtains the number of entities in a Want
261 * @return Returns the entity quantity
262 */
CountEntities()263 int Want::CountEntities()
264 {
265 return operation_.CountEntities();
266 }
267
268 /**
269 * @description: Obtains the name of the bundle specified in a Want
270 * @return Returns the bundle name specified in the Want
271 */
GetBundle() const272 std::string Want::GetBundle() const
273 {
274 return operation_.GetBundleName();
275 }
276
277 /**
278 * @description: Sets a bundle name in this Want.
279 * If a bundle name is specified in a Want, the Want will match only
280 * the abilities in the specified bundle. You cannot use this method and
281 * setPicker(ohos.aafwk.content.Want) on the same Want.
282 * @param bundleName Indicates the bundle name to set.
283 * @return Returns a Want object containing the specified bundle name.
284 */
SetBundle(const std::string & bundleName)285 Want &Want::SetBundle(const std::string &bundleName)
286 {
287 operation_.SetBundleName(bundleName);
288 return *this;
289 }
290
291 /**
292 * @description: Obtains the description of the type in this Want
293 * @return Returns the type description in this Want
294 */
GetType() const295 std::string Want::GetType() const
296 {
297 auto value = parameters_.GetParam(MIME_TYPE);
298 IString *ao = IString::Query(value);
299 if (ao != nullptr) {
300 return String::Unbox(ao);
301 }
302 return std::string();
303 }
304
305 /**
306 * @description: Sets the description of a type in this Want
307 * @param type Indicates the type description
308 * @return Returns this Want object containing the type
309 */
SetType(const std::string & type)310 Want &Want::SetType(const std::string &type)
311 {
312 sptr<IString> valueObj = String::Parse(type);
313 parameters_.SetParam(MIME_TYPE, valueObj);
314 return *this;
315 }
316
317 /**
318 * @description: Formats a specified MIME type. This method uses
319 * the formatMimeType(java.lang.String) method to format a MIME type
320 * and then saves the formatted type to this Want object.
321 * @param type Indicates the MIME type to format
322 * @return Returns this Want object that contains the formatted type attribute
323 */
FormatType(const std::string & type)324 Want &Want::FormatType(const std::string &type)
325 {
326 std::string typetemp = FormatMimeType(type);
327 SetType(typetemp);
328
329 return *this;
330 }
331
332 /**
333 * @description: Convert the scheme of URI to lower-case, and return the uri which be converted.
334 * @param uri Indicates the URI to format.
335 * @return Returns this URI Object.
336 */
GetLowerCaseScheme(const Uri & uri)337 Uri Want::GetLowerCaseScheme(const Uri &uri)
338 {
339 std::string strUri = const_cast<Uri &>(uri).ToString();
340 std::string schemeStr = const_cast<Uri &>(uri).GetScheme();
341 if (strUri.empty() || schemeStr.empty()) {
342 return uri;
343 }
344
345 std::string lowSchemeStr = schemeStr;
346 std::transform(lowSchemeStr.begin(), lowSchemeStr.end(), lowSchemeStr.begin(), [](unsigned char c) {
347 return std::tolower(c);
348 });
349
350 if (schemeStr == lowSchemeStr) {
351 return uri;
352 }
353
354 std::size_t pos = strUri.find_first_of(schemeStr, 0);
355 if (pos != std::string::npos) {
356 strUri.replace(pos, schemeStr.length(), lowSchemeStr);
357 }
358
359 return Uri(strUri);
360 }
361
362 /**
363 * @description: Formats a specified URI and MIME type.
364 * This method works in the same way as formatUri(ohos.utils.net.URI)
365 * and formatType(java.lang.String).
366 * @param uri Indicates the URI to format.
367 * @param type Indicates the MIME type to format.
368 * @return Returns this Want object that contains the formatted URI and type attributes.
369 */
FormatUriAndType(const Uri & uri,const std::string & type)370 Want &Want::FormatUriAndType(const Uri &uri, const std::string &type)
371 {
372 return SetUriAndType(GetLowerCaseScheme(uri), FormatMimeType(type));
373 }
374
375 /**
376 * @description: This method formats data of a specified MIME type
377 * by removing spaces from the data and converting the data into
378 * lowercase letters. You can use this method to normalize
379 * the external data used to create Want information.
380 * @param type Indicates the MIME type to format
381 * @return Returns this Want object that contains the formatted type attribute
382 */
FormatMimeType(const std::string & mimeType)383 std::string Want::FormatMimeType(const std::string &mimeType)
384 {
385 std::string strMimeType = mimeType;
386 strMimeType.erase(std::remove(strMimeType.begin(), strMimeType.end(), ' '), strMimeType.end());
387
388 std::transform(
389 strMimeType.begin(), strMimeType.end(), strMimeType.begin(), [](unsigned char c) { return std::tolower(c); });
390
391 std::size_t pos = 0;
392 std::size_t begin = 0;
393 pos = strMimeType.find_first_of(";", begin);
394 if (pos != std::string::npos) {
395 strMimeType = strMimeType.substr(begin, pos - begin);
396 }
397
398 return strMimeType;
399 }
400
401 /**
402 * @description: Obtains the description of an action in a want.
403 * @return Returns the action description in the want.
404 */
GetAction() const405 std::string Want::GetAction() const
406 {
407 return operation_.GetAction();
408 }
409
410 /**
411 * @description: Sets the description of an action in a want.
412 * @param action Indicates the action description.
413 * @return Returns this want object containing the action.
414 */
SetAction(const std::string & action)415 Want &Want::SetAction(const std::string &action)
416 {
417 operation_.SetAction(action);
418 return *this;
419 }
420
421 /**
422 * @description: Obtains the description of the URI scheme in this want.
423 * @return Returns the URI scheme description in this want.
424 */
GetScheme() const425 const std::string Want::GetScheme() const
426 {
427 return operation_.GetUri().GetScheme();
428 }
429
430 /**
431 * @description: Creates a want with its corresponding attributes specified for starting the main ability of an
432 * application.
433 * @param ElementName Indicates the ElementName object defining the deviceId, bundleName,
434 * and abilityName sub-attributes of the operation attribute in a want.
435 * @return Returns the want object used to start the main ability of an application.
436 */
MakeMainAbility(const OHOS::AppExecFwk::ElementName & elementName)437 Want *Want::MakeMainAbility(const OHOS::AppExecFwk::ElementName &elementName)
438 {
439 Want *want = new (std::nothrow) Want();
440
441 if (want != nullptr) {
442 want->SetAction(ACTION_HOME);
443 want->AddEntity(ENTITY_HOME);
444 want->SetElement(elementName);
445 } else {
446 return nullptr;
447 }
448
449 return want;
450 }
451
452 /**
453 * @description: Obtains the description of the WantParams object in a Want
454 * @return Returns the WantParams description in the Want
455 */
GetParams() const456 const WantParams &Want::GetParams() const
457 {
458 return parameters_;
459 }
460
461 /**
462 * @description: Sets a wantParams object in a want.
463 * @param wantParams Indicates the wantParams description.
464 * @return Returns this want object containing the wantParams.
465 */
SetParams(const WantParams & wantParams)466 Want &Want::SetParams(const WantParams &wantParams)
467 {
468 parameters_ = wantParams;
469 return *this;
470 }
471
472 /**
473 * @description: Obtains a bool-type value matching the given key.
474 * @param key Indicates the key of WantParams.
475 * @param defaultValue Indicates the default bool-type value.
476 * @return Returns the bool-type value of the parameter matching the given key;
477 * returns the default value if the key does not exist.
478 */
GetBoolParam(const std::string & key,bool defaultValue) const479 bool Want::GetBoolParam(const std::string &key, bool defaultValue) const
480 {
481 auto value = parameters_.GetParam(key);
482 IBoolean *bo = IBoolean::Query(value);
483 if (bo != nullptr) {
484 return Boolean::Unbox(bo);
485 }
486 return defaultValue;
487 }
488
489 /**
490 * @description:Obtains a bool-type array matching the given key.
491 * @param key Indicates the key of WantParams.
492 * @return Returns the bool-type array of the parameter matching the given key;
493 * returns null if the key does not exist.
494 */
GetBoolArrayParam(const std::string & key) const495 std::vector<bool> Want::GetBoolArrayParam(const std::string &key) const
496 {
497 std::vector<bool> array;
498 auto value = parameters_.GetParam(key);
499 IArray *ao = IArray::Query(value);
500 if (ao != nullptr && Array::IsBooleanArray(ao)) {
501 auto func = [&](IInterface *object) {
502 if (object != nullptr) {
503 IBoolean *value = IBoolean::Query(object);
504 if (value != nullptr) {
505 array.push_back(Boolean::Unbox(value));
506 }
507 }
508 };
509
510 Array::ForEach(ao, func);
511 }
512 return array;
513 }
514
SetParam(const std::string & key,const sptr<IRemoteObject> & remoteObject)515 Want& Want::SetParam(const std::string& key, const sptr<IRemoteObject>& remoteObject)
516 {
517 WantParams wp;
518 wp.SetParam(AAFwk::TYPE_PROPERTY, AAFwk::String::Box(AAFwk::REMOTE_OBJECT));
519 wp.SetParam(AAFwk::VALUE_PROPERTY, AAFwk::RemoteObjectWrap::Box(remoteObject));
520 parameters_.SetParam(key, WantParamWrapper::Box(wp));
521 return *this;
522 }
523
GetRemoteObject(const std::string & key) const524 sptr<IRemoteObject> Want::GetRemoteObject(const std::string &key) const
525 {
526 auto value = parameters_.GetParam(key);
527 IWantParams* iwp = IWantParams::Query(value);
528 if (iwp == nullptr) {
529 ABILITYBASE_LOGI("%{public}s is invalid.", key.c_str());
530 return nullptr;
531 }
532 auto wp = WantParamWrapper::Unbox(iwp);
533
534 auto type = wp.GetParam(TYPE_PROPERTY);
535 IString* iString = IString::Query(type);
536 if (iString == nullptr) {
537 ABILITYBASE_LOGI("it not contains TYPE_PROPERTY.");
538 return nullptr;
539 }
540 if (REMOTE_OBJECT != String::Unbox(iString)) {
541 ABILITYBASE_LOGE("invalid type.");
542 return nullptr;
543 }
544
545 auto remoteObjVal = wp.GetParam(VALUE_PROPERTY);
546 IRemoteObjectWrap* iRemoteObj = IRemoteObjectWrap::Query(remoteObjVal);
547 if (iRemoteObj == nullptr) {
548 ABILITYBASE_LOGE("it not contains VALUE_PROPERTY.");
549 return nullptr;
550 }
551 return RemoteObjectWrap::UnBox(iRemoteObj);
552 }
553
554 /**
555 * @description: Sets a parameter value of the boolean type.
556 * @param key Indicates the key matching the parameter.
557 * @param value Indicates the boolean value of the parameter.
558 * @return Returns this want object containing the parameter value.
559 */
SetParam(const std::string & key,bool value)560 Want &Want::SetParam(const std::string &key, bool value)
561 {
562 parameters_.SetParam(key, Boolean::Box(value));
563 return *this;
564 }
565
566 /**
567 * @description: Sets a parameter value of the boolean array type.
568 * @param key Indicates the key matching the parameter.
569 * @param value Indicates the boolean array of the parameter.
570 * @return Returns this want object containing the parameter value.
571 */
SetParam(const std::string & key,const std::vector<bool> & value)572 Want &Want::SetParam(const std::string &key, const std::vector<bool> &value)
573 {
574 std::size_t size = value.size();
575 sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IBoolean);
576 if (ao != nullptr) {
577 for (std::size_t i = 0; i < size; i++) {
578 ao->Set(i, Boolean::Box(value[i]));
579 }
580 parameters_.SetParam(key, ao);
581 }
582
583 return *this;
584 }
585
586 /**
587 * @description: Obtains a byte-type value matching the given key.
588 * @param key Indicates the key of WantParams.
589 * @param defaultValue Indicates the default byte-type value.
590 * @return Returns the byte-type value of the parameter matching the given key;
591 * returns the default value if the key does not exist.
592 */
GetByteParam(const std::string & key,const byte defaultValue) const593 byte Want::GetByteParam(const std::string &key, const byte defaultValue) const
594 {
595 auto value = parameters_.GetParam(key);
596 IByte *bo = IByte::Query(value);
597 if (bo != nullptr) {
598 return Byte::Unbox(bo);
599 }
600 return defaultValue;
601 }
602
603 /**
604 * @description: Obtains a byte-type array matching the given key.
605 * @param key Indicates the key of WantParams.
606 * @return Returns the byte-type array of the parameter matching the given key;
607 * returns null if the key does not exist.
608 */
GetByteArrayParam(const std::string & key) const609 std::vector<byte> Want::GetByteArrayParam(const std::string &key) const
610 {
611 std::vector<byte> array;
612 auto value = parameters_.GetParam(key);
613 IArray *ao = IArray::Query(value);
614 if (ao != nullptr && Array::IsByteArray(ao)) {
615 auto func = [&](IInterface *object) {
616 if (object != nullptr) {
617 IByte *value = IByte::Query(object);
618 if (value != nullptr) {
619 array.push_back(Byte::Unbox(value));
620 }
621 }
622 };
623 Array::ForEach(ao, func);
624 }
625 return array;
626 }
627
628 /**
629 * @description: Sets a parameter value of the byte type.
630 * @param key Indicates the key matching the parameter.
631 * @param value Indicates the byte-type value of the parameter.
632 * @return Returns this Want object containing the parameter value.
633 */
SetParam(const std::string & key,byte value)634 Want &Want::SetParam(const std::string &key, byte value)
635 {
636 parameters_.SetParam(key, Byte::Box(value));
637 return *this;
638 }
639
640 /**
641 * @description: Sets a parameter value of the byte array type.
642 * @param key Indicates the key matching the parameter.
643 * @param value Indicates the byte array of the parameter.
644 * @return Returns this Want object containing the parameter value.
645 */
SetParam(const std::string & key,const std::vector<byte> & value)646 Want &Want::SetParam(const std::string &key, const std::vector<byte> &value)
647 {
648 std::size_t size = value.size();
649 sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IByte);
650 if (ao == nullptr) {
651 return *this;
652 }
653 for (std::size_t i = 0; i < size; i++) {
654 ao->Set(i, Byte::Box(value[i]));
655 }
656 parameters_.SetParam(key, ao);
657 return *this;
658 }
659
660 /**
661 * @description: Obtains a char value matching the given key.
662 * @param key Indicates the key of wnatParams.
663 * @param value Indicates the default char value.
664 * @return Returns the char value of the parameter matching the given key;
665 * returns the default value if the key does not exist.
666 */
GetCharParam(const std::string & key,zchar defaultValue) const667 zchar Want::GetCharParam(const std::string &key, zchar defaultValue) const
668 {
669 auto value = parameters_.GetParam(key);
670 IChar *ao = IChar::Query(value);
671 if (ao != nullptr) {
672 return Char::Unbox(ao);
673 }
674 return defaultValue;
675 }
676
677 /**
678 * @description: Obtains a char array matching the given key.
679 * @param key Indicates the key of wantParams.
680 * @return Returns the char array of the parameter matching the given key;
681 * returns null if the key does not exist.
682 */
GetCharArrayParam(const std::string & key) const683 std::vector<zchar> Want::GetCharArrayParam(const std::string &key) const
684 {
685 std::vector<zchar> array;
686 auto value = parameters_.GetParam(key);
687 IArray *ao = IArray::Query(value);
688 if (ao != nullptr && Array::IsCharArray(ao)) {
689 auto func = [&](IInterface *object) {
690 if (object != nullptr) {
691 IChar *value = IChar::Query(object);
692 if (value != nullptr) {
693 array.push_back(Char::Unbox(value));
694 }
695 }
696 };
697 Array::ForEach(ao, func);
698 }
699 return array;
700 }
701
702 /**
703 * @description: Sets a parameter value of the char type.
704 * @param key Indicates the key of wantParams.
705 * @param value Indicates the char value of the parameter.
706 * @return Returns this want object containing the parameter value.
707 */
SetParam(const std::string & key,zchar value)708 Want &Want::SetParam(const std::string &key, zchar value)
709 {
710 parameters_.SetParam(key, Char::Box(value));
711 return *this;
712 }
713
714 /**
715 * @description: Sets a parameter value of the char array type.
716 * @param key Indicates the key of wantParams.
717 * @param value Indicates the char array of the parameter.
718 * @return Returns this want object containing the parameter value.
719 */
SetParam(const std::string & key,const std::vector<zchar> & value)720 Want &Want::SetParam(const std::string &key, const std::vector<zchar> &value)
721 {
722 std::size_t size = value.size();
723 sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IChar);
724 if (ao == nullptr) {
725 return *this;
726 }
727 for (std::size_t i = 0; i < size; i++) {
728 ao->Set(i, Char::Box(value[i]));
729 }
730 parameters_.SetParam(key, ao);
731 return *this;
732 }
733
734 /**
735 * @description: Obtains an int value matching the given key.
736 * @param key Indicates the key of wantParams.
737 * @param value Indicates the default int value.
738 * @return Returns the int value of the parameter matching the given key;
739 * returns the default value if the key does not exist.
740 */
GetIntParam(const std::string & key,const int defaultValue) const741 int Want::GetIntParam(const std::string &key, const int defaultValue) const
742 {
743 auto value = parameters_.GetParam(key);
744 IInteger *ao = IInteger::Query(value);
745 if (ao != nullptr) {
746 return Integer::Unbox(ao);
747 }
748 return defaultValue;
749 }
750
751 /**
752 * @description: Obtains an int array matching the given key.
753 * @param key Indicates the key of wantParams.
754 * @return Returns the int array of the parameter matching the given key;
755 * returns null if the key does not exist.
756 */
GetIntArrayParam(const std::string & key) const757 std::vector<int> Want::GetIntArrayParam(const std::string &key) const
758 {
759 std::vector<int> array;
760 auto value = parameters_.GetParam(key);
761 IArray *ao = IArray::Query(value);
762 if (ao != nullptr && Array::IsIntegerArray(ao)) {
763 auto func = [&](IInterface *object) {
764 if (object != nullptr) {
765 IInteger *value = IInteger::Query(object);
766 if (value != nullptr) {
767 array.push_back(Integer::Unbox(value));
768 }
769 }
770 };
771 Array::ForEach(ao, func);
772 }
773 return array;
774 }
775
776 /**
777 * @description: Sets a parameter value of the int type.
778 * @param key Indicates the key matching the parameter.
779 * @param value Indicates the int value of the parameter.
780 * @return Returns this Want object containing the parameter value.
781 */
SetParam(const std::string & key,int value)782 Want &Want::SetParam(const std::string &key, int value)
783 {
784 parameters_.SetParam(key, Integer::Box(value));
785 return *this;
786 }
787
788 /**
789 * @description: Sets a parameter value of the int array type.
790 * @param key Indicates the key matching the parameter.
791 * @param value Indicates the int array of the parameter.
792 * @return Returns this Want object containing the parameter value.
793 */
SetParam(const std::string & key,const std::vector<int> & value)794 Want &Want::SetParam(const std::string &key, const std::vector<int> &value)
795 {
796 std::size_t size = value.size();
797 sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IInteger);
798 if (ao == nullptr) {
799 return *this;
800 }
801 for (std::size_t i = 0; i < size; i++) {
802 ao->Set(i, Integer::Box(value[i]));
803 }
804 parameters_.SetParam(key, ao);
805 return *this;
806 }
807
808 /**
809 * @description: Obtains a double value matching the given key.
810 * @param key Indicates the key of wantParams.
811 * @param defaultValue Indicates the default double value.
812 * @return Returns the double value of the parameter matching the given key;
813 * returns the default value if the key does not exist.
814 */
GetDoubleParam(const std::string & key,double defaultValue) const815 double Want::GetDoubleParam(const std::string &key, double defaultValue) const
816 {
817 auto value = parameters_.GetParam(key);
818 IDouble *ao = IDouble::Query(value);
819 if (ao != nullptr) {
820 return Double::Unbox(ao);
821 }
822 return defaultValue;
823 }
824
825 /**
826 * @description: Obtains a double array matching the given key.
827 * @param key Indicates the key of WantParams.
828 * @return Returns the double array of the parameter matching the given key;
829 * returns null if the key does not exist.
830 */
GetDoubleArrayParam(const std::string & key) const831 std::vector<double> Want::GetDoubleArrayParam(const std::string &key) const
832 {
833 std::vector<double> array;
834 auto value = parameters_.GetParam(key);
835 IArray *ao = IArray::Query(value);
836 if (ao != nullptr && Array::IsDoubleArray(ao)) {
837 auto func = [&](IInterface *object) {
838 if (object != nullptr) {
839 IDouble *value = IDouble::Query(object);
840 if (value != nullptr) {
841 array.push_back(Double::Unbox(value));
842 }
843 }
844 };
845 Array::ForEach(ao, func);
846 }
847 return array;
848 }
849
850 /**
851 * @description: Sets a parameter value of the double type.
852 * @param key Indicates the key matching the parameter.
853 * @param value Indicates the int value of the parameter.
854 * @return Returns this Want object containing the parameter value.
855 */
SetParam(const std::string & key,double value)856 Want &Want::SetParam(const std::string &key, double value)
857 {
858 parameters_.SetParam(key, Double::Box(value));
859 return *this;
860 }
861
862 /**
863 * @description: Sets a parameter value of the double array type.
864 * @param key Indicates the key matching the parameter.
865 * @param value Indicates the double array of the parameter.
866 * @return Returns this want object containing the parameter value.
867 */
SetParam(const std::string & key,const std::vector<double> & value)868 Want &Want::SetParam(const std::string &key, const std::vector<double> &value)
869 {
870 std::size_t size = value.size();
871 sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IDouble);
872 if (ao == nullptr) {
873 return *this;
874 }
875 for (std::size_t i = 0; i < size; i++) {
876 ao->Set(i, Double::Box(value[i]));
877 }
878 parameters_.SetParam(key, ao);
879 return *this;
880 }
881
882 /**
883 * @description: Obtains a float value matching the given key.
884 * @param key Indicates the key of wnatParams.
885 * @param value Indicates the default float value.
886 * @return Returns the float value of the parameter matching the given key;
887 * returns the default value if the key does not exist.
888 */
GetFloatParam(const std::string & key,float defaultValue) const889 float Want::GetFloatParam(const std::string &key, float defaultValue) const
890 {
891 auto value = parameters_.GetParam(key);
892 IFloat *ao = IFloat::Query(value);
893 if (ao != nullptr) {
894 return Float::Unbox(ao);
895 }
896 return defaultValue;
897 }
898
899 /**
900 * @description: Obtains a float array matching the given key.
901 * @param key Indicates the key of WantParams.
902 * @return Obtains a float array matching the given key.
903 */
GetFloatArrayParam(const std::string & key) const904 std::vector<float> Want::GetFloatArrayParam(const std::string &key) const
905 {
906 std::vector<float> array;
907 auto value = parameters_.GetParam(key);
908 IArray *ao = IArray::Query(value);
909 if (ao != nullptr && Array::IsFloatArray(ao)) {
910 auto func = [&](IInterface *object) {
911 if (object != nullptr) {
912 IFloat *value = IFloat::Query(object);
913 if (value != nullptr) {
914 array.push_back(Float::Unbox(value));
915 }
916 }
917 };
918 Array::ForEach(ao, func);
919 }
920 return array;
921 }
922
923 /**
924 * @description: Sets a parameter value of the float type.
925 * @param key Indicates the key matching the parameter.
926 * @param value Indicates the byte-type value of the parameter.
927 * @return Returns this Want object containing the parameter value.
928 */
SetParam(const std::string & key,float value)929 Want &Want::SetParam(const std::string &key, float value)
930 {
931 parameters_.SetParam(key, Float::Box(value));
932 return *this;
933 }
934
935 /**
936 * @description: Sets a parameter value of the float array type.
937 * @param key Indicates the key matching the parameter.
938 * @param value Indicates the byte-type value of the parameter.
939 * @return Returns this Want object containing the parameter value.
940 */
SetParam(const std::string & key,const std::vector<float> & value)941 Want &Want::SetParam(const std::string &key, const std::vector<float> &value)
942 {
943 std::size_t size = value.size();
944 sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IFloat);
945 if (ao == nullptr) {
946 return *this;
947 }
948
949 for (std::size_t i = 0; i < size; i++) {
950 ao->Set(i, Float::Box(value[i]));
951 }
952 parameters_.SetParam(key, ao);
953 return *this;
954 }
955
956 /**
957 * @description: Obtains a long value matching the given key.
958 * @param key Indicates the key of wantParams.
959 * @param value Indicates the default long value.
960 * @return Returns the long value of the parameter matching the given key;
961 * returns the default value if the key does not exist.
962 */
GetLongParam(const std::string & key,long defaultValue) const963 long Want::GetLongParam(const std::string &key, long defaultValue) const
964 {
965 auto value = parameters_.GetParam(key);
966 if (ILong::Query(value) != nullptr) {
967 return Long::Unbox(ILong::Query(value));
968 } else if (IString::Query(value) != nullptr) {
969 // Marshalling
970 std::string str = String::Unbox(IString::Query(value));
971 if (std::regex_match(str, NUMBER_REGEX)) {
972 return std::atoll(str.c_str());
973 }
974 }
975
976 return defaultValue;
977 }
ArrayAddData(IInterface * object,std::vector<long> & array)978 void ArrayAddData(IInterface *object, std::vector<long> &array)
979 {
980 if (object == nullptr) {
981 return;
982 }
983
984 IString *o = IString::Query(object);
985 if (o != nullptr) {
986 std::string str = String::Unbox(o);
987 if (std::regex_match(str, NUMBER_REGEX)) {
988 array.push_back(std::atoll(str.c_str()));
989 }
990 }
991 }
992 /**
993 * @description: Obtains a long array matching the given key.
994 * @param key Indicates the key of wantParams.
995 * @return Returns the long array of the parameter matching the given key;
996 * returns null if the key does not exist.
997 */
GetLongArrayParam(const std::string & key) const998 std::vector<long> Want::GetLongArrayParam(const std::string &key) const
999 {
1000 std::vector<long> array;
1001 auto value = parameters_.GetParam(key);
1002 IArray *ao = IArray::Query(value);
1003 if (ao != nullptr && Array::IsLongArray(ao)) {
1004 auto func = [&](IInterface *object) {
1005 if (object != nullptr) {
1006 ILong *value = ILong::Query(object);
1007 if (value != nullptr) {
1008 array.push_back(Long::Unbox(value));
1009 }
1010 }
1011 };
1012 Array::ForEach(ao, func);
1013 } else if (ao != nullptr && Array::IsStringArray(ao)) {
1014 // Marshalling
1015 auto func = [&](IInterface *object) { ArrayAddData(object, array); };
1016 Array::ForEach(ao, func);
1017 }
1018
1019 return array;
1020 }
1021
1022 /**
1023 * @description: Sets a parameter value of the long type.
1024 * @param key Indicates the key matching the parameter.
1025 * @param value Indicates the byte-type value of the parameter.
1026 * @return Returns this Want object containing the parameter value.
1027 */
SetParam(const std::string & key,long value)1028 Want &Want::SetParam(const std::string &key, long value)
1029 {
1030 parameters_.SetParam(key, Long::Box(value));
1031 return *this;
1032 }
1033
1034 /**
1035 * @description: Sets a parameter value of the long array type.
1036 * @param key Indicates the key matching the parameter.
1037 * @param value Indicates the byte-type value of the parameter.
1038 * @return Returns this Want object containing the parameter value.
1039 */
SetParam(const std::string & key,const std::vector<long> & value)1040 Want &Want::SetParam(const std::string &key, const std::vector<long> &value)
1041 {
1042 std::size_t size = value.size();
1043 sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_ILong);
1044 if (ao == nullptr) {
1045 return *this;
1046 }
1047 for (std::size_t i = 0; i < size; i++) {
1048 ao->Set(i, Long::Box(value[i]));
1049 }
1050 parameters_.SetParam(key, ao);
1051 return *this;
1052 }
1053
SetParam(const std::string & key,long long value)1054 Want &Want::SetParam(const std::string &key, long long value)
1055 {
1056 parameters_.SetParam(key, Long::Box(value));
1057 return *this;
1058 }
1059
1060 /**
1061 * @description: a short value matching the given key.
1062 * @param key Indicates the key of wantParams.
1063 * @param defaultValue Indicates the default short value.
1064 * @return Returns the short value of the parameter matching the given key;
1065 * returns the default value if the key does not exist.
1066 */
GetShortParam(const std::string & key,short defaultValue) const1067 short Want::GetShortParam(const std::string &key, short defaultValue) const
1068 {
1069 auto value = parameters_.GetParam(key);
1070 IShort *ao = IShort::Query(value);
1071 if (ao != nullptr) {
1072 return Short::Unbox(ao);
1073 }
1074 return defaultValue;
1075 }
1076
1077 /**
1078 * @description: Obtains a short array matching the given key.
1079 * @param key Indicates the key of wantParams.
1080 * @return Returns the short array of the parameter matching the given key;
1081 * returns null if the key does not exist.
1082 */
GetShortArrayParam(const std::string & key) const1083 std::vector<short> Want::GetShortArrayParam(const std::string &key) const
1084 {
1085 std::vector<short> array;
1086 auto value = parameters_.GetParam(key);
1087 IArray *ao = IArray::Query(value);
1088 if (ao != nullptr && Array::IsShortArray(ao)) {
1089 auto func = [&](IInterface *object) {
1090 if (object != nullptr) {
1091 IShort *value = IShort::Query(object);
1092 if (value != nullptr) {
1093 array.push_back(Short::Unbox(value));
1094 }
1095 }
1096 };
1097 Array::ForEach(ao, func);
1098 }
1099 return array;
1100 }
1101
1102 /**
1103 * @description: Sets a parameter value of the short type.
1104 * @param key Indicates the key matching the parameter.
1105 * @param value Indicates the byte-type value of the parameter.
1106 * @return Returns this Want object containing the parameter value.
1107 */
SetParam(const std::string & key,short value)1108 Want &Want::SetParam(const std::string &key, short value)
1109 {
1110 parameters_.SetParam(key, Short::Box(value));
1111 return *this;
1112 }
1113
1114 /**
1115 * @description: Sets a parameter value of the short array type.
1116 * @param key Indicates the key matching the parameter.
1117 * @param value Indicates the byte-type value of the parameter.
1118 * @return Returns this Want object containing the parameter value.
1119 */
SetParam(const std::string & key,const std::vector<short> & value)1120 Want &Want::SetParam(const std::string &key, const std::vector<short> &value)
1121 {
1122 std::size_t size = value.size();
1123 sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IShort);
1124 if (ao == nullptr) {
1125 return *this;
1126 }
1127 for (std::size_t i = 0; i < size; i++) {
1128 ao->Set(i, Short::Box(value[i]));
1129 }
1130 parameters_.SetParam(key, ao);
1131 return *this;
1132 }
1133
1134 /**
1135 * @description: Obtains a string value matching the given key.
1136 * @param key Indicates the key of wantParams.
1137 * @return Returns the string value of the parameter matching the given key;
1138 * returns null if the key does not exist.
1139 */
GetStringParam(const std::string & key) const1140 std::string Want::GetStringParam(const std::string &key) const
1141 {
1142 auto value = parameters_.GetParam(key);
1143 IString *ao = IString::Query(value);
1144 if (ao != nullptr) {
1145 return String::Unbox(ao);
1146 }
1147 return std::string();
1148 }
1149
1150 /**
1151 * @description: Obtains a string array matching the given key.
1152 * @param key Indicates the key of wantParams.
1153 * @return Returns the string array of the parameter matching the given key;
1154 * returns null if the key does not exist.
1155 */
GetStringArrayParam(const std::string & key) const1156 std::vector<std::string> Want::GetStringArrayParam(const std::string &key) const
1157 {
1158 std::vector<std::string> array;
1159 auto value = parameters_.GetParam(key);
1160 IArray *ao = IArray::Query(value);
1161 if (ao != nullptr && Array::IsStringArray(ao)) {
1162 auto func = [&](IInterface *object) {
1163 if (object != nullptr) {
1164 IString *value = IString::Query(object);
1165 if (value != nullptr) {
1166 array.push_back(String::Unbox(value));
1167 }
1168 }
1169 };
1170 Array::ForEach(ao, func);
1171 }
1172 return array;
1173 }
1174
1175 /**
1176 * @description: Sets a parameter value of the string type.
1177 * @param key Indicates the key matching the parameter.
1178 * @param value Indicates the byte-type value of the parameter.
1179 * @return Returns this Want object containing the parameter value.
1180 */
SetParam(const std::string & key,const std::string & value)1181 Want &Want::SetParam(const std::string &key, const std::string &value)
1182 {
1183 parameters_.SetParam(key, String::Box(value));
1184 return *this;
1185 }
1186
1187 /**
1188 * @description: Sets a parameter value of the string array type.
1189 * @param key Indicates the key matching the parameter.
1190 * @param value Indicates the byte-type value of the parameter.
1191 * @return Returns this Want object containing the parameter value.
1192 */
SetParam(const std::string & key,const std::vector<std::string> & value)1193 Want &Want::SetParam(const std::string &key, const std::vector<std::string> &value)
1194 {
1195 std::size_t size = value.size();
1196 sptr<IArray> ao = new (std::nothrow) Array(size, g_IID_IString);
1197 if (ao == nullptr) {
1198 return *this;
1199 }
1200 for (std::size_t i = 0; i < size; i++) {
1201 ao->Set(i, String::Box(value[i]));
1202 }
1203 parameters_.SetParam(key, ao);
1204 return *this;
1205 }
1206
1207 /**
1208 * @description: Gets the description of an operation in a Want.
1209 * @return Returns the operation included in this Want.
1210 */
GetOperation() const1211 Operation Want::GetOperation() const
1212 {
1213 return operation_;
1214 }
1215
1216 /**
1217 * @description: Sets the description of an operation in a Want.
1218 * @param operation Indicates the operation description.
1219 */
SetOperation(const OHOS::AAFwk::Operation & operation)1220 void Want::SetOperation(const OHOS::AAFwk::Operation &operation)
1221 {
1222 operation_ = operation;
1223 }
1224
1225 /**
1226 * @description: Sets the description of an operation in a Want.
1227 * @param want Indicates the Want object to compare.
1228 * @return Returns true if the operation components of the two objects are equal; returns false otherwise.
1229 */
OperationEquals(const Want & want)1230 bool Want::OperationEquals(const Want &want)
1231 {
1232 return (operation_ == want.operation_);
1233 }
1234
1235 /**
1236 * @description: Creates a Want object that contains only the operation component of this Want.
1237 * @return Returns the created Want object.
1238 */
CloneOperation()1239 Want *Want::CloneOperation()
1240 {
1241 Want *want = new (std::nothrow) Want();
1242 if (want == nullptr) {
1243 return nullptr;
1244 }
1245 want->SetOperation(operation_);
1246 return want;
1247 }
1248
1249 /**
1250 * @description: Creates a Want instance by using a given Uniform Resource Identifier (URI).
1251 * This method parses the input URI and saves it in a Want object.
1252 * @param uri Indicates the URI to parse.
1253 * @return Returns a Want object containing the URI.
1254 */
ParseUri(const std::string & uri)1255 Want *Want::ParseUri(const std::string &uri)
1256 {
1257 if (!CheckUri(uri)) {
1258 return nullptr;
1259 }
1260
1261 bool ret = true;
1262 std::string content;
1263 std::size_t pos;
1264 std::size_t begin = WANT_HEADER.length();
1265 ElementName element;
1266 Want *want = new (std::nothrow) Want();
1267 if (want == nullptr) {
1268 return nullptr;
1269 }
1270 bool inPicker = false;
1271 pos = uri.find_first_of(";", begin);
1272 while (pos != std::string::npos) {
1273 content = uri.substr(begin, pos - begin);
1274 if (content.compare("PICK") == 0) {
1275 inPicker = true;
1276 begin = pos + 1;
1277 pos = uri.find(";", begin);
1278 break;
1279 }
1280 ret = ParseUriInternal(content, element, *want);
1281 if (!ret) {
1282 break;
1283 }
1284 begin = pos + 1;
1285 pos = uri.find(";", begin);
1286 }
1287 if (inPicker) {
1288 sptr<Want> pickerWant = new (std::nothrow) Want();
1289 ElementName pickerElement;
1290 while (pos != std::string::npos) {
1291 content = uri.substr(begin, pos - begin);
1292 ret = ParseUriInternal(content, pickerElement, *pickerWant);
1293 if (!ret) {
1294 break;
1295 }
1296 begin = pos + 1;
1297 pos = uri.find(";", begin);
1298 }
1299 pickerWant->SetElement(pickerElement);
1300 if (want->GetBundle().empty()) {
1301 }
1302 }
1303 if (ret) {
1304 want->SetElement(element);
1305 } else {
1306 delete want;
1307 want = nullptr;
1308 }
1309 return want;
1310 }
1311
1312 /**
1313 * @description: Creates a Want instance by using a given Uniform Resource Identifier (URI).
1314 * This method parses the input URI and saves it in a Want object.
1315 * @param uri Indicates the URI to parse.
1316 * @return Returns a Want object containing the URI.
1317 */
WantParseUri(const char * uri)1318 Want *Want::WantParseUri(const char *uri)
1319 {
1320 if (uri == nullptr) {
1321 return nullptr;
1322 }
1323 std::string strUri(uri);
1324
1325 return ParseUri(strUri);
1326 }
1327
1328 /**
1329 * @description: Obtains the string representation of the URI in this Want.
1330 * @return Returns the string of the URI.
1331 */
GetUriString() const1332 std::string Want::GetUriString() const
1333 {
1334 return operation_.GetUri().ToString();
1335 }
1336
1337 /**
1338 * @description: Obtains the description of a URI in a Want.
1339 * @return Returns the URI description in the Want.
1340 */
GetUri() const1341 Uri Want::GetUri() const
1342 {
1343 return operation_.GetUri();
1344 }
1345
1346 /**
1347 * @description: Sets the description of a URI in a Want.
1348 * @param uri Indicates the string of URI description.
1349 * @return Returns this Want object containing the URI.
1350 */
SetUri(const std::string & uri)1351 Want &Want::SetUri(const std::string &uri)
1352 {
1353 operation_.SetUri(Uri(uri));
1354 return *this;
1355 }
1356
1357 /**
1358 * @description: Sets the description of a URI in a Want.
1359 * @param uri Indicates the URI description.
1360 * @return Returns this Want object containing the URI.
1361 */
SetUri(const Uri & uri)1362 Want &Want::SetUri(const Uri &uri)
1363 {
1364 operation_.SetUri(uri);
1365 return *this;
1366 }
1367
1368 /**
1369 * @description: Sets the description of a URI and a type in this Want.
1370 * @param uri Indicates the URI description.
1371 * @param type Indicates the type description.
1372 * @return Returns this Want object containing the URI and the type.
1373 */
SetUriAndType(const Uri & uri,const std::string & type)1374 Want &Want::SetUriAndType(const Uri &uri, const std::string &type)
1375 {
1376 operation_.SetUri(uri);
1377 return SetType(type);
1378 }
1379
1380 /**
1381 * @description: Converts a Want into a URI string containing a representation of it.
1382 * @param want Indicates the want description.--Want.
1383 * @return Returns an encoding URI string describing the Want object.
1384 */
WantToUri(Want & want)1385 std::string Want::WantToUri(Want &want)
1386 {
1387 return want.ToUri();
1388 }
1389
1390 /**
1391 * @description: Converts parameter information in a Want into a URI string.
1392 * @return Returns the URI string.
1393 */
ToUri() const1394 std::string Want::ToUri() const
1395 {
1396 std::string uriString = WANT_HEADER;
1397 ToUriStringInner(uriString);
1398
1399 uriString += "end";
1400
1401 return uriString;
1402 }
ToUriStringInner(std::string & uriString) const1403 void Want::ToUriStringInner(std::string &uriString) const
1404 {
1405 if (operation_.GetAction().length() > 0) {
1406 uriString += "action=" + Encode(operation_.GetAction()) + ";";
1407 }
1408 if (GetUriString().length() > 0) {
1409 uriString += "uri=" + Encode(GetUriString()) + ";";
1410 }
1411 for (auto entity : operation_.GetEntities()) {
1412 if (entity.length() > 0) {
1413 uriString += "entity=" + Encode(entity) + ";";
1414 }
1415 }
1416 if (operation_.GetDeviceId().length() > 0) {
1417 uriString += "device=" + Encode(operation_.GetDeviceId()) + ";";
1418 }
1419 if (operation_.GetBundleName().length() > 0) {
1420 uriString += "bundle=" + Encode(operation_.GetBundleName()) + ";";
1421 }
1422 if (operation_.GetAbilityName().length() > 0) {
1423 uriString += "ability=" + Encode(operation_.GetAbilityName()) + ";";
1424 }
1425 if (operation_.GetFlags() != 0) {
1426 uriString += "flag=";
1427 char buf[HEX_STRING_BUF_LEN] {0};
1428 int len = snprintf_s(buf, HEX_STRING_BUF_LEN, HEX_STRING_BUF_LEN - 1, "0x%08x", operation_.GetFlags());
1429 if (len == HEX_STRING_LEN) {
1430 std::string flag = buf;
1431 uriString += Encode(flag);
1432 uriString += ";";
1433 }
1434 }
1435 if (!operation_.GetBundleName().empty()) {
1436 uriString.append("package=");
1437 uriString.append(Encode(operation_.GetBundleName()));
1438 uriString.append(";");
1439 }
1440
1441 UriStringAppendParam(uriString);
1442 }
1443 /**
1444 * @description: Formats a specified URI.
1445 * This method uses the Uri.getLowerCaseScheme() method to format a URI and then saves
1446 * the formatted URI to this Want object.
1447 * @param uri Indicates the URI to format.
1448 * @return Returns this Want object that contains the formatted uri attribute.
1449 */
FormatUri(const std::string & uri)1450 Want &Want::FormatUri(const std::string &uri)
1451 {
1452 return FormatUri(Uri(uri));
1453 }
1454
1455 /**
1456 * @description: Formats a specified URI.
1457 * This method uses the GetLowerCaseScheme() method to format a URI and then saves
1458 * the formatted URI to this Want object.
1459 * @param uri Indicates the URI to format.
1460 * @return Returns this Want object that contains the formatted uri attribute.
1461 */
FormatUri(const Uri & uri)1462 Want &Want::FormatUri(const Uri &uri)
1463 {
1464 operation_.SetUri(GetLowerCaseScheme(uri));
1465 return *this;
1466 }
1467
1468 /**
1469 * @description: Checks whether a Want contains the parameter matching a given key.
1470 * @param key Indicates the key.
1471 * @return Returns true if the Want contains the parameter; returns false otherwise.
1472 */
HasParameter(const std::string & key) const1473 bool Want::HasParameter(const std::string &key) const
1474 {
1475 return parameters_.HasParam(key);
1476 }
1477
1478 /**
1479 * @description: Replaces parameters in this Want object with those in the given WantParams object.
1480 * @param wantParams Indicates the WantParams object containing the new parameters.
1481 * @return Returns this Want object containing the new parameters.
1482 */
ReplaceParams(WantParams & wantParams)1483 Want *Want::ReplaceParams(WantParams &wantParams)
1484 {
1485 parameters_ = wantParams;
1486 return this;
1487 }
1488
1489 /**
1490 * @description: Replaces parameters in this Want object with those in the given Want object.
1491 * @param want Indicates the Want object containing the new parameters.
1492 * @return Returns this Want object containing the new parameters.
1493 */
ReplaceParams(Want & want)1494 Want *Want::ReplaceParams(Want &want)
1495 {
1496 parameters_ = want.parameters_;
1497 return this;
1498 }
1499
1500 /**
1501 * @description: Removes the parameter matching the given key.
1502 * @param key Indicates the key matching the parameter to be removed.
1503 */
RemoveParam(const std::string & key)1504 void Want::RemoveParam(const std::string &key)
1505 {
1506 parameters_.Remove(key);
1507 }
1508
1509 /**
1510 * @description: clear the specific want object.
1511 * @param want Indicates the want to clear
1512 */
ClearWant(Want * want)1513 void Want::ClearWant(Want *want)
1514 {
1515 want->SetType("");
1516 want->SetAction("");
1517 want->SetFlags(0);
1518 OHOS::AppExecFwk::ElementName elementName;
1519 want->SetElement(elementName);
1520 OHOS::AAFwk::Operation operation;
1521 want->SetOperation(operation);
1522 WantParams parameters;
1523 want->SetParams(parameters);
1524 }
1525
1526 /**
1527 * @description: Marshals a Want into a Parcel.
1528 * Fields in the Want are marshalled separately. If any field fails to be marshalled, false is returned.
1529 * @param parcel Indicates the Parcel object for marshalling.
1530 * @return Returns true if the marshalling is successful; returns false otherwise.
1531 */
Marshalling(Parcel & parcel) const1532 bool Want::Marshalling(Parcel &parcel) const
1533 {
1534 // write action
1535 if (!parcel.WriteString16(Str8ToStr16(GetAction()))) {
1536 return false;
1537 }
1538
1539 // write uri
1540 if (!WriteUri(parcel)) {
1541 return false;
1542 }
1543
1544 // write entities
1545 if (!WriteEntities(parcel)) {
1546 return false;
1547 }
1548
1549 // write flags
1550 if (!parcel.WriteUint32(GetFlags())) {
1551 return false;
1552 }
1553
1554 // write element
1555 if (!WriteElement(parcel)) {
1556 return false;
1557 }
1558
1559 // write parameters
1560 if (!WriteParameters(parcel)) {
1561 return false;
1562 }
1563
1564 // write package
1565 if (!parcel.WriteString16(Str8ToStr16(GetBundle()))) {
1566 return false;
1567 }
1568
1569 return true;
1570 }
1571
1572 /**
1573 * @description: Unmarshals a Want from a Parcel.
1574 * Fields in the Want are unmarshalled separately. If any field fails to be unmarshalled, false is returned.
1575 * @param parcel Indicates the Parcel object for unmarshalling.
1576 * @return Returns true if the unmarshalling is successful; returns false otherwise.
1577 */
Unmarshalling(Parcel & parcel)1578 Want *Want::Unmarshalling(Parcel &parcel)
1579 {
1580 Want *want = new (std::nothrow) Want();
1581 if (want != nullptr && !want->ReadFromParcel(parcel)) {
1582 delete want;
1583 want = nullptr;
1584 }
1585 return want;
1586 }
1587
ReadFromParcel(Parcel & parcel)1588 bool Want::ReadFromParcel(Parcel &parcel)
1589 {
1590 // read action
1591 operation_.SetAction(Str16ToStr8(parcel.ReadString16()));
1592
1593 // read uri
1594 if (!ReadUri(parcel)) {
1595 return false;
1596 }
1597
1598 // read entities
1599 if (!ReadEntities(parcel)) {
1600 return false;
1601 }
1602
1603 // read flags
1604 unsigned int flags;
1605 if (!parcel.ReadUint32(flags)) {
1606 return false;
1607 }
1608 operation_.SetFlags(flags);
1609
1610 // read element
1611 if (!ReadElement(parcel)) {
1612 return false;
1613 }
1614
1615 // read parameters
1616 if (!ReadParameters(parcel)) {
1617 return false;
1618 }
1619
1620 // read package
1621 operation_.SetBundleName(Str16ToStr8(parcel.ReadString16()));
1622
1623 return true;
1624 }
1625
ParseUriInternal(const std::string & content,ElementName & element,Want & want)1626 bool Want::ParseUriInternal(const std::string &content, ElementName &element, Want &want)
1627 {
1628 static constexpr int TYPE_TAG_SIZE = 2;
1629
1630 std::string prop;
1631 std::string value;
1632
1633 if (content.empty() || content[0] == '=') {
1634 return true;
1635 }
1636
1637 if (!ParseContent(content, prop, value)) {
1638 return false;
1639 }
1640
1641 if (value.empty()) {
1642 return true;
1643 }
1644
1645 if (prop == "action") {
1646 want.SetAction(value);
1647 } else if (prop == "entity") {
1648 want.AddEntity(value);
1649 } else if (prop == "flag") {
1650 if (!ParseFlag(value, want)) {
1651 return false;
1652 }
1653 } else if (prop == "device") {
1654 element.SetDeviceID(value);
1655 } else if (prop == "bundle") {
1656 element.SetBundleName(value);
1657 } else if (prop == "ability") {
1658 element.SetAbilityName(value);
1659 } else if (prop == "package") {
1660 want.SetBundle(Decode(value));
1661 } else if (prop.length() > TYPE_TAG_SIZE) {
1662 std::string key = prop.substr(TYPE_TAG_SIZE);
1663 if (!Want::CheckAndSetParameters(want, key, prop, value)) {
1664 return false;
1665 }
1666 std::string moduleName = want.GetStringParam(PARAM_MODULE_NAME);
1667 want.SetModuleName(moduleName);
1668 element.SetModuleName(moduleName);
1669 }
1670
1671 return true;
1672 }
1673
ParseContent(const std::string & content,std::string & prop,std::string & value)1674 bool Want::ParseContent(const std::string &content, std::string &prop, std::string &value)
1675 {
1676 std::size_t pos = content.find("=");
1677 if (pos != std::string::npos) {
1678 std::string subString = content.substr(0, pos);
1679 prop = Decode(subString);
1680 subString = content.substr(pos + 1, content.length() - pos - 1);
1681 value = Decode(subString);
1682 return true;
1683 }
1684 return false;
1685 }
1686
ParseFlag(const std::string & content,Want & want)1687 bool Want::ParseFlag(const std::string &content, Want &want)
1688 {
1689 std::string contentLower = LowerStr(content);
1690 std::string prefix = "0x";
1691 if (!contentLower.empty()) {
1692 if (contentLower.find(prefix) != 0) {
1693 return false;
1694 }
1695
1696 for (std::size_t i = prefix.length(); i < contentLower.length(); i++) {
1697 if (!isxdigit(contentLower[i])) {
1698 return false;
1699 }
1700 }
1701 int base = 16; // hex string
1702 unsigned int flag = std::stoul(contentLower, nullptr, base);
1703 want.SetFlags(flag);
1704 }
1705 return true;
1706 }
1707
Decode(const std::string & str)1708 std::string Want::Decode(const std::string &str)
1709 {
1710 std::string decode;
1711
1712 for (std::size_t i = 0; i < str.length();) {
1713 if (str[i] != '\\') {
1714 decode += str[i];
1715 i++;
1716 continue;
1717 }
1718 if (++i >= str.length()) {
1719 decode += "\\";
1720 break;
1721 }
1722 if (str[i] == '\\') {
1723 decode += "\\";
1724 i++;
1725 } else if (str[i] == '0') {
1726 if (str.compare(i, OCT_EQUALSTO.length(), OCT_EQUALSTO) == 0) {
1727 decode += "=";
1728 i += OCT_EQUALSTO.length();
1729 } else if (str.compare(i, OCT_SEMICOLON.length(), OCT_SEMICOLON) == 0) {
1730 decode += ";";
1731 i += OCT_SEMICOLON.length();
1732 } else {
1733 decode += "\\" + str.substr(i, 1);
1734 i++;
1735 }
1736 } else {
1737 decode += "\\" + str.substr(i, 1);
1738 i++;
1739 }
1740 }
1741
1742 return decode;
1743 }
1744
Encode(const std::string & str)1745 std::string Want::Encode(const std::string &str)
1746 {
1747 std::string encode;
1748
1749 for (std::size_t i = 0; i < str.length(); i++) {
1750 if (str[i] == '\\') {
1751 encode += "\\\\";
1752 } else if (str[i] == '=') {
1753 encode += "\\" + OCT_EQUALSTO;
1754 } else if (str[i] == ';') {
1755 encode += "\\" + OCT_SEMICOLON;
1756 } else {
1757 encode += str[i];
1758 }
1759 }
1760
1761 return encode;
1762 }
1763
CheckAndSetParameters(Want & want,const std::string & key,std::string & prop,const std::string & value)1764 bool Want::CheckAndSetParameters(Want &want, const std::string &key, std::string &prop, const std::string &value)
1765 {
1766 sptr<IInterface> valueObj;
1767 if (prop[0] == String::SIGNATURE && prop[1] == '.') {
1768 valueObj = String::Parse(value);
1769 } else if (prop[0] == Boolean::SIGNATURE && prop[1] == '.') {
1770 valueObj = Boolean::Parse(value);
1771 } else if (prop[0] == Char::SIGNATURE && prop[1] == '.') {
1772 valueObj = Char::Parse(value);
1773 } else if (prop[0] == Byte::SIGNATURE && prop[1] == '.') {
1774 valueObj = Byte::Parse(value);
1775 } else if (prop[0] == Short::SIGNATURE && prop[1] == '.') {
1776 valueObj = Short::Parse(value);
1777 } else if (prop[0] == Integer::SIGNATURE && prop[1] == '.') {
1778 valueObj = Integer::Parse(value);
1779 } else if (prop[0] == Long::SIGNATURE && prop[1] == '.') {
1780 valueObj = Long::Parse(value);
1781 } else if (prop[0] == Float::SIGNATURE && prop[1] == '.') {
1782 valueObj = Float::Parse(value);
1783 } else if (prop[0] == Double::SIGNATURE && prop[1] == '.') {
1784 valueObj = Double::Parse(value);
1785 } else if (prop[0] == Array::SIGNATURE && prop[1] == '.') {
1786 valueObj = Array::Parse(value);
1787 } else {
1788 return true;
1789 }
1790
1791 if (valueObj == nullptr) {
1792 return false;
1793 }
1794 want.parameters_.SetParam(key, valueObj);
1795 return true;
1796 }
1797
DumpInfo(int level) const1798 void Want::DumpInfo(int level) const
1799 {
1800 operation_.DumpInfo(level);
1801 parameters_.DumpInfo(level);
1802 }
1803
ToJson() const1804 nlohmann::json Want::ToJson() const
1805 {
1806 WantParamWrapper wrapper(parameters_);
1807 std::string parametersString = wrapper.ToString();
1808
1809 nlohmann::json entitiesJson;
1810 std::vector<std::string> entities = GetEntities();
1811 for (auto entity : entities) {
1812 entitiesJson.emplace_back(entity);
1813 }
1814
1815 nlohmann::json wantJson = nlohmann::json {
1816 {"deviceId", operation_.GetDeviceId()},
1817 {"bundleName", operation_.GetBundleName()},
1818 {"abilityName", operation_.GetAbilityName()},
1819 {"uri", GetUriString()},
1820 {"type", GetType()},
1821 {"flags", GetFlags()},
1822 {"action", GetAction()},
1823 {"parameters", parametersString},
1824 {"entities", entitiesJson},
1825 };
1826
1827 return wantJson;
1828 }
1829
ReadFromJson(nlohmann::json & wantJson)1830 bool Want::ReadFromJson(nlohmann::json &wantJson)
1831 {
1832 const auto &jsonObjectEnd = wantJson.end();
1833 if ((wantJson.find("deviceId") == jsonObjectEnd)
1834 || (wantJson.find("bundleName") == jsonObjectEnd)
1835 || (wantJson.find("abilityName") == jsonObjectEnd)
1836 || (wantJson.find("uri") == jsonObjectEnd)
1837 || (wantJson.find("type") == jsonObjectEnd)
1838 || (wantJson.find("flags") == jsonObjectEnd)
1839 || (wantJson.find("action") == jsonObjectEnd)
1840 || (wantJson.find("parameters") == jsonObjectEnd)
1841 || (wantJson.find("entities") == jsonObjectEnd)) {
1842 ABILITYBASE_LOGE("Incomplete wantJson");
1843 return false;
1844 }
1845
1846 std::string deviceId = wantJson.at("deviceId").get<std::string>();
1847 std::string bundleName = wantJson.at("bundleName").get<std::string>();
1848 std::string abilityName = wantJson.at("abilityName").get<std::string>();
1849 SetElementName(deviceId, bundleName, abilityName);
1850
1851 std::string uri = wantJson.at("uri").get<std::string>();
1852 SetUri(uri);
1853
1854 std::string type = wantJson.at("type").get<std::string>();
1855 SetType(type);
1856
1857 unsigned int flags = wantJson.at("flags").get<unsigned int>();
1858 SetFlags(flags);
1859
1860 std::string action = wantJson.at("action").get<std::string>();
1861 SetAction(action);
1862
1863 std::string parametersString = wantJson.at("parameters").get<std::string>();
1864 WantParams parameters = WantParamWrapper::ParseWantParams(parametersString);
1865 SetParams(parameters);
1866 std::string moduleName = GetStringParam(PARAM_MODULE_NAME);
1867 SetModuleName(moduleName);
1868
1869 if (wantJson.at("entities").is_null()) {
1870 ABILITYBASE_LOGI("entities is null");
1871 } else {
1872 std::vector<std::string> entities;
1873 wantJson.at("entities").get_to<std::vector<std::string>>(entities);
1874 for (size_t i = 0; i < entities.size(); i++) {
1875 AddEntity(entities[i]);
1876 }
1877 }
1878 return true;
1879 }
1880
ToString() const1881 std::string Want::ToString() const
1882 {
1883 return ToJson().dump();
1884 }
1885
FromString(std::string & string)1886 Want *Want::FromString(std::string &string)
1887 {
1888 if (string.empty()) {
1889 ABILITYBASE_LOGE("Invalid string.");
1890 return nullptr;
1891 }
1892
1893 nlohmann::json wantJson = nlohmann::json::parse(string);
1894
1895 Want *want = new (std::nothrow) Want();
1896 if (want != nullptr && !want->ReadFromJson(wantJson)) {
1897 delete want;
1898 want = nullptr;
1899 }
1900 return want;
1901 }
1902
1903 /**
1904 * @description: Sets a device id in a Want.
1905 * @param deviceId Indicates the device id to set.
1906 * @return Returns this Want object containing the flag.
1907 */
SetDeviceId(const std::string & deviceId)1908 Want &Want::SetDeviceId(const std::string &deviceId)
1909 {
1910 operation_.SetDeviceId(deviceId);
1911 return *this;
1912 }
1913
GetDeviceId() const1914 std::string Want::GetDeviceId() const
1915 {
1916 return operation_.GetDeviceId();
1917 }
1918
SetModuleName(const std::string & moduleName)1919 Want& Want::SetModuleName(const std::string &moduleName)
1920 {
1921 operation_.SetModuleName(moduleName);
1922 SetParam(PARAM_MODULE_NAME, moduleName);
1923 return *this;
1924 }
1925
GetModuleName() const1926 std::string Want::GetModuleName() const
1927 {
1928 return operation_.GetModuleName();
1929 }
1930
CheckUri(const std::string & uri)1931 bool Want::CheckUri(const std::string &uri)
1932 {
1933 if (uri.length() <= 0) {
1934 return false;
1935 }
1936
1937 if (uri.find(WANT_HEADER) != 0) {
1938 return false;
1939 }
1940 if (uri.rfind(WANT_END) != (uri.length() - WANT_END.length())) {
1941 return false;
1942 }
1943
1944 return true;
1945 }
1946
UriStringAppendParam(std::string & uriString) const1947 void Want::UriStringAppendParam(std::string &uriString) const
1948 {
1949 auto params = parameters_.GetParams();
1950 auto iter = params.cbegin();
1951 while (iter != params.cend()) {
1952 sptr<IInterface> o = iter->second;
1953 if (IString::Query(o) != nullptr) {
1954 uriString += String::SIGNATURE;
1955 } else if (IBoolean::Query(o) != nullptr) {
1956 uriString += Boolean::SIGNATURE;
1957 } else if (IChar::Query(o) != nullptr) {
1958 uriString += Char::SIGNATURE;
1959 } else if (IByte::Query(o) != nullptr) {
1960 uriString += Byte::SIGNATURE;
1961 } else if (IShort::Query(o) != nullptr) {
1962 uriString += Short::SIGNATURE;
1963 } else if (IInteger::Query(o) != nullptr) {
1964 uriString += Integer::SIGNATURE;
1965 } else if (ILong::Query(o) != nullptr) {
1966 uriString += Long::SIGNATURE;
1967 } else if (IFloat::Query(o) != nullptr) {
1968 uriString += Float::SIGNATURE;
1969 } else if (IDouble::Query(o) != nullptr) {
1970 uriString += Double::SIGNATURE;
1971 } else if (IArray::Query(o) != nullptr) {
1972 uriString += Array::SIGNATURE;
1973 }
1974 uriString += "." + Encode(iter->first) + "=" + Encode(Object::ToString(*(o.GetRefPtr()))) + ";";
1975 iter++;
1976 }
1977 }
1978
WriteUri(Parcel & parcel) const1979 bool Want::WriteUri(Parcel &parcel) const
1980 {
1981 if (GetUriString().empty()) {
1982 if (!parcel.WriteInt32(VALUE_NULL)) {
1983 return false;
1984 }
1985 } else {
1986 if (!parcel.WriteInt32(VALUE_OBJECT)) {
1987 return false;
1988 }
1989 if (!parcel.WriteString16(Str8ToStr16(GetUriString()))) {
1990 return false;
1991 }
1992 }
1993
1994 return true;
1995 }
1996
WriteEntities(Parcel & parcel) const1997 bool Want::WriteEntities(Parcel &parcel) const
1998 {
1999 std::vector<std::string> entities = GetEntities();
2000 if (entities.empty()) {
2001 if (!parcel.WriteInt32(VALUE_NULL)) {
2002 return false;
2003 }
2004 return true;
2005 }
2006
2007 std::vector<std::u16string> entityU16;
2008 for (std::vector<std::string>::size_type i = 0; i < entities.size(); i++) {
2009 entityU16.push_back(Str8ToStr16(entities[i]));
2010 }
2011 if (!parcel.WriteInt32(VALUE_OBJECT)) {
2012 return false;
2013 }
2014 if (!parcel.WriteString16Vector(entityU16)) {
2015 return false;
2016 }
2017 return true;
2018 }
2019
WriteElement(Parcel & parcel) const2020 bool Want::WriteElement(Parcel &parcel) const
2021 {
2022 ElementName emptyElement;
2023 ElementName element = GetElement();
2024 if (element == emptyElement) {
2025 if (!parcel.WriteInt32(VALUE_NULL)) {
2026 return false;
2027 }
2028 } else {
2029 if (!parcel.WriteInt32(VALUE_OBJECT)) {
2030 return false;
2031 }
2032 if (!parcel.WriteParcelable(&element)) {
2033 return false;
2034 }
2035 }
2036
2037 return true;
2038 }
2039
WriteParameters(Parcel & parcel) const2040 bool Want::WriteParameters(Parcel &parcel) const
2041 {
2042 if (parameters_.Size() == 0) {
2043 if (!parcel.WriteInt32(VALUE_NULL)) {
2044 return false;
2045 }
2046 } else {
2047 if (!parcel.WriteInt32(VALUE_OBJECT)) {
2048 return false;
2049 }
2050 if (!parcel.WriteParcelable(¶meters_)) {
2051 return false;
2052 }
2053 }
2054
2055 return true;
2056 }
2057
ReadUri(Parcel & parcel)2058 bool Want::ReadUri(Parcel &parcel)
2059 {
2060 int empty = VALUE_NULL;
2061 if (!parcel.ReadInt32(empty)) {
2062 return false;
2063 }
2064 if (empty == VALUE_OBJECT) {
2065 SetUri(Str16ToStr8(parcel.ReadString16()));
2066 }
2067
2068 return true;
2069 }
2070
ReadEntities(Parcel & parcel)2071 bool Want::ReadEntities(Parcel &parcel)
2072 {
2073 std::vector<std::u16string> entityU16;
2074 std::vector<std::string> entities;
2075 int empty = VALUE_NULL;
2076 if (!parcel.ReadInt32(empty)) {
2077 return false;
2078 }
2079 if (empty == VALUE_OBJECT) {
2080 if (!parcel.ReadString16Vector(&entityU16)) {
2081 return false;
2082 }
2083 }
2084 for (std::vector<std::u16string>::size_type i = 0; i < entityU16.size(); i++) {
2085 entities.push_back(Str16ToStr8(entityU16[i]));
2086 }
2087 operation_.SetEntities(entities);
2088
2089 return true;
2090 }
2091
ReadElement(Parcel & parcel)2092 bool Want::ReadElement(Parcel &parcel)
2093 {
2094 int empty = VALUE_NULL;
2095 if (!parcel.ReadInt32(empty)) {
2096 return false;
2097 }
2098
2099 if (empty == VALUE_OBJECT) {
2100 auto element = parcel.ReadParcelable<ElementName>();
2101 if (element != nullptr) {
2102 SetElement(*element);
2103 delete element;
2104 } else {
2105 return false;
2106 }
2107 }
2108
2109 return true;
2110 }
2111
ReadParameters(Parcel & parcel)2112 bool Want::ReadParameters(Parcel &parcel)
2113 {
2114 int empty = VALUE_NULL;
2115 if (!parcel.ReadInt32(empty)) {
2116 return false;
2117 }
2118
2119 if (empty == VALUE_OBJECT) {
2120 auto params = parcel.ReadParcelable<WantParams>();
2121 if (params != nullptr) {
2122 parameters_ = *params;
2123 delete params;
2124 params = nullptr;
2125 std::string moduleName = GetStringParam(PARAM_MODULE_NAME);
2126 SetModuleName(moduleName);
2127 } else {
2128 return false;
2129 }
2130 }
2131
2132 return true;
2133 }
2134 } // namespace AAFwk
2135 } // namespace OHOS
2136