Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "chrome/common/content_settings_pattern.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 | 123 |
| 124 BuilderInterface* ContentSettingsPattern::Builder::WithSchemeWildcard() { | 124 BuilderInterface* ContentSettingsPattern::Builder::WithSchemeWildcard() { |
| 125 parts_.scheme = ""; | 125 parts_.scheme = ""; |
| 126 parts_.is_scheme_wildcard = true; | 126 parts_.is_scheme_wildcard = true; |
| 127 return this; | 127 return this; |
| 128 } | 128 } |
| 129 | 129 |
| 130 BuilderInterface* ContentSettingsPattern::Builder::WithPath( | 130 BuilderInterface* ContentSettingsPattern::Builder::WithPath( |
| 131 const std::string& path) { | 131 const std::string& path) { |
| 132 parts_.path = path; | 132 parts_.path = path; |
| 133 parts_.is_path_wildcard = false; | |
| 134 return this; | |
| 135 } | |
| 136 | |
| 137 BuilderInterface* ContentSettingsPattern::Builder::WithPathWildcard() { | |
| 138 parts_.path = ""; | |
| 139 parts_.is_path_wildcard = true; | |
| 133 return this; | 140 return this; |
| 134 } | 141 } |
| 135 | 142 |
| 136 BuilderInterface* ContentSettingsPattern::Builder::Invalid() { | 143 BuilderInterface* ContentSettingsPattern::Builder::Invalid() { |
| 137 is_valid_ = false; | 144 is_valid_ = false; |
| 138 return this; | 145 return this; |
| 139 } | 146 } |
| 140 | 147 |
| 141 ContentSettingsPattern ContentSettingsPattern::Builder::Build() { | 148 ContentSettingsPattern ContentSettingsPattern::Builder::Build() { |
| 142 if (!is_valid_) | 149 if (!is_valid_) |
| 143 return ContentSettingsPattern(); | 150 return ContentSettingsPattern(); |
| 144 if (!Canonicalize(&parts_)) | 151 if (!Canonicalize(&parts_)) |
| 145 return ContentSettingsPattern(); | 152 return ContentSettingsPattern(); |
| 146 if (use_legacy_validate_) { | 153 if (use_legacy_validate_) { |
| 147 is_valid_ = LegacyValidate(parts_); | 154 is_valid_ = LegacyValidate(parts_); |
| 148 } else { | 155 } else { |
| 149 is_valid_ = Validate(parts_); | 156 is_valid_ = Validate(parts_); |
| 150 } | 157 } |
| 151 return ContentSettingsPattern(parts_, is_valid_); | 158 return ContentSettingsPattern(parts_, is_valid_); |
| 152 } | 159 } |
| 153 | 160 |
| 154 // static | 161 // static |
| 155 bool ContentSettingsPattern::Builder::Canonicalize(PatternParts* parts) { | 162 bool ContentSettingsPattern::Builder::Canonicalize(PatternParts* parts) { |
| 156 // Canonicalize the scheme part. | 163 // Canonicalize the scheme part. |
| 157 const std::string scheme(StringToLowerASCII(parts->scheme)); | 164 const std::string scheme(StringToLowerASCII(parts->scheme)); |
| 158 parts->scheme = scheme; | 165 parts->scheme = scheme; |
| 159 | 166 |
| 160 if (parts->scheme == std::string(chrome::kFileScheme)) { | 167 if (parts->scheme == std::string(chrome::kFileScheme) && |
| 161 GURL url(std::string(chrome::kFileScheme) + | 168 !parts->is_path_wildcard) { |
| 162 std::string(chrome::kStandardSchemeSeparator) + parts->path); | 169 GURL url(std::string(chrome::kFileScheme) + |
| 163 parts->path = url.path(); | 170 std::string(chrome::kStandardSchemeSeparator) + parts->path); |
| 171 parts->path = url.path(); | |
| 164 } | 172 } |
| 165 | 173 |
| 166 // Canonicalize the host part. | 174 // Canonicalize the host part. |
| 167 const std::string host(parts->host); | 175 const std::string host(parts->host); |
| 168 url_canon::CanonHostInfo host_info; | 176 url_canon::CanonHostInfo host_info; |
| 169 std::string canonicalized_host(net::CanonicalizeHost(host, &host_info)); | 177 std::string canonicalized_host(net::CanonicalizeHost(host, &host_info)); |
| 170 if (host_info.IsIPAddress() && parts->has_domain_wildcard) | 178 if (host_info.IsIPAddress() && parts->has_domain_wildcard) |
| 171 return false; | 179 return false; |
| 172 canonicalized_host = net::TrimEndingDot(canonicalized_host); | 180 canonicalized_host = net::TrimEndingDot(canonicalized_host); |
| 173 | 181 |
| 174 parts->host = ""; | 182 parts->host = ""; |
| 175 if ((host.find('*') == std::string::npos) && | 183 if ((host.find('*') == std::string::npos) && |
| 176 !canonicalized_host.empty()) { | 184 !canonicalized_host.empty()) { |
| 177 // Valid host. | 185 // Valid host. |
| 178 parts->host += canonicalized_host; | 186 parts->host += canonicalized_host; |
| 179 } | 187 } |
| 180 return true; | 188 return true; |
| 181 } | 189 } |
| 182 | 190 |
| 183 // static | 191 // static |
| 184 bool ContentSettingsPattern::Builder::Validate(const PatternParts& parts) { | 192 bool ContentSettingsPattern::Builder::Validate(const PatternParts& parts) { |
| 185 // Sanity checks first: {scheme, port} wildcards imply empty {scheme, port}. | 193 // Sanity checks first: {scheme, port} wildcards imply empty {scheme, port}. |
| 186 if ((parts.is_scheme_wildcard && !parts.scheme.empty()) || | 194 if ((parts.is_scheme_wildcard && !parts.scheme.empty()) || |
| 187 (parts.is_port_wildcard && !parts.port.empty())) { | 195 (parts.is_port_wildcard && !parts.port.empty())) { |
| 188 NOTREACHED(); | 196 NOTREACHED(); |
| 189 return false; | 197 return false; |
| 190 } | 198 } |
| 191 | 199 |
| 192 // file:// URL patterns have an empty host and port. | 200 // file:// URL patterns have an empty host and port. |
| 193 if (parts.scheme == std::string(chrome::kFileScheme)) | 201 if (parts.scheme == std::string(chrome::kFileScheme)) { |
| 194 return parts.host.empty() && | 202 if (parts.has_domain_wildcard || !parts.host.empty() || !parts.port.empty()) |
| 195 parts.port.empty() && | 203 return false; |
| 196 !parts.path.empty() && | 204 else if (parts.is_path_wildcard) |
|
Bernhard Bauer
2012/01/09 13:23:41
The |else| is unnecessary if you have a |return| i
Francois
2012/01/09 14:51:03
Done.
| |
| 197 parts.path != std::string("/") && | 205 return parts.path.empty(); |
| 198 parts.path.find("*") == std::string::npos; | 206 else |
|
Bernhard Bauer
2012/01/09 13:23:41
Same here.
(If you'd keep the else clause, you sh
Francois
2012/01/09 14:51:03
Done.
| |
| 207 return (!parts.path.empty() && | |
| 208 parts.path != "/" && | |
| 209 parts.path.find("*") == std::string::npos); | |
| 210 } | |
| 199 | 211 |
| 200 // If the pattern is for an extension URL test if it is valid. | 212 // If the pattern is for an extension URL test if it is valid. |
| 201 if (parts.scheme == std::string(chrome::kExtensionScheme) && | 213 if (parts.scheme == std::string(chrome::kExtensionScheme) && |
| 202 !parts.host.empty() && | 214 !parts.host.empty() && |
| 203 !parts.has_domain_wildcard && | 215 !parts.has_domain_wildcard && |
| 204 parts.port.empty() && | 216 parts.port.empty() && |
| 205 !parts.is_port_wildcard) { | 217 !parts.is_port_wildcard) { |
| 206 return true; | 218 return true; |
| 207 } | 219 } |
| 208 | 220 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 } | 272 } |
| 261 return true; | 273 return true; |
| 262 } | 274 } |
| 263 | 275 |
| 264 // //////////////////////////////////////////////////////////////////////////// | 276 // //////////////////////////////////////////////////////////////////////////// |
| 265 // ContentSettingsPattern::PatternParts | 277 // ContentSettingsPattern::PatternParts |
| 266 // | 278 // |
| 267 ContentSettingsPattern::PatternParts::PatternParts() | 279 ContentSettingsPattern::PatternParts::PatternParts() |
| 268 : is_scheme_wildcard(false), | 280 : is_scheme_wildcard(false), |
| 269 has_domain_wildcard(false), | 281 has_domain_wildcard(false), |
| 270 is_port_wildcard(false) {} | 282 is_port_wildcard(false), |
| 283 is_path_wildcard(false) {} | |
| 271 | 284 |
| 272 ContentSettingsPattern::PatternParts::~PatternParts() {} | 285 ContentSettingsPattern::PatternParts::~PatternParts() {} |
| 273 | 286 |
| 274 // //////////////////////////////////////////////////////////////////////////// | 287 // //////////////////////////////////////////////////////////////////////////// |
| 275 // ContentSettingsPattern | 288 // ContentSettingsPattern |
| 276 // | 289 // |
| 277 | 290 |
| 278 // The version of the pattern format implemented. Version 1 includes the | 291 // The version of the pattern format implemented. Version 1 includes the |
| 279 // following patterns: | 292 // following patterns: |
| 280 // - [*.]domain.tld (matches domain.tld and all sub-domains) | 293 // - [*.]domain.tld (matches domain.tld and all sub-domains) |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | 377 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( |
| 365 ContentSettingsPattern::CreateBuilder(true)); | 378 ContentSettingsPattern::CreateBuilder(true)); |
| 366 content_settings::PatternParser::Parse(pattern_spec, builder.get()); | 379 content_settings::PatternParser::Parse(pattern_spec, builder.get()); |
| 367 return builder->Build(); | 380 return builder->Build(); |
| 368 } | 381 } |
| 369 | 382 |
| 370 // static | 383 // static |
| 371 ContentSettingsPattern ContentSettingsPattern::Wildcard() { | 384 ContentSettingsPattern ContentSettingsPattern::Wildcard() { |
| 372 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | 385 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( |
| 373 ContentSettingsPattern::CreateBuilder(true)); | 386 ContentSettingsPattern::CreateBuilder(true)); |
| 374 builder->WithSchemeWildcard()->WithDomainWildcard()->WithPortWildcard(); | 387 builder->WithSchemeWildcard()->WithDomainWildcard()->WithPortWildcard()-> |
| 388 WithPathWildcard(); | |
|
Bernhard Bauer
2012/01/09 13:23:41
Nit: I think it's nicer if you align this call wit
Francois
2012/01/09 14:51:03
Done.
| |
| 375 return builder->Build(); | 389 return builder->Build(); |
| 376 } | 390 } |
| 377 | 391 |
| 378 ContentSettingsPattern::ContentSettingsPattern() | 392 ContentSettingsPattern::ContentSettingsPattern() |
| 379 : is_valid_(false) { | 393 : is_valid_(false) { |
| 380 } | 394 } |
| 381 | 395 |
| 382 ContentSettingsPattern::ContentSettingsPattern( | 396 ContentSettingsPattern::ContentSettingsPattern( |
| 383 const PatternParts& parts, | 397 const PatternParts& parts, |
| 384 bool valid) | 398 bool valid) |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 406 // Match the scheme part. | 420 // Match the scheme part. |
| 407 const std::string scheme(url.scheme()); | 421 const std::string scheme(url.scheme()); |
| 408 if (!parts_.is_scheme_wildcard && | 422 if (!parts_.is_scheme_wildcard && |
| 409 parts_.scheme != scheme) { | 423 parts_.scheme != scheme) { |
| 410 return false; | 424 return false; |
| 411 } | 425 } |
| 412 | 426 |
| 413 // File URLs have no host. For file URLs check if the url path matches the | 427 // File URLs have no host. For file URLs check if the url path matches the |
| 414 // path in the pattern. | 428 // path in the pattern. |
| 415 // TODO(markusheintz): This should change in the future. There should be only | 429 // TODO(markusheintz): This should change in the future. There should be only |
| 416 // one setting for all file URLs. So the path should be ignored. | 430 // one setting for all file URLs. So the path should be ignored. |
|
Bernhard Bauer
2012/01/09 13:23:41
Nit: Can you update this comment?
Francois
2012/01/09 14:51:03
Done, but not sure whether the TODO needs to chang
markusheintz_
2012/01/09 15:01:00
Please also remove the TODO completely. Thanks
Francois
2012/01/09 15:33:01
Done.
| |
| 417 if (!parts_.is_scheme_wildcard && | 431 if (!parts_.is_scheme_wildcard && |
| 418 scheme == std::string(chrome::kFileScheme)) { | 432 scheme == std::string(chrome::kFileScheme)) { |
| 419 if (parts_.path == std::string(url.path())) | 433 if (parts_.is_path_wildcard || parts_.path == std::string(url.path())) |
|
markusheintz_
2012/01/09 15:01:00
Nit: You could get rid of the if. Just
" return p
Francois
2012/01/09 15:33:01
Done.
| |
| 420 return true; | 434 return true; |
| 421 return false; | 435 return false; |
| 422 } | 436 } |
| 423 | 437 |
| 424 // Match the host part. | 438 // Match the host part. |
| 425 const std::string host(net::TrimEndingDot(url.host())); | 439 const std::string host(net::TrimEndingDot(url.host())); |
| 426 if (!parts_.has_domain_wildcard) { | 440 if (!parts_.has_domain_wildcard) { |
| 427 if (parts_.host != host) | 441 if (parts_.host != host) |
| 428 return false; | 442 return false; |
| 429 } else { | 443 } else { |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 630 if (!parts.is_port_wildcard && other_parts.is_port_wildcard) | 644 if (!parts.is_port_wildcard && other_parts.is_port_wildcard) |
| 631 return ContentSettingsPattern::PREDECESSOR; | 645 return ContentSettingsPattern::PREDECESSOR; |
| 632 | 646 |
| 633 int result = parts.port.compare(other_parts.port); | 647 int result = parts.port.compare(other_parts.port); |
| 634 if (result == 0) | 648 if (result == 0) |
| 635 return ContentSettingsPattern::IDENTITY; | 649 return ContentSettingsPattern::IDENTITY; |
| 636 if (result > 0) | 650 if (result > 0) |
| 637 return ContentSettingsPattern::DISJOINT_ORDER_PRE; | 651 return ContentSettingsPattern::DISJOINT_ORDER_PRE; |
| 638 return ContentSettingsPattern::DISJOINT_ORDER_POST; | 652 return ContentSettingsPattern::DISJOINT_ORDER_POST; |
| 639 } | 653 } |
| OLD | NEW |