1 /*
2  * Copyright (c) 2022-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 "continuation_extra_params.h"
17 
18 #include <iosfwd>
19 #include <new>
20 
21 #include "base/continuationmgr_log.h"
22 
23 namespace OHOS {
24 namespace DistributedSchedule {
25 namespace {
26 const std::string TAG = "ContinuationExtraParams";
27 }
28 
ReadFromParcel(Parcel & parcel)29 bool ContinuationExtraParams::ReadFromParcel(Parcel &parcel)
30 {
31     std::vector<std::u16string> deviceTypeVec;
32     if (!parcel.ReadString16Vector(&deviceTypeVec)) {
33         HILOGE("read device type failed.");
34         return false;
35     }
36     SetDeviceType(ContinationManagerUtils::Str16VecToStr8Vec(deviceTypeVec));
37     SetTargetBundle(Str16ToStr8(parcel.ReadString16()));
38     SetDescription(Str16ToStr8(parcel.ReadString16()));
39     SetFilter(Str16ToStr8(parcel.ReadString16()));
40     SetContinuationMode(static_cast<ContinuationMode>(parcel.ReadInt32()));
41     SetAuthInfo(Str16ToStr8(parcel.ReadString16()));
42     return true;
43 }
44 
Unmarshalling(Parcel & parcel)45 ContinuationExtraParams *ContinuationExtraParams::Unmarshalling(Parcel &parcel)
46 {
47     ContinuationExtraParams *continuationExtraParams = new (std::nothrow) ContinuationExtraParams();
48     if (continuationExtraParams == nullptr) {
49         return nullptr;
50     }
51 
52     if (!continuationExtraParams->ReadFromParcel(parcel)) {
53         delete continuationExtraParams;
54         continuationExtraParams = nullptr;
55     }
56 
57     return continuationExtraParams;
58 }
59 
Marshalling(Parcel & parcel) const60 bool ContinuationExtraParams::Marshalling(Parcel &parcel) const
61 {
62     parcel.WriteString16Vector(ContinationManagerUtils::Str8VecToStr16Vec(GetDeviceType()));
63     parcel.WriteString16(Str8ToStr16(GetTargetBundle()));
64     parcel.WriteString16(Str8ToStr16(GetDescription()));
65     parcel.WriteString16(Str8ToStr16(GetFilter()));
66     parcel.WriteInt32(static_cast<int32_t>(GetContinuationMode()));
67     parcel.WriteString16(Str8ToStr16(GetAuthInfo()));
68     return true;
69 }
70 
SetDeviceType(std::vector<std::string> deviceTypeVec)71 void ContinuationExtraParams::SetDeviceType(std::vector<std::string> deviceTypeVec)
72 {
73     deviceTypeVec_ = deviceTypeVec;
74 }
75 
GetDeviceType() const76 std::vector<std::string> ContinuationExtraParams::GetDeviceType() const
77 {
78     return deviceTypeVec_;
79 }
80 
SetTargetBundle(std::string targetBundle)81 void ContinuationExtraParams::SetTargetBundle(std::string targetBundle)
82 {
83     targetBundle_ = targetBundle;
84 }
85 
GetTargetBundle() const86 std::string ContinuationExtraParams::GetTargetBundle() const
87 {
88     return targetBundle_;
89 }
90 
SetDescription(std::string description)91 void ContinuationExtraParams::SetDescription(std::string description)
92 {
93     description_ = description;
94 }
95 
GetDescription() const96 std::string ContinuationExtraParams::GetDescription() const
97 {
98     return description_;
99 }
100 
SetFilter(std::string filter)101 void ContinuationExtraParams::SetFilter(std::string filter)
102 {
103     filter_ = filter;
104 }
105 
GetFilter() const106 std::string ContinuationExtraParams::GetFilter() const
107 {
108     return filter_;
109 }
110 
SetContinuationMode(ContinuationMode continuationMode)111 void ContinuationExtraParams::SetContinuationMode(ContinuationMode continuationMode)
112 {
113     continuationMode_ = continuationMode;
114 }
115 
GetContinuationMode() const116 ContinuationMode ContinuationExtraParams::GetContinuationMode() const
117 {
118     return continuationMode_;
119 }
120 
SetAuthInfo(std::string authInfo)121 void ContinuationExtraParams::SetAuthInfo(std::string authInfo)
122 {
123     authInfo_ = authInfo;
124 }
125 
GetAuthInfo() const126 std::string ContinuationExtraParams::GetAuthInfo() const
127 {
128     return authInfo_;
129 }
130 
Str8VecToStr16Vec(const std::vector<std::string> & input)131 std::vector<std::u16string> ContinationManagerUtils::Str8VecToStr16Vec(const std::vector<std::string>& input)
132 {
133     std::vector<std::u16string> output;
134     for (auto iter = input.begin(); iter != input.end(); iter++) {
135         output.emplace_back(Str8ToStr16(*iter));
136     }
137     return output;
138 }
139 
Str16VecToStr8Vec(const std::vector<std::u16string> & input)140 std::vector<std::string> ContinationManagerUtils::Str16VecToStr8Vec(const std::vector<std::u16string>& input)
141 {
142     std::vector<std::string> output;
143     for (auto iter = input.begin(); iter != input.end(); iter++) {
144         output.emplace_back(Str16ToStr8(*iter));
145     }
146     return output;
147 }
148 }  // namespace DistributedSchedule
149 }  // namespace OHOS