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 STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_BACKEND_H_
6#define STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_BACKEND_H_
7
8#include <limits>
9#include <string>
10#include <vector>
11
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"
21
22class GURL;
23
24namespace storage {
25
26class AsyncFileUtil;
27class CopyOrMoveFileValidatorFactory;
28class FileSystemURL;
29class FileStreamReader;
30class FileStreamWriter;
31class FileSystemContext;
32class FileSystemFileUtil;
33class FileSystemOperation;
34class FileSystemQuotaUtil;
35class WatcherManager;
36
37// Callback to take GURL.
38typedef base::Callback<void(const GURL& url)> URLCallback;
39
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.
43const int64 kMaximumLength = std::numeric_limits<int64>::max();
44
45// An interface for defining a file system backend.
46//
47// NOTE: when you implement a new FileSystemBackend for your own
48// FileSystem module, please contact to kinuko@chromium.org.
49//
50class STORAGE_EXPORT FileSystemBackend {
51 public:
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() {}
58
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;
62
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;
67
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
73  // doesn't exist.
74  virtual void ResolveURL(const FileSystemURL& url,
75                          OpenFileSystemMode mode,
76                          const OpenFileSystemCallback& callback) = 0;
77
78  // Returns the specialized AsyncFileUtil for this backend.
79  virtual AsyncFileUtil* GetAsyncFileUtil(FileSystemType type) = 0;
80
81  // Returns the specialized WatcherManager for this backend.
82  virtual WatcherManager* GetWatcherManager(FileSystemType type) = 0;
83
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;
89
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;
100
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;
105
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;
110
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
119  // stream reader.
120  virtual scoped_ptr<storage::FileStreamReader> CreateFileStreamReader(
121      const FileSystemURL& url,
122      int64 offset,
123      int64 max_bytes_to_read,
124      const base::Time& expected_modification_time,
125      FileSystemContext* context) const = 0;
126
127  // Creates a new file stream writer for a given filesystem URL |url| with an
128  // offset |offset|.
129  // This method itself does *not* check if the given path exists and is a
130  // regular file.
131  virtual scoped_ptr<FileStreamWriter> CreateFileStreamWriter(
132      const FileSystemURL& url,
133      int64 offset,
134      FileSystemContext* context) const = 0;
135
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;
139
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;
144
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;
149
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;
154};
155
156// An interface to control external file system access permissions.
157// TODO(satorux): Move this out of 'storage/browser/fileapi'. crbug.com/257279
158class ExternalFileSystemBackend : public FileSystemBackend {
159 public:
160  // Returns true if |url| is allowed to be accessed.
161  // This is supposed to perform ExternalFileSystem-specific security
162  // checks.
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
166  // permissions.
167  virtual std::vector<base::FilePath> GetRootDirectories() const = 0;
168  // Grants access to all external file system from extension identified with
169  // |extension_id|.
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;
187};
188
189}  // namespace storage
190
191#endif  // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_BACKEND_H_
192