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/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 |http_following_terms| matches the beginning of | |
30 // the URL (sans scheme). (Recall that |http_following_terms|, for the input | |
31 // "http://a b" will be ["a"].) This suggests that the user wants a particular | |
32 // URL with a scheme in mind, hence the caller should not consider another URL | |
33 // like this one but with a different scheme to be a duplicate. | |
34 bool WordMatchesURLContent( | |
35 const std::vector<base::string16>& http_following_terms, | |
36 const GURL& url) { | |
37 const base::string16& url_spec_without_scheme_as_utf16 = | |
Peter Kasting
2015/06/09 20:36:39
If you follow my suggestion in autocomplete_input.
Mark P
2015/06/10 23:38:35
Acknowledged, but not relevant given my earlier pr
| |
38 base::UTF8ToUTF16(url.spec().substr( | |
39 url.scheme().length() + | |
40 std::string(url::kStandardSchemeSeparator).length())); | |
41 for (auto it : http_following_terms) { | |
Peter Kasting
2015/06/09 20:36:39
Nit: const auto&, to avoid a copy
Mark P
2015/06/10 23:38:35
Done.
BTW, also renamed "it" to "term".
| |
42 if (StartsWith(url_spec_without_scheme_as_utf16, it, false)) { | |
Peter Kasting
2015/06/09 20:36:39
Nit: No {}
Mark P
2015/06/10 23:38:35
Done.
| |
43 return true; | |
44 } | |
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 Loading... | |
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 Loading... | |
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 (!WordMatchesURLContent(input.http_following_terms(), url) && | |
442 stripped_destination_url.SchemeIs(url::kHttpsScheme)) { | |
Peter Kasting
2015/06/09 20:36:39
Nit: For efficiency, reverse these subexpressions;
Mark P
2015/06/10 23:38:35
Good idea. Done.
| |
420 replacements.SetScheme(url::kHttpScheme, | 443 replacements.SetScheme(url::kHttpScheme, |
421 url::Component(0, strlen(url::kHttpScheme))); | 444 url::Component(0, strlen(url::kHttpScheme))); |
422 needs_replacement = true; | 445 needs_replacement = true; |
423 } | 446 } |
424 | 447 |
425 if (needs_replacement) | 448 if (needs_replacement) |
426 stripped_destination_url = stripped_destination_url.ReplaceComponents( | 449 stripped_destination_url = stripped_destination_url.ReplaceComponents( |
427 replacements); | 450 replacements); |
428 return stripped_destination_url; | 451 return stripped_destination_url; |
429 } | 452 } |
430 | 453 |
431 void AutocompleteMatch::ComputeStrippedDestinationURL( | 454 void AutocompleteMatch::ComputeStrippedDestinationURL( |
455 const AutocompleteInput& input, | |
432 TemplateURLService* template_url_service) { | 456 TemplateURLService* template_url_service) { |
433 stripped_destination_url = | 457 stripped_destination_url = GURLToStrippedGURL( |
434 GURLToStrippedGURL(destination_url, template_url_service, keyword); | 458 destination_url, input, template_url_service, keyword); |
435 } | 459 } |
436 | 460 |
437 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault( | 461 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault( |
438 const GURL& canonical_input_url, | 462 const AutocompleteInput& input, |
439 TemplateURLService* template_url_service) { | 463 TemplateURLService* template_url_service) { |
440 if (!allowed_to_be_default_match) { | 464 if (!allowed_to_be_default_match) { |
441 const GURL& stripped_canonical_input_url = | 465 const GURL& stripped_canonical_input_url = |
442 AutocompleteMatch::GURLToStrippedGURL( | 466 AutocompleteMatch::GURLToStrippedGURL( |
443 canonical_input_url, template_url_service, base::string16()); | 467 input.canonicalized_url(), input, template_url_service, |
444 ComputeStrippedDestinationURL(template_url_service); | 468 base::string16()); |
469 ComputeStrippedDestinationURL(input, template_url_service); | |
445 allowed_to_be_default_match = | 470 allowed_to_be_default_match = |
446 stripped_canonical_input_url == stripped_destination_url; | 471 stripped_canonical_input_url == stripped_destination_url; |
447 } | 472 } |
448 } | 473 } |
449 | 474 |
450 void AutocompleteMatch::GetKeywordUIState( | 475 void AutocompleteMatch::GetKeywordUIState( |
451 TemplateURLService* template_url_service, | 476 TemplateURLService* template_url_service, |
452 base::string16* keyword, | 477 base::string16* keyword, |
453 bool* is_keyword_hint) const { | 478 bool* is_keyword_hint) const { |
454 *is_keyword_hint = associated_keyword.get() != NULL; | 479 *is_keyword_hint = associated_keyword.get() != NULL; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
557 << " is unsorted in relation to last offset of " << last_offset | 582 << " is unsorted in relation to last offset of " << last_offset |
558 << ". Provider: " << provider_name << "."; | 583 << ". Provider: " << provider_name << "."; |
559 DCHECK_LT(i->offset, text.length()) | 584 DCHECK_LT(i->offset, text.length()) |
560 << " Classification of [" << i->offset << "," << text.length() | 585 << " Classification of [" << i->offset << "," << text.length() |
561 << "] is out of bounds for \"" << text << "\". Provider: " | 586 << "] is out of bounds for \"" << text << "\". Provider: " |
562 << provider_name << "."; | 587 << provider_name << "."; |
563 last_offset = i->offset; | 588 last_offset = i->offset; |
564 } | 589 } |
565 } | 590 } |
566 #endif | 591 #endif |
OLD | NEW |