OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/search_engines/desktop_search_utils.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/metrics/user_metrics.h" |
| 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/string_util.h" |
| 14 #include "components/pref_registry/pref_registry_syncable.h" |
| 15 #include "components/search_engines/prepopulated_engines.h" |
| 16 #include "components/search_engines/template_url_prepopulate_data.h" |
| 17 #include "components/search_engines/template_url_service.h" |
| 18 #include "components/search_engines/util.h" |
| 19 #include "net/base/url_util.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Values for the Search.DesktopSearch.URLAction histogram. |
| 24 enum DesktopSearchURLAction { |
| 25 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_FEATURE_DISABLED = 0, |
| 26 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_DEFAULT_SEARCH_IS_BING = 1, |
| 27 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_INVALID_SEARCH_ENGINE = 2, |
| 28 DESKTOP_SEARCH_URL_ACTION_REDIRECTION = 3, |
| 29 DESKTOP_SEARCH_URL_ACTION_MAX |
| 30 }; |
| 31 |
| 32 void RecordDesktopSearchURLAction(DesktopSearchURLAction action) { |
| 33 DCHECK_LT(action, DESKTOP_SEARCH_URL_ACTION_MAX); |
| 34 UMA_HISTOGRAM_ENUMERATION("Search.DesktopSearch.URLAction", action, |
| 35 DESKTOP_SEARCH_URL_ACTION_MAX); |
| 36 } |
| 37 |
| 38 // Detects whether a |url| comes from a desktop search. If so, puts the search |
| 39 // terms in |search_terms| and returns true. |
| 40 bool DetectDesktopSearch(const GURL& url, |
| 41 const SearchTermsData& search_terms_data, |
| 42 base::string16* search_terms) { |
| 43 DCHECK(search_terms); |
| 44 search_terms->clear(); |
| 45 |
| 46 scoped_ptr<TemplateURLData> template_url_data = |
| 47 TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine( |
| 48 TemplateURLPrepopulateData::bing); |
| 49 TemplateURL template_url(*template_url_data); |
| 50 if (!template_url.ExtractSearchTermsFromURL(url, search_terms_data, |
| 51 search_terms)) |
| 52 return false; |
| 53 |
| 54 // Query parameter that tells the source of a Bing search URL, and values |
| 55 // associated with desktop search. |
| 56 const char kBingSourceQueryKey[] = "form"; |
| 57 const char kBingSourceDesktopText[] = "WNSGPH"; |
| 58 const char kBingSourceDesktopVoice[] = "WNSBOX"; |
| 59 for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { |
| 60 // Use a case-insensitive comparison because the key is sometimes in capital |
| 61 // letters. |
| 62 if (base::EqualsCaseInsensitiveASCII(it.GetKey(), kBingSourceQueryKey)) { |
| 63 const std::string source = it.GetValue(); |
| 64 if (source == kBingSourceDesktopText || source == kBingSourceDesktopVoice) |
| 65 return true; |
| 66 } |
| 67 } |
| 68 |
| 69 return false; |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
| 74 namespace prefs { |
| 75 const char kDesktopSearchRedirectionInfobarShownPref[] = |
| 76 "desktop_search_redirection_infobar_shown"; |
| 77 } // namespace prefs |
| 78 |
| 79 const base::Feature kDesktopSearchRedirectionFeature{ |
| 80 "DesktopSearchRedirection", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 81 |
| 82 void RegisterDesktopSearchRedirectionPref( |
| 83 user_prefs::PrefRegistrySyncable* registry) { |
| 84 registry->RegisterBooleanPref( |
| 85 prefs::kDesktopSearchRedirectionInfobarShownPref, false); |
| 86 } |
| 87 |
| 88 bool ReplaceDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| 89 const PrefService* pref_service, |
| 90 TemplateURLService* template_url_service, |
| 91 GURL* url) { |
| 92 DCHECK(pref_service); |
| 93 DCHECK(template_url_service); |
| 94 DCHECK(url); |
| 95 |
| 96 // Check if |url| is a desktop search. |
| 97 base::string16 search_terms; |
| 98 if (!DetectDesktopSearch(*url, template_url_service->search_terms_data(), |
| 99 &search_terms)) |
| 100 return false; |
| 101 |
| 102 // Record that the user searched from the desktop. |
| 103 base::RecordAction(base::UserMetricsAction("DesktopSearch")); |
| 104 |
| 105 // Check if the redirection feature is enabled. |
| 106 if (!base::FeatureList::IsEnabled(kDesktopSearchRedirectionFeature)) { |
| 107 RecordDesktopSearchURLAction( |
| 108 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_FEATURE_DISABLED); |
| 109 return false; |
| 110 } |
| 111 |
| 112 // Check if the default search engine is Bing. |
| 113 const TemplateURL* default_search_engine = |
| 114 template_url_service->GetDefaultSearchProvider(); |
| 115 if (default_search_engine && |
| 116 TemplateURLPrepopulateData::GetEngineType( |
| 117 *default_search_engine, template_url_service->search_terms_data()) == |
| 118 SEARCH_ENGINE_BING) { |
| 119 RecordDesktopSearchURLAction( |
| 120 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_DEFAULT_SEARCH_IS_BING); |
| 121 return false; |
| 122 } |
| 123 |
| 124 // Replace |url| by a default search engine URL. |
| 125 GURL search_url( |
| 126 GetDefaultSearchURLForSearchTerms(template_url_service, search_terms)); |
| 127 if (!search_url.is_valid()) { |
| 128 RecordDesktopSearchURLAction( |
| 129 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_INVALID_SEARCH_ENGINE); |
| 130 return false; |
| 131 } |
| 132 |
| 133 RecordDesktopSearchURLAction(DESKTOP_SEARCH_URL_ACTION_REDIRECTION); |
| 134 |
| 135 url->Swap(&search_url); |
| 136 return !pref_service->GetBoolean( |
| 137 prefs::kDesktopSearchRedirectionInfobarShownPref); |
| 138 } |
OLD | NEW |