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/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 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); | 682 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); |
678 ACMatches::iterator::difference_type default_offset = | 683 ACMatches::iterator::difference_type default_offset = |
679 default_match_ - begin(); | 684 default_match_ - begin(); |
680 if ((insertion_point - begin()) <= default_offset) | 685 if ((insertion_point - begin()) <= default_offset) |
681 ++default_offset; | 686 ++default_offset; |
682 matches_.insert(insertion_point, match); | 687 matches_.insert(insertion_point, match); |
683 default_match_ = begin() + default_offset; | 688 default_match_ = begin() + default_offset; |
684 } | 689 } |
685 | 690 |
686 void AutocompleteResult::SortAndCull(const AutocompleteInput& input) { | 691 void AutocompleteResult::SortAndCull(const AutocompleteInput& input) { |
| 692 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i) |
| 693 i->ComputeStrippedDestinationURL(); |
| 694 |
687 // Remove duplicates. | 695 // Remove duplicates. |
688 std::sort(matches_.begin(), matches_.end(), | 696 std::sort(matches_.begin(), matches_.end(), |
689 &AutocompleteMatch::DestinationSortFunc); | 697 &AutocompleteMatch::DestinationSortFunc); |
690 matches_.erase(std::unique(matches_.begin(), matches_.end(), | 698 matches_.erase(std::unique(matches_.begin(), matches_.end(), |
691 &AutocompleteMatch::DestinationsEqual), | 699 &AutocompleteMatch::DestinationsEqual), |
692 matches_.end()); | 700 matches_.end()); |
693 | 701 |
694 // Sort and trim to the most relevant kMaxMatches matches. | 702 // Sort and trim to the most relevant kMaxMatches matches. |
695 const size_t num_matches = std::min(kMaxMatches, matches_.size()); | 703 const size_t num_matches = std::min(kMaxMatches, matches_.size()); |
696 std::partial_sort(matches_.begin(), matches_.begin() + num_matches, | 704 std::partial_sort(matches_.begin(), matches_.begin() + num_matches, |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
982 result_.Validate(); | 990 result_.Validate(); |
983 #endif | 991 #endif |
984 | 992 |
985 if (!done_) { | 993 if (!done_) { |
986 // This conditional needs to match the conditional in Start that invokes | 994 // This conditional needs to match the conditional in Start that invokes |
987 // StartExpireTimer. | 995 // StartExpireTimer. |
988 result_.CopyOldMatches(input_, last_result); | 996 result_.CopyOldMatches(input_, last_result); |
989 } | 997 } |
990 | 998 |
991 UpdateKeywordDescriptions(&result_); | 999 UpdateKeywordDescriptions(&result_); |
| 1000 UpdateAssociatedKeywords(&result_); |
992 | 1001 |
993 bool notify_default_match = is_synchronous_pass; | 1002 bool notify_default_match = is_synchronous_pass; |
994 if (!is_synchronous_pass) { | 1003 if (!is_synchronous_pass) { |
995 const bool last_default_was_valid = | 1004 const bool last_default_was_valid = |
996 last_result.default_match() != last_result.end(); | 1005 last_result.default_match() != last_result.end(); |
997 const bool default_is_valid = result_.default_match() != result_.end(); | 1006 const bool default_is_valid = result_.default_match() != result_.end(); |
998 // We've gotten async results. Send notification that the default match | 1007 // We've gotten async results. Send notification that the default match |
999 // updated if fill_into_edit differs. We don't check the URL as that may | 1008 // updated if fill_into_edit differs or associated_keyword differ. (The |
1000 // change for the default match even though the fill into edit hasn't | 1009 // latter can change if we've just started Chrome and the keyword database |
1001 // changed (see SearchProvider for one case of this). | 1010 // finishes loading while processing this request.) We don't check the URL |
| 1011 // as that may change for the default match even though the fill into edit |
| 1012 // hasn't changed (see SearchProvider for one case of this). |
1002 notify_default_match = | 1013 notify_default_match = |
1003 (last_default_was_valid != default_is_valid) || | 1014 (last_default_was_valid != default_is_valid) || |
1004 (default_is_valid && | 1015 (default_is_valid && |
1005 (result_.default_match()->fill_into_edit != | 1016 ((result_.default_match()->fill_into_edit != |
1006 last_result.default_match()->fill_into_edit)); | 1017 last_result.default_match()->fill_into_edit) || |
| 1018 (result_.default_match()->associated_keyword.get() != |
| 1019 last_result.default_match()->associated_keyword.get()))); |
1007 } | 1020 } |
1008 | 1021 |
1009 NotifyChanged(notify_default_match); | 1022 NotifyChanged(notify_default_match); |
1010 } | 1023 } |
1011 | 1024 |
| 1025 void AutocompleteController::UpdateAssociatedKeywords( |
| 1026 AutocompleteResult* result) { |
| 1027 if (!keyword_provider_) |
| 1028 return; |
| 1029 |
| 1030 std::set<string16> keywords; |
| 1031 for (ACMatches::iterator match(result->begin()); match != result->end(); |
| 1032 ++match) { |
| 1033 if (!match->keyword.empty()) { |
| 1034 keywords.insert(match->keyword); |
| 1035 } else { |
| 1036 string16 keyword = match->associated_keyword.get() ? |
| 1037 match->associated_keyword->keyword : |
| 1038 keyword_provider_->GetKeywordForText(match->fill_into_edit); |
| 1039 |
| 1040 // Only add the keyword if the match does not have a duplicate keyword |
| 1041 // with a more relevant match. |
| 1042 if (!keyword.empty() && !keywords.count(keyword)) { |
| 1043 keywords.insert(keyword); |
| 1044 |
| 1045 if (!match->associated_keyword.get()) |
| 1046 match->associated_keyword.reset(new AutocompleteMatch( |
| 1047 keyword_provider_->CreateAutocompleteMatch(match->fill_into_edit, |
| 1048 keyword, input_))); |
| 1049 } else { |
| 1050 match->associated_keyword.reset(); |
| 1051 } |
| 1052 } |
| 1053 } |
| 1054 } |
| 1055 |
1012 void AutocompleteController::UpdateKeywordDescriptions( | 1056 void AutocompleteController::UpdateKeywordDescriptions( |
1013 AutocompleteResult* result) { | 1057 AutocompleteResult* result) { |
1014 const TemplateURL* last_template_url = NULL; | 1058 const TemplateURL* last_template_url = NULL; |
1015 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); | 1059 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); |
1016 ++i) { | 1060 ++i) { |
1017 if (((i->provider == keyword_provider_) && i->template_url) || | 1061 if (((i->provider == keyword_provider_) && i->template_url) || |
1018 ((i->provider == search_provider_) && | 1062 ((i->provider == search_provider_) && |
1019 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || | 1063 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || |
1020 i->type == AutocompleteMatch::SEARCH_HISTORY || | 1064 i->type == AutocompleteMatch::SEARCH_HISTORY || |
1021 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { | 1065 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1057 } | 1101 } |
1058 done_ = true; | 1102 done_ = true; |
1059 } | 1103 } |
1060 | 1104 |
1061 void AutocompleteController::StartExpireTimer() { | 1105 void AutocompleteController::StartExpireTimer() { |
1062 if (result_.HasCopiedMatches()) | 1106 if (result_.HasCopiedMatches()) |
1063 expire_timer_.Start(FROM_HERE, | 1107 expire_timer_.Start(FROM_HERE, |
1064 base::TimeDelta::FromMilliseconds(kExpireTimeMS), | 1108 base::TimeDelta::FromMilliseconds(kExpireTimeMS), |
1065 this, &AutocompleteController::ExpireCopiedEntries); | 1109 this, &AutocompleteController::ExpireCopiedEntries); |
1066 } | 1110 } |
OLD | NEW |