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 <sstream>
17 #include "cloud_sync_common.h"
18
19 #include "utils_log.h"
20
21 namespace OHOS::FileManagement::CloudSync {
22 namespace {
23 constexpr uint32_t MAX_MAP_SIZE = 1024;
24 }
Marshalling(Parcel & parcel) const25 bool SwitchDataObj::Marshalling(Parcel &parcel) const
26 {
27 if (!parcel.WriteUint32(switchData.size())) {
28 LOGE("failed to write switch data size");
29 return false;
30 }
31 for (const auto& it : switchData) {
32 if (!parcel.WriteString(it.first)) {
33 LOGE("failed to write key");
34 return false;
35 }
36 if (!parcel.WriteBool(it.second)) {
37 LOGE("failed to write value");
38 return false;
39 }
40 }
41 return true;
42 }
43
Marshalling(Parcel & parcel) const44 bool DownloadProgressObj::Marshalling(Parcel &parcel) const
45 {
46 if (!parcel.WriteString(path)) {
47 LOGE("failed to write download path");
48 return false;
49 }
50 if (!parcel.WriteInt32(state)) {
51 LOGE("failed to write download state");
52 return false;
53 }
54 if (!parcel.WriteInt64(downloadedSize)) {
55 LOGE("failed to write downloadedSize");
56 return false;
57 }
58 if (!parcel.WriteInt64(totalSize)) {
59 LOGE("failed to write totalSize");
60 return false;
61 }
62 return true;
63 }
64
Marshalling(Parcel & parcel) const65 bool CleanOptions::Marshalling(Parcel &parcel) const
66 {
67 if (!parcel.WriteUint32(appActionsData.size())) {
68 LOGE("failed to write appActions data size");
69 return false;
70 }
71 for (const auto& it : appActionsData) {
72 if (!parcel.WriteString(it.first)) {
73 LOGE("failed to write key");
74 return false;
75 }
76 if (!parcel.WriteInt32(it.second)) {
77 LOGE("failed to write value");
78 return false;
79 }
80 }
81 return true;
82 }
83
Unmarshalling(Parcel & parcel)84 SwitchDataObj *SwitchDataObj::Unmarshalling(Parcel &parcel)
85 {
86 SwitchDataObj *info = new (std::nothrow) SwitchDataObj();
87 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
88 LOGW("read from parcel failed");
89 delete info;
90 info = nullptr;
91 }
92 return info;
93 }
94
Unmarshalling(Parcel & parcel)95 DownloadProgressObj *DownloadProgressObj::Unmarshalling(Parcel &parcel)
96 {
97 DownloadProgressObj *info = new (std::nothrow) DownloadProgressObj();
98 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
99 LOGW("read from parcel failed");
100 delete info;
101 info = nullptr;
102 }
103 return info;
104 }
105
Unmarshalling(Parcel & parcel)106 CleanOptions *CleanOptions::Unmarshalling(Parcel &parcel)
107 {
108 CleanOptions *info = new (std::nothrow) CleanOptions();
109 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
110 LOGW("read from parcel failed");
111 delete info;
112 info = nullptr;
113 }
114 return info;
115 }
116
ReadFromParcel(Parcel & parcel)117 bool SwitchDataObj::ReadFromParcel(Parcel &parcel)
118 {
119 switchData.clear();
120 uint32_t size = 0;
121 if (!parcel.ReadUint32(size)) {
122 LOGE("fail to read switch data size");
123 return false;
124 }
125 if (size > MAX_MAP_SIZE) {
126 LOGE("switch data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
127 return false;
128 }
129 for (uint32_t i = 0; i < size; ++i) {
130 std::string key;
131 if (!parcel.ReadString(key)) {
132 LOGE("fail to read switch data key");
133 return false;
134 }
135 bool value = false;
136 if (!parcel.ReadBool(value)) {
137 LOGE("fail to read switch data value");
138 return false;
139 }
140 switchData.emplace(key, value);
141 }
142 return true;
143 }
144
ReadFromParcel(Parcel & parcel)145 bool CleanOptions::ReadFromParcel(Parcel &parcel)
146 {
147 appActionsData.clear();
148 uint32_t size = 0;
149 if (!parcel.ReadUint32(size)) {
150 LOGE("fail to read appActions data size");
151 return false;
152 }
153 if (size > MAX_MAP_SIZE) {
154 LOGE("appActions data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
155 return false;
156 }
157 for (uint32_t i = 0; i < size; ++i) {
158 std::string key;
159 if (!parcel.ReadString(key)) {
160 LOGE("fail to read appActions data key");
161 return false;
162 }
163 int value = 0;
164 if (!parcel.ReadInt32(value)) {
165 LOGE("fail to read appActions data value");
166 return false;
167 }
168 appActionsData.emplace(key, value);
169 }
170 return true;
171 }
172
ReadFromParcel(Parcel & parcel)173 bool DownloadProgressObj::ReadFromParcel(Parcel &parcel)
174 {
175 if (!parcel.ReadString(path)) {
176 LOGE("failed to write download path");
177 return false;
178 }
179 int32_t tempState = 0;
180 if (!parcel.ReadInt32(tempState)) {
181 LOGE("failed to write download state");
182 return false;
183 }
184 state = static_cast<Status>(tempState);
185 if (!parcel.ReadInt64(downloadedSize)) {
186 LOGE("failed to write downloadedSize");
187 return false;
188 }
189 if (!parcel.ReadInt64(totalSize)) {
190 LOGE("failed to write totalSize");
191 return false;
192 }
193 return true;
194 }
195
to_string()196 std::string DownloadProgressObj::to_string()
197 {
198 std::stringstream ss;
199 ss << "DownloadProgressObj [path: " << path;
200 ss << " state: " << state;
201 ss << " downloaded: " << downloadedSize;
202 ss << " total: " << totalSize << "]";
203
204 return ss.str();
205 }
206
ReadFromParcel(Parcel & parcel)207 bool AssetInfoObj::ReadFromParcel(Parcel &parcel)
208 {
209 parcel.ReadString(uri);
210 parcel.ReadString(recordType);
211 parcel.ReadString(recordId);
212 parcel.ReadString(fieldKey);
213 parcel.ReadString(assetName);
214 return true;
215 }
216
Marshalling(Parcel & parcel) const217 bool AssetInfoObj::Marshalling(Parcel &parcel) const
218 {
219 parcel.WriteString(uri);
220 parcel.WriteString(recordType);
221 parcel.WriteString(recordId);
222 parcel.WriteString(fieldKey);
223 parcel.WriteString(assetName);
224 return true;
225 }
226
Unmarshalling(Parcel & parcel)227 AssetInfoObj *AssetInfoObj::Unmarshalling(Parcel &parcel)
228 {
229 AssetInfoObj *info = new (std::nothrow) AssetInfoObj();
230 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
231 LOGW("read from parcel failed");
232 delete info;
233 info = nullptr;
234 }
235 return info;
236 }
237 } // namespace OHOS::FileManagement::CloudSync
238