[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 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 | |
[email protected] | c6f9203a | 2013-05-28 02:08:07 | [diff] [blame] | 5 | #include "webkit/browser/fileapi/file_system_url.h" |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 6 | |
[email protected] | a349dd94 | 2012-10-15 16:09:13 | [diff] [blame] | 7 | #include <sstream> |
| 8 | |
[email protected] | bf2d3f9 | 2012-09-10 17:14:14 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 3e4ec1b | 2012-09-05 06:45:53 | [diff] [blame] | 10 | #include "base/string_util.h" |
| 11 | #include "net/base/escape.h" |
[email protected] | 61d271f3 | 2013-05-28 04:59:23 | [diff] [blame] | 12 | #include "webkit/common/fileapi/file_system_types.h" |
| 13 | #include "webkit/common/fileapi/file_system_util.h" |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 14 | |
| 15 | namespace fileapi { |
[email protected] | 8f69789 | 2012-10-09 07:18:49 | [diff] [blame] | 16 | |
[email protected] | 3e4ec1b | 2012-09-05 06:45:53 | [diff] [blame] | 17 | namespace { |
[email protected] | 8f69789 | 2012-10-09 07:18:49 | [diff] [blame] | 18 | |
[email protected] | 5b4bd11 | 2013-03-06 15:46:22 | [diff] [blame] | 19 | } // namespace |
| 20 | |
| 21 | FileSystemURL::FileSystemURL() |
| 22 | : is_valid_(false), |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 23 | mount_type_(kFileSystemTypeUnknown), |
| 24 | type_(kFileSystemTypeUnknown) { |
[email protected] | 5b4bd11 | 2013-03-06 15:46:22 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | // static |
| 28 | FileSystemURL FileSystemURL::CreateForTest(const GURL& url) { |
| 29 | return FileSystemURL(url); |
| 30 | } |
| 31 | |
| 32 | FileSystemURL FileSystemURL::CreateForTest(const GURL& origin, |
[email protected] | 3075512b | 2013-05-08 23:21:39 | [diff] [blame] | 33 | FileSystemType mount_type, |
| 34 | const base::FilePath& virtual_path) { |
| 35 | return FileSystemURL(origin, mount_type, virtual_path); |
[email protected] | 5b4bd11 | 2013-03-06 15:46:22 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // static |
| 39 | bool FileSystemURL::ParseFileSystemSchemeURL( |
| 40 | const GURL& url, |
| 41 | GURL* origin_url, |
[email protected] | 3075512b | 2013-05-08 23:21:39 | [diff] [blame] | 42 | FileSystemType* mount_type, |
| 43 | base::FilePath* virtual_path) { |
[email protected] | 3e4ec1b | 2012-09-05 06:45:53 | [diff] [blame] | 44 | GURL origin; |
| 45 | FileSystemType file_system_type = kFileSystemTypeUnknown; |
| 46 | |
| 47 | if (!url.is_valid() || !url.SchemeIsFileSystem()) |
| 48 | return false; |
| 49 | DCHECK(url.inner_url()); |
| 50 | |
| 51 | std::string inner_path = url.inner_url()->path(); |
| 52 | |
| 53 | const struct { |
| 54 | FileSystemType type; |
| 55 | const char* dir; |
| 56 | } kValidTypes[] = { |
| 57 | { kFileSystemTypePersistent, kPersistentDir }, |
| 58 | { kFileSystemTypeTemporary, kTemporaryDir }, |
| 59 | { kFileSystemTypeIsolated, kIsolatedDir }, |
| 60 | { kFileSystemTypeExternal, kExternalDir }, |
| 61 | { kFileSystemTypeTest, kTestDir }, |
| 62 | }; |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 63 | |
[email protected] | 3e4ec1b | 2012-09-05 06:45:53 | [diff] [blame] | 64 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kValidTypes); ++i) { |
| 65 | if (StartsWithASCII(inner_path, kValidTypes[i].dir, true)) { |
| 66 | file_system_type = kValidTypes[i].type; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (file_system_type == kFileSystemTypeUnknown) |
| 72 | return false; |
| 73 | |
| 74 | std::string path = net::UnescapeURLComponent(url.path(), |
| 75 | net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS | |
| 76 | net::UnescapeRule::CONTROL_CHARS); |
| 77 | |
| 78 | // Ensure the path is relative. |
| 79 | while (!path.empty() && path[0] == '/') |
| 80 | path.erase(0, 1); |
| 81 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 82 | base::FilePath converted_path = base::FilePath::FromUTF8Unsafe(path); |
[email protected] | 3e4ec1b | 2012-09-05 06:45:53 | [diff] [blame] | 83 | |
| 84 | // All parent references should have been resolved in the renderer. |
| 85 | if (converted_path.ReferencesParent()) |
| 86 | return false; |
| 87 | |
| 88 | if (origin_url) |
| 89 | *origin_url = url.GetOrigin(); |
[email protected] | 3075512b | 2013-05-08 23:21:39 | [diff] [blame] | 90 | if (mount_type) |
| 91 | *mount_type = file_system_type; |
| 92 | if (virtual_path) |
| 93 | *virtual_path = converted_path.NormalizePathSeparators(). |
[email protected] | 3e4ec1b | 2012-09-05 06:45:53 | [diff] [blame] | 94 | StripTrailingSeparators(); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 99 | FileSystemURL::FileSystemURL(const GURL& url) |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 100 | : mount_type_(kFileSystemTypeUnknown), |
| 101 | type_(kFileSystemTypeUnknown) { |
[email protected] | 3075512b | 2013-05-08 23:21:39 | [diff] [blame] | 102 | is_valid_ = ParseFileSystemSchemeURL(url, &origin_, &mount_type_, |
| 103 | &virtual_path_); |
| 104 | path_ = virtual_path_; |
| 105 | type_ = mount_type_; |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | FileSystemURL::FileSystemURL(const GURL& origin, |
[email protected] | 3075512b | 2013-05-08 23:21:39 | [diff] [blame] | 109 | FileSystemType mount_type, |
| 110 | const base::FilePath& virtual_path) |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 111 | : is_valid_(true), |
| 112 | origin_(origin), |
[email protected] | 3075512b | 2013-05-08 23:21:39 | [diff] [blame] | 113 | mount_type_(mount_type), |
| 114 | virtual_path_(virtual_path.NormalizePathSeparators()), |
| 115 | type_(mount_type), |
| 116 | path_(virtual_path.NormalizePathSeparators()) { |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | FileSystemURL::FileSystemURL(const GURL& origin, |
[email protected] | 04c899f | 2013-02-08 08:28:42 | [diff] [blame] | 120 | FileSystemType mount_type, |
| 121 | const base::FilePath& virtual_path, |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 122 | const std::string& mount_filesystem_id, |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 123 | FileSystemType cracked_type, |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 124 | const base::FilePath& cracked_path, |
| 125 | const std::string& filesystem_id) |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 126 | : is_valid_(true), |
| 127 | origin_(origin), |
[email protected] | 04c899f | 2013-02-08 08:28:42 | [diff] [blame] | 128 | mount_type_(mount_type), |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 129 | virtual_path_(virtual_path.NormalizePathSeparators()), |
| 130 | mount_filesystem_id_(mount_filesystem_id), |
| 131 | type_(cracked_type), |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 132 | path_(cracked_path.NormalizePathSeparators()), |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 133 | filesystem_id_(filesystem_id) { |
[email protected] | d6afd11 | 2012-07-25 22:55:04 | [diff] [blame] | 134 | } |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 135 | |
| 136 | FileSystemURL::~FileSystemURL() {} |
| 137 | |
[email protected] | a349dd94 | 2012-10-15 16:09:13 | [diff] [blame] | 138 | std::string FileSystemURL::DebugString() const { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 139 | if (!is_valid_) |
[email protected] | a349dd94 | 2012-10-15 16:09:13 | [diff] [blame] | 140 | return "invalid filesystem: URL"; |
| 141 | std::ostringstream ss; |
| 142 | ss << GetFileSystemRootURI(origin_, mount_type_); |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 143 | |
| 144 | // filesystem_id_ will be non empty for (and only for) cracked URLs. |
| 145 | if (!filesystem_id_.empty()) { |
[email protected] | a349dd94 | 2012-10-15 16:09:13 | [diff] [blame] | 146 | ss << virtual_path_.value(); |
[email protected] | a349dd94 | 2012-10-15 16:09:13 | [diff] [blame] | 147 | ss << " ("; |
| 148 | ss << GetFileSystemTypeString(type_) << "@" << filesystem_id_ << ":"; |
| 149 | ss << path_.value(); |
| 150 | ss << ")"; |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 151 | } else { |
| 152 | ss << path_.value(); |
[email protected] | a349dd94 | 2012-10-15 16:09:13 | [diff] [blame] | 153 | } |
| 154 | return ss.str(); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 5dca49e | 2012-10-10 14:56:13 | [diff] [blame] | 157 | bool FileSystemURL::IsParent(const FileSystemURL& child) const { |
[email protected] | 4c413de0 | 2013-05-27 06:00:59 | [diff] [blame] | 158 | return IsInSameFileSystem(child) && |
[email protected] | 5dca49e | 2012-10-10 14:56:13 | [diff] [blame] | 159 | path().IsParent(child.path()); |
| 160 | } |
| 161 | |
[email protected] | 4c413de0 | 2013-05-27 06:00:59 | [diff] [blame] | 162 | bool FileSystemURL::IsInSameFileSystem(const FileSystemURL& other) const { |
| 163 | return origin() == other.origin() && |
| 164 | type() == other.type() && |
| 165 | filesystem_id() == other.filesystem_id(); |
| 166 | } |
| 167 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 168 | bool FileSystemURL::operator==(const FileSystemURL& that) const { |
| 169 | return origin_ == that.origin_ && |
| 170 | type_ == that.type_ && |
| 171 | path_ == that.path_ && |
[email protected] | d6afd11 | 2012-07-25 22:55:04 | [diff] [blame] | 172 | filesystem_id_ == that.filesystem_id_ && |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 173 | is_valid_ == that.is_valid_; |
| 174 | } |
| 175 | |
[email protected] | bf2d3f9 | 2012-09-10 17:14:14 | [diff] [blame] | 176 | bool FileSystemURL::Comparator::operator()(const FileSystemURL& lhs, |
| 177 | const FileSystemURL& rhs) const { |
| 178 | DCHECK(lhs.is_valid_ && rhs.is_valid_); |
| 179 | if (lhs.origin_ != rhs.origin_) |
| 180 | return lhs.origin_ < rhs.origin_; |
| 181 | if (lhs.type_ != rhs.type_) |
| 182 | return lhs.type_ < rhs.type_; |
[email protected] | 8f69789 | 2012-10-09 07:18:49 | [diff] [blame] | 183 | if (lhs.filesystem_id_ != rhs.filesystem_id_) |
| 184 | return lhs.filesystem_id_ < rhs.filesystem_id_; |
| 185 | return lhs.path_ < rhs.path_; |
[email protected] | bf2d3f9 | 2012-09-10 17:14:14 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 188 | } // namespace fileapi |