1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "distributed_operation.h"
17686862fbSopenharmony_ci#include "distributed_operation_builder.h"
18686862fbSopenharmony_ci#include "parcel_macro_base.h"
19686862fbSopenharmony_ci
20686862fbSopenharmony_cinamespace OHOS {
21686862fbSopenharmony_cinamespace DistributedSchedule {
22686862fbSopenharmony_ciDistributedOperation::DistributedOperation() : flags_(0), uri_("")
23686862fbSopenharmony_ci{
24686862fbSopenharmony_ci}
25686862fbSopenharmony_ci
26686862fbSopenharmony_ciDistributedOperation::DistributedOperation(const DistributedOperation& other) : flags_(0), uri_(other.uri_.ToString())
27686862fbSopenharmony_ci{
28686862fbSopenharmony_ci    flags_ = other.flags_;
29686862fbSopenharmony_ci    action_ = other.action_;
30686862fbSopenharmony_ci    deviceId_ = other.deviceId_;
31686862fbSopenharmony_ci    entities_ = other.entities_;
32686862fbSopenharmony_ci    bundleName_ = other.bundleName_;
33686862fbSopenharmony_ci    abilityName_ = other.abilityName_;
34686862fbSopenharmony_ci    moduleName_ = other.moduleName_;
35686862fbSopenharmony_ci    entities_.clear();
36686862fbSopenharmony_ci}
37686862fbSopenharmony_ci
38686862fbSopenharmony_ciDistributedOperation::~DistributedOperation()
39686862fbSopenharmony_ci{}
40686862fbSopenharmony_ci
41686862fbSopenharmony_cistd::string DistributedOperation::GetAbilityName() const
42686862fbSopenharmony_ci{
43686862fbSopenharmony_ci    return abilityName_;
44686862fbSopenharmony_ci}
45686862fbSopenharmony_ci
46686862fbSopenharmony_cistd::string DistributedOperation::GetAction() const
47686862fbSopenharmony_ci{
48686862fbSopenharmony_ci    return action_;
49686862fbSopenharmony_ci}
50686862fbSopenharmony_ci
51686862fbSopenharmony_civoid DistributedOperation::SetBundleName(const std::string& bundleName)
52686862fbSopenharmony_ci{
53686862fbSopenharmony_ci    bundleName_ = bundleName;
54686862fbSopenharmony_ci}
55686862fbSopenharmony_ci
56686862fbSopenharmony_cistd::string DistributedOperation::GetBundleName() const
57686862fbSopenharmony_ci{
58686862fbSopenharmony_ci    return bundleName_;
59686862fbSopenharmony_ci}
60686862fbSopenharmony_ci
61686862fbSopenharmony_cistd::string DistributedOperation::GetDeviceId() const
62686862fbSopenharmony_ci{
63686862fbSopenharmony_ci    return deviceId_;
64686862fbSopenharmony_ci}
65686862fbSopenharmony_ci
66686862fbSopenharmony_ciconst std::vector<std::string>& DistributedOperation::GetEntities() const
67686862fbSopenharmony_ci{
68686862fbSopenharmony_ci    return entities_;
69686862fbSopenharmony_ci}
70686862fbSopenharmony_ci
71686862fbSopenharmony_cistd::string DistributedOperation::GetModuleName() const
72686862fbSopenharmony_ci{
73686862fbSopenharmony_ci    return moduleName_;
74686862fbSopenharmony_ci}
75686862fbSopenharmony_ci
76686862fbSopenharmony_civoid DistributedOperation::AddEntity(const std::string& entity)
77686862fbSopenharmony_ci{
78686862fbSopenharmony_ci    if (!HasEntity(entity)) {
79686862fbSopenharmony_ci        entities_.emplace_back(entity);
80686862fbSopenharmony_ci    }
81686862fbSopenharmony_ci}
82686862fbSopenharmony_ci
83686862fbSopenharmony_civoid DistributedOperation::RemoveEntity(const std::string& entity)
84686862fbSopenharmony_ci{
85686862fbSopenharmony_ci    if (!entities_.empty()) {
86686862fbSopenharmony_ci        auto it = std::find(entities_.begin(), entities_.end(), entity);
87686862fbSopenharmony_ci        if (it != entities_.end()) {
88686862fbSopenharmony_ci            entities_.erase(it);
89686862fbSopenharmony_ci        }
90686862fbSopenharmony_ci    }
91686862fbSopenharmony_ci}
92686862fbSopenharmony_ci
93686862fbSopenharmony_cibool DistributedOperation::HasEntity(const std::string& entity) const
94686862fbSopenharmony_ci{
95686862fbSopenharmony_ci    return std::find(entities_.begin(), entities_.end(), entity) != entities_.end();
96686862fbSopenharmony_ci}
97686862fbSopenharmony_ci
98686862fbSopenharmony_ciint DistributedOperation::CountEntities() const
99686862fbSopenharmony_ci{
100686862fbSopenharmony_ci    return entities_.size();
101686862fbSopenharmony_ci}
102686862fbSopenharmony_ci
103686862fbSopenharmony_ciunsigned int DistributedOperation::GetFlags() const
104686862fbSopenharmony_ci{
105686862fbSopenharmony_ci    return flags_;
106686862fbSopenharmony_ci}
107686862fbSopenharmony_ci
108686862fbSopenharmony_civoid DistributedOperation::SetFlags(unsigned int flags)
109686862fbSopenharmony_ci{
110686862fbSopenharmony_ci    flags_ = flags;
111686862fbSopenharmony_ci}
112686862fbSopenharmony_ci
113686862fbSopenharmony_civoid DistributedOperation::AddFlags(unsigned int flags)
114686862fbSopenharmony_ci{
115686862fbSopenharmony_ci    flags_ |= flags;
116686862fbSopenharmony_ci}
117686862fbSopenharmony_ci
118686862fbSopenharmony_civoid DistributedOperation::RemoveFlags(unsigned int flags)
119686862fbSopenharmony_ci{
120686862fbSopenharmony_ci    flags_ &= ~flags;
121686862fbSopenharmony_ci}
122686862fbSopenharmony_ci
123686862fbSopenharmony_ciUri DistributedOperation::GetUri() const
124686862fbSopenharmony_ci{
125686862fbSopenharmony_ci    return uri_;
126686862fbSopenharmony_ci}
127686862fbSopenharmony_ci
128686862fbSopenharmony_cibool DistributedOperation::operator==(const DistributedOperation& other) const
129686862fbSopenharmony_ci{
130686862fbSopenharmony_ci    if (abilityName_ != other.abilityName_) {
131686862fbSopenharmony_ci        return false;
132686862fbSopenharmony_ci    }
133686862fbSopenharmony_ci    if (action_ != other.action_) {
134686862fbSopenharmony_ci        return false;
135686862fbSopenharmony_ci    }
136686862fbSopenharmony_ci    if (bundleName_ != other.bundleName_) {
137686862fbSopenharmony_ci        return false;
138686862fbSopenharmony_ci    }
139686862fbSopenharmony_ci    if (deviceId_ != other.deviceId_) {
140686862fbSopenharmony_ci        return false;
141686862fbSopenharmony_ci    }
142686862fbSopenharmony_ci    if (moduleName_ != other.moduleName_) {
143686862fbSopenharmony_ci        return false;
144686862fbSopenharmony_ci    }
145686862fbSopenharmony_ci
146686862fbSopenharmony_ci    size_t dEntitiesCount = entities_.size();
147686862fbSopenharmony_ci    size_t otherEntitiesCount = other.entities_.size();
148686862fbSopenharmony_ci    if (dEntitiesCount != otherEntitiesCount) {
149686862fbSopenharmony_ci        return false;
150686862fbSopenharmony_ci    } else {
151686862fbSopenharmony_ci        for (size_t i = 0; i < dEntitiesCount; i++) {
152686862fbSopenharmony_ci            if (entities_[i] != other.entities_[i]) {
153686862fbSopenharmony_ci                return false;
154686862fbSopenharmony_ci            }
155686862fbSopenharmony_ci        }
156686862fbSopenharmony_ci    }
157686862fbSopenharmony_ci    if (flags_ != other.flags_) {
158686862fbSopenharmony_ci        return false;
159686862fbSopenharmony_ci    }
160686862fbSopenharmony_ci    if (uri_.ToString() != other.uri_.ToString()) {
161686862fbSopenharmony_ci        return false;
162686862fbSopenharmony_ci    }
163686862fbSopenharmony_ci    return true;
164686862fbSopenharmony_ci}
165686862fbSopenharmony_ci
166686862fbSopenharmony_ciDistributedOperation& DistributedOperation::operator=(const DistributedOperation& other)
167686862fbSopenharmony_ci{
168686862fbSopenharmony_ci    if (this != &other) {
169686862fbSopenharmony_ci        uri_ = other.uri_;
170686862fbSopenharmony_ci        flags_ = other.flags_;
171686862fbSopenharmony_ci        action_ = other.action_;
172686862fbSopenharmony_ci        deviceId_ = other.deviceId_;
173686862fbSopenharmony_ci        entities_ = other.entities_;
174686862fbSopenharmony_ci        bundleName_ = other.bundleName_;
175686862fbSopenharmony_ci        abilityName_ = other.abilityName_;
176686862fbSopenharmony_ci        moduleName_ = other.moduleName_;
177686862fbSopenharmony_ci    }
178686862fbSopenharmony_ci    return *this;
179686862fbSopenharmony_ci}
180686862fbSopenharmony_ci
181686862fbSopenharmony_cibool DistributedOperation::Marshalling(Parcel& parcel) const
182686862fbSopenharmony_ci{
183686862fbSopenharmony_ci    WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName_));
184686862fbSopenharmony_ci    WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(action_));
185686862fbSopenharmony_ci    WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName_));
186686862fbSopenharmony_ci    WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(deviceId_));
187686862fbSopenharmony_ci    WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(StringVector, parcel, entities_);
188686862fbSopenharmony_ci    WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, flags_);
189686862fbSopenharmony_ci
190686862fbSopenharmony_ci    Uri dUri("");
191686862fbSopenharmony_ci    if (uri_ == dUri) {
192686862fbSopenharmony_ci        WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, VALUE_NULL);
193686862fbSopenharmony_ci    } else {
194686862fbSopenharmony_ci        if (!parcel.WriteInt32(VALUE_OBJECT)) {
195686862fbSopenharmony_ci            return false;
196686862fbSopenharmony_ci        }
197686862fbSopenharmony_ci        if (!parcel.WriteParcelable(&uri_)) {
198686862fbSopenharmony_ci            return false;
199686862fbSopenharmony_ci        }
200686862fbSopenharmony_ci    }
201686862fbSopenharmony_ci    return true;
202686862fbSopenharmony_ci}
203686862fbSopenharmony_ci
204686862fbSopenharmony_ciDistributedOperation* DistributedOperation::Unmarshalling(Parcel& parcel)
205686862fbSopenharmony_ci{
206686862fbSopenharmony_ci    DistributedOperation* operation = new (std::nothrow) DistributedOperation();
207686862fbSopenharmony_ci    if (operation != nullptr && !operation->ReadFromParcel(parcel)) {
208686862fbSopenharmony_ci        delete operation;
209686862fbSopenharmony_ci        operation = nullptr;
210686862fbSopenharmony_ci    }
211686862fbSopenharmony_ci
212686862fbSopenharmony_ci    return operation;
213686862fbSopenharmony_ci}
214686862fbSopenharmony_ci
215686862fbSopenharmony_cibool DistributedOperation::ReadFromParcel(Parcel& parcel)
216686862fbSopenharmony_ci{
217686862fbSopenharmony_ci    std::u16string dReadString16;
218686862fbSopenharmony_ci    READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, dReadString16);
219686862fbSopenharmony_ci    abilityName_ = Str16ToStr8(dReadString16);
220686862fbSopenharmony_ci    dReadString16.clear();
221686862fbSopenharmony_ci
222686862fbSopenharmony_ci    READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, dReadString16);
223686862fbSopenharmony_ci    action_ = Str16ToStr8(dReadString16);
224686862fbSopenharmony_ci    dReadString16.clear();
225686862fbSopenharmony_ci
226686862fbSopenharmony_ci    READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, dReadString16);
227686862fbSopenharmony_ci    bundleName_ = Str16ToStr8(dReadString16);
228686862fbSopenharmony_ci    dReadString16.clear();
229686862fbSopenharmony_ci
230686862fbSopenharmony_ci    READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, dReadString16);
231686862fbSopenharmony_ci    deviceId_ = Str16ToStr8(dReadString16);
232686862fbSopenharmony_ci    dReadString16.clear();
233686862fbSopenharmony_ci
234686862fbSopenharmony_ci    READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(StringVector, parcel, &entities_);
235686862fbSopenharmony_ci    READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, flags_);
236686862fbSopenharmony_ci
237686862fbSopenharmony_ci    // uri_
238686862fbSopenharmony_ci    int32_t value = VALUE_NULL;
239686862fbSopenharmony_ci    if (!parcel.ReadInt32(value)) {
240686862fbSopenharmony_ci        return false;
241686862fbSopenharmony_ci    }
242686862fbSopenharmony_ci
243686862fbSopenharmony_ci    if (value == VALUE_OBJECT) {
244686862fbSopenharmony_ci        auto uri = parcel.ReadParcelable<Uri>();
245686862fbSopenharmony_ci        if (uri != nullptr) {
246686862fbSopenharmony_ci            uri_ = *uri;
247686862fbSopenharmony_ci            delete uri;
248686862fbSopenharmony_ci            uri = nullptr;
249686862fbSopenharmony_ci        } else {
250686862fbSopenharmony_ci            return false;
251686862fbSopenharmony_ci        }
252686862fbSopenharmony_ci    }
253686862fbSopenharmony_ci    return true;
254686862fbSopenharmony_ci}
255686862fbSopenharmony_ci
256686862fbSopenharmony_civoid DistributedOperation::SetUri(const Uri& uri)
257686862fbSopenharmony_ci{
258686862fbSopenharmony_ci    uri_ = uri;
259686862fbSopenharmony_ci}
260686862fbSopenharmony_ci
261686862fbSopenharmony_ciUri& DistributedOperation::GetUri(const Uri& uri)
262686862fbSopenharmony_ci{
263686862fbSopenharmony_ci    return uri_;
264686862fbSopenharmony_ci}
265686862fbSopenharmony_ci
266686862fbSopenharmony_civoid DistributedOperation::SetAbilityName(const std::string& abilityname)
267686862fbSopenharmony_ci{
268686862fbSopenharmony_ci    abilityName_ = abilityname;
269686862fbSopenharmony_ci}
270686862fbSopenharmony_ci
271686862fbSopenharmony_civoid DistributedOperation::SetDeviceId(const std::string& deviceid)
272686862fbSopenharmony_ci{
273686862fbSopenharmony_ci    deviceId_ = deviceid;
274686862fbSopenharmony_ci}
275686862fbSopenharmony_ci
276686862fbSopenharmony_civoid DistributedOperation::SetAction(const std::string& action)
277686862fbSopenharmony_ci{
278686862fbSopenharmony_ci    action_ = action;
279686862fbSopenharmony_ci}
280686862fbSopenharmony_ci
281686862fbSopenharmony_civoid DistributedOperation::SetEntities(const std::vector<std::string>& entities)
282686862fbSopenharmony_ci{
283686862fbSopenharmony_ci    entities_.clear();
284686862fbSopenharmony_ci    entities_ = entities;
285686862fbSopenharmony_ci}
286686862fbSopenharmony_ci
287686862fbSopenharmony_civoid DistributedOperation::SetModuleName(const std::string& moduleName)
288686862fbSopenharmony_ci{
289686862fbSopenharmony_ci    moduleName_ = moduleName;
290686862fbSopenharmony_ci}
291686862fbSopenharmony_ci} // namespace DistributedSchedule
292686862fbSopenharmony_ci} // namespace OHOS