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 WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_ 6 #define WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "base/platform_file.h" 12 #include "url/gurl.h" 13 #include "webkit/browser/webkit_storage_browser_export.h" 14 #include "webkit/common/fileapi/file_system_mount_option.h" 15 #include "webkit/common/fileapi/file_system_types.h" 16 17 namespace fileapi { 18 19 // A class representing a filesystem URL which consists of origin URL, 20 // type and an internal path used inside the filesystem. 21 // 22 // When a FileSystemURL instance is created for a GURL (for filesystem: scheme), 23 // each accessor method would return following values: 24 // 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(), 31 // 32 // All other accessors return empty or invalid value. 33 // 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. 37 // 38 // Example: Assume a path '/media/removable' is mounted at mount name 39 // 'mount_name' with type kFileSystemTypeFoo as an external file system. 40 // 41 // The original URL would look like: 42 // 'filesystem:http://bar.com/external/mount_name/foo/bar': 43 // 44 // FileSystemURL('http://bar.com', 45 // kFileSystemTypeExternal, 46 // 'mount_name/foo/bar' 47 // 'mount_name', 48 // kFileSystemTypeFoo, 49 // '/media/removable/foo/bar'); 50 // would create a FileSystemURL whose accessors return: 51 // 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', 57 // 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. 61 // 62 // Additionally, following accessors would return valid values: 63 // filesystem_id() returns 'mount_name'. 64 // 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. 70 // 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 WEBKIT_STORAGE_BROWSER_EXPORT FileSystemURL { 79 public: 80 FileSystemURL(); 81 ~FileSystemURL(); 82 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 90 // Parses filesystem scheme |url| into uncracked FileSystemURL components. 91 static bool ParseFileSystemSchemeURL(const GURL& url, 92 GURL* origin, 93 FileSystemType* mount_type, 94 base::FilePath* virtual_path); 95 96 // Returns true if this instance represents a valid FileSystem URL. is_valid()97 bool is_valid() const { return is_valid_; } 98 99 // Returns the origin part of this URL. See the class comment for details. origin()100 const GURL& origin() const { return origin_; } 101 102 // Returns the type part of this URL. See the class comment for details. type()103 FileSystemType type() const { return type_; } 104 105 // Returns the cracked path of this URL. See the class comment for details. path()106 const base::FilePath& path() const { return path_; } 107 108 // Returns the original path part of this URL. 109 // See the class comment for details. 110 // TODO(kinuko): this must return std::string. virtual_path()111 const base::FilePath& virtual_path() const { return virtual_path_; } 112 113 // Returns the filesystem ID/mount name for isolated/external filesystem URLs. 114 // See the class comment for details. filesystem_id()115 const std::string& filesystem_id() const { return filesystem_id_; } mount_filesystem_id()116 const std::string& mount_filesystem_id() const { 117 return mount_filesystem_id_; 118 } 119 mount_type()120 FileSystemType mount_type() const { return mount_type_; } 121 mount_option()122 const FileSystemMountOption& mount_option() const { return mount_option_; } 123 124 // Returns the formatted URL of this instance. 125 GURL ToGURL() const; 126 127 std::string DebugString() const; 128 129 // Returns true if this URL is a strict parent of the |child|. 130 bool IsParent(const FileSystemURL& child) const; 131 132 bool IsInSameFileSystem(const FileSystemURL& other) const; 133 134 bool operator==(const FileSystemURL& that) const; 135 136 bool operator!=(const FileSystemURL& that) const { 137 return !(*this == that); 138 } 139 140 struct WEBKIT_STORAGE_BROWSER_EXPORT Comparator { 141 bool operator() (const FileSystemURL& lhs, const FileSystemURL& rhs) const; 142 }; 143 144 private: 145 friend class FileSystemContext; 146 friend class ExternalMountPoints; 147 friend class IsolatedContext; 148 149 explicit FileSystemURL(const GURL& filesystem_url); 150 FileSystemURL(const GURL& origin, 151 FileSystemType mount_type, 152 const base::FilePath& virtual_path); 153 // Creates a cracked FileSystemURL. 154 FileSystemURL(const GURL& origin, 155 FileSystemType mount_type, 156 const base::FilePath& virtual_path, 157 const std::string& mount_filesystem_id, 158 FileSystemType cracked_type, 159 const base::FilePath& cracked_path, 160 const std::string& filesystem_id, 161 const FileSystemMountOption& mount_option); 162 163 bool is_valid_; 164 165 // Values parsed from the original URL. 166 GURL origin_; 167 FileSystemType mount_type_; 168 base::FilePath virtual_path_; 169 170 // Values obtained by cracking URLs. 171 // |mount_filesystem_id_| is retrieved from the first round of cracking, 172 // and the rest of the fields are from recursive cracking. Permission 173 // checking on the top-level mount information should be done with the former, 174 // and the low-level file operation should be implemented with the latter. 175 std::string mount_filesystem_id_; 176 FileSystemType type_; 177 base::FilePath path_; 178 std::string filesystem_id_; 179 FileSystemMountOption mount_option_; 180 }; 181 182 typedef std::set<FileSystemURL, FileSystemURL::Comparator> FileSystemURLSet; 183 184 } // namespace fileapi 185 186 #endif // WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_ 187