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

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

Issue 7049032: Make URLPattern::OverlapsWith handle wildcards better by expanding them to explicit schemes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 7 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 | Annotate | Revision Log
OLDNEW
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/extensions/url_pattern.h" 5 #include "chrome/common/extensions/url_pattern.h"
6 6
7 #include "base/string_piece.h" 7 #include "base/string_piece.h"
8 #include "base/string_split.h" 8 #include "base/string_split.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "chrome/common/url_constants.h" 10 #include "chrome/common/url_constants.h"
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 spec += host_; 304 spec += host_;
305 } 305 }
306 306
307 if (!path_.empty()) 307 if (!path_.empty())
308 spec += path_; 308 spec += path_;
309 309
310 return spec; 310 return spec;
311 } 311 }
312 312
313 bool URLPattern::OverlapsWith(const URLPattern& other) const { 313 bool URLPattern::OverlapsWith(const URLPattern& other) const {
314 if (!MatchesScheme(other.scheme_) && !other.MatchesScheme(scheme_)) 314 if (!MatchesAnyScheme(other.GetExplicitSchemes()) &&
315 !other.MatchesAnyScheme(GetExplicitSchemes())) {
315 return false; 316 return false;
317 }
316 318
317 if (!MatchesHost(other.host()) && !other.MatchesHost(host_)) 319 if (!MatchesHost(other.host()) && !other.MatchesHost(host_))
318 return false; 320 return false;
319 321
320 // We currently only use OverlapsWith() for the patterns inside 322 // We currently only use OverlapsWith() for the patterns inside
321 // ExtensionExtent. In those cases, we know that the path will have only a 323 // ExtensionExtent. In those cases, we know that the path will have only a
322 // single wildcard at the end. This makes figuring out overlap much easier. It 324 // single wildcard at the end. This makes figuring out overlap much easier. It
323 // seems like there is probably a computer-sciency way to solve the general 325 // seems like there is probably a computer-sciency way to solve the general
324 // case, but we don't need that yet. 326 // case, but we don't need that yet.
325 DCHECK(path_.find('*') == path_.size() - 1); 327 DCHECK(path_.find('*') == path_.size() - 1);
326 DCHECK(other.path().find('*') == other.path().size() - 1); 328 DCHECK(other.path().find('*') == other.path().size() - 1);
327 329
328 if (!MatchesPath(other.path().substr(0, other.path().size() - 1)) && 330 if (!MatchesPath(other.path().substr(0, other.path().size() - 1)) &&
329 !other.MatchesPath(path_.substr(0, path_.size() - 1))) 331 !other.MatchesPath(path_.substr(0, path_.size() - 1)))
330 return false; 332 return false;
331 333
332 return true; 334 return true;
333 } 335 }
334 336
335 std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const { 337 bool URLPattern::MatchesAnyScheme(
336 std::vector<URLPattern> result; 338 const std::vector<std::string>& schemes) const {
339 for (std::vector<std::string>::const_iterator i = schemes.begin();
340 i != schemes.end(); ++i) {
341 if (MatchesScheme(*i))
342 return true;
343 }
344
345 return false;
346 }
347
348 std::vector<std::string> URLPattern::GetExplicitSchemes() const {
349 std::vector<std::string> result;
337 350
338 if (scheme_ != "*" && !match_all_urls_ && IsValidScheme(scheme_)) { 351 if (scheme_ != "*" && !match_all_urls_ && IsValidScheme(scheme_)) {
339 result.push_back(*this); 352 result.push_back(scheme_);
340 return result; 353 return result;
341 } 354 }
342 355
343 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { 356 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) {
344 if (MatchesScheme(kValidSchemes[i])) { 357 if (MatchesScheme(kValidSchemes[i])) {
345 URLPattern temp = *this; 358 result.push_back(kValidSchemes[i]);
346 temp.SetScheme(kValidSchemes[i]);
347 temp.set_match_all_urls(false);
348 result.push_back(temp);
349 } 359 }
350 } 360 }
351 361
352 return result; 362 return result;
353 } 363 }
354 364
365 std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const {
366 std::vector<std::string> explicit_schemes = GetExplicitSchemes();
367 std::vector<URLPattern> result;
368
369 for (std::vector<std::string>::const_iterator i = explicit_schemes.begin();
370 i != explicit_schemes.end(); ++i) {
371 URLPattern temp = *this;
372 temp.SetScheme(*i);
373 temp.set_match_all_urls(false);
374 result.push_back(temp);
375 }
376
377 return result;
378 }
379
355 // static 380 // static
356 const char* URLPattern::GetParseResultString( 381 const char* URLPattern::GetParseResultString(
357 URLPattern::ParseResult parse_result) { 382 URLPattern::ParseResult parse_result) {
358 return kParseResultMessages[parse_result]; 383 return kParseResultMessages[parse_result];
359 } 384 }
OLDNEW
« no previous file with comments | « chrome/common/extensions/url_pattern.h ('k') | chrome/common/extensions/url_pattern_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698