• 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 <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     if (!parcel.WriteInt32(downloadErrorType)) {
63         LOGE("failed to write downloadErrorType");
64         return false;
65     }
66     return true;
67 }
68 
Marshalling(Parcel & parcel) const69 bool CleanOptions::Marshalling(Parcel &parcel) const
70 {
71     if (!parcel.WriteUint32(appActionsData.size())) {
72         LOGE("failed to write appActions data size");
73         return false;
74     }
75     for (const auto& it : appActionsData) {
76         if (!parcel.WriteString(it.first)) {
77             LOGE("failed to write key");
78             return false;
79         }
80         if (!parcel.WriteInt32(it.second)) {
81             LOGE("failed to write value");
82             return false;
83         }
84     }
85     return true;
86 }
87 
Unmarshalling(Parcel & parcel)88 SwitchDataObj *SwitchDataObj::Unmarshalling(Parcel &parcel)
89 {
90     SwitchDataObj *info = new (std::nothrow) SwitchDataObj();
91     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
92         LOGW("read from parcel failed");
93         delete info;
94         info = nullptr;
95     }
96     return info;
97 }
98 
Unmarshalling(Parcel & parcel)99 DownloadProgressObj *DownloadProgressObj::Unmarshalling(Parcel &parcel)
100 {
101     DownloadProgressObj *info = new (std::nothrow) DownloadProgressObj();
102     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
103         LOGW("read from parcel failed");
104         delete info;
105         info = nullptr;
106     }
107     return info;
108 }
109 
Unmarshalling(Parcel & parcel)110 CleanOptions *CleanOptions::Unmarshalling(Parcel &parcel)
111 {
112     CleanOptions *info = new (std::nothrow) CleanOptions();
113     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
114         LOGW("read from parcel failed");
115         delete info;
116         info = nullptr;
117     }
118     return info;
119 }
120 
ReadFromParcel(Parcel & parcel)121 bool SwitchDataObj::ReadFromParcel(Parcel &parcel)
122 {
123     switchData.clear();
124     uint32_t size = 0;
125     if (!parcel.ReadUint32(size)) {
126         LOGE("fail to read switch data size");
127         return false;
128     }
129     if (size > MAX_MAP_SIZE) {
130         LOGE("switch data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
131         return false;
132     }
133     for (uint32_t i = 0; i < size; ++i) {
134         std::string key;
135         if (!parcel.ReadString(key)) {
136             LOGE("fail to read switch data key");
137             return false;
138         }
139         bool value = false;
140         if (!parcel.ReadBool(value)) {
141             LOGE("fail to read switch data value");
142             return false;
143         }
144         switchData.emplace(key, value);
145     }
146     return true;
147 }
148 
ReadFromParcel(Parcel & parcel)149 bool CleanOptions::ReadFromParcel(Parcel &parcel)
150 {
151     appActionsData.clear();
152     uint32_t size = 0;
153     if (!parcel.ReadUint32(size)) {
154         LOGE("fail to read appActions data size");
155         return false;
156     }
157     if (size > MAX_MAP_SIZE) {
158         LOGE("appActions data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
159         return false;
160     }
161     for (uint32_t i = 0; i < size; ++i) {
162         std::string key;
163         if (!parcel.ReadString(key)) {
164             LOGE("fail to read appActions data key");
165             return false;
166         }
167         int value = 0;
168         if (!parcel.ReadInt32(value)) {
169             LOGE("fail to read appActions data value");
170             return false;
171         }
172         appActionsData.emplace(key, value);
173     }
174     return true;
175 }
176 
ReadFromParcel(Parcel & parcel)177 bool DownloadProgressObj::ReadFromParcel(Parcel &parcel)
178 {
179     if (!parcel.ReadString(path)) {
180         LOGE("failed to write download path");
181         return false;
182     }
183     int32_t tempState = 0;
184     if (!parcel.ReadInt32(tempState)) {
185         LOGE("failed to write download state");
186         return false;
187     }
188     state = static_cast<Status>(tempState);
189     if (!parcel.ReadInt64(downloadedSize)) {
190         LOGE("failed to write downloadedSize");
191         return false;
192     }
193     if (!parcel.ReadInt64(totalSize)) {
194         LOGE("failed to write totalSize");
195         return false;
196     }
197     if (!parcel.ReadInt32(downloadErrorType)) {
198         LOGE("failed to write downloadErrorType");
199         return false;
200     }
201     return true;
202 }
203 
to_string()204 std::string DownloadProgressObj::to_string()
205 {
206     std::stringstream ss;
207     ss << "DownloadProgressObj [path: " << path;
208     ss << " state: " << state;
209     ss << " downloaded: " << downloadedSize;
210     ss << " total: " << totalSize;
211     ss << " downloadErrorType: " << downloadErrorType << "]";
212 
213     return ss.str();
214 }
215 
ReadFromParcel(Parcel & parcel)216 bool AssetInfoObj::ReadFromParcel(Parcel &parcel)
217 {
218     parcel.ReadString(uri);
219     parcel.ReadString(recordType);
220     parcel.ReadString(recordId);
221     parcel.ReadString(fieldKey);
222     parcel.ReadString(assetName);
223     return true;
224 }
225 
Marshalling(Parcel & parcel) const226 bool AssetInfoObj::Marshalling(Parcel &parcel) const
227 {
228     parcel.WriteString(uri);
229     parcel.WriteString(recordType);
230     parcel.WriteString(recordId);
231     parcel.WriteString(fieldKey);
232     parcel.WriteString(assetName);
233     return true;
234 }
235 
Unmarshalling(Parcel & parcel)236 AssetInfoObj *AssetInfoObj::Unmarshalling(Parcel &parcel)
237 {
238     AssetInfoObj *info = new (std::nothrow) AssetInfoObj();
239     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
240         LOGW("read from parcel failed");
241         delete info;
242         info = nullptr;
243     }
244     return info;
245 }
246 } // namespace OHOS::FileManagement::CloudSync
247