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

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: Made GetVerbatimRelevance const 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
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_| because we use |verbatim_relevance|
194 navigation_results_.clear(); 192 // value on the next query to set the relevance of the current URL match.
H Fung 2014/02/19 06:33:31 I don't think I understand this comment.
Maria 2014/02/19 18:25:53 Clear() call resets results_.relevance value, but
H Fung 2014/02/19 21:03:16 I think there might be a bug since the verbatim sc
193 results_.suggest_results.clear();
194 results_.navigation_results.clear();
195 current_query_.clear(); 195 current_query_.clear();
196 matches_.clear(); 196 matches_.clear();
197 } 197 }
198 198
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( 199 void ZeroSuggestProvider::AddSuggestResultsToMap(
283 const SuggestResults& results, 200 const SuggestResults& results,
284 MatchMap* map) { 201 MatchMap* map) {
285 for (size_t i = 0; i < results.size(); ++i) { 202 for (size_t i = 0; i < results.size(); ++i) {
286 const base::string16& query_string(results[i].suggestion()); 203 const base::string16& query_string(results[i].suggestion());
287 // TODO(mariakhomenko): Do not reconstruct SuggestResult objects with 204 // TODO(mariakhomenko): Do not reconstruct SuggestResult objects with
288 // a different query -- create correct objects to begin with. 205 // a different query -- create correct objects to begin with.
289 const SuggestResult suggestion( 206 const SuggestResult suggestion(
290 query_string, AutocompleteMatchType::SEARCH_SUGGEST, query_string, 207 query_string, AutocompleteMatchType::SEARCH_SUGGEST, query_string,
291 base::string16(), std::string(), std::string(), false, 208 base::string16(), std::string(), std::string(), false,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 ts->GetMostVisitedURLs( 262 ts->GetMostVisitedURLs(
346 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, 263 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
347 weak_ptr_factory_.GetWeakPtr()), false); 264 weak_ptr_factory_.GetWeakPtr()), false);
348 } 265 }
349 } 266 }
350 have_pending_request_ = true; 267 have_pending_request_ = true;
351 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); 268 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
352 } 269 }
353 270
354 void ZeroSuggestProvider::ParseSuggestResults(const base::Value& root_val) { 271 void ZeroSuggestProvider::ParseSuggestResults(const base::Value& root_val) {
355 SuggestResults suggest_results; 272 base::string16 query;
356 FillResults(root_val, &verbatim_relevance_, 273 const base::ListValue* root_list = NULL;
357 &suggest_results, &navigation_results_); 274 const base::ListValue* results = NULL;
275 const base::ListValue* relevances = NULL;
276 // The response includes the query, which should be empty for ZeroSuggest
277 // responses.
278 if (!root_val.GetAsList(&root_list) || !root_list->GetString(0, &query) ||
279 (!query.empty()) || !root_list->GetList(1, &results))
280 return;
358 281
359 query_matches_map_.clear(); 282 // 3rd element: Description list.
360 AddSuggestResultsToMap(suggest_results, &query_matches_map_); 283 const base::ListValue* descriptions = NULL;
284 root_list->GetList(2, &descriptions);
285
286 // 4th element: Disregard the query URL list for now.
287
288 // Reset suggested relevance information from the provider.
289 results_.verbatim_relevance = -1;
H Fung 2014/02/19 06:33:31 It seems like verbatim_relevance is only set in th
Maria 2014/02/19 18:25:53 It's also initialized in Results constructor to -1
290
291 // 5th element: Optional key-value pairs from the Suggest server.
292 const base::ListValue* types = NULL;
293 const base::DictionaryValue* extras = NULL;
294 if (root_list->GetDictionary(4, &extras)) {
295 extras->GetList("google:suggesttype", &types);
296
297 // Discard this list if its size does not match that of the suggestions.
298 if (extras->GetList("google:suggestrelevance", &relevances) &&
299 relevances->GetSize() != results->GetSize())
300 relevances = NULL;
301 extras->GetInteger("google:verbatimrelevance",
302 &results_.verbatim_relevance);
303
304 // Check if the active suggest field trial (if any) has triggered.
305 bool triggered = false;
306 extras->GetBoolean("google:fieldtrialtriggered", &triggered);
307 field_trial_triggered_ |= triggered;
308 field_trial_triggered_in_session_ |= triggered;
309 }
310
311 // Clear the previous results now that new results are available.
312 results_.suggest_results.clear();
313 results_.navigation_results.clear();
314
315 base::string16 result, title;
316 std::string type;
317 const base::string16 current_query_string16 =
318 base::ASCIIToUTF16(current_query_);
319 const std::string languages(
320 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
321 for (size_t index = 0; results->GetString(index, &result); ++index) {
322 // Google search may return empty suggestions for weird input characters,
323 // they make no sense at all and can cause problems in our code.
324 if (result.empty())
325 continue;
326
327 int relevance = kDefaultZeroSuggestRelevance;
328
329 // Apply valid suggested relevance scores; discard invalid lists.
330 if (relevances != NULL && !relevances->GetInteger(index, &relevance))
331 relevances = NULL;
332 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) {
333 // Do not blindly trust the URL coming from the server to be valid.
334 GURL url(URLFixerUpper::FixupURL(
335 base::UTF16ToUTF8(result), std::string()));
336 if (url.is_valid()) {
337 if (descriptions != NULL)
338 descriptions->GetString(index, &title);
339 results_.navigation_results.push_back(NavigationResult(
340 *this, url, title, false, relevance, relevances != NULL,
341 current_query_string16, languages));
342 }
343 } else {
344 results_.suggest_results.push_back(SuggestResult(
345 result, AutocompleteMatchType::SEARCH_SUGGEST, result,
346 base::string16(), std::string(), std::string(), false, relevance,
347 relevances != NULL, false, current_query_string16));
348 }
349 }
361 } 350 }
362 351
363 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( 352 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
364 const history::MostVisitedURLList& urls) { 353 const history::MostVisitedURLList& urls) {
365 most_visited_urls_ = urls; 354 most_visited_urls_ = urls;
366 } 355 }
367 356
368 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { 357 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
369 matches_.clear(); 358 matches_.clear();
370 359
371 const TemplateURL* default_provider = 360 const TemplateURL* default_provider =
372 template_url_service_->GetDefaultSearchProvider(); 361 template_url_service_->GetDefaultSearchProvider();
373 // Fail if we can't set the clickthrough URL for query suggestions. 362 // Fail if we can't set the clickthrough URL for query suggestions.
374 if (default_provider == NULL || !default_provider->SupportsReplacement()) 363 if (default_provider == NULL || !default_provider->SupportsReplacement())
375 return; 364 return;
376 365
377 const int num_query_results = query_matches_map_.size(); 366 MatchMap map;
378 const int num_nav_results = navigation_results_.size(); 367 AddSuggestResultsToMap(results_.suggest_results, &map);
368
369 const int num_query_results = map.size();
370 const int num_nav_results = results_.navigation_results.size();
379 const int num_results = num_query_results + num_nav_results; 371 const int num_results = num_query_results + num_nav_results;
380 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results); 372 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
381 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results); 373 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
382 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results); 374 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
383 375
384 // Show Most Visited results after ZeroSuggest response is received. 376 // Show Most Visited results after ZeroSuggest response is received.
385 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { 377 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
386 if (!current_url_match_.destination_url.is_valid()) 378 if (!current_url_match_.destination_url.is_valid())
387 return; 379 return;
388 matches_.push_back(current_url_match_); 380 matches_.push_back(current_url_match_);
(...skipping 17 matching lines...) Expand all
406 return; 398 return;
407 } 399 }
408 400
409 if (num_results == 0) 401 if (num_results == 0)
410 return; 402 return;
411 403
412 // TODO(jered): Rip this out once the first match is decoupled from the 404 // TODO(jered): Rip this out once the first match is decoupled from the
413 // current typing in the omnibox. 405 // current typing in the omnibox.
414 matches_.push_back(current_url_match_); 406 matches_.push_back(current_url_match_);
415 407
416 for (MatchMap::const_iterator it(query_matches_map_.begin()); 408 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
417 it != query_matches_map_.end(); ++it)
418 matches_.push_back(it->second); 409 matches_.push_back(it->second);
419 410
420 for (NavigationResults::const_iterator it(navigation_results_.begin()); 411 const NavigationResults& nav_results(results_.navigation_results);
421 it != navigation_results_.end(); ++it) 412 for (NavigationResults::const_iterator it(nav_results.begin());
413 it != nav_results.end(); ++it)
422 matches_.push_back(NavigationToMatch(*it)); 414 matches_.push_back(NavigationToMatch(*it));
423 } 415 }
424 416
425 AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() { 417 AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
426 AutocompleteInput input(permanent_text_, base::string16::npos, base::string16( ), 418 AutocompleteInput input(permanent_text_, base::string16::npos, base::string16( ),
427 GURL(current_query_), current_page_classification_, 419 GURL(current_query_), current_page_classification_,
428 false, false, true, AutocompleteInput::ALL_MATCHES); 420 false, false, true, AutocompleteInput::ALL_MATCHES);
429 421
430 AutocompleteMatch match; 422 AutocompleteMatch match;
431 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify( 423 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(
432 permanent_text_, false, true, current_page_classification_, &match, NULL); 424 permanent_text_, false, true, current_page_classification_, &match, NULL);
433 match.is_history_what_you_typed_match = false; 425 match.is_history_what_you_typed_match = false;
434 match.allowed_to_be_default_match = true; 426 match.allowed_to_be_default_match = true;
435 427
436 // The placeholder suggestion for the current URL has high relevance so 428 // The placeholder suggestion for the current URL has high relevance so
437 // that it is in the first suggestion slot and inline autocompleted. It 429 // that it is in the first suggestion slot and inline autocompleted. It
438 // gets dropped as soon as the user types something. 430 // gets dropped as soon as the user types something.
439 match.relevance = verbatim_relevance_; 431 match.relevance = GetVerbatimRelevance();
440 432
441 return match; 433 return match;
442 } 434 }
435
436 int ZeroSuggestProvider::GetVerbatimRelevance() const {
437 return results_.verbatim_relevance >= 0 ?
438 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance;
439 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698