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