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 // Mode of opening a file. Used by OpenFile().
55 enum OpenFileMode
{ OPEN_FILE_MODE_READ
, OPEN_FILE_MODE_WRITE
};
57 // Contains information about an opened file.
59 OpenedFile(const base::FilePath
& file_path
, OpenFileMode
& mode
);
63 base::FilePath file_path
;
67 // Map from a file handle to an OpenedFile struct.
68 typedef std::map
<int, OpenedFile
> OpenedFiles
;
70 // Interface for a provided file system. Acts as a proxy between providers
71 // and clients. All of the request methods return an abort callback in order to
72 // terminate it while running. They must be called on the same thread as the
73 // request methods. The cancellation callback may be null if the operation
74 // fails synchronously. It must not be called once the operation is completed
75 // with either a success or an error.
76 class ProvidedFileSystemInterface
{
78 // Extra fields to be fetched with metadata.
80 METADATA_FIELD_DEFAULT
= 0,
81 METADATA_FIELD_THUMBNAIL
= 1 << 0
84 typedef base::Callback
<void(int file_handle
, base::File::Error result
)>
87 typedef base::Callback
<
88 void(int chunk_length
, bool has_more
, base::File::Error result
)>
89 ReadChunkReceivedCallback
;
91 typedef base::Callback
<void(scoped_ptr
<EntryMetadata
> entry_metadata
,
92 base::File::Error result
)> GetMetadataCallback
;
94 // Mask of fields requested from the GetMetadata() call.
95 typedef int MetadataFieldMask
;
97 virtual ~ProvidedFileSystemInterface() {}
99 // Requests unmounting of the file system. The callback is called when the
100 // request is accepted or rejected, with an error code.
101 virtual AbortCallback
RequestUnmount(
102 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
104 // Requests metadata of the passed |entry_path|. It can be either a file
105 // or a directory. All |fields| will be returned if supported. Note, that
106 // default fields are always returned.
107 virtual AbortCallback
GetMetadata(const base::FilePath
& entry_path
,
108 MetadataFieldMask fields
,
109 const GetMetadataCallback
& callback
) = 0;
111 // Requests enumerating entries from the passed |directory_path|. The callback
112 // can be called multiple times until |has_more| is set to false.
113 virtual AbortCallback
ReadDirectory(
114 const base::FilePath
& directory_path
,
115 const storage::AsyncFileUtil::ReadDirectoryCallback
& callback
) = 0;
117 // Requests opening a file at |file_path|. If the file doesn't exist, then the
118 // operation will fail.
119 virtual AbortCallback
OpenFile(const base::FilePath
& file_path
,
121 const OpenFileCallback
& callback
) = 0;
123 // Requests closing a file, previously opened with OpenFile() as a file with
124 // |file_handle|. For either succes or error |callback| must be called.
125 virtual AbortCallback
CloseFile(
127 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
129 // Requests reading a file previously opened with |file_handle|. The callback
130 // can be called multiple times until |has_more| is set to false. On success
131 // it should return |length| bytes starting from |offset| in total. It can
132 // return less only in case EOF is encountered.
133 virtual AbortCallback
ReadFile(int file_handle
,
134 net::IOBuffer
* buffer
,
137 const ReadChunkReceivedCallback
& callback
) = 0;
139 // Requests creating a directory. If |recursive| is passed, then all non
140 // existing directories on the path will be created. The operation will fail
141 // if the target directory already exists.
142 virtual AbortCallback
CreateDirectory(
143 const base::FilePath
& directory_path
,
145 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
147 // Requests creating a file. If the entry already exists, then the
148 // FILE_ERROR_EXISTS error must be returned.
149 virtual AbortCallback
CreateFile(
150 const base::FilePath
& file_path
,
151 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
153 // Requests deleting a directory. If |recursive| is passed and the entry is
154 // a directory, then all contents of it (recursively) will be deleted too.
155 virtual AbortCallback
DeleteEntry(
156 const base::FilePath
& entry_path
,
158 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
160 // Requests copying an entry (recursively in case of a directory) within the
162 virtual AbortCallback
CopyEntry(
163 const base::FilePath
& source_path
,
164 const base::FilePath
& target_path
,
165 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
167 // Requests moving an entry (recursively in case of a directory) within the
169 virtual AbortCallback
MoveEntry(
170 const base::FilePath
& source_path
,
171 const base::FilePath
& target_path
,
172 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
174 // Requests truncating a file to the desired length.
175 virtual AbortCallback
Truncate(
176 const base::FilePath
& file_path
,
178 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
180 // Requests writing to a file previously opened with |file_handle|.
181 virtual AbortCallback
WriteFile(
183 net::IOBuffer
* buffer
,
186 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
188 // Requests adding a watcher on an entry. |recursive| must not be true for
189 // files. |callback| is optional, but it can't be used for persistent
191 virtual AbortCallback
AddWatcher(
193 const base::FilePath
& entry_path
,
196 const storage::AsyncFileUtil::StatusCallback
& callback
,
197 const storage::WatcherManager::NotificationCallback
&
198 notification_callback
) = 0;
200 // Requests removing a watcher, which is immediately deleted from the internal
201 // list, hence the operation is not abortable.
202 virtual void RemoveWatcher(
204 const base::FilePath
& entry_path
,
206 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
208 // Notifies about changes related to the watcher within the file system.
209 // Invoked by the file system implementation. Returns an error code via the
210 // callback if the notification arguments are malformed or the entry is not
211 // watched anymore. On success, returns base::File::FILE_OK.
212 // TODO(mtomasz): Replace [entry_path, recursive] with a watcher id.
214 const base::FilePath
& entry_path
,
216 storage::WatcherManager::ChangeType change_type
,
217 scoped_ptr
<ProvidedFileSystemObserver::Changes
> changes
,
218 const std::string
& tag
,
219 const storage::AsyncFileUtil::StatusCallback
& callback
) = 0;
221 // Returns a provided file system info for this file system.
222 virtual const ProvidedFileSystemInfo
& GetFileSystemInfo() const = 0;
224 // Returns a mutable list of watchers.
225 virtual Watchers
* GetWatchers() = 0;
227 // Returns a list of opened files.
228 virtual const OpenedFiles
& GetOpenedFiles() const = 0;
230 // Returns a request manager for the file system.
231 virtual RequestManager
* GetRequestManager() = 0;
233 // Adds an observer on the file system.
234 virtual void AddObserver(ProvidedFileSystemObserver
* observer
) = 0;
236 // Removes an observer.
237 virtual void RemoveObserver(ProvidedFileSystemObserver
* observer
) = 0;
239 // Returns a weak pointer to this object.
240 virtual base::WeakPtr
<ProvidedFileSystemInterface
> GetWeakPtr() = 0;
243 } // namespace file_system_provider
244 } // namespace chromeos
246 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_INTERFACE_H_