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" |
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
833 NavigationResults* list) { | 836 NavigationResults* list) { |
834 for (size_t i = 0; i < list->size(); ++i) { | 837 for (size_t i = 0; i < list->size(); ++i) { |
835 NavigationResult& result = (*list)[i]; | 838 NavigationResult& result = (*list)[i]; |
836 result.set_relevance( | 839 result.set_relevance( |
837 result.CalculateRelevance(input_, providers_.has_keyword_provider()) + | 840 result.CalculateRelevance(input_, providers_.has_keyword_provider()) + |
838 (list->size() - i - 1)); | 841 (list->size() - i - 1)); |
839 result.set_relevance_from_server(false); | 842 result.set_relevance_from_server(false); |
840 } | 843 } |
841 } | 844 } |
842 | 845 |
846 bool SearchProvider::CanSendURL(const GURL& url, | |
847 const TemplateURL* template_url, | |
848 Profile* profile){ | |
849 if (!url.is_valid()) | |
850 return false; | |
851 | |
852 // Only allow HTTP URLs or Google HTTPS URLs (including Google search | |
853 // result pages). For the latter case, Google was already sent the HTTPS | |
854 // URLs when requesting the page, so the information is just re-sent. | |
855 if ((url.scheme() != content::kHttpScheme) && | |
856 !google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, | |
857 google_util::ALLOW_NON_STANDARD_PORTS)) | |
858 return false; | |
859 | |
860 // Don't run if there's no profile or in incognito mode. | |
861 if (profile == NULL || profile->IsOffTheRecord()) | |
862 return false; | |
863 | |
864 // Don't run if we can't get preferences or search suggest is not enabled. | |
865 PrefService* prefs = profile->GetPrefs(); | |
866 if (!prefs->GetBoolean(prefs::kSearchSuggestEnabled)) | |
867 return false; | |
868 | |
869 // TODO(hfung): Generalize detection of if the provider supports zero suggest. | |
870 // User permission to send URLs to non-Google providers also need to be | |
871 // determined. | |
Peter Kasting
2013/10/08 01:10:25
Nit: This sentence seems redundant with the paragr
H Fung
2013/10/30 23:56:15
Done.
| |
872 // Only make the request if we know that the provider supports zero suggest | |
873 // (currently only the prepopulated Google provider). | |
874 if (template_url == NULL || !template_url->SupportsReplacement() || | |
875 template_url->prepopulate_id() != 1) | |
Peter Kasting
2013/10/08 01:10:25
Never check prepopulate IDs in order to determine
H Fung
2013/10/30 23:56:15
Done.
| |
876 return false; | |
877 | |
878 ProfileSyncService* service = | |
879 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | |
880 browser_sync::SyncPrefs sync_prefs(prefs); | |
881 // ZeroSuggest requires sending the current URL to the suggest provider, so we | |
882 // only want to enable it if the user is willing to have this data sent. | |
883 // Because tab sync involves sending the same data, we currently use | |
884 // "tab sync is enabled and tab sync data is unencrypted" as a proxy for | |
885 // "the user is OK with sending this data". We might someday want to change | |
886 // this to a standalone setting or part of some other explicit general opt-in. | |
Peter Kasting
2013/10/08 01:10:25
This comment paragraph probably belongs in the hea
H Fung
2013/10/30 23:56:15
Done.
| |
887 if (!OmniboxFieldTrial::InZeroSuggestFieldTrial() || | |
888 service == NULL || | |
889 !service->IsSyncEnabledAndLoggedIn() || | |
890 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( | |
891 syncer::PROXY_TABS) || | |
892 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) { | |
893 return false; | |
894 } | |
895 return true; | |
896 } | |
897 | |
843 net::URLFetcher* SearchProvider::CreateSuggestFetcher( | 898 net::URLFetcher* SearchProvider::CreateSuggestFetcher( |
844 int id, | 899 int id, |
845 const TemplateURL* template_url, | 900 const TemplateURL* template_url, |
846 const AutocompleteInput& input) { | 901 const AutocompleteInput& input) { |
847 if (!template_url || template_url->suggestions_url().empty()) | 902 if (!template_url || template_url->suggestions_url().empty()) |
848 return NULL; | 903 return NULL; |
849 | 904 |
850 // Bail if the suggestion URL is invalid with the given replacements. | 905 // Bail if the suggestion URL is invalid with the given replacements. |
851 TemplateURLRef::SearchTermsArgs search_term_args(input.text()); | 906 TemplateURLRef::SearchTermsArgs search_term_args(input.text()); |
852 search_term_args.cursor_position = input.cursor_position(); | 907 search_term_args.cursor_position = input.cursor_position(); |
853 search_term_args.page_classification = input.current_page_classification(); | 908 search_term_args.page_classification = input.current_page_classification(); |
909 // Send the current page URL if | |
Peter Kasting
2013/10/08 01:10:25
This comment is incomplete.
H Fung
2013/10/30 23:56:15
Done.
| |
910 if (CanSendURL(current_page_url_, template_url, profile_) && | |
911 OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial() && | |
912 template_url->prepopulate_id() == 1) | |
Peter Kasting
2013/10/08 01:10:25
Why do we need to re-check the prepopulate ID (eve
H Fung
2013/10/30 23:56:15
Done.
| |
913 search_term_args.current_page_url = current_page_url_.spec(); | |
854 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms( | 914 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms( |
855 search_term_args)); | 915 search_term_args)); |
856 if (!suggest_url.is_valid()) | 916 if (!suggest_url.is_valid()) |
857 return NULL; | 917 return NULL; |
858 | 918 |
859 suggest_results_pending_++; | 919 suggest_results_pending_++; |
860 LogOmniboxSuggestRequest(REQUEST_SENT); | 920 LogOmniboxSuggestRequest(REQUEST_SENT); |
861 | 921 |
862 net::URLFetcher* fetcher = | 922 net::URLFetcher* fetcher = |
863 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this); | 923 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this); |
(...skipping 754 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1618 it->set_relevance(max_query_relevance); | 1678 it->set_relevance(max_query_relevance); |
1619 it->set_relevance_from_server(relevance_from_server); | 1679 it->set_relevance_from_server(relevance_from_server); |
1620 } | 1680 } |
1621 } | 1681 } |
1622 | 1682 |
1623 void SearchProvider::UpdateDone() { | 1683 void SearchProvider::UpdateDone() { |
1624 // We're done when the timer isn't running, there are no suggest queries | 1684 // We're done when the timer isn't running, there are no suggest queries |
1625 // pending, and we're not waiting on Instant. | 1685 // pending, and we're not waiting on Instant. |
1626 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); | 1686 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); |
1627 } | 1687 } |
OLD | NEW |