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(); |
30 *origin_url = bare_url.GetOrigin(); | 30 // TODO(ericu) remove this code when that ceases to be true, which should be |
| 31 // soon. |
| 32 // On Windows, this will have backslashes for now. |
| 33 // url will look something like: |
| 34 // filesystem:http://example.com/temporary/\dir\file.txt |
| 35 // temp will look something like: |
| 36 // http://example.com/temporary/\dir\file.txt |
| 37 // On posix, url will look something like: |
| 38 // filesystem:http://example.com/temporary/dir/file.txt |
| 39 // temp will look something like: |
| 40 // http://example.com/temporary/dir/file.txt |
| 41 size_t pos = temp.find('\\'); |
| 42 for (; pos != std::string::npos; pos = temp.find('\\', pos + 1)) { |
| 43 temp[pos] = '/'; |
| 44 } |
| 45 // TODO(ericu): This should probably be done elsewhere after the stackable |
| 46 // layers are properly in. We're supposed to reject any paths that contain |
| 47 // '..' segments, but the GURL constructor is helpfully resolving them for us. |
| 48 // Make sure there aren't any before we call it. |
| 49 pos = temp.find(".."); |
| 50 for (; pos != std::string::npos; pos = temp.find("..", pos + 1)) { |
| 51 if ((pos == 0 || temp[pos - 1] == '/') && |
| 52 (pos == temp.length() - 2 || temp[pos + 2] == '/')) |
| 53 return false; |
| 54 } |
| 55 |
| 56 // bare_url will look something like: |
| 57 // http://example.com/temporary//dir/file.txt [on Windows; the double slash |
| 58 // before dir will be single on posix]. |
| 59 GURL bare_url(temp); |
31 | 60 |
32 // The input URL was malformed, bail out early. | 61 // The input URL was malformed, bail out early. |
33 if (origin_url->is_empty() || bare_url.path().empty()) | 62 if (bare_url.path().empty()) |
| 63 return false; |
| 64 |
| 65 origin = bare_url.GetOrigin(); |
| 66 |
| 67 // The input URL was malformed, bail out early. |
| 68 if (origin.is_empty()) |
34 return false; | 69 return false; |
35 | 70 |
36 std::string path = UnescapeURLComponent(bare_url.path(), | 71 std::string path = UnescapeURLComponent(bare_url.path(), |
37 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); | 72 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); |
38 if (path.compare(0, strlen(kPersistentDir), kPersistentDir) == 0) { | 73 if (path.compare(0, strlen(kPersistentDir), kPersistentDir) == 0) { |
39 *type = kFileSystemTypePersistent; | 74 file_system_type = kFileSystemTypePersistent; |
40 path = path.substr(strlen(kPersistentDir)); | 75 path = path.substr(strlen(kPersistentDir)); |
41 } else if (path.compare(0, strlen(kTemporaryDir), kTemporaryDir) == 0) { | 76 } else if (path.compare(0, strlen(kTemporaryDir), kTemporaryDir) == 0) { |
42 *type = kFileSystemTypeTemporary; | 77 file_system_type = kFileSystemTypeTemporary; |
43 path = path.substr(strlen(kTemporaryDir)); | 78 path = path.substr(strlen(kTemporaryDir)); |
44 } else { | 79 } else { |
45 return false; | 80 return false; |
46 } | 81 } |
47 | 82 |
48 // Ensure the path is relative. | 83 // Ensure the path is relative. |
49 while (!path.empty() && path[0] == '/') | 84 while (!path.empty() && path[0] == '/') |
50 path.erase(0, 1); | 85 path.erase(0, 1); |
51 | 86 |
52 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
53 const FilePath::StringType& sys_path = base::SysUTF8ToWide(path); | 88 const FilePath::StringType sys_path = base::SysUTF8ToWide(path); |
54 #elif defined(OS_POSIX) | 89 #elif defined(OS_POSIX) |
55 const FilePath::StringType& sys_path = path; | 90 const FilePath::StringType sys_path = path; |
56 #endif | 91 #endif |
| 92 if (origin_url) |
| 93 *origin_url = origin; |
| 94 if (type) |
| 95 *type = file_system_type; |
| 96 if (file_path) |
| 97 *file_path = FilePath(sys_path); |
57 | 98 |
58 *file_path = FilePath(sys_path); | |
59 return true; | 99 return true; |
60 } | 100 } |
61 | 101 |
| 102 GURL GetFileSystemRootURI( |
| 103 const GURL& origin_url, fileapi::FileSystemType type) { |
| 104 std::string path("filesystem:"); |
| 105 path += origin_url.spec(); |
| 106 switch (type) { |
| 107 case kFileSystemTypeTemporary: |
| 108 path += (kTemporaryDir + 1); // We don't want the leading slash. |
| 109 break; |
| 110 case kFileSystemTypePersistent: |
| 111 path += (kPersistentDir + 1); // We don't want the leading slash. |
| 112 break; |
| 113 default: |
| 114 NOTREACHED(); |
| 115 return GURL(); |
| 116 } |
| 117 return GURL(path); |
| 118 } |
| 119 |
62 } // namespace fileapi | 120 } // namespace fileapi |
OLD | NEW |