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