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 WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_
6 #define WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_
11 #include "base/platform_file.h"
13 #include "webkit/browser/webkit_storage_browser_export.h"
14 #include "webkit/common/fileapi/file_system_types.h"
18 // A class representing a filesystem URL which consists of origin URL,
19 // type and an internal path used inside the filesystem.
21 // When a FileSystemURL instance is created for a GURL (for filesystem: scheme),
22 // each accessor method would return following values:
24 // Example: For a URL 'filesystem:http://foo.com/temporary/foo/bar':
25 // origin() returns 'http://foo.com',
26 // mount_type() returns kFileSystemTypeTemporary,
27 // virtual_path() returns 'foo/bar',
28 // type() returns the same value as mount_type(),
29 // path() returns the same value as virtual_path(),
31 // All other accessors return empty or invalid value.
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.
37 // Example: Assume a path '/media/removable' is mounted at mount name
38 // 'mount_name' with type kFileSystemTypeFoo as an external file system.
40 // The original URL would look like:
41 // 'filesystem:http://bar.com/external/mount_name/foo/bar':
43 // FileSystemURL('http://bar.com',
44 // kFileSystemTypeExternal,
45 // 'mount_name/foo/bar'
47 // kFileSystemTypeFoo,
48 // '/media/removable/foo/bar');
49 // would create a FileSystemURL whose accessors return:
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',
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.
61 // Additionally, following accessors would return valid values:
62 // filesystem_id() returns 'mount_name'.
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.
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 WEBKIT_STORAGE_BROWSER_EXPORT FileSystemURL
{
82 // Methods for creating FileSystemURL without attempting to crack them.
83 // Should be used only in tests.
84 static FileSystemURL
CreateForTest(const GURL
& url
);
85 static FileSystemURL
CreateForTest(const GURL
& origin
,
86 FileSystemType mount_type
,
87 const base::FilePath
& virtual_path
);
89 // Parses filesystem scheme |url| into uncracked FileSystemURL components.
90 static bool ParseFileSystemSchemeURL(const GURL
& url
,
92 FileSystemType
* mount_type
,
93 base::FilePath
* virtual_path
);
95 // Returns true if this instance represents a valid FileSystem URL.
96 bool is_valid() const { return is_valid_
; }
98 // Returns the origin part of this URL. See the class comment for details.
99 const GURL
& origin() const { return origin_
; }
101 // Returns the type part of this URL. See the class comment for details.
102 FileSystemType
type() const { return type_
; }
104 // Returns the cracked path of this URL. See the class comment for details.
105 const base::FilePath
& path() const { return path_
; }
107 // Returns the original path part of this URL.
108 // See the class comment for details.
109 // TODO(kinuko): this must return std::string.
110 const base::FilePath
& virtual_path() const { return virtual_path_
; }
112 // Returns the filesystem ID/mount name for isolated/external filesystem URLs.
113 // See the class comment for details.
114 const std::string
& filesystem_id() const { return filesystem_id_
; }
115 const std::string
& mount_filesystem_id() const {
116 return mount_filesystem_id_
;
119 FileSystemType
mount_type() const { return mount_type_
; }
121 // Returns the formatted URL of this instance.
124 std::string
DebugString() const;
126 // Returns true if this URL is a strict parent of the |child|.
127 bool IsParent(const FileSystemURL
& child
) const;
129 bool IsInSameFileSystem(const FileSystemURL
& other
) const;
131 bool operator==(const FileSystemURL
& that
) const;
133 bool operator!=(const FileSystemURL
& that
) const {
134 return !(*this == that
);
137 struct WEBKIT_STORAGE_BROWSER_EXPORT Comparator
{
138 bool operator() (const FileSystemURL
& lhs
, const FileSystemURL
& rhs
) const;
142 friend class FileSystemContext
;
143 friend class ExternalMountPoints
;
144 friend class IsolatedContext
;
146 explicit FileSystemURL(const GURL
& filesystem_url
);
147 FileSystemURL(const GURL
& origin
,
148 FileSystemType mount_type
,
149 const base::FilePath
& virtual_path
);
150 // Creates a cracked FileSystemURL.
151 FileSystemURL(const GURL
& origin
,
152 FileSystemType mount_type
,
153 const base::FilePath
& virtual_path
,
154 const std::string
& mount_filesystem_id
,
155 FileSystemType cracked_type
,
156 const base::FilePath
& cracked_path
,
157 const std::string
& filesystem_id
);
161 // Values parsed from the original URL.
163 FileSystemType mount_type_
;
164 base::FilePath virtual_path_
;
166 // Values obtained by cracking URLs.
167 // |mount_filesystem_id_| is retrieved from the first round of cracking,
168 // and the rest of the fields are from recursive cracking. Permission
169 // checking on the top-level mount information should be done with the former,
170 // and the low-level file operation should be implemented with the latter.
171 std::string mount_filesystem_id_
;
172 FileSystemType type_
;
173 base::FilePath path_
;
174 std::string filesystem_id_
;
177 typedef std::set
<FileSystemURL
, FileSystemURL::Comparator
> FileSystemURLSet
;
179 } // namespace fileapi
181 #endif // WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_