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 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
676 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); | 681 std::upper_bound(begin(), end(), match, &AutocompleteMatch::MoreRelevant); |
677 ACMatches::iterator::difference_type default_offset = | 682 ACMatches::iterator::difference_type default_offset = |
678 default_match_ - begin(); | 683 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 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
981 result_.Validate(); | 989 result_.Validate(); |
982 #endif | 990 #endif |
983 | 991 |
984 if (!done_) { | 992 if (!done_) { |
985 // This conditional needs to match the conditional in Start that invokes | 993 // This conditional needs to match the conditional in Start that invokes |
986 // StartExpireTimer. | 994 // StartExpireTimer. |
987 result_.CopyOldMatches(input_, last_result); | 995 result_.CopyOldMatches(input_, last_result); |
988 } | 996 } |
989 | 997 |
990 UpdateKeywordDescriptions(&result_); | 998 UpdateKeywordDescriptions(&result_); |
999 UpdateAssociatedKeywords(&result_); | |
991 | 1000 |
992 bool notify_default_match = is_synchronous_pass; | 1001 bool notify_default_match = is_synchronous_pass; |
993 if (!is_synchronous_pass) { | 1002 if (!is_synchronous_pass) { |
994 const bool last_default_was_valid = | 1003 const bool last_default_was_valid = |
995 last_result.default_match() != last_result.end(); | 1004 last_result.default_match() != last_result.end(); |
996 const bool default_is_valid = result_.default_match() != result_.end(); | 1005 const bool default_is_valid = result_.default_match() != result_.end(); |
997 // We've gotten async results. Send notification that the default match | 1006 // We've gotten async results. Send notification that the default match |
998 // updated if fill_into_edit differs. We don't check the URL as that may | 1007 // updated if fill_into_edit differs. We don't check the URL as that may |
999 // change for the default match even though the fill into edit hasn't | 1008 // change for the default match even though the fill into edit hasn't |
1000 // changed (see SearchProvider for one case of this). | 1009 // changed (see SearchProvider for one case of this). |
1001 notify_default_match = | 1010 notify_default_match = |
1002 (last_default_was_valid != default_is_valid) || | 1011 (last_default_was_valid != default_is_valid) || |
1003 (default_is_valid && | 1012 (default_is_valid && |
1004 (result_.default_match()->fill_into_edit != | 1013 ((result_.default_match()->fill_into_edit != |
1005 last_result.default_match()->fill_into_edit)); | 1014 last_result.default_match()->fill_into_edit) || |
1015 (result_.default_match()->associated_keyword.get() != | |
1016 last_result.default_match()->associated_keyword.get()))); | |
Peter Kasting
2011/12/15 22:56:04
Can the associated keyword actually differ in this
aaron.randolph
2011/12/20 21:07:57
Yes, there were cases where the result set would u
| |
1006 } | 1017 } |
1007 | 1018 |
1008 NotifyChanged(notify_default_match); | 1019 NotifyChanged(notify_default_match); |
1009 } | 1020 } |
1010 | 1021 |
1022 void AutocompleteController::UpdateAssociatedKeywords( | |
1023 AutocompleteResult* result) { | |
1024 if (!keyword_provider_) | |
1025 return; | |
1026 | |
1027 std::set<string16> keywords; | |
1028 for (ACMatches::iterator match(result->begin()); match != result->end(); | |
1029 ++match) { | |
1030 if (!match->keyword.empty()) { | |
1031 keywords.insert(match->keyword); | |
1032 } else { | |
1033 string16 keyword = match->associated_keyword.get() ? | |
1034 match->associated_keyword->keyword : | |
1035 keyword_provider_->GetKeywordForText(match->fill_into_edit); | |
1036 | |
1037 // Only add the keyword if the match does not have a duplicate keyword | |
1038 // with a more relevant match. | |
1039 if (!keyword.empty() && !keywords.count(keyword)) { | |
1040 keywords.insert(keyword); | |
1041 | |
1042 if (!match->associated_keyword.get()) | |
1043 match->associated_keyword.reset(new AutocompleteMatch( | |
1044 keyword_provider_->CreateAutocompleteMatch(match->fill_into_edit, | |
1045 keyword, input_))); | |
1046 } else { | |
1047 match->associated_keyword.reset(NULL); | |
Peter Kasting
2011/12/15 22:56:04
Nit: NULL arg is unnecessary
| |
1048 } | |
1049 } | |
1050 } | |
1051 } | |
1052 | |
1011 void AutocompleteController::UpdateKeywordDescriptions( | 1053 void AutocompleteController::UpdateKeywordDescriptions( |
1012 AutocompleteResult* result) { | 1054 AutocompleteResult* result) { |
1013 const TemplateURL* last_template_url = NULL; | 1055 const TemplateURL* last_template_url = NULL; |
1014 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); | 1056 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); |
1015 ++i) { | 1057 ++i) { |
1016 if (((i->provider == keyword_provider_) && i->template_url) || | 1058 if (((i->provider == keyword_provider_) && i->template_url) || |
1017 ((i->provider == search_provider_) && | 1059 ((i->provider == search_provider_) && |
1018 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || | 1060 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || |
1019 i->type == AutocompleteMatch::SEARCH_HISTORY || | 1061 i->type == AutocompleteMatch::SEARCH_HISTORY || |
1020 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { | 1062 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1056 } | 1098 } |
1057 done_ = true; | 1099 done_ = true; |
1058 } | 1100 } |
1059 | 1101 |
1060 void AutocompleteController::StartExpireTimer() { | 1102 void AutocompleteController::StartExpireTimer() { |
1061 if (result_.HasCopiedMatches()) | 1103 if (result_.HasCopiedMatches()) |
1062 expire_timer_.Start(FROM_HERE, | 1104 expire_timer_.Start(FROM_HERE, |
1063 base::TimeDelta::FromMilliseconds(kExpireTimeMS), | 1105 base::TimeDelta::FromMilliseconds(kExpireTimeMS), |
1064 this, &AutocompleteController::ExpireCopiedEntries); | 1106 this, &AutocompleteController::ExpireCopiedEntries); |
1065 } | 1107 } |
1108 | |
Peter Kasting
2011/12/15 22:56:04
Nit: Unnecessary newline?
| |
OLD | NEW |