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_
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"
28 namespace file_system_provider {
30 class ProvidedFileSystemInfo;
33 // Represents metadata for either a file or a directory. Returned by GetMetadata
34 // method in ProvidedFileSystemInterface.
35 struct EntryMetadata {
42 base::Time modification_time;
43 std::string mime_type;
44 std::string thumbnail;
47 DISALLOW_COPY_AND_ASSIGN(EntryMetadata);
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 {
57 // Mode of opening a file. Used by OpenFile().
58 enum OpenFileMode { OPEN_FILE_MODE_READ, OPEN_FILE_MODE_WRITE };
60 // Extra fields to be fetched with metadata.
62 METADATA_FIELD_DEFAULT = 0,
63 METADATA_FIELD_THUMBNAIL = 1 << 0
66 typedef base::Callback<void(int file_handle, base::File::Error result)>
69 typedef base::Callback<
70 void(int chunk_length, bool has_more, base::File::Error result)>
71 ReadChunkReceivedCallback;
73 typedef base::Callback<void(scoped_ptr<EntryMetadata> entry_metadata,
74 base::File::Error result)> GetMetadataCallback;
76 typedef base::Callback<void(
77 const storage::AsyncFileUtil::StatusCallback& callback)> AbortCallback;
79 // Mask of fields requested from the GetMetadata() call.
80 typedef int MetadataFieldMask;
82 virtual ~ProvidedFileSystemInterface() {}
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;
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;
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;
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,
106 const OpenFileCallback& callback) = 0;
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(
112 const storage::AsyncFileUtil::StatusCallback& callback) = 0;
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,
122 const ReadChunkReceivedCallback& callback) = 0;
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,
130 const storage::AsyncFileUtil::StatusCallback& callback) = 0;
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;
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,
143 const storage::AsyncFileUtil::StatusCallback& callback) = 0;
145 // Requests copying an entry (recursively in case of a directory) within the
147 virtual AbortCallback CopyEntry(
148 const base::FilePath& source_path,
149 const base::FilePath& target_path,
150 const storage::AsyncFileUtil::StatusCallback& callback) = 0;
152 // Requests moving an entry (recursively in case of a directory) within the
154 virtual AbortCallback MoveEntry(
155 const base::FilePath& source_path,
156 const base::FilePath& target_path,
157 const storage::AsyncFileUtil::StatusCallback& callback) = 0;
159 // Requests truncating a file to the desired length.
160 virtual AbortCallback Truncate(
161 const base::FilePath& file_path,
163 const storage::AsyncFileUtil::StatusCallback& callback) = 0;
165 // Requests writing to a file previously opened with |file_handle|.
166 virtual AbortCallback WriteFile(
168 net::IOBuffer* buffer,
171 const storage::AsyncFileUtil::StatusCallback& callback) = 0;
173 // Returns a provided file system info for this file system.
174 virtual const ProvidedFileSystemInfo& GetFileSystemInfo() const = 0;
176 // Returns a request manager for the file system.
177 virtual RequestManager* GetRequestManager() = 0;
179 // Returns a weak pointer to this object.
180 virtual base::WeakPtr<ProvidedFileSystemInterface> GetWeakPtr() = 0;
183 } // namespace file_system_provider
184 } // namespace chromeos
186 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_PROVIDED_FILE_SYSTEM_INTERFACE_H_