| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/fileapi/file_system_util.h" | 5 #include "webkit/fileapi/file_system_util.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 #include "net/base/escape.h" | 14 #include "net/base/escape.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 18 #include "webkit/fileapi/file_system_types.h" | 18 #include "webkit/fileapi/file_system_types.h" |
| 19 | 19 |
| 20 namespace fileapi { | 20 namespace fileapi { |
| 21 | 21 |
| 22 const char kPersistentDir[] = "/persistent/"; | 22 const char kPersistentDir[] = "/persistent"; |
| 23 const char kTemporaryDir[] = "/temporary/"; | 23 const char kTemporaryDir[] = "/temporary"; |
| 24 const char kExternalDir[] = "/external/"; | 24 const char kExternalDir[] = "/external"; |
| 25 | 25 |
| 26 const char kPersistentName[] = "Persistent"; | 26 const char kPersistentName[] = "Persistent"; |
| 27 const char kTemporaryName[] = "Temporary"; | 27 const char kTemporaryName[] = "Temporary"; |
| 28 const char kExternalName[] = "External"; | 28 const char kExternalName[] = "External"; |
| 29 | 29 |
| 30 bool CrackFileSystemURL(const GURL& url, GURL* origin_url, FileSystemType* type, | 30 bool CrackFileSystemURL(const GURL& url, GURL* origin_url, FileSystemType* type, |
| 31 FilePath* file_path) { | 31 FilePath* file_path) { |
| 32 GURL origin; | 32 GURL origin; |
| 33 FileSystemType file_system_type; | 33 FileSystemType file_system_type; |
| 34 | 34 |
| 35 if (url.scheme() != "filesystem") | 35 if (!url.is_valid() || !url.SchemeIsFileSystem()) |
| 36 return false; | 36 return false; |
| 37 DCHECK(url.inner_url()); |
| 37 | 38 |
| 38 std::string temp = url.path(); | 39 std::string inner_path = url.inner_url()->path(); |
| 39 // TODO(ericu): This should probably be done elsewhere after the stackable | 40 if (inner_path.compare( |
| 40 // layers are properly in. We're supposed to reject any paths that contain | 41 0, arraysize(kPersistentDir) - 1, kPersistentDir) == 0) { |
| 41 // '..' segments, but the GURL constructor is helpfully resolving them for us. | |
| 42 // Make sure there aren't any before we call it. | |
| 43 size_t pos = temp.find(".."); | |
| 44 for (; pos != std::string::npos; pos = temp.find("..", pos + 1)) { | |
| 45 if ((pos == 0 || temp[pos - 1] == '/') && | |
| 46 (pos == temp.length() - 2 || temp[pos + 2] == '/')) | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 // bare_url will look something like: | |
| 51 // http://example.com/temporary/dir/file.txt. | |
| 52 GURL bare_url(temp); | |
| 53 | |
| 54 // The input URL was malformed, bail out early. | |
| 55 if (bare_url.path().empty()) | |
| 56 return false; | |
| 57 | |
| 58 origin = bare_url.GetOrigin(); | |
| 59 | |
| 60 // The input URL was malformed, bail out early. | |
| 61 if (origin.is_empty()) | |
| 62 return false; | |
| 63 | |
| 64 std::string path = net::UnescapeURLComponent(bare_url.path(), | |
| 65 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS | | |
| 66 net::UnescapeRule::CONTROL_CHARS); | |
| 67 if (path.compare(0, strlen(kPersistentDir), kPersistentDir) == 0) { | |
| 68 file_system_type = kFileSystemTypePersistent; | 42 file_system_type = kFileSystemTypePersistent; |
| 69 path = path.substr(strlen(kPersistentDir)); | 43 } else if (inner_path.compare( |
| 70 } else if (path.compare(0, strlen(kTemporaryDir), kTemporaryDir) == 0) { | 44 0, arraysize(kTemporaryDir) - 1, kTemporaryDir) == 0) { |
| 71 file_system_type = kFileSystemTypeTemporary; | 45 file_system_type = kFileSystemTypeTemporary; |
| 72 path = path.substr(strlen(kTemporaryDir)); | 46 } else if (inner_path.compare( |
| 73 } else if (path.compare(0, strlen(kExternalDir), kExternalDir) == 0) { | 47 0, arraysize(kExternalDir) - 1, kExternalDir) == 0) { |
| 74 file_system_type = kFileSystemTypeExternal; | 48 file_system_type = kFileSystemTypeExternal; |
| 75 path = path.substr(strlen(kExternalDir)); | |
| 76 } else { | 49 } else { |
| 77 return false; | 50 return false; |
| 78 } | 51 } |
| 79 | 52 |
| 53 std::string path = net::UnescapeURLComponent(url.path(), |
| 54 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS | |
| 55 net::UnescapeRule::CONTROL_CHARS); |
| 56 |
| 80 // Ensure the path is relative. | 57 // Ensure the path is relative. |
| 81 while (!path.empty() && path[0] == '/') | 58 while (!path.empty() && path[0] == '/') |
| 82 path.erase(0, 1); | 59 path.erase(0, 1); |
| 83 | 60 |
| 61 FilePath converted_path = FilePath::FromUTF8Unsafe(path); |
| 62 |
| 63 // All parent references should have been resolved in the renderer. |
| 64 if (converted_path.ReferencesParent()) |
| 65 return false; |
| 66 |
| 84 if (origin_url) | 67 if (origin_url) |
| 85 *origin_url = origin; | 68 *origin_url = url.GetOrigin(); |
| 86 if (type) | 69 if (type) |
| 87 *type = file_system_type; | 70 *type = file_system_type; |
| 88 if (file_path) | 71 if (file_path) |
| 89 *file_path = FilePath::FromUTF8Unsafe(path). | 72 *file_path = converted_path.NormalizePathSeparators(). |
| 90 NormalizePathSeparators().StripTrailingSeparators(); | 73 StripTrailingSeparators(); |
| 91 | 74 |
| 92 return true; | 75 return true; |
| 93 } | 76 } |
| 94 | 77 |
| 95 //TODO(ericu): Consider removing support for '\', even on Windows, if possible. | 78 //TODO(ericu): Consider removing support for '\', even on Windows, if possible. |
| 96 // There's a lot of test code that will need reworking, and we may have trouble | 79 // There's a lot of test code that will need reworking, and we may have trouble |
| 97 // with FilePath elsewhere [e.g. DirName and other methods may also need | 80 // with FilePath elsewhere [e.g. DirName and other methods may also need |
| 98 // replacement]. | 81 // replacement]. |
| 99 FilePath VirtualPath::BaseName(const FilePath& virtual_path) { | 82 FilePath VirtualPath::BaseName(const FilePath& virtual_path) { |
| 100 FilePath::StringType path = virtual_path.value(); | 83 FilePath::StringType path = virtual_path.value(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 131 base = BaseName(current); | 114 base = BaseName(current); |
| 132 ret_val.push_back(base.value()); | 115 ret_val.push_back(base.value()); |
| 133 current = current.DirName(); | 116 current = current.DirName(); |
| 134 } | 117 } |
| 135 | 118 |
| 136 *components = | 119 *components = |
| 137 std::vector<FilePath::StringType>(ret_val.rbegin(), ret_val.rend()); | 120 std::vector<FilePath::StringType>(ret_val.rbegin(), ret_val.rend()); |
| 138 } | 121 } |
| 139 | 122 |
| 140 GURL GetFileSystemRootURI(const GURL& origin_url, FileSystemType type) { | 123 GURL GetFileSystemRootURI(const GURL& origin_url, FileSystemType type) { |
| 141 std::string path("filesystem:"); | 124 // origin_url is based on a security origin, so http://foo.com or file:/// |
| 142 path += origin_url.spec(); | 125 // instead of the corresponding filesystem URL. |
| 126 DCHECK(!origin_url.SchemeIsFileSystem()); |
| 127 |
| 128 std::string url = "filesystem:" + origin_url.GetWithEmptyPath().spec(); |
| 143 switch (type) { | 129 switch (type) { |
| 144 case kFileSystemTypeTemporary: | 130 case kFileSystemTypeTemporary: |
| 145 path += (kTemporaryDir + 1); // We don't want the leading slash. | 131 url += (kTemporaryDir + 1); // We don't want the leading slash. |
| 146 break; | 132 break; |
| 147 case kFileSystemTypePersistent: | 133 case kFileSystemTypePersistent: |
| 148 path += (kPersistentDir + 1); // We don't want the leading slash. | 134 url += (kPersistentDir + 1); // We don't want the leading slash. |
| 149 break; | 135 break; |
| 150 case kFileSystemTypeExternal: | 136 case kFileSystemTypeExternal: |
| 151 path += (kExternalDir + 1); // We don't want the leading slash. | 137 url += (kExternalDir + 1); // We don't want the leading slash. |
| 152 break; | 138 break; |
| 153 default: | 139 default: |
| 154 NOTREACHED(); | 140 NOTREACHED(); |
| 155 return GURL(); | 141 return GURL(); |
| 156 } | 142 } |
| 157 return GURL(path); | 143 url += "/"; |
| 144 |
| 145 return GURL(url); |
| 158 } | 146 } |
| 159 | 147 |
| 160 std::string GetFileSystemName(const GURL& origin_url, FileSystemType type) { | 148 std::string GetFileSystemName(const GURL& origin_url, FileSystemType type) { |
| 161 std::string origin_identifier = GetOriginIdentifierFromURL(origin_url); | 149 std::string origin_identifier = GetOriginIdentifierFromURL(origin_url); |
| 162 std::string type_string = GetFileSystemTypeString(type); | 150 std::string type_string = GetFileSystemTypeString(type); |
| 163 DCHECK(!type_string.empty()); | 151 DCHECK(!type_string.empty()); |
| 164 return origin_identifier + ":" + type_string; | 152 return origin_identifier + ":" + type_string; |
| 165 } | 153 } |
| 166 | 154 |
| 167 FileSystemType QuotaStorageTypeToFileSystemType( | 155 FileSystemType QuotaStorageTypeToFileSystemType( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 return fileapi::kPersistentName; | 206 return fileapi::kPersistentName; |
| 219 case kFileSystemTypeExternal: | 207 case kFileSystemTypeExternal: |
| 220 return fileapi::kExternalName; | 208 return fileapi::kExternalName; |
| 221 case kFileSystemTypeUnknown: | 209 case kFileSystemTypeUnknown: |
| 222 default: | 210 default: |
| 223 return std::string(); | 211 return std::string(); |
| 224 } | 212 } |
| 225 } | 213 } |
| 226 | 214 |
| 227 } // namespace fileapi | 215 } // namespace fileapi |
| OLD | NEW |