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