• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_operation.h"
17 #include "distributed_operation_builder.h"
18 #include "parcel_macro_base.h"
19 
20 namespace OHOS {
21 namespace DistributedSchedule {
DistributedOperation()22 DistributedOperation::DistributedOperation() : flags_(0), uri_("")
23 {
24 }
25 
DistributedOperation(const DistributedOperation & other)26 DistributedOperation::DistributedOperation(const DistributedOperation& 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 
~DistributedOperation()38 DistributedOperation::~DistributedOperation()
39 {}
40 
GetAbilityName() const41 std::string DistributedOperation::GetAbilityName() const
42 {
43     return abilityName_;
44 }
45 
GetAction() const46 std::string DistributedOperation::GetAction() const
47 {
48     return action_;
49 }
50 
SetBundleName(const std::string & bundleName)51 void DistributedOperation::SetBundleName(const std::string& bundleName)
52 {
53     bundleName_ = bundleName;
54 }
55 
GetBundleName() const56 std::string DistributedOperation::GetBundleName() const
57 {
58     return bundleName_;
59 }
60 
GetDeviceId() const61 std::string DistributedOperation::GetDeviceId() const
62 {
63     return deviceId_;
64 }
65 
GetEntities() const66 const std::vector<std::string>& DistributedOperation::GetEntities() const
67 {
68     return entities_;
69 }
70 
GetModuleName() const71 std::string DistributedOperation::GetModuleName() const
72 {
73     return moduleName_;
74 }
75 
AddEntity(const std::string & entity)76 void DistributedOperation::AddEntity(const std::string& entity)
77 {
78     if (!HasEntity(entity)) {
79         entities_.emplace_back(entity);
80     }
81 }
82 
RemoveEntity(const std::string & entity)83 void DistributedOperation::RemoveEntity(const std::string& entity)
84 {
85     if (!entities_.empty()) {
86         auto it = std::find(entities_.begin(), entities_.end(), entity);
87         if (it != entities_.end()) {
88             entities_.erase(it);
89         }
90     }
91 }
92 
HasEntity(const std::string & entity) const93 bool DistributedOperation::HasEntity(const std::string& entity) const
94 {
95     return std::find(entities_.begin(), entities_.end(), entity) != entities_.end();
96 }
97 
CountEntities() const98 int DistributedOperation::CountEntities() const
99 {
100     return entities_.size();
101 }
102 
GetFlags() const103 unsigned int DistributedOperation::GetFlags() const
104 {
105     return flags_;
106 }
107 
SetFlags(unsigned int flags)108 void DistributedOperation::SetFlags(unsigned int flags)
109 {
110     flags_ = flags;
111 }
112 
AddFlags(unsigned int flags)113 void DistributedOperation::AddFlags(unsigned int flags)
114 {
115     flags_ |= flags;
116 }
117 
RemoveFlags(unsigned int flags)118 void DistributedOperation::RemoveFlags(unsigned int flags)
119 {
120     flags_ &= ~flags;
121 }
122 
GetUri() const123 Uri DistributedOperation::GetUri() const
124 {
125     return uri_;
126 }
127 
operator ==(const DistributedOperation & other) const128 bool DistributedOperation::operator==(const DistributedOperation& other) const
129 {
130     if (abilityName_ != other.abilityName_) {
131         return false;
132     }
133     if (action_ != other.action_) {
134         return false;
135     }
136     if (bundleName_ != other.bundleName_) {
137         return false;
138     }
139     if (deviceId_ != other.deviceId_) {
140         return false;
141     }
142     if (moduleName_ != other.moduleName_) {
143         return false;
144     }
145 
146     size_t entitiesCount = entities_.size();
147     size_t otherEntitiesCount = other.entities_.size();
148     if (entitiesCount != otherEntitiesCount) {
149         return false;
150     } else {
151         for (size_t i = 0; i < entitiesCount; i++) {
152             if (entities_[i] != other.entities_[i]) {
153                 return false;
154             }
155         }
156     }
157     if (flags_ != other.flags_) {
158         return false;
159     }
160     if (uri_.ToString() != other.uri_.ToString()) {
161         return false;
162     }
163     return true;
164 }
165 
operator =(const DistributedOperation & other)166 DistributedOperation& DistributedOperation::operator=(const DistributedOperation& other)
167 {
168     if (this != &other) {
169         uri_ = other.uri_;
170         flags_ = other.flags_;
171         action_ = other.action_;
172         deviceId_ = other.deviceId_;
173         entities_ = other.entities_;
174         bundleName_ = other.bundleName_;
175         abilityName_ = other.abilityName_;
176         moduleName_ = other.moduleName_;
177     }
178     return *this;
179 }
180 
Marshalling(Parcel & parcel) const181 bool DistributedOperation::Marshalling(Parcel& parcel) const
182 {
183     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName_));
184     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(action_));
185     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName_));
186     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(deviceId_));
187     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(StringVector, parcel, entities_);
188     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, flags_);
189 
190     Uri uri("");
191     if (uri_ == uri) {
192         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, VALUE_NULL);
193     } else {
194         if (!parcel.WriteInt32(VALUE_OBJECT)) {
195             return false;
196         }
197         if (!parcel.WriteParcelable(&uri_)) {
198             return false;
199         }
200     }
201     return true;
202 }
203 
Unmarshalling(Parcel & parcel)204 DistributedOperation* DistributedOperation::Unmarshalling(Parcel& parcel)
205 {
206     DistributedOperation* operation = new (std::nothrow) DistributedOperation();
207     if (operation != nullptr && !operation->ReadFromParcel(parcel)) {
208         delete operation;
209         operation = nullptr;
210     }
211 
212     return operation;
213 }
214 
ReadFromParcel(Parcel & parcel)215 bool DistributedOperation::ReadFromParcel(Parcel& parcel)
216 {
217     std::u16string readString16;
218     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, readString16);
219     abilityName_ = Str16ToStr8(readString16);
220     readString16.clear();
221 
222     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, readString16);
223     action_ = Str16ToStr8(readString16);
224     readString16.clear();
225 
226     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, readString16);
227     bundleName_ = Str16ToStr8(readString16);
228     readString16.clear();
229 
230     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, readString16);
231     deviceId_ = Str16ToStr8(readString16);
232     readString16.clear();
233 
234     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(StringVector, parcel, &entities_);
235     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, flags_);
236 
237     // uri_
238     int32_t empty = VALUE_NULL;
239     if (!parcel.ReadInt32(empty)) {
240         return false;
241     }
242 
243     if (empty == VALUE_OBJECT) {
244         auto uri = parcel.ReadParcelable<Uri>();
245         if (uri != nullptr) {
246             uri_ = *uri;
247             delete uri;
248             uri = nullptr;
249         } else {
250             return false;
251         }
252     }
253     return true;
254 }
255 
SetUri(const Uri & uri)256 void DistributedOperation::SetUri(const Uri& uri)
257 {
258     uri_ = uri;
259 }
260 
GetUri(const Uri & uri)261 Uri& DistributedOperation::GetUri(const Uri& uri)
262 {
263     return uri_;
264 }
265 
SetAbilityName(const std::string & abilityname)266 void DistributedOperation::SetAbilityName(const std::string& abilityname)
267 {
268     abilityName_ = abilityname;
269 }
270 
SetDeviceId(const std::string & deviceid)271 void DistributedOperation::SetDeviceId(const std::string& deviceid)
272 {
273     deviceId_ = deviceid;
274 }
275 
SetAction(const std::string & action)276 void DistributedOperation::SetAction(const std::string& action)
277 {
278     action_ = action;
279 }
280 
SetEntities(const std::vector<std::string> & entities)281 void DistributedOperation::SetEntities(const std::vector<std::string>& entities)
282 {
283     entities_.clear();
284     entities_ = entities;
285 }
286 
SetModuleName(const std::string & moduleName)287 void DistributedOperation::SetModuleName(const std::string& moduleName)
288 {
289     moduleName_ = moduleName;
290 }
291 } // namespace DistributedSchedule
292 } // namespace OHOS