blob: f6b8813e2d7341cc390441e5f7ffb11f6b5c8f24 [file] [log] [blame]
pilgrim4af8c212014-09-05 17:30:151// 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 Costanfeaee8cf72018-12-28 00:39:1511#include "base/component_export.h"
pilgrim4af8c212014-09-05 17:30:1512#include "base/files/file_path.h"
pilgrim4af8c212014-09-05 17:30:1513#include "storage/common/fileapi/file_system_mount_option.h"
14#include "storage/common/fileapi/file_system_types.h"
15#include "url/gurl.h"
Erik Anderson1606ab82019-02-04 22:15:2516#include "url/origin.h"
pilgrim4af8c212014-09-05 17:30:1517
18namespace 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 Costanfeaee8cf72018-12-28 00:39:1579class COMPONENT_EXPORT(STORAGE_BROWSER) FileSystemURL {
pilgrim4af8c212014-09-05 17:30:1580 public:
81 FileSystemURL();
vmpstr9dfd8d82016-02-26 21:38:1682 FileSystemURL(const FileSystemURL& other);
Chris Mumfordef8a99c2018-03-15 16:09:1083 // Constructs FileSystemURL with the contents of |other|, which is left in
84 // valid but unspecified state.
Raphael Kubo da Costa316734d82019-02-13 14:19:1385 FileSystemURL(FileSystemURL&& other);
pilgrim4af8c212014-09-05 17:30:1586 ~FileSystemURL();
87
Chris Mumfordef8a99c2018-03-15 16:09:1088 // 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
pilgrim4af8c212014-09-05 17:30:1594 // Methods for creating FileSystemURL without attempting to crack them.
95 // Should be used only in tests.
96 static FileSystemURL CreateForTest(const GURL& url);
Erik Anderson1606ab82019-02-04 22:15:2597 static FileSystemURL CreateForTest(const url::Origin& origin,
pilgrim4af8c212014-09-05 17:30:1598 FileSystemType mount_type,
99 const base::FilePath& virtual_path);
Erik Anderson1606ab82019-02-04 22:15:25100 static FileSystemURL CreateForTest(const url::Origin& origin,
hirono83a448b2014-09-26 06:53:37101 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);
pilgrim4af8c212014-09-05 17:30:15108
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 Anderson1606ab82019-02-04 22:15:25113 const url::Origin& origin() const { return origin_; }
pilgrim4af8c212014-09-05 17:30:15114
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 Costanfeaee8cf72018-12-28 00:39:15153 struct COMPONENT_EXPORT(STORAGE_BROWSER) Comparator {
pilgrim4af8c212014-09-05 17:30:15154 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 Anderson1606ab82019-02-04 22:15:25163 FileSystemURL(const url::Origin& origin,
pilgrim4af8c212014-09-05 17:30:15164 FileSystemType mount_type,
165 const base::FilePath& virtual_path);
166 // Creates a cracked FileSystemURL.
Erik Anderson1606ab82019-02-04 22:15:25167 FileSystemURL(const url::Origin& origin,
pilgrim4af8c212014-09-05 17:30:15168 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 Anderson1606ab82019-02-04 22:15:25176 // Used to determine if a FileSystemURL was default constructed.
177 bool is_null_ = false;
178
pilgrim4af8c212014-09-05 17:30:15179 bool is_valid_;
180
181 // Values parsed from the original URL.
Erik Anderson1606ab82019-02-04 22:15:25182 url::Origin origin_;
pilgrim4af8c212014-09-05 17:30:15183 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 Bell3bad1f62018-04-24 05:15:08198using FileSystemURLSet = std::set<FileSystemURL, FileSystemURL::Comparator>;
pilgrim4af8c212014-09-05 17:30:15199
200} // namespace storage
201
202#endif // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_