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(0, strlen(kPersistentDir), kPersistentDir) == 0) { |
40 // layers are properly in. We're supposed to reject any paths that contain | 41 file_system_type = kFileSystemTypePersistent; |
41 // '..' segments, but the GURL constructor is helpfully resolving them for us. | 42 } else if (inner_path.compare(0, strlen(kTemporaryDir), kTemporaryDir) == 0) { |
42 // Make sure there aren't any before we call it. | 43 file_system_type = kFileSystemTypeTemporary; |
43 size_t pos = temp.find(".."); | 44 } else if (inner_path.compare(0, strlen(kExternalDir), kExternalDir) == 0) { |
44 for (; pos != std::string::npos; pos = temp.find("..", pos + 1)) { | 45 file_system_type = kFileSystemTypeExternal; |
45 if ((pos == 0 || temp[pos - 1] == '/') && | 46 } else { |
46 (pos == temp.length() - 2 || temp[pos + 2] == '/')) | 47 return false; |
47 return false; | |
48 } | 48 } |
49 | 49 |
50 // bare_url will look something like: | 50 std::string path = net::UnescapeURLComponent(url.path(), |
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 | | 51 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS | |
66 net::UnescapeRule::CONTROL_CHARS); | 52 net::UnescapeRule::CONTROL_CHARS); |
67 if (path.compare(0, strlen(kPersistentDir), kPersistentDir) == 0) { | 53 |
68 file_system_type = kFileSystemTypePersistent; | 54 // Make sure no '..' segments have leaked through after unescaping. |
69 path = path.substr(strlen(kPersistentDir)); | 55 size_t pos = path.find(".."); |
70 } else if (path.compare(0, strlen(kTemporaryDir), kTemporaryDir) == 0) { | 56 for (; pos != std::string::npos; pos = path.find("..", pos + 1)) { |
71 file_system_type = kFileSystemTypeTemporary; | 57 if ((pos == 0 || path[pos - 1] == '/') && |
72 path = path.substr(strlen(kTemporaryDir)); | 58 (pos == path.length() - 2 || path[pos + 2] == '/')) |
73 } else if (path.compare(0, strlen(kExternalDir), kExternalDir) == 0) { | 59 return false; |
74 file_system_type = kFileSystemTypeExternal; | |
75 path = path.substr(strlen(kExternalDir)); | |
76 } else { | |
77 return false; | |
78 } | 60 } |
79 | 61 |
80 // Ensure the path is relative. | 62 // Ensure the path is relative. |
81 while (!path.empty() && path[0] == '/') | 63 while (!path.empty() && path[0] == '/') |
82 path.erase(0, 1); | 64 path.erase(0, 1); |
83 | 65 |
84 if (origin_url) | 66 if (origin_url) |
85 *origin_url = origin; | 67 *origin_url = url.GetOrigin(); |
86 if (type) | 68 if (type) |
87 *type = file_system_type; | 69 *type = file_system_type; |
88 if (file_path) | 70 if (file_path) |
89 #if defined(OS_WIN) | 71 #if defined(OS_WIN) |
90 *file_path = FilePath(base::SysUTF8ToWide(path)). | 72 *file_path = FilePath(base::SysUTF8ToWide(path)). |
91 NormalizeWindowsPathSeparators().StripTrailingSeparators(); | 73 NormalizeWindowsPathSeparators().StripTrailingSeparators(); |
92 #elif defined(OS_POSIX) | 74 #elif defined(OS_POSIX) |
93 *file_path = FilePath(path).StripTrailingSeparators(); | 75 *file_path = FilePath(path).StripTrailingSeparators(); |
94 #endif | 76 #endif |
95 | 77 |
96 return true; | 78 return true; |
97 } | 79 } |
98 | 80 |
99 GURL GetFileSystemRootURI(const GURL& origin_url, FileSystemType type) { | 81 GURL GetFileSystemRootURI(const GURL& origin_url, FileSystemType type) { |
100 std::string path("filesystem:"); | 82 // origin_url is based on a security origin, so http://foo.com or file:/// |
101 path += origin_url.spec(); | 83 // instead of the corresponding filesystem URL. |
| 84 DCHECK(!origin_url.SchemeIsFileSystem()); |
| 85 |
| 86 std::string inner_url = origin_url.GetWithEmptyPath().spec(); |
102 switch (type) { | 87 switch (type) { |
103 case kFileSystemTypeTemporary: | 88 case kFileSystemTypeTemporary: |
104 path += (kTemporaryDir + 1); // We don't want the leading slash. | 89 inner_url += (kTemporaryDir + 1); // We don't want the leading slash. |
105 break; | 90 break; |
106 case kFileSystemTypePersistent: | 91 case kFileSystemTypePersistent: |
107 path += (kPersistentDir + 1); // We don't want the leading slash. | 92 inner_url += (kPersistentDir + 1); // We don't want the leading slash. |
108 break; | 93 break; |
109 case kFileSystemTypeExternal: | 94 case kFileSystemTypeExternal: |
110 path += (kExternalDir + 1); // We don't want the leading slash. | 95 inner_url += (kExternalDir + 1); // We don't want the leading slash. |
111 break; | 96 break; |
112 default: | 97 default: |
113 NOTREACHED(); | 98 NOTREACHED(); |
114 return GURL(); | 99 return GURL(); |
115 } | 100 } |
116 return GURL(path); | 101 |
| 102 return GURL("filesystem:" + inner_url + "/"); |
117 } | 103 } |
118 | 104 |
119 std::string GetFileSystemName(const GURL& origin_url, FileSystemType type) { | 105 std::string GetFileSystemName(const GURL& origin_url, FileSystemType type) { |
120 std::string origin_identifier = GetOriginIdentifierFromURL(origin_url); | 106 std::string origin_identifier = GetOriginIdentifierFromURL(origin_url); |
121 std::string type_string = GetFileSystemTypeString(type); | 107 std::string type_string = GetFileSystemTypeString(type); |
122 DCHECK(!type_string.empty()); | 108 DCHECK(!type_string.empty()); |
123 return origin_identifier + ":" + type_string; | 109 return origin_identifier + ":" + type_string; |
124 } | 110 } |
125 | 111 |
126 FileSystemType QuotaStorageTypeToFileSystemType( | 112 FileSystemType QuotaStorageTypeToFileSystemType( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 return fileapi::kPersistentName; | 162 return fileapi::kPersistentName; |
177 case kFileSystemTypeExternal: | 163 case kFileSystemTypeExternal: |
178 return fileapi::kExternalName; | 164 return fileapi::kExternalName; |
179 case kFileSystemTypeUnknown: | 165 case kFileSystemTypeUnknown: |
180 default: | 166 default: |
181 return std::string(); | 167 return std::string(); |
182 } | 168 } |
183 } | 169 } |
184 | 170 |
185 } // namespace fileapi | 171 } // namespace fileapi |
OLD | NEW |