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 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" 15 #include "url/gurl.h" 16 17 namespace storage { 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 STORAGE_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 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); 97 98 // Returns true if this instance represents a valid FileSystem URL. is_valid()99 bool is_valid() const { return is_valid_; } 100 101 // Returns the origin part of this URL. See the class comment for details. origin()102 const GURL& origin() const { return origin_; } 103 104 // Returns the type part of this URL. See the class comment for details. type()105 FileSystemType type() const { return type_; } 106 107 // Returns the cracked path of this URL. See the class comment for details. path()108 const base::FilePath& path() const { return path_; } 109 110 // Returns the original path part of this URL. 111 // See the class comment for details. 112 // TODO(kinuko): this must return std::string. virtual_path()113 const base::FilePath& virtual_path() const { return virtual_path_; } 114 115 // Returns the filesystem ID/mount name for isolated/external filesystem URLs. 116 // See the class comment for details. filesystem_id()117 const std::string& filesystem_id() const { return filesystem_id_; } mount_filesystem_id()118 const std::string& mount_filesystem_id() const { 119 return mount_filesystem_id_; 120 } 121 mount_type()122 FileSystemType mount_type() const { return mount_type_; } 123 mount_option()124 const FileSystemMountOption& mount_option() const { return mount_option_; } 125 126 // Returns the formatted URL of this instance. 127 GURL ToGURL() const; 128 129 std::string DebugString() const; 130 131 // Returns true if this URL is a strict parent of the |child|. 132 bool IsParent(const FileSystemURL& child) const; 133 134 bool IsInSameFileSystem(const FileSystemURL& other) const; 135 136 bool operator==(const FileSystemURL& that) const; 137 138 bool operator!=(const FileSystemURL& that) const { 139 return !(*this == that); 140 } 141 142 struct STORAGE_EXPORT Comparator { 143 bool operator() (const FileSystemURL& lhs, const FileSystemURL& rhs) const; 144 }; 145 146 private: 147 friend class FileSystemContext; 148 friend class ExternalMountPoints; 149 friend class IsolatedContext; 150 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); 164 165 bool is_valid_; 166 167 // Values parsed from the original URL. 168 GURL origin_; 169 FileSystemType mount_type_; 170 base::FilePath virtual_path_; 171 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_; 182 }; 183 184 typedef std::set<FileSystemURL, FileSystemURL::Comparator> FileSystemURLSet; 185 186 } // namespace storage 187 188 #endif // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_URL_H_ 189