file_system_backend.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_BACKEND_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_BACKEND_H_
  6. #include <stdint.h>
  7. #include <limits>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/callback_forward.h"
  12. #include "base/component_export.h"
  13. #include "base/files/file.h"
  14. #include "base/files/file_path.h"
  15. #include "storage/browser/file_system/file_permission_policy.h"
  16. #include "storage/browser/file_system/open_file_system_mode.h"
  17. #include "storage/browser/file_system/task_runner_bound_observer_list.h"
  18. #include "storage/common/file_system/file_system_types.h"
  19. #include "url/origin.h"
  20. class GURL;
  21. namespace storage {
  22. class AsyncFileUtil;
  23. class CopyOrMoveFileValidatorFactory;
  24. class FileSystemURL;
  25. class FileStreamReader;
  26. class FileStreamWriter;
  27. class FileSystemContext;
  28. class FileSystemOperation;
  29. class FileSystemQuotaUtil;
  30. class WatcherManager;
  31. // Callback to take GURL.
  32. using URLCallback = base::OnceCallback<void(const GURL& url)>;
  33. // Maximum numer of bytes to be read by FileStreamReader classes. Used in
  34. // FileSystemBackend::CreateFileStreamReader(), when it's not known how many
  35. // bytes will be fetched in total.
  36. const int64_t kMaximumLength = INT64_MAX;
  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@chromium.org.
  41. //
  42. class COMPONENT_EXPORT(STORAGE_BROWSER) FileSystemBackend {
  43. public:
  44. // Callback for InitializeFileSystem.
  45. using OpenFileSystemCallback =
  46. base::OnceCallback<void(const FileSystemURL& root_url,
  47. const std::string& name,
  48. base::File::Error error)>;
  49. using ResolveURLCallback = base::OnceCallback<
  50. void(const GURL&, const std::string&, base::File::Error)>;
  51. virtual ~FileSystemBackend() = default;
  52. // Returns true if this filesystem backend can handle |type|.
  53. // One filesystem backend may be able to handle multiple filesystem types.
  54. virtual bool CanHandleType(FileSystemType type) const = 0;
  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. // Resolves the filesystem root URL and the name for the given |url|.
  60. // This verifies if it is allowed to request (or create) the filesystem and if
  61. // it can access (or create) the root directory.
  62. // If |mode| is CREATE_IF_NONEXISTENT calling this may also create the root
  63. // directory (and/or related database entries etc) for the filesystem if it
  64. // doesn't exist.
  65. virtual void ResolveURL(const FileSystemURL& url,
  66. OpenFileSystemMode mode,
  67. ResolveURLCallback callback) = 0;
  68. // Returns the specialized AsyncFileUtil for this backend.
  69. virtual AsyncFileUtil* GetAsyncFileUtil(FileSystemType type) = 0;
  70. // Returns the specialized WatcherManager for this backend.
  71. virtual WatcherManager* GetWatcherManager(FileSystemType type) = 0;
  72. // Returns the specialized CopyOrMoveFileValidatorFactory for this backend
  73. // and |type|. If |error_code| is File::FILE_OK and the result is nullptr,
  74. // then no validator is required.
  75. virtual CopyOrMoveFileValidatorFactory* GetCopyOrMoveFileValidatorFactory(
  76. FileSystemType type,
  77. base::File::Error* error_code) = 0;
  78. // Returns a new instance of the specialized FileSystemOperation for this
  79. // backend based on the given triplet of |origin_url|, |file_system_type|
  80. // and |virtual_path|. On failure to create a file system operation, set
  81. // |error_code| correspondingly.
  82. // This method is usually dispatched by
  83. // FileSystemContext::CreateFileSystemOperation.
  84. virtual std::unique_ptr<FileSystemOperation> CreateFileSystemOperation(
  85. const FileSystemURL& url,
  86. FileSystemContext* context,
  87. base::File::Error* error_code) const = 0;
  88. // Returns true if Blobs accessing |url| should use FileStreamReader.
  89. // If false, Blobs are accessed using a snapshot file by calling
  90. // AsyncFileUtil::CreateSnapshotFile.
  91. virtual bool SupportsStreaming(const FileSystemURL& url) const = 0;
  92. // Returns true if specified |type| of filesystem can handle Copy()
  93. // of the files in the same file system instead of streaming
  94. // read/write implementation.
  95. virtual bool HasInplaceCopyImplementation(FileSystemType type) const = 0;
  96. // Creates a new file stream reader for a given filesystem URL |url| with an
  97. // offset |offset|. |expected_modification_time| specifies the expected last
  98. // modification if the value is non-null, the reader will check the underlying
  99. // file's actual modification time to see if the file has been modified, and
  100. // if it does any succeeding read operations should fail with
  101. // ERR_UPLOAD_FILE_CHANGED error.
  102. // This method itself does *not* check if the given path exists and is a
  103. // regular file. At most |max_bytes_to_read| can be fetched from the file
  104. // stream reader.
  105. virtual std::unique_ptr<FileStreamReader> CreateFileStreamReader(
  106. const FileSystemURL& url,
  107. int64_t offset,
  108. int64_t max_bytes_to_read,
  109. const base::Time& expected_modification_time,
  110. FileSystemContext* context) const = 0;
  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 std::unique_ptr<FileStreamWriter> CreateFileStreamWriter(
  116. const FileSystemURL& url,
  117. int64_t offset,
  118. FileSystemContext* context) const = 0;
  119. // Returns the specialized FileSystemQuotaUtil for this backend.
  120. // This could return nullptr if this backend does not support quota.
  121. virtual FileSystemQuotaUtil* GetQuotaUtil() = 0;
  122. // Returns the update observer list for |type|. It may return nullptr when no
  123. // observers are added.
  124. virtual const UpdateObserverList* GetUpdateObservers(
  125. FileSystemType type) const = 0;
  126. // Returns the change observer list for |type|. It may return nullptr when no
  127. // observers are added.
  128. virtual const ChangeObserverList* GetChangeObservers(
  129. FileSystemType type) const = 0;
  130. // Returns the access observer list for |type|. It may return nullptr when no
  131. // observers are added.
  132. virtual const AccessObserverList* GetAccessObservers(
  133. FileSystemType type) const = 0;
  134. };
  135. // An interface to control external file system access permissions.
  136. // TODO(satorux): Move this out of 'storage/browser/fileapi'. crbug.com/257279
  137. class ExternalFileSystemBackend : public FileSystemBackend {
  138. public:
  139. // Returns true if |url| is allowed to be accessed.
  140. // This is supposed to perform ExternalFileSystem-specific security
  141. // checks.
  142. virtual bool IsAccessAllowed(const FileSystemURL& url) const = 0;
  143. // Returns the list of top level directories that are exposed by this
  144. // provider. This list is used to set appropriate child process file access
  145. // permissions.
  146. virtual std::vector<base::FilePath> GetRootDirectories() const = 0;
  147. // Grants access to |virtual_path| from |origin| URL.
  148. virtual void GrantFileAccessToOrigin(const url::Origin& origin,
  149. const base::FilePath& virtual_path) = 0;
  150. // Revokes file access from origin identified with |origin|.
  151. virtual void RevokeAccessForOrigin(const url::Origin& origin) = 0;
  152. // Gets virtual path by known filesystem path. Returns false when filesystem
  153. // path is not exposed by this provider.
  154. virtual bool GetVirtualPath(const base::FilePath& file_system_path,
  155. base::FilePath* virtual_path) const = 0;
  156. // Gets a redirect URL for contents. e.g. Google Drive URL for hosted
  157. // documents. Returns empty URL if the entry does not have the redirect URL.
  158. virtual void GetRedirectURLForContents(const FileSystemURL& url,
  159. URLCallback callback) const = 0;
  160. // Creates an internal File System URL for performing internal operations such
  161. // as confirming if a file or a directory exist before granting the final
  162. // permission to the entry. The path must be an absolute path.
  163. virtual FileSystemURL CreateInternalURL(
  164. FileSystemContext* context,
  165. const base::FilePath& entry_path) const = 0;
  166. };
  167. } // namespace storage
  168. #endif // STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_BACKEND_H_