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