OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/common/database/database_identifier.h" | 5 #include "storage/common/database/database_identifier.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "url/url_canon.h" | 9 #include "url/url_canon.h" |
10 | 10 |
11 namespace webkit_database { | 11 namespace webkit_database { |
12 | 12 |
13 // static | 13 // static |
14 std::string GetIdentifierFromOrigin(const GURL& origin) { | 14 std::string GetIdentifierFromOrigin(const GURL& origin) { |
15 return DatabaseIdentifier::CreateFromOrigin(origin).ToString(); | 15 return DatabaseIdentifier::CreateFromOrigin(origin).ToString(); |
16 } | 16 } |
17 | 17 |
18 // static | 18 // static |
19 GURL GetOriginFromIdentifier(const std::string& identifier) { | 19 GURL GetOriginFromIdentifier(const std::string& identifier) { |
20 return DatabaseIdentifier::Parse(identifier).ToOrigin(); | 20 return DatabaseIdentifier::Parse(identifier).ToOrigin(); |
21 } | 21 } |
22 | 22 |
23 static bool SchemeIsUnique(const std::string& scheme) { | 23 static bool SchemeIsUnique(const std::string& scheme) { |
24 return scheme == "about" || scheme == "data" || scheme == "javascript"; | 24 return scheme == "about" || scheme == "data" || scheme == "javascript"; |
25 } | 25 } |
26 | 26 |
27 // static | 27 // static |
28 const DatabaseIdentifier DatabaseIdentifier::UniqueFileIdentifier() { | 28 const DatabaseIdentifier DatabaseIdentifier::UniqueFileIdentifier() { |
29 return DatabaseIdentifier("", "", 0, true, true); | 29 return DatabaseIdentifier("", "", 0, true, true); |
30 } | 30 } |
31 | 31 |
32 // static | 32 // static |
33 DatabaseIdentifier DatabaseIdentifier::CreateFromOrigin(const GURL& origin) { | 33 DatabaseIdentifier DatabaseIdentifier::CreateFromOrigin(const GURL& origin) { |
34 if (!origin.is_valid() || origin.is_empty() || | 34 if (!origin.is_valid() || origin.is_empty() || !origin.IsStandard() || |
35 !origin.IsStandard() || SchemeIsUnique(origin.scheme())) | 35 SchemeIsUnique(origin.scheme())) |
36 return DatabaseIdentifier(); | 36 return DatabaseIdentifier(); |
37 | 37 |
38 if (origin.SchemeIsFile()) | 38 if (origin.SchemeIsFile()) |
39 return UniqueFileIdentifier(); | 39 return UniqueFileIdentifier(); |
40 | 40 |
41 int port = origin.IntPort(); | 41 int port = origin.IntPort(); |
42 if (port == url::PORT_INVALID) | 42 if (port == url::PORT_INVALID) |
43 return DatabaseIdentifier(); | 43 return DatabaseIdentifier(); |
44 | 44 |
45 // We encode the default port for the specified scheme as 0. GURL | 45 // We encode the default port for the specified scheme as 0. GURL |
46 // canonicalizes this as an unspecified port. | 46 // canonicalizes this as an unspecified port. |
47 if (port == url::PORT_UNSPECIFIED) | 47 if (port == url::PORT_UNSPECIFIED) |
48 port = 0; | 48 port = 0; |
49 | 49 |
50 return DatabaseIdentifier(origin.scheme(), | 50 return DatabaseIdentifier(origin.scheme(), |
51 origin.host(), | 51 origin.host(), |
52 port, | 52 port, |
53 false /* unique */, | 53 false /* unique */, |
54 false /* file */); | 54 false /* file */); |
55 } | 55 } |
56 | 56 |
57 // static | 57 // static |
58 DatabaseIdentifier DatabaseIdentifier::Parse(const std::string& identifier) { | 58 DatabaseIdentifier DatabaseIdentifier::Parse(const std::string& identifier) { |
59 if (!base::IsStringASCII(identifier)) | 59 if (!base::IsStringASCII(identifier)) |
60 return DatabaseIdentifier(); | 60 return DatabaseIdentifier(); |
61 if (identifier.find("..") != std::string::npos) | 61 if (identifier.find("..") != std::string::npos) |
62 return DatabaseIdentifier(); | 62 return DatabaseIdentifier(); |
63 char forbidden[] = {'\\', '/', ':' ,'\0'}; | 63 char forbidden[] = {'\\', '/', ':', '\0'}; |
64 if (identifier.find_first_of(forbidden, 0, arraysize(forbidden)) != | 64 if (identifier.find_first_of(forbidden, 0, arraysize(forbidden)) != |
65 std::string::npos) { | 65 std::string::npos) { |
66 return DatabaseIdentifier(); | 66 return DatabaseIdentifier(); |
67 } | 67 } |
68 | 68 |
69 size_t first_underscore = identifier.find_first_of('_'); | 69 size_t first_underscore = identifier.find_first_of('_'); |
70 if (first_underscore == std::string::npos || first_underscore == 0) | 70 if (first_underscore == std::string::npos || first_underscore == 0) |
71 return DatabaseIdentifier(); | 71 return DatabaseIdentifier(); |
72 | 72 |
73 size_t last_underscore = identifier.find_last_of('_'); | 73 size_t last_underscore = identifier.find_last_of('_'); |
74 if (last_underscore == std::string::npos || | 74 if (last_underscore == std::string::npos || |
75 last_underscore == first_underscore || | 75 last_underscore == first_underscore || |
(...skipping 22 matching lines...) Expand all Loading... |
98 hostname = ""; | 98 hostname = ""; |
99 | 99 |
100 // If a url doesn't parse cleanly or doesn't round trip, reject it. | 100 // If a url doesn't parse cleanly or doesn't round trip, reject it. |
101 if (!url.is_valid() || url.scheme() != scheme || url.host() != hostname) | 101 if (!url.is_valid() || url.scheme() != scheme || url.host() != hostname) |
102 return DatabaseIdentifier(); | 102 return DatabaseIdentifier(); |
103 | 103 |
104 return DatabaseIdentifier(scheme, hostname, port, false /* unique */, false); | 104 return DatabaseIdentifier(scheme, hostname, port, false /* unique */, false); |
105 } | 105 } |
106 | 106 |
107 DatabaseIdentifier::DatabaseIdentifier() | 107 DatabaseIdentifier::DatabaseIdentifier() |
108 : port_(0), | 108 : port_(0), is_unique_(true), is_file_(false) { |
109 is_unique_(true), | |
110 is_file_(false) { | |
111 } | 109 } |
112 | 110 |
113 DatabaseIdentifier::DatabaseIdentifier(const std::string& scheme, | 111 DatabaseIdentifier::DatabaseIdentifier(const std::string& scheme, |
114 const std::string& hostname, | 112 const std::string& hostname, |
115 int port, | 113 int port, |
116 bool is_unique, | 114 bool is_unique, |
117 bool is_file) | 115 bool is_file) |
118 : scheme_(scheme), | 116 : scheme_(scheme), |
119 hostname_(base::StringToLowerASCII(hostname)), | 117 hostname_(base::StringToLowerASCII(hostname)), |
120 port_(port), | 118 port_(port), |
121 is_unique_(is_unique), | 119 is_unique_(is_unique), |
122 is_file_(is_file) { | 120 is_file_(is_file) { |
123 } | 121 } |
124 | 122 |
125 DatabaseIdentifier::~DatabaseIdentifier() {} | 123 DatabaseIdentifier::~DatabaseIdentifier() { |
| 124 } |
126 | 125 |
127 std::string DatabaseIdentifier::ToString() const { | 126 std::string DatabaseIdentifier::ToString() const { |
128 if (is_file_) | 127 if (is_file_) |
129 return "file__0"; | 128 return "file__0"; |
130 if (is_unique_) | 129 if (is_unique_) |
131 return "__0"; | 130 return "__0"; |
132 return scheme_ + "_" + hostname_ + "_" + base::IntToString(port_); | 131 return scheme_ + "_" + hostname_ + "_" + base::IntToString(port_); |
133 } | 132 } |
134 | 133 |
135 GURL DatabaseIdentifier::ToOrigin() const { | 134 GURL DatabaseIdentifier::ToOrigin() const { |
136 if (is_file_) | 135 if (is_file_) |
137 return GURL("file:///"); | 136 return GURL("file:///"); |
138 if (is_unique_) | 137 if (is_unique_) |
139 return GURL(); | 138 return GURL(); |
140 if (port_ == 0) | 139 if (port_ == 0) |
141 return GURL(scheme_ + "://" + hostname_); | 140 return GURL(scheme_ + "://" + hostname_); |
142 return GURL(scheme_ + "://" + hostname_ + ":" + base::IntToString(port_)); | 141 return GURL(scheme_ + "://" + hostname_ + ":" + base::IntToString(port_)); |
143 } | 142 } |
144 | 143 |
145 } // namespace webkit_database | 144 } // namespace webkit_database |
OLD | NEW |