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

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: Addressed code review feedback. Created 8 years, 10 months 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) 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) {
kinuko 2012/02/17 23:47:37 nit: I think we can use arraysize here
ericu 2012/02/22 00:00:51 Done.
40 // layers are properly in. We're supposed to reject any paths that contain
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; 41 file_system_type = kFileSystemTypePersistent;
69 path = path.substr(strlen(kPersistentDir)); 42 } else if (inner_path.compare(0, strlen(kTemporaryDir), kTemporaryDir) == 0) {
70 } else if (path.compare(0, strlen(kTemporaryDir), kTemporaryDir) == 0) {
71 file_system_type = kFileSystemTypeTemporary; 43 file_system_type = kFileSystemTypeTemporary;
72 path = path.substr(strlen(kTemporaryDir)); 44 } else if (inner_path.compare(0, strlen(kExternalDir), kExternalDir) == 0) {
73 } else if (path.compare(0, strlen(kExternalDir), kExternalDir) == 0) {
74 file_system_type = kFileSystemTypeExternal; 45 file_system_type = kFileSystemTypeExternal;
75 path = path.substr(strlen(kExternalDir));
76 } else { 46 } else {
77 return false; 47 return false;
78 } 48 }
79 49
50 std::string path = net::UnescapeURLComponent(url.path(),
51 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS |
52 net::UnescapeRule::CONTROL_CHARS);
53
80 // Ensure the path is relative. 54 // Ensure the path is relative.
81 while (!path.empty() && path[0] == '/') 55 while (!path.empty() && path[0] == '/')
82 path.erase(0, 1); 56 path.erase(0, 1);
83 57
58 FilePath converted_path = FilePath::FromUTF8Unsafe(path);
59
60 // All parent references should have been resolved in the renderer.
61 if (converted_path.ReferencesParent())
62 return false;
63
84 if (origin_url) 64 if (origin_url)
85 *origin_url = origin; 65 *origin_url = url.GetOrigin();
86 if (type) 66 if (type)
87 *type = file_system_type; 67 *type = file_system_type;
88 if (file_path) 68 if (file_path)
89 *file_path = FilePath::FromUTF8Unsafe(path). 69 *file_path = converted_path.NormalizePathSeparators().
90 NormalizePathSeparators().StripTrailingSeparators(); 70 StripTrailingSeparators();
91 71
92 return true; 72 return true;
93 } 73 }
94 74
95 GURL GetFileSystemRootURI(const GURL& origin_url, FileSystemType type) { 75 GURL GetFileSystemRootURI(const GURL& origin_url, FileSystemType type) {
96 std::string path("filesystem:"); 76 // origin_url is based on a security origin, so http://foo.com or file:///
97 path += origin_url.spec(); 77 // instead of the corresponding filesystem URL.
78 DCHECK(!origin_url.SchemeIsFileSystem());
79
80 std::string url = "filesystem:" + origin_url.GetWithEmptyPath().spec();
98 switch (type) { 81 switch (type) {
99 case kFileSystemTypeTemporary: 82 case kFileSystemTypeTemporary:
100 path += (kTemporaryDir + 1); // We don't want the leading slash. 83 url += (kTemporaryDir + 1); // We don't want the leading slash.
101 break; 84 break;
102 case kFileSystemTypePersistent: 85 case kFileSystemTypePersistent:
103 path += (kPersistentDir + 1); // We don't want the leading slash. 86 url += (kPersistentDir + 1); // We don't want the leading slash.
104 break; 87 break;
105 case kFileSystemTypeExternal: 88 case kFileSystemTypeExternal:
106 path += (kExternalDir + 1); // We don't want the leading slash. 89 url += (kExternalDir + 1); // We don't want the leading slash.
107 break; 90 break;
108 default: 91 default:
109 NOTREACHED(); 92 NOTREACHED();
110 return GURL(); 93 return GURL();
111 } 94 }
112 return GURL(path); 95 url += "/";
96
97 return GURL(url);
113 } 98 }
114 99
115 std::string GetFileSystemName(const GURL& origin_url, FileSystemType type) { 100 std::string GetFileSystemName(const GURL& origin_url, FileSystemType type) {
116 std::string origin_identifier = GetOriginIdentifierFromURL(origin_url); 101 std::string origin_identifier = GetOriginIdentifierFromURL(origin_url);
117 std::string type_string = GetFileSystemTypeString(type); 102 std::string type_string = GetFileSystemTypeString(type);
118 DCHECK(!type_string.empty()); 103 DCHECK(!type_string.empty());
119 return origin_identifier + ":" + type_string; 104 return origin_identifier + ":" + type_string;
120 } 105 }
121 106
122 FileSystemType QuotaStorageTypeToFileSystemType( 107 FileSystemType QuotaStorageTypeToFileSystemType(
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 return fileapi::kPersistentName; 157 return fileapi::kPersistentName;
173 case kFileSystemTypeExternal: 158 case kFileSystemTypeExternal:
174 return fileapi::kExternalName; 159 return fileapi::kExternalName;
175 case kFileSystemTypeUnknown: 160 case kFileSystemTypeUnknown:
176 default: 161 default:
177 return std::string(); 162 return std::string();
178 } 163 }
179 } 164 }
180 165
181 } // namespace fileapi 166 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698