| OLD | NEW |
| (Empty) |
| 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 WEBKIT_FILEAPI_FILE_SYSTEM_CONTEXT_H_ | |
| 6 #define WEBKIT_FILEAPI_FILE_SYSTEM_CONTEXT_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 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" | |
| 22 | |
| 23 namespace base { | |
| 24 class FilePath; | |
| 25 } | |
| 26 | |
| 27 namespace chrome { | |
| 28 class NativeMediaFileUtilTest; | |
| 29 } | |
| 30 | |
| 31 namespace quota { | |
| 32 class QuotaManagerProxy; | |
| 33 class SpecialStoragePolicy; | |
| 34 } | |
| 35 | |
| 36 namespace sync_file_system { | |
| 37 class LocalFileChangeTracker; | |
| 38 class LocalFileSyncContext; | |
| 39 } | |
| 40 | |
| 41 namespace webkit_blob { | |
| 42 class BlobURLRequestJobTest; | |
| 43 class FileStreamReader; | |
| 44 } | |
| 45 | |
| 46 namespace fileapi { | |
| 47 | |
| 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; | |
| 63 | |
| 64 struct DefaultContextDeleter; | |
| 65 | |
| 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). | |
| 79 // | |
| 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). | |
| 85 // | |
| 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); | |
| 98 | |
| 99 bool DeleteDataForOriginOnFileThread(const GURL& origin_url); | |
| 100 | |
| 101 quota::QuotaManagerProxy* quota_manager_proxy() const { | |
| 102 return quota_manager_proxy_.get(); | |
| 103 } | |
| 104 | |
| 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; | |
| 109 | |
| 110 // Returns the appropriate AsyncFileUtil instance for the given |type|. | |
| 111 AsyncFileUtil* GetAsyncFileUtil(FileSystemType type) const; | |
| 112 | |
| 113 // Returns the appropriate CopyOrMoveFileValidatorFactory for the given | |
| 114 // |type|. If |error_code| is PLATFORM_FILE_OK and the result is NULL, | |
| 115 // then no validator is required. | |
| 116 CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory( | |
| 117 FileSystemType type, base::PlatformFileError* error_code) const; | |
| 118 | |
| 119 // Returns the mount point provider instance for the given |type|. | |
| 120 // This may return NULL if it is given an invalid or unsupported filesystem | |
| 121 // type. | |
| 122 FileSystemMountPointProvider* GetMountPointProvider( | |
| 123 FileSystemType type) const; | |
| 124 | |
| 125 // Returns update observers for the given filesystem type. | |
| 126 const UpdateObserverList* GetUpdateObservers(FileSystemType type) const; | |
| 127 | |
| 128 // Returns a FileSystemMountPointProvider instance for sandboxed filesystem | |
| 129 // types (e.g. TEMPORARY or PERSISTENT). This is equivalent to calling | |
| 130 // GetMountPointProvider(kFileSystemType{Temporary, Persistent}). | |
| 131 SandboxMountPointProvider* sandbox_provider() const; | |
| 132 | |
| 133 // Returns a FileSystemMountPointProvider instance for external filesystem | |
| 134 // type, which is used only by chromeos for now. This is equivalent to | |
| 135 // calling GetMountPointProvider(kFileSystemTypeExternal). | |
| 136 ExternalFileSystemMountPointProvider* external_provider() const; | |
| 137 | |
| 138 // Used for OpenFileSystem. | |
| 139 typedef base::Callback<void(base::PlatformFileError result, | |
| 140 const std::string& name, | |
| 141 const GURL& root)> OpenFileSystemCallback; | |
| 142 | |
| 143 // Used for DeleteFileSystem. | |
| 144 typedef base::Callback<void(base::PlatformFileError result)> | |
| 145 DeleteFileSystemCallback; | |
| 146 | |
| 147 // Opens the filesystem for the given |origin_url| and |type|, and dispatches | |
| 148 // |callback| on completion. | |
| 149 // If |create| is true this may actually set up a filesystem instance | |
| 150 // (e.g. by creating the root directory or initializing the database | |
| 151 // entry etc). | |
| 152 void OpenFileSystem( | |
| 153 const GURL& origin_url, | |
| 154 FileSystemType type, | |
| 155 bool create, | |
| 156 const OpenFileSystemCallback& callback); | |
| 157 | |
| 158 // Opens a syncable filesystem for the given |origin_url|. | |
| 159 // The file system is internally mounted as an external file system at the | |
| 160 // given |mount_name|. | |
| 161 // Currently only kFileSystemTypeSyncable type is supported. | |
| 162 void OpenSyncableFileSystem( | |
| 163 const std::string& mount_name, | |
| 164 const GURL& origin_url, | |
| 165 FileSystemType type, | |
| 166 bool create, | |
| 167 const OpenFileSystemCallback& callback); | |
| 168 | |
| 169 // Deletes the filesystem for the given |origin_url| and |type|. This should | |
| 170 // be called on the IO Thread. | |
| 171 void DeleteFileSystem( | |
| 172 const GURL& origin_url, | |
| 173 FileSystemType type, | |
| 174 const DeleteFileSystemCallback& callback); | |
| 175 | |
| 176 // Creates a new FileSystemOperation instance by getting an appropriate | |
| 177 // MountPointProvider for |url| and calling the provider's corresponding | |
| 178 // CreateFileSystemOperation method. | |
| 179 // The resolved MountPointProvider could perform further specialization | |
| 180 // depending on the filesystem type pointed by the |url|. | |
| 181 FileSystemOperation* CreateFileSystemOperation( | |
| 182 const FileSystemURL& url, | |
| 183 base::PlatformFileError* error_code); | |
| 184 | |
| 185 // Creates new FileStreamReader instance to read a file pointed by the given | |
| 186 // filesystem URL |url| starting from |offset|. |expected_modification_time| | |
| 187 // specifies the expected last modification if the value is non-null, the | |
| 188 // reader will check the underlying file's actual modification time to see if | |
| 189 // the file has been modified, and if it does any succeeding read operations | |
| 190 // should fail with ERR_UPLOAD_FILE_CHANGED error. | |
| 191 // This method internally cracks the |url|, get an appropriate | |
| 192 // MountPointProvider for the URL and call the provider's CreateFileReader. | |
| 193 // The resolved MountPointProvider could perform further specialization | |
| 194 // depending on the filesystem type pointed by the |url|. | |
| 195 scoped_ptr<webkit_blob::FileStreamReader> CreateFileStreamReader( | |
| 196 const FileSystemURL& url, | |
| 197 int64 offset, | |
| 198 const base::Time& expected_modification_time); | |
| 199 | |
| 200 // Creates new FileStreamWriter instance to write into a file pointed by | |
| 201 // |url| from |offset|. | |
| 202 scoped_ptr<FileStreamWriter> CreateFileStreamWriter( | |
| 203 const FileSystemURL& url, | |
| 204 int64 offset); | |
| 205 | |
| 206 FileSystemTaskRunners* task_runners() { return task_runners_.get(); } | |
| 207 | |
| 208 sync_file_system::LocalFileChangeTracker* change_tracker() { | |
| 209 return change_tracker_.get(); | |
| 210 } | |
| 211 void SetLocalFileChangeTracker( | |
| 212 scoped_ptr<sync_file_system::LocalFileChangeTracker> tracker); | |
| 213 | |
| 214 sync_file_system::LocalFileSyncContext* sync_context() { | |
| 215 return sync_context_.get(); | |
| 216 } | |
| 217 void set_sync_context(sync_file_system::LocalFileSyncContext* sync_context); | |
| 218 | |
| 219 const base::FilePath& partition_path() const { return partition_path_; } | |
| 220 | |
| 221 // Same as |CrackFileSystemURL|, but cracks FileSystemURL created from |url|. | |
| 222 FileSystemURL CrackURL(const GURL& url) const; | |
| 223 // Same as |CrackFileSystemURL|, but cracks FileSystemURL created from method | |
| 224 // arguments. | |
| 225 FileSystemURL CreateCrackedFileSystemURL(const GURL& origin, | |
| 226 FileSystemType type, | |
| 227 const base::FilePath& path) const; | |
| 228 | |
| 229 private: | |
| 230 typedef std::map<FileSystemType, FileSystemMountPointProvider*> | |
| 231 MountPointProviderMap; | |
| 232 | |
| 233 // Friended for GetFileUtil. | |
| 234 // These classes know the target filesystem (i.e. sandbox filesystem) | |
| 235 // supports synchronous FileUtil. | |
| 236 friend class LocalFileSystemOperation; | |
| 237 friend class sync_file_system::LocalFileChangeTracker; | |
| 238 friend class sync_file_system::LocalFileSyncContext; | |
| 239 | |
| 240 // Friended for GetFileUtil. | |
| 241 // Test classes that rely on synchronous FileUtils. | |
| 242 friend class webkit_blob::BlobURLRequestJobTest; | |
| 243 friend class FileSystemQuotaClientTest; | |
| 244 friend class LocalFileSystemTestOriginHelper; | |
| 245 friend class chrome::NativeMediaFileUtilTest; | |
| 246 friend class FileSystemURLRequestJobTest; | |
| 247 friend class UploadFileSystemFileElementReaderTest; | |
| 248 | |
| 249 // Deleters. | |
| 250 friend struct DefaultContextDeleter; | |
| 251 friend class base::DeleteHelper<FileSystemContext>; | |
| 252 friend class base::RefCountedThreadSafe<FileSystemContext, | |
| 253 DefaultContextDeleter>; | |
| 254 ~FileSystemContext(); | |
| 255 | |
| 256 void DeleteOnCorrectThread() const; | |
| 257 | |
| 258 // For non-cracked isolated and external mount points, returns a FileSystemURL | |
| 259 // created by cracking |url|. The url is cracked using MountPoints registered | |
| 260 // as |url_crackers_|. If the url cannot be cracked, returns invalid | |
| 261 // FileSystemURL. | |
| 262 // | |
| 263 // If the original url does not point to an isolated or external filesystem, | |
| 264 // returns the original url, without attempting to crack it. | |
| 265 FileSystemURL CrackFileSystemURL(const FileSystemURL& url) const; | |
| 266 | |
| 267 // Returns the appropriate FileUtil instance for the given |type|. | |
| 268 // This may return NULL if it is given an invalid type or the filesystem | |
| 269 // does not support synchronous file operations. | |
| 270 FileSystemFileUtil* GetFileUtil(FileSystemType type) const; | |
| 271 | |
| 272 // For initial provider_map construction. This must be called only from | |
| 273 // the constructor. | |
| 274 void RegisterMountPointProvider(FileSystemMountPointProvider* provider); | |
| 275 | |
| 276 scoped_ptr<FileSystemTaskRunners> task_runners_; | |
| 277 | |
| 278 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; | |
| 279 | |
| 280 // Regular mount point providers. | |
| 281 scoped_ptr<SandboxMountPointProvider> sandbox_provider_; | |
| 282 scoped_ptr<IsolatedMountPointProvider> isolated_provider_; | |
| 283 scoped_ptr<ExternalFileSystemMountPointProvider> external_provider_; | |
| 284 | |
| 285 // Additional mount point providers. | |
| 286 ScopedVector<FileSystemMountPointProvider> additional_providers_; | |
| 287 | |
| 288 // Registered mount point providers. | |
| 289 // The map must be constructed in the constructor since it can be accessed | |
| 290 // on multiple threads. | |
| 291 // The ownership of each provider is held by mount_point_providers_. | |
| 292 MountPointProviderMap provider_map_; | |
| 293 // External mount points visible in the file system context (excluding system | |
| 294 // external mount points). | |
| 295 scoped_refptr<ExternalMountPoints> external_mount_points_; | |
| 296 | |
| 297 // MountPoints used to crack FileSystemURLs. The MountPoints are ordered | |
| 298 // in order they should try to crack a FileSystemURL. | |
| 299 std::vector<MountPoints*> url_crackers_; | |
| 300 | |
| 301 // The base path of the storage partition for this context. | |
| 302 const base::FilePath partition_path_; | |
| 303 | |
| 304 // For syncable file systems. | |
| 305 scoped_ptr<sync_file_system::LocalFileChangeTracker> change_tracker_; | |
| 306 scoped_refptr<sync_file_system::LocalFileSyncContext> sync_context_; | |
| 307 | |
| 308 DISALLOW_IMPLICIT_CONSTRUCTORS(FileSystemContext); | |
| 309 }; | |
| 310 | |
| 311 struct DefaultContextDeleter { | |
| 312 static void Destruct(const FileSystemContext* context) { | |
| 313 context->DeleteOnCorrectThread(); | |
| 314 } | |
| 315 }; | |
| 316 | |
| 317 } // namespace fileapi | |
| 318 | |
| 319 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_CONTEXT_H_ | |
| OLD | NEW |