Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: webkit/fileapi/file_system_util.cc

Issue 7811006: Add full support for filesystem URLs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Better content_settings_pattern.cc changes. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/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( 81 GURL GetFileSystemRootURI(
100 const GURL& origin_url, fileapi::FileSystemType type) { 82 const GURL& origin_url, fileapi::FileSystemType type) {
101 std::string path("filesystem:"); 83 DCHECK(!origin_url.SchemeIsFileSystem());
102 path += origin_url.spec(); 84
85 std::string inner_url = origin_url.GetWithEmptyPath().spec();
103 switch (type) { 86 switch (type) {
104 case kFileSystemTypeTemporary: 87 case kFileSystemTypeTemporary:
105 path += (kTemporaryDir + 1); // We don't want the leading slash. 88 inner_url += (kTemporaryDir + 1); // We don't want the leading slash.
106 break; 89 break;
107 case kFileSystemTypePersistent: 90 case kFileSystemTypePersistent:
108 path += (kPersistentDir + 1); // We don't want the leading slash. 91 inner_url += (kPersistentDir + 1); // We don't want the leading slash.
109 break; 92 break;
110 case kFileSystemTypeExternal: 93 case kFileSystemTypeExternal:
111 path += (kExternalDir + 1); // We don't want the leading slash. 94 inner_url += (kExternalDir + 1); // We don't want the leading slash.
112 break; 95 break;
113 default: 96 default:
114 NOTREACHED(); 97 NOTREACHED();
115 return GURL(); 98 return GURL();
116 } 99 }
117 return GURL(path); 100
101 return GURL("filesystem:" + inner_url + "/");
118 } 102 }
119 103
120 FileSystemType QuotaStorageTypeToFileSystemType( 104 FileSystemType QuotaStorageTypeToFileSystemType(
121 quota::StorageType storage_type) { 105 quota::StorageType storage_type) {
122 switch (storage_type) { 106 switch (storage_type) {
123 case quota::kStorageTypeTemporary: 107 case quota::kStorageTypeTemporary:
124 return kFileSystemTypeTemporary; 108 return kFileSystemTypeTemporary;
125 case quota::kStorageTypePersistent: 109 case quota::kStorageTypePersistent:
126 return kFileSystemTypePersistent; 110 return kFileSystemTypePersistent;
127 default: 111 default:
(...skipping 28 matching lines...) Expand all
156 140
157 // We need this work-around for file:/// URIs as 141 // We need this work-around for file:/// URIs as
158 // createFromDatabaseIdentifier returns empty origin_url for them. 142 // createFromDatabaseIdentifier returns empty origin_url for them.
159 if (origin_url.spec().empty() && 143 if (origin_url.spec().empty() &&
160 origin_identifier.find("file__") == 0) 144 origin_identifier.find("file__") == 0)
161 return GURL("file:///"); 145 return GURL("file:///");
162 return origin_url; 146 return origin_url;
163 } 147 }
164 148
165 } // namespace fileapi 149 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698