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/i18n/case_conversion.h" |
8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 AutocompleteProvider::Type type) | 35 AutocompleteProvider::Type type) |
36 : AutocompleteProvider(listener, profile, type), | 36 : AutocompleteProvider(listener, profile, type), |
37 field_trial_triggered_(false), | 37 field_trial_triggered_(false), |
38 field_trial_triggered_in_session_(false) {} | 38 field_trial_triggered_in_session_(false) {} |
39 | 39 |
40 // static | 40 // static |
41 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { | 41 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { |
42 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; | 42 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; |
43 } | 43 } |
44 | 44 |
| 45 void BaseSearchProvider::Stop(bool clear_cached_results) { |
| 46 StopSuggest(); |
| 47 done_ = true; |
| 48 |
| 49 if (clear_cached_results) |
| 50 ClearAllResults(); |
| 51 } |
| 52 |
45 void BaseSearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const { | 53 void BaseSearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
46 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); | 54 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); |
47 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); | 55 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); |
48 new_entry.set_provider(AsOmniboxEventProviderType()); | 56 new_entry.set_provider(AsOmniboxEventProviderType()); |
49 new_entry.set_provider_done(done_); | 57 new_entry.set_provider_done(done_); |
50 std::vector<uint32> field_trial_hashes; | 58 std::vector<uint32> field_trial_hashes; |
51 OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes); | 59 OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes); |
52 for (size_t i = 0; i < field_trial_hashes.size(); ++i) { | 60 for (size_t i = 0; i < field_trial_hashes.size(); ++i) { |
53 if (field_trial_triggered_) | 61 if (field_trial_triggered_) |
54 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); | 62 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 // already in |map|, replace it if |match| is more relevant. | 467 // already in |map|, replace it if |match| is more relevant. |
460 // NOTE: Keep this ToLower() call in sync with url_database.cc. | 468 // NOTE: Keep this ToLower() call in sync with url_database.cc. |
461 MatchKey match_key( | 469 MatchKey match_key( |
462 std::make_pair(base::i18n::ToLower(result.suggestion()), | 470 std::make_pair(base::i18n::ToLower(result.suggestion()), |
463 match.search_terms_args->suggest_query_params)); | 471 match.search_terms_args->suggest_query_params)); |
464 const std::pair<MatchMap::iterator, bool> i( | 472 const std::pair<MatchMap::iterator, bool> i( |
465 map->insert(std::make_pair(match_key, match))); | 473 map->insert(std::make_pair(match_key, match))); |
466 | 474 |
467 bool should_prefetch = result.should_prefetch(); | 475 bool should_prefetch = result.should_prefetch(); |
468 if (!i.second) { | 476 if (!i.second) { |
469 // NOTE: We purposefully do a direct relevance comparison here instead of | 477 // NOTE: We purposefully do a direct relevance comparison here instead of |
470 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items | 478 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items |
471 // added first" rather than "items alphabetically first" when the scores | 479 // added first" rather than "items alphabetically first" when the scores |
472 // are equal. The only case this matters is when a user has results with | 480 // are equal. The only case this matters is when a user has results with |
473 // the same score that differ only by capitalization; because the history | 481 // the same score that differ only by capitalization; because the history |
474 // system returns results sorted by recency, this means we'll pick the most | 482 // system returns results sorted by recency, this means we'll pick the most |
475 // recent such result even if the precision of our relevance score is too | 483 // recent such result even if the precision of our relevance score is too |
476 // low to distinguish the two. | 484 // low to distinguish the two. |
477 if (match.relevance > i.first->second.relevance) { | 485 if (match.relevance > i.first->second.relevance) { |
478 i.first->second = match; | 486 i.first->second = match; |
479 } else if (match.keyword == i.first->second.keyword) { | 487 } else if (match.keyword == i.first->second.keyword) { |
480 // Old and new matches are from the same search provider. It is okay to | 488 // Old and new matches are from the same search provider. It is okay to |
481 // record one match's prefetch data onto a different match (for the same | 489 // record one match's prefetch data onto a different match (for the same |
482 // query string) for the following reasons: | 490 // query string) for the following reasons: |
483 // 1. Because the suggest server only sends down a query string from | 491 // 1. Because the suggest server only sends down a query string from |
484 // which we construct a URL, rather than sending a full URL, and because | 492 // which we construct a URL, rather than sending a full URL, and because |
485 // we construct URLs from query strings in the same way every time, the | 493 // we construct URLs from query strings in the same way every time, the |
486 // URLs for the two matches will be the same. Therefore, we won't end up | 494 // URLs for the two matches will be the same. Therefore, we won't end up |
487 // prefetching something the server didn't intend. | 495 // prefetching something the server didn't intend. |
488 // 2. Presumably the server sets the prefetch bit on a match it things is | 496 // 2. Presumably the server sets the prefetch bit on a match it things is |
489 // sufficiently relevant that the user is likely to choose it. Surely | 497 // sufficiently relevant that the user is likely to choose it. Surely |
490 // setting the prefetch bit on a match of even higher relevance won't | 498 // setting the prefetch bit on a match of even higher relevance won't |
491 // violate this assumption. | 499 // violate this assumption. |
492 should_prefetch |= ShouldPrefetch(i.first->second); | 500 should_prefetch |= ShouldPrefetch(i.first->second); |
493 i.first->second.RecordAdditionalInfo(kShouldPrefetchKey, | 501 i.first->second.RecordAdditionalInfo(kShouldPrefetchKey, |
494 should_prefetch ? kTrue : kFalse); | 502 should_prefetch ? kTrue : kFalse); |
495 if (should_prefetch) | 503 if (should_prefetch) |
496 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); | 504 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); |
497 } | 505 } |
498 } | 506 } |
499 } | 507 } |
OLD | NEW |