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