Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 | |
|
H Fung
2014/02/12 03:17:49
I'm not sure if you need comments here since they
Maria
2014/02/12 19:00:02
I added all those based on Mark's comment on the p
| |
| 63 const char BaseSearchProvider::kRelevanceFromServerKey[] = | |
| 64 "relevance_from_server"; | |
| 65 // static | |
| 66 const char BaseSearchProvider::kShouldPrefetchKey[] = "should_prefetch"; | |
| 67 // static | |
| 68 const char BaseSearchProvider::kSuggestMetadataKey[] = "suggest_metadata"; | |
| 69 // static | |
| 70 const char BaseSearchProvider::kDeletionUrlKey[] = "deletion_url"; | |
| 71 // static | |
| 72 const char BaseSearchProvider::kTrue[] = "true"; | |
| 73 // static | |
| 74 const char BaseSearchProvider::kFalse[] = "false"; | |
| 75 | |
| 53 BaseSearchProvider::~BaseSearchProvider() {} | 76 BaseSearchProvider::~BaseSearchProvider() {} |
| 54 | 77 |
| 55 // BaseSearchProvider::Result -------------------------------------------------- | 78 // BaseSearchProvider::Result -------------------------------------------------- |
| 56 | 79 |
| 57 BaseSearchProvider::Result::Result(bool from_keyword_provider, | 80 BaseSearchProvider::Result::Result(bool from_keyword_provider, |
| 58 int relevance, | 81 int relevance, |
| 59 bool relevance_from_server) | 82 bool relevance_from_server) |
| 60 : from_keyword_provider_(from_keyword_provider), | 83 : from_keyword_provider_(from_keyword_provider), |
| 61 relevance_(relevance), | 84 relevance_(relevance), |
| 62 relevance_from_server_(relevance_from_server) {} | 85 relevance_from_server_(relevance_from_server) {} |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 | 274 |
| 252 return false; | 275 return false; |
| 253 } | 276 } |
| 254 | 277 |
| 255 // BaseSearchProvider --------------------------------------------------------- | 278 // BaseSearchProvider --------------------------------------------------------- |
| 256 | 279 |
| 257 // static | 280 // static |
| 258 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( | 281 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( |
| 259 AutocompleteProvider* autocomplete_provider, | 282 AutocompleteProvider* autocomplete_provider, |
| 260 const AutocompleteInput& input, | 283 const AutocompleteInput& input, |
| 261 const base::string16& input_text, | |
| 262 const SuggestResult& suggestion, | 284 const SuggestResult& suggestion, |
| 263 const TemplateURL* template_url, | 285 const TemplateURL* template_url, |
| 264 int accepted_suggestion, | 286 int accepted_suggestion, |
| 265 int omnibox_start_margin, | 287 int omnibox_start_margin, |
| 266 bool append_extra_query_params) { | 288 bool append_extra_query_params) { |
| 267 AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false, | 289 AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false, |
| 268 suggestion.type()); | 290 suggestion.type()); |
| 269 | 291 |
| 270 if (!template_url) | 292 if (!template_url) |
| 271 return match; | 293 return match; |
| 272 match.keyword = template_url->keyword(); | 294 match.keyword = template_url->keyword(); |
| 273 match.contents = suggestion.match_contents(); | 295 match.contents = suggestion.match_contents(); |
| 274 match.contents_class = suggestion.match_contents_class(); | 296 match.contents_class = suggestion.match_contents_class(); |
| 275 | 297 |
| 276 if (!suggestion.annotation().empty()) | 298 if (!suggestion.annotation().empty()) |
| 277 match.description = suggestion.annotation(); | 299 match.description = suggestion.annotation(); |
| 278 | 300 |
| 279 match.allowed_to_be_default_match = | 301 match.allowed_to_be_default_match = |
| 280 (input_text == suggestion.match_contents()); | 302 (input.text() == suggestion.match_contents()); |
| 281 | 303 |
| 282 // When the user forced a query, we need to make sure all the fill_into_edit | 304 // 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 | 305 // values preserve that property. Otherwise, if the user starts editing a |
| 284 // suggestion, non-Search results will suddenly appear. | 306 // suggestion, non-Search results will suddenly appear. |
| 285 if (input.type() == AutocompleteInput::FORCED_QUERY) | 307 if (input.type() == AutocompleteInput::FORCED_QUERY) |
| 286 match.fill_into_edit.assign(base::ASCIIToUTF16("?")); | 308 match.fill_into_edit.assign(base::ASCIIToUTF16("?")); |
| 287 if (suggestion.from_keyword_provider()) | 309 if (suggestion.from_keyword_provider()) |
| 288 match.fill_into_edit.append(match.keyword + base::char16(' ')); | 310 match.fill_into_edit.append(match.keyword + base::char16(' ')); |
| 289 if (!input.prevent_inline_autocomplete() && | 311 if (!input.prevent_inline_autocomplete() && |
| 290 StartsWith(suggestion.suggestion(), input_text, false)) { | 312 StartsWith(suggestion.suggestion(), input.text(), false)) { |
| 291 match.inline_autocompletion = | 313 match.inline_autocompletion = |
| 292 suggestion.suggestion().substr(input_text.length()); | 314 suggestion.suggestion().substr(input.text().length()); |
| 293 match.allowed_to_be_default_match = true; | 315 match.allowed_to_be_default_match = true; |
| 294 } | 316 } |
| 295 match.fill_into_edit.append(suggestion.suggestion()); | 317 match.fill_into_edit.append(suggestion.suggestion()); |
| 296 | 318 |
| 297 const TemplateURLRef& search_url = template_url->url_ref(); | 319 const TemplateURLRef& search_url = template_url->url_ref(); |
| 298 DCHECK(search_url.SupportsReplacement()); | 320 DCHECK(search_url.SupportsReplacement()); |
| 299 match.search_terms_args.reset( | 321 match.search_terms_args.reset( |
| 300 new TemplateURLRef::SearchTermsArgs(suggestion.suggestion())); | 322 new TemplateURLRef::SearchTermsArgs(suggestion.suggestion())); |
| 301 match.search_terms_args->original_query = input_text; | 323 match.search_terms_args->original_query = input.text(); |
| 302 match.search_terms_args->accepted_suggestion = accepted_suggestion; | 324 match.search_terms_args->accepted_suggestion = accepted_suggestion; |
| 303 match.search_terms_args->omnibox_start_margin = omnibox_start_margin; | 325 match.search_terms_args->omnibox_start_margin = omnibox_start_margin; |
| 304 match.search_terms_args->suggest_query_params = | 326 match.search_terms_args->suggest_query_params = |
| 305 suggestion.suggest_query_params(); | 327 suggestion.suggest_query_params(); |
| 306 match.search_terms_args->append_extra_query_params = | 328 match.search_terms_args->append_extra_query_params = |
| 307 append_extra_query_params; | 329 append_extra_query_params; |
| 308 // This is the destination URL sans assisted query stats. This must be set | 330 // This is the destination URL sans assisted query stats. This must be set |
| 309 // so the AutocompleteController can properly de-dupe; the controller will | 331 // so the AutocompleteController can properly de-dupe; the controller will |
| 310 // eventually overwrite it before it reaches the user. | 332 // eventually overwrite it before it reaches the user. |
| 311 match.destination_url = | 333 match.destination_url = |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 396 service == NULL || | 418 service == NULL || |
| 397 !service->IsSyncEnabledAndLoggedIn() || | 419 !service->IsSyncEnabledAndLoggedIn() || |
| 398 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( | 420 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( |
| 399 syncer::PROXY_TABS) || | 421 syncer::PROXY_TABS) || |
| 400 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) | 422 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) |
| 401 return false; | 423 return false; |
| 402 | 424 |
| 403 return true; | 425 return true; |
| 404 } | 426 } |
| 405 | 427 |
| 428 void BaseSearchProvider::AddMatchToMap(const SuggestResult& result, | |
| 429 const AutocompleteInput& input, | |
| 430 const TemplateURL* template_url, | |
| 431 const std::string& metadata, | |
| 432 int accepted_suggestion, | |
| 433 MatchMap* map) { | |
| 434 InstantService* instant_service = | |
| 435 InstantServiceFactory::GetForProfile(profile_); | |
| 436 // Android and iOS have on InstantService | |
| 437 const int omnibox_start_margin = instant_service ? | |
| 438 instant_service->omnibox_start_margin() : chrome::kDisableStartMargin; | |
| 439 | |
| 440 AutocompleteMatch match = CreateSearchSuggestion( | |
| 441 this, input, result, template_url, accepted_suggestion, | |
| 442 omnibox_start_margin, ShouldAppendExtraParams(result)); | |
|
H Fung
2014/02/12 03:17:49
I'm not sure if you need to define the extra Shoul
Maria
2014/02/12 19:00:02
The reason I need to is the definition in search_p
| |
| 443 if (!match.destination_url.is_valid()) | |
| 444 return; | |
| 445 match.search_terms_args->bookmark_bar_pinned = | |
| 446 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); | |
| 447 match.RecordAdditionalInfo(kRelevanceFromServerKey, | |
| 448 result.relevance_from_server() ? kTrue : kFalse); | |
| 449 match.RecordAdditionalInfo(kShouldPrefetchKey, | |
| 450 result.should_prefetch() ? kTrue : kFalse); | |
| 451 | |
| 452 if (!result.deletion_url().empty()) { | |
| 453 GURL url(match.destination_url.GetOrigin().Resolve(result.deletion_url())); | |
| 454 if (url.is_valid()) { | |
| 455 match.RecordAdditionalInfo(kDeletionUrlKey, url.spec()); | |
| 456 match.deletable = true; | |
| 457 } | |
| 458 } | |
| 459 | |
| 460 // Metadata is needed only for prefetching queries. | |
| 461 if (result.should_prefetch()) | |
| 462 match.RecordAdditionalInfo(kSuggestMetadataKey, metadata); | |
| 463 | |
| 464 // Try to add |match| to |map|. If a match for |query_string| is already in | |
| 465 // |map|, replace it if |match| is more relevant. | |
| 466 // NOTE: Keep this ToLower() call in sync with url_database.cc. | |
| 467 MatchKey match_key( | |
| 468 std::make_pair(base::i18n::ToLower(result.suggestion()), | |
| 469 match.search_terms_args->suggest_query_params)); | |
| 470 const std::pair<MatchMap::iterator, bool> i( | |
| 471 map->insert(std::make_pair(match_key, match))); | |
| 472 | |
| 473 bool should_prefetch = result.should_prefetch(); | |
| 474 if (!i.second) { | |
| 475 // NOTE: We purposefully do a direct relevance comparison here instead of | |
| 476 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items | |
| 477 // added first" rather than "items alphabetically first" when the scores | |
| 478 // are equal. The only case this matters is when a user has results with | |
| 479 // the same score that differ only by capitalization; because the history | |
| 480 // system returns results sorted by recency, this means we'll pick the most | |
| 481 // recent such result even if the precision of our relevance score is too | |
| 482 // low to distinguish the two. | |
| 483 if (match.relevance > i.first->second.relevance) { | |
| 484 i.first->second = match; | |
| 485 } else if (match.keyword == i.first->second.keyword) { | |
| 486 // Old and new matches are from the same search provider. It is okay to | |
| 487 // record one match's prefetch data onto a different match (for the same | |
| 488 // query string) for the following reasons: | |
| 489 // 1. Because the suggest server only sends down a query string from | |
| 490 // which we construct a URL, rather than sending a full URL, and because | |
| 491 // we construct URLs from query strings in the same way every time, the | |
| 492 // URLs for the two matches will be the same. Therefore, we won't end up | |
| 493 // prefetching something the server didn't intend. | |
| 494 // 2. Presumably the server sets the prefetch bit on a match it things is | |
| 495 // sufficiently relevant that the user is likely to choose it. Surely | |
| 496 // setting the prefetch bit on a match of even higher relevance won't | |
| 497 // violate this assumption. | |
| 498 should_prefetch |= ShouldPrefetch(i.first->second); | |
| 499 i.first->second.RecordAdditionalInfo(kShouldPrefetchKey, | |
| 500 should_prefetch ? kTrue : kFalse); | |
| 501 if (should_prefetch) | |
| 502 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); | |
| 503 } | |
| 504 } | |
| 505 } | |
| OLD | NEW |