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"
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"
20 FileSystemURL::FileSystemURL()
22 mount_type_(kFileSystemTypeUnknown
),
23 type_(kFileSystemTypeUnknown
),
24 mount_option_(FlushPolicy::NO_FLUSH_ON_COMPLETION
) {
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(
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
,
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_
,
63 path_
= virtual_path_
;
67 FileSystemURL::FileSystemURL(const GURL
& origin
,
68 FileSystemType mount_type
,
69 const base::FilePath
& virtual_path
)
72 mount_type_(mount_type
),
73 virtual_path_(virtual_path
.NormalizePathSeparators()),
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
)
89 mount_type_(mount_type
),
90 virtual_path_(virtual_path
.NormalizePathSeparators()),
91 mount_filesystem_id_(mount_filesystem_id
),
93 path_(cracked_path
.NormalizePathSeparators()),
94 filesystem_id_(filesystem_id
),
95 mount_option_(mount_option
) {
98 FileSystemURL::~FileSystemURL() {}
100 GURL
FileSystemURL::ToGURL() const {
104 std::string url
= GetFileSystemRootURI(origin_
, mount_type_
).spec();
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 ReplaceSubstringsAfterOffset(&escaped
, 0, "%2F", "/");
117 // Build nested GURL.
121 std::string
FileSystemURL::DebugString() const {
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();
131 ss
<< GetFileSystemTypeString(type_
) << "@" << filesystem_id_
<< ":";
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