Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/common/content_settings_pattern_parser.h" | 5 #include "chrome/common/content_settings_pattern_parser.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "chrome/common/url_constants.h" | 8 #include "components/content_settings/core/common/content_settings_client.h" |
| 9 #include "extensions/common/constants.h" | 9 #include "url/url_constants.h" |
| 10 #include "net/base/net_util.h" | |
| 11 #include "url/gurl.h" | |
| 12 #include "url/url_canon.h" | |
| 13 | 10 |
| 14 namespace { | 11 namespace { |
| 15 | 12 |
| 13 const char* kDomainWildcard = "[*.]"; | |
|
Bernhard Bauer
2014/08/11 08:43:50
If you're touching this code anyway: const char kC
vasilii
2014/08/11 13:57:18
DomainWildcard is used in Builder too. Below we ha
Bernhard Bauer
2014/08/11 14:06:43
Declare it as an array of characters, not as a poi
vasilii
2014/08/11 16:37:47
Done.
| |
| 14 const size_t kDomainWildcardLength = 4; | |
| 15 const char* kHostWildcard = "*"; | |
| 16 const char* kPathWildcard = "*"; | |
| 17 const char* kPortWildcard = "*"; | |
| 18 const char* kSchemeWildcard = "*"; | |
| 16 const char* kUrlPathSeparator = "/"; | 19 const char* kUrlPathSeparator = "/"; |
| 17 const char* kUrlPortSeparator = ":"; | 20 const char* kUrlPortSeparator = ":"; |
| 18 | 21 |
| 19 class Component { | 22 class Component { |
| 20 public: | 23 public: |
| 21 Component() : start(0), len(0) {} | 24 Component() : start(0), len(0) {} |
| 22 Component(size_t s, size_t l) : start(s), len(l) {} | 25 Component(size_t s, size_t l) : start(s), len(l) {} |
| 23 | 26 |
| 24 bool IsNonEmpty() { | 27 bool IsNonEmpty() { |
| 25 return len > 0; | 28 return len > 0; |
| 26 } | 29 } |
| 27 | 30 |
| 28 size_t start; | 31 size_t start; |
| 29 size_t len; | 32 size_t len; |
| 30 }; | 33 }; |
| 31 | 34 |
| 32 } // namespace | 35 } // namespace |
| 33 | 36 |
| 34 namespace content_settings { | 37 namespace content_settings { |
| 35 | 38 |
| 36 const char* PatternParser::kDomainWildcard = "[*.]"; | 39 void PatternParser::Parse(ContentSettingsClient* client, |
| 37 | 40 const std::string& pattern_spec, |
| 38 const size_t PatternParser::kDomainWildcardLength = 4; | |
| 39 | |
| 40 const char* PatternParser::kSchemeWildcard = "*"; | |
| 41 | |
| 42 const char* PatternParser::kHostWildcard = "*"; | |
| 43 | |
| 44 const char* PatternParser::kPortWildcard = "*"; | |
| 45 | |
| 46 const char* PatternParser::kPathWildcard = "*"; | |
| 47 | |
| 48 // static | |
| 49 void PatternParser::Parse(const std::string& pattern_spec, | |
| 50 ContentSettingsPattern::BuilderInterface* builder) { | 41 ContentSettingsPattern::BuilderInterface* builder) { |
| 42 DCHECK(client); | |
| 51 if (pattern_spec == "*") { | 43 if (pattern_spec == "*") { |
| 52 builder->WithSchemeWildcard(); | 44 builder->WithSchemeWildcard(); |
| 53 builder->WithDomainWildcard(); | 45 builder->WithDomainWildcard(); |
| 54 builder->WithPortWildcard(); | 46 builder->WithPortWildcard(); |
| 55 return; | 47 return; |
| 56 } | 48 } |
| 57 | 49 |
| 58 // Initialize components for the individual patterns parts to empty | 50 // Initialize components for the individual patterns parts to empty |
| 59 // sub-strings. | 51 // sub-strings. |
| 60 Component scheme_component; | 52 Component scheme_component; |
| 61 Component host_component; | 53 Component host_component; |
| 62 Component port_component; | 54 Component port_component; |
| 63 Component path_component; | 55 Component path_component; |
| 64 | 56 |
| 65 size_t start = 0; | 57 size_t start = 0; |
| 66 size_t current_pos = 0; | 58 size_t current_pos = 0; |
| 67 | 59 |
| 68 if (pattern_spec.empty()) | 60 if (pattern_spec.empty()) |
| 69 return; | 61 return; |
| 70 | 62 |
| 71 // Test if a scheme pattern is in the spec. | 63 // Test if a scheme pattern is in the spec. |
| 72 current_pos = pattern_spec.find( | 64 const std::string standard_scheme_separator(url::kStandardSchemeSeparator); |
| 73 std::string(url::kStandardSchemeSeparator), start); | 65 current_pos = pattern_spec.find(standard_scheme_separator, start); |
| 74 if (current_pos != std::string::npos) { | 66 if (current_pos != std::string::npos) { |
| 75 scheme_component = Component(start, current_pos); | 67 scheme_component = Component(start, current_pos); |
| 76 start = current_pos + strlen(url::kStandardSchemeSeparator); | 68 start = current_pos + standard_scheme_separator.size(); |
| 77 current_pos = start; | 69 current_pos = start; |
| 78 } else { | 70 } else { |
| 79 current_pos = start; | 71 current_pos = start; |
| 80 } | 72 } |
| 81 | 73 |
| 82 if (start >= pattern_spec.size()) | 74 if (start >= pattern_spec.size()) |
| 83 return; // Bad pattern spec. | 75 return; // Bad pattern spec. |
| 84 | 76 |
| 85 // Jump to the end of domain wildcards or an IPv6 addresses. IPv6 addresses | 77 // Jump to the end of domain wildcards or an IPv6 addresses. IPv6 addresses |
| 86 // contain ':'. So first move to the end of an IPv6 address befor searching | 78 // contain ':'. So first move to the end of an IPv6 address befor searching |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 for (size_t i = 0; i < port.size(); ++i) { | 157 for (size_t i = 0; i < port.size(); ++i) { |
| 166 if (!IsAsciiDigit(port[i])) { | 158 if (!IsAsciiDigit(port[i])) { |
| 167 builder->Invalid(); | 159 builder->Invalid(); |
| 168 return; | 160 return; |
| 169 } | 161 } |
| 170 } | 162 } |
| 171 // TODO(markusheintz): Check port range. | 163 // TODO(markusheintz): Check port range. |
| 172 builder->WithPort(port); | 164 builder->WithPort(port); |
| 173 } | 165 } |
| 174 } else { | 166 } else { |
| 175 if (scheme != std::string(extensions::kExtensionScheme) && | 167 if (!client->IsExtensionScheme(scheme) && |
| 176 scheme != std::string(url::kFileScheme)) | 168 scheme != std::string(url::kFileScheme)) |
| 177 builder->WithPortWildcard(); | 169 builder->WithPortWildcard(); |
| 178 } | 170 } |
| 179 | 171 |
| 180 if (path_component.IsNonEmpty()) { | 172 if (path_component.IsNonEmpty()) { |
| 181 const std::string path = pattern_spec.substr(path_component.start, | 173 const std::string path = pattern_spec.substr(path_component.start, |
| 182 path_component.len); | 174 path_component.len); |
| 183 if (path.substr(1) == kPathWildcard) | 175 if (path.substr(1) == kPathWildcard) |
| 184 builder->WithPathWildcard(); | 176 builder->WithPathWildcard(); |
| 185 else | 177 else |
| 186 builder->WithPath(path); | 178 builder->WithPath(path); |
| 187 } | 179 } |
| 188 } | 180 } |
| 189 | 181 |
| 190 // static | |
| 191 std::string PatternParser::ToString( | 182 std::string PatternParser::ToString( |
| 183 ContentSettingsClient* client, | |
| 192 const ContentSettingsPattern::PatternParts& parts) { | 184 const ContentSettingsPattern::PatternParts& parts) { |
| 185 DCHECK(client); | |
| 193 // Return the most compact form to support legacy code and legacy pattern | 186 // Return the most compact form to support legacy code and legacy pattern |
| 194 // strings. | 187 // strings. |
| 195 if (parts.is_scheme_wildcard && | 188 if (parts.is_scheme_wildcard && |
| 196 parts.has_domain_wildcard && | 189 parts.has_domain_wildcard && |
| 197 parts.host.empty() && | 190 parts.host.empty() && |
| 198 parts.is_port_wildcard) | 191 parts.is_port_wildcard) |
| 199 return "*"; | 192 return "*"; |
| 200 | 193 |
| 201 std::string str; | 194 std::string str; |
| 202 if (!parts.is_scheme_wildcard) | 195 if (!parts.is_scheme_wildcard) |
| 203 str += parts.scheme + url::kStandardSchemeSeparator; | 196 str += parts.scheme + url::kStandardSchemeSeparator; |
| 204 | 197 |
| 205 if (parts.scheme == url::kFileScheme) { | 198 if (parts.scheme == url::kFileScheme) { |
| 206 if (parts.is_path_wildcard) | 199 if (parts.is_path_wildcard) |
| 207 return str + kUrlPathSeparator + kPathWildcard; | 200 return str + kUrlPathSeparator + kPathWildcard; |
| 208 else | 201 else |
| 209 return str + parts.path; | 202 return str + parts.path; |
| 210 } | 203 } |
| 211 | 204 |
| 212 if (parts.has_domain_wildcard) { | 205 if (parts.has_domain_wildcard) { |
| 213 if (parts.host.empty()) | 206 if (parts.host.empty()) |
| 214 str += kHostWildcard; | 207 str += kHostWildcard; |
| 215 else | 208 else |
| 216 str += kDomainWildcard; | 209 str += kDomainWildcard; |
| 217 } | 210 } |
| 218 str += parts.host; | 211 str += parts.host; |
| 219 | 212 |
| 220 if (parts.scheme == std::string(extensions::kExtensionScheme)) { | 213 if (client->IsExtensionScheme(parts.scheme)) { |
| 221 str += parts.path.empty() ? std::string(kUrlPathSeparator) : parts.path; | 214 str += parts.path.empty() ? std::string(kUrlPathSeparator) : parts.path; |
| 222 return str; | 215 return str; |
| 223 } | 216 } |
| 224 | 217 |
| 225 if (!parts.is_port_wildcard) { | 218 if (!parts.is_port_wildcard) { |
| 226 str += std::string(kUrlPortSeparator) + parts.port; | 219 str += std::string(kUrlPortSeparator) + parts.port; |
| 227 } | 220 } |
| 228 | 221 |
| 229 return str; | 222 return str; |
| 230 } | 223 } |
| 231 | 224 |
| 232 } // namespace content_settings | 225 } // namespace content_settings |
| OLD | NEW |