| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/i18n/break_iterator.h" | 11 #include "base/i18n/break_iterator.h" |
| 12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
| 13 #include "base/i18n/icu_string_conversions.h" | 13 #include "base/i18n/icu_string_conversions.h" |
| 14 #include "base/json/json_string_value_serializer.h" | 14 #include "base/json/json_string_value_serializer.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
| 18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 21 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 21 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
| 22 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 22 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 23 #include "chrome/browser/autocomplete/autocomplete_match.h" | 23 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 24 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 24 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 25 #include "chrome/browser/autocomplete/autocomplete_result.h" | 25 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 26 #include "chrome/browser/autocomplete/keyword_provider.h" | 26 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 27 #include "chrome/browser/autocomplete/url_prefix.h" | 27 #include "chrome/browser/autocomplete/url_prefix.h" |
| 28 #include "chrome/browser/google/google_util.h" |
| 28 #include "chrome/browser/history/history_service.h" | 29 #include "chrome/browser/history/history_service.h" |
| 29 #include "chrome/browser/history/history_service_factory.h" | 30 #include "chrome/browser/history/history_service_factory.h" |
| 30 #include "chrome/browser/history/in_memory_database.h" | 31 #include "chrome/browser/history/in_memory_database.h" |
| 31 #include "chrome/browser/metrics/variations/variations_http_header_provider.h" | 32 #include "chrome/browser/metrics/variations/variations_http_header_provider.h" |
| 32 #include "chrome/browser/omnibox/omnibox_field_trial.h" | 33 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
| 33 #include "chrome/browser/profiles/profile.h" | 34 #include "chrome/browser/profiles/profile.h" |
| 34 #include "chrome/browser/search/search.h" | 35 #include "chrome/browser/search/search.h" |
| 35 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 36 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 36 #include "chrome/browser/search_engines/template_url_service.h" | 37 #include "chrome/browser/search_engines/template_url_service.h" |
| 37 #include "chrome/browser/search_engines/template_url_service_factory.h" | 38 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 39 #include "chrome/browser/sync/profile_sync_service.h" |
| 40 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 38 #include "chrome/browser/ui/browser.h" | 41 #include "chrome/browser/ui/browser.h" |
| 39 #include "chrome/browser/ui/browser_finder.h" | 42 #include "chrome/browser/ui/browser_finder.h" |
| 40 #include "chrome/browser/ui/browser_instant_controller.h" | 43 #include "chrome/browser/ui/browser_instant_controller.h" |
| 41 #include "chrome/browser/ui/search/instant_controller.h" | 44 #include "chrome/browser/ui/search/instant_controller.h" |
| 42 #include "chrome/common/net/url_fixer_upper.h" | 45 #include "chrome/common/net/url_fixer_upper.h" |
| 43 #include "chrome/common/pref_names.h" | 46 #include "chrome/common/pref_names.h" |
| 44 #include "chrome/common/url_constants.h" | 47 #include "chrome/common/url_constants.h" |
| 45 #include "grit/generated_resources.h" | 48 #include "grit/generated_resources.h" |
| 46 #include "net/base/escape.h" | 49 #include "net/base/escape.h" |
| 47 #include "net/base/load_flags.h" | 50 #include "net/base/load_flags.h" |
| 48 #include "net/base/net_util.h" | 51 #include "net/base/net_util.h" |
| 52 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 49 #include "net/http/http_request_headers.h" | 53 #include "net/http/http_request_headers.h" |
| 50 #include "net/http/http_response_headers.h" | 54 #include "net/http/http_response_headers.h" |
| 51 #include "net/url_request/url_fetcher.h" | 55 #include "net/url_request/url_fetcher.h" |
| 52 #include "net/url_request/url_request_status.h" | 56 #include "net/url_request/url_request_status.h" |
| 53 #include "ui/base/l10n/l10n_util.h" | 57 #include "ui/base/l10n/l10n_util.h" |
| 54 #include "url/url_util.h" | 58 #include "url/url_util.h" |
| 55 | 59 |
| 56 | 60 |
| 57 // Helpers -------------------------------------------------------------------- | 61 // Helpers -------------------------------------------------------------------- |
| 58 | 62 |
| (...skipping 854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 NavigationResults* list) { | 917 NavigationResults* list) { |
| 914 for (size_t i = 0; i < list->size(); ++i) { | 918 for (size_t i = 0; i < list->size(); ++i) { |
| 915 NavigationResult& result = (*list)[i]; | 919 NavigationResult& result = (*list)[i]; |
| 916 result.set_relevance( | 920 result.set_relevance( |
| 917 result.CalculateRelevance(input_, providers_.has_keyword_provider()) + | 921 result.CalculateRelevance(input_, providers_.has_keyword_provider()) + |
| 918 (list->size() - i - 1)); | 922 (list->size() - i - 1)); |
| 919 result.set_relevance_from_server(false); | 923 result.set_relevance_from_server(false); |
| 920 } | 924 } |
| 921 } | 925 } |
| 922 | 926 |
| 927 bool SearchProvider::CanSendURL( |
| 928 const GURL& current_page_url, |
| 929 const GURL& suggest_url, |
| 930 const TemplateURL* template_url, |
| 931 AutocompleteInput::PageClassification page_classification, |
| 932 Profile* profile){ |
| 933 if (!current_page_url.is_valid()) |
| 934 return false; |
| 935 |
| 936 // TODO(hfung): Show Most Visited on NTP with appropriate verbatim |
| 937 // description when the user actively focuses on the omnibox as discussed in |
| 938 // crbug/305366 if Most Visited (or something similar) will launch. |
| 939 // TODO(hfung): Shorten the INSTANT_NEW_TAB_PAGE_WITH_... enums and remove |
| 940 // the const variables. |
| 941 const AutocompleteInput::PageClassification instant_ntp_fakebox = |
| 942 AutocompleteInput::INSTANT_NEW_TAB_PAGE_WITH_FAKEBOX_AS_STARTING_FOCUS; |
| 943 const AutocompleteInput::PageClassification instant_ntp_omnibox = |
| 944 AutocompleteInput::INSTANT_NEW_TAB_PAGE_WITH_OMNIBOX_AS_STARTING_FOCUS; |
| 945 if ((page_classification == instant_ntp_fakebox) || |
| 946 (page_classification == instant_ntp_omnibox)) |
| 947 return false; |
| 948 |
| 949 // Only allow HTTP URLs or HTTPS URLs for the same domain as the search |
| 950 // provider. |
| 951 if ((current_page_url.scheme() != content::kHttpScheme) && |
| 952 ((current_page_url.scheme() != content::kHttpsScheme) || |
| 953 !net::registry_controlled_domains::SameDomainOrHost( |
| 954 current_page_url, suggest_url, |
| 955 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES))) |
| 956 return false; |
| 957 |
| 958 // Make sure we are sending the suggest request through HTTPS to prevent |
| 959 // exposing the current page URL to networks before the search provider. |
| 960 if (!suggest_url.SchemeIs(content::kHttpsScheme)) |
| 961 return false; |
| 962 |
| 963 // Don't run if there's no profile or in incognito mode. |
| 964 if (profile == NULL || profile->IsOffTheRecord()) |
| 965 return false; |
| 966 |
| 967 // Don't run if we can't get preferences or search suggest is not enabled. |
| 968 PrefService* prefs = profile->GetPrefs(); |
| 969 if (!prefs->GetBoolean(prefs::kSearchSuggestEnabled)) |
| 970 return false; |
| 971 |
| 972 // Only make the request if we know that the provider supports zero suggest |
| 973 // (currently only the prepopulated Google provider). |
| 974 if (template_url == NULL || !template_url->SupportsReplacement() || |
| 975 TemplateURLPrepopulateData::GetEngineType(*template_url) != |
| 976 SEARCH_ENGINE_GOOGLE) |
| 977 return false; |
| 978 |
| 979 // Check field trials and settings allow sending the URL on suggest requests. |
| 980 ProfileSyncService* service = |
| 981 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
| 982 browser_sync::SyncPrefs sync_prefs(prefs); |
| 983 if (!OmniboxFieldTrial::InZeroSuggestFieldTrial() || |
| 984 service == NULL || |
| 985 !service->IsSyncEnabledAndLoggedIn() || |
| 986 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( |
| 987 syncer::PROXY_TABS) || |
| 988 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) |
| 989 return false; |
| 990 |
| 991 return true; |
| 992 } |
| 993 |
| 923 net::URLFetcher* SearchProvider::CreateSuggestFetcher( | 994 net::URLFetcher* SearchProvider::CreateSuggestFetcher( |
| 924 int id, | 995 int id, |
| 925 const TemplateURL* template_url, | 996 const TemplateURL* template_url, |
| 926 const AutocompleteInput& input) { | 997 const AutocompleteInput& input) { |
| 927 if (!template_url || template_url->suggestions_url().empty()) | 998 if (!template_url || template_url->suggestions_url().empty()) |
| 928 return NULL; | 999 return NULL; |
| 929 | 1000 |
| 930 // Bail if the suggestion URL is invalid with the given replacements. | 1001 // Bail if the suggestion URL is invalid with the given replacements. |
| 931 TemplateURLRef::SearchTermsArgs search_term_args(input.text()); | 1002 TemplateURLRef::SearchTermsArgs search_term_args(input.text()); |
| 932 search_term_args.cursor_position = input.cursor_position(); | 1003 search_term_args.cursor_position = input.cursor_position(); |
| 933 search_term_args.page_classification = input.current_page_classification(); | 1004 search_term_args.page_classification = input.current_page_classification(); |
| 934 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms( | 1005 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms( |
| 935 search_term_args)); | 1006 search_term_args)); |
| 936 if (!suggest_url.is_valid()) | 1007 if (!suggest_url.is_valid()) |
| 937 return NULL; | 1008 return NULL; |
| 1009 // Send the current page URL if user setting and URL requirements are met and |
| 1010 // the user is in the field trial. |
| 1011 if (CanSendURL(current_page_url_, suggest_url, template_url, |
| 1012 input.current_page_classification(), profile_) && |
| 1013 OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial()) { |
| 1014 search_term_args.current_page_url = current_page_url_.spec(); |
| 1015 // Create the suggest URL again with the current page URL. |
| 1016 suggest_url = GURL(template_url->suggestions_url_ref().ReplaceSearchTerms( |
| 1017 search_term_args)); |
| 1018 } |
| 938 | 1019 |
| 939 suggest_results_pending_++; | 1020 suggest_results_pending_++; |
| 940 LogOmniboxSuggestRequest(REQUEST_SENT); | 1021 LogOmniboxSuggestRequest(REQUEST_SENT); |
| 941 | 1022 |
| 942 net::URLFetcher* fetcher = | 1023 net::URLFetcher* fetcher = |
| 943 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this); | 1024 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this); |
| 944 fetcher->SetRequestContext(profile_->GetRequestContext()); | 1025 fetcher->SetRequestContext(profile_->GetRequestContext()); |
| 945 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 1026 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| 946 // Add Chrome experiment state to the request headers. | 1027 // Add Chrome experiment state to the request headers. |
| 947 net::HttpRequestHeaders headers; | 1028 net::HttpRequestHeaders headers; |
| (...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1779 it->set_relevance(max_query_relevance); | 1860 it->set_relevance(max_query_relevance); |
| 1780 it->set_relevance_from_server(relevance_from_server); | 1861 it->set_relevance_from_server(relevance_from_server); |
| 1781 } | 1862 } |
| 1782 } | 1863 } |
| 1783 | 1864 |
| 1784 void SearchProvider::UpdateDone() { | 1865 void SearchProvider::UpdateDone() { |
| 1785 // We're done when the timer isn't running, there are no suggest queries | 1866 // We're done when the timer isn't running, there are no suggest queries |
| 1786 // pending, and we're not waiting on Instant. | 1867 // pending, and we're not waiting on Instant. |
| 1787 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); | 1868 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); |
| 1788 } | 1869 } |
| OLD | NEW |