| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/notification_service.h" | 43 #include "content/common/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 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); | 649 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); |
| 645 ACMatches::iterator::difference_type default_offset = | 650 ACMatches::iterator::difference_type default_offset = |
| 646 default_match_ - begin(); | 651 default_match_ - begin(); |
| 647 if ((insertion_point - begin()) <= default_offset) | 652 if ((insertion_point - begin()) <= default_offset) |
| 648 ++default_offset; | 653 ++default_offset; |
| 649 matches_.insert(insertion_point, match); | 654 matches_.insert(insertion_point, match); |
| 650 default_match_ = begin() + default_offset; | 655 default_match_ = begin() + default_offset; |
| 651 } | 656 } |
| 652 | 657 |
| 653 void AutocompleteResult::SortAndCull(const AutocompleteInput& input) { | 658 void AutocompleteResult::SortAndCull(const AutocompleteInput& input) { |
| 659 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i) |
| 660 i->ComputeStrippedDestinationURL(); |
| 661 |
| 654 // Remove duplicates. | 662 // Remove duplicates. |
| 655 std::sort(matches_.begin(), matches_.end(), | 663 std::sort(matches_.begin(), matches_.end(), |
| 656 &AutocompleteMatch::DestinationSortFunc); | 664 &AutocompleteMatch::DestinationSortFunc); |
| 657 matches_.erase(std::unique(matches_.begin(), matches_.end(), | 665 matches_.erase(std::unique(matches_.begin(), matches_.end(), |
| 658 &AutocompleteMatch::DestinationsEqual), | 666 &AutocompleteMatch::DestinationsEqual), |
| 659 matches_.end()); | 667 matches_.end()); |
| 660 | 668 |
| 661 // Sort and trim to the most relevant kMaxMatches matches. | 669 // Sort and trim to the most relevant kMaxMatches matches. |
| 662 const size_t num_matches = std::min(kMaxMatches, matches_.size()); | 670 const size_t num_matches = std::min(kMaxMatches, matches_.size()); |
| 663 std::partial_sort(matches_.begin(), matches_.begin() + num_matches, | 671 std::partial_sort(matches_.begin(), matches_.begin() + num_matches, |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 946 AutocompleteResult last_result; | 954 AutocompleteResult last_result; |
| 947 last_result.Swap(&result_); | 955 last_result.Swap(&result_); |
| 948 | 956 |
| 949 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end(); | 957 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end(); |
| 950 ++i) | 958 ++i) |
| 951 result_.AppendMatches((*i)->matches()); | 959 result_.AppendMatches((*i)->matches()); |
| 952 | 960 |
| 953 // Sort the matches and trim to a small number of "best" matches. | 961 // Sort the matches and trim to a small number of "best" matches. |
| 954 result_.SortAndCull(input_); | 962 result_.SortAndCull(input_); |
| 955 | 963 |
| 964 PopulateMatchKeywordHints(); |
| 965 |
| 956 // Need to validate before invoking CopyOldMatches as the old matches are not | 966 // Need to validate before invoking CopyOldMatches as the old matches are not |
| 957 // valid against the current input. | 967 // valid against the current input. |
| 958 #ifndef NDEBUG | 968 #ifndef NDEBUG |
| 959 result_.Validate(); | 969 result_.Validate(); |
| 960 #endif | 970 #endif |
| 961 | 971 |
| 962 if (!done_) { | 972 if (!done_) { |
| 963 // This conditional needs to match the conditional in Start that invokes | 973 // This conditional needs to match the conditional in Start that invokes |
| 964 // StartExpireTimer. | 974 // StartExpireTimer. |
| 965 result_.CopyOldMatches(input_, last_result); | 975 result_.CopyOldMatches(input_, last_result); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 979 notify_default_match = | 989 notify_default_match = |
| 980 (last_default_was_valid != default_is_valid) || | 990 (last_default_was_valid != default_is_valid) || |
| 981 (default_is_valid && | 991 (default_is_valid && |
| 982 (result_.default_match()->fill_into_edit != | 992 (result_.default_match()->fill_into_edit != |
| 983 last_result.default_match()->fill_into_edit)); | 993 last_result.default_match()->fill_into_edit)); |
| 984 } | 994 } |
| 985 | 995 |
| 986 NotifyChanged(notify_default_match); | 996 NotifyChanged(notify_default_match); |
| 987 } | 997 } |
| 988 | 998 |
| 999 void AutocompleteController::PopulateMatchKeywordHints() { |
| 1000 std::set<string16> keywords; |
| 1001 for (ACMatches::iterator match(result_.begin()); match != result_.end(); |
| 1002 ++match) { |
| 1003 if (!match->keyword.empty()) { |
| 1004 keywords.insert(match->keyword); |
| 1005 } else if (keyword_provider_) { |
| 1006 string16 keyword = |
| 1007 keyword_provider_->GetKeywordForText(match->fill_into_edit); |
| 1008 |
| 1009 // Only add the keyword if the match does not have a duplicate keyword |
| 1010 // with a more relevant match. |
| 1011 if (!keyword.empty() && !keywords.count(keyword)) { |
| 1012 match->associated_keyword.reset(new AutocompleteMatch( |
| 1013 keyword_provider_->CreateAutocompleteMatch(keyword, input_))); |
| 1014 |
| 1015 keywords.insert(keyword); |
| 1016 } |
| 1017 } |
| 1018 } |
| 1019 } |
| 1020 |
| 989 void AutocompleteController::UpdateKeywordDescriptions( | 1021 void AutocompleteController::UpdateKeywordDescriptions( |
| 990 AutocompleteResult* result) { | 1022 AutocompleteResult* result) { |
| 991 const TemplateURL* last_template_url = NULL; | 1023 const TemplateURL* last_template_url = NULL; |
| 992 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); | 1024 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); |
| 993 ++i) { | 1025 ++i) { |
| 994 if (((i->provider == keyword_provider_) && i->template_url) || | 1026 if (((i->provider == keyword_provider_) && i->template_url) || |
| 995 ((i->provider == search_provider_) && | 1027 ((i->provider == search_provider_) && |
| 996 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || | 1028 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || |
| 997 i->type == AutocompleteMatch::SEARCH_HISTORY || | 1029 i->type == AutocompleteMatch::SEARCH_HISTORY || |
| 998 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { | 1030 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1033 } | 1065 } |
| 1034 } | 1066 } |
| 1035 done_ = true; | 1067 done_ = true; |
| 1036 } | 1068 } |
| 1037 | 1069 |
| 1038 void AutocompleteController::StartExpireTimer() { | 1070 void AutocompleteController::StartExpireTimer() { |
| 1039 if (result_.HasCopiedMatches()) | 1071 if (result_.HasCopiedMatches()) |
| 1040 expire_timer_.Start(base::TimeDelta::FromMilliseconds(kExpireTimeMS), | 1072 expire_timer_.Start(base::TimeDelta::FromMilliseconds(kExpireTimeMS), |
| 1041 this, &AutocompleteController::ExpireCopiedEntries); | 1073 this, &AutocompleteController::ExpireCopiedEntries); |
| 1042 } | 1074 } |
| 1075 |
| OLD | NEW |