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

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: Addressed comments. 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/format_macros.h"
14 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
15 #include "base/string_number_conversions.h" 16 #include "base/string_number_conversions.h"
16 #include "base/string_util.h" 17 #include "base/string_util.h"
18 #include "base/stringprintf.h"
17 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
18 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" 20 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h"
19 #include "chrome/browser/autocomplete/autocomplete_match.h" 21 #include "chrome/browser/autocomplete/autocomplete_match.h"
20 #include "chrome/browser/autocomplete/builtin_provider.h" 22 #include "chrome/browser/autocomplete/builtin_provider.h"
21 #include "chrome/browser/autocomplete/extension_app_provider.h" 23 #include "chrome/browser/autocomplete/extension_app_provider.h"
22 #include "chrome/browser/autocomplete/history_contents_provider.h" 24 #include "chrome/browser/autocomplete/history_contents_provider.h"
23 #include "chrome/browser/autocomplete/history_quick_provider.h" 25 #include "chrome/browser/autocomplete/history_quick_provider.h"
24 #include "chrome/browser/autocomplete/history_url_provider.h" 26 #include "chrome/browser/autocomplete/history_url_provider.h"
25 #include "chrome/browser/autocomplete/keyword_provider.h" 27 #include "chrome/browser/autocomplete/keyword_provider.h"
26 #include "chrome/browser/autocomplete/search_provider.h" 28 #include "chrome/browser/autocomplete/search_provider.h"
(...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 AutocompleteResult::const_iterator AutocompleteResult::end() const { 777 AutocompleteResult::const_iterator AutocompleteResult::end() const {
776 return matches_.end(); 778 return matches_.end();
777 } 779 }
778 780
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_LT(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_LT(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 } // namespace
894
855 const int AutocompleteController::kNoItemSelected = -1; 895 const int AutocompleteController::kNoItemSelected = -1;
856 896
857 // Amount of time (in ms) between when the user stops typing and when we remove 897 // 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 898 // 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 899 // providers (such as SearchProvider) wait for the user to stop typing before
860 // they initiate a query. 900 // they initiate a query.
861 static const int kExpireTimeMS = 500; 901 static const int kExpireTimeMS = 500;
862 902
863 AutocompleteController::AutocompleteController( 903 AutocompleteController::AutocompleteController(
864 Profile* profile, 904 Profile* profile,
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 #endif 1077 #endif
1038 1078
1039 if (!done_) { 1079 if (!done_) {
1040 // This conditional needs to match the conditional in Start that invokes 1080 // This conditional needs to match the conditional in Start that invokes
1041 // StartExpireTimer. 1081 // StartExpireTimer.
1042 result_.CopyOldMatches(input_, last_result); 1082 result_.CopyOldMatches(input_, last_result);
1043 } 1083 }
1044 1084
1045 UpdateKeywordDescriptions(&result_); 1085 UpdateKeywordDescriptions(&result_);
1046 UpdateAssociatedKeywords(&result_); 1086 UpdateAssociatedKeywords(&result_);
1087 UpdateAssistedQueryStats(&result_);
1047 1088
1048 bool notify_default_match = is_synchronous_pass; 1089 bool notify_default_match = is_synchronous_pass;
1049 if (!is_synchronous_pass) { 1090 if (!is_synchronous_pass) {
1050 const bool last_default_was_valid = 1091 const bool last_default_was_valid =
1051 last_result.default_match() != last_result.end(); 1092 last_result.default_match() != last_result.end();
1052 const bool default_is_valid = result_.default_match() != result_.end(); 1093 const bool default_is_valid = result_.default_match() != result_.end();
1053 // We've gotten async results. Send notification that the default match 1094 // We've gotten async results. Send notification that the default match
1054 // updated if fill_into_edit differs or associated_keyword differ. (The 1095 // 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 1096 // 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 1097 // 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( 1134 match->associated_keyword.reset(new AutocompleteMatch(
1094 keyword_provider_->CreateAutocompleteMatch(match->fill_into_edit, 1135 keyword_provider_->CreateAutocompleteMatch(match->fill_into_edit,
1095 keyword, input_))); 1136 keyword, input_)));
1096 } else { 1137 } else {
1097 match->associated_keyword.reset(); 1138 match->associated_keyword.reset();
1098 } 1139 }
1099 } 1140 }
1100 } 1141 }
1101 } 1142 }
1102 1143
1144 void AutocompleteController::UpdateAssistedQueryStats(
1145 AutocompleteResult* result) {
1146 if (result->empty())
1147 return;
1148
1149 // Build the impressions string (the AQS part after ".").
1150 std::string autocompletions;
1151 int count = 0;
1152 int last_type = -1;
1153 for (ACMatches::iterator match(result->begin()); match != result->end();
1154 ++match) {
1155 int type = AutocompleteMatchToAssistedQueryType(match->type);
1156 if (last_type != -1 && type != last_type) {
1157 AppendAvailableAutocompletion(last_type, count, &autocompletions);
1158 count = 1;
1159 } else {
1160 count++;
1161 }
1162 last_type = type;
1163 }
1164 AppendAvailableAutocompletion(last_type, count, &autocompletions);
1165
1166 // Go over all matches and set AQS if the match supports it.
1167 for (size_t index = 0; index < result->size(); ++index) {
1168 AutocompleteMatch* match = result->match_at(index);
1169 const TemplateURL* template_url = match->GetTemplateURL(profile_);
1170 if (!template_url || !match->search_terms_args.get())
1171 continue;
1172 match->search_terms_args->assisted_query_stats =
1173 base::StringPrintf("chrome.%" PRIuS ".%s",
1174 index,
1175 autocompletions.c_str());
1176 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms(
1177 *match->search_terms_args));
1178 }
1179 }
1180
1103 void AutocompleteController::UpdateKeywordDescriptions( 1181 void AutocompleteController::UpdateKeywordDescriptions(
1104 AutocompleteResult* result) { 1182 AutocompleteResult* result) {
1105 string16 last_keyword; 1183 string16 last_keyword;
1106 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); 1184 for (AutocompleteResult::iterator i = result->begin(); i != result->end();
1107 ++i) { 1185 ++i) {
1108 if (((i->provider == keyword_provider_) && !i->keyword.empty()) || 1186 if (((i->provider == keyword_provider_) && !i->keyword.empty()) ||
1109 ((i->provider == search_provider_) && 1187 ((i->provider == search_provider_) &&
1110 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || 1188 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED ||
1111 i->type == AutocompleteMatch::SEARCH_HISTORY || 1189 i->type == AutocompleteMatch::SEARCH_HISTORY ||
1112 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { 1190 i->type == AutocompleteMatch::SEARCH_SUGGEST))) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 current_page_classification(current_page_classification), 1257 current_page_classification(current_page_classification),
1180 elapsed_time_since_user_first_modified_omnibox( 1258 elapsed_time_since_user_first_modified_omnibox(
1181 elapsed_time_since_user_first_modified_omnibox), 1259 elapsed_time_since_user_first_modified_omnibox),
1182 inline_autocompleted_length(inline_autocompleted_length), 1260 inline_autocompleted_length(inline_autocompleted_length),
1183 result(result), 1261 result(result),
1184 providers_info() { 1262 providers_info() {
1185 } 1263 }
1186 1264
1187 AutocompleteLog::~AutocompleteLog() { 1265 AutocompleteLog::~AutocompleteLog() {
1188 } 1266 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698