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/extensions/url_pattern.h" | 5 #include "chrome/common/extensions/url_pattern.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 if (!base::StringToInt(port, &parsed_port)) | 90 if (!base::StringToInt(port, &parsed_port)) |
91 return false; | 91 return false; |
92 return (parsed_port >= 0) && (parsed_port < 65536); | 92 return (parsed_port >= 0) && (parsed_port < 65536); |
93 } | 93 } |
94 | 94 |
95 } // namespace | 95 } // namespace |
96 | 96 |
97 URLPattern::URLPattern() | 97 URLPattern::URLPattern() |
98 : valid_schemes_(SCHEME_NONE), | 98 : valid_schemes_(SCHEME_NONE), |
99 match_all_urls_(false), | 99 match_all_urls_(false), |
| 100 partial_filesystem_support_hack_(false), |
100 match_subdomains_(false), | 101 match_subdomains_(false), |
101 port_("*") {} | 102 port_("*") {} |
102 | 103 |
103 URLPattern::URLPattern(int valid_schemes) | 104 URLPattern::URLPattern(int valid_schemes) |
104 : valid_schemes_(valid_schemes), | 105 : valid_schemes_(valid_schemes), |
105 match_all_urls_(false), | 106 match_all_urls_(false), |
| 107 partial_filesystem_support_hack_(false), |
106 match_subdomains_(false), | 108 match_subdomains_(false), |
107 port_("*") {} | 109 port_("*") {} |
108 | 110 |
109 URLPattern::URLPattern(int valid_schemes, const std::string& pattern) | 111 URLPattern::URLPattern(int valid_schemes, const std::string& pattern) |
110 // Strict error checking is used, because this constructor is only | 112 // Strict error checking is used, because this constructor is only |
111 // appropriate when we know |pattern| is valid. | 113 // appropriate when we know |pattern| is valid. |
112 : valid_schemes_(valid_schemes), | 114 : valid_schemes_(valid_schemes), |
113 match_all_urls_(false), | 115 match_all_urls_(false), |
| 116 partial_filesystem_support_hack_(false), |
114 match_subdomains_(false), | 117 match_subdomains_(false), |
115 port_("*") { | 118 port_("*") { |
116 if (PARSE_SUCCESS != Parse(pattern)) | 119 if (PARSE_SUCCESS != Parse(pattern)) |
117 NOTREACHED() << "URLPattern is invalid: " << pattern; | 120 NOTREACHED() << "URLPattern is invalid: " << pattern; |
118 } | 121 } |
119 | 122 |
120 URLPattern::~URLPattern() { | 123 URLPattern::~URLPattern() { |
121 } | 124 } |
122 | 125 |
123 bool URLPattern::operator<(const URLPattern& other) const { | 126 bool URLPattern::operator<(const URLPattern& other) const { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 bool URLPattern::SetPort(const std::string& port) { | 291 bool URLPattern::SetPort(const std::string& port) { |
289 spec_.clear(); | 292 spec_.clear(); |
290 if (IsValidPortForScheme(scheme_, port)) { | 293 if (IsValidPortForScheme(scheme_, port)) { |
291 port_ = port; | 294 port_ = port; |
292 return true; | 295 return true; |
293 } | 296 } |
294 return false; | 297 return false; |
295 } | 298 } |
296 | 299 |
297 bool URLPattern::MatchesURL(const GURL& test) const { | 300 bool URLPattern::MatchesURL(const GURL& test) const { |
298 if (!MatchesScheme(test.scheme())) | 301 const GURL* test_url = &test; |
| 302 bool has_inner_url = test.inner_url() != NULL; |
| 303 |
| 304 if (partial_filesystem_support_hack_ != has_inner_url) |
| 305 return false; |
| 306 |
| 307 if (has_inner_url) |
| 308 test_url = test.inner_url(); |
| 309 |
| 310 if (!MatchesScheme(test_url->scheme())) |
299 return false; | 311 return false; |
300 | 312 |
301 if (match_all_urls_) | 313 if (match_all_urls_) |
302 return true; | 314 return true; |
303 | 315 |
304 return MatchesSecurityOriginHelper(test) && | 316 std::string path_for_request = test.PathForRequest(); |
305 MatchesPath(test.PathForRequest()); | 317 if (has_inner_url) |
| 318 path_for_request = test_url->path() + path_for_request; |
| 319 |
| 320 return MatchesSecurityOriginHelper(*test_url) && |
| 321 MatchesPath(path_for_request); |
306 } | 322 } |
307 | 323 |
308 bool URLPattern::MatchesSecurityOrigin(const GURL& test) const { | 324 bool URLPattern::MatchesSecurityOrigin(const GURL& test) const { |
309 if (!MatchesScheme(test.scheme())) | 325 if (!MatchesScheme(test.scheme())) |
310 return false; | 326 return false; |
311 | 327 |
312 if (match_all_urls_) | 328 if (match_all_urls_) |
313 return true; | 329 return true; |
314 | 330 |
315 return MatchesSecurityOriginHelper(test); | 331 return MatchesSecurityOriginHelper(test); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 return false; | 442 return false; |
427 | 443 |
428 // We currently only use OverlapsWith() for the patterns inside | 444 // We currently only use OverlapsWith() for the patterns inside |
429 // URLPatternSet. In those cases, we know that the path will have only a | 445 // URLPatternSet. In those cases, we know that the path will have only a |
430 // single wildcard at the end. This makes figuring out overlap much easier. It | 446 // single wildcard at the end. This makes figuring out overlap much easier. It |
431 // seems like there is probably a computer-sciency way to solve the general | 447 // seems like there is probably a computer-sciency way to solve the general |
432 // case, but we don't need that yet. | 448 // case, but we don't need that yet. |
433 DCHECK(path_.find('*') == path_.size() - 1); | 449 DCHECK(path_.find('*') == path_.size() - 1); |
434 DCHECK(other.path().find('*') == other.path().size() - 1); | 450 DCHECK(other.path().find('*') == other.path().size() - 1); |
435 | 451 |
| 452 if (partial_filesystem_support_hack_ != |
| 453 other.partial_filesystem_support_hack()) |
| 454 return false; |
| 455 |
436 if (!MatchesPath(other.path().substr(0, other.path().size() - 1)) && | 456 if (!MatchesPath(other.path().substr(0, other.path().size() - 1)) && |
437 !other.MatchesPath(path_.substr(0, path_.size() - 1))) | 457 !other.MatchesPath(path_.substr(0, path_.size() - 1))) |
438 return false; | 458 return false; |
439 | 459 |
440 return true; | 460 return true; |
441 } | 461 } |
442 | 462 |
443 bool URLPattern::MatchesAnyScheme( | 463 bool URLPattern::MatchesAnyScheme( |
444 const std::vector<std::string>& schemes) const { | 464 const std::vector<std::string>& schemes) const { |
445 for (std::vector<std::string>::const_iterator i = schemes.begin(); | 465 for (std::vector<std::string>::const_iterator i = schemes.begin(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 } | 512 } |
493 | 513 |
494 return result; | 514 return result; |
495 } | 515 } |
496 | 516 |
497 // static | 517 // static |
498 const char* URLPattern::GetParseResultString( | 518 const char* URLPattern::GetParseResultString( |
499 URLPattern::ParseResult parse_result) { | 519 URLPattern::ParseResult parse_result) { |
500 return kParseResultMessages[parse_result]; | 520 return kParseResultMessages[parse_result]; |
501 } | 521 } |
OLD | NEW |