Chromium Code Reviews| 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 prefs { | |
| 22 const char kDesktopSearchRedirectionInfobarShownPref[] = | |
| 23 "desktop_search_redirection_infobar_shown"; | |
| 24 } // namespace prefs | |
| 25 | |
| 26 const base::Feature kDesktopSearchRedirectionFeature{ | |
| 27 "DesktopSearchRedirection", base::FEATURE_DISABLED_BY_DEFAULT}; | |
| 28 | |
| 29 namespace { | |
|
Peter Kasting
2016/01/22 00:38:57
Nit: Put anonymous namespace stuff just below the
fdoray
2016/02/01 15:15:53
Done.
| |
| 30 | |
| 31 // Values for the Search.DesktopSearch.URLAction histogram. | |
| 32 enum DesktopSearchURLAction { | |
| 33 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_FEATURE_DISABLED = 0, | |
| 34 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_DEFAULT_SEARCH_IS_BING = 1, | |
| 35 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_INVALID_SEARCH_ENGINE = 2, | |
| 36 DESKTOP_SEARCH_URL_ACTION_REDIRECTION = 3, | |
| 37 DESKTOP_SEARCH_URL_ACTION_MAX | |
| 38 }; | |
| 39 | |
| 40 void RecordDesktopSearchURLAction(DesktopSearchURLAction action) { | |
| 41 DCHECK_LT(action, DESKTOP_SEARCH_URL_ACTION_MAX); | |
| 42 UMA_HISTOGRAM_ENUMERATION("Search.DesktopSearch.URLAction", action, | |
| 43 DESKTOP_SEARCH_URL_ACTION_MAX); | |
| 44 } | |
| 45 | |
| 46 // Detects whether a |url| comes from a desktop search. If so, puts the search | |
| 47 // terms in |search_terms| and returns true. | |
| 48 bool DetectDesktopSearch(const GURL& url, | |
| 49 const SearchTermsData& search_terms_data, | |
| 50 base::string16* search_terms) { | |
| 51 DCHECK(search_terms); | |
| 52 search_terms->clear(); | |
| 53 | |
| 54 scoped_ptr<TemplateURLData> template_url_data = | |
| 55 TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine( | |
| 56 TemplateURLPrepopulateData::bing); | |
| 57 TemplateURL template_url(*template_url_data); | |
| 58 if (!template_url.ExtractSearchTermsFromURL(url, search_terms_data, | |
| 59 search_terms)) | |
| 60 return false; | |
| 61 | |
| 62 // Query parameter that tells the source of a Bing search URL, and values | |
| 63 // associated with desktop search. | |
| 64 const char kBingSourceQueryKey[] = "form"; | |
| 65 const char kBingSourceDesktopText[] = "WNSGPH"; | |
| 66 const char kBingSourceDesktopVoice[] = "WNSBOX"; | |
| 67 for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { | |
| 68 // Use a case-insensitive comparison because the key is sometimes in capital | |
| 69 // letters. | |
| 70 if (base::EqualsCaseInsensitiveASCII(it.GetKey(), kBingSourceQueryKey)) { | |
| 71 const std::string source = it.GetValue(); | |
| 72 if (source == kBingSourceDesktopText || source == kBingSourceDesktopVoice) | |
| 73 return true; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 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 const TemplateURL* default_search_engine = | |
|
Peter Kasting
2016/01/22 00:38:57
Nit: Put this block just below the next comment si
fdoray
2016/02/01 15:15:53
Done.
| |
| 113 template_url_service->GetDefaultSearchProvider(); | |
| 114 DCHECK(default_search_engine); | |
|
Peter Kasting
2016/01/22 00:38:57
It's not safe to DCHECK this; users may not have a
fdoray
2016/02/01 15:15:53
Done.
| |
| 115 | |
| 116 // Check if the default search engine is Bing. | |
| 117 if (TemplateURLPrepopulateData::GetEngineType( | |
| 118 *default_search_engine, template_url_service->search_terms_data()) == | |
| 119 SEARCH_ENGINE_BING) { | |
| 120 RecordDesktopSearchURLAction( | |
| 121 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_DEFAULT_SEARCH_IS_BING); | |
| 122 return false; | |
| 123 } | |
| 124 | |
| 125 // Replace |url| by a default search engine URL. | |
| 126 GURL search_url( | |
| 127 GetDefaultSearchURLForSearchTerms(template_url_service, search_terms)); | |
| 128 if (!search_url.is_valid()) { | |
| 129 RecordDesktopSearchURLAction( | |
| 130 DESKTOP_SEARCH_URL_ACTION_NO_REDIRECTION_INVALID_SEARCH_ENGINE); | |
| 131 return false; | |
| 132 } | |
| 133 | |
| 134 RecordDesktopSearchURLAction(DESKTOP_SEARCH_URL_ACTION_REDIRECTION); | |
| 135 | |
| 136 url->Swap(&search_url); | |
| 137 return !pref_service->GetBoolean( | |
| 138 prefs::kDesktopSearchRedirectionInfobarShownPref); | |
| 139 } | |
| OLD | NEW |