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_BROWSER_FILEAPI_FILE_SYSTEM_BACKEND_H_ 6 #define WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_BACKEND_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/callback_forward.h" 12 #include "base/files/file_path.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/platform_file.h" 15 #include "webkit/browser/fileapi/file_permission_policy.h" 16 #include "webkit/browser/fileapi/open_file_system_mode.h" 17 #include "webkit/browser/webkit_storage_browser_export.h" 18 #include "webkit/common/fileapi/file_system_types.h" 19 20 class GURL; 21 22 namespace webkit_blob { 23 class FileStreamReader; 24 } 25 26 namespace fileapi { 27 28 class AsyncFileUtil; 29 class CopyOrMoveFileValidatorFactory; 30 class FileSystemURL; 31 class FileStreamWriter; 32 class FileSystemContext; 33 class FileSystemFileUtil; 34 class FileSystemOperation; 35 class FileSystemQuotaUtil; 36 37 // An interface for defining a file system backend. 38 // 39 // NOTE: when you implement a new FileSystemBackend for your own 40 // FileSystem module, please contact to kinuko (at) chromium.org. 41 // 42 class WEBKIT_STORAGE_BROWSER_EXPORT FileSystemBackend { 43 public: 44 // Callback for InitializeFileSystem. 45 typedef base::Callback<void(const GURL& root_url, 46 const std::string& name, 47 base::PlatformFileError error)> 48 OpenFileSystemCallback; 49 virtual ~FileSystemBackend() {} 50 51 // Returns true if this filesystem backend can handle |type|. 52 // One filesystem backend may be able to handle multiple filesystem types. 53 virtual bool CanHandleType(FileSystemType type) const = 0; 54 55 // This method is called right after the backend is registered in the 56 // FileSystemContext and before any other methods are called. Each backend can 57 // do additional initialization which depends on FileSystemContext here. 58 virtual void Initialize(FileSystemContext* context) = 0; 59 60 // Opens the filesystem for the given |origin_url| and |type|. 61 // This verifies if it is allowed to request (or create) the filesystem 62 // and if it can access (or create) the root directory. 63 // If |mode| is CREATE_IF_NONEXISTENT calling this may also create 64 // the root directory (and/or related database entries etc) for 65 // the filesystem if it doesn't exist. 66 virtual void OpenFileSystem( 67 const GURL& origin_url, 68 FileSystemType type, 69 OpenFileSystemMode mode, 70 const OpenFileSystemCallback& callback) = 0; 71 72 // Returns the specialized FileSystemFileUtil for this backend. 73 // It is ok to return NULL if the filesystem doesn't support synchronous 74 // version of FileUtil. 75 virtual FileSystemFileUtil* GetFileUtil(FileSystemType type) = 0; 76 77 // Returns the specialized AsyncFileUtil for this backend. 78 virtual AsyncFileUtil* GetAsyncFileUtil(FileSystemType type) = 0; 79 80 // Returns the specialized CopyOrMoveFileValidatorFactory for this backend 81 // and |type|. If |error_code| is PLATFORM_FILE_OK and the result is NULL, 82 // then no validator is required. 83 virtual CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory( 84 FileSystemType type, base::PlatformFileError* error_code) = 0; 85 86 // Returns a new instance of the specialized FileSystemOperation for this 87 // backend based on the given triplet of |origin_url|, |file_system_type| 88 // and |virtual_path|. On failure to create a file system operation, set 89 // |error_code| correspondingly. 90 // This method is usually dispatched by 91 // FileSystemContext::CreateFileSystemOperation. 92 virtual FileSystemOperation* CreateFileSystemOperation( 93 const FileSystemURL& url, 94 FileSystemContext* context, 95 base::PlatformFileError* error_code) const = 0; 96 97 // Creates a new file stream reader for a given filesystem URL |url| with an 98 // offset |offset|. |expected_modification_time| specifies the expected last 99 // modification if the value is non-null, the reader will check the underlying 100 // file's actual modification time to see if the file has been modified, and 101 // if it does any succeeding read operations should fail with 102 // ERR_UPLOAD_FILE_CHANGED error. 103 // This method itself does *not* check if the given path exists and is a 104 // regular file. 105 virtual scoped_ptr<webkit_blob::FileStreamReader> CreateFileStreamReader( 106 const FileSystemURL& url, 107 int64 offset, 108 const base::Time& expected_modification_time, 109 FileSystemContext* context) const = 0; 110 111 // Creates a new file stream writer for a given filesystem URL |url| with an 112 // offset |offset|. 113 // This method itself does *not* check if the given path exists and is a 114 // regular file. 115 virtual scoped_ptr<FileStreamWriter> CreateFileStreamWriter( 116 const FileSystemURL& url, 117 int64 offset, 118 FileSystemContext* context) const = 0; 119 120 // Returns the specialized FileSystemQuotaUtil for this backend. 121 // This could return NULL if this backend does not support quota. 122 virtual FileSystemQuotaUtil* GetQuotaUtil() = 0; 123 }; 124 125 // An interface to control external file system access permissions. 126 // TODO(satorux): Move this out of 'webkit/browser/fileapi'. crbug.com/257279 127 class ExternalFileSystemBackend : public FileSystemBackend { 128 public: 129 // Returns true if |url| is allowed to be accessed. 130 // This is supposed to perform ExternalFileSystem-specific security 131 // checks. 132 virtual bool IsAccessAllowed(const fileapi::FileSystemURL& url) const = 0; 133 // Returns the list of top level directories that are exposed by this 134 // provider. This list is used to set appropriate child process file access 135 // permissions. 136 virtual std::vector<base::FilePath> GetRootDirectories() const = 0; 137 // Grants access to all external file system from extension identified with 138 // |extension_id|. 139 virtual void GrantFullAccessToExtension(const std::string& extension_id) = 0; 140 // Grants access to |virtual_path| from |origin_url|. 141 virtual void GrantFileAccessToExtension( 142 const std::string& extension_id, 143 const base::FilePath& virtual_path) = 0; 144 // Revokes file access from extension identified with |extension_id|. 145 virtual void RevokeAccessForExtension( 146 const std::string& extension_id) = 0; 147 // Gets virtual path by known filesystem path. Returns false when filesystem 148 // path is not exposed by this provider. 149 virtual bool GetVirtualPath(const base::FilePath& file_system_path, 150 base::FilePath* virtual_path) = 0; 151 }; 152 153 } // namespace fileapi 154 155 #endif // WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_BACKEND_H_ 156