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

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

Issue 158053002: Part 4 of search provider refactoring. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove static comment 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/base_search_provider.h" 5 #include "chrome/browser/autocomplete/base_search_provider.h"
6 6
7 #include "base/i18n/case_conversion.h"
7 #include "base/json/json_string_value_serializer.h" 8 #include "base/json/json_string_value_serializer.h"
8 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" 12 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
12 #include "chrome/browser/autocomplete/url_prefix.h" 13 #include "chrome/browser/autocomplete/url_prefix.h"
13 #include "chrome/browser/omnibox/omnibox_field_trial.h" 14 #include "chrome/browser/omnibox/omnibox_field_trial.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search/instant_service.h"
17 #include "chrome/browser/search/instant_service_factory.h"
18 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/search_engines/template_url.h" 19 #include "chrome/browser/search_engines/template_url.h"
16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" 20 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
17 #include "chrome/browser/sync/profile_sync_service.h" 21 #include "chrome/browser/sync/profile_sync_service.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h" 22 #include "chrome/browser/sync/profile_sync_service_factory.h"
19 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
20 #include "content/public/common/url_constants.h" 24 #include "content/public/common/url_constants.h"
21 #include "net/base/escape.h" 25 #include "net/base/escape.h"
22 #include "net/base/net_util.h" 26 #include "net/base/net_util.h"
23 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 27 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
24 #include "net/url_request/url_fetcher_delegate.h" 28 #include "net/url_request/url_fetcher_delegate.h"
25 #include "url/gurl.h" 29 #include "url/gurl.h"
26 30
27 // BaseSearchProvider --------------------------------------------------------- 31 // BaseSearchProvider ---------------------------------------------------------
28 32
29 BaseSearchProvider::BaseSearchProvider(AutocompleteProviderListener* listener, 33 BaseSearchProvider::BaseSearchProvider(AutocompleteProviderListener* listener,
30 Profile* profile, 34 Profile* profile,
31 AutocompleteProvider::Type type) 35 AutocompleteProvider::Type type)
32 : AutocompleteProvider(listener, profile, type), 36 : AutocompleteProvider(listener, profile, type),
33 field_trial_triggered_(false), 37 field_trial_triggered_(false),
34 field_trial_triggered_in_session_(false) {} 38 field_trial_triggered_in_session_(false) {}
35 39
40 // static
41 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) {
42 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue;
43 }
44
36 void BaseSearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const { 45 void BaseSearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
37 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); 46 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo());
38 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); 47 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back();
39 new_entry.set_provider(AsOmniboxEventProviderType()); 48 new_entry.set_provider(AsOmniboxEventProviderType());
40 new_entry.set_provider_done(done_); 49 new_entry.set_provider_done(done_);
41 std::vector<uint32> field_trial_hashes; 50 std::vector<uint32> field_trial_hashes;
42 OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes); 51 OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes);
43 for (size_t i = 0; i < field_trial_hashes.size(); ++i) { 52 for (size_t i = 0; i < field_trial_hashes.size(); ++i) {
44 if (field_trial_triggered_) 53 if (field_trial_triggered_)
45 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); 54 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]);
46 if (field_trial_triggered_in_session_) { 55 if (field_trial_triggered_in_session_) {
47 new_entry.mutable_field_trial_triggered_in_session()->Add( 56 new_entry.mutable_field_trial_triggered_in_session()->Add(
48 field_trial_hashes[i]); 57 field_trial_hashes[i]);
49 } 58 }
50 } 59 }
51 } 60 }
52 61
62 // static
63 const char BaseSearchProvider::kRelevanceFromServerKey[] =
64 "relevance_from_server";
65 const char BaseSearchProvider::kShouldPrefetchKey[] = "should_prefetch";
66 const char BaseSearchProvider::kSuggestMetadataKey[] = "suggest_metadata";
67 const char BaseSearchProvider::kDeletionUrlKey[] = "deletion_url";
68 const char BaseSearchProvider::kTrue[] = "true";
69 const char BaseSearchProvider::kFalse[] = "false";
70
53 BaseSearchProvider::~BaseSearchProvider() {} 71 BaseSearchProvider::~BaseSearchProvider() {}
54 72
55 // BaseSearchProvider::Result -------------------------------------------------- 73 // BaseSearchProvider::Result --------------------------------------------------
56 74
57 BaseSearchProvider::Result::Result(bool from_keyword_provider, 75 BaseSearchProvider::Result::Result(bool from_keyword_provider,
58 int relevance, 76 int relevance,
59 bool relevance_from_server) 77 bool relevance_from_server)
60 : from_keyword_provider_(from_keyword_provider), 78 : from_keyword_provider_(from_keyword_provider),
61 relevance_(relevance), 79 relevance_(relevance),
62 relevance_from_server_(relevance_from_server) {} 80 relevance_from_server_(relevance_from_server) {}
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 269
252 return false; 270 return false;
253 } 271 }
254 272
255 // BaseSearchProvider --------------------------------------------------------- 273 // BaseSearchProvider ---------------------------------------------------------
256 274
257 // static 275 // static
258 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( 276 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion(
259 AutocompleteProvider* autocomplete_provider, 277 AutocompleteProvider* autocomplete_provider,
260 const AutocompleteInput& input, 278 const AutocompleteInput& input,
261 const base::string16& input_text,
262 const SuggestResult& suggestion, 279 const SuggestResult& suggestion,
263 const TemplateURL* template_url, 280 const TemplateURL* template_url,
264 int accepted_suggestion, 281 int accepted_suggestion,
265 int omnibox_start_margin, 282 int omnibox_start_margin,
266 bool append_extra_query_params) { 283 bool append_extra_query_params) {
267 AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false, 284 AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false,
268 suggestion.type()); 285 suggestion.type());
269 286
270 if (!template_url) 287 if (!template_url)
271 return match; 288 return match;
272 match.keyword = template_url->keyword(); 289 match.keyword = template_url->keyword();
273 match.contents = suggestion.match_contents(); 290 match.contents = suggestion.match_contents();
274 match.contents_class = suggestion.match_contents_class(); 291 match.contents_class = suggestion.match_contents_class();
275 292
276 if (!suggestion.annotation().empty()) 293 if (!suggestion.annotation().empty())
277 match.description = suggestion.annotation(); 294 match.description = suggestion.annotation();
278 295
279 match.allowed_to_be_default_match = 296 match.allowed_to_be_default_match =
280 (input_text == suggestion.match_contents()); 297 (input.text() == suggestion.match_contents());
281 298
282 // When the user forced a query, we need to make sure all the fill_into_edit 299 // When the user forced a query, we need to make sure all the fill_into_edit
283 // values preserve that property. Otherwise, if the user starts editing a 300 // values preserve that property. Otherwise, if the user starts editing a
284 // suggestion, non-Search results will suddenly appear. 301 // suggestion, non-Search results will suddenly appear.
285 if (input.type() == AutocompleteInput::FORCED_QUERY) 302 if (input.type() == AutocompleteInput::FORCED_QUERY)
286 match.fill_into_edit.assign(base::ASCIIToUTF16("?")); 303 match.fill_into_edit.assign(base::ASCIIToUTF16("?"));
287 if (suggestion.from_keyword_provider()) 304 if (suggestion.from_keyword_provider())
288 match.fill_into_edit.append(match.keyword + base::char16(' ')); 305 match.fill_into_edit.append(match.keyword + base::char16(' '));
289 if (!input.prevent_inline_autocomplete() && 306 if (!input.prevent_inline_autocomplete() &&
290 StartsWith(suggestion.suggestion(), input_text, false)) { 307 StartsWith(suggestion.suggestion(), input.text(), false)) {
291 match.inline_autocompletion = 308 match.inline_autocompletion =
292 suggestion.suggestion().substr(input_text.length()); 309 suggestion.suggestion().substr(input.text().length());
293 match.allowed_to_be_default_match = true; 310 match.allowed_to_be_default_match = true;
294 } 311 }
295 match.fill_into_edit.append(suggestion.suggestion()); 312 match.fill_into_edit.append(suggestion.suggestion());
296 313
297 const TemplateURLRef& search_url = template_url->url_ref(); 314 const TemplateURLRef& search_url = template_url->url_ref();
298 DCHECK(search_url.SupportsReplacement()); 315 DCHECK(search_url.SupportsReplacement());
299 match.search_terms_args.reset( 316 match.search_terms_args.reset(
300 new TemplateURLRef::SearchTermsArgs(suggestion.suggestion())); 317 new TemplateURLRef::SearchTermsArgs(suggestion.suggestion()));
301 match.search_terms_args->original_query = input_text; 318 match.search_terms_args->original_query = input.text();
302 match.search_terms_args->accepted_suggestion = accepted_suggestion; 319 match.search_terms_args->accepted_suggestion = accepted_suggestion;
303 match.search_terms_args->omnibox_start_margin = omnibox_start_margin; 320 match.search_terms_args->omnibox_start_margin = omnibox_start_margin;
304 match.search_terms_args->suggest_query_params = 321 match.search_terms_args->suggest_query_params =
305 suggestion.suggest_query_params(); 322 suggestion.suggest_query_params();
306 match.search_terms_args->append_extra_query_params = 323 match.search_terms_args->append_extra_query_params =
307 append_extra_query_params; 324 append_extra_query_params;
308 // This is the destination URL sans assisted query stats. This must be set 325 // This is the destination URL sans assisted query stats. This must be set
309 // so the AutocompleteController can properly de-dupe; the controller will 326 // so the AutocompleteController can properly de-dupe; the controller will
310 // eventually overwrite it before it reaches the user. 327 // eventually overwrite it before it reaches the user.
311 match.destination_url = 328 match.destination_url =
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 service == NULL || 413 service == NULL ||
397 !service->IsSyncEnabledAndLoggedIn() || 414 !service->IsSyncEnabledAndLoggedIn() ||
398 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( 415 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has(
399 syncer::PROXY_TABS) || 416 syncer::PROXY_TABS) ||
400 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) 417 service->GetEncryptedDataTypes().Has(syncer::SESSIONS))
401 return false; 418 return false;
402 419
403 return true; 420 return true;
404 } 421 }
405 422
423 void BaseSearchProvider::AddMatchToMap(const SuggestResult& result,
424 const AutocompleteInput& input,
425 const TemplateURL* template_url,
426 const std::string& metadata,
427 int accepted_suggestion,
428 MatchMap* map) {
429 InstantService* instant_service =
430 InstantServiceFactory::GetForProfile(profile_);
431 // Android and iOS have on InstantService
432 const int omnibox_start_margin = instant_service ?
433 instant_service->omnibox_start_margin() : chrome::kDisableStartMargin;
434
435 AutocompleteMatch match = CreateSearchSuggestion(
436 this, input, result, template_url, accepted_suggestion,
437 omnibox_start_margin, ShouldAppendExtraParams(result));
438 if (!match.destination_url.is_valid())
439 return;
440 match.search_terms_args->bookmark_bar_pinned =
441 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
442 match.RecordAdditionalInfo(kRelevanceFromServerKey,
443 result.relevance_from_server() ? kTrue : kFalse);
444 match.RecordAdditionalInfo(kShouldPrefetchKey,
445 result.should_prefetch() ? kTrue : kFalse);
446
447 if (!result.deletion_url().empty()) {
448 GURL url(match.destination_url.GetOrigin().Resolve(result.deletion_url()));
449 if (url.is_valid()) {
450 match.RecordAdditionalInfo(kDeletionUrlKey, url.spec());
451 match.deletable = true;
452 }
453 }
454
455 // Metadata is needed only for prefetching queries.
456 if (result.should_prefetch())
457 match.RecordAdditionalInfo(kSuggestMetadataKey, metadata);
458
459 // Try to add |match| to |map|. If a match for |query_string| is already in
Mark P 2014/02/12 23:35:49 query_string -> result.suggestion()
Maria 2014/02/13 21:07:36 Done.
460 // |map|, replace it if |match| is more relevant.
461 // NOTE: Keep this ToLower() call in sync with url_database.cc.
462 MatchKey match_key(
463 std::make_pair(base::i18n::ToLower(result.suggestion()),
464 match.search_terms_args->suggest_query_params));
465 const std::pair<MatchMap::iterator, bool> i(
466 map->insert(std::make_pair(match_key, match)));
467
468 bool should_prefetch = result.should_prefetch();
469 if (!i.second) {
470 // NOTE: We purposefully do a direct relevance comparison here instead of
471 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items
472 // added first" rather than "items alphabetically first" when the scores
473 // are equal. The only case this matters is when a user has results with
474 // the same score that differ only by capitalization; because the history
475 // system returns results sorted by recency, this means we'll pick the most
476 // recent such result even if the precision of our relevance score is too
477 // low to distinguish the two.
478 if (match.relevance > i.first->second.relevance) {
479 i.first->second = match;
480 } else if (match.keyword == i.first->second.keyword) {
481 // Old and new matches are from the same search provider. It is okay to
482 // record one match's prefetch data onto a different match (for the same
483 // query string) for the following reasons:
484 // 1. Because the suggest server only sends down a query string from
485 // which we construct a URL, rather than sending a full URL, and because
486 // we construct URLs from query strings in the same way every time, the
487 // URLs for the two matches will be the same. Therefore, we won't end up
488 // prefetching something the server didn't intend.
489 // 2. Presumably the server sets the prefetch bit on a match it things is
490 // sufficiently relevant that the user is likely to choose it. Surely
491 // setting the prefetch bit on a match of even higher relevance won't
492 // violate this assumption.
493 should_prefetch |= ShouldPrefetch(i.first->second);
494 i.first->second.RecordAdditionalInfo(kShouldPrefetchKey,
495 should_prefetch ? kTrue : kFalse);
496 if (should_prefetch)
497 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata);
498 }
499 }
500 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698