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

Side by Side Diff: components/omnibox/autocomplete_match.cc

Issue 1098843004: Omnibox - Do Not Allow HTTP/HTTPS Equivalence if User Explicitly Entered A Scheme (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use StartsWithASCII Created 5 years, 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/omnibox/autocomplete_match.h" 5 #include "components/omnibox/autocomplete_match.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/i18n/time_formatting.h" 8 #include "base/i18n/time_formatting.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
(...skipping 10 matching lines...) Expand all
21 #include "grit/components_scaled_resources.h" 21 #include "grit/components_scaled_resources.h"
22 22
23 namespace { 23 namespace {
24 24
25 bool IsTrivialClassification(const ACMatchClassifications& classifications) { 25 bool IsTrivialClassification(const ACMatchClassifications& classifications) {
26 return classifications.empty() || 26 return classifications.empty() ||
27 ((classifications.size() == 1) && 27 ((classifications.size() == 1) &&
28 (classifications.back().style == ACMatchClassification::NONE)); 28 (classifications.back().style == ACMatchClassification::NONE));
29 } 29 }
30 30
31 // Returns true if one of the |terms_prefixed_by_http_or_https| matches the
32 // beginning of the URL (sans scheme). (Recall that
33 // |terms_prefixed_by_http_or_https|, for the input "http://a b" will be
34 // ["a"].) This suggests that the user wants a particular URL with a scheme
35 // in mind, hence the caller should not consider another URL like this one
36 // but with a different scheme to be a duplicate.
37 bool WordMatchesURLContent(
38 const std::vector<std::string>& terms_prefixed_by_http_or_https,
39 const GURL& url) {
40 const size_t prefix_length =
41 url.scheme().length() + strlen(url::kStandardSchemeSeparator);
42 DCHECK_GE(url.spec().length(), prefix_length);
43 const std::string& url_spec_without_scheme = url.spec().substr(prefix_length);
44 for (const auto& term : terms_prefixed_by_http_or_https) {
45 if (base::StartsWithASCII(url_spec_without_scheme, term, false))
46 return true;
47 }
48 return false;
49 }
50
31 } // namespace 51 } // namespace
32 52
33 // AutocompleteMatch ---------------------------------------------------------- 53 // AutocompleteMatch ----------------------------------------------------------
34 54
35 // static 55 // static
36 const base::char16 AutocompleteMatch::kInvalidChars[] = { 56 const base::char16 AutocompleteMatch::kInvalidChars[] = {
37 '\n', '\r', '\t', 57 '\n', '\r', '\t',
38 0x2028, // Line separator 58 0x2028, // Line separator
39 0x2029, // Paragraph separator 59 0x2029, // Paragraph separator
40 0 60 0
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 return NULL; 385 return NULL;
366 TemplateURL* template_url = keyword.empty() ? 386 TemplateURL* template_url = keyword.empty() ?
367 NULL : template_url_service->GetTemplateURLForKeyword(keyword); 387 NULL : template_url_service->GetTemplateURLForKeyword(keyword);
368 return (template_url || host.empty()) ? 388 return (template_url || host.empty()) ?
369 template_url : template_url_service->GetTemplateURLForHost(host); 389 template_url : template_url_service->GetTemplateURLForHost(host);
370 } 390 }
371 391
372 // static 392 // static
373 GURL AutocompleteMatch::GURLToStrippedGURL( 393 GURL AutocompleteMatch::GURLToStrippedGURL(
374 const GURL& url, 394 const GURL& url,
395 const AutocompleteInput& input,
375 TemplateURLService* template_url_service, 396 TemplateURLService* template_url_service,
376 const base::string16& keyword) { 397 const base::string16& keyword) {
377 if (!url.is_valid()) 398 if (!url.is_valid())
378 return url; 399 return url;
379 400
380 GURL stripped_destination_url = url; 401 GURL stripped_destination_url = url;
381 402
382 // If the destination URL looks like it was generated from a TemplateURL, 403 // If the destination URL looks like it was generated from a TemplateURL,
383 // remove all substitutions other than the search terms. This allows us 404 // remove all substitutions other than the search terms. This allows us
384 // to eliminate cases like past search URLs from history that differ only 405 // to eliminate cases like past search URLs from history that differ only
(...skipping 25 matching lines...) Expand all
410 431
411 // Remove the www. prefix from the host. 432 // Remove the www. prefix from the host.
412 static const char prefix[] = "www."; 433 static const char prefix[] = "www.";
413 static const size_t prefix_len = arraysize(prefix) - 1; 434 static const size_t prefix_len = arraysize(prefix) - 1;
414 std::string host = stripped_destination_url.host(); 435 std::string host = stripped_destination_url.host();
415 if (host.compare(0, prefix_len, prefix) == 0) { 436 if (host.compare(0, prefix_len, prefix) == 0) {
416 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len)); 437 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len));
417 needs_replacement = true; 438 needs_replacement = true;
418 } 439 }
419 440
420 // Replace https protocol with http protocol. 441 // Remove any trailing slash (if it's not a lone slash), or add a slash (to
421 if (stripped_destination_url.SchemeIs(url::kHttpsScheme)) { 442 // make a lone slash) if the path is empty. (We can't unconditionally
443 // remove even lone slashes because for some schemes the path must consist
444 // of at least a slash.)
445 const std::string& path = stripped_destination_url.path();
446 if ((path.length() > 1) && (path[path.length() - 1] == '/')) {
447 replacements.SetPathStr(
448 base::StringPiece(path).substr(0, path.length() - 1));
449 needs_replacement = true;
450 } else if (path.empty()) {
451 static const char slash[] = "/";
452 replacements.SetPathStr(base::StringPiece(slash));
453 needs_replacement = true;
454 }
455
456 // Replace https protocol with http, as long as the user didn't explicitly
457 // specify one of the two.
458 if (stripped_destination_url.SchemeIs(url::kHttpsScheme) &&
459 (input.terms_prefixed_by_http_or_https().empty() ||
460 !WordMatchesURLContent(input.terms_prefixed_by_http_or_https(), url))) {
422 replacements.SetScheme(url::kHttpScheme, 461 replacements.SetScheme(url::kHttpScheme,
423 url::Component(0, strlen(url::kHttpScheme))); 462 url::Component(0, strlen(url::kHttpScheme)));
424 needs_replacement = true; 463 needs_replacement = true;
425 } 464 }
426 465
427 if (needs_replacement) 466 if (needs_replacement)
428 stripped_destination_url = stripped_destination_url.ReplaceComponents( 467 stripped_destination_url = stripped_destination_url.ReplaceComponents(
429 replacements); 468 replacements);
430 return stripped_destination_url; 469 return stripped_destination_url;
431 } 470 }
432 471
433 void AutocompleteMatch::ComputeStrippedDestinationURL( 472 void AutocompleteMatch::ComputeStrippedDestinationURL(
473 const AutocompleteInput& input,
434 TemplateURLService* template_url_service) { 474 TemplateURLService* template_url_service) {
435 stripped_destination_url = 475 stripped_destination_url = GURLToStrippedGURL(
436 GURLToStrippedGURL(destination_url, template_url_service, keyword); 476 destination_url, input, template_url_service, keyword);
437 } 477 }
438 478
439 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault( 479 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault(
440 const GURL& canonical_input_url, 480 const AutocompleteInput& input,
441 TemplateURLService* template_url_service) { 481 TemplateURLService* template_url_service) {
442 if (!allowed_to_be_default_match) { 482 if (!allowed_to_be_default_match) {
443 const GURL& stripped_canonical_input_url = 483 const GURL& stripped_canonical_input_url =
444 AutocompleteMatch::GURLToStrippedGURL( 484 AutocompleteMatch::GURLToStrippedGURL(
445 canonical_input_url, template_url_service, base::string16()); 485 input.canonicalized_url(), input, template_url_service,
446 ComputeStrippedDestinationURL(template_url_service); 486 base::string16());
487 ComputeStrippedDestinationURL(input, template_url_service);
447 allowed_to_be_default_match = 488 allowed_to_be_default_match =
448 stripped_canonical_input_url == stripped_destination_url; 489 stripped_canonical_input_url == stripped_destination_url;
449 } 490 }
450 } 491 }
451 492
452 void AutocompleteMatch::GetKeywordUIState( 493 void AutocompleteMatch::GetKeywordUIState(
453 TemplateURLService* template_url_service, 494 TemplateURLService* template_url_service,
454 base::string16* keyword, 495 base::string16* keyword,
455 bool* is_keyword_hint) const { 496 bool* is_keyword_hint) const {
456 *is_keyword_hint = associated_keyword.get() != NULL; 497 *is_keyword_hint = associated_keyword.get() != NULL;
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 << " is unsorted in relation to last offset of " << last_offset 612 << " is unsorted in relation to last offset of " << last_offset
572 << ". Provider: " << provider_name << "."; 613 << ". Provider: " << provider_name << ".";
573 DCHECK_LT(i->offset, text.length()) 614 DCHECK_LT(i->offset, text.length())
574 << " Classification of [" << i->offset << "," << text.length() 615 << " Classification of [" << i->offset << "," << text.length()
575 << "] is out of bounds for \"" << text << "\". Provider: " 616 << "] is out of bounds for \"" << text << "\". Provider: "
576 << provider_name << "."; 617 << provider_name << ".";
577 last_offset = i->offset; 618 last_offset = i->offset;
578 } 619 }
579 } 620 }
580 #endif 621 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698