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

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: forgot string implementation 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
« no previous file with comments | « components/omnibox/autocomplete_match.h ('k') | components/omnibox/autocomplete_result.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/i18n/time_formatting.h" 7 #include "base/i18n/time_formatting.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string16.h" 9 #include "base/strings/string16.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_piece.h" 11 #include "base/strings/string_piece.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "components/omnibox/autocomplete_provider.h" 15 #include "components/omnibox/autocomplete_provider.h"
16 #include "components/omnibox/suggestion_answer.h" 16 #include "components/omnibox/suggestion_answer.h"
17 #include "components/search_engines/template_url.h" 17 #include "components/search_engines/template_url.h"
18 #include "components/search_engines/template_url_service.h" 18 #include "components/search_engines/template_url_service.h"
19 #include "grit/components_scaled_resources.h" 19 #include "grit/components_scaled_resources.h"
20 20
21 namespace { 21 namespace {
22 22
23 bool IsTrivialClassification(const ACMatchClassifications& classifications) { 23 bool IsTrivialClassification(const ACMatchClassifications& classifications) {
24 return classifications.empty() || 24 return classifications.empty() ||
25 ((classifications.size() == 1) && 25 ((classifications.size() == 1) &&
26 (classifications.back().style == ACMatchClassification::NONE)); 26 (classifications.back().style == ACMatchClassification::NONE));
27 } 27 }
28 28
29 // Returns true if one of the |terms_prefixed_by_http_or_https| matches the
30 // beginning of the URL (sans scheme). (Recall that
31 // |terms_prefixed_by_http_or_https|, for the input "http://a b" will be
32 // ["a"].) This suggests that the user wants a particular URL with a scheme
33 // in mind, hence the caller should not consider another URL like this one
34 // but with a different scheme to be a duplicate.
35 bool WordMatchesURLContent(
36 const std::vector<std::string>& terms_prefixed_by_http_or_https,
37 const GURL& url) {
38 const size_t prefix_length =
39 url.scheme().length() + strlen(url::kStandardSchemeSeparator);
40 DCHECK_GE(url.spec().length(), prefix_length);
41 const std::string& url_spec_without_scheme = url.spec().substr(prefix_length);
42 for (const auto& term : terms_prefixed_by_http_or_https) {
43 if (StartsWith(url_spec_without_scheme, term, false))
44 return true;
45 }
46 return false;
47 }
48
29 } // namespace 49 } // namespace
30 50
31 // AutocompleteMatch ---------------------------------------------------------- 51 // AutocompleteMatch ----------------------------------------------------------
32 52
33 // static 53 // static
34 const base::char16 AutocompleteMatch::kInvalidChars[] = { 54 const base::char16 AutocompleteMatch::kInvalidChars[] = {
35 '\n', '\r', '\t', 55 '\n', '\r', '\t',
36 0x2028, // Line separator 56 0x2028, // Line separator
37 0x2029, // Paragraph separator 57 0x2029, // Paragraph separator
38 0 58 0
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 return NULL; 383 return NULL;
364 TemplateURL* template_url = keyword.empty() ? 384 TemplateURL* template_url = keyword.empty() ?
365 NULL : template_url_service->GetTemplateURLForKeyword(keyword); 385 NULL : template_url_service->GetTemplateURLForKeyword(keyword);
366 return (template_url || host.empty()) ? 386 return (template_url || host.empty()) ?
367 template_url : template_url_service->GetTemplateURLForHost(host); 387 template_url : template_url_service->GetTemplateURLForHost(host);
368 } 388 }
369 389
370 // static 390 // static
371 GURL AutocompleteMatch::GURLToStrippedGURL( 391 GURL AutocompleteMatch::GURLToStrippedGURL(
372 const GURL& url, 392 const GURL& url,
393 const AutocompleteInput& input,
373 TemplateURLService* template_url_service, 394 TemplateURLService* template_url_service,
374 const base::string16& keyword) { 395 const base::string16& keyword) {
375 if (!url.is_valid()) 396 if (!url.is_valid())
376 return url; 397 return url;
377 398
378 GURL stripped_destination_url = url; 399 GURL stripped_destination_url = url;
379 400
380 // If the destination URL looks like it was generated from a TemplateURL, 401 // If the destination URL looks like it was generated from a TemplateURL,
381 // remove all substitutions other than the search terms. This allows us 402 // remove all substitutions other than the search terms. This allows us
382 // to eliminate cases like past search URLs from history that differ only 403 // to eliminate cases like past search URLs from history that differ only
(...skipping 25 matching lines...) Expand all
408 429
409 // Remove the www. prefix from the host. 430 // Remove the www. prefix from the host.
410 static const char prefix[] = "www."; 431 static const char prefix[] = "www.";
411 static const size_t prefix_len = arraysize(prefix) - 1; 432 static const size_t prefix_len = arraysize(prefix) - 1;
412 std::string host = stripped_destination_url.host(); 433 std::string host = stripped_destination_url.host();
413 if (host.compare(0, prefix_len, prefix) == 0) { 434 if (host.compare(0, prefix_len, prefix) == 0) {
414 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len)); 435 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len));
415 needs_replacement = true; 436 needs_replacement = true;
416 } 437 }
417 438
418 // Replace https protocol with http protocol. 439 // Replace https protocol with http, as long as the user didn't explicitly
419 if (stripped_destination_url.SchemeIs(url::kHttpsScheme)) { 440 // specify one of the two.
441 if (stripped_destination_url.SchemeIs(url::kHttpsScheme) &&
442 (input.terms_prefixed_by_http_or_https().empty() ||
443 !WordMatchesURLContent(input.terms_prefixed_by_http_or_https(), url))) {
420 replacements.SetScheme(url::kHttpScheme, 444 replacements.SetScheme(url::kHttpScheme,
421 url::Component(0, strlen(url::kHttpScheme))); 445 url::Component(0, strlen(url::kHttpScheme)));
422 needs_replacement = true; 446 needs_replacement = true;
423 } 447 }
424 448
425 if (needs_replacement) 449 if (needs_replacement)
426 stripped_destination_url = stripped_destination_url.ReplaceComponents( 450 stripped_destination_url = stripped_destination_url.ReplaceComponents(
427 replacements); 451 replacements);
428 return stripped_destination_url; 452 return stripped_destination_url;
429 } 453 }
430 454
431 void AutocompleteMatch::ComputeStrippedDestinationURL( 455 void AutocompleteMatch::ComputeStrippedDestinationURL(
456 const AutocompleteInput& input,
432 TemplateURLService* template_url_service) { 457 TemplateURLService* template_url_service) {
433 stripped_destination_url = 458 stripped_destination_url = GURLToStrippedGURL(
434 GURLToStrippedGURL(destination_url, template_url_service, keyword); 459 destination_url, input, template_url_service, keyword);
435 } 460 }
436 461
437 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault( 462 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault(
438 const GURL& canonical_input_url, 463 const AutocompleteInput& input,
439 TemplateURLService* template_url_service) { 464 TemplateURLService* template_url_service) {
440 if (!allowed_to_be_default_match) { 465 if (!allowed_to_be_default_match) {
441 const GURL& stripped_canonical_input_url = 466 const GURL& stripped_canonical_input_url =
442 AutocompleteMatch::GURLToStrippedGURL( 467 AutocompleteMatch::GURLToStrippedGURL(
443 canonical_input_url, template_url_service, base::string16()); 468 input.canonicalized_url(), input, template_url_service,
444 ComputeStrippedDestinationURL(template_url_service); 469 base::string16());
470 ComputeStrippedDestinationURL(input, template_url_service);
445 allowed_to_be_default_match = 471 allowed_to_be_default_match =
446 stripped_canonical_input_url == stripped_destination_url; 472 stripped_canonical_input_url == stripped_destination_url;
447 } 473 }
448 } 474 }
449 475
450 void AutocompleteMatch::GetKeywordUIState( 476 void AutocompleteMatch::GetKeywordUIState(
451 TemplateURLService* template_url_service, 477 TemplateURLService* template_url_service,
452 base::string16* keyword, 478 base::string16* keyword,
453 bool* is_keyword_hint) const { 479 bool* is_keyword_hint) const {
454 *is_keyword_hint = associated_keyword.get() != NULL; 480 *is_keyword_hint = associated_keyword.get() != NULL;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 << " is unsorted in relation to last offset of " << last_offset 583 << " is unsorted in relation to last offset of " << last_offset
558 << ". Provider: " << provider_name << "."; 584 << ". Provider: " << provider_name << ".";
559 DCHECK_LT(i->offset, text.length()) 585 DCHECK_LT(i->offset, text.length())
560 << " Classification of [" << i->offset << "," << text.length() 586 << " Classification of [" << i->offset << "," << text.length()
561 << "] is out of bounds for \"" << text << "\". Provider: " 587 << "] is out of bounds for \"" << text << "\". Provider: "
562 << provider_name << "."; 588 << provider_name << ".";
563 last_offset = i->offset; 589 last_offset = i->offset;
564 } 590 }
565 } 591 }
566 #endif 592 #endif
OLDNEW
« no previous file with comments | « components/omnibox/autocomplete_match.h ('k') | components/omnibox/autocomplete_result.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698