OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/autocomplete/autocomplete.h" | 5 #include "chrome/browser/autocomplete/autocomplete.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> |
8 | 9 |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
11 #include "base/i18n/number_formatting.h" | 12 #include "base/i18n/number_formatting.h" |
12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
13 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
14 #include "base/string_util.h" | 15 #include "base/string_util.h" |
15 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
16 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" | 17 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" |
17 #include "chrome/browser/autocomplete/autocomplete_match.h" | 18 #include "chrome/browser/autocomplete/autocomplete_match.h" |
18 #include "chrome/browser/autocomplete/builtin_provider.h" | 19 #include "chrome/browser/autocomplete/builtin_provider.h" |
19 #include "chrome/browser/autocomplete/extension_app_provider.h" | 20 #include "chrome/browser/autocomplete/extension_app_provider.h" |
20 #include "chrome/browser/autocomplete/history_contents_provider.h" | 21 #include "chrome/browser/autocomplete/history_contents_provider.h" |
21 #include "chrome/browser/autocomplete/history_quick_provider.h" | 22 #include "chrome/browser/autocomplete/history_quick_provider.h" |
22 #include "chrome/browser/autocomplete/history_url_provider.h" | 23 #include "chrome/browser/autocomplete/history_url_provider.h" |
23 #include "chrome/browser/autocomplete/keyword_provider.h" | 24 #include "chrome/browser/autocomplete/keyword_provider.h" |
24 #include "chrome/browser/autocomplete/search_provider.h" | 25 #include "chrome/browser/autocomplete/search_provider.h" |
25 #include "chrome/browser/autocomplete/shortcuts_provider.h" | 26 #include "chrome/browser/autocomplete/shortcuts_provider.h" |
26 #include "chrome/browser/bookmarks/bookmark_model.h" | 27 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 28 #include "chrome/browser/extensions/extension_service.h" |
27 #include "chrome/browser/external_protocol/external_protocol_handler.h" | 29 #include "chrome/browser/external_protocol/external_protocol_handler.h" |
28 #include "chrome/browser/instant/instant_field_trial.h" | 30 #include "chrome/browser/instant/instant_field_trial.h" |
29 #include "chrome/browser/net/url_fixer_upper.h" | 31 #include "chrome/browser/net/url_fixer_upper.h" |
30 #include "chrome/browser/prefs/pref_service.h" | 32 #include "chrome/browser/prefs/pref_service.h" |
31 #include "chrome/browser/profiles/profile.h" | 33 #include "chrome/browser/profiles/profile.h" |
32 #include "chrome/browser/profiles/profile_io_data.h" | 34 #include "chrome/browser/profiles/profile_io_data.h" |
| 35 #include "chrome/browser/search_engines/template_url.h" |
| 36 #include "chrome/browser/search_engines/template_url_service.h" |
| 37 #include "chrome/browser/search_engines/template_url_service_factory.h" |
33 #include "chrome/browser/ui/webui/history_ui.h" | 38 #include "chrome/browser/ui/webui/history_ui.h" |
34 #include "chrome/common/chrome_notification_types.h" | 39 #include "chrome/common/chrome_notification_types.h" |
35 #include "chrome/common/chrome_switches.h" | 40 #include "chrome/common/chrome_switches.h" |
36 #include "chrome/common/pref_names.h" | 41 #include "chrome/common/pref_names.h" |
37 #include "chrome/common/url_constants.h" | 42 #include "chrome/common/url_constants.h" |
38 #include "content/public/browser/notification_service.h" | 43 #include "content/public/browser/notification_service.h" |
39 #include "googleurl/src/gurl.h" | 44 #include "googleurl/src/gurl.h" |
40 #include "googleurl/src/url_canon_ip.h" | 45 #include "googleurl/src/url_canon_ip.h" |
41 #include "googleurl/src/url_util.h" | 46 #include "googleurl/src/url_util.h" |
42 #include "grit/generated_resources.h" | 47 #include "grit/generated_resources.h" |
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 ACMatches::iterator insertion_point = | 681 ACMatches::iterator insertion_point = |
677 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); | 682 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); |
678 matches_difference_type default_offset = default_match_ - begin(); | 683 matches_difference_type default_offset = default_match_ - begin(); |
679 if ((insertion_point - begin()) <= default_offset) | 684 if ((insertion_point - begin()) <= default_offset) |
680 ++default_offset; | 685 ++default_offset; |
681 matches_.insert(insertion_point, match); | 686 matches_.insert(insertion_point, match); |
682 default_match_ = begin() + default_offset; | 687 default_match_ = begin() + default_offset; |
683 } | 688 } |
684 | 689 |
685 void AutocompleteResult::SortAndCull(const AutocompleteInput& input) { | 690 void AutocompleteResult::SortAndCull(const AutocompleteInput& input) { |
| 691 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i) |
| 692 i->ComputeStrippedDestinationURL(); |
| 693 |
686 // Remove duplicates. | 694 // Remove duplicates. |
687 std::sort(matches_.begin(), matches_.end(), | 695 std::sort(matches_.begin(), matches_.end(), |
688 &AutocompleteMatch::DestinationSortFunc); | 696 &AutocompleteMatch::DestinationSortFunc); |
689 matches_.erase(std::unique(matches_.begin(), matches_.end(), | 697 matches_.erase(std::unique(matches_.begin(), matches_.end(), |
690 &AutocompleteMatch::DestinationsEqual), | 698 &AutocompleteMatch::DestinationsEqual), |
691 matches_.end()); | 699 matches_.end()); |
692 | 700 |
693 // Sort and trim to the most relevant kMaxMatches matches. | 701 // Sort and trim to the most relevant kMaxMatches matches. |
694 const size_t num_matches = std::min(kMaxMatches, matches_.size()); | 702 const size_t num_matches = std::min(kMaxMatches, matches_.size()); |
695 std::partial_sort(matches_.begin(), matches_.begin() + num_matches, | 703 std::partial_sort(matches_.begin(), matches_.begin() + num_matches, |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
984 result_.Validate(); | 992 result_.Validate(); |
985 #endif | 993 #endif |
986 | 994 |
987 if (!done_) { | 995 if (!done_) { |
988 // This conditional needs to match the conditional in Start that invokes | 996 // This conditional needs to match the conditional in Start that invokes |
989 // StartExpireTimer. | 997 // StartExpireTimer. |
990 result_.CopyOldMatches(input_, last_result); | 998 result_.CopyOldMatches(input_, last_result); |
991 } | 999 } |
992 | 1000 |
993 UpdateKeywordDescriptions(&result_); | 1001 UpdateKeywordDescriptions(&result_); |
| 1002 UpdateAssociatedKeywords(&result_); |
994 | 1003 |
995 bool notify_default_match = is_synchronous_pass; | 1004 bool notify_default_match = is_synchronous_pass; |
996 if (!is_synchronous_pass) { | 1005 if (!is_synchronous_pass) { |
997 const bool last_default_was_valid = | 1006 const bool last_default_was_valid = |
998 last_result.default_match() != last_result.end(); | 1007 last_result.default_match() != last_result.end(); |
999 const bool default_is_valid = result_.default_match() != result_.end(); | 1008 const bool default_is_valid = result_.default_match() != result_.end(); |
1000 // We've gotten async results. Send notification that the default match | 1009 // We've gotten async results. Send notification that the default match |
1001 // updated if fill_into_edit differs. We don't check the URL as that may | 1010 // updated if fill_into_edit differs or associated_keyword differ. (The |
1002 // change for the default match even though the fill into edit hasn't | 1011 // latter can change if we've just started Chrome and the keyword database |
1003 // changed (see SearchProvider for one case of this). | 1012 // finishes loading while processing this request.) We don't check the URL |
| 1013 // as that may change for the default match even though the fill into edit |
| 1014 // hasn't changed (see SearchProvider for one case of this). |
1004 notify_default_match = | 1015 notify_default_match = |
1005 (last_default_was_valid != default_is_valid) || | 1016 (last_default_was_valid != default_is_valid) || |
1006 (default_is_valid && | 1017 (default_is_valid && |
1007 (result_.default_match()->fill_into_edit != | 1018 ((result_.default_match()->fill_into_edit != |
1008 last_result.default_match()->fill_into_edit)); | 1019 last_result.default_match()->fill_into_edit) || |
| 1020 (result_.default_match()->associated_keyword.get() != |
| 1021 last_result.default_match()->associated_keyword.get()))); |
1009 } | 1022 } |
1010 | 1023 |
1011 NotifyChanged(notify_default_match); | 1024 NotifyChanged(notify_default_match); |
1012 } | 1025 } |
1013 | 1026 |
| 1027 void AutocompleteController::UpdateAssociatedKeywords( |
| 1028 AutocompleteResult* result) { |
| 1029 if (!keyword_provider_) |
| 1030 return; |
| 1031 |
| 1032 std::set<string16> keywords; |
| 1033 for (ACMatches::iterator match(result->begin()); match != result->end(); |
| 1034 ++match) { |
| 1035 if (!match->keyword.empty()) { |
| 1036 keywords.insert(match->keyword); |
| 1037 } else { |
| 1038 string16 keyword = match->associated_keyword.get() ? |
| 1039 match->associated_keyword->keyword : |
| 1040 keyword_provider_->GetKeywordForText(match->fill_into_edit); |
| 1041 |
| 1042 // Only add the keyword if the match does not have a duplicate keyword |
| 1043 // with a more relevant match. |
| 1044 if (!keyword.empty() && !keywords.count(keyword)) { |
| 1045 keywords.insert(keyword); |
| 1046 |
| 1047 if (!match->associated_keyword.get()) |
| 1048 match->associated_keyword.reset(new AutocompleteMatch( |
| 1049 keyword_provider_->CreateAutocompleteMatch(match->fill_into_edit, |
| 1050 keyword, input_))); |
| 1051 } else { |
| 1052 match->associated_keyword.reset(); |
| 1053 } |
| 1054 } |
| 1055 } |
| 1056 } |
| 1057 |
1014 void AutocompleteController::UpdateKeywordDescriptions( | 1058 void AutocompleteController::UpdateKeywordDescriptions( |
1015 AutocompleteResult* result) { | 1059 AutocompleteResult* result) { |
1016 const TemplateURL* last_template_url = NULL; | 1060 const TemplateURL* last_template_url = NULL; |
1017 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); | 1061 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); |
1018 ++i) { | 1062 ++i) { |
1019 if (((i->provider == keyword_provider_) && i->template_url) || | 1063 if (((i->provider == keyword_provider_) && i->template_url) || |
1020 ((i->provider == search_provider_) && | 1064 ((i->provider == search_provider_) && |
1021 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || | 1065 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || |
1022 i->type == AutocompleteMatch::SEARCH_HISTORY || | 1066 i->type == AutocompleteMatch::SEARCH_HISTORY || |
1023 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { | 1067 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1079 const AutocompleteResult& result) | 1123 const AutocompleteResult& result) |
1080 : text(text), | 1124 : text(text), |
1081 input_type(input_type), | 1125 input_type(input_type), |
1082 selected_index(selected_index), | 1126 selected_index(selected_index), |
1083 tab_id(tab_id), | 1127 tab_id(tab_id), |
1084 elapsed_time_since_user_first_modified_omnibox( | 1128 elapsed_time_since_user_first_modified_omnibox( |
1085 elapsed_time_since_user_first_modified_omnibox), | 1129 elapsed_time_since_user_first_modified_omnibox), |
1086 inline_autocompleted_length(inline_autocompleted_length), | 1130 inline_autocompleted_length(inline_autocompleted_length), |
1087 result(result) { | 1131 result(result) { |
1088 } | 1132 } |
OLD | NEW |