Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(695)

Side by Side Diff: extensions/common/url_pattern.cc

Issue 2499493004: Communicate ExtensionSettings policy to renderers (Closed)
Patch Set: Fixed static non-pod, removed default params, fixed formatting, fixed nits, simplified code Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "extensions/common/url_pattern.h" 5 #include "extensions/common/url_pattern.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <ostream> 9 #include <ostream>
10 10
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 int result = 0; 136 int result = 0;
137 for (size_t i = 0; i < arraysize(kValidSchemeMasks); ++i) 137 for (size_t i = 0; i < arraysize(kValidSchemeMasks); ++i)
138 result |= kValidSchemeMasks[i]; 138 result |= kValidSchemeMasks[i];
139 return result; 139 return result;
140 } 140 }
141 141
142 URLPattern::URLPattern() 142 URLPattern::URLPattern()
143 : valid_schemes_(SCHEME_NONE), 143 : valid_schemes_(SCHEME_NONE),
144 match_all_urls_(false), 144 match_all_urls_(false),
145 match_subdomains_(false), 145 match_subdomains_(false),
146 match_effective_tld_(true),
146 port_("*") {} 147 port_("*") {}
147 148
148 URLPattern::URLPattern(int valid_schemes) 149 URLPattern::URLPattern(int valid_schemes)
149 : valid_schemes_(valid_schemes), 150 : valid_schemes_(valid_schemes),
150 match_all_urls_(false), 151 match_all_urls_(false),
151 match_subdomains_(false), 152 match_subdomains_(false),
153 match_effective_tld_(true),
152 port_("*") {} 154 port_("*") {}
153 155
154 URLPattern::URLPattern(int valid_schemes, const std::string& pattern) 156 URLPattern::URLPattern(int valid_schemes, const std::string& pattern)
155 // Strict error checking is used, because this constructor is only 157 // Strict error checking is used, because this constructor is only
156 // appropriate when we know |pattern| is valid. 158 // appropriate when we know |pattern| is valid.
157 : valid_schemes_(valid_schemes), 159 : valid_schemes_(valid_schemes),
158 match_all_urls_(false), 160 match_all_urls_(false),
159 match_subdomains_(false), 161 match_subdomains_(false),
162 match_effective_tld_(true),
160 port_("*") { 163 port_("*") {
161 ParseResult result = Parse(pattern); 164 ParseResult result = Parse(pattern);
162 if (PARSE_SUCCESS != result) 165 if (PARSE_SUCCESS != result)
163 NOTREACHED() << "URLPattern invalid: " << pattern << " result " << result; 166 NOTREACHED() << "URLPattern invalid: " << pattern << " result " << result;
164 } 167 }
165 168
166 URLPattern::URLPattern(const URLPattern& other) = default; 169 URLPattern::URLPattern(const URLPattern& other) = default;
167 170
168 URLPattern::~URLPattern() { 171 URLPattern::~URLPattern() {
169 } 172 }
170 173
171 bool URLPattern::operator<(const URLPattern& other) const { 174 bool URLPattern::operator<(const URLPattern& other) const {
172 return GetAsString() < other.GetAsString(); 175 return GetAsString() < other.GetAsString();
173 } 176 }
174 177
175 bool URLPattern::operator>(const URLPattern& other) const { 178 bool URLPattern::operator>(const URLPattern& other) const {
176 return GetAsString() > other.GetAsString(); 179 return GetAsString() > other.GetAsString();
177 } 180 }
178 181
179 bool URLPattern::operator==(const URLPattern& other) const { 182 bool URLPattern::operator==(const URLPattern& other) const {
180 return GetAsString() == other.GetAsString(); 183 return GetAsString() == other.GetAsString();
181 } 184 }
182 185
183 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) { 186 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) {
184 return out << '"' << url_pattern.GetAsString() << '"'; 187 return out << '"' << url_pattern.GetAsString() << '"';
185 } 188 }
186 189
187 URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) { 190 URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) {
191 return Parse(pattern, false);
192 }
193
194 URLPattern::ParseResult URLPattern::Parse(
195 const std::string& pattern,
196 const bool allow_wildcard_effective_tld) {
188 spec_.clear(); 197 spec_.clear();
189 SetMatchAllURLs(false); 198 SetMatchAllURLs(false);
190 SetMatchSubdomains(false); 199 SetMatchSubdomains(false);
200 SetMatchEffectiveTld(true);
191 SetPort("*"); 201 SetPort("*");
192 202
193 // Special case pattern to match every valid URL. 203 // Special case pattern to match every valid URL.
194 if (pattern == kAllUrlsPattern) { 204 if (pattern == kAllUrlsPattern) {
195 SetMatchAllURLs(true); 205 SetMatchAllURLs(true);
196 return PARSE_SUCCESS; 206 return PARSE_SUCCESS;
197 } 207 }
198 208
199 // Parse out the scheme. 209 // Parse out the scheme.
200 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator); 210 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 // Could be empty if the host only consists of whitespace characters. 269 // Could be empty if the host only consists of whitespace characters.
260 if (host_components.empty() || 270 if (host_components.empty() ||
261 (host_components.size() == 1 && host_components[0].empty())) 271 (host_components.size() == 1 && host_components[0].empty()))
262 return PARSE_ERROR_EMPTY_HOST; 272 return PARSE_ERROR_EMPTY_HOST;
263 273
264 if (host_components[0] == "*") { 274 if (host_components[0] == "*") {
265 match_subdomains_ = true; 275 match_subdomains_ = true;
266 host_components.erase(host_components.begin(), 276 host_components.erase(host_components.begin(),
267 host_components.begin() + 1); 277 host_components.begin() + 1);
268 } 278 }
279
280 // If explicitly allowed, the last component can optionally be '*' to
281 // match all effective TLDs.
282 if (allow_wildcard_effective_tld && host_components.size() > 1 &&
283 host_components[1] == "*") {
284 match_effective_tld_ = false;
285 host_components.pop_back();
286 }
269 host_ = base::JoinString(host_components, "."); 287 host_ = base::JoinString(host_components, ".");
270 288
271 path_start_pos = host_end_pos; 289 path_start_pos = host_end_pos;
272 } 290 }
273 291
274 SetPath(pattern.substr(path_start_pos)); 292 SetPath(pattern.substr(path_start_pos));
275 293
276 size_t port_pos = host_.find(':'); 294 size_t port_pos = host_.find(':');
277 if (port_pos != std::string::npos) { 295 if (port_pos != std::string::npos) {
278 if (!SetPort(host_.substr(port_pos + 1))) 296 if (!SetPort(host_.substr(port_pos + 1)))
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 host_.clear(); 331 host_.clear();
314 SetPath("/*"); 332 SetPath("/*");
315 } 333 }
316 } 334 }
317 335
318 void URLPattern::SetMatchSubdomains(bool val) { 336 void URLPattern::SetMatchSubdomains(bool val) {
319 spec_.clear(); 337 spec_.clear();
320 match_subdomains_ = val; 338 match_subdomains_ = val;
321 } 339 }
322 340
341 void URLPattern::SetMatchEffectiveTld(bool val) {
342 spec_.clear();
343 match_effective_tld_ = val;
344 }
345
323 bool URLPattern::SetScheme(const std::string& scheme) { 346 bool URLPattern::SetScheme(const std::string& scheme) {
324 spec_.clear(); 347 spec_.clear();
325 scheme_ = scheme; 348 scheme_ = scheme;
326 if (scheme_ == "*") { 349 if (scheme_ == "*") {
327 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); 350 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS);
328 } else if (!IsValidScheme(scheme_)) { 351 } else if (!IsValidScheme(scheme_)) {
329 return false; 352 return false;
330 } 353 }
331 return true; 354 return true;
332 } 355 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 435
413 bool URLPattern::MatchesHost(const std::string& host) const { 436 bool URLPattern::MatchesHost(const std::string& host) const {
414 std::string test(url::kHttpScheme); 437 std::string test(url::kHttpScheme);
415 test += url::kStandardSchemeSeparator; 438 test += url::kStandardSchemeSeparator;
416 test += host; 439 test += host;
417 test += "/"; 440 test += "/";
418 return MatchesHost(GURL(test)); 441 return MatchesHost(GURL(test));
419 } 442 }
420 443
421 bool URLPattern::MatchesHost(const GURL& test) const { 444 bool URLPattern::MatchesHost(const GURL& test) const {
422 const base::StringPiece test_host( 445 base::StringPiece test_host(CanonicalizeHostForMatching(test.host_piece()));
423 CanonicalizeHostForMatching(test.host_piece()));
424 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_)); 446 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_));
425 447
448 // If we don't care about matching the effective TLD, remove it.
449 if (!match_effective_tld_) {
450 int reg_length = net::registry_controlled_domains::GetRegistryLength(
451 test, net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
452 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
453 if (reg_length > 0) {
454 test_host = test_host.substr(0, test_host.size() - reg_length - 1);
455 }
456 }
457
426 // If the hosts are exactly equal, we have a match. 458 // If the hosts are exactly equal, we have a match.
427 if (test_host == pattern_host) 459 if (test_host == pattern_host)
428 return true; 460 return true;
429 461
430 // If we're matching subdomains, and we have no host in the match pattern, 462 // If we're matching subdomains, and we have no host in the match pattern,
431 // that means that we're matching all hosts, which means we have a match no 463 // that means that we're matching all hosts, which means we have a match no
432 // matter what the test host is. 464 // matter what the test host is.
433 if (match_subdomains_ && pattern_host.empty()) 465 if (match_subdomains_ && pattern_host.empty())
434 return true; 466 return true;
435 467
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 if (scheme_ != url::kFileScheme && standard_scheme) { 547 if (scheme_ != url::kFileScheme && standard_scheme) {
516 if (match_subdomains_) { 548 if (match_subdomains_) {
517 spec += "*"; 549 spec += "*";
518 if (!host_.empty()) 550 if (!host_.empty())
519 spec += "."; 551 spec += ".";
520 } 552 }
521 553
522 if (!host_.empty()) 554 if (!host_.empty())
523 spec += host_; 555 spec += host_;
524 556
557 if (!match_effective_tld_) {
558 if (!host_.empty())
559 spec += ".";
560 spec += "*";
561 }
562
525 if (port_ != "*") { 563 if (port_ != "*") {
526 spec += ":"; 564 spec += ":";
527 spec += port_; 565 spec += port_;
528 } 566 }
529 } 567 }
530 568
531 if (!path_.empty()) 569 if (!path_.empty())
532 spec += path_; 570 spec += path_;
533 571
534 spec_ = spec; 572 spec_ = spec;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 } 661 }
624 662
625 return result; 663 return result;
626 } 664 }
627 665
628 // static 666 // static
629 const char* URLPattern::GetParseResultString( 667 const char* URLPattern::GetParseResultString(
630 URLPattern::ParseResult parse_result) { 668 URLPattern::ParseResult parse_result) {
631 return kParseResultMessages[parse_result]; 669 return kParseResultMessages[parse_result];
632 } 670 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698