1 /*
2  * Copyright (c) 2022 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 #ifndef FILE_ACCESS_EXTENSION_INFO_H
17 #define FILE_ACCESS_EXTENSION_INFO_H
18 
19 #include <bitset>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "iremote_object.h"
24 #include "parcel.h"
25 #include "uri.h"
26 #include "want.h"
27 
28 namespace OHOS {
29 namespace FileAccessFwk {
30 //Indicates the external root uri.
31 const std::string EXTERNAL_ROOT = "file://docs/storage/External";
32 //Properties of the Common file
33 const std::string DISPLAY_NAME = "display_name";
34 const std::string RELATIVE_PATH = "relative_path";
35 const std::string FILE_SIZE = "size";
36 const std::string DATE_MODIFIED = "date_modified";
37 const std::string DATE_ADDED = "date_added";
38 //Properties of the picture file
39 const std::string HEIGHT = "height";
40 const std::string WIDTH = "width";
41 //Properties of an image or audio file
42 const std::string DURATION = "duration";
43 
44 const std::string FILE_DATA_ATIME = "atime";
45 const std::string FILE_DATA_MTIME = "mtime";
46 /**
47  * Indicates the type of the device.
48  */
49 constexpr int32_t DEVICE_LOCAL_DISK = 1;                // Local c,d... disk
50 constexpr int32_t DEVICE_SHARED_DISK = 2;               // Multi-user shared disk
51 constexpr int32_t DEVICE_SHARED_TERMINAL = 3;           // Distributed networking terminal device
52 constexpr int32_t DEVICE_NETWORK_NEIGHBORHOODS = 4;     // Network neighbor device
53 constexpr int32_t DEVICE_EXTERNAL_MTP = 5;              // MTP device
54 constexpr int32_t DEVICE_EXTERNAL_USB = 6;              // USB device
55 constexpr int32_t DEVICE_EXTERNAL_CLOUD = 7;            // Cloud disk device
56 
57 /**
58  * Indicates the supported capabilities of the device.
59  */
60 const uint32_t DEVICE_FLAG_SUPPORTS_READ = 1;
61 const uint32_t DEVICE_FLAG_SUPPORTS_WRITE = 1 << 1;
62 
63 /**
64  * Indicates the supported capabilities of the file or directory.
65  */
66 const uint32_t DOCUMENT_FLAG_REPRESENTS_FILE = 1;
67 const uint32_t DOCUMENT_FLAG_REPRESENTS_DIR = 1 << 1;
68 const uint32_t DOCUMENT_FLAG_SUPPORTS_READ = 1 << 2;
69 const uint32_t DOCUMENT_FLAG_SUPPORTS_WRITE = 1 << 3;
70 
71 /**
72  * Indicators of the supported capabilities of the file descriptor.
73  */
74 constexpr int32_t READ = 0;
75 constexpr int32_t WRITE = 1;
76 constexpr int32_t WRITE_READ = 2;
77 
78 /**
79  * Indicates the supported Event change type.
80  */
81 constexpr int32_t ADD_EVENT = 0;
82 constexpr int32_t DELETE_EVENT = 1;
83 constexpr int32_t MOVED_TO = 2;
84 constexpr int32_t MOVED_FROM = 3;
85 constexpr int32_t MOVED_SELF = 4;
86 constexpr int32_t DEVICE_ONLINE = 5;
87 constexpr int32_t DEVICE_OFFLINE = 6;
88 
89 //Indicates the device root uris
90 const std::string DEVICES_URI = "file://docs";
91 
92 //Indicates the root uri of the External device.
93 const std::string EXTERNAL_ROOT_URI = "file://docs/storage/External";
94 /**
95  * Indicates the root uri for all device.
96  */
97 static const std::vector<std::string> DEVICE_ROOTS = {EXTERNAL_ROOT_URI};
98 
99 struct FileInfo : public virtual OHOS::Parcelable {
100 public:
101     std::string uri { "" };
102     std::string relativePath { "" };
103     std::string fileName { "" };
104     int32_t mode { 0 };
105     int64_t size { 0 };
106     int64_t mtime { 0 };
107     std::string mimeType { "" };
108 
109     FileInfo() = default;
FileInfoFileInfo110     FileInfo(std::string uri, std::string relativePath, std::string fileName, int32_t mode, std::string mimeType)
111         : uri(uri), relativePath(relativePath), fileName(fileName), mode(mode), mimeType(mimeType)
112     {}
113 
ReadFromParcelFileInfo114     bool ReadFromParcel(Parcel &parcel)
115     {
116         uri = parcel.ReadString();
117         relativePath = parcel.ReadString();
118         fileName = parcel.ReadString();
119         mode = parcel.ReadInt32();
120         size = parcel.ReadInt64();
121         mtime = parcel.ReadInt64();
122         mimeType = parcel.ReadString();
123         return true;
124     }
125 
MarshallingFileInfo126     virtual bool Marshalling(Parcel &parcel) const override
127     {
128         if (!parcel.WriteString(uri)) {
129             return false;
130         }
131         if (!parcel.WriteString(relativePath)) {
132             return false;
133         }
134         if (!parcel.WriteString(fileName)) {
135             return false;
136         }
137         if (!parcel.WriteInt32(mode)) {
138             return false;
139         }
140         if (!parcel.WriteInt64(size)) {
141             return false;
142         }
143         if (!parcel.WriteInt64(mtime)) {
144             return false;
145         }
146         if (!parcel.WriteString(mimeType)) {
147             return false;
148         }
149         return true;
150     }
151 
UnmarshallingFileInfo152     static FileInfo *Unmarshalling(Parcel &parcel)
153     {
154         FileInfo *info = new (std::nothrow) FileInfo();
155         if (info == nullptr) {
156             return nullptr;
157         }
158 
159         if (!info->ReadFromParcel(parcel)) {
160             delete info;
161             info = nullptr;
162         }
163         return info;
164     }
165 };
166 
167 struct RootInfo : public virtual OHOS::Parcelable {
168 public:
169     int32_t deviceType { 0 };
170     std::string uri { "" };
171     std::string relativePath { "" };
172     std::string displayName { "" };
173     int32_t deviceFlags { 0 };
174 
ReadFromParcelRootInfo175     bool ReadFromParcel(Parcel &parcel)
176     {
177         deviceType = parcel.ReadInt32();
178         uri = parcel.ReadString();
179         relativePath = parcel.ReadString();
180         displayName = parcel.ReadString();
181         deviceFlags = parcel.ReadInt32();
182         return true;
183     }
184 
MarshallingRootInfo185     virtual bool Marshalling(Parcel &parcel) const override
186     {
187         if (!parcel.WriteInt32(deviceType)) {
188             return false;
189         }
190         if (!parcel.WriteString(uri)) {
191             return false;
192         }
193         if (!parcel.WriteString(relativePath)) {
194             return false;
195         }
196         if (!parcel.WriteString(displayName)) {
197             return false;
198         }
199         if (!parcel.WriteInt32(deviceFlags)) {
200             return false;
201         }
202         return true;
203     }
204 
UnmarshallingRootInfo205     static RootInfo *Unmarshalling(Parcel &parcel)
206     {
207         RootInfo *info = new (std::nothrow) RootInfo();
208         if (info == nullptr) {
209             return nullptr;
210         }
211 
212         if (!info->ReadFromParcel(parcel)) {
213             delete info;
214             info = nullptr;
215         }
216         return info;
217     }
218 };
219 
220 enum ResultType {
221     STRING_TYPE = 1,
222     INT32_TYPE,
223     INT64_TYPE,
224 };
225 
226 static const std::unordered_map<std::string, ResultType> FILE_RESULT_TYPE = {
227     { DISPLAY_NAME, STRING_TYPE },
228     { RELATIVE_PATH, STRING_TYPE },
229     { FILE_SIZE, INT64_TYPE },
230     { DATE_ADDED, INT64_TYPE },
231     { DATE_MODIFIED, INT64_TYPE },
232     { WIDTH, INT32_TYPE },
233     { HEIGHT, INT32_TYPE },
234     { DURATION, INT32_TYPE },
235 };
236 
237 static const std::unordered_map<std::string, std::string> CONVERT_FILE_COLUMN = {
238     {DATE_ADDED, FILE_DATA_ATIME},
239     {DATE_MODIFIED, FILE_DATA_MTIME}
240 };
241 
242 struct Result : public virtual OHOS::Parcelable {
243 public:
244     std::string sourceUri { "" };
245     std::string destUri { "" };
246     int32_t errCode { 0 };
247     std::string errMsg { "" };
248 
249     Result() = default;
ResultResult250     Result(std::string sourceUri, std::string destUri, int32_t errCode, std::string errMsg)
251         : sourceUri(sourceUri), destUri(destUri), errCode(errCode),  errMsg(errMsg)
252     {}
253 
ReadFromParcelResult254     bool ReadFromParcel(Parcel &parcel)
255     {
256         sourceUri = parcel.ReadString();
257         destUri = parcel.ReadString();
258         errCode = parcel.ReadInt32();
259         errMsg = parcel.ReadString();
260         return true;
261     }
262 
MarshallingResult263     virtual bool Marshalling(Parcel &parcel) const override
264     {
265         if (!parcel.WriteString(sourceUri)) {
266             return false;
267         }
268         if (!parcel.WriteString(destUri)) {
269             return false;
270         }
271         if (!parcel.WriteInt32(errCode)) {
272             return false;
273         }
274         if (!parcel.WriteString(errMsg)) {
275             return false;
276         }
277         return true;
278     }
279 
UnmarshallingResult280     static Result *Unmarshalling(Parcel &parcel)
281     {
282         Result *result = new (std::nothrow)Result();
283         if (result == nullptr) {
284             return nullptr;
285         }
286 
287         if (!result->ReadFromParcel(parcel)) {
288             delete result;
289             result = nullptr;
290         }
291         return result;
292     }
293 };
294 
295 class MessageParcelable {
296 public:
297     MessageParcelable() = default;
298     virtual ~MessageParcelable() = default;
299     virtual bool WriteToParcel(MessageParcel& parcel) const = 0;
300     virtual bool ReadFromParcel(MessageParcel& parcel) = 0;
301 };
302 
303 struct ConnectExtensionInfo : public MessageParcelable {
304 public:
305     AAFwk::Want want = {};
306     sptr<IRemoteObject> token = nullptr;
307 
308     ConnectExtensionInfo() = default;
ConnectExtensionInfoConnectExtensionInfo309     ConnectExtensionInfo(AAFwk::Want want, sptr<IRemoteObject> token) : want(want), token(token) {}
310 
WriteToParcelConnectExtensionInfo311     bool WriteToParcel(MessageParcel &parcel) const override
312     {
313         if (!parcel.WriteParcelable(&want)) {
314             return false;
315         }
316         if (!parcel.WriteRemoteObject(token)) {
317             return false;
318         }
319         return true;
320     }
321 
ReadFromParcelConnectExtensionInfo322     bool ReadFromParcel(MessageParcel &parcel) override
323     {
324         std::shared_ptr<AAFwk::Want> wantPtr(parcel.ReadParcelable<AAFwk::Want>());
325         token = parcel.ReadRemoteObject();
326         if (wantPtr == nullptr || token == nullptr) {
327             return false;
328         }
329         want = AAFwk::Want(*wantPtr);
330         return true;
331     }
332 };
333 } // namespace FileAccessFwk
334 } // namespace OHOS
335 #endif // FILE_ACCESS_EXTENSION_INFO_H