Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / provided_file_system_interface.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_INTERFACE_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_INTERFACE_H_
7
8 #include <string>
9
10 #include "base/callback.h"
11 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "storage/browser/fileapi/async_file_util.h"
16
17 class EventRouter;
18
19 namespace base {
20 class Time;
21 }  // namespace base
22
23 namespace net {
24 class IOBuffer;
25 }  // namespace net
26
27 namespace chromeos {
28 namespace file_system_provider {
29
30 class ProvidedFileSystemInfo;
31 class RequestManager;
32
33 // Represents metadata for either a file or a directory. Returned by GetMetadata
34 // method in ProvidedFileSystemInterface.
35 struct EntryMetadata {
36   EntryMetadata();
37   ~EntryMetadata();
38
39   bool is_directory;
40   std::string name;
41   int64 size;
42   base::Time modification_time;
43   std::string mime_type;
44   std::string thumbnail;
45
46  private:
47   DISALLOW_COPY_AND_ASSIGN(EntryMetadata);
48 };
49
50 // Interface for a provided file system. Acts as a proxy between providers
51 // and clients. All of the request methods return an abort callback in order to
52 // terminate it while running. They must be called on the same thread as the
53 // request methods. The cancellation callback may be null if the operation
54 // fails synchronously.
55 class ProvidedFileSystemInterface {
56  public:
57   // Mode of opening a file. Used by OpenFile().
58   enum OpenFileMode { OPEN_FILE_MODE_READ, OPEN_FILE_MODE_WRITE };
59
60   // Extra fields to be fetched with metadata.
61   enum MetadataField {
62     METADATA_FIELD_DEFAULT = 0,
63     METADATA_FIELD_THUMBNAIL = 1 << 0
64   };
65
66   typedef base::Callback<void(int file_handle, base::File::Error result)>
67       OpenFileCallback;
68
69   typedef base::Callback<
70       void(int chunk_length, bool has_more, base::File::Error result)>
71       ReadChunkReceivedCallback;
72
73   typedef base::Callback<void(scoped_ptr<EntryMetadata> entry_metadata,
74                               base::File::Error result)> GetMetadataCallback;
75
76   typedef base::Callback<void(
77       const storage::AsyncFileUtil::StatusCallback& callback)> AbortCallback;
78
79   // Mask of fields requested from the GetMetadata() call.
80   typedef int MetadataFieldMask;
81
82   virtual ~ProvidedFileSystemInterface() {}
83
84   // Requests unmounting of the file system. The callback is called when the
85   // request is accepted or rejected, with an error code.
86   virtual AbortCallback RequestUnmount(
87       const storage::AsyncFileUtil::StatusCallback& callback) = 0;
88
89   // Requests metadata of the passed |entry_path|. It can be either a file
90   // or a directory. All |fields| will be returned if supported. Note, that
91   // default fields are always returned.
92   virtual AbortCallback GetMetadata(const base::FilePath& entry_path,
93                                     MetadataFieldMask fields,
94                                     const GetMetadataCallback& callback) = 0;
95
96   // Requests enumerating entries from the passed |directory_path|. The callback
97   // can be called multiple times until |has_more| is set to false.
98   virtual AbortCallback ReadDirectory(
99       const base::FilePath& directory_path,
100       const storage::AsyncFileUtil::ReadDirectoryCallback& callback) = 0;
101
102   // Requests opening a file at |file_path|. If the file doesn't exist, then the
103   // operation will fail.
104   virtual AbortCallback OpenFile(const base::FilePath& file_path,
105                                  OpenFileMode mode,
106                                  const OpenFileCallback& callback) = 0;
107
108   // Requests closing a file, previously opened with OpenFile() as a file with
109   // |file_handle|. For either succes or error |callback| must be called.
110   virtual AbortCallback CloseFile(
111       int file_handle,
112       const storage::AsyncFileUtil::StatusCallback& callback) = 0;
113
114   // Requests reading a file previously opened with |file_handle|. The callback
115   // can be called multiple times until |has_more| is set to false. On success
116   // it should return |length| bytes starting from |offset| in total. It can
117   // return less only in case EOF is encountered.
118   virtual AbortCallback ReadFile(int file_handle,
119                                  net::IOBuffer* buffer,
120                                  int64 offset,
121                                  int length,
122                                  const ReadChunkReceivedCallback& callback) = 0;
123
124   // Requests creating a directory. If |recursive| is passed, then all non
125   // existing directories on the path will be created. The operation will fail
126   // if the target directory already exists.
127   virtual AbortCallback CreateDirectory(
128       const base::FilePath& directory_path,
129       bool recursive,
130       const storage::AsyncFileUtil::StatusCallback& callback) = 0;
131
132   // Requests creating a file. If the entry already exists, then the
133   // FILE_ERROR_EXISTS error must be returned.
134   virtual AbortCallback CreateFile(
135       const base::FilePath& file_path,
136       const storage::AsyncFileUtil::StatusCallback& callback) = 0;
137
138   // Requests deleting a directory. If |recursive| is passed and the entry is
139   // a directory, then all contents of it (recursively) will be deleted too.
140   virtual AbortCallback DeleteEntry(
141       const base::FilePath& entry_path,
142       bool recursive,
143       const storage::AsyncFileUtil::StatusCallback& callback) = 0;
144
145   // Requests copying an entry (recursively in case of a directory) within the
146   // same file system.
147   virtual AbortCallback CopyEntry(
148       const base::FilePath& source_path,
149       const base::FilePath& target_path,
150       const storage::AsyncFileUtil::StatusCallback& callback) = 0;
151
152   // Requests moving an entry (recursively in case of a directory) within the
153   // same file system.
154   virtual AbortCallback MoveEntry(
155       const base::FilePath& source_path,
156       const base::FilePath& target_path,
157       const storage::AsyncFileUtil::StatusCallback& callback) = 0;
158
159   // Requests truncating a file to the desired length.
160   virtual AbortCallback Truncate(
161       const base::FilePath& file_path,
162       int64 length,
163       const storage::AsyncFileUtil::StatusCallback& callback) = 0;
164
165   // Requests writing to a file previously opened with |file_handle|.
166   virtual AbortCallback WriteFile(
167       int file_handle,
168       net::IOBuffer* buffer,
169       int64 offset,
170       int length,
171       const storage::AsyncFileUtil::StatusCallback& callback) = 0;
172
173   // Returns a provided file system info for this file system.
174   virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const = 0;
175
176   // Returns a request manager for the file system.
177   virtual RequestManager* GetRequestManager() = 0;
178
179   // Returns a weak pointer to this object.
180   virtual base::WeakPtr<ProvidedFileSystemInterface> GetWeakPtr() = 0;
181 };
182
183 }  // namespace file_system_provider
184 }  // namespace chromeos
185
186 #endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_INTERFACE_H_