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

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

Issue 171323002: Part 6 of search provider refactoring. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update comment on ClearAllResults Created 6 years, 10 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
« no previous file with comments | « chrome/browser/autocomplete/zero_suggest_provider.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/zero_suggest_provider.h" 5 #include "chrome/browser/autocomplete/zero_suggest_provider.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/i18n/case_conversion.h" 8 #include "base/i18n/case_conversion.h"
9 #include "base/json/json_string_value_serializer.h" 9 #include "base/json/json_string_value_serializer.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 base::string16 prefix; 132 base::string16 prefix;
133 TemplateURLRef::SearchTermsArgs search_term_args(prefix); 133 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
134 search_term_args.current_page_url = current_query_; 134 search_term_args.current_page_url = current_query_;
135 GURL suggest_url(default_provider->suggestions_url_ref(). 135 GURL suggest_url(default_provider->suggestions_url_ref().
136 ReplaceSearchTerms(search_term_args)); 136 ReplaceSearchTerms(search_term_args));
137 if (!CanSendURL(current_page_url, suggest_url, 137 if (!CanSendURL(current_page_url, suggest_url,
138 template_url_service_->GetDefaultSearchProvider(), 138 template_url_service_->GetDefaultSearchProvider(),
139 page_classification, profile_) || 139 page_classification, profile_) ||
140 !OmniboxFieldTrial::InZeroSuggestFieldTrial()) 140 !OmniboxFieldTrial::InZeroSuggestFieldTrial())
141 return; 141 return;
142 verbatim_relevance_ = kDefaultVerbatimZeroSuggestRelevance;
143 done_ = false; 142 done_ = false;
144 // TODO(jered): Consider adding locally-sourced zero-suggestions here too. 143 // TODO(jered): Consider adding locally-sourced zero-suggestions here too.
145 // These may be useful on the NTP or more relevant to the user than server 144 // These may be useful on the NTP or more relevant to the user than server
146 // suggestions, if based on local browsing history. 145 // suggestions, if based on local browsing history.
147 Run(suggest_url); 146 Run(suggest_url);
148 } 147 }
149 148
150 ZeroSuggestProvider::ZeroSuggestProvider( 149 ZeroSuggestProvider::ZeroSuggestProvider(
151 AutocompleteProviderListener* listener, 150 AutocompleteProviderListener* listener,
152 Profile* profile) 151 Profile* profile)
153 : BaseSearchProvider(listener, profile, 152 : BaseSearchProvider(listener, profile,
154 AutocompleteProvider::TYPE_ZERO_SUGGEST), 153 AutocompleteProvider::TYPE_ZERO_SUGGEST),
155 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), 154 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)),
156 have_pending_request_(false), 155 have_pending_request_(false),
157 verbatim_relevance_(kDefaultVerbatimZeroSuggestRelevance),
158 weak_ptr_factory_(this) { 156 weak_ptr_factory_(this) {
159 } 157 }
160 158
161 ZeroSuggestProvider::~ZeroSuggestProvider() { 159 ZeroSuggestProvider::~ZeroSuggestProvider() {
162 } 160 }
163 161
164 const TemplateURL* ZeroSuggestProvider::GetTemplateURL( 162 const TemplateURL* ZeroSuggestProvider::GetTemplateURL(
165 const SuggestResult& result) const { 163 const SuggestResult& result) const {
166 // Zero suggest provider should not receive keyword results. 164 // Zero suggest provider should not receive keyword results.
167 DCHECK(!result.from_keyword_provider()); 165 DCHECK(!result.from_keyword_provider());
(...skipping 15 matching lines...) Expand all
183 } 181 }
184 182
185 void ZeroSuggestProvider::StopSuggest() { 183 void ZeroSuggestProvider::StopSuggest() {
186 if (have_pending_request_) 184 if (have_pending_request_)
187 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); 185 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
188 have_pending_request_ = false; 186 have_pending_request_ = false;
189 fetcher_.reset(); 187 fetcher_.reset();
190 } 188 }
191 189
192 void ZeroSuggestProvider::ClearAllResults() { 190 void ZeroSuggestProvider::ClearAllResults() {
193 query_matches_map_.clear(); 191 // We do not call Clear() on |results_| to retain |verbatim_relevance|
194 navigation_results_.clear(); 192 // value in the |results_| object. |verbatim_relevance| is used at the
193 // beginning of the next StartZeroSuggest() call to determine the current url
194 // match relevance.
195 results_.suggest_results.clear();
196 results_.navigation_results.clear();
195 current_query_.clear(); 197 current_query_.clear();
196 matches_.clear(); 198 matches_.clear();
197 } 199 }
198 200
199 void ZeroSuggestProvider::FillResults(const base::Value& root_val,
200 int* verbatim_relevance,
201 SuggestResults* suggest_results,
202 NavigationResults* navigation_results) {
203 base::string16 query;
204 const base::ListValue* root_list = NULL;
205 const base::ListValue* results = NULL;
206 const base::ListValue* relevances = NULL;
207 // The response includes the query, which should be empty for ZeroSuggest
208 // responses.
209 if (!root_val.GetAsList(&root_list) || !root_list->GetString(0, &query) ||
210 (!query.empty()) || !root_list->GetList(1, &results))
211 return;
212
213 // 3rd element: Description list.
214 const base::ListValue* descriptions = NULL;
215 root_list->GetList(2, &descriptions);
216
217 // 4th element: Disregard the query URL list for now.
218
219 // Reset suggested relevance information from the provider.
220 *verbatim_relevance = kDefaultVerbatimZeroSuggestRelevance;
221
222 // 5th element: Optional key-value pairs from the Suggest server.
223 const base::ListValue* types = NULL;
224 const base::DictionaryValue* extras = NULL;
225 if (root_list->GetDictionary(4, &extras)) {
226 extras->GetList("google:suggesttype", &types);
227
228 // Discard this list if its size does not match that of the suggestions.
229 if (extras->GetList("google:suggestrelevance", &relevances) &&
230 relevances->GetSize() != results->GetSize())
231 relevances = NULL;
232 extras->GetInteger("google:verbatimrelevance", verbatim_relevance);
233
234 // Check if the active suggest field trial (if any) has triggered.
235 bool triggered = false;
236 extras->GetBoolean("google:fieldtrialtriggered", &triggered);
237 field_trial_triggered_ |= triggered;
238 field_trial_triggered_in_session_ |= triggered;
239 }
240
241 // Clear the previous results now that new results are available.
242 suggest_results->clear();
243 navigation_results->clear();
244
245 base::string16 result, title;
246 std::string type;
247 const base::string16 current_query_string16 =
248 base::ASCIIToUTF16(current_query_);
249 const std::string languages(
250 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
251 for (size_t index = 0; results->GetString(index, &result); ++index) {
252 // Google search may return empty suggestions for weird input characters,
253 // they make no sense at all and can cause problems in our code.
254 if (result.empty())
255 continue;
256
257 int relevance = kDefaultZeroSuggestRelevance;
258
259 // Apply valid suggested relevance scores; discard invalid lists.
260 if (relevances != NULL && !relevances->GetInteger(index, &relevance))
261 relevances = NULL;
262 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) {
263 // Do not blindly trust the URL coming from the server to be valid.
264 GURL url(URLFixerUpper::FixupURL(
265 base::UTF16ToUTF8(result), std::string()));
266 if (url.is_valid()) {
267 if (descriptions != NULL)
268 descriptions->GetString(index, &title);
269 navigation_results->push_back(NavigationResult(
270 *this, url, title, false, relevance, relevances != NULL,
271 current_query_string16, languages));
272 }
273 } else {
274 suggest_results->push_back(SuggestResult(
275 result, AutocompleteMatchType::SEARCH_SUGGEST, result,
276 base::string16(), std::string(), std::string(), false, relevance,
277 relevances != NULL, false, current_query_string16));
278 }
279 }
280 }
281
282 void ZeroSuggestProvider::AddSuggestResultsToMap( 201 void ZeroSuggestProvider::AddSuggestResultsToMap(
283 const SuggestResults& results, 202 const SuggestResults& results,
284 MatchMap* map) { 203 MatchMap* map) {
285 for (size_t i = 0; i < results.size(); ++i) { 204 for (size_t i = 0; i < results.size(); ++i) {
286 const base::string16& query_string(results[i].suggestion()); 205 const base::string16& query_string(results[i].suggestion());
287 // TODO(mariakhomenko): Do not reconstruct SuggestResult objects with 206 // TODO(mariakhomenko): Do not reconstruct SuggestResult objects with
288 // a different query -- create correct objects to begin with. 207 // a different query -- create correct objects to begin with.
289 const SuggestResult suggestion( 208 const SuggestResult suggestion(
290 query_string, AutocompleteMatchType::SEARCH_SUGGEST, query_string, 209 query_string, AutocompleteMatchType::SEARCH_SUGGEST, query_string,
291 base::string16(), std::string(), std::string(), false, 210 base::string16(), std::string(), std::string(), false,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 ts->GetMostVisitedURLs( 264 ts->GetMostVisitedURLs(
346 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, 265 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
347 weak_ptr_factory_.GetWeakPtr()), false); 266 weak_ptr_factory_.GetWeakPtr()), false);
348 } 267 }
349 } 268 }
350 have_pending_request_ = true; 269 have_pending_request_ = true;
351 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); 270 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
352 } 271 }
353 272
354 void ZeroSuggestProvider::ParseSuggestResults(const base::Value& root_val) { 273 void ZeroSuggestProvider::ParseSuggestResults(const base::Value& root_val) {
355 SuggestResults suggest_results; 274 base::string16 query;
356 FillResults(root_val, &verbatim_relevance_, 275 const base::ListValue* root_list = NULL;
357 &suggest_results, &navigation_results_); 276 const base::ListValue* results = NULL;
277 const base::ListValue* relevances = NULL;
278 // The response includes the query, which should be empty for ZeroSuggest
279 // responses.
280 if (!root_val.GetAsList(&root_list) || !root_list->GetString(0, &query) ||
281 (!query.empty()) || !root_list->GetList(1, &results))
282 return;
358 283
359 query_matches_map_.clear(); 284 // 3rd element: Description list.
360 AddSuggestResultsToMap(suggest_results, &query_matches_map_); 285 const base::ListValue* descriptions = NULL;
286 root_list->GetList(2, &descriptions);
287
288 // 4th element: Disregard the query URL list for now.
289
290 // Reset suggested relevance information from the provider.
291 results_.verbatim_relevance = -1;
292
293 // 5th element: Optional key-value pairs from the Suggest server.
294 const base::ListValue* types = NULL;
295 const base::DictionaryValue* extras = NULL;
296 if (root_list->GetDictionary(4, &extras)) {
297 extras->GetList("google:suggesttype", &types);
298
299 // Discard this list if its size does not match that of the suggestions.
300 if (extras->GetList("google:suggestrelevance", &relevances) &&
301 relevances->GetSize() != results->GetSize())
302 relevances = NULL;
303 extras->GetInteger("google:verbatimrelevance",
304 &results_.verbatim_relevance);
305
306 // Check if the active suggest field trial (if any) has triggered.
307 bool triggered = false;
308 extras->GetBoolean("google:fieldtrialtriggered", &triggered);
309 field_trial_triggered_ |= triggered;
310 field_trial_triggered_in_session_ |= triggered;
311 }
312
313 // Clear the previous results now that new results are available.
314 results_.suggest_results.clear();
315 results_.navigation_results.clear();
316
317 base::string16 result, title;
318 std::string type;
319 const base::string16 current_query_string16 =
320 base::ASCIIToUTF16(current_query_);
321 const std::string languages(
322 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
323 for (size_t index = 0; results->GetString(index, &result); ++index) {
324 // Google search may return empty suggestions for weird input characters,
325 // they make no sense at all and can cause problems in our code.
326 if (result.empty())
327 continue;
328
329 int relevance = kDefaultZeroSuggestRelevance;
330
331 // Apply valid suggested relevance scores; discard invalid lists.
332 if (relevances != NULL && !relevances->GetInteger(index, &relevance))
333 relevances = NULL;
334 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) {
335 // Do not blindly trust the URL coming from the server to be valid.
336 GURL url(URLFixerUpper::FixupURL(
337 base::UTF16ToUTF8(result), std::string()));
338 if (url.is_valid()) {
339 if (descriptions != NULL)
340 descriptions->GetString(index, &title);
341 results_.navigation_results.push_back(NavigationResult(
342 *this, url, title, false, relevance, relevances != NULL,
343 current_query_string16, languages));
344 }
345 } else {
346 results_.suggest_results.push_back(SuggestResult(
347 result, AutocompleteMatchType::SEARCH_SUGGEST, result,
348 base::string16(), std::string(), std::string(), false, relevance,
349 relevances != NULL, false, current_query_string16));
350 }
351 }
361 } 352 }
362 353
363 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( 354 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
364 const history::MostVisitedURLList& urls) { 355 const history::MostVisitedURLList& urls) {
365 most_visited_urls_ = urls; 356 most_visited_urls_ = urls;
366 } 357 }
367 358
368 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { 359 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
369 matches_.clear(); 360 matches_.clear();
370 361
371 const TemplateURL* default_provider = 362 const TemplateURL* default_provider =
372 template_url_service_->GetDefaultSearchProvider(); 363 template_url_service_->GetDefaultSearchProvider();
373 // Fail if we can't set the clickthrough URL for query suggestions. 364 // Fail if we can't set the clickthrough URL for query suggestions.
374 if (default_provider == NULL || !default_provider->SupportsReplacement()) 365 if (default_provider == NULL || !default_provider->SupportsReplacement())
375 return; 366 return;
376 367
377 const int num_query_results = query_matches_map_.size(); 368 MatchMap map;
378 const int num_nav_results = navigation_results_.size(); 369 AddSuggestResultsToMap(results_.suggest_results, &map);
370
371 const int num_query_results = map.size();
372 const int num_nav_results = results_.navigation_results.size();
379 const int num_results = num_query_results + num_nav_results; 373 const int num_results = num_query_results + num_nav_results;
380 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results); 374 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
381 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results); 375 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
382 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results); 376 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
383 377
384 // Show Most Visited results after ZeroSuggest response is received. 378 // Show Most Visited results after ZeroSuggest response is received.
385 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { 379 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
386 if (!current_url_match_.destination_url.is_valid()) 380 if (!current_url_match_.destination_url.is_valid())
387 return; 381 return;
388 matches_.push_back(current_url_match_); 382 matches_.push_back(current_url_match_);
(...skipping 17 matching lines...) Expand all
406 return; 400 return;
407 } 401 }
408 402
409 if (num_results == 0) 403 if (num_results == 0)
410 return; 404 return;
411 405
412 // TODO(jered): Rip this out once the first match is decoupled from the 406 // TODO(jered): Rip this out once the first match is decoupled from the
413 // current typing in the omnibox. 407 // current typing in the omnibox.
414 matches_.push_back(current_url_match_); 408 matches_.push_back(current_url_match_);
415 409
416 for (MatchMap::const_iterator it(query_matches_map_.begin()); 410 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
417 it != query_matches_map_.end(); ++it)
418 matches_.push_back(it->second); 411 matches_.push_back(it->second);
419 412
420 for (NavigationResults::const_iterator it(navigation_results_.begin()); 413 const NavigationResults& nav_results(results_.navigation_results);
421 it != navigation_results_.end(); ++it) 414 for (NavigationResults::const_iterator it(nav_results.begin());
415 it != nav_results.end(); ++it)
422 matches_.push_back(NavigationToMatch(*it)); 416 matches_.push_back(NavigationToMatch(*it));
423 } 417 }
424 418
425 AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() { 419 AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
426 AutocompleteInput input(permanent_text_, base::string16::npos, base::string16( ), 420 AutocompleteInput input(permanent_text_, base::string16::npos, base::string16( ),
427 GURL(current_query_), current_page_classification_, 421 GURL(current_query_), current_page_classification_,
428 false, false, true, AutocompleteInput::ALL_MATCHES); 422 false, false, true, AutocompleteInput::ALL_MATCHES);
429 423
430 AutocompleteMatch match; 424 AutocompleteMatch match;
431 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify( 425 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(
432 permanent_text_, false, true, current_page_classification_, &match, NULL); 426 permanent_text_, false, true, current_page_classification_, &match, NULL);
433 match.is_history_what_you_typed_match = false; 427 match.is_history_what_you_typed_match = false;
434 match.allowed_to_be_default_match = true; 428 match.allowed_to_be_default_match = true;
435 429
436 // The placeholder suggestion for the current URL has high relevance so 430 // The placeholder suggestion for the current URL has high relevance so
437 // that it is in the first suggestion slot and inline autocompleted. It 431 // that it is in the first suggestion slot and inline autocompleted. It
438 // gets dropped as soon as the user types something. 432 // gets dropped as soon as the user types something.
439 match.relevance = verbatim_relevance_; 433 match.relevance = GetVerbatimRelevance();
440 434
441 return match; 435 return match;
442 } 436 }
437
438 int ZeroSuggestProvider::GetVerbatimRelevance() const {
439 return results_.verbatim_relevance >= 0 ?
440 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance;
441 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/zero_suggest_provider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698