• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "operation.h"
17 #include "operation_builder.h"
18 #include "parcel_macro_base.h"
19 using namespace OHOS::AppExecFwk;
20 namespace OHOS {
21 namespace AAFwk {
Operation()22 Operation::Operation() : flags_(0), uri_("")
23 {
24 }
25 
Operation(const Operation & other)26 Operation::Operation(const Operation &other) : flags_(0), uri_(other.uri_.ToString())
27 {
28     flags_ = other.flags_;
29     action_ = other.action_;
30     deviceId_ = other.deviceId_;
31     entities_ = other.entities_;
32     bundleName_ = other.bundleName_;
33     abilityName_ = other.abilityName_;
34     moduleName_ = other.moduleName_;
35     entities_.clear();
36 }
37 
~Operation()38 Operation::~Operation()
39 {}
40 
41 /**
42  * @description: Obtains the value of the abilityName attribute included in this Operation.
43  * @return Returns the ability name included in this Operation.
44  */
GetAbilityName() const45 std::string Operation::GetAbilityName() const
46 {
47     return abilityName_;
48 }
49 
50 /**
51  * @description: Obtains the value of the action attribute included in this Operation.
52  * @return Returns the action included in this Operation.
53  */
GetAction() const54 std::string Operation::GetAction() const
55 {
56     return action_;
57 }
58 /**
59  * @description: Sets a bundle name in this Want.
60  * If a bundle name is specified in a Want, the Want will match only
61  * the abilities in the specified bundle. You cannot use this method and
62  * setPicker(ohos.aafwk.content.Want) on the same Want.
63  * @param bundleName Indicates the bundle name to set.
64  * @return Returns a Want object containing the specified bundle name.
65  */
SetBundleName(const std::string & bundleName)66 void Operation::SetBundleName(const std::string &bundleName)
67 {
68     bundleName_ = bundleName;
69 }
70 /**
71  * @description: Obtains the value of the bundleName attribute included in this Operation.
72  * @return Returns the bundle name included in this Operation.
73  */
GetBundleName() const74 std::string Operation::GetBundleName() const
75 {
76     return bundleName_;
77 }
78 
79 /**
80  * @description: Obtains the value of the deviceId attribute included in this Operation.
81  * @return Returns the device ID included in this Operation.
82  */
GetDeviceId() const83 std::string Operation::GetDeviceId() const
84 {
85     return deviceId_;
86 }
87 
88 /**
89  * @description: Obtains the value of the entities attribute included in this Operation.
90  * @return Returns the entities included in this Operation.
91  */
GetEntities() const92 const std::vector<std::string> &Operation::GetEntities() const
93 {
94     return entities_;
95 }
96 
GetModuleName() const97 std::string Operation::GetModuleName() const
98 {
99     return moduleName_;
100 }
101 
102 /**
103  * @description: Adds the description of an entity to a Want
104  * @param entity Indicates the entity description to add
105  * @return Returns this Want object containing the entity.
106  */
AddEntity(const std::string & entity)107 void Operation::AddEntity(const std::string &entity)
108 {
109     if (!HasEntity(entity)) {
110         entities_.emplace_back(entity);
111     }
112 }
113 
114 /**
115  * @description: Removes the description of an entity from a Want
116  * @param entity Indicates the entity description to remove.
117  * @return void
118  */
RemoveEntity(const std::string & entity)119 void Operation::RemoveEntity(const std::string &entity)
120 {
121     if (!entities_.empty()) {
122         auto it = std::find(entities_.begin(), entities_.end(), entity);
123         if (it != entities_.end()) {
124             entities_.erase(it);
125         }
126     }
127 }
128 
129 /**
130  * @description: Checks whether a Want contains the given entity
131  * @param entity Indicates the entity to check
132  * @return Returns true if the given entity is contained; returns false otherwise
133  */
HasEntity(const std::string & entity) const134 bool Operation::HasEntity(const std::string &entity) const
135 {
136     return std::find(entities_.begin(), entities_.end(), entity) != entities_.end();
137 }
138 
139 /**
140  * @description: Obtains the number of entities in a Want
141  * @return Returns the entity quantity
142  */
CountEntities() const143 int Operation::CountEntities() const
144 {
145     return entities_.size();
146 }
147 /**
148  * @description: Obtains the value of the flags attribute included in this Operation.
149  * @return Returns the flags included in this Operation.
150  */
GetFlags() const151 unsigned int Operation::GetFlags() const
152 {
153     return flags_;
154 }
155 
156 /**
157  * @description: Sets a flag in a Want.
158  * @param flags Indicates the flag to set.
159  * @return Returns this Want object containing the flag.
160  */
SetFlags(unsigned int flags)161 void Operation::SetFlags(unsigned int flags)
162 {
163     flags_ = flags;
164 }
165 /**
166  * @description: Adds a flag to a Want.
167  * @param flags Indicates the flag to add.
168  * @return Returns the Want object with the added flag.
169  */
AddFlags(unsigned int flags)170 void Operation::AddFlags(unsigned int flags)
171 {
172     flags_ |= flags;
173 }
174 
175 /**
176  * @description: Removes the description of a flag from a Want.
177  * @param flags Indicates the flag to remove.
178  * @return Removes the description of a flag from a Want.
179  */
RemoveFlags(unsigned int flags)180 void Operation::RemoveFlags(unsigned int flags)
181 {
182     flags_ &= ~flags;
183 }
184 
185 /**
186  * @description: Obtains the value of the uri attribute included in this Operation.
187  * @return Returns the URI included in this Operation.
188  */
GetUri() const189 Uri Operation::GetUri() const
190 {
191     return uri_;
192 }
193 
operator ==(const Operation & other) const194 bool Operation::operator==(const Operation &other) const
195 {
196     if (abilityName_ != other.abilityName_) {
197         return false;
198     }
199     if (action_ != other.action_) {
200         return false;
201     }
202     if (bundleName_ != other.bundleName_) {
203         return false;
204     }
205     if (deviceId_ != other.deviceId_) {
206         return false;
207     }
208     if (moduleName_ != other.moduleName_) {
209         return false;
210     }
211 
212     size_t entitiesCount = entities_.size();
213     size_t otherEntitiesCount = other.entities_.size();
214     if (entitiesCount != otherEntitiesCount) {
215         return false;
216     } else {
217         for (size_t i = 0; i < entitiesCount; i++) {
218             if (entities_[i] != other.entities_[i]) {
219                 return false;
220             }
221         }
222     }
223     if (flags_ != other.flags_) {
224         return false;
225     }
226     if (uri_.ToString() != other.uri_.ToString()) {
227         return false;
228     }
229     return true;
230 }
231 
operator =(const Operation & other)232 Operation &Operation::operator=(const Operation &other)
233 {
234     if (this != &other) {
235         uri_ = other.uri_;
236         flags_ = other.flags_;
237         action_ = other.action_;
238         deviceId_ = other.deviceId_;
239         entities_ = other.entities_;
240         bundleName_ = other.bundleName_;
241         abilityName_ = other.abilityName_;
242         moduleName_ = other.moduleName_;
243     }
244     return *this;
245 }
246 
Marshalling(Parcel & parcel) const247 bool Operation::Marshalling(Parcel &parcel) const
248 {
249     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName_));
250     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(action_));
251     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName_));
252     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(deviceId_));
253     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(StringVector, parcel, entities_);
254     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, flags_);
255 
256     Uri uri("");
257     if (uri_ == uri) {
258         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, VALUE_NULL);
259     } else {
260         if (!parcel.WriteInt32(VALUE_OBJECT)) {
261             return false;
262         }
263         if (!parcel.WriteParcelable(&uri_)) {
264             return false;
265         }
266     }
267     return true;
268 }
269 
Unmarshalling(Parcel & parcel)270 Operation *Operation::Unmarshalling(Parcel &parcel)
271 {
272     Operation *operation = new (std::nothrow) Operation();
273     if (operation != nullptr && !operation->ReadFromParcel(parcel)) {
274         delete operation;
275         operation = nullptr;
276     }
277 
278     return operation;
279 }
280 
ReadFromParcel(Parcel & parcel)281 bool Operation::ReadFromParcel(Parcel &parcel)
282 {
283     std::u16string readString16;
284     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, readString16);
285     abilityName_ = Str16ToStr8(readString16);
286     readString16.clear();
287 
288     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, readString16);
289     action_ = Str16ToStr8(readString16);
290     readString16.clear();
291 
292     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, readString16);
293     bundleName_ = Str16ToStr8(readString16);
294     readString16.clear();
295 
296     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, readString16);
297     deviceId_ = Str16ToStr8(readString16);
298     readString16.clear();
299 
300     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(StringVector, parcel, &entities_);
301     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, flags_);
302 
303     // uri_
304     int32_t empty = VALUE_NULL;
305     if (!parcel.ReadInt32(empty)) {
306         return false;
307     }
308 
309     if (empty == VALUE_OBJECT) {
310         auto uri = parcel.ReadParcelable<Uri>();
311         if (uri != nullptr) {
312             uri_ = *uri;
313             delete uri;
314             uri = nullptr;
315         } else {
316             return false;
317         }
318     }
319     return true;
320 }
321 
322 /**
323  * @description: Sets a uri in this operation.
324  * @param uri Indicates uri object to set.
325  * @return -
326  */
SetUri(const Uri & uri)327 void Operation::SetUri(const Uri &uri)
328 {
329     uri_ = uri;
330 }
331 
332 /**
333  * @description: Returns a uri in this operation.
334  * @param uri Indicates uri object to set.
335  * @return Returns a uri in this operation.
336  */
GetUri(const Uri & uri)337 Uri &Operation::GetUri(const Uri &uri)
338 {
339     return uri_;
340 }
341 
342 /**
343  * @description: Sets the value of the abilityName attribute included in this Operation.
344  * @param abilityname
345  * @return -
346  */
SetAbilityName(const std::string & abilityname)347 void Operation::SetAbilityName(const std::string &abilityname)
348 {
349     abilityName_ = abilityname;
350 }
351 /**
352  * @description: Sets the value of the deviceId attribute included in this Operation.
353  * @param  deviceid
354  * @return -
355  */
SetDeviceId(const std::string & deviceid)356 void Operation::SetDeviceId(const std::string &deviceid)
357 {
358     deviceId_ = deviceid;
359 }
360 
361 /**
362  * @description: Sets the value of the action attribute included in this Operation.
363  * @param deviceid Indicates deviceid object to set.
364  * @return -
365  */
SetAction(const std::string & action)366 void Operation::SetAction(const std::string &action)
367 {
368     action_ = action;
369 }
370 
371 /**
372  * @description: Sets the entities of this Operation.
373  * @param entities Indicates entities to set.
374  * @return -
375  */
SetEntities(const std::vector<std::string> & entities)376 void Operation::SetEntities(const std::vector<std::string> &entities)
377 {
378     entities_.clear();
379     entities_ = entities;
380 }
381 
SetModuleName(const std::string & moduleName)382 void Operation::SetModuleName(const std::string &moduleName)
383 {
384     moduleName_ = moduleName;
385 }
386 
DumpInfo(int level) const387 void Operation::DumpInfo(int level) const
388 {
389     ABILITYBASE_LOGI("===Operation::abilityName_ %{public}s =============", abilityName_.c_str());
390     ABILITYBASE_LOGI("===Operation::action_ %{public}s =============", action_.c_str());
391     ABILITYBASE_LOGI("===Operation::bundleName_ %{public}s =============", bundleName_.c_str());
392     ABILITYBASE_LOGI("===Operation::moduleName_ %{public}s =============", moduleName_.c_str());
393     size_t entities_count = entities_.size();
394     ABILITYBASE_LOGI("===Operation::entities_: count %{public}u =============", (uint32_t)entities_count);
395     for (size_t i = 0; i < entities_count; i++) {
396         ABILITYBASE_LOGI("=Operation::entities_[%{public}u]: %{public}s =============", (uint32_t)i,
397             entities_[i].c_str());
398     }
399     ABILITYBASE_LOGI("===Operation::flags_ %{public}ud =============", flags_);
400     ABILITYBASE_LOGI("===Operation::uri_ %{public}s =============", uri_.ToString().c_str());
401 }
402 }  // namespace AAFwk
403 }  // namespace OHOS