OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
11 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
12 #include "net/base/escape.h" | 13 #include "net/base/escape.h" |
13 #include "webkit/fileapi/file_system_types.h" | 14 #include "webkit/fileapi/file_system_types.h" |
14 | 15 |
15 namespace fileapi { | 16 namespace fileapi { |
16 | 17 |
17 static const char kPersistentDir[] = "/persistent/"; | 18 static const char kPersistentDir[] = "/persistent/"; |
18 static const char kTemporaryDir[] = "/temporary/"; | 19 static const char kTemporaryDir[] = "/temporary/"; |
19 | 20 |
20 bool CrackFileSystemURL(const GURL& url, GURL* origin_url, FileSystemType* type, | 21 bool CrackFileSystemURL(const GURL& url, GURL* origin_url, FileSystemType* type, |
21 FilePath* file_path) { | 22 FilePath* file_path) { |
22 *origin_url = GURL(); | 23 GURL origin; |
23 *type = kFileSystemTypeUnknown; | 24 FileSystemType file_system_type; |
24 *file_path = FilePath(); | |
25 | 25 |
26 if (url.scheme() != "filesystem") | 26 if (url.scheme() != "filesystem") |
27 return false; | 27 return false; |
28 | 28 |
29 GURL bare_url(url.path()); | 29 std::string temp = url.path(); |
kinuko
2011/03/14 11:03:57
Can't we create a new FilePath (e.g. by FilePath()
ericu
2011/03/15 02:43:11
url.path() is something like "file:///temporary/\t
kinuko
2011/03/16 19:33:38
I see, it's a bit sad though. How about using Fil
ericu
2011/03/16 23:47:31
Using FilePath wouldn't shrink the code much. Fir
| |
30 *origin_url = bare_url.GetOrigin(); | 30 // On Windows, this will have backslashes for now. TODO(ericu) remove this |
31 // code when that ceases to be true, which should be soon. | |
32 size_t pos = temp.find('\\'); | |
33 for (; pos != std::string::npos; pos = temp.find('\\', pos + 1)) { | |
34 temp[pos] = '/'; | |
35 } | |
36 // TODO(ericu): This should probably be done elsewhere after the stackable | |
37 // layers are properly in. We're supposed to reject any paths that contain | |
38 // '..' segments, but the GURL constructor is helpfully resolving them for us. | |
39 // Make sure there aren't any before we call it. | |
40 pos = temp.find(".."); | |
41 for (; pos != std::string::npos; pos = temp.find("..", pos + 1)) { | |
42 if ((pos == 0 || temp[pos - 1] == '/') && | |
43 (pos == temp.length() - 2 || temp[pos + 2] == '/')) | |
44 return false; | |
45 } | |
46 | |
47 GURL bare_url(temp); | |
kinuko
2011/03/14 11:03:57
It might be great to have a brief comment to expla
ericu
2011/03/15 02:43:11
Good point. Done.
| |
48 // The input URL was malformed, bail out early. | |
49 if (bare_url.path().empty()) | |
50 return false; | |
51 | |
52 origin = bare_url.GetOrigin(); | |
31 | 53 |
32 // The input URL was malformed, bail out early. | 54 // The input URL was malformed, bail out early. |
33 if (origin_url->is_empty() || bare_url.path().empty()) | 55 if (origin.is_empty()) |
34 return false; | 56 return false; |
35 | 57 |
36 std::string path = UnescapeURLComponent(bare_url.path(), | 58 std::string path = UnescapeURLComponent(bare_url.path(), |
37 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); | 59 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); |
38 if (path.compare(0, strlen(kPersistentDir), kPersistentDir) == 0) { | 60 if (path.compare(0, strlen(kPersistentDir), kPersistentDir) == 0) { |
39 *type = kFileSystemTypePersistent; | 61 file_system_type = kFileSystemTypePersistent; |
40 path = path.substr(strlen(kPersistentDir)); | 62 path = path.substr(strlen(kPersistentDir)); |
41 } else if (path.compare(0, strlen(kTemporaryDir), kTemporaryDir) == 0) { | 63 } else if (path.compare(0, strlen(kTemporaryDir), kTemporaryDir) == 0) { |
42 *type = kFileSystemTypeTemporary; | 64 file_system_type = kFileSystemTypeTemporary; |
43 path = path.substr(strlen(kTemporaryDir)); | 65 path = path.substr(strlen(kTemporaryDir)); |
44 } else { | 66 } else { |
45 return false; | 67 return false; |
46 } | 68 } |
47 | 69 |
48 // Ensure the path is relative. | 70 // Ensure the path is relative. |
49 while (!path.empty() && path[0] == '/') | 71 while (!path.empty() && path[0] == '/') |
50 path.erase(0, 1); | 72 path.erase(0, 1); |
51 | 73 |
52 #if defined(OS_WIN) | 74 #if defined(OS_WIN) |
53 const FilePath::StringType& sys_path = base::SysUTF8ToWide(path); | 75 const FilePath::StringType sys_path = base::SysUTF8ToWide(path); |
54 #elif defined(OS_POSIX) | 76 #elif defined(OS_POSIX) |
55 const FilePath::StringType& sys_path = path; | 77 const FilePath::StringType sys_path = path; |
56 #endif | 78 #endif |
79 if (origin_url) | |
80 *origin_url = origin; | |
81 if (type) | |
82 *type = file_system_type; | |
83 if (file_path) | |
84 *file_path = FilePath(sys_path); | |
57 | 85 |
58 *file_path = FilePath(sys_path); | |
59 return true; | 86 return true; |
60 } | 87 } |
61 | 88 |
89 GURL GetFileSystemRootURI( | |
90 const GURL& origin_url, fileapi::FileSystemType type) { | |
91 std::string path("filesystem:"); | |
92 path += origin_url.spec(); | |
93 switch (type) { | |
94 case kFileSystemTypeTemporary: | |
95 path += (kTemporaryDir + 1); // We don't want the leading slash. | |
96 break; | |
97 case kFileSystemTypePersistent: | |
98 path += (kPersistentDir + 1); // We don't want the leading slash. | |
99 break; | |
100 default: | |
101 NOTREACHED(); | |
102 return GURL(); | |
103 } | |
104 return GURL(path); | |
105 } | |
106 | |
62 } // namespace fileapi | 107 } // namespace fileapi |
OLD | NEW |