blob: 79fa06dcd36b1dd83e6614c203ec15c29e086084 [file] [log] [blame]
[email protected]949f25a2012-06-27 01:53:091// 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
[email protected]c6f9203a2013-05-28 02:08:075#include "webkit/browser/fileapi/file_system_url.h"
[email protected]949f25a2012-06-27 01:53:096
[email protected]a349dd942012-10-15 16:09:137#include <sstream>
8
[email protected]bf2d3f92012-09-10 17:14:149#include "base/logging.h"
[email protected]3e4ec1b2012-09-05 06:45:5310#include "base/string_util.h"
11#include "net/base/escape.h"
[email protected]61d271f32013-05-28 04:59:2312#include "webkit/common/fileapi/file_system_types.h"
13#include "webkit/common/fileapi/file_system_util.h"
[email protected]949f25a2012-06-27 01:53:0914
15namespace fileapi {
[email protected]8f697892012-10-09 07:18:4916
[email protected]3e4ec1b2012-09-05 06:45:5317namespace {
[email protected]8f697892012-10-09 07:18:4918
[email protected]5b4bd112013-03-06 15:46:2219} // namespace
20
21FileSystemURL::FileSystemURL()
22 : is_valid_(false),
[email protected]906ae212013-03-24 01:37:1323 mount_type_(kFileSystemTypeUnknown),
24 type_(kFileSystemTypeUnknown) {
[email protected]5b4bd112013-03-06 15:46:2225}
26
27// static
28FileSystemURL FileSystemURL::CreateForTest(const GURL& url) {
29 return FileSystemURL(url);
30}
31
32FileSystemURL FileSystemURL::CreateForTest(const GURL& origin,
[email protected]3075512b2013-05-08 23:21:3933 FileSystemType mount_type,
34 const base::FilePath& virtual_path) {
35 return FileSystemURL(origin, mount_type, virtual_path);
[email protected]5b4bd112013-03-06 15:46:2236}
37
38// static
39bool FileSystemURL::ParseFileSystemSchemeURL(
40 const GURL& url,
41 GURL* origin_url,
[email protected]3075512b2013-05-08 23:21:3942 FileSystemType* mount_type,
43 base::FilePath* virtual_path) {
[email protected]3e4ec1b2012-09-05 06:45:5344 GURL origin;
45 FileSystemType file_system_type = kFileSystemTypeUnknown;
46
47 if (!url.is_valid() || !url.SchemeIsFileSystem())
48 return false;
49 DCHECK(url.inner_url());
50
51 std::string inner_path = url.inner_url()->path();
52
53 const struct {
54 FileSystemType type;
55 const char* dir;
56 } kValidTypes[] = {
57 { kFileSystemTypePersistent, kPersistentDir },
58 { kFileSystemTypeTemporary, kTemporaryDir },
59 { kFileSystemTypeIsolated, kIsolatedDir },
60 { kFileSystemTypeExternal, kExternalDir },
61 { kFileSystemTypeTest, kTestDir },
62 };
[email protected]5b7e42e62013-01-24 22:01:5163
[email protected]3e4ec1b2012-09-05 06:45:5364 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kValidTypes); ++i) {
65 if (StartsWithASCII(inner_path, kValidTypes[i].dir, true)) {
66 file_system_type = kValidTypes[i].type;
67 break;
68 }
69 }
70
71 if (file_system_type == kFileSystemTypeUnknown)
72 return false;
73
74 std::string path = net::UnescapeURLComponent(url.path(),
75 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS |
76 net::UnescapeRule::CONTROL_CHARS);
77
78 // Ensure the path is relative.
79 while (!path.empty() && path[0] == '/')
80 path.erase(0, 1);
81
[email protected]a3ef4832013-02-02 05:12:3382 base::FilePath converted_path = base::FilePath::FromUTF8Unsafe(path);
[email protected]3e4ec1b2012-09-05 06:45:5383
84 // All parent references should have been resolved in the renderer.
85 if (converted_path.ReferencesParent())
86 return false;
87
88 if (origin_url)
89 *origin_url = url.GetOrigin();
[email protected]3075512b2013-05-08 23:21:3990 if (mount_type)
91 *mount_type = file_system_type;
92 if (virtual_path)
93 *virtual_path = converted_path.NormalizePathSeparators().
[email protected]3e4ec1b2012-09-05 06:45:5394 StripTrailingSeparators();
95
96 return true;
97}
98
[email protected]5b7e42e62013-01-24 22:01:5199FileSystemURL::FileSystemURL(const GURL& url)
[email protected]906ae212013-03-24 01:37:13100 : mount_type_(kFileSystemTypeUnknown),
101 type_(kFileSystemTypeUnknown) {
[email protected]3075512b2013-05-08 23:21:39102 is_valid_ = ParseFileSystemSchemeURL(url, &origin_, &mount_type_,
103 &virtual_path_);
104 path_ = virtual_path_;
105 type_ = mount_type_;
[email protected]5b7e42e62013-01-24 22:01:51106}
107
108FileSystemURL::FileSystemURL(const GURL& origin,
[email protected]3075512b2013-05-08 23:21:39109 FileSystemType mount_type,
110 const base::FilePath& virtual_path)
[email protected]5b7e42e62013-01-24 22:01:51111 : is_valid_(true),
112 origin_(origin),
[email protected]3075512b2013-05-08 23:21:39113 mount_type_(mount_type),
114 virtual_path_(virtual_path.NormalizePathSeparators()),
115 type_(mount_type),
116 path_(virtual_path.NormalizePathSeparators()) {
[email protected]5b7e42e62013-01-24 22:01:51117}
118
119FileSystemURL::FileSystemURL(const GURL& origin,
[email protected]04c899f2013-02-08 08:28:42120 FileSystemType mount_type,
121 const base::FilePath& virtual_path,
[email protected]906ae212013-03-24 01:37:13122 const std::string& mount_filesystem_id,
[email protected]5b7e42e62013-01-24 22:01:51123 FileSystemType cracked_type,
[email protected]906ae212013-03-24 01:37:13124 const base::FilePath& cracked_path,
125 const std::string& filesystem_id)
[email protected]5b7e42e62013-01-24 22:01:51126 : is_valid_(true),
127 origin_(origin),
[email protected]04c899f2013-02-08 08:28:42128 mount_type_(mount_type),
[email protected]906ae212013-03-24 01:37:13129 virtual_path_(virtual_path.NormalizePathSeparators()),
130 mount_filesystem_id_(mount_filesystem_id),
131 type_(cracked_type),
[email protected]5b7e42e62013-01-24 22:01:51132 path_(cracked_path.NormalizePathSeparators()),
[email protected]906ae212013-03-24 01:37:13133 filesystem_id_(filesystem_id) {
[email protected]d6afd112012-07-25 22:55:04134}
[email protected]949f25a2012-06-27 01:53:09135
136FileSystemURL::~FileSystemURL() {}
137
[email protected]a349dd942012-10-15 16:09:13138std::string FileSystemURL::DebugString() const {
[email protected]949f25a2012-06-27 01:53:09139 if (!is_valid_)
[email protected]a349dd942012-10-15 16:09:13140 return "invalid filesystem: URL";
141 std::ostringstream ss;
142 ss << GetFileSystemRootURI(origin_, mount_type_);
[email protected]5b7e42e62013-01-24 22:01:51143
144 // filesystem_id_ will be non empty for (and only for) cracked URLs.
145 if (!filesystem_id_.empty()) {
[email protected]a349dd942012-10-15 16:09:13146 ss << virtual_path_.value();
[email protected]a349dd942012-10-15 16:09:13147 ss << " (";
148 ss << GetFileSystemTypeString(type_) << "@" << filesystem_id_ << ":";
149 ss << path_.value();
150 ss << ")";
[email protected]5b7e42e62013-01-24 22:01:51151 } else {
152 ss << path_.value();
[email protected]a349dd942012-10-15 16:09:13153 }
154 return ss.str();
[email protected]949f25a2012-06-27 01:53:09155}
156
[email protected]5dca49e2012-10-10 14:56:13157bool FileSystemURL::IsParent(const FileSystemURL& child) const {
[email protected]4c413de02013-05-27 06:00:59158 return IsInSameFileSystem(child) &&
[email protected]5dca49e2012-10-10 14:56:13159 path().IsParent(child.path());
160}
161
[email protected]4c413de02013-05-27 06:00:59162bool FileSystemURL::IsInSameFileSystem(const FileSystemURL& other) const {
163 return origin() == other.origin() &&
164 type() == other.type() &&
165 filesystem_id() == other.filesystem_id();
166}
167
[email protected]949f25a2012-06-27 01:53:09168bool FileSystemURL::operator==(const FileSystemURL& that) const {
169 return origin_ == that.origin_ &&
170 type_ == that.type_ &&
171 path_ == that.path_ &&
[email protected]d6afd112012-07-25 22:55:04172 filesystem_id_ == that.filesystem_id_ &&
[email protected]949f25a2012-06-27 01:53:09173 is_valid_ == that.is_valid_;
174}
175
[email protected]bf2d3f92012-09-10 17:14:14176bool FileSystemURL::Comparator::operator()(const FileSystemURL& lhs,
177 const FileSystemURL& rhs) const {
178 DCHECK(lhs.is_valid_ && rhs.is_valid_);
179 if (lhs.origin_ != rhs.origin_)
180 return lhs.origin_ < rhs.origin_;
181 if (lhs.type_ != rhs.type_)
182 return lhs.type_ < rhs.type_;
[email protected]8f697892012-10-09 07:18:49183 if (lhs.filesystem_id_ != rhs.filesystem_id_)
184 return lhs.filesystem_id_ < rhs.filesystem_id_;
185 return lhs.path_ < rhs.path_;
[email protected]bf2d3f92012-09-10 17:14:14186}
187
[email protected]949f25a2012-06-27 01:53:09188} // namespace fileapi