Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / storage / browser / fileapi / file_system_url.cc
blob13baad42b23f37ae4789e7b3905c34a2b97992d0
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.
5 #include "storage/browser/fileapi/file_system_url.h"
7 #include <sstream>
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "net/base/escape.h"
12 #include "storage/common/fileapi/file_system_util.h"
14 namespace storage {
16 namespace {
18 } // namespace
20 FileSystemURL::FileSystemURL()
21 : is_valid_(false),
22 mount_type_(kFileSystemTypeUnknown),
23 type_(kFileSystemTypeUnknown),
24 mount_option_(FlushPolicy::NO_FLUSH_ON_COMPLETION) {
27 // static
28 FileSystemURL FileSystemURL::CreateForTest(const GURL& url) {
29 return FileSystemURL(url);
32 FileSystemURL FileSystemURL::CreateForTest(const GURL& origin,
33 FileSystemType mount_type,
34 const base::FilePath& virtual_path) {
35 return FileSystemURL(origin, mount_type, virtual_path);
38 FileSystemURL FileSystemURL::CreateForTest(
39 const GURL& origin,
40 FileSystemType mount_type,
41 const base::FilePath& virtual_path,
42 const std::string& mount_filesystem_id,
43 FileSystemType cracked_type,
44 const base::FilePath& cracked_path,
45 const std::string& filesystem_id,
46 const FileSystemMountOption& mount_option) {
47 return FileSystemURL(origin,
48 mount_type,
49 virtual_path,
50 mount_filesystem_id,
51 cracked_type,
52 cracked_path,
53 filesystem_id,
54 mount_option);
57 FileSystemURL::FileSystemURL(const GURL& url)
58 : mount_type_(kFileSystemTypeUnknown),
59 type_(kFileSystemTypeUnknown),
60 mount_option_(FlushPolicy::NO_FLUSH_ON_COMPLETION) {
61 is_valid_ = ParseFileSystemSchemeURL(url, &origin_, &mount_type_,
62 &virtual_path_);
63 path_ = virtual_path_;
64 type_ = mount_type_;
67 FileSystemURL::FileSystemURL(const GURL& origin,
68 FileSystemType mount_type,
69 const base::FilePath& virtual_path)
70 : is_valid_(true),
71 origin_(origin),
72 mount_type_(mount_type),
73 virtual_path_(virtual_path.NormalizePathSeparators()),
74 type_(mount_type),
75 path_(virtual_path.NormalizePathSeparators()),
76 mount_option_(FlushPolicy::NO_FLUSH_ON_COMPLETION) {
79 FileSystemURL::FileSystemURL(const GURL& origin,
80 FileSystemType mount_type,
81 const base::FilePath& virtual_path,
82 const std::string& mount_filesystem_id,
83 FileSystemType cracked_type,
84 const base::FilePath& cracked_path,
85 const std::string& filesystem_id,
86 const FileSystemMountOption& mount_option)
87 : is_valid_(true),
88 origin_(origin),
89 mount_type_(mount_type),
90 virtual_path_(virtual_path.NormalizePathSeparators()),
91 mount_filesystem_id_(mount_filesystem_id),
92 type_(cracked_type),
93 path_(cracked_path.NormalizePathSeparators()),
94 filesystem_id_(filesystem_id),
95 mount_option_(mount_option) {
98 FileSystemURL::~FileSystemURL() {}
100 GURL FileSystemURL::ToGURL() const {
101 if (!is_valid_)
102 return GURL();
104 std::string url = GetFileSystemRootURI(origin_, mount_type_).spec();
105 if (url.empty())
106 return GURL();
108 // Exactly match with DOMFileSystemBase::createFileSystemURL()'s encoding
109 // behavior, where the path is escaped by KURL::encodeWithURLEscapeSequences
110 // which is essentially encodeURIComponent except '/'.
111 std::string escaped = net::EscapeQueryParamValue(
112 virtual_path_.NormalizePathSeparatorsTo('/').AsUTF8Unsafe(),
113 false /* use_plus */);
114 base::ReplaceSubstringsAfterOffset(&escaped, 0, "%2F", "/");
115 url.append(escaped);
117 // Build nested GURL.
118 return GURL(url);
121 std::string FileSystemURL::DebugString() const {
122 if (!is_valid_)
123 return "invalid filesystem: URL";
124 std::ostringstream ss;
125 ss << GetFileSystemRootURI(origin_, mount_type_);
127 // filesystem_id_ will be non empty for (and only for) cracked URLs.
128 if (!filesystem_id_.empty()) {
129 ss << virtual_path_.value();
130 ss << " (";
131 ss << GetFileSystemTypeString(type_) << "@" << filesystem_id_ << ":";
132 ss << path_.value();
133 ss << ")";
134 } else {
135 ss << path_.value();
137 return ss.str();
140 bool FileSystemURL::IsParent(const FileSystemURL& child) const {
141 return IsInSameFileSystem(child) &&
142 path().IsParent(child.path());
145 bool FileSystemURL::IsInSameFileSystem(const FileSystemURL& other) const {
146 return origin() == other.origin() &&
147 type() == other.type() &&
148 filesystem_id() == other.filesystem_id();
151 bool FileSystemURL::operator==(const FileSystemURL& that) const {
152 return origin_ == that.origin_ &&
153 type_ == that.type_ &&
154 path_ == that.path_ &&
155 filesystem_id_ == that.filesystem_id_ &&
156 is_valid_ == that.is_valid_;
159 bool FileSystemURL::Comparator::operator()(const FileSystemURL& lhs,
160 const FileSystemURL& rhs) const {
161 DCHECK(lhs.is_valid_ && rhs.is_valid_);
162 if (lhs.origin_ != rhs.origin_)
163 return lhs.origin_ < rhs.origin_;
164 if (lhs.type_ != rhs.type_)
165 return lhs.type_ < rhs.type_;
166 if (lhs.filesystem_id_ != rhs.filesystem_id_)
167 return lhs.filesystem_id_ < rhs.filesystem_id_;
168 return lhs.path_ < rhs.path_;
171 } // namespace storage