Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / storage / browser / fileapi / file_system_backend.h
blob58333b3d9a80b8b72673663cb53aa85c24b16352
1 // Copyright (c) 2012 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 STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_BACKEND_H_
6 #define STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_BACKEND_H_
8 #include <limits>
9 #include <string>
10 #include <vector>
12 #include "base/callback_forward.h"
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "storage/browser/fileapi/file_permission_policy.h"
17 #include "storage/browser/fileapi/open_file_system_mode.h"
18 #include "storage/browser/fileapi/task_runner_bound_observer_list.h"
19 #include "storage/browser/storage_browser_export.h"
20 #include "storage/common/fileapi/file_system_types.h"
22 class GURL;
24 namespace storage {
26 class AsyncFileUtil;
27 class CopyOrMoveFileValidatorFactory;
28 class FileSystemURL;
29 class FileStreamReader;
30 class FileStreamWriter;
31 class FileSystemContext;
32 class FileSystemFileUtil;
33 class FileSystemOperation;
34 class FileSystemQuotaUtil;
35 class WatcherManager;
37 // Callback to take GURL.
38 typedef base::Callback<void(const GURL& url)> URLCallback;
40 // Maximum numer of bytes to be read by FileStreamReader classes. Used in
41 // FileSystemBackend::CreateFileStreamReader(), when it's not known how many
42 // bytes will be fetched in total.
43 const int64 kMaximumLength = std::numeric_limits<int64>::max();
45 // An interface for defining a file system backend.
47 // NOTE: when you implement a new FileSystemBackend for your own
48 // FileSystem module, please contact to kinuko@chromium.org.
50 class STORAGE_EXPORT FileSystemBackend {
51 public:
52 // Callback for InitializeFileSystem.
53 typedef base::Callback<void(const GURL& root_url,
54 const std::string& name,
55 base::File::Error error)>
56 OpenFileSystemCallback;
57 virtual ~FileSystemBackend() {}
59 // Returns true if this filesystem backend can handle |type|.
60 // One filesystem backend may be able to handle multiple filesystem types.
61 virtual bool CanHandleType(FileSystemType type) const = 0;
63 // This method is called right after the backend is registered in the
64 // FileSystemContext and before any other methods are called. Each backend can
65 // do additional initialization which depends on FileSystemContext here.
66 virtual void Initialize(FileSystemContext* context) = 0;
68 // Resolves the filesystem root URL and the name for the given |url|.
69 // This verifies if it is allowed to request (or create) the filesystem and if
70 // it can access (or create) the root directory.
71 // If |mode| is CREATE_IF_NONEXISTENT calling this may also create the root
72 // directory (and/or related database entries etc) for the filesystem if it
73 // doesn't exist.
74 virtual void ResolveURL(const FileSystemURL& url,
75 OpenFileSystemMode mode,
76 const OpenFileSystemCallback& callback) = 0;
78 // Returns the specialized AsyncFileUtil for this backend.
79 virtual AsyncFileUtil* GetAsyncFileUtil(FileSystemType type) = 0;
81 // Returns the specialized WatcherManager for this backend.
82 virtual WatcherManager* GetWatcherManager(FileSystemType type) = 0;
84 // Returns the specialized CopyOrMoveFileValidatorFactory for this backend
85 // and |type|. If |error_code| is File::FILE_OK and the result is NULL,
86 // then no validator is required.
87 virtual CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory(
88 FileSystemType type, base::File::Error* error_code) = 0;
90 // Returns a new instance of the specialized FileSystemOperation for this
91 // backend based on the given triplet of |origin_url|, |file_system_type|
92 // and |virtual_path|. On failure to create a file system operation, set
93 // |error_code| correspondingly.
94 // This method is usually dispatched by
95 // FileSystemContext::CreateFileSystemOperation.
96 virtual FileSystemOperation* CreateFileSystemOperation(
97 const FileSystemURL& url,
98 FileSystemContext* context,
99 base::File::Error* error_code) const = 0;
101 // Returns true if Blobs accessing |url| should use FileStreamReader.
102 // If false, Blobs are accessed using a snapshot file by calling
103 // AsyncFileUtil::CreateSnapshotFile.
104 virtual bool SupportsStreaming(const FileSystemURL& url) const = 0;
106 // Returns true if specified |type| of filesystem can handle Copy()
107 // of the files in the same file system instead of streaming
108 // read/write implementation.
109 virtual bool HasInplaceCopyImplementation(FileSystemType type) const = 0;
111 // Creates a new file stream reader for a given filesystem URL |url| with an
112 // offset |offset|. |expected_modification_time| specifies the expected last
113 // modification if the value is non-null, the reader will check the underlying
114 // file's actual modification time to see if the file has been modified, and
115 // if it does any succeeding read operations should fail with
116 // ERR_UPLOAD_FILE_CHANGED error.
117 // This method itself does *not* check if the given path exists and is a
118 // regular file. At most |max_bytes_to_read| can be fetched from the file
119 // stream reader.
120 virtual scoped_ptr<storage::FileStreamReader> CreateFileStreamReader(
121 const FileSystemURL& url,
122 int64 offset,
123 int64 max_bytes_to_read,
124 const base::Time& expected_modification_time,
125 FileSystemContext* context) const = 0;
127 // Creates a new file stream writer for a given filesystem URL |url| with an
128 // offset |offset|.
129 // This method itself does *not* check if the given path exists and is a
130 // regular file.
131 virtual scoped_ptr<FileStreamWriter> CreateFileStreamWriter(
132 const FileSystemURL& url,
133 int64 offset,
134 FileSystemContext* context) const = 0;
136 // Returns the specialized FileSystemQuotaUtil for this backend.
137 // This could return NULL if this backend does not support quota.
138 virtual FileSystemQuotaUtil* GetQuotaUtil() = 0;
140 // Returns the update observer list for |type|. It may return NULL when no
141 // observers are added.
142 virtual const UpdateObserverList* GetUpdateObservers(
143 FileSystemType type) const = 0;
145 // Returns the change observer list for |type|. It may return NULL when no
146 // observers are added.
147 virtual const ChangeObserverList* GetChangeObservers(
148 FileSystemType type) const = 0;
150 // Returns the access observer list for |type|. It may return NULL when no
151 // observers are added.
152 virtual const AccessObserverList* GetAccessObservers(
153 FileSystemType type) const = 0;
156 // An interface to control external file system access permissions.
157 // TODO(satorux): Move this out of 'storage/browser/fileapi'. crbug.com/257279
158 class ExternalFileSystemBackend : public FileSystemBackend {
159 public:
160 // Returns true if |url| is allowed to be accessed.
161 // This is supposed to perform ExternalFileSystem-specific security
162 // checks.
163 virtual bool IsAccessAllowed(const storage::FileSystemURL& url) const = 0;
164 // Returns the list of top level directories that are exposed by this
165 // provider. This list is used to set appropriate child process file access
166 // permissions.
167 virtual std::vector<base::FilePath> GetRootDirectories() const = 0;
168 // Grants access to |virtual_path| from |origin_url|.
169 virtual void GrantFileAccessToExtension(
170 const std::string& extension_id,
171 const base::FilePath& virtual_path) = 0;
172 // Revokes file access from extension identified with |extension_id|.
173 virtual void RevokeAccessForExtension(
174 const std::string& extension_id) = 0;
175 // Gets virtual path by known filesystem path. Returns false when filesystem
176 // path is not exposed by this provider.
177 virtual bool GetVirtualPath(const base::FilePath& file_system_path,
178 base::FilePath* virtual_path) const = 0;
179 // Gets a redirect URL for contents. e.g. Google Drive URL for hosted
180 // documents. Returns empty URL if the entry does not have the redirect URL.
181 virtual void GetRedirectURLForContents(
182 const storage::FileSystemURL& url,
183 const storage::URLCallback& callback) const = 0;
184 // Creates an internal File System URL for performing internal operations such
185 // as confirming if a file or a directory exist before granting the final
186 // permission to the entry. The path must be an absolute path.
187 virtual storage::FileSystemURL CreateInternalURL(
188 storage::FileSystemContext* context,
189 const base::FilePath& entry_path) const = 0;
192 } // namespace storage
194 #endif // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_BACKEND_H_