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 GURL GetFileSystemRootURI(const GURL& origin_url, FileSystemType type) { | 78 GURL GetFileSystemRootURI(const GURL& origin_url, FileSystemType type) { |
96 std::string path("filesystem:"); | 79 // origin_url is based on a security origin, so http://foo.com or file:/// |
97 path += origin_url.spec(); | 80 // instead of the corresponding filesystem URL. |
| 81 DCHECK(!origin_url.SchemeIsFileSystem()); |
| 82 |
| 83 std::string url = "filesystem:" + origin_url.GetWithEmptyPath().spec(); |
98 switch (type) { | 84 switch (type) { |
99 case kFileSystemTypeTemporary: | 85 case kFileSystemTypeTemporary: |
100 path += (kTemporaryDir + 1); // We don't want the leading slash. | 86 url += (kTemporaryDir + 1); // We don't want the leading slash. |
101 break; | 87 break; |
102 case kFileSystemTypePersistent: | 88 case kFileSystemTypePersistent: |
103 path += (kPersistentDir + 1); // We don't want the leading slash. | 89 url += (kPersistentDir + 1); // We don't want the leading slash. |
104 break; | 90 break; |
105 case kFileSystemTypeExternal: | 91 case kFileSystemTypeExternal: |
106 path += (kExternalDir + 1); // We don't want the leading slash. | 92 url += (kExternalDir + 1); // We don't want the leading slash. |
107 break; | 93 break; |
108 default: | 94 default: |
109 NOTREACHED(); | 95 NOTREACHED(); |
110 return GURL(); | 96 return GURL(); |
111 } | 97 } |
112 return GURL(path); | 98 url += "/"; |
| 99 |
| 100 return GURL(url); |
113 } | 101 } |
114 | 102 |
115 std::string GetFileSystemName(const GURL& origin_url, FileSystemType type) { | 103 std::string GetFileSystemName(const GURL& origin_url, FileSystemType type) { |
116 std::string origin_identifier = GetOriginIdentifierFromURL(origin_url); | 104 std::string origin_identifier = GetOriginIdentifierFromURL(origin_url); |
117 std::string type_string = GetFileSystemTypeString(type); | 105 std::string type_string = GetFileSystemTypeString(type); |
118 DCHECK(!type_string.empty()); | 106 DCHECK(!type_string.empty()); |
119 return origin_identifier + ":" + type_string; | 107 return origin_identifier + ":" + type_string; |
120 } | 108 } |
121 | 109 |
122 FileSystemType QuotaStorageTypeToFileSystemType( | 110 FileSystemType QuotaStorageTypeToFileSystemType( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 return fileapi::kPersistentName; | 160 return fileapi::kPersistentName; |
173 case kFileSystemTypeExternal: | 161 case kFileSystemTypeExternal: |
174 return fileapi::kExternalName; | 162 return fileapi::kExternalName; |
175 case kFileSystemTypeUnknown: | 163 case kFileSystemTypeUnknown: |
176 default: | 164 default: |
177 return std::string(); | 165 return std::string(); |
178 } | 166 } |
179 } | 167 } |
180 | 168 |
181 } // namespace fileapi | 169 } // namespace fileapi |
OLD | NEW |