| OLD | NEW |
| 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/browser/autocomplete_match.h" | 5 #include "components/omnibox/browser/autocomplete_match.h" |
| 6 | 6 |
| 7 #include "base/i18n/time_formatting.h" | 7 #include "base/i18n/time_formatting.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 return classifications.empty() || | 29 return classifications.empty() || |
| 30 ((classifications.size() == 1) && | 30 ((classifications.size() == 1) && |
| 31 (classifications.back().style == ACMatchClassification::NONE)); | 31 (classifications.back().style == ACMatchClassification::NONE)); |
| 32 } | 32 } |
| 33 | 33 |
| 34 // Returns true if one of the |terms_prefixed_by_http_or_https| matches the | 34 // Returns true if one of the |terms_prefixed_by_http_or_https| matches the |
| 35 // beginning of the URL (sans scheme). (Recall that | 35 // beginning of the URL (sans scheme). (Recall that |
| 36 // |terms_prefixed_by_http_or_https|, for the input "http://a b" will be | 36 // |terms_prefixed_by_http_or_https|, for the input "http://a b" will be |
| 37 // ["a"].) This suggests that the user wants a particular URL with a scheme | 37 // ["a"].) This suggests that the user wants a particular URL with a scheme |
| 38 // in mind, hence the caller should not consider another URL like this one | 38 // in mind, hence the caller should not consider another URL like this one |
| 39 // but with a different scheme to be a duplicate. |languages| is used to | 39 // but with a different scheme to be a duplicate. |
| 40 // format punycoded URLs to decide if they match. | |
| 41 bool WordMatchesURLContent( | 40 bool WordMatchesURLContent( |
| 42 const std::vector<base::string16>& terms_prefixed_by_http_or_https, | 41 const std::vector<base::string16>& terms_prefixed_by_http_or_https, |
| 43 const std::string& languages, | |
| 44 const GURL& url) { | 42 const GURL& url) { |
| 45 size_t prefix_length = | 43 size_t prefix_length = |
| 46 url.scheme().length() + strlen(url::kStandardSchemeSeparator); | 44 url.scheme().length() + strlen(url::kStandardSchemeSeparator); |
| 47 DCHECK_GE(url.spec().length(), prefix_length); | 45 DCHECK_GE(url.spec().length(), prefix_length); |
| 48 const base::string16& formatted_url = url_formatter::FormatUrl( | 46 const base::string16& formatted_url = url_formatter::FormatUrl( |
| 49 url, languages, url_formatter::kFormatUrlOmitNothing, | 47 url, url_formatter::kFormatUrlOmitNothing, |
| 50 net::UnescapeRule::NORMAL, nullptr, nullptr, &prefix_length); | 48 net::UnescapeRule::NORMAL, nullptr, nullptr, &prefix_length); |
| 51 if (prefix_length == base::string16::npos) | 49 if (prefix_length == base::string16::npos) |
| 52 return false; | 50 return false; |
| 53 const base::string16& formatted_url_without_scheme = | 51 const base::string16& formatted_url_without_scheme = |
| 54 formatted_url.substr(prefix_length); | 52 formatted_url.substr(prefix_length); |
| 55 for (const auto& term : terms_prefixed_by_http_or_https) { | 53 for (const auto& term : terms_prefixed_by_http_or_https) { |
| 56 if (base::StartsWith(formatted_url_without_scheme, term, | 54 if (base::StartsWith(formatted_url_without_scheme, term, |
| 57 base::CompareCase::SENSITIVE)) | 55 base::CompareCase::SENSITIVE)) |
| 58 return true; | 56 return true; |
| 59 } | 57 } |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 TemplateURL* template_url = keyword.empty() ? | 452 TemplateURL* template_url = keyword.empty() ? |
| 455 NULL : template_url_service->GetTemplateURLForKeyword(keyword); | 453 NULL : template_url_service->GetTemplateURLForKeyword(keyword); |
| 456 return (template_url || host.empty()) ? | 454 return (template_url || host.empty()) ? |
| 457 template_url : template_url_service->GetTemplateURLForHost(host); | 455 template_url : template_url_service->GetTemplateURLForHost(host); |
| 458 } | 456 } |
| 459 | 457 |
| 460 // static | 458 // static |
| 461 GURL AutocompleteMatch::GURLToStrippedGURL( | 459 GURL AutocompleteMatch::GURLToStrippedGURL( |
| 462 const GURL& url, | 460 const GURL& url, |
| 463 const AutocompleteInput& input, | 461 const AutocompleteInput& input, |
| 464 const std::string& languages, | |
| 465 TemplateURLService* template_url_service, | 462 TemplateURLService* template_url_service, |
| 466 const base::string16& keyword) { | 463 const base::string16& keyword) { |
| 467 if (!url.is_valid()) | 464 if (!url.is_valid()) |
| 468 return url; | 465 return url; |
| 469 | 466 |
| 470 GURL stripped_destination_url = url; | 467 GURL stripped_destination_url = url; |
| 471 | 468 |
| 472 // If the destination URL looks like it was generated from a TemplateURL, | 469 // If the destination URL looks like it was generated from a TemplateURL, |
| 473 // remove all substitutions other than the search terms. This allows us | 470 // remove all substitutions other than the search terms. This allows us |
| 474 // to eliminate cases like past search URLs from history that differ only | 471 // to eliminate cases like past search URLs from history that differ only |
| (...skipping 30 matching lines...) Expand all Loading... |
| 505 if (host.compare(0, prefix_len, prefix) == 0) { | 502 if (host.compare(0, prefix_len, prefix) == 0) { |
| 506 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len)); | 503 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len)); |
| 507 needs_replacement = true; | 504 needs_replacement = true; |
| 508 } | 505 } |
| 509 | 506 |
| 510 // Replace https protocol with http, as long as the user didn't explicitly | 507 // Replace https protocol with http, as long as the user didn't explicitly |
| 511 // specify one of the two. | 508 // specify one of the two. |
| 512 if (stripped_destination_url.SchemeIs(url::kHttpsScheme) && | 509 if (stripped_destination_url.SchemeIs(url::kHttpsScheme) && |
| 513 (input.terms_prefixed_by_http_or_https().empty() || | 510 (input.terms_prefixed_by_http_or_https().empty() || |
| 514 !WordMatchesURLContent( | 511 !WordMatchesURLContent( |
| 515 input.terms_prefixed_by_http_or_https(), languages, url))) { | 512 input.terms_prefixed_by_http_or_https(), url))) { |
| 516 replacements.SetScheme(url::kHttpScheme, | 513 replacements.SetScheme(url::kHttpScheme, |
| 517 url::Component(0, strlen(url::kHttpScheme))); | 514 url::Component(0, strlen(url::kHttpScheme))); |
| 518 needs_replacement = true; | 515 needs_replacement = true; |
| 519 } | 516 } |
| 520 | 517 |
| 521 if (needs_replacement) | 518 if (needs_replacement) |
| 522 stripped_destination_url = stripped_destination_url.ReplaceComponents( | 519 stripped_destination_url = stripped_destination_url.ReplaceComponents( |
| 523 replacements); | 520 replacements); |
| 524 return stripped_destination_url; | 521 return stripped_destination_url; |
| 525 } | 522 } |
| 526 | 523 |
| 527 void AutocompleteMatch::ComputeStrippedDestinationURL( | 524 void AutocompleteMatch::ComputeStrippedDestinationURL( |
| 528 const AutocompleteInput& input, | 525 const AutocompleteInput& input, |
| 529 const std::string& languages, | |
| 530 TemplateURLService* template_url_service) { | 526 TemplateURLService* template_url_service) { |
| 531 stripped_destination_url = GURLToStrippedGURL( | 527 stripped_destination_url = GURLToStrippedGURL( |
| 532 destination_url, input, languages, template_url_service, keyword); | 528 destination_url, input, template_url_service, keyword); |
| 533 } | 529 } |
| 534 | 530 |
| 535 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault( | 531 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault( |
| 536 const AutocompleteInput& input, | 532 const AutocompleteInput& input, |
| 537 const std::string& languages, | |
| 538 TemplateURLService* template_url_service) { | 533 TemplateURLService* template_url_service) { |
| 539 if (!allowed_to_be_default_match) { | 534 if (!allowed_to_be_default_match) { |
| 540 const GURL& stripped_canonical_input_url = | 535 const GURL& stripped_canonical_input_url = |
| 541 AutocompleteMatch::GURLToStrippedGURL( | 536 AutocompleteMatch::GURLToStrippedGURL( |
| 542 input.canonicalized_url(), input, languages, template_url_service, | 537 input.canonicalized_url(), input, template_url_service, |
| 543 base::string16()); | 538 base::string16()); |
| 544 ComputeStrippedDestinationURL(input, languages, template_url_service); | 539 ComputeStrippedDestinationURL(input, template_url_service); |
| 545 allowed_to_be_default_match = | 540 allowed_to_be_default_match = |
| 546 stripped_canonical_input_url == stripped_destination_url; | 541 stripped_canonical_input_url == stripped_destination_url; |
| 547 } | 542 } |
| 548 } | 543 } |
| 549 | 544 |
| 550 void AutocompleteMatch::GetKeywordUIState( | 545 void AutocompleteMatch::GetKeywordUIState( |
| 551 TemplateURLService* template_url_service, | 546 TemplateURLService* template_url_service, |
| 552 base::string16* keyword, | 547 base::string16* keyword, |
| 553 bool* is_keyword_hint) const { | 548 bool* is_keyword_hint) const { |
| 554 *is_keyword_hint = associated_keyword.get() != NULL; | 549 *is_keyword_hint = associated_keyword.get() != NULL; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 << " is unsorted in relation to last offset of " << last_offset | 659 << " is unsorted in relation to last offset of " << last_offset |
| 665 << ". Provider: " << provider_name << "."; | 660 << ". Provider: " << provider_name << "."; |
| 666 DCHECK_LT(i->offset, text.length()) | 661 DCHECK_LT(i->offset, text.length()) |
| 667 << " Classification of [" << i->offset << "," << text.length() | 662 << " Classification of [" << i->offset << "," << text.length() |
| 668 << "] is out of bounds for \"" << text << "\". Provider: " | 663 << "] is out of bounds for \"" << text << "\". Provider: " |
| 669 << provider_name << "."; | 664 << provider_name << "."; |
| 670 last_offset = i->offset; | 665 last_offset = i->offset; |
| 671 } | 666 } |
| 672 } | 667 } |
| 673 #endif | 668 #endif |
| OLD | NEW |