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_CONTEXT_H_
6 #define STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_CONTEXT_H_
12 #include "base/callback.h"
13 #include "base/files/file.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/sequenced_task_runner_helpers.h"
18 #include "storage/browser/fileapi/file_system_url.h"
19 #include "storage/browser/fileapi/open_file_system_mode.h"
20 #include "storage/browser/fileapi/plugin_private_file_system_backend.h"
21 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h"
22 #include "storage/browser/fileapi/task_runner_bound_observer_list.h"
23 #include "storage/browser/storage_browser_export.h"
24 #include "storage/common/fileapi/file_system_types.h"
28 class SequencedTaskRunner
;
29 class SingleThreadTaskRunner
;
33 class NativeMediaFileUtilTest
;
37 class QuotaManagerProxy
;
38 class SpecialStoragePolicy
;
46 class BlobURLRequestJobTest
;
47 class FileStreamReader
;
53 class CopyOrMoveFileValidatorFactory
;
54 class ExternalFileSystemBackend
;
55 class ExternalMountPoints
;
56 class FileStreamWriter
;
57 class FileSystemBackend
;
58 class FileSystemFileUtil
;
59 class FileSystemOperation
;
60 class FileSystemOperationRunner
;
61 class FileSystemOptions
;
62 class FileSystemQuotaUtil
;
64 class IsolatedFileSystemBackend
;
66 class QuotaReservation
;
67 class SandboxFileSystemBackend
;
70 struct DefaultContextDeleter
;
71 struct FileSystemInfo
;
73 // An auto mount handler will attempt to mount the file system requested in
74 // |url_request|. If the URL is for this auto mount handler, it returns true
75 // and calls |callback| when the attempt is complete. If the auto mounter
76 // does not recognize the URL, it returns false and does not call |callback|.
77 // Called on the IO thread.
78 typedef base::Callback
<bool(
79 const net::URLRequest
* url_request
,
80 const FileSystemURL
& filesystem_url
,
81 const std::string
& storage_domain
,
82 const base::Callback
<void(base::File::Error result
)>& callback
)>
83 URLRequestAutoMountHandler
;
85 // This class keeps and provides a file system context for FileSystem API.
86 // An instance of this class is created and owned by profile.
87 class STORAGE_EXPORT FileSystemContext
88 : public base::RefCountedThreadSafe
<FileSystemContext
,
89 DefaultContextDeleter
> {
91 // Returns file permission policy we should apply for the given |type|.
92 // The return value must be bitwise-or'd of FilePermissionPolicy.
94 // Note: if a part of a filesystem is returned via 'Isolated' mount point,
95 // its per-filesystem permission overrides the underlying filesystem's
97 static int GetPermissionPolicy(FileSystemType type
);
99 // file_task_runner is used as default TaskRunner.
100 // Unless a FileSystemBackend is overridden in CreateFileSystemOperation,
101 // it is used for all file operations and file related meta operations.
102 // The code assumes that file_task_runner->RunsTasksOnCurrentThread()
103 // returns false if the current task is not running on the thread that allows
104 // blocking file operations (like SequencedWorkerPool implementation does).
106 // |external_mount_points| contains non-system external mount points available
107 // in the context. If not NULL, it will be used during URL cracking.
108 // |external_mount_points| may be NULL only on platforms different from
109 // ChromeOS (i.e. platforms that don't use external_mount_point_provider).
111 // |additional_backends| are added to the internal backend map
112 // to serve filesystem requests for non-regular types.
113 // If none is given, this context only handles HTML5 Sandbox FileSystem
114 // and Drag-and-drop Isolated FileSystem requests.
116 // |auto_mount_handlers| are used to resolve calls to
117 // AttemptAutoMountForURLRequest. Only external filesystems are auto mounted
118 // when a filesystem: URL request is made.
120 base::SingleThreadTaskRunner
* io_task_runner
,
121 base::SequencedTaskRunner
* file_task_runner
,
122 ExternalMountPoints
* external_mount_points
,
123 storage::SpecialStoragePolicy
* special_storage_policy
,
124 storage::QuotaManagerProxy
* quota_manager_proxy
,
125 ScopedVector
<FileSystemBackend
> additional_backends
,
126 const std::vector
<URLRequestAutoMountHandler
>& auto_mount_handlers
,
127 const base::FilePath
& partition_path
,
128 const FileSystemOptions
& options
);
130 bool DeleteDataForOriginOnFileTaskRunner(const GURL
& origin_url
);
132 // Creates a new QuotaReservation for the given |origin_url| and |type|.
133 // Returns NULL if |type| does not support quota or reservation fails.
134 // This should be run on |default_file_task_runner_| and the returned value
135 // should be destroyed on the runner.
136 scoped_refptr
<QuotaReservation
> CreateQuotaReservationOnFileTaskRunner(
137 const GURL
& origin_url
,
138 FileSystemType type
);
140 storage::QuotaManagerProxy
* quota_manager_proxy() const {
141 return quota_manager_proxy_
.get();
144 // Discards inflight operations in the operation runner.
147 // Returns a quota util for a given filesystem type. This may
148 // return NULL if the type does not support the usage tracking or
149 // it is not a quota-managed storage.
150 FileSystemQuotaUtil
* GetQuotaUtil(FileSystemType type
) const;
152 // Returns the appropriate AsyncFileUtil instance for the given |type|.
153 AsyncFileUtil
* GetAsyncFileUtil(FileSystemType type
) const;
155 // Returns the appropriate CopyOrMoveFileValidatorFactory for the given
156 // |type|. If |error_code| is File::FILE_OK and the result is NULL,
157 // then no validator is required.
158 CopyOrMoveFileValidatorFactory
* GetCopyOrMoveFileValidatorFactory(
159 FileSystemType type
, base::File::Error
* error_code
) const;
161 // Returns the file system backend instance for the given |type|.
162 // This may return NULL if it is given an invalid or unsupported filesystem
164 FileSystemBackend
* GetFileSystemBackend(
165 FileSystemType type
) const;
167 // Returns the watcher manager for the given |type|.
168 // This may return NULL if the type does not support watching.
169 WatcherManager
* GetWatcherManager(FileSystemType type
) const;
171 // Returns true for sandboxed filesystems. Currently this does
172 // the same as GetQuotaUtil(type) != NULL. (In an assumption that
173 // all sandboxed filesystems must cooperate with QuotaManager so that
174 // they can get deleted)
175 bool IsSandboxFileSystem(FileSystemType type
) const;
177 // Returns observers for the given filesystem type.
178 const UpdateObserverList
* GetUpdateObservers(FileSystemType type
) const;
179 const ChangeObserverList
* GetChangeObservers(FileSystemType type
) const;
180 const AccessObserverList
* GetAccessObservers(FileSystemType type
) const;
182 // Returns all registered filesystem types.
183 void GetFileSystemTypes(std::vector
<FileSystemType
>* types
) const;
185 // Returns a FileSystemBackend instance for external filesystem
186 // type, which is used only by chromeos for now. This is equivalent to
187 // calling GetFileSystemBackend(kFileSystemTypeExternal).
188 ExternalFileSystemBackend
* external_backend() const;
190 // Used for OpenFileSystem.
191 typedef base::Callback
<void(const GURL
& root
,
192 const std::string
& name
,
193 base::File::Error result
)>
194 OpenFileSystemCallback
;
196 // Used for ResolveURL.
197 enum ResolvedEntryType
{
199 RESOLVED_ENTRY_DIRECTORY
,
200 RESOLVED_ENTRY_NOT_FOUND
,
202 typedef base::Callback
<void(base::File::Error result
,
203 const FileSystemInfo
& info
,
204 const base::FilePath
& file_path
,
205 ResolvedEntryType type
)> ResolveURLCallback
;
207 // Used for DeleteFileSystem and OpenPluginPrivateFileSystem.
208 typedef base::Callback
<void(base::File::Error result
)> StatusCallback
;
210 // Opens the filesystem for the given |origin_url| and |type|, and dispatches
211 // |callback| on completion.
212 // If |create| is true this may actually set up a filesystem instance
213 // (e.g. by creating the root directory or initializing the database
216 const GURL
& origin_url
,
218 OpenFileSystemMode mode
,
219 const OpenFileSystemCallback
& callback
);
221 // Opens the filesystem for the given |url| as read-only, if the filesystem
222 // backend referred by the URL allows opening by resolveURL. Otherwise it
223 // fails with FILE_ERROR_SECURITY. The entry pointed by the URL can be
224 // absent; in that case RESOLVED_ENTRY_NOT_FOUND type is returned to the
225 // callback for indicating the absence. Can be called from any thread with
226 // a message loop. |callback| is invoked on the caller thread.
228 const FileSystemURL
& url
,
229 const ResolveURLCallback
& callback
);
231 // Attempts to mount the filesystem needed to satisfy |url_request| made
232 // from |storage_domain|. If an appropriate file system is not found,
233 // callback will return an error.
234 void AttemptAutoMountForURLRequest(const net::URLRequest
* url_request
,
235 const std::string
& storage_domain
,
236 const StatusCallback
& callback
);
238 // Deletes the filesystem for the given |origin_url| and |type|. This should
239 // be called on the IO thread.
240 void DeleteFileSystem(
241 const GURL
& origin_url
,
243 const StatusCallback
& callback
);
245 // Creates new FileStreamReader instance to read a file pointed by the given
246 // filesystem URL |url| starting from |offset|. |expected_modification_time|
247 // specifies the expected last modification if the value is non-null, the
248 // reader will check the underlying file's actual modification time to see if
249 // the file has been modified, and if it does any succeeding read operations
250 // should fail with ERR_UPLOAD_FILE_CHANGED error.
251 // This method internally cracks the |url|, get an appropriate
252 // FileSystemBackend for the URL and call the backend's CreateFileReader.
253 // The resolved FileSystemBackend could perform further specialization
254 // depending on the filesystem type pointed by the |url|.
255 // At most |max_bytes_to_read| can be fetched from the file stream reader.
256 scoped_ptr
<storage::FileStreamReader
> CreateFileStreamReader(
257 const FileSystemURL
& url
,
259 int64 max_bytes_to_read
,
260 const base::Time
& expected_modification_time
);
262 // Creates new FileStreamWriter instance to write into a file pointed by
263 // |url| from |offset|.
264 scoped_ptr
<FileStreamWriter
> CreateFileStreamWriter(
265 const FileSystemURL
& url
,
268 // Creates a new FileSystemOperationRunner.
269 scoped_ptr
<FileSystemOperationRunner
> CreateFileSystemOperationRunner();
271 base::SequencedTaskRunner
* default_file_task_runner() {
272 return default_file_task_runner_
.get();
275 FileSystemOperationRunner
* operation_runner() {
276 return operation_runner_
.get();
279 const base::FilePath
& partition_path() const { return partition_path_
; }
281 // Same as |CrackFileSystemURL|, but cracks FileSystemURL created from |url|.
282 FileSystemURL
CrackURL(const GURL
& url
) const;
283 // Same as |CrackFileSystemURL|, but cracks FileSystemURL created from method
285 FileSystemURL
CreateCrackedFileSystemURL(const GURL
& origin
,
287 const base::FilePath
& path
) const;
289 #if defined(OS_CHROMEOS)
290 // Used only on ChromeOS for now.
291 void EnableTemporaryFileSystemInIncognito();
294 SandboxFileSystemBackendDelegate
* sandbox_delegate() {
295 return sandbox_delegate_
.get();
298 // Returns true if the requested url is ok to be served.
299 // (E.g. this returns false if the context is created for incognito mode)
300 bool CanServeURLRequest(const FileSystemURL
& url
) const;
302 // This must be used to open 'plugin private' filesystem.
303 // See "plugin_private_file_system_backend.h" for more details.
304 void OpenPluginPrivateFileSystem(
305 const GURL
& origin_url
,
307 const std::string
& filesystem_id
,
308 const std::string
& plugin_id
,
309 OpenFileSystemMode mode
,
310 const StatusCallback
& callback
);
313 typedef std::map
<FileSystemType
, FileSystemBackend
*>
314 FileSystemBackendMap
;
316 // For CreateFileSystemOperation.
317 friend class FileSystemOperationRunner
;
319 // For sandbox_backend().
320 friend class content::SandboxFileSystemTestHelper
;
322 // For plugin_private_backend().
323 friend class content::PluginPrivateFileSystemBackendTest
;
326 friend struct DefaultContextDeleter
;
327 friend class base::DeleteHelper
<FileSystemContext
>;
328 friend class base::RefCountedThreadSafe
<FileSystemContext
,
329 DefaultContextDeleter
>;
330 ~FileSystemContext();
332 void DeleteOnCorrectThread() const;
334 // Creates a new FileSystemOperation instance by getting an appropriate
335 // FileSystemBackend for |url| and calling the backend's corresponding
336 // CreateFileSystemOperation method.
337 // The resolved FileSystemBackend could perform further specialization
338 // depending on the filesystem type pointed by the |url|.
340 // Called by FileSystemOperationRunner.
341 FileSystemOperation
* CreateFileSystemOperation(
342 const FileSystemURL
& url
,
343 base::File::Error
* error_code
);
345 // For non-cracked isolated and external mount points, returns a FileSystemURL
346 // created by cracking |url|. The url is cracked using MountPoints registered
347 // as |url_crackers_|. If the url cannot be cracked, returns invalid
350 // If the original url does not point to an isolated or external filesystem,
351 // returns the original url, without attempting to crack it.
352 FileSystemURL
CrackFileSystemURL(const FileSystemURL
& url
) const;
354 // For initial backend_map construction. This must be called only from
356 void RegisterBackend(FileSystemBackend
* backend
);
358 void DidOpenFileSystemForResolveURL(
359 const FileSystemURL
& url
,
360 const ResolveURLCallback
& callback
,
361 const GURL
& filesystem_root
,
362 const std::string
& filesystem_name
,
363 base::File::Error error
);
365 // Returns a FileSystemBackend, used only by test code.
366 SandboxFileSystemBackend
* sandbox_backend() const {
367 return sandbox_backend_
.get();
370 // Used only by test code.
371 PluginPrivateFileSystemBackend
* plugin_private_backend() const {
372 return plugin_private_backend_
.get();
375 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
376 scoped_refptr
<base::SequencedTaskRunner
> default_file_task_runner_
;
378 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
380 scoped_ptr
<SandboxFileSystemBackendDelegate
> sandbox_delegate_
;
382 // Regular file system backends.
383 scoped_ptr
<SandboxFileSystemBackend
> sandbox_backend_
;
384 scoped_ptr
<IsolatedFileSystemBackend
> isolated_backend_
;
386 // Additional file system backends.
387 scoped_ptr
<PluginPrivateFileSystemBackend
> plugin_private_backend_
;
388 ScopedVector
<FileSystemBackend
> additional_backends_
;
390 std::vector
<URLRequestAutoMountHandler
> auto_mount_handlers_
;
392 // Registered file system backends.
393 // The map must be constructed in the constructor since it can be accessed
394 // on multiple threads.
395 // This map itself doesn't retain each backend's ownership; ownerships
396 // of the backends are held by additional_backends_ or other scoped_ptr
398 FileSystemBackendMap backend_map_
;
400 // External mount points visible in the file system context (excluding system
401 // external mount points).
402 scoped_refptr
<ExternalMountPoints
> external_mount_points_
;
404 // MountPoints used to crack FileSystemURLs. The MountPoints are ordered
405 // in order they should try to crack a FileSystemURL.
406 std::vector
<MountPoints
*> url_crackers_
;
408 // The base path of the storage partition for this context.
409 const base::FilePath partition_path_
;
413 scoped_ptr
<FileSystemOperationRunner
> operation_runner_
;
415 DISALLOW_IMPLICIT_CONSTRUCTORS(FileSystemContext
);
418 struct DefaultContextDeleter
{
419 static void Destruct(const FileSystemContext
* context
) {
420 context
->DeleteOnCorrectThread();
424 } // namespace storage
426 #endif // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_CONTEXT_H_