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