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

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: Moved kMaxRelevance inside the function which uses it. 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 18 matching lines...) Expand all
45 #include "googleurl/src/url_canon_ip.h" 47 #include "googleurl/src/url_canon_ip.h"
46 #include "googleurl/src/url_util.h" 48 #include "googleurl/src/url_util.h"
47 #include "grit/generated_resources.h" 49 #include "grit/generated_resources.h"
48 #include "grit/theme_resources.h" 50 #include "grit/theme_resources.h"
49 #include "net/base/net_util.h" 51 #include "net/base/net_util.h"
50 #include "net/base/registry_controlled_domain.h" 52 #include "net/base/registry_controlled_domain.h"
51 #include "net/url_request/url_request.h" 53 #include "net/url_request/url_request.h"
52 #include "ui/base/l10n/l10n_util.h" 54 #include "ui/base/l10n/l10n_util.h"
53 55
54 using base::TimeDelta; 56 using base::TimeDelta;
55 57
tfarina 2012/06/18 22:01:57 nit: why add this extra blank line here?
Bart N 2012/06/18 22:26:24 Done.
Peter Kasting 2012/06/19 00:38:08 Actually I preferred it as it was, which is why I
Bart N 2012/06/19 01:30:17 Done.
58
56 // AutocompleteInput ---------------------------------------------------------- 59 // AutocompleteInput ----------------------------------------------------------
57 60
58 AutocompleteInput::AutocompleteInput() 61 AutocompleteInput::AutocompleteInput()
59 : type_(INVALID), 62 : type_(INVALID),
60 prevent_inline_autocomplete_(false), 63 prevent_inline_autocomplete_(false),
61 prefer_keyword_(false), 64 prefer_keyword_(false),
62 allow_exact_keyword_match_(true), 65 allow_exact_keyword_match_(true),
63 matches_requested_(ALL_MATCHES) { 66 matches_requested_(ALL_MATCHES) {
64 } 67 }
65 68
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 AutocompleteResult::iterator AutocompleteResult::end() { 782 AutocompleteResult::iterator AutocompleteResult::end() {
780 return matches_.end(); 783 return matches_.end();
781 } 784 }
782 785
783 // Returns the match at the given index. 786 // Returns the match at the given index.
784 const AutocompleteMatch& AutocompleteResult::match_at(size_t index) const { 787 const AutocompleteMatch& AutocompleteResult::match_at(size_t index) const {
785 DCHECK(index < matches_.size()); 788 DCHECK(index < matches_.size());
786 return matches_[index]; 789 return matches_[index];
787 } 790 }
788 791
792 AutocompleteMatch* AutocompleteResult::match_at(size_t index) {
793 DCHECK(index < matches_.size());
tfarina 2012/06/18 22:01:57 nit: DCHECK_LT(...)
Bart N 2012/06/18 22:26:24 Done. Here and below.
794 return &matches_[index];
795 }
796
789 void AutocompleteResult::Reset() { 797 void AutocompleteResult::Reset() {
790 matches_.clear(); 798 matches_.clear();
791 default_match_ = end(); 799 default_match_ = end();
792 } 800 }
793 801
794 void AutocompleteResult::Swap(AutocompleteResult* other) { 802 void AutocompleteResult::Swap(AutocompleteResult* other) {
795 const size_t default_match_offset = default_match_ - begin(); 803 const size_t default_match_offset = default_match_ - begin();
796 const size_t other_default_match_offset = 804 const size_t other_default_match_offset =
797 other->default_match_ - other->begin(); 805 other->default_match_ - other->begin();
798 matches_.swap(other->matches_); 806 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); 853 match.relevance = std::min(max_relevance, match.relevance);
846 match.from_previous = true; 854 match.from_previous = true;
847 AddMatch(match); 855 AddMatch(match);
848 delta--; 856 delta--;
849 } 857 }
850 } 858 }
851 } 859 }
852 860
853 // AutocompleteController ----------------------------------------------------- 861 // AutocompleteController -----------------------------------------------------
854 862
863 namespace {
tfarina 2012/06/18 22:01:57 this usually goes into the top of the source file
Bart N 2012/06/18 22:26:24 I put it originally at the top but my reviewer ask
Peter Kasting 2012/06/19 00:38:08 Indeed, please leave it.
Bart N 2012/06/19 01:30:17 ACK-ed. On 2012/06/19 00:38:08, Peter Kasting wrot
864
865 // Converts the given type to an integer based on the AQS specification.
866 // For more details, See http://goto.google.com/binary-clients-logging .
867 int AutocompleteMatchToAssistedQueryType(const AutocompleteMatch::Type& type) {
868 switch (type) {
869 case AutocompleteMatch::SEARCH_SUGGEST: return 0;
870 case AutocompleteMatch::NAVSUGGEST: return 5;
871 case AutocompleteMatch::SEARCH_WHAT_YOU_TYPED: return 57;
872 case AutocompleteMatch::URL_WHAT_YOU_TYPED: return 58;
873 case AutocompleteMatch::SEARCH_HISTORY: return 59;
874 case AutocompleteMatch::HISTORY_URL: return 60;
875 case AutocompleteMatch::HISTORY_TITLE: return 61;
876 case AutocompleteMatch::HISTORY_BODY: return 62;
877 case AutocompleteMatch::HISTORY_KEYWORD: return 63;
878 default: return 64;
879 }
880 }
881
882 // Appends available autocompletion of the given type and number to the existing
883 // available autocompletions string, encoding according to the spec.
884 void AppendAvailableAutocompletion(int type,
885 int count,
886 std::string* autocompletions) {
887 if (!autocompletions->empty())
888 autocompletions->append("j");
889 base::StringAppendF(autocompletions, "%d", type);
890 if (count > 1)
891 base::StringAppendF(autocompletions, "l%d", count);
892 }
893
894 } // namespace
895
855 const int AutocompleteController::kNoItemSelected = -1; 896 const int AutocompleteController::kNoItemSelected = -1;
856 897
857 // Amount of time (in ms) between when the user stops typing and when we remove 898 // 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 899 // 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 900 // providers (such as SearchProvider) wait for the user to stop typing before
860 // they initiate a query. 901 // they initiate a query.
861 static const int kExpireTimeMS = 500; 902 static const int kExpireTimeMS = 500;
862 903
863 AutocompleteController::AutocompleteController( 904 AutocompleteController::AutocompleteController(
864 Profile* profile, 905 Profile* profile,
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 #endif 1078 #endif
1038 1079
1039 if (!done_) { 1080 if (!done_) {
1040 // This conditional needs to match the conditional in Start that invokes 1081 // This conditional needs to match the conditional in Start that invokes
1041 // StartExpireTimer. 1082 // StartExpireTimer.
1042 result_.CopyOldMatches(input_, last_result); 1083 result_.CopyOldMatches(input_, last_result);
1043 } 1084 }
1044 1085
1045 UpdateKeywordDescriptions(&result_); 1086 UpdateKeywordDescriptions(&result_);
1046 UpdateAssociatedKeywords(&result_); 1087 UpdateAssociatedKeywords(&result_);
1088 UpdateAssistedQueryStats(&result_);
1047 1089
1048 bool notify_default_match = is_synchronous_pass; 1090 bool notify_default_match = is_synchronous_pass;
1049 if (!is_synchronous_pass) { 1091 if (!is_synchronous_pass) {
1050 const bool last_default_was_valid = 1092 const bool last_default_was_valid =
1051 last_result.default_match() != last_result.end(); 1093 last_result.default_match() != last_result.end();
1052 const bool default_is_valid = result_.default_match() != result_.end(); 1094 const bool default_is_valid = result_.default_match() != result_.end();
1053 // We've gotten async results. Send notification that the default match 1095 // We've gotten async results. Send notification that the default match
1054 // updated if fill_into_edit differs or associated_keyword differ. (The 1096 // 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 1097 // 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 1098 // 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( 1135 match->associated_keyword.reset(new AutocompleteMatch(
1094 keyword_provider_->CreateAutocompleteMatch(match->fill_into_edit, 1136 keyword_provider_->CreateAutocompleteMatch(match->fill_into_edit,
1095 keyword, input_))); 1137 keyword, input_)));
1096 } else { 1138 } else {
1097 match->associated_keyword.reset(); 1139 match->associated_keyword.reset();
1098 } 1140 }
1099 } 1141 }
1100 } 1142 }
1101 } 1143 }
1102 1144
1145 void AutocompleteController::UpdateAssistedQueryStats(
1146 AutocompleteResult* result) {
1147 if (result->empty())
1148 return;
1149
1150 // Build the impressions string (the AQS part after ".").
1151 std::string autocompletions;
1152 int count = 0;
1153 int last_type = -1;
1154 for (ACMatches::iterator match(result->begin()); match != result->end();
1155 ++match) {
1156 int type = AutocompleteMatchToAssistedQueryType(match->type);
1157 if (last_type != -1 && type != last_type) {
1158 AppendAvailableAutocompletion(last_type, count, &autocompletions);
1159 count = 1;
1160 } else {
1161 count++;
1162 }
1163 last_type = type;
1164 }
1165 AppendAvailableAutocompletion(last_type, count, &autocompletions);
1166
1167 // Go over all matches and set AQS if the match supports it.
1168 for (size_t index = 0; index < result->size(); ++index) {
1169 AutocompleteMatch* match = result->match_at(index);
1170 const TemplateURL* template_url = match->GetTemplateURL(profile_);
1171 if (!template_url || !match->search_terms_args.get())
1172 continue;
1173 match->search_terms_args->assisted_query_stats =
1174 base::StringPrintf("chrome.%" PRIuS ".%s",
1175 index,
1176 autocompletions.c_str());
1177 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms(
1178 *match->search_terms_args));
1179 }
1180 }
1181
1103 void AutocompleteController::UpdateKeywordDescriptions( 1182 void AutocompleteController::UpdateKeywordDescriptions(
1104 AutocompleteResult* result) { 1183 AutocompleteResult* result) {
1105 string16 last_keyword; 1184 string16 last_keyword;
1106 for (AutocompleteResult::iterator i = result->begin(); i != result->end(); 1185 for (AutocompleteResult::iterator i = result->begin(); i != result->end();
1107 ++i) { 1186 ++i) {
1108 if (((i->provider == keyword_provider_) && !i->keyword.empty()) || 1187 if (((i->provider == keyword_provider_) && !i->keyword.empty()) ||
1109 ((i->provider == search_provider_) && 1188 ((i->provider == search_provider_) &&
1110 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || 1189 (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED ||
1111 i->type == AutocompleteMatch::SEARCH_HISTORY || 1190 i->type == AutocompleteMatch::SEARCH_HISTORY ||
1112 i->type == AutocompleteMatch::SEARCH_SUGGEST))) { 1191 i->type == AutocompleteMatch::SEARCH_SUGGEST))) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 current_page_classification(current_page_classification), 1258 current_page_classification(current_page_classification),
1180 elapsed_time_since_user_first_modified_omnibox( 1259 elapsed_time_since_user_first_modified_omnibox(
1181 elapsed_time_since_user_first_modified_omnibox), 1260 elapsed_time_since_user_first_modified_omnibox),
1182 inline_autocompleted_length(inline_autocompleted_length), 1261 inline_autocompleted_length(inline_autocompleted_length),
1183 result(result), 1262 result(result),
1184 providers_info() { 1263 providers_info() {
1185 } 1264 }
1186 1265
1187 AutocompleteLog::~AutocompleteLog() { 1266 AutocompleteLog::~AutocompleteLog() {
1188 } 1267 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698