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