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.
5 #ifndef STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_
6 #define STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_
11 #include "base/files/file_path.h"
12 #include "storage/browser/storage_browser_export.h"
13 #include "storage/common/fileapi/file_system_mount_option.h"
14 #include "storage/common/fileapi/file_system_types.h"
19 // A class representing a filesystem URL which consists of origin URL,
20 // type and an internal path used inside the filesystem.
22 // When a FileSystemURL instance is created for a GURL (for filesystem: scheme),
23 // each accessor method would return following values:
25 // Example: For a URL 'filesystem:http://foo.com/temporary/foo/bar':
26 // origin() returns 'http://foo.com',
27 // mount_type() returns kFileSystemTypeTemporary,
28 // virtual_path() returns 'foo/bar',
29 // type() returns the same value as mount_type(),
30 // path() returns the same value as virtual_path(),
32 // All other accessors return empty or invalid value.
34 // FileSystemURL can also be created to represent a 'cracked' filesystem URL if
35 // the original URL's type/path is pointing to a mount point which can be
36 // further resolved to a lower filesystem type/path.
38 // Example: Assume a path '/media/removable' is mounted at mount name
39 // 'mount_name' with type kFileSystemTypeFoo as an external file system.
41 // The original URL would look like:
42 // 'filesystem:http://bar.com/external/mount_name/foo/bar':
44 // FileSystemURL('http://bar.com',
45 // kFileSystemTypeExternal,
46 // 'mount_name/foo/bar'
48 // kFileSystemTypeFoo,
49 // '/media/removable/foo/bar');
50 // would create a FileSystemURL whose accessors return:
52 // origin() returns 'http://bar.com',
53 // mount_type() returns kFileSystemTypeExternal,
54 // virtual_path() returns 'mount_name/foo/bar',
55 // type() returns the kFileSystemTypeFoo,
56 // path() returns '/media/removable/foo/bar',
58 // Note that in either case virtual_path() always returns the path part after
59 // 'type' part in the original URL, and mount_type() always returns the 'type'
60 // part in the original URL.
62 // Additionally, following accessors would return valid values:
63 // filesystem_id() returns 'mount_name'.
65 // It is impossible to directly create a valid FileSystemURL instance (except by
66 // using CreatedForTest methods, which should not be used in production code).
67 // To get a valid FileSystemURL, one of the following methods can be used:
68 // <Friend>::CrackURL, <Friend>::CreateCrackedFileSystemURL, where <Friend> is
69 // one of the friended classes.
71 // TODO(ericu): Look into making virtual_path() [and all FileSystem API virtual
72 // paths] just an std::string, to prevent platform-specific base::FilePath
73 // behavior from getting invoked by accident. Currently the base::FilePath
74 // returned here needs special treatment, as it may contain paths that are
75 // illegal on the current platform.
76 // To avoid problems, use VirtualPath::BaseName and
77 // VirtualPath::GetComponents instead of the base::FilePath methods.
78 class STORAGE_EXPORT FileSystemURL
{
83 // Methods for creating FileSystemURL without attempting to crack them.
84 // Should be used only in tests.
85 static FileSystemURL
CreateForTest(const GURL
& url
);
86 static FileSystemURL
CreateForTest(const GURL
& origin
,
87 FileSystemType mount_type
,
88 const base::FilePath
& virtual_path
);
89 static FileSystemURL
CreateForTest(const GURL
& origin
,
90 FileSystemType mount_type
,
91 const base::FilePath
& virtual_path
,
92 const std::string
& mount_filesystem_id
,
93 FileSystemType cracked_type
,
94 const base::FilePath
& cracked_path
,
95 const std::string
& filesystem_id
,
96 const FileSystemMountOption
& mount_option
);
98 // Returns true if this instance represents a valid FileSystem URL.
99 bool is_valid() const { return is_valid_
; }
101 // Returns the origin part of this URL. See the class comment for details.
102 const GURL
& origin() const { return origin_
; }
104 // Returns the type part of this URL. See the class comment for details.
105 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_
; }
110 // Returns the original path part of this URL.
111 // See the class comment for details.
112 // TODO(kinuko): this must return std::string.
113 const base::FilePath
& virtual_path() const { return virtual_path_
; }
115 // Returns the filesystem ID/mount name for isolated/external filesystem URLs.
116 // See the class comment for details.
117 const std::string
& filesystem_id() const { return filesystem_id_
; }
118 const std::string
& mount_filesystem_id() const {
119 return mount_filesystem_id_
;
122 FileSystemType
mount_type() const { return mount_type_
; }
124 const FileSystemMountOption
& mount_option() const { return mount_option_
; }
126 // Returns the formatted URL of this instance.
129 std::string
DebugString() const;
131 // Returns true if this URL is a strict parent of the |child|.
132 bool IsParent(const FileSystemURL
& child
) const;
134 bool IsInSameFileSystem(const FileSystemURL
& other
) const;
136 bool operator==(const FileSystemURL
& that
) const;
138 bool operator!=(const FileSystemURL
& that
) const {
139 return !(*this == that
);
142 struct STORAGE_EXPORT Comparator
{
143 bool operator() (const FileSystemURL
& lhs
, const FileSystemURL
& rhs
) const;
147 friend class FileSystemContext
;
148 friend class ExternalMountPoints
;
149 friend class IsolatedContext
;
151 explicit FileSystemURL(const GURL
& filesystem_url
);
152 FileSystemURL(const GURL
& origin
,
153 FileSystemType mount_type
,
154 const base::FilePath
& virtual_path
);
155 // Creates a cracked FileSystemURL.
156 FileSystemURL(const GURL
& origin
,
157 FileSystemType mount_type
,
158 const base::FilePath
& virtual_path
,
159 const std::string
& mount_filesystem_id
,
160 FileSystemType cracked_type
,
161 const base::FilePath
& cracked_path
,
162 const std::string
& filesystem_id
,
163 const FileSystemMountOption
& mount_option
);
167 // Values parsed from the original URL.
169 FileSystemType mount_type_
;
170 base::FilePath virtual_path_
;
172 // Values obtained by cracking URLs.
173 // |mount_filesystem_id_| is retrieved from the first round of cracking,
174 // and the rest of the fields are from recursive cracking. Permission
175 // checking on the top-level mount information should be done with the former,
176 // and the low-level file operation should be implemented with the latter.
177 std::string mount_filesystem_id_
;
178 FileSystemType type_
;
179 base::FilePath path_
;
180 std::string filesystem_id_
;
181 FileSystemMountOption mount_option_
;
184 typedef std::set
<FileSystemURL
, FileSystemURL::Comparator
> FileSystemURLSet
;
186 } // namespace storage
188 #endif // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_