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