Only sync parent directory once after a leveldb file rename.
[chromium-blink-merge.git] / webkit / fileapi / file_system_context.h
blobd9343cbdbd33714aa850fb307a55942cb1757c2e
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 WEBKIT_FILEAPI_FILE_SYSTEM_CONTEXT_H_
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_CONTEXT_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/platform_file.h"
17 #include "base/sequenced_task_runner_helpers.h"
18 #include "webkit/fileapi/file_system_types.h"
19 #include "webkit/fileapi/file_system_url.h"
20 #include "webkit/fileapi/task_runner_bound_observer_list.h"
21 #include "webkit/storage/webkit_storage_export.h"
23 namespace base {
24 class FilePath;
27 namespace chrome {
28 class NativeMediaFileUtilTest;
31 namespace quota {
32 class QuotaManagerProxy;
33 class SpecialStoragePolicy;
36 namespace sync_file_system {
37 class LocalFileChangeTracker;
38 class LocalFileSyncContext;
41 namespace webkit_blob {
42 class BlobURLRequestJobTest;
43 class FileStreamReader;
46 namespace fileapi {
48 class AsyncFileUtil;
49 class CopyOrMoveFileValidatorFactory;
50 class ExternalFileSystemMountPointProvider;
51 class ExternalMountPoints;
52 class FileStreamWriter;
53 class FileSystemFileUtil;
54 class FileSystemMountPointProvider;
55 class FileSystemOperation;
56 class FileSystemOptions;
57 class FileSystemQuotaUtil;
58 class FileSystemTaskRunners;
59 class FileSystemURL;
60 class IsolatedMountPointProvider;
61 class MountPoints;
62 class SandboxMountPointProvider;
64 struct DefaultContextDeleter;
66 // This class keeps and provides a file system context for FileSystem API.
67 // An instance of this class is created and owned by profile.
68 class WEBKIT_STORAGE_EXPORT FileSystemContext
69 : public base::RefCountedThreadSafe<FileSystemContext,
70 DefaultContextDeleter> {
71 public:
72 // task_runners->file_task_runner() is used as default TaskRunner.
73 // Unless a MountPointProvider is overridden in CreateFileSystemOperation,
74 // it is used for all file operations and file related meta operations.
75 // The code assumes that
76 // task_runners->file_task_runner()->RunsTasksOnCurrentThread()
77 // returns false if the current task is not running on the thread that allows
78 // blocking file operations (like SequencedWorkerPool implementation does).
80 // |external_mount_points| contains non-system external mount points available
81 // in the context. If not NULL, it will be used during URL cracking. On
82 // ChromeOS, it will be passed to external_mount_point_provider.
83 // |external_mount_points| may be NULL only on platforms different from
84 // ChromeOS (i.e. platforms that don't use external_mount_point_provider).
86 // |additional_providers| are added to the internal provider map
87 // to serve filesystem requests for non-regular types.
88 // If none is given, this context only handles HTML5 Sandbox FileSystem
89 // and Drag-and-drop Isolated FileSystem requests.
90 FileSystemContext(
91 scoped_ptr<FileSystemTaskRunners> task_runners,
92 ExternalMountPoints* external_mount_points,
93 quota::SpecialStoragePolicy* special_storage_policy,
94 quota::QuotaManagerProxy* quota_manager_proxy,
95 ScopedVector<FileSystemMountPointProvider> additional_providers,
96 const base::FilePath& partition_path,
97 const FileSystemOptions& options);
99 bool DeleteDataForOriginOnFileThread(const GURL& origin_url);
101 quota::QuotaManagerProxy* quota_manager_proxy() const {
102 return quota_manager_proxy_.get();
105 // Returns a quota util for a given filesystem type. This may
106 // return NULL if the type does not support the usage tracking or
107 // it is not a quota-managed storage.
108 FileSystemQuotaUtil* GetQuotaUtil(FileSystemType type) const;
110 // Returns the appropriate AsyncFileUtil instance for the given |type|.
111 AsyncFileUtil* GetAsyncFileUtil(FileSystemType type) const;
113 // Returns the appropriate FileUtil instance for the given |type|.
114 // This may return NULL if it is given an invalid type or the filesystem
115 // does not support synchronous file operations.
116 FileSystemFileUtil* GetFileUtil(FileSystemType type) const;
118 // Returns the appropriate CopyOrMoveFileValidatorFactory for the given
119 // |type|. If |error_code| is PLATFORM_FILE_OK and the result is NULL,
120 // then no validator is required.
121 CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory(
122 FileSystemType type, base::PlatformFileError* error_code) const;
124 // Returns the mount point provider instance for the given |type|.
125 // This may return NULL if it is given an invalid or unsupported filesystem
126 // type.
127 FileSystemMountPointProvider* GetMountPointProvider(
128 FileSystemType type) const;
130 // Returns update observers for the given filesystem type.
131 const UpdateObserverList* GetUpdateObservers(FileSystemType type) const;
133 // Returns a FileSystemMountPointProvider instance for sandboxed filesystem
134 // types (e.g. TEMPORARY or PERSISTENT). This is equivalent to calling
135 // GetMountPointProvider(kFileSystemType{Temporary, Persistent}).
136 SandboxMountPointProvider* sandbox_provider() const;
138 // Returns a FileSystemMountPointProvider instance for external filesystem
139 // type, which is used only by chromeos for now. This is equivalent to
140 // calling GetMountPointProvider(kFileSystemTypeExternal).
141 ExternalFileSystemMountPointProvider* external_provider() const;
143 // Used for OpenFileSystem.
144 typedef base::Callback<void(base::PlatformFileError result,
145 const std::string& name,
146 const GURL& root)> OpenFileSystemCallback;
148 // Used for DeleteFileSystem.
149 typedef base::Callback<void(base::PlatformFileError result)>
150 DeleteFileSystemCallback;
152 // Opens the filesystem for the given |origin_url| and |type|, and dispatches
153 // |callback| on completion.
154 // If |create| is true this may actually set up a filesystem instance
155 // (e.g. by creating the root directory or initializing the database
156 // entry etc).
157 void OpenFileSystem(
158 const GURL& origin_url,
159 FileSystemType type,
160 bool create,
161 const OpenFileSystemCallback& callback);
163 // Opens a syncable filesystem for the given |origin_url|.
164 // The file system is internally mounted as an external file system at the
165 // given |mount_name|.
166 // Currently only kFileSystemTypeSyncable type is supported.
167 void OpenSyncableFileSystem(
168 const std::string& mount_name,
169 const GURL& origin_url,
170 FileSystemType type,
171 bool create,
172 const OpenFileSystemCallback& callback);
174 // Deletes the filesystem for the given |origin_url| and |type|. This should
175 // be called on the IO Thread.
176 void DeleteFileSystem(
177 const GURL& origin_url,
178 FileSystemType type,
179 const DeleteFileSystemCallback& callback);
181 // Creates a new FileSystemOperation instance by getting an appropriate
182 // MountPointProvider for |url| and calling the provider's corresponding
183 // CreateFileSystemOperation method.
184 // The resolved MountPointProvider could perform further specialization
185 // depending on the filesystem type pointed by the |url|.
186 FileSystemOperation* CreateFileSystemOperation(
187 const FileSystemURL& url,
188 base::PlatformFileError* error_code);
190 // Creates new FileStreamReader instance to read a file pointed by the given
191 // filesystem URL |url| starting from |offset|. |expected_modification_time|
192 // specifies the expected last modification if the value is non-null, the
193 // reader will check the underlying file's actual modification time to see if
194 // the file has been modified, and if it does any succeeding read operations
195 // should fail with ERR_UPLOAD_FILE_CHANGED error.
196 // This method internally cracks the |url|, get an appropriate
197 // MountPointProvider for the URL and call the provider's CreateFileReader.
198 // The resolved MountPointProvider could perform further specialization
199 // depending on the filesystem type pointed by the |url|.
200 scoped_ptr<webkit_blob::FileStreamReader> CreateFileStreamReader(
201 const FileSystemURL& url,
202 int64 offset,
203 const base::Time& expected_modification_time);
205 // Creates new FileStreamWriter instance to write into a file pointed by
206 // |url| from |offset|.
207 scoped_ptr<FileStreamWriter> CreateFileStreamWriter(
208 const FileSystemURL& url,
209 int64 offset);
211 FileSystemTaskRunners* task_runners() { return task_runners_.get(); }
213 sync_file_system::LocalFileChangeTracker* change_tracker() {
214 return change_tracker_.get();
216 void SetLocalFileChangeTracker(
217 scoped_ptr<sync_file_system::LocalFileChangeTracker> tracker);
219 sync_file_system::LocalFileSyncContext* sync_context() {
220 return sync_context_.get();
222 void set_sync_context(sync_file_system::LocalFileSyncContext* sync_context);
224 const base::FilePath& partition_path() const { return partition_path_; }
226 // Same as |CrackFileSystemURL|, but cracks FileSystemURL created from |url|.
227 FileSystemURL CrackURL(const GURL& url) const;
228 // Same as |CrackFileSystemURL|, but cracks FileSystemURL created from method
229 // arguments.
230 FileSystemURL CreateCrackedFileSystemURL(const GURL& origin,
231 FileSystemType type,
232 const base::FilePath& path) const;
234 private:
235 typedef std::map<FileSystemType, FileSystemMountPointProvider*>
236 MountPointProviderMap;
238 // These classes know the target filesystem (i.e. sandbox filesystem)
239 // supports synchronous FileUtil.
240 friend class LocalFileSystemOperation;
241 friend class sync_file_system::LocalFileChangeTracker;
242 friend class sync_file_system::LocalFileSyncContext;
244 // Deleters.
245 friend struct DefaultContextDeleter;
246 friend class base::DeleteHelper<FileSystemContext>;
247 friend class base::RefCountedThreadSafe<FileSystemContext,
248 DefaultContextDeleter>;
249 ~FileSystemContext();
251 void DeleteOnCorrectThread() const;
253 // For non-cracked isolated and external mount points, returns a FileSystemURL
254 // created by cracking |url|. The url is cracked using MountPoints registered
255 // as |url_crackers_|. If the url cannot be cracked, returns invalid
256 // FileSystemURL.
258 // If the original url does not point to an isolated or external filesystem,
259 // returns the original url, without attempting to crack it.
260 FileSystemURL CrackFileSystemURL(const FileSystemURL& url) const;
262 // For initial provider_map construction. This must be called only from
263 // the constructor.
264 void RegisterMountPointProvider(FileSystemMountPointProvider* provider);
266 scoped_ptr<FileSystemTaskRunners> task_runners_;
268 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_;
270 // Regular mount point providers.
271 scoped_ptr<SandboxMountPointProvider> sandbox_provider_;
272 scoped_ptr<IsolatedMountPointProvider> isolated_provider_;
273 scoped_ptr<ExternalFileSystemMountPointProvider> external_provider_;
275 // Additional mount point providers.
276 ScopedVector<FileSystemMountPointProvider> additional_providers_;
278 // Registered mount point providers.
279 // The map must be constructed in the constructor since it can be accessed
280 // on multiple threads.
281 // The ownership of each provider is held by mount_point_providers_.
282 MountPointProviderMap provider_map_;
283 // External mount points visible in the file system context (excluding system
284 // external mount points).
285 scoped_refptr<ExternalMountPoints> external_mount_points_;
287 // MountPoints used to crack FileSystemURLs. The MountPoints are ordered
288 // in order they should try to crack a FileSystemURL.
289 std::vector<MountPoints*> url_crackers_;
291 // The base path of the storage partition for this context.
292 const base::FilePath partition_path_;
294 // For syncable file systems.
295 scoped_ptr<sync_file_system::LocalFileChangeTracker> change_tracker_;
296 scoped_refptr<sync_file_system::LocalFileSyncContext> sync_context_;
298 DISALLOW_IMPLICIT_CONSTRUCTORS(FileSystemContext);
301 struct DefaultContextDeleter {
302 static void Destruct(const FileSystemContext* context) {
303 context->DeleteOnCorrectThread();
307 } // namespace fileapi
309 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_CONTEXT_H_