• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "distributed_want_v2.h"
17 
18 #include <algorithm>
19 #include <climits>
20 #include <cstdlib>
21 #include <regex>
22 #include <securec.h>
23 
24 #include "array_wrapper.h"
25 #include "base_obj.h"
26 #include "bool_wrapper.h"
27 #include "byte_wrapper.h"
28 #include "double_wrapper.h"
29 #include "float_wrapper.h"
30 #include "int_wrapper.h"
31 #include "long_wrapper.h"
32 #include "parcel_macro_base.h"
33 #include "remote_object_wrapper.h"
34 #include "short_wrapper.h"
35 #include "string_ex.h"
36 #include "string_wrapper.h"
37 #include "zchar_wrapper.h"
38 #include "want_params_wrapper.h"
39 
40 #include "distributed_operation_builder.h"
41 #include "dtbschedmgr_log.h"
42 
43 using namespace OHOS::AppExecFwk;
44 using OHOS::AppExecFwk::ElementName;
45 namespace OHOS {
46 namespace DistributedSchedule {
47 namespace {
48 const std::regex NUMBER_REGEX("^[-+]?([0-9]+)([.]([0-9]+))?$");
49 const std::string TAG = "DistributedWantV2";
50 const char* TYPE_PROPERTY = "type";
51 };  // namespace
52 const std::string DistributedWantV2::ACTION_PLAY("action.system.play");
53 const std::string DistributedWantV2::ACTION_HOME("action.system.home");
54 
55 const std::string DistributedWantV2::ENTITY_HOME("entity.system.home");
56 const std::string DistributedWantV2::ENTITY_VIDEO("entity.system.video");
57 const std::string DistributedWantV2::FLAG_HOME_INTENT_FROM_SYSTEM("flag.home.intent.from.system");
58 const std::string DistributedWantV2::ENTITY_MUSIC("entity.app.music");
59 const std::string DistributedWantV2::ENTITY_EMAIL("entity.app.email");
60 const std::string DistributedWantV2::ENTITY_CONTACTS("entity.app.contacts");
61 const std::string DistributedWantV2::ENTITY_MAPS("entity.app.maps");
62 const std::string DistributedWantV2::ENTITY_BROWSER("entity.app.browser");
63 const std::string DistributedWantV2::ENTITY_CALENDAR("entity.app.calendar");
64 const std::string DistributedWantV2::ENTITY_MESSAGING("entity.app.messaging");
65 const std::string DistributedWantV2::ENTITY_FILES("entity.app.files");
66 const std::string DistributedWantV2::ENTITY_GALLERY("entity.app.gallery");
67 
68 const std::string DistributedWantV2::OCT_EQUALSTO("075");   // '='
69 const std::string DistributedWantV2::OCT_SEMICOLON("073");  // ';'
70 const std::string DistributedWantV2::MIME_TYPE("mime-type");
71 const std::string DistributedWantV2::WANT_HEADER("#Intent;");
72 
73 const std::string DistributedWantV2::PARAM_RESV_WINDOW_MODE("ohos.aafwk.param.windowMode");
74 const std::string DistributedWantV2::PARAM_RESV_DISPLAY_ID("ohos.aafwk.param.displayId");
75 const std::string DistributedWantV2::PARAM_RESV_CALLER_TOKEN("ohos.aafwk.param.callerToken");
76 const std::string DistributedWantV2::PARAM_RESV_CALLER_UID("ohos.aafwk.param.callerUid");
77 const std::string DistributedWantV2::PARAM_RESV_CALLER_PID("ohos.aafwk.param.callerPid");
78 
DistributedWantV2()79 DistributedWantV2::DistributedWantV2()
80 {}
81 
~DistributedWantV2()82 DistributedWantV2::~DistributedWantV2()
83 {}
84 
DistributedWantV2(const DistributedWantV2 & other)85 DistributedWantV2::DistributedWantV2(const DistributedWantV2& other)
86 {
87     operation_ = other.operation_;
88     parameters_ = other.parameters_;
89 }
90 
operator =(const DistributedWantV2 & other)91 DistributedWantV2& DistributedWantV2::operator=(const DistributedWantV2& other)
92 {
93     operation_ = other.operation_;
94     parameters_ = other.parameters_;
95     return *this;
96 }
97 
DistributedWantV2(const AAFwk::Want & want)98 DistributedWantV2::DistributedWantV2(const AAFwk::Want& want)
99 {
100     DistributedOperationBuilder builder;
101     builder.WithAbilityName(want.GetElement().GetAbilityName());
102     builder.WithBundleName(want.GetElement().GetBundleName());
103     builder.WithDeviceId(want.GetElement().GetDeviceID());
104     builder.WithAction(want.GetAction());
105     builder.WithEntities(want.GetEntities());
106     builder.WithFlags(want.GetFlags());
107     builder.WithUri(want.GetUri());
108     std::shared_ptr<DistributedOperation> op = builder.build();
109     operation_ = *op;
110     parameters_ = want.GetParams();
111 }
112 
ToWant()113 std::shared_ptr<AAFwk::Want> DistributedWantV2::ToWant()
114 {
115     auto want = std::make_shared<AAFwk::Want>();
116     want->SetFlags(GetFlags());
117     want->SetElement(GetElement());
118     want->SetUri(GetUri());
119     want->SetAction(GetAction());
120     want->SetBundle(GetBundle());
121     want->SetType(GetType());
122     std::vector<std::string> ents = GetEntities();
123     for (auto it = ents.begin(); it != ents.end(); it++) {
124         want->AddEntity(*it);
125     }
126     want->SetParams(parameters_);
127     return want;
128 }
129 
GetFlags() const130 unsigned int DistributedWantV2::GetFlags() const
131 {
132     return operation_.GetFlags();
133 }
134 
SetFlags(unsigned int flags)135 DistributedWantV2& DistributedWantV2::SetFlags(unsigned int flags)
136 {
137     operation_.SetFlags(flags);
138     return *this;
139 }
140 
AddFlags(unsigned int flags)141 DistributedWantV2& DistributedWantV2::AddFlags(unsigned int flags)
142 {
143     operation_.AddFlags(flags);
144     return *this;
145 }
146 
RemoveFlags(unsigned int flags)147 void DistributedWantV2::RemoveFlags(unsigned int flags)
148 {
149     operation_.RemoveFlags(flags);
150 }
151 
GetElement() const152 OHOS::AppExecFwk::ElementName DistributedWantV2::GetElement() const
153 {
154     return ElementName(operation_.GetDeviceId(), operation_.GetBundleName(), operation_.GetAbilityName());
155 }
156 
SetElementName(const std::string & bundleName,const std::string & abilityName)157 DistributedWantV2& DistributedWantV2::SetElementName(const std::string& bundleName, const std::string& abilityName)
158 {
159     operation_.SetBundleName(bundleName);
160     operation_.SetAbilityName(abilityName);
161     return *this;
162 }
163 
SetElementName(const std::string & deviceId,const std::string & bundleName,const std::string & abilityName)164 DistributedWantV2& DistributedWantV2::SetElementName(const std::string& deviceId, const std::string& bundleName,
165     const std::string& abilityName)
166 {
167     operation_.SetDeviceId(deviceId);
168     operation_.SetBundleName(bundleName);
169     operation_.SetAbilityName(abilityName);
170     return *this;
171 }
172 
SetElement(const OHOS::AppExecFwk::ElementName & element)173 DistributedWantV2& DistributedWantV2::SetElement(const OHOS::AppExecFwk::ElementName& element)
174 {
175     operation_.SetDeviceId(element.GetDeviceID());
176     operation_.SetBundleName(element.GetBundleName());
177     operation_.SetAbilityName(element.GetAbilityName());
178     return *this;
179 }
180 
GetEntities() const181 const std::vector<std::string>& DistributedWantV2::GetEntities() const
182 {
183     return operation_.GetEntities();
184 }
185 
AddEntity(const std::string & entity)186 DistributedWantV2& DistributedWantV2::AddEntity(const std::string& entity)
187 {
188     operation_.AddEntity(entity);
189     return *this;
190 }
191 
RemoveEntity(const std::string & entity)192 void DistributedWantV2::RemoveEntity(const std::string& entity)
193 {
194     operation_.RemoveEntity(entity);
195 }
196 
HasEntity(const std::string & entity) const197 bool DistributedWantV2::HasEntity(const std::string& entity) const
198 {
199     return operation_.HasEntity(entity);
200 }
201 
CountEntities()202 int DistributedWantV2::CountEntities()
203 {
204     return operation_.CountEntities();
205 }
206 
GetBundle() const207 std::string DistributedWantV2::GetBundle() const
208 {
209     return operation_.GetBundleName();
210 }
211 
SetBundle(const std::string & bundleName)212 DistributedWantV2& DistributedWantV2::SetBundle(const std::string& bundleName)
213 {
214     operation_.SetBundleName(bundleName);
215     return *this;
216 }
217 
GetType() const218 std::string DistributedWantV2::GetType() const
219 {
220     auto value = parameters_.GetParam(MIME_TYPE);
221     AAFwk::IString* ao = AAFwk::IString::Query(value);
222     if (ao != nullptr) {
223         return AAFwk::String::Unbox(ao);
224     }
225     return std::string();
226 }
227 
SetType(const std::string & type)228 DistributedWantV2& DistributedWantV2::SetType(const std::string& type)
229 {
230     sptr<AAFwk::IString> valueObj = AAFwk::String::Parse(type);
231     parameters_.SetParam(MIME_TYPE, valueObj);
232     return *this;
233 }
234 
GetLowerCaseScheme(const Uri & uri)235 Uri DistributedWantV2::GetLowerCaseScheme(const Uri& uri)
236 {
237     std::string dStrUri = const_cast<Uri&>(uri).ToString();
238     std::string schemeStr = const_cast<Uri&>(uri).GetScheme();
239     if (dStrUri.empty() || schemeStr.empty()) {
240         return uri;
241     }
242 
243     std::string lowSchemeStr = schemeStr;
244     std::transform(lowSchemeStr.begin(), lowSchemeStr.end(), lowSchemeStr.begin(), [](unsigned char c) {
245         return std::tolower(c);
246     });
247 
248     if (schemeStr == lowSchemeStr) {
249         return uri;
250     }
251 
252     std::size_t pos = dStrUri.find_first_of(schemeStr, 0);
253     if (pos != std::string::npos) {
254         dStrUri.replace(pos, schemeStr.length(), lowSchemeStr);
255     }
256 
257     return Uri(dStrUri);
258 }
259 
GetAction() const260 std::string DistributedWantV2::GetAction() const
261 {
262     return operation_.GetAction();
263 }
264 
SetAction(const std::string & action)265 DistributedWantV2& DistributedWantV2::SetAction(const std::string& action)
266 {
267     operation_.SetAction(action);
268     return *this;
269 }
270 
GetScheme() const271 const std::string DistributedWantV2::GetScheme() const
272 {
273     return operation_.GetUri().GetScheme();
274 }
275 
GetParams() const276 const AAFwk::WantParams& DistributedWantV2::GetParams() const
277 {
278     return parameters_;
279 }
280 
SetParams(const AAFwk::WantParams & wantParams)281 DistributedWantV2& DistributedWantV2::SetParams(const AAFwk::WantParams& wantParams)
282 {
283     parameters_ = wantParams;
284     return *this;
285 }
286 
GetBoolParam(const std::string & key,bool defaultValue) const287 bool DistributedWantV2::GetBoolParam(const std::string& key, bool defaultValue) const
288 {
289     auto value = parameters_.GetParam(key);
290     AAFwk::IBoolean* bo = AAFwk::IBoolean::Query(value);
291     if (bo != nullptr) {
292         return AAFwk::Boolean::Unbox(bo);
293     }
294     return defaultValue;
295 }
296 
GetBoolArrayParam(const std::string & key) const297 std::vector<bool> DistributedWantV2::GetBoolArrayParam(const std::string& key) const
298 {
299     std::vector<bool> array;
300     auto value = parameters_.GetParam(key);
301     AAFwk::IArray* ao = AAFwk::IArray::Query(value);
302     if (ao != nullptr && AAFwk::Array::IsBooleanArray(ao)) {
303         auto func = [&](AAFwk::IInterface* object) {
304             if (object == nullptr) {
305                 return;
306             }
307             AAFwk::IBoolean* value = AAFwk::IBoolean::Query(object);
308             if (value != nullptr) {
309                 array.push_back(AAFwk::Boolean::Unbox(value));
310             }
311         };
312         AAFwk::Array::ForEach(ao, func);
313     }
314     return array;
315 }
316 
SetParam(const std::string & key,const sptr<IRemoteObject> & remoteObject)317 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const sptr<IRemoteObject>& remoteObject)
318 {
319     AAFwk::WantParams wp;
320     wp.SetParam(TYPE_PROPERTY, AAFwk::String::Box(AAFwk::REMOTE_OBJECT));
321     wp.SetParam(AAFwk::VALUE_PROPERTY, AAFwk::RemoteObjectWrap::Box(remoteObject));
322     parameters_.SetParam(key, AAFwk::WantParamWrapper::Box(wp));
323     return *this;
324 }
325 
SetParam(const std::string & key,bool value)326 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, bool value)
327 {
328     parameters_.SetParam(key, AAFwk::Boolean::Box(value));
329     return *this;
330 }
331 
SetParam(const std::string & key,const std::vector<bool> & value)332 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::vector<bool>& value)
333 {
334     std::size_t size = value.size();
335     sptr<AAFwk::IArray> ao(new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IBoolean));
336     if (ao != nullptr) {
337         for (std::size_t i = 0; i < size; i++) {
338             ao->Set(i, AAFwk::Boolean::Box(value[i]));
339         }
340         parameters_.SetParam(key, ao);
341     }
342     return *this;
343 }
344 
GetByteParam(const std::string & key,const AAFwk::byte defaultValue) const345 AAFwk::byte DistributedWantV2::GetByteParam(const std::string& key, const AAFwk::byte defaultValue) const
346 {
347     auto value = parameters_.GetParam(key);
348     AAFwk::IByte* bo = AAFwk::IByte::Query(value);
349     if (bo != nullptr) {
350         return AAFwk::Byte::Unbox(bo);
351     }
352     return defaultValue;
353 }
354 
GetByteArrayParam(const std::string & key) const355 std::vector<AAFwk::byte> DistributedWantV2::GetByteArrayParam(const std::string& key) const
356 {
357     std::vector<AAFwk::byte> array;
358     auto value = parameters_.GetParam(key);
359     AAFwk::IArray* ao = AAFwk::IArray::Query(value);
360     if (ao != nullptr && AAFwk::Array::IsByteArray(ao)) {
361         auto func = [&](AAFwk::IInterface* object) {
362             if (object == nullptr) {
363                 return;
364             }
365             AAFwk::IByte* value = AAFwk::IByte::Query(object);
366             if (value != nullptr) {
367                 array.push_back(AAFwk::Byte::Unbox(value));
368             }
369         };
370         AAFwk::Array::ForEach(ao, func);
371     }
372     return array;
373 }
374 
SetParam(const std::string & key,AAFwk::byte value)375 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, AAFwk::byte value)
376 {
377     parameters_.SetParam(key, AAFwk::Byte::Box(value));
378     return *this;
379 }
380 
SetParam(const std::string & key,const std::vector<AAFwk::byte> & value)381 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::vector<AAFwk::byte>& value)
382 {
383     std::size_t size = value.size();
384     sptr<AAFwk::IArray> ao(new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IByte));
385     if (ao == nullptr) {
386         return *this;
387     }
388     for (std::size_t i = 0; i < size; i++) {
389         ao->Set(i, AAFwk::Byte::Box(value[i]));
390     }
391     parameters_.SetParam(key, ao);
392     return *this;
393 }
394 
GetCharParam(const std::string & key,AAFwk::zchar defaultValue) const395 AAFwk::zchar DistributedWantV2::GetCharParam(const std::string& key, AAFwk::zchar defaultValue) const
396 {
397     auto value = parameters_.GetParam(key);
398     AAFwk::IChar* ao = AAFwk::IChar::Query(value);
399     if (ao != nullptr) {
400         return AAFwk::Char::Unbox(ao);
401     }
402     return defaultValue;
403 }
404 
GetCharArrayParam(const std::string & key) const405 std::vector<AAFwk::zchar> DistributedWantV2::GetCharArrayParam(const std::string& key) const
406 {
407     std::vector<AAFwk::zchar> array;
408     auto value = parameters_.GetParam(key);
409     AAFwk::IArray* ao = AAFwk::IArray::Query(value);
410     if (ao != nullptr && AAFwk::Array::IsCharArray(ao)) {
411         auto func = [&](AAFwk::IInterface* object) {
412             if (object == nullptr) {
413                 return;
414             }
415             AAFwk::IChar* value = AAFwk::IChar::Query(object);
416             if (value != nullptr) {
417                 array.push_back(AAFwk::Char::Unbox(value));
418             }
419         };
420         AAFwk::Array::ForEach(ao, func);
421     }
422     return array;
423 }
424 
SetParam(const std::string & key,AAFwk::zchar value)425 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, AAFwk::zchar value)
426 {
427     parameters_.SetParam(key, AAFwk::Char::Box(value));
428     return *this;
429 }
430 
SetParam(const std::string & key,const std::vector<AAFwk::zchar> & value)431 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::vector<AAFwk::zchar>& value)
432 {
433     std::size_t size = value.size();
434     sptr<AAFwk::IArray> ao(new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IChar));
435     if (ao == nullptr) {
436         return *this;
437     }
438     for (std::size_t i = 0; i < size; i++) {
439         ao->Set(i, AAFwk::Char::Box(value[i]));
440     }
441     parameters_.SetParam(key, ao);
442     return *this;
443 }
444 
GetIntParam(const std::string & key,const int defaultValue) const445 int DistributedWantV2::GetIntParam(const std::string& key, const int defaultValue) const
446 {
447     auto value = parameters_.GetParam(key);
448     AAFwk::IInteger* ao = AAFwk::IInteger::Query(value);
449     if (ao != nullptr) {
450         return AAFwk::Integer::Unbox(ao);
451     }
452     return defaultValue;
453 }
454 
GetIntArrayParam(const std::string & key) const455 std::vector<int> DistributedWantV2::GetIntArrayParam(const std::string& key) const
456 {
457     std::vector<int> array;
458     auto value = parameters_.GetParam(key);
459     AAFwk::IArray* ao = AAFwk::IArray::Query(value);
460     if (ao != nullptr && AAFwk::Array::IsIntegerArray(ao)) {
461         auto func = [&](AAFwk::IInterface* object) {
462             if (object == nullptr) {
463                 return;
464             }
465             AAFwk::IInteger* value = AAFwk::IInteger::Query(object);
466             if (value != nullptr) {
467                 array.push_back(AAFwk::Integer::Unbox(value));
468             }
469         };
470         AAFwk::Array::ForEach(ao, func);
471     }
472     return array;
473 }
474 
SetParam(const std::string & key,int value)475 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, int value)
476 {
477     parameters_.SetParam(key, AAFwk::Integer::Box(value));
478     return *this;
479 }
480 
SetParam(const std::string & key,const std::vector<int> & value)481 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::vector<int>& value)
482 {
483     std::size_t size = value.size();
484     sptr<AAFwk::IArray> ao(new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IInteger));
485     if (ao == nullptr) {
486         return *this;
487     }
488     for (std::size_t i = 0; i < size; i++) {
489         ao->Set(i, AAFwk::Integer::Box(value[i]));
490     }
491     parameters_.SetParam(key, ao);
492     return *this;
493 }
494 
GetDoubleParam(const std::string & key,double defaultValue) const495 double DistributedWantV2::GetDoubleParam(const std::string& key, double defaultValue) const
496 {
497     auto value = parameters_.GetParam(key);
498     AAFwk::IDouble* ao = AAFwk::IDouble::Query(value);
499     if (ao != nullptr) {
500         return AAFwk::Double::Unbox(ao);
501     }
502     return defaultValue;
503 }
504 
GetDoubleArrayParam(const std::string & key) const505 std::vector<double> DistributedWantV2::GetDoubleArrayParam(const std::string& key) const
506 {
507     std::vector<double> array;
508     auto value = parameters_.GetParam(key);
509     AAFwk::IArray* ao = AAFwk::IArray::Query(value);
510     if (ao != nullptr && AAFwk::Array::IsDoubleArray(ao)) {
511         auto func = [&](AAFwk::IInterface* object) {
512             if (object == nullptr) {
513                 return;
514             }
515             AAFwk::IDouble* value = AAFwk::IDouble::Query(object);
516             if (value != nullptr) {
517                 array.push_back(AAFwk::Double::Unbox(value));
518             }
519         };
520         AAFwk::Array::ForEach(ao, func);
521     }
522     return array;
523 }
524 
SetParam(const std::string & key,double value)525 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, double value)
526 {
527     parameters_.SetParam(key, AAFwk::Double::Box(value));
528     return *this;
529 }
530 
SetParam(const std::string & key,const std::vector<double> & value)531 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::vector<double>& value)
532 {
533     std::size_t size = value.size();
534     sptr<AAFwk::IArray> ao(new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IDouble));
535     if (ao == nullptr) {
536         return *this;
537     }
538     for (std::size_t i = 0; i < size; i++) {
539         ao->Set(i, AAFwk::Double::Box(value[i]));
540     }
541     parameters_.SetParam(key, ao);
542     return *this;
543 }
544 
GetFloatParam(const std::string & key,float defaultValue) const545 float DistributedWantV2::GetFloatParam(const std::string& key, float defaultValue) const
546 {
547     auto value = parameters_.GetParam(key);
548     AAFwk::IFloat* ao = AAFwk::IFloat::Query(value);
549     if (ao != nullptr) {
550         return AAFwk::Float::Unbox(ao);
551     }
552     return defaultValue;
553 }
554 
GetFloatArrayParam(const std::string & key) const555 std::vector<float> DistributedWantV2::GetFloatArrayParam(const std::string& key) const
556 {
557     std::vector<float> array;
558     auto value = parameters_.GetParam(key);
559     AAFwk::IArray* ao = AAFwk::IArray::Query(value);
560     if (ao != nullptr && AAFwk::Array::IsFloatArray(ao)) {
561         auto func = [&](AAFwk::IInterface* object) {
562             if (object == nullptr) {
563                 return;
564             }
565             AAFwk::IFloat* value = AAFwk::IFloat::Query(object);
566             if (value != nullptr) {
567                 array.push_back(AAFwk::Float::Unbox(value));
568             }
569         };
570         AAFwk::Array::ForEach(ao, func);
571     }
572     return array;
573 }
574 
SetParam(const std::string & key,float value)575 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, float value)
576 {
577     parameters_.SetParam(key, AAFwk::Float::Box(value));
578     return *this;
579 }
580 
SetParam(const std::string & key,const std::vector<float> & value)581 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::vector<float>& value)
582 {
583     std::size_t size = value.size();
584     sptr<AAFwk::IArray> ao(new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IFloat));
585     if (ao == nullptr) {
586         return *this;
587     }
588 
589     for (std::size_t i = 0; i < size; i++) {
590         ao->Set(i, AAFwk::Float::Box(value[i]));
591     }
592     parameters_.SetParam(key, ao);
593     return *this;
594 }
595 
GetLongParam(const std::string & key,long defaultValue) const596 long DistributedWantV2::GetLongParam(const std::string& key, long defaultValue) const
597 {
598     auto value = parameters_.GetParam(key);
599     if (AAFwk::ILong::Query(value) != nullptr) {
600         return AAFwk::Long::Unbox(AAFwk::ILong::Query(value));
601     } else if (AAFwk::IString::Query(value) != nullptr) {
602         // Marshalling
603         std::string str = AAFwk::String::Unbox(AAFwk::IString::Query(value));
604         if (std::regex_match(str, NUMBER_REGEX)) {
605             return std::atoll(str.c_str());
606         }
607     }
608 
609     return defaultValue;
610 }
611 
ArrayAddDataV2(AAFwk::IInterface * object,std::vector<long> & array)612 void ArrayAddDataV2(AAFwk::IInterface* object, std::vector<long>& array)
613 {
614     if (object == nullptr) {
615         return;
616     }
617 
618     AAFwk::IString* o = AAFwk::IString::Query(object);
619     if (o != nullptr) {
620         std::string str = AAFwk::String::Unbox(o);
621         if (std::regex_match(str, NUMBER_REGEX)) {
622             array.push_back(std::atoll(str.c_str()));
623         }
624     }
625 }
626 
GetLongArrayParam(const std::string & key) const627 std::vector<long> DistributedWantV2::GetLongArrayParam(const std::string& key) const
628 {
629     std::vector<long> array;
630     auto value = parameters_.GetParam(key);
631     AAFwk::IArray* ao = AAFwk::IArray::Query(value);
632     if (ao != nullptr && AAFwk::Array::IsLongArray(ao)) {
633         auto func = [&](AAFwk::IInterface* object) {
634             if (object == nullptr) {
635                 return;
636             }
637             AAFwk::ILong* value = AAFwk::ILong::Query(object);
638             if (value != nullptr) {
639                 array.push_back(AAFwk::Long::Unbox(value));
640             }
641         };
642         AAFwk::Array::ForEach(ao, func);
643     } else if (ao != nullptr && AAFwk::Array::IsStringArray(ao)) {
644         // Marshalling
645         auto func = [&](AAFwk::IInterface* object) { ArrayAddDataV2(object, array); };
646         AAFwk::Array::ForEach(ao, func);
647     }
648     return array;
649 }
650 
SetParam(const std::string & key,long value)651 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, long value)
652 {
653     parameters_.SetParam(key, AAFwk::Long::Box(value));
654     return *this;
655 }
656 
SetParam(const std::string & key,const std::vector<long> & value)657 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::vector<long>& value)
658 {
659     std::size_t size = value.size();
660     sptr<AAFwk::IArray> ao(new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_ILong));
661     if (ao == nullptr) {
662         return *this;
663     }
664     for (std::size_t i = 0; i < size; i++) {
665         ao->Set(i, AAFwk::Long::Box(value[i]));
666     }
667     parameters_.SetParam(key, ao);
668     return *this;
669 }
670 
SetParam(const std::string & key,long long value)671 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, long long value)
672 {
673     parameters_.SetParam(key, AAFwk::Long::Box(value));
674     return *this;
675 }
676 
GetShortParam(const std::string & key,short defaultValue) const677 short DistributedWantV2::GetShortParam(const std::string& key, short defaultValue) const
678 {
679     auto value = parameters_.GetParam(key);
680     AAFwk::IShort* ao = AAFwk::IShort::Query(value);
681     if (ao != nullptr) {
682         return AAFwk::Short::Unbox(ao);
683     }
684     return defaultValue;
685 }
686 
GetShortArrayParam(const std::string & key) const687 std::vector<short> DistributedWantV2::GetShortArrayParam(const std::string& key) const
688 {
689     std::vector<short> array;
690     auto value = parameters_.GetParam(key);
691     AAFwk::IArray* ao = AAFwk::IArray::Query(value);
692     if (ao != nullptr && AAFwk::Array::IsShortArray(ao)) {
693         auto func = [&](AAFwk::IInterface* object) {
694             if (object == nullptr) {
695                 return;
696             }
697             AAFwk::IShort* value = AAFwk::IShort::Query(object);
698             if (value != nullptr) {
699                 array.push_back(AAFwk::Short::Unbox(value));
700             }
701         };
702         AAFwk::Array::ForEach(ao, func);
703     }
704     return array;
705 }
706 
SetParam(const std::string & key,short value)707 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, short value)
708 {
709     parameters_.SetParam(key, AAFwk::Short::Box(value));
710     return *this;
711 }
712 
SetParam(const std::string & key,const std::vector<short> & value)713 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::vector<short>& value)
714 {
715     std::size_t size = value.size();
716     sptr<AAFwk::IArray> ao(new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IShort));
717     if (ao == nullptr) {
718         return *this;
719     }
720     for (std::size_t i = 0; i < size; i++) {
721         ao->Set(i, AAFwk::Short::Box(value[i]));
722     }
723     parameters_.SetParam(key, ao);
724     return *this;
725 }
726 
GetStringParam(const std::string & key) const727 std::string DistributedWantV2::GetStringParam(const std::string& key) const
728 {
729     auto value = parameters_.GetParam(key);
730     AAFwk::IString* ao = AAFwk::IString::Query(value);
731     if (ao != nullptr) {
732         return AAFwk::String::Unbox(ao);
733     }
734     return std::string();
735 }
736 
GetStringArrayParam(const std::string & key) const737 std::vector<std::string> DistributedWantV2::GetStringArrayParam(const std::string& key) const
738 {
739     std::vector<std::string> array;
740     auto value = parameters_.GetParam(key);
741     AAFwk::IArray* ao = AAFwk::IArray::Query(value);
742     if (ao != nullptr && AAFwk::Array::IsStringArray(ao)) {
743         auto func = [&](AAFwk::IInterface* object) {
744             if (object == nullptr) {
745                 return;
746             }
747             AAFwk::IString* value = AAFwk::IString::Query(object);
748             if (value != nullptr) {
749                 array.push_back(AAFwk::String::Unbox(value));
750             }
751         };
752         AAFwk::Array::ForEach(ao, func);
753     }
754     return array;
755 }
756 
SetParam(const std::string & key,const std::string & value)757 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::string& value)
758 {
759     parameters_.SetParam(key, AAFwk::String::Box(value));
760     return *this;
761 }
762 
SetParam(const std::string & key,const std::vector<std::string> & value)763 DistributedWantV2& DistributedWantV2::SetParam(const std::string& key, const std::vector<std::string>& value)
764 {
765     std::size_t size = value.size();
766     sptr<AAFwk::IArray> ao(new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IString));
767     if (ao == nullptr) {
768         return *this;
769     }
770     for (std::size_t i = 0; i < size; i++) {
771         ao->Set(i, AAFwk::String::Box(value[i]));
772     }
773     parameters_.SetParam(key, ao);
774     return *this;
775 }
776 
GetOperation() const777 DistributedOperation DistributedWantV2::GetOperation() const
778 {
779     return operation_;
780 }
781 
SetOperation(const DistributedOperation & operation)782 void DistributedWantV2::SetOperation(const DistributedOperation& operation)
783 {
784     operation_ = operation;
785 }
786 
OperationEquals(const DistributedWantV2 & want)787 bool DistributedWantV2::OperationEquals(const DistributedWantV2& want)
788 {
789     return (operation_ == want.operation_);
790 }
791 
GetUriString() const792 std::string DistributedWantV2::GetUriString() const
793 {
794     return operation_.GetUri().ToString();
795 }
796 
GetUri() const797 OHOS::Uri DistributedWantV2::GetUri() const
798 {
799     return operation_.GetUri();
800 }
801 
SetUri(const std::string & uri)802 DistributedWantV2& DistributedWantV2::SetUri(const std::string& uri)
803 {
804     operation_.SetUri(OHOS::Uri(uri));
805     return *this;
806 }
807 
SetUri(const OHOS::Uri & uri)808 DistributedWantV2& DistributedWantV2::SetUri(const OHOS::Uri& uri)
809 {
810     operation_.SetUri(uri);
811     return *this;
812 }
813 
SetUriAndType(const OHOS::Uri & uri,const std::string & type)814 DistributedWantV2& DistributedWantV2::SetUriAndType(const OHOS::Uri& uri, const std::string& type)
815 {
816     operation_.SetUri(uri);
817     return SetType(type);
818 }
819 
FormatUri(const std::string & uri)820 DistributedWantV2& DistributedWantV2::FormatUri(const std::string& uri)
821 {
822     return FormatUri(OHOS::Uri(uri));
823 }
824 
FormatUri(const OHOS::Uri & uri)825 DistributedWantV2& DistributedWantV2::FormatUri(const OHOS::Uri& uri)
826 {
827     operation_.SetUri(GetLowerCaseScheme(uri));
828     return *this;
829 }
830 
HasParameter(const std::string & key) const831 bool DistributedWantV2::HasParameter(const std::string& key) const
832 {
833     return parameters_.HasParam(key);
834 }
835 
ReplaceParams(AAFwk::WantParams & wantParams)836 DistributedWantV2* DistributedWantV2::ReplaceParams(AAFwk::WantParams& wantParams)
837 {
838     parameters_ = wantParams;
839     return this;
840 }
841 
ReplaceParams(DistributedWantV2 & want)842 DistributedWantV2* DistributedWantV2::ReplaceParams(DistributedWantV2& want)
843 {
844     parameters_ = want.parameters_;
845     return this;
846 }
847 
RemoveParam(const std::string & key)848 void DistributedWantV2::RemoveParam(const std::string& key)
849 {
850     parameters_.Remove(key);
851 }
852 
ClearWant(DistributedWantV2 * want)853 void DistributedWantV2::ClearWant(DistributedWantV2* want)
854 {
855     if (want == nullptr) {
856         return;
857     }
858     want->SetType("");
859     want->SetAction("");
860     want->SetFlags(0);
861     OHOS::AppExecFwk::ElementName elementName;
862     want->SetElement(elementName);
863     DistributedOperation operation;
864     want->SetOperation(operation);
865     AAFwk::WantParams parameters;
866     want->SetParams(parameters);
867 }
868 
MarshallingWriteUri(Parcel & parcel) const869 bool DistributedWantV2::MarshallingWriteUri(Parcel& parcel) const
870 {
871     if (GetUriString().empty()) {
872         if (!parcel.WriteInt32(VALUE_NULL)) {
873             return false;
874         }
875         return true;
876     }
877 
878     if (!parcel.WriteInt32(VALUE_OBJECT)) {
879         return false;
880     }
881 
882     if (!parcel.WriteString16(Str8ToStr16(GetUriString()))) {
883         return false;
884     }
885 
886     return true;
887 }
888 
MarshallingWriteEntities(Parcel & parcel) const889 bool DistributedWantV2::MarshallingWriteEntities(Parcel& parcel) const
890 {
891     std::vector<std::u16string> entityU16;
892     std::vector<std::string> entities = GetEntities();
893     for (std::vector<std::string>::size_type i = 0; i < entities.size(); i++) {
894         entityU16.push_back(Str8ToStr16(entities[i]));
895     }
896     if (entityU16.size() == 0) {
897         if (!parcel.WriteInt32(VALUE_NULL)) {
898             return false;
899         }
900     } else {
901         if (!parcel.WriteInt32(VALUE_OBJECT)) {
902             return false;
903         }
904         if (!parcel.WriteString16Vector(entityU16)) {
905             return false;
906         }
907     }
908     return true;
909 }
910 
MarshallingWriteElement(Parcel & parcel) const911 bool DistributedWantV2::MarshallingWriteElement(Parcel& parcel) const
912 {
913     ElementName empty;
914     ElementName element = GetElement();
915     if (element == empty) {
916         if (!parcel.WriteInt32(VALUE_NULL)) {
917             return false;
918         }
919     } else {
920         if (!parcel.WriteInt32(VALUE_OBJECT)) {
921             return false;
922         }
923         if (!parcel.WriteParcelable(&element)) {
924             return false;
925         }
926     }
927     return true;
928 }
929 
MarshallingWriteParameters(Parcel & parcel) const930 bool DistributedWantV2::MarshallingWriteParameters(Parcel& parcel) const
931 {
932     if (parameters_.Size() == 0) {
933         if (!parcel.WriteInt32(VALUE_NULL)) {
934             return false;
935         }
936         return true;
937     }
938 
939     if (!parcel.WriteInt32(VALUE_OBJECT)) {
940         return false;
941     }
942 
943     if (!parcel.WriteParcelable(&parameters_)) {
944         return false;
945     }
946     return true;
947 }
948 
Marshalling(Parcel & parcel) const949 bool DistributedWantV2::Marshalling(Parcel& parcel) const
950 {
951     // write action
952     if (!parcel.WriteString16(Str8ToStr16(GetAction()))) {
953         return false;
954     }
955     // write uri
956     if (!MarshallingWriteUri(parcel)) {
957         return false;
958     }
959     // write entities
960     if (!MarshallingWriteEntities(parcel)) {
961         return false;
962     }
963     // write flags
964     if (!parcel.WriteUint32(GetFlags())) {
965         return false;
966     }
967     // write element
968     if (!MarshallingWriteElement(parcel)) {
969         return false;
970     }
971     // write parameters
972     if (!MarshallingWriteParameters(parcel)) {
973         return false;
974     }
975     // write package
976     if (!parcel.WriteString16(Str8ToStr16(GetBundle()))) {
977         return false;
978     }
979     return true;
980 }
981 
Unmarshalling(Parcel & parcel)982 DistributedWantV2* DistributedWantV2::Unmarshalling(Parcel& parcel)
983 {
984     DistributedWantV2* want = new (std::nothrow) DistributedWantV2();
985     if (want != nullptr && !want->ReadFromParcel(parcel)) {
986         delete want;
987         want = nullptr;
988     }
989     return want;
990 }
991 
ReadUriFromParcel(Parcel & parcel)992 bool DistributedWantV2::ReadUriFromParcel(Parcel& parcel)
993 {
994     int value = VALUE_NULL;
995     if (!parcel.ReadInt32(value)) {
996         return false;
997     }
998     if (value == VALUE_OBJECT) {
999         SetUri(Str16ToStr8(parcel.ReadString16()));
1000     }
1001     return true;
1002 }
1003 
ReadEntitiesFromParcel(Parcel & parcel)1004 bool DistributedWantV2::ReadEntitiesFromParcel(Parcel& parcel)
1005 {
1006     std::vector<std::string> entities;
1007     std::vector<std::u16string> entityU16;
1008     int value = VALUE_NULL;
1009     if (!parcel.ReadInt32(value)) {
1010         return false;
1011     }
1012     if (value == VALUE_OBJECT) {
1013         if (!parcel.ReadString16Vector(&entityU16)) {
1014             return false;
1015         }
1016     }
1017     for (std::vector<std::u16string>::size_type i = 0; i < entityU16.size(); i++) {
1018         entities.push_back(Str16ToStr8(entityU16[i]));
1019     }
1020     operation_.SetEntities(entities);
1021     return true;
1022 }
1023 
ReadElementFromParcel(Parcel & parcel)1024 bool DistributedWantV2::ReadElementFromParcel(Parcel& parcel)
1025 {
1026     int value = VALUE_NULL;
1027     if (!parcel.ReadInt32(value)) {
1028         return false;
1029     }
1030     if (value == VALUE_OBJECT) {
1031         auto element = parcel.ReadParcelable<ElementName>();
1032         if (element != nullptr) {
1033             SetElement(*element);
1034             delete element;
1035         } else {
1036             return false;
1037         }
1038     }
1039     return true;
1040 }
1041 
ReadParametersFromParcel(Parcel & parcel)1042 bool DistributedWantV2::ReadParametersFromParcel(Parcel& parcel)
1043 {
1044     int empty = VALUE_NULL;
1045     if (!parcel.ReadInt32(empty)) {
1046         return false;
1047     }
1048     if (empty == VALUE_OBJECT) {
1049         auto params = parcel.ReadParcelable<AAFwk::WantParams>();
1050         if (params != nullptr) {
1051             parameters_ = *params;
1052             delete params;
1053             params = nullptr;
1054         } else {
1055             return false;
1056         }
1057     }
1058     return true;
1059 }
1060 
ReadFromParcel(Parcel & parcel)1061 bool DistributedWantV2::ReadFromParcel(Parcel& parcel)
1062 {
1063     // read action
1064     operation_.SetAction(Str16ToStr8(parcel.ReadString16()));
1065     // read uri
1066     if (!ReadUriFromParcel(parcel)) {
1067         return false;
1068     }
1069     // read entities
1070     if (!ReadEntitiesFromParcel(parcel)) {
1071         return false;
1072     }
1073     // read flags
1074     unsigned int flags;
1075     if (!parcel.ReadUint32(flags)) {
1076         return false;
1077     }
1078     operation_.SetFlags(flags);
1079     // read element
1080     if (!ReadElementFromParcel(parcel)) {
1081         return false;
1082     }
1083     // read parameters
1084     if (!ReadParametersFromParcel(parcel)) {
1085         return false;
1086     }
1087     // read package
1088     operation_.SetBundleName(Str16ToStr8(parcel.ReadString16()));
1089     return true;
1090 }
1091 
ToJson() const1092 nlohmann::json DistributedWantV2::ToJson() const
1093 {
1094     AAFwk::WantParamWrapper wrapper(parameters_);
1095     std::string parametersString = wrapper.ToString();
1096 
1097     nlohmann::json dEntitiesJson;
1098     std::vector<std::string> entities = GetEntities();
1099     for (auto entity : entities) {
1100         dEntitiesJson.emplace_back(entity);
1101     }
1102 
1103     nlohmann::json wantJson = nlohmann::json {
1104         {"deviceId", operation_.GetDeviceId()},
1105         {"bundleName", operation_.GetBundleName()},
1106         {"abilityName", operation_.GetAbilityName()},
1107         {"uri", GetUriString()},
1108         {"type", GetType()},
1109         {"flags", GetFlags()},
1110         {"action", GetAction()},
1111         {"parameters", parametersString},
1112         {"entities", dEntitiesJson},
1113     };
1114     return wantJson;
1115 }
1116 
CanReadFromJson(nlohmann::json & wantJson)1117 bool DistributedWantV2::CanReadFromJson(nlohmann::json& wantJson)
1118 {
1119     const auto& jsonObjectEnd = wantJson.end();
1120     if ((wantJson.find("deviceId") == jsonObjectEnd)
1121         || (wantJson.find("bundleName") == jsonObjectEnd)
1122         || (wantJson.find("abilityName") == jsonObjectEnd)
1123         || (wantJson.find("uri") == jsonObjectEnd)
1124         || (wantJson.find("type") == jsonObjectEnd)
1125         || (wantJson.find("flags") == jsonObjectEnd)
1126         || (wantJson.find("action") == jsonObjectEnd)
1127         || (wantJson.find("parameters") == jsonObjectEnd)
1128         || (wantJson.find("entities") == jsonObjectEnd)) {
1129         return false;
1130     }
1131     if (!wantJson["deviceId"].is_string()) {
1132         HILOGE("device id is not string");
1133         return false;
1134     }
1135     if (!wantJson["bundleName"].is_string()) {
1136         HILOGE("bundle name is not string");
1137         return false;
1138     }
1139     if (!wantJson["abilityName"].is_string()) {
1140         HILOGE("ability name is not string");
1141         return false;
1142     }
1143     if (!wantJson["uri"].is_string()) {
1144         HILOGE("uri is not string");
1145         return false;
1146     }
1147     if (!wantJson["type"].is_string()) {
1148         HILOGE("type is not string");
1149         return false;
1150     }
1151     if (!wantJson["flags"].is_number_unsigned()) {
1152         HILOGE("flags is not a number");
1153         return false;
1154     }
1155     if (!wantJson["action"].is_string()) {
1156         HILOGE("action is not string");
1157         return false;
1158     }
1159     if (!wantJson["parameters"].is_string()) {
1160         HILOGE("parameters is not string");
1161         return false;
1162     }
1163     return true;
1164 }
1165 
ReadFromJson(nlohmann::json & wantJson)1166 bool DistributedWantV2::ReadFromJson(nlohmann::json& wantJson)
1167 {
1168     if (!CanReadFromJson(wantJson)) {
1169         HILOGE("can not read from json");
1170         return false;
1171     }
1172     if (!wantJson.contains("deviceId") || !wantJson.contains("bundleName") || !wantJson.contains("abilityName") ||
1173         !wantJson.contains("uri") || !wantJson.contains("type") || !wantJson.contains("flags") ||
1174         !wantJson.contains("action") || !wantJson.contains("parameters") || !wantJson.contains("entities")) {
1175         HILOGE("data is empty");
1176         return false;
1177     }
1178 
1179     std::string deviceId = wantJson.at("deviceId").get<std::string>();
1180     std::string bundleName = wantJson.at("bundleName").get<std::string>();
1181     std::string abilityName = wantJson.at("abilityName").get<std::string>();
1182     SetElementName(deviceId, bundleName, abilityName);
1183 
1184     std::string uri = wantJson.at("uri").get<std::string>();
1185     SetUri(uri);
1186 
1187     std::string type = wantJson.at("type").get<std::string>();
1188     SetType(type);
1189 
1190     if (!wantJson.at("flags").is_number()) {
1191         return false;
1192     }
1193     unsigned int flags = wantJson.at("flags").get<unsigned int>();
1194     SetFlags(flags);
1195 
1196     std::string action = wantJson.at("action").get<std::string>();
1197     SetAction(action);
1198 
1199     if (!wantJson.at("parameters").is_string()) {
1200         return false;
1201     }
1202     std::string parametersString = wantJson.at("parameters").get<std::string>();
1203     AAFwk::WantParams parameters = AAFwk::WantParamWrapper::ParseWantParams(parametersString);
1204     SetParams(parameters);
1205     if (!wantJson.at("entities").is_null()) {
1206         std::vector<std::string> entities;
1207         wantJson.at("entities").get_to<std::vector<std::string>>(entities);
1208         for (size_t i = 0; i < entities.size(); i++) {
1209             AddEntity(entities[i]);
1210         }
1211     }
1212     return true;
1213 }
1214 
ToString() const1215 std::string DistributedWantV2::ToString() const
1216 {
1217     return ToJson().dump();
1218 }
1219 
FromString(std::string & string)1220 DistributedWantV2* DistributedWantV2::FromString(std::string& string)
1221 {
1222     if (string.empty()) {
1223         return nullptr;
1224     }
1225 
1226     nlohmann::json wantJson = nlohmann::json::parse(string, nullptr, false);
1227     if (wantJson.is_discarded()) {
1228         return nullptr;
1229     }
1230 
1231     DistributedWantV2* want = new (std::nothrow) DistributedWantV2();
1232     if (want != nullptr && !want->ReadFromJson(wantJson)) {
1233         delete want;
1234         want = nullptr;
1235     }
1236     return want;
1237 }
1238 
SetDeviceId(const std::string & deviceId)1239 DistributedWantV2& DistributedWantV2::SetDeviceId(const std::string& deviceId)
1240 {
1241     operation_.SetDeviceId(deviceId);
1242     return *this;
1243 }
1244 } // namespace DistributedSchedule
1245 } // namespace OHOS
1246