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_types.h"
13 #include "storage/common/fileapi/file_system_util.h"
21 FileSystemURL::FileSystemURL()
23 mount_type_(kFileSystemTypeUnknown
),
24 type_(kFileSystemTypeUnknown
),
25 mount_option_(COPY_SYNC_OPTION_NO_SYNC
) {
29 FileSystemURL
FileSystemURL::CreateForTest(const GURL
& url
) {
30 return FileSystemURL(url
);
33 FileSystemURL
FileSystemURL::CreateForTest(const GURL
& origin
,
34 FileSystemType mount_type
,
35 const base::FilePath
& virtual_path
) {
36 return FileSystemURL(origin
, mount_type
, virtual_path
);
39 FileSystemURL::FileSystemURL(const GURL
& url
)
40 : mount_type_(kFileSystemTypeUnknown
),
41 type_(kFileSystemTypeUnknown
),
42 mount_option_(COPY_SYNC_OPTION_NO_SYNC
) {
43 is_valid_
= ParseFileSystemSchemeURL(url
, &origin_
, &mount_type_
,
45 path_
= virtual_path_
;
49 FileSystemURL::FileSystemURL(const GURL
& origin
,
50 FileSystemType mount_type
,
51 const base::FilePath
& virtual_path
)
54 mount_type_(mount_type
),
55 virtual_path_(virtual_path
.NormalizePathSeparators()),
57 path_(virtual_path
.NormalizePathSeparators()),
58 mount_option_(COPY_SYNC_OPTION_NO_SYNC
) {
61 FileSystemURL::FileSystemURL(const GURL
& origin
,
62 FileSystemType mount_type
,
63 const base::FilePath
& virtual_path
,
64 const std::string
& mount_filesystem_id
,
65 FileSystemType cracked_type
,
66 const base::FilePath
& cracked_path
,
67 const std::string
& filesystem_id
,
68 const FileSystemMountOption
& mount_option
)
71 mount_type_(mount_type
),
72 virtual_path_(virtual_path
.NormalizePathSeparators()),
73 mount_filesystem_id_(mount_filesystem_id
),
75 path_(cracked_path
.NormalizePathSeparators()),
76 filesystem_id_(filesystem_id
),
77 mount_option_(mount_option
) {
80 FileSystemURL::~FileSystemURL() {}
82 GURL
FileSystemURL::ToGURL() const {
86 std::string url
= GetFileSystemRootURI(origin_
, mount_type_
).spec();
90 // Exactly match with DOMFileSystemBase::createFileSystemURL()'s encoding
91 // behavior, where the path is escaped by KURL::encodeWithURLEscapeSequences
92 // which is essentially encodeURIComponent except '/'.
93 std::string escaped
= net::EscapeQueryParamValue(
94 virtual_path_
.NormalizePathSeparatorsTo('/').AsUTF8Unsafe(),
95 false /* use_plus */);
96 ReplaceSubstringsAfterOffset(&escaped
, 0, "%2F", "/");
103 std::string
FileSystemURL::DebugString() const {
105 return "invalid filesystem: URL";
106 std::ostringstream ss
;
107 ss
<< GetFileSystemRootURI(origin_
, mount_type_
);
109 // filesystem_id_ will be non empty for (and only for) cracked URLs.
110 if (!filesystem_id_
.empty()) {
111 ss
<< virtual_path_
.value();
113 ss
<< GetFileSystemTypeString(type_
) << "@" << filesystem_id_
<< ":";
122 bool FileSystemURL::IsParent(const FileSystemURL
& child
) const {
123 return IsInSameFileSystem(child
) &&
124 path().IsParent(child
.path());
127 bool FileSystemURL::IsInSameFileSystem(const FileSystemURL
& other
) const {
128 return origin() == other
.origin() &&
129 type() == other
.type() &&
130 filesystem_id() == other
.filesystem_id();
133 bool FileSystemURL::operator==(const FileSystemURL
& that
) const {
134 return origin_
== that
.origin_
&&
135 type_
== that
.type_
&&
136 path_
== that
.path_
&&
137 filesystem_id_
== that
.filesystem_id_
&&
138 is_valid_
== that
.is_valid_
;
141 bool FileSystemURL::Comparator::operator()(const FileSystemURL
& lhs
,
142 const FileSystemURL
& rhs
) const {
143 DCHECK(lhs
.is_valid_
&& rhs
.is_valid_
);
144 if (lhs
.origin_
!= rhs
.origin_
)
145 return lhs
.origin_
< rhs
.origin_
;
146 if (lhs
.type_
!= rhs
.type_
)
147 return lhs
.type_
< rhs
.type_
;
148 if (lhs
.filesystem_id_
!= rhs
.filesystem_id_
)
149 return lhs
.filesystem_id_
< rhs
.filesystem_id_
;
150 return lhs
.path_
< rhs
.path_
;
153 } // namespace storage