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 input words includes a scheme and followed by | |
30 // something that matches at the beginning of the URL's hostname. This | |
31 // indicates that the user desires this URL loaded with this scheme in | |
32 // particular. | |
33 bool InputIncludesSchemePlusPartOfHost( | |
34 const std::vector<base::string16>& input_words, | |
35 const GURL& url) { | |
36 // Find scheme separator and see if the stuff following it matches the | |
37 // beginning of the URL (after the scheme). | |
38 const base::string16& separator_as_utf16 = | |
39 base::UTF8ToUTF16(url::kStandardSchemeSeparator); | |
40 const base::string16& url_spec_without_scheme_as_utf16 = | |
41 base::UTF8ToUTF16(url.spec().substr( | |
42 url.scheme().length() + | |
43 std::string(url::kStandardSchemeSeparator).length())); | |
Peter Kasting
2015/06/01 21:04:08
Why not just url.host().begin?
Mark P
2015/06/01 22:05:50
If there is a username or password, url.host() pos
Peter Kasting
2015/06/01 22:21:58
Maybe url.GetContent() then?
Mark P
2015/06/04 23:31:31
I think that's still ugly. It's require a shift b
| |
44 for (auto it : input_words) { | |
45 const size_t pos = it.find(separator_as_utf16); | |
46 if ((pos != base::string16::npos) && | |
47 StartsWith(url_spec_without_scheme_as_utf16, | |
48 it.substr(pos + separator_as_utf16.length()), false)) { | |
Peter Kasting
2015/06/01 21:04:08
Won't this find input like "://foo"? Shouldn't we
Mark P
2015/06/01 22:05:50
Yes. Do you want me to add a length>0 check?
Peter Kasting
2015/06/01 22:21:58
Maybe instead we should do something like check ur
Mark P
2015/06/04 23:31:31
I'll prefer to add the >0 test. I don't see any r
| |
49 return true; | |
50 } | |
51 } | |
52 return false; | |
53 } | |
54 | |
29 } // namespace | 55 } // namespace |
30 | 56 |
31 // AutocompleteMatch ---------------------------------------------------------- | 57 // AutocompleteMatch ---------------------------------------------------------- |
32 | 58 |
33 // static | 59 // static |
34 const base::char16 AutocompleteMatch::kInvalidChars[] = { | 60 const base::char16 AutocompleteMatch::kInvalidChars[] = { |
35 '\n', '\r', '\t', | 61 '\n', '\r', '\t', |
36 0x2028, // Line separator | 62 0x2028, // Line separator |
37 0x2029, // Paragraph separator | 63 0x2029, // Paragraph separator |
38 0 | 64 0 |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 return NULL; | 389 return NULL; |
364 TemplateURL* template_url = keyword.empty() ? | 390 TemplateURL* template_url = keyword.empty() ? |
365 NULL : template_url_service->GetTemplateURLForKeyword(keyword); | 391 NULL : template_url_service->GetTemplateURLForKeyword(keyword); |
366 return (template_url || host.empty()) ? | 392 return (template_url || host.empty()) ? |
367 template_url : template_url_service->GetTemplateURLForHost(host); | 393 template_url : template_url_service->GetTemplateURLForHost(host); |
368 } | 394 } |
369 | 395 |
370 // static | 396 // static |
371 GURL AutocompleteMatch::GURLToStrippedGURL( | 397 GURL AutocompleteMatch::GURLToStrippedGURL( |
372 const GURL& url, | 398 const GURL& url, |
399 const std::vector<base::string16>& input_words, | |
373 TemplateURLService* template_url_service, | 400 TemplateURLService* template_url_service, |
374 const base::string16& keyword) { | 401 const base::string16& keyword) { |
375 if (!url.is_valid()) | 402 if (!url.is_valid()) |
376 return url; | 403 return url; |
377 | 404 |
378 GURL stripped_destination_url = url; | 405 GURL stripped_destination_url = url; |
379 | 406 |
380 // If the destination URL looks like it was generated from a TemplateURL, | 407 // If the destination URL looks like it was generated from a TemplateURL, |
381 // remove all substitutions other than the search terms. This allows us | 408 // remove all substitutions other than the search terms. This allows us |
382 // to eliminate cases like past search URLs from history that differ only | 409 // to eliminate cases like past search URLs from history that differ only |
(...skipping 25 matching lines...) Expand all Loading... | |
408 | 435 |
409 // Remove the www. prefix from the host. | 436 // Remove the www. prefix from the host. |
410 static const char prefix[] = "www."; | 437 static const char prefix[] = "www."; |
411 static const size_t prefix_len = arraysize(prefix) - 1; | 438 static const size_t prefix_len = arraysize(prefix) - 1; |
412 std::string host = stripped_destination_url.host(); | 439 std::string host = stripped_destination_url.host(); |
413 if (host.compare(0, prefix_len, prefix) == 0) { | 440 if (host.compare(0, prefix_len, prefix) == 0) { |
414 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len)); | 441 replacements.SetHostStr(base::StringPiece(host).substr(prefix_len)); |
415 needs_replacement = true; | 442 needs_replacement = true; |
416 } | 443 } |
417 | 444 |
418 // Replace https protocol with http protocol. | 445 // Possibly replace https protocol with http protocol. |
419 if (stripped_destination_url.SchemeIs(url::kHttpsScheme)) { | 446 if (!InputIncludesSchemePlusPartOfHost(input_words, url) && |
447 stripped_destination_url.SchemeIs(url::kHttpsScheme)) { | |
420 replacements.SetScheme(url::kHttpScheme, | 448 replacements.SetScheme(url::kHttpScheme, |
421 url::Component(0, strlen(url::kHttpScheme))); | 449 url::Component(0, strlen(url::kHttpScheme))); |
422 needs_replacement = true; | 450 needs_replacement = true; |
423 } | 451 } |
424 | 452 |
425 if (needs_replacement) | 453 if (needs_replacement) |
426 stripped_destination_url = stripped_destination_url.ReplaceComponents( | 454 stripped_destination_url = stripped_destination_url.ReplaceComponents( |
427 replacements); | 455 replacements); |
428 return stripped_destination_url; | 456 return stripped_destination_url; |
429 } | 457 } |
430 | 458 |
431 void AutocompleteMatch::ComputeStrippedDestinationURL( | 459 void AutocompleteMatch::ComputeStrippedDestinationURL( |
460 const std::vector<base::string16>& input_words, | |
432 TemplateURLService* template_url_service) { | 461 TemplateURLService* template_url_service) { |
433 stripped_destination_url = | 462 stripped_destination_url = GURLToStrippedGURL( |
434 GURLToStrippedGURL(destination_url, template_url_service, keyword); | 463 destination_url, input_words, template_url_service, keyword); |
435 } | 464 } |
436 | 465 |
437 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault( | 466 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault( |
438 const GURL& canonical_input_url, | 467 const GURL& canonical_input_url, |
468 const std::vector<base::string16>& input_words, | |
439 TemplateURLService* template_url_service) { | 469 TemplateURLService* template_url_service) { |
440 if (!allowed_to_be_default_match) { | 470 if (!allowed_to_be_default_match) { |
441 const GURL& stripped_canonical_input_url = | 471 const GURL& stripped_canonical_input_url = |
442 AutocompleteMatch::GURLToStrippedGURL( | 472 AutocompleteMatch::GURLToStrippedGURL( |
443 canonical_input_url, template_url_service, base::string16()); | 473 canonical_input_url, input_words, |
444 ComputeStrippedDestinationURL(template_url_service); | 474 template_url_service, base::string16()); |
475 ComputeStrippedDestinationURL(input_words, template_url_service); | |
445 allowed_to_be_default_match = | 476 allowed_to_be_default_match = |
446 stripped_canonical_input_url == stripped_destination_url; | 477 stripped_canonical_input_url == stripped_destination_url; |
447 } | 478 } |
448 } | 479 } |
449 | 480 |
450 void AutocompleteMatch::GetKeywordUIState( | 481 void AutocompleteMatch::GetKeywordUIState( |
451 TemplateURLService* template_url_service, | 482 TemplateURLService* template_url_service, |
452 base::string16* keyword, | 483 base::string16* keyword, |
453 bool* is_keyword_hint) const { | 484 bool* is_keyword_hint) const { |
454 *is_keyword_hint = associated_keyword.get() != NULL; | 485 *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 | 588 << " is unsorted in relation to last offset of " << last_offset |
558 << ". Provider: " << provider_name << "."; | 589 << ". Provider: " << provider_name << "."; |
559 DCHECK_LT(i->offset, text.length()) | 590 DCHECK_LT(i->offset, text.length()) |
560 << " Classification of [" << i->offset << "," << text.length() | 591 << " Classification of [" << i->offset << "," << text.length() |
561 << "] is out of bounds for \"" << text << "\". Provider: " | 592 << "] is out of bounds for \"" << text << "\". Provider: " |
562 << provider_name << "."; | 593 << provider_name << "."; |
563 last_offset = i->offset; | 594 last_offset = i->offset; |
564 } | 595 } |
565 } | 596 } |
566 #endif | 597 #endif |
OLD | NEW |