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

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

Issue 2499493004: Communicate ExtensionSettings policy to renderers (Closed)
Patch Set: Style fixes, prevent heap leak. 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, DENY_WILDCARD_FOR_EFFECTIVE_TLD);
192 }
193
194 URLPattern::ParseResult URLPattern::Parse(const std::string& pattern,
195 const ParseOptions parse_options) {
188 spec_.clear(); 196 spec_.clear();
189 SetMatchAllURLs(false); 197 SetMatchAllURLs(false);
190 SetMatchSubdomains(false); 198 SetMatchSubdomains(false);
199 SetMatchEffectiveTld(true);
191 SetPort("*"); 200 SetPort("*");
192 201
193 // Special case pattern to match every valid URL. 202 // Special case pattern to match every valid URL.
194 if (pattern == kAllUrlsPattern) { 203 if (pattern == kAllUrlsPattern) {
195 SetMatchAllURLs(true); 204 SetMatchAllURLs(true);
196 return PARSE_SUCCESS; 205 return PARSE_SUCCESS;
197 } 206 }
198 207
199 // Parse out the scheme. 208 // Parse out the scheme.
200 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator); 209 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. 268 // Could be empty if the host only consists of whitespace characters.
260 if (host_components.empty() || 269 if (host_components.empty() ||
261 (host_components.size() == 1 && host_components[0].empty())) 270 (host_components.size() == 1 && host_components[0].empty()))
262 return PARSE_ERROR_EMPTY_HOST; 271 return PARSE_ERROR_EMPTY_HOST;
263 272
264 if (host_components[0] == "*") { 273 if (host_components[0] == "*") {
265 match_subdomains_ = true; 274 match_subdomains_ = true;
266 host_components.erase(host_components.begin(), 275 host_components.erase(host_components.begin(),
267 host_components.begin() + 1); 276 host_components.begin() + 1);
268 } 277 }
278
279 // If explicitly allowed, the last component can optionally be '*' to
280 // match all effective TLDs.
281 if (parse_options == ALLOW_WILDCARD_FOR_EFFECTIVE_TLD &&
282 host_components.size() > 1 && host_components[1] == "*") {
Devlin 2017/02/14 23:17:10 Won't this fail on something like maps.google.*, w
nrpeter 2017/03/22 23:47:39 Yes, updated. Added as a test case as well.
283 match_effective_tld_ = false;
284 host_components.pop_back();
285 }
269 host_ = base::JoinString(host_components, "."); 286 host_ = base::JoinString(host_components, ".");
270 287
271 path_start_pos = host_end_pos; 288 path_start_pos = host_end_pos;
272 } 289 }
273 290
274 SetPath(pattern.substr(path_start_pos)); 291 SetPath(pattern.substr(path_start_pos));
275 292
276 size_t port_pos = host_.find(':'); 293 size_t port_pos = host_.find(':');
277 if (port_pos != std::string::npos) { 294 if (port_pos != std::string::npos) {
278 if (!SetPort(host_.substr(port_pos + 1))) 295 if (!SetPort(host_.substr(port_pos + 1)))
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 host_.clear(); 330 host_.clear();
314 SetPath("/*"); 331 SetPath("/*");
315 } 332 }
316 } 333 }
317 334
318 void URLPattern::SetMatchSubdomains(bool val) { 335 void URLPattern::SetMatchSubdomains(bool val) {
319 spec_.clear(); 336 spec_.clear();
320 match_subdomains_ = val; 337 match_subdomains_ = val;
321 } 338 }
322 339
340 void URLPattern::SetMatchEffectiveTld(bool val) {
341 spec_.clear();
342 match_effective_tld_ = val;
343 }
344
323 bool URLPattern::SetScheme(const std::string& scheme) { 345 bool URLPattern::SetScheme(const std::string& scheme) {
324 spec_.clear(); 346 spec_.clear();
325 scheme_ = scheme; 347 scheme_ = scheme;
326 if (scheme_ == "*") { 348 if (scheme_ == "*") {
327 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); 349 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS);
328 } else if (!IsValidScheme(scheme_)) { 350 } else if (!IsValidScheme(scheme_)) {
329 return false; 351 return false;
330 } 352 }
331 return true; 353 return true;
332 } 354 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 434
413 bool URLPattern::MatchesHost(const std::string& host) const { 435 bool URLPattern::MatchesHost(const std::string& host) const {
414 std::string test(url::kHttpScheme); 436 std::string test(url::kHttpScheme);
415 test += url::kStandardSchemeSeparator; 437 test += url::kStandardSchemeSeparator;
416 test += host; 438 test += host;
417 test += "/"; 439 test += "/";
418 return MatchesHost(GURL(test)); 440 return MatchesHost(GURL(test));
419 } 441 }
420 442
421 bool URLPattern::MatchesHost(const GURL& test) const { 443 bool URLPattern::MatchesHost(const GURL& test) const {
422 const base::StringPiece test_host( 444 base::StringPiece test_host(CanonicalizeHostForMatching(test.host_piece()));
423 CanonicalizeHostForMatching(test.host_piece()));
424 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_)); 445 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_));
425 446
447 // If we don't care about matching the effective TLD, remove it.
448 if (!match_effective_tld_) {
449 int reg_length = net::registry_controlled_domains::GetRegistryLength(
450 test, net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
451 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
452 if (reg_length > 0) {
453 test_host = test_host.substr(0, test_host.size() - reg_length - 1);
454 }
455 }
456
426 // If the hosts are exactly equal, we have a match. 457 // If the hosts are exactly equal, we have a match.
427 if (test_host == pattern_host) 458 if (test_host == pattern_host)
428 return true; 459 return true;
429 460
430 // If we're matching subdomains, and we have no host in the match pattern, 461 // 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 462 // that means that we're matching all hosts, which means we have a match no
432 // matter what the test host is. 463 // matter what the test host is.
433 if (match_subdomains_ && pattern_host.empty()) 464 if (match_subdomains_ && pattern_host.empty())
434 return true; 465 return true;
435 466
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 if (scheme_ != url::kFileScheme && standard_scheme) { 546 if (scheme_ != url::kFileScheme && standard_scheme) {
516 if (match_subdomains_) { 547 if (match_subdomains_) {
517 spec += "*"; 548 spec += "*";
518 if (!host_.empty()) 549 if (!host_.empty())
519 spec += "."; 550 spec += ".";
520 } 551 }
521 552
522 if (!host_.empty()) 553 if (!host_.empty())
523 spec += host_; 554 spec += host_;
524 555
556 if (!match_effective_tld_) {
557 if (!host_.empty())
558 spec += ".";
559 spec += "*";
560 }
561
525 if (port_ != "*") { 562 if (port_ != "*") {
526 spec += ":"; 563 spec += ":";
527 spec += port_; 564 spec += port_;
528 } 565 }
529 } 566 }
530 567
531 if (!path_.empty()) 568 if (!path_.empty())
532 spec += path_; 569 spec += path_;
533 570
534 spec_ = spec; 571 spec_ = spec;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 } 660 }
624 661
625 return result; 662 return result;
626 } 663 }
627 664
628 // static 665 // static
629 const char* URLPattern::GetParseResultString( 666 const char* URLPattern::GetParseResultString(
630 URLPattern::ParseResult parse_result) { 667 URLPattern::ParseResult parse_result) {
631 return kParseResultMessages[parse_result]; 668 return kParseResultMessages[parse_result];
632 } 669 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698