file_system_url.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_URL_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_URL_H_
  6. #include <set>
  7. #include <string>
  8. #include "base/component_export.h"
  9. #include "base/files/file_path.h"
  10. #include "components/services/storage/public/cpp/buckets/bucket_locator.h"
  11. #include "storage/common/file_system/file_system_mount_option.h"
  12. #include "storage/common/file_system/file_system_types.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "third_party/blink/public/common/storage_key/storage_key.h"
  15. #include "url/gurl.h"
  16. namespace storage {
  17. // A class representing a filesystem URL which consists of origin URL,
  18. // type and an internal path used inside the filesystem.
  19. //
  20. // When a FileSystemURL instance is created for a GURL (for filesystem: scheme),
  21. // each accessor method would return following values:
  22. //
  23. // Example: For a URL 'filesystem:http://foo.com/temporary/foo/bar':
  24. // origin() returns 'http://foo.com',
  25. // mount_type() returns kFileSystemTypeTemporary,
  26. // virtual_path() returns 'foo/bar',
  27. // type() returns the same value as mount_type(),
  28. // path() returns the same value as virtual_path(),
  29. // bucket() returns an empty string unless explicitly set with SetBucket(),
  30. //
  31. // All other accessors return empty or invalid value.
  32. //
  33. // FileSystemURL can also be created to represent a 'cracked' filesystem URL if
  34. // the original URL's type/path is pointing to a mount point which can be
  35. // further resolved to a lower filesystem type/path.
  36. //
  37. // Example: Assume a path '/media/removable' is mounted at mount name
  38. // 'mount_name' with type kFileSystemTypeFoo as an external file system.
  39. //
  40. // The original URL would look like:
  41. // 'filesystem:http://bar.com/external/mount_name/foo/bar':
  42. //
  43. // FileSystemURL('http://bar.com',
  44. // kFileSystemTypeExternal,
  45. // 'mount_name/foo/bar'
  46. // 'mount_name',
  47. // kFileSystemTypeFoo,
  48. // '/media/removable/foo/bar');
  49. // would create a FileSystemURL whose accessors return:
  50. //
  51. // origin() returns 'http://bar.com',
  52. // mount_type() returns kFileSystemTypeExternal,
  53. // virtual_path() returns 'mount_name/foo/bar',
  54. // type() returns the kFileSystemTypeFoo,
  55. // path() returns '/media/removable/foo/bar',
  56. //
  57. // Note that in either case virtual_path() always returns the path part after
  58. // 'type' part in the original URL, and mount_type() always returns the 'type'
  59. // part in the original URL.
  60. //
  61. // Additionally, following accessors would return valid values:
  62. // filesystem_id() returns 'mount_name'.
  63. //
  64. // It is impossible to directly create a valid FileSystemURL instance (except by
  65. // using CreatedForTest methods, which should not be used in production code).
  66. // To get a valid FileSystemURL, one of the following methods can be used:
  67. // <Friend>::CrackURL, <Friend>::CreateCrackedFileSystemURL, where <Friend> is
  68. // one of the friended classes.
  69. //
  70. // TODO(ericu): Look into making virtual_path() [and all FileSystem API virtual
  71. // paths] just an std::string, to prevent platform-specific base::FilePath
  72. // behavior from getting invoked by accident. Currently the base::FilePath
  73. // returned here needs special treatment, as it may contain paths that are
  74. // illegal on the current platform.
  75. // To avoid problems, use VirtualPath::BaseName and
  76. // VirtualPath::GetComponents instead of the base::FilePath methods.
  77. class COMPONENT_EXPORT(STORAGE_BROWSER) FileSystemURL {
  78. public:
  79. FileSystemURL();
  80. FileSystemURL(const FileSystemURL&);
  81. FileSystemURL(FileSystemURL&&) noexcept;
  82. FileSystemURL& operator=(const FileSystemURL&);
  83. FileSystemURL& operator=(FileSystemURL&&) noexcept;
  84. ~FileSystemURL();
  85. // Methods for creating FileSystemURL without attempting to crack them.
  86. // Should be used only in tests.
  87. static FileSystemURL CreateForTest(const GURL& url);
  88. static FileSystemURL CreateForTest(const blink::StorageKey& storage_key,
  89. FileSystemType mount_type,
  90. const base::FilePath& virtual_path);
  91. static FileSystemURL CreateForTest(const blink::StorageKey& storage_key,
  92. FileSystemType mount_type,
  93. const base::FilePath& virtual_path,
  94. const std::string& mount_filesystem_id,
  95. FileSystemType cracked_type,
  96. const base::FilePath& cracked_path,
  97. const std::string& filesystem_id,
  98. const FileSystemMountOption& mount_option);
  99. // Returns true if this instance represents a valid FileSystem URL.
  100. bool is_valid() const { return is_valid_; }
  101. // Returns the storage key. See the class comment for details.
  102. const blink::StorageKey& storage_key() const { return storage_key_; }
  103. // Returns the origin part of this URL. See the class comment for details.
  104. const url::Origin& origin() const { return storage_key_.origin(); }
  105. // Returns the type part of this URL. See the class comment for details.
  106. FileSystemType type() const { return type_; }
  107. // Returns the cracked path of this URL. See the class comment for details.
  108. const base::FilePath& path() const { return path_; }
  109. // Returns the original path part of this URL.
  110. // See the class comment for details.
  111. // TODO(kinuko): this must return std::string.
  112. const base::FilePath& virtual_path() const { return virtual_path_; }
  113. // Returns the filesystem ID/mount name for isolated/external filesystem URLs.
  114. // See the class comment for details.
  115. const std::string& filesystem_id() const { return filesystem_id_; }
  116. const std::string& mount_filesystem_id() const {
  117. return mount_filesystem_id_;
  118. }
  119. FileSystemType mount_type() const { return mount_type_; }
  120. const FileSystemMountOption& mount_option() const { return mount_option_; }
  121. // Returns the BucketLocator for this URL's partitioned file location. In
  122. // the majority of cases, this will not be populated and the default storage
  123. // bucket will be used.
  124. const absl::optional<BucketLocator>& bucket() const { return bucket_; }
  125. void SetBucket(const BucketLocator& bucket) { bucket_ = bucket; }
  126. // Returns the formatted URL of this instance.
  127. GURL ToGURL() const;
  128. std::string DebugString() const;
  129. // Returns true if this URL is a strict parent of the |child|.
  130. bool IsParent(const FileSystemURL& child) const;
  131. bool IsInSameFileSystem(const FileSystemURL& other) const;
  132. bool operator==(const FileSystemURL& that) const;
  133. bool operator!=(const FileSystemURL& that) const { return !(*this == that); }
  134. struct COMPONENT_EXPORT(STORAGE_BROWSER) Comparator {
  135. bool operator()(const FileSystemURL& lhs, const FileSystemURL& rhs) const;
  136. };
  137. private:
  138. friend class FileSystemContext;
  139. friend class ExternalMountPoints;
  140. friend class IsolatedContext;
  141. FileSystemURL(const GURL& filesystem_url,
  142. const blink::StorageKey& storage_key);
  143. FileSystemURL(const blink::StorageKey& storage_key,
  144. FileSystemType mount_type,
  145. const base::FilePath& virtual_path);
  146. // Creates a cracked FileSystemURL.
  147. FileSystemURL(const blink::StorageKey& storage_key,
  148. FileSystemType mount_type,
  149. const base::FilePath& virtual_path,
  150. const std::string& mount_filesystem_id,
  151. FileSystemType cracked_type,
  152. const base::FilePath& cracked_path,
  153. const std::string& filesystem_id,
  154. const FileSystemMountOption& mount_option);
  155. // Used to determine if a FileSystemURL was default constructed.
  156. bool is_null_ = false;
  157. bool is_valid_;
  158. // Values parsed from the original URL.
  159. blink::StorageKey storage_key_;
  160. FileSystemType mount_type_;
  161. base::FilePath virtual_path_;
  162. // Values obtained by cracking URLs.
  163. // |mount_filesystem_id_| is retrieved from the first round of cracking,
  164. // and the rest of the fields are from recursive cracking. Permission
  165. // checking on the top-level mount information should be done with the former,
  166. // and the low-level file operation should be implemented with the latter.
  167. std::string mount_filesystem_id_;
  168. FileSystemType type_;
  169. base::FilePath path_;
  170. std::string filesystem_id_;
  171. FileSystemMountOption mount_option_;
  172. // Values that must be explicitly set.
  173. absl::optional<BucketLocator> bucket_;
  174. };
  175. using FileSystemURLSet = std::set<FileSystemURL, FileSystemURL::Comparator>;
  176. } // namespace storage
  177. #endif // STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_URL_H_