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

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

Issue 15658004: Split FileAPI code for common|common_child|renderer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "webkit/fileapi/file_system_url.h"
6
7 #include <sstream>
8
9 #include "base/logging.h"
10 #include "base/string_util.h"
11 #include "net/base/escape.h"
12 #include "webkit/fileapi/external_mount_points.h"
13 #include "webkit/fileapi/file_system_types.h"
14 #include "webkit/fileapi/file_system_util.h"
15 #include "webkit/fileapi/isolated_context.h"
16
17 namespace fileapi {
18
19 namespace {
20
21 } // namespace
22
23 FileSystemURL::FileSystemURL()
24 : is_valid_(false),
25 mount_type_(kFileSystemTypeUnknown),
26 type_(kFileSystemTypeUnknown) {
27 }
28
29 // static
30 FileSystemURL FileSystemURL::CreateForTest(const GURL& url) {
31 return FileSystemURL(url);
32 }
33
34 FileSystemURL FileSystemURL::CreateForTest(const GURL& origin,
35 FileSystemType mount_type,
36 const base::FilePath& virtual_path) {
37 return FileSystemURL(origin, mount_type, virtual_path);
38 }
39
40 // static
41 bool FileSystemURL::ParseFileSystemSchemeURL(
42 const GURL& url,
43 GURL* origin_url,
44 FileSystemType* mount_type,
45 base::FilePath* virtual_path) {
46 GURL origin;
47 FileSystemType file_system_type = kFileSystemTypeUnknown;
48
49 if (!url.is_valid() || !url.SchemeIsFileSystem())
50 return false;
51 DCHECK(url.inner_url());
52
53 std::string inner_path = url.inner_url()->path();
54
55 const struct {
56 FileSystemType type;
57 const char* dir;
58 } kValidTypes[] = {
59 { kFileSystemTypePersistent, kPersistentDir },
60 { kFileSystemTypeTemporary, kTemporaryDir },
61 { kFileSystemTypeIsolated, kIsolatedDir },
62 { kFileSystemTypeExternal, kExternalDir },
63 { kFileSystemTypeTest, kTestDir },
64 };
65
66 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kValidTypes); ++i) {
67 if (StartsWithASCII(inner_path, kValidTypes[i].dir, true)) {
68 file_system_type = kValidTypes[i].type;
69 break;
70 }
71 }
72
73 if (file_system_type == kFileSystemTypeUnknown)
74 return false;
75
76 std::string path = net::UnescapeURLComponent(url.path(),
77 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS |
78 net::UnescapeRule::CONTROL_CHARS);
79
80 // Ensure the path is relative.
81 while (!path.empty() && path[0] == '/')
82 path.erase(0, 1);
83
84 base::FilePath converted_path = base::FilePath::FromUTF8Unsafe(path);
85
86 // All parent references should have been resolved in the renderer.
87 if (converted_path.ReferencesParent())
88 return false;
89
90 if (origin_url)
91 *origin_url = url.GetOrigin();
92 if (mount_type)
93 *mount_type = file_system_type;
94 if (virtual_path)
95 *virtual_path = converted_path.NormalizePathSeparators().
96 StripTrailingSeparators();
97
98 return true;
99 }
100
101 FileSystemURL::FileSystemURL(const GURL& url)
102 : mount_type_(kFileSystemTypeUnknown),
103 type_(kFileSystemTypeUnknown) {
104 is_valid_ = ParseFileSystemSchemeURL(url, &origin_, &mount_type_,
105 &virtual_path_);
106 path_ = virtual_path_;
107 type_ = mount_type_;
108 }
109
110 FileSystemURL::FileSystemURL(const GURL& origin,
111 FileSystemType mount_type,
112 const base::FilePath& virtual_path)
113 : is_valid_(true),
114 origin_(origin),
115 mount_type_(mount_type),
116 virtual_path_(virtual_path.NormalizePathSeparators()),
117 type_(mount_type),
118 path_(virtual_path.NormalizePathSeparators()) {
119 }
120
121 FileSystemURL::FileSystemURL(const GURL& origin,
122 FileSystemType mount_type,
123 const base::FilePath& virtual_path,
124 const std::string& mount_filesystem_id,
125 FileSystemType cracked_type,
126 const base::FilePath& cracked_path,
127 const std::string& filesystem_id)
128 : is_valid_(true),
129 origin_(origin),
130 mount_type_(mount_type),
131 virtual_path_(virtual_path.NormalizePathSeparators()),
132 mount_filesystem_id_(mount_filesystem_id),
133 type_(cracked_type),
134 path_(cracked_path.NormalizePathSeparators()),
135 filesystem_id_(filesystem_id) {
136 }
137
138 FileSystemURL::~FileSystemURL() {}
139
140 std::string FileSystemURL::DebugString() const {
141 if (!is_valid_)
142 return "invalid filesystem: URL";
143 std::ostringstream ss;
144 ss << GetFileSystemRootURI(origin_, mount_type_);
145
146 // filesystem_id_ will be non empty for (and only for) cracked URLs.
147 if (!filesystem_id_.empty()) {
148 ss << virtual_path_.value();
149 ss << " (";
150 ss << GetFileSystemTypeString(type_) << "@" << filesystem_id_ << ":";
151 ss << path_.value();
152 ss << ")";
153 } else {
154 ss << path_.value();
155 }
156 return ss.str();
157 }
158
159 bool FileSystemURL::IsParent(const FileSystemURL& child) const {
160 return origin() == child.origin() &&
161 type() == child.type() &&
162 filesystem_id() == child.filesystem_id() &&
163 path().IsParent(child.path());
164 }
165
166 bool FileSystemURL::operator==(const FileSystemURL& that) const {
167 return origin_ == that.origin_ &&
168 type_ == that.type_ &&
169 path_ == that.path_ &&
170 filesystem_id_ == that.filesystem_id_ &&
171 is_valid_ == that.is_valid_;
172 }
173
174 bool FileSystemURL::Comparator::operator()(const FileSystemURL& lhs,
175 const FileSystemURL& rhs) const {
176 DCHECK(lhs.is_valid_ && rhs.is_valid_);
177 if (lhs.origin_ != rhs.origin_)
178 return lhs.origin_ < rhs.origin_;
179 if (lhs.type_ != rhs.type_)
180 return lhs.type_ < rhs.type_;
181 if (lhs.filesystem_id_ != rhs.filesystem_id_)
182 return lhs.filesystem_id_ < rhs.filesystem_id_;
183 return lhs.path_ < rhs.path_;
184 }
185
186 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698