Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(634)

Side by Side Diff: chrome/browser/autocomplete/autocomplete.cc

Issue 10537154: A working implementation of AQS (Assisted Query Stats). (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Added Autocomplete unit tests. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <iterator> 8 #include <iterator>
9 #include <set> 9 #include <set>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/i18n/number_formatting.h" 13 #include "base/i18n/number_formatting.h"
14 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
15 #include "base/string_number_conversions.h" 15 #include "base/string_number_conversions.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/stringprintf.h"
17 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
18 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" 19 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h"
19 #include "chrome/browser/autocomplete/autocomplete_match.h" 20 #include "chrome/browser/autocomplete/autocomplete_match.h"
20 #include "chrome/browser/autocomplete/builtin_provider.h" 21 #include "chrome/browser/autocomplete/builtin_provider.h"
21 #include "chrome/browser/autocomplete/extension_app_provider.h" 22 #include "chrome/browser/autocomplete/extension_app_provider.h"
22 #include "chrome/browser/autocomplete/history_contents_provider.h" 23 #include "chrome/browser/autocomplete/history_contents_provider.h"
23 #include "chrome/browser/autocomplete/history_quick_provider.h" 24 #include "chrome/browser/autocomplete/history_quick_provider.h"
24 #include "chrome/browser/autocomplete/history_url_provider.h" 25 #include "chrome/browser/autocomplete/history_url_provider.h"
25 #include "chrome/browser/autocomplete/keyword_provider.h" 26 #include "chrome/browser/autocomplete/keyword_provider.h"
26 #include "chrome/browser/autocomplete/search_provider.h" 27 #include "chrome/browser/autocomplete/search_provider.h"
(...skipping 19 matching lines...) Expand all
46 #include "googleurl/src/url_util.h" 47 #include "googleurl/src/url_util.h"
47 #include "grit/generated_resources.h" 48 #include "grit/generated_resources.h"
48 #include "grit/theme_resources.h" 49 #include "grit/theme_resources.h"
49 #include "net/base/net_util.h" 50 #include "net/base/net_util.h"
50 #include "net/base/registry_controlled_domain.h" 51 #include "net/base/registry_controlled_domain.h"
51 #include "net/url_request/url_request.h" 52 #include "net/url_request/url_request.h"
52 #include "ui/base/l10n/l10n_util.h" 53 #include "ui/base/l10n/l10n_util.h"
53 54
54 using base::TimeDelta; 55 using base::TimeDelta;
55 56
57
56 // AutocompleteInput ---------------------------------------------------------- 58 // AutocompleteInput ----------------------------------------------------------
57 59
58 AutocompleteInput::AutocompleteInput() 60 AutocompleteInput::AutocompleteInput()
59 : type_(INVALID), 61 : type_(INVALID),
60 prevent_inline_autocomplete_(false), 62 prevent_inline_autocomplete_(false),
61 prefer_keyword_(false), 63 prefer_keyword_(false),
62 allow_exact_keyword_match_(true), 64 allow_exact_keyword_match_(true),
63 matches_requested_(ALL_MATCHES) { 65 matches_requested_(ALL_MATCHES) {
64 } 66 }
65 67
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 AutocompleteResult::iterator AutocompleteResult::end() { 781 AutocompleteResult::iterator AutocompleteResult::end() {
780 return matches_.end(); 782 return matches_.end();
781 } 783 }
782 784
783 // Returns the match at the given index. 785 // Returns the match at the given index.
784 const AutocompleteMatch& AutocompleteResult::match_at(size_t index) const { 786 const AutocompleteMatch& AutocompleteResult::match_at(size_t index) const {
785 DCHECK(index < matches_.size()); 787 DCHECK(index < matches_.size());
786 return matches_[index]; 788 return matches_[index];
787 } 789 }
788 790
791 AutocompleteMatch* AutocompleteResult::match_at(size_t index) {
792 DCHECK(index < matches_.size());
793 return &matches_[index];
794 }
795
789 void AutocompleteResult::Reset() { 796 void AutocompleteResult::Reset() {
790 matches_.clear(); 797 matches_.clear();
791 default_match_ = end(); 798 default_match_ = end();
792 } 799 }
793 800
794 void AutocompleteResult::Swap(AutocompleteResult* other) { 801 void AutocompleteResult::Swap(AutocompleteResult* other) {
795 const size_t default_match_offset = default_match_ - begin(); 802 const size_t default_match_offset = default_match_ - begin();
796 const size_t other_default_match_offset = 803 const size_t other_default_match_offset =
797 other->default_match_ - other->begin(); 804 other->default_match_ - other->begin();
798 matches_.swap(other->matches_); 805 matches_.swap(other->matches_);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 match.relevance = std::min(max_relevance, match.relevance); 852 match.relevance = std::min(max_relevance, match.relevance);
846 match.from_previous = true; 853 match.from_previous = true;
847 AddMatch(match); 854 AddMatch(match);
848 delta--; 855 delta--;
849 } 856 }
850 } 857 }
851 } 858 }
852 859
853 // AutocompleteController ----------------------------------------------------- 860 // AutocompleteController -----------------------------------------------------
854 861
862 namespace {
863
864 // Converts the given type to an integer based on the AQS specification.
865 // For more details, See http://goto.google.com/binary-clients-logging .
866 int AutocompleteMatchToAssistedQueryType(const AutocompleteMatch::Type& type) {
867 switch (type) {
868 case AutocompleteMatch::SEARCH_SUGGEST: return 0;
869 case AutocompleteMatch::NAVSUGGEST: return 5;
870 case AutocompleteMatch::SEARCH_WHAT_YOU_TYPED: return 57;
871 case AutocompleteMatch::URL_WHAT_YOU_TYPED: return 58;
872 case AutocompleteMatch::SEARCH_HISTORY: return 59;
873 case AutocompleteMatch::HISTORY_URL: return 60;
874 case AutocompleteMatch::HISTORY_TITLE: return 61;
875 case AutocompleteMatch::HISTORY_BODY: return 62;
876 case AutocompleteMatch::HISTORY_KEYWORD: return 63;
877 default: return 64;
878 }
879 }
880
881 // Appends available autocompletion of the given type and number to the existing
882 // available autocompletions string, encoding according to the spec.
883 void AppendAvailableAutocompletion(int type,
884 int count,
885 std::string* autocompletions) {
886 if (!autocompletions->empty())
887 autocompletions->append("j");
888 base::StringAppendF(autocompletions, "%d", type);
889 if (count > 1)
890 base::StringAppendF(autocompletions, "l%d", count);
891 }
892
893 // Constructs the final form of AQS param.
894 std::string ConstructAssistedQueryStatsString(
895 const std::string& client,
896 int query_index,
Peter Kasting 2012/06/18 19:45:52 Taking an int here results in a typecast (bad anyw
Bart N 2012/06/18 20:34:18 Ah thanks, I was searching on the web and couldn't
897 const std::string& available_autocompletions) {
898 return base::StringPrintf("%s.%d.%s", client.c_str(), query_index,
899 available_autocompletions.c_str());
900 }
901
902 } // namespace
903
855 const int AutocompleteController::kNoItemSelected = -1; 904 const int AutocompleteController::kNoItemSelected = -1;
856 905
857 // Amount of time (in ms) between when the user stops typing and when we remove 906 // Amount of time (in ms) between when the user stops typing and when we remove
858 // any copied entries. We do this from the time the user stopped typing as some 907 // any copied entries. We do this from the time the user stopped typing as some
859 // providers (such as SearchProvider) wait for the user to stop typing before 908 // providers (such as SearchProvider) wait for the user to stop typing before
860 // they initiate a query. 909 // they initiate a query.
861 static const int kExpireTimeMS = 500; 910 static const int kExpireTimeMS = 500;
862 911
863 AutocompleteController::AutocompleteController( 912 AutocompleteController::AutocompleteController(
864 Profile* profile, 913 Profile* profile,
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 #endif 1086 #endif
1038 1087
1039 if (!done_) { 1088 if (!done_) {
1040 // This conditional needs to match the conditional in Start that invokes 1089 // This conditional needs to match the conditional in Start that invokes
1041 // StartExpireTimer. 1090 // StartExpireTimer.
1042 result_.CopyOldMatches(input_, last_result); 1091 result_.CopyOldMatches(input_, last_result);
1043 } 1092 }
1044 1093
1045 UpdateKeywordDescriptions(&result_); 1094 UpdateKeywordDescriptions(&result_);
1046 UpdateAssociatedKeywords(&result_); 1095 UpdateAssociatedKeywords(&result_);
1096 UpdateAssistedQueryStats(&result_);
1047 1097
1048 bool notify_default_match = is_synchronous_pass; 1098 bool notify_default_match = is_synchronous_pass;
1049 if (!is_synchronous_pass) { 1099 if (!is_synchronous_pass) {
1050 const bool last_default_was_valid = 1100 const bool last_default_was_valid =
1051 last_result.default_match() != last_result.end(); 1101 last_result.default_match() != last_result.end();
1052 const bool default_is_valid = result_.default_match() != result_.end(); 1102 const bool default_is_valid = result_.default_match() != result_.end();
1053 // We've gotten async results. Send notification that the default match 1103 // We've gotten async results. Send notification that the default match
1054 // updated if fill_into_edit differs or associated_keyword differ. (The 1104 // updated if fill_into_edit differs or associated_keyword differ. (The
1055 // latter can change if we've just started Chrome and the keyword database 1105 // latter can change if we've just started Chrome and the keyword database
1056 // finishes loading while processing this request.) We don't check the URL 1106 // finishes loading while processing this request.) We don't check the URL
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1093 match->associated_keyword.reset(new AutocompleteMatch( 1143 match->associated_keyword.reset(new AutocompleteMatch(
1094 keyword_provider_->CreateAutocompleteMatch(match->fill_into_edit, 1144 keyword_provider_->CreateAutocompleteMatch(match->fill_into_edit,
1095 keyword, input_))); 1145 keyword, input_)));
1096 } else { 1146 } else {
1097 match->associated_keyword.reset(); 1147 match->associated_keyword.reset();
1098 } 1148 }
1099 } 1149 }
1100 } 1150 }
1101 } 1151 }
1102 1152
1153 void AutocompleteController::UpdateAssistedQueryStats(
1154 AutocompleteResult* result) {
1155 if (result->empty())
1156 return;
1157
1158 // Build the impressions string (the AQS part after ".").
1159 std::string autocompletions;
1160 int count = 0;
1161 int last_type = -1;
1162 for (ACMatches::iterator match(result->begin()); match != result->end();
1163 ++match) {
1164 int type = AutocompleteMatchToAssistedQueryType(match->type);
1165 if (last_type != -1 && type != last_type) {
1166 AppendAvailableAutocompletion(last_type, count, &autocompletions);
1167 count = 1;
1168 } else {
1169 count++;
1170 }
1171 last_type = type;
1172 }
1173 AppendAvailableAutocompletion(last_type, count, &autocompletions);
1174
1175 // Go over all matches and set AQS if the match supports it.
1176 for (size_t index = 0; index < result->size(); ++index) {
1177 AutocompleteMatch* match = result->match_at(index);
1178 const TemplateURL* template_url = match->GetTemplateURL(profile_);
1179 if (!template_url || !match->search_terms_args.get())
1180 continue;
1181 match->search_terms_args->assisted_query_stats =
1182 ConstructAssistedQueryStatsString("chrome", index, autocompletions);
1183 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms(
1184 *match->search_terms_args));
1185 }
1186 }
1187
1103 void AutocompleteController::UpdateKeywordDescriptions( 1188 void AutocompleteController::UpdateKeywordDescriptions(
1104 AutocompleteResult* result) { 1189 AutocompleteResult* result) {
1105 string16 last_keyword; 1190 string16 last_keyword;
1106 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); 1191 for (AutocompleteResult::iterator i = result->begin(); i != result->end();
1107 ++i) { 1192 ++i) {
1108 if (((i->provider == keyword_provider_) && !i->keyword.empty()) || 1193 if (((i->provider == keyword_provider_) && !i->keyword.empty()) ||
1109 ((i->provider == search_provider_) && 1194 ((i->provider == search_provider_) &&
1110 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || 1195 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED ||
1111 i->type == AutocompleteMatch::SEARCH_HISTORY || 1196 i->type == AutocompleteMatch::SEARCH_HISTORY ||
1112 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { 1197 i->type == AutocompleteMatch::SEARCH_SUGGEST))) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 current_page_classification(current_page_classification), 1264 current_page_classification(current_page_classification),
1180 elapsed_time_since_user_first_modified_omnibox( 1265 elapsed_time_since_user_first_modified_omnibox(
1181 elapsed_time_since_user_first_modified_omnibox), 1266 elapsed_time_since_user_first_modified_omnibox),
1182 inline_autocompleted_length(inline_autocompleted_length), 1267 inline_autocompleted_length(inline_autocompleted_length),
1183 result(result), 1268 result(result),
1184 providers_info() { 1269 providers_info() {
1185 } 1270 }
1186 1271
1187 AutocompleteLog::~AutocompleteLog() { 1272 AutocompleteLog::~AutocompleteLog() {
1188 } 1273 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698