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.
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_
11 #include "base/callback.h"
12 #include "base/files/file.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h"
18 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_observer.h"
19 #include "chrome/browser/chromeos/file_system_provider/watcher.h"
20 #include "storage/browser/fileapi/async_file_util.h"
21 #include "storage/browser/fileapi/watcher_manager.h"
33 namespace file_system_provider
{
35 class ProvidedFileSystemInfo
;
38 // Represents metadata for either a file or a directory.
39 struct EntryMetadata
{
46 base::Time modification_time
;
47 std::string mime_type
;
48 std::string thumbnail
;
51 DISALLOW_COPY_AND_ASSIGN(EntryMetadata
);
54 // Represents actions for either a file or a directory.
60 typedef std::vector
<Action
> Actions
;
62 // Mode of opening a file. Used by OpenFile().
63 enum OpenFileMode
{ OPEN_FILE_MODE_READ
, OPEN_FILE_MODE_WRITE
};
65 // Contains information about an opened file.
67 OpenedFile(const base::FilePath
& file_path
, OpenFileMode
& mode
);
71 base::FilePath file_path
;
75 // Map from a file handle to an OpenedFile struct.
76 typedef std::map
<int, OpenedFile
> OpenedFiles
;
78 // Interface for a provided file system. Acts as a proxy between providers
79 // and clients. All of the request methods return an abort callback in order to
80 // terminate it while running. They must be called on the same thread as the
81 // request methods. The cancellation callback may be null if the operation
82 // fails synchronously. It must not be called once the operation is completed
83 // with either a success or an error.
84 class ProvidedFileSystemInterface
{
86 // Extra fields to be fetched with metadata.
88 METADATA_FIELD_DEFAULT
= 0,
89 METADATA_FIELD_THUMBNAIL
= 1 << 0
92 typedef base::Callback
<void(int file_handle
, base::File::Error result
)>
95 typedef base::Callback
<
96 void(int chunk_length
, bool has_more
, base::File::Error result
)>
97 ReadChunkReceivedCallback
;
99 typedef base::Callback
<void(scoped_ptr
<EntryMetadata
> entry_metadata
,
100 base::File::Error result
)> GetMetadataCallback
;
102 typedef base::Callback
<void(const Actions
& actions
, base::File::Error result
)>
105 // Mask of fields requested from the GetMetadata() call.
106 typedef int MetadataFieldMask
;
108 virtual ~ProvidedFileSystemInterface() {}
110 // Requests unmounting of the file system. The callback is called when the
111 // request is accepted or rejected, with an error code.
112 virtual AbortCallback
RequestUnmount(
113 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
115 // Requests metadata of the passed |entry_path|. It can be either a file
116 // or a directory. All |fields| will be returned if supported. Note, that
117 // default fields are always returned.
118 virtual AbortCallback
GetMetadata(const base::FilePath
& entry_path
,
119 MetadataFieldMask fields
,
120 const GetMetadataCallback
& callback
) = 0;
122 // Requests list of actions for the passed |entry_path|. It can be either a
123 // file or a directory.
124 virtual AbortCallback
GetActions(const base::FilePath
& entry_path
,
125 const GetActionsCallback
& callback
) = 0;
127 // Executes the |action_id| action on the entry at |entry_path|.
128 virtual AbortCallback
ExecuteAction(
129 const base::FilePath
& entry_path
,
130 const std::string
& action_id
,
131 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
133 // Requests enumerating entries from the passed |directory_path|. The callback
134 // can be called multiple times until |has_more| is set to false.
135 virtual AbortCallback
ReadDirectory(
136 const base::FilePath
& directory_path
,
137 const storage::AsyncFileUtil::ReadDirectoryCallback
& callback
) = 0;
139 // Requests opening a file at |file_path|. If the file doesn't exist, then the
140 // operation will fail.
141 virtual AbortCallback
OpenFile(const base::FilePath
& file_path
,
143 const OpenFileCallback
& callback
) = 0;
145 // Requests closing a file, previously opened with OpenFile() as a file with
146 // |file_handle|. For either succes or error |callback| must be called.
147 virtual AbortCallback
CloseFile(
149 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
151 // Requests reading a file previously opened with |file_handle|. The callback
152 // can be called multiple times until |has_more| is set to false. On success
153 // it should return |length| bytes starting from |offset| in total. It can
154 // return less only in case EOF is encountered.
155 virtual AbortCallback
ReadFile(int file_handle
,
156 net::IOBuffer
* buffer
,
159 const ReadChunkReceivedCallback
& callback
) = 0;
161 // Requests creating a directory. If |recursive| is passed, then all non
162 // existing directories on the path will be created. The operation will fail
163 // if the target directory already exists.
164 virtual AbortCallback
CreateDirectory(
165 const base::FilePath
& directory_path
,
167 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
169 // Requests creating a file. If the entry already exists, then the
170 // FILE_ERROR_EXISTS error must be returned.
171 virtual AbortCallback
CreateFile(
172 const base::FilePath
& file_path
,
173 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
175 // Requests deleting a directory. If |recursive| is passed and the entry is
176 // a directory, then all contents of it (recursively) will be deleted too.
177 virtual AbortCallback
DeleteEntry(
178 const base::FilePath
& entry_path
,
180 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
182 // Requests copying an entry (recursively in case of a directory) within the
184 virtual AbortCallback
CopyEntry(
185 const base::FilePath
& source_path
,
186 const base::FilePath
& target_path
,
187 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
189 // Requests moving an entry (recursively in case of a directory) within the
191 virtual AbortCallback
MoveEntry(
192 const base::FilePath
& source_path
,
193 const base::FilePath
& target_path
,
194 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
196 // Requests truncating a file to the desired length.
197 virtual AbortCallback
Truncate(
198 const base::FilePath
& file_path
,
200 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
202 // Requests writing to a file previously opened with |file_handle|.
203 virtual AbortCallback
WriteFile(
205 net::IOBuffer
* buffer
,
208 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
210 // Requests adding a watcher on an entry. |recursive| must not be true for
211 // files. |callback| is optional, but it can't be used for persistent
213 virtual AbortCallback
AddWatcher(
215 const base::FilePath
& entry_path
,
218 const storage::AsyncFileUtil::StatusCallback
& callback
,
219 const storage::WatcherManager::NotificationCallback
&
220 notification_callback
) = 0;
222 // Requests removing a watcher, which is immediately deleted from the internal
223 // list, hence the operation is not abortable.
224 virtual void RemoveWatcher(
226 const base::FilePath
& entry_path
,
228 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
230 // Notifies about changes related to the watcher within the file system.
231 // Invoked by the file system implementation. Returns an error code via the
232 // callback if the notification arguments are malformed or the entry is not
233 // watched anymore. On success, returns base::File::FILE_OK.
234 // TODO(mtomasz): Replace [entry_path, recursive] with a watcher id.
236 const base::FilePath
& entry_path
,
238 storage::WatcherManager::ChangeType change_type
,
239 scoped_ptr
<ProvidedFileSystemObserver::Changes
> changes
,
240 const std::string
& tag
,
241 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
243 // Requests showing UI for configuring the file system by user. Once the
244 // configuration process is completed, base::File::FILE_OK or an error code is
245 // returned via the |callback|.
246 virtual void Configure(
247 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
249 // Returns a provided file system info for this file system.
250 virtual const ProvidedFileSystemInfo
& GetFileSystemInfo() const = 0;
252 // Returns a mutable list of watchers.
253 virtual Watchers
* GetWatchers() = 0;
255 // Returns a list of opened files.
256 virtual const OpenedFiles
& GetOpenedFiles() const = 0;
258 // Returns a request manager for the file system.
259 virtual RequestManager
* GetRequestManager() = 0;
261 // Adds an observer on the file system.
262 virtual void AddObserver(ProvidedFileSystemObserver
* observer
) = 0;
264 // Removes an observer.
265 virtual void RemoveObserver(ProvidedFileSystemObserver
* observer
) = 0;
267 // Returns a weak pointer to this object.
268 virtual base::WeakPtr
<ProvidedFileSystemInterface
> GetWeakPtr() = 0;
271 } // namespace file_system_provider
272 } // namespace chromeos
274 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_INTERFACE_H_