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_win.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/metrics/histogram_macros.h" | |
11 #include "base/prefs/pref_service.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "components/pref_registry/pref_registry_syncable.h" | |
14 #include "components/search_engines/prepopulated_engines.h" | |
15 #include "components/search_engines/template_url_prepopulate_data.h" | |
16 #include "components/search_engines/template_url_service.h" | |
17 #include "components/search_engines/util.h" | |
18 #include "net/base/url_util.h" | |
19 | |
20 namespace prefs { | |
21 // Name of the preference keeping track of whether the Windows desktop search | |
22 // redirection infobar has already been shown. | |
23 const char kWindowsDesktopSearchRedirectionInfobarShownPref[] = | |
24 "windows_desktop_search_redirection_infobar_shown"; | |
25 } // namespace prefs | |
26 | |
27 const base::Feature kWindowsDesktopSearchRedirectionFeature = { | |
Alexei Svitkine (slow)
2016/01/18 16:08:52
Nit: No =
fdoray
2016/01/18 18:51:45
Done.
| |
28 "WindowsDesktopSearchRedirection", base::FEATURE_DISABLED_BY_DEFAULT}; | |
29 | |
30 namespace { | |
31 | |
32 // Values for the Search.WindowsDesktopSearch.URLAction histogram. | |
33 enum WindowsDesktopSearchURLAction { | |
34 NO_REDIRECTION_FEATURE_DISABLED = 0, | |
35 NO_REDIRECTION_DEFAULT_SEARCH_IS_BING = 1, | |
36 NO_REDIRECTION_INVALID_SEARCH_ENGINE = 2, | |
37 REDIRECTION_INFOBAR_SHOWN = 3, | |
38 REDIRECTION_NO_INFOBAR = 4, | |
39 WINDOWS_DESKTOP_SEARCH_URL_ACTION_MAX | |
40 }; | |
41 | |
42 void RecordWindowsDesktopSearchURLAction(WindowsDesktopSearchURLAction action) { | |
43 DCHECK_LT(action, WINDOWS_DESKTOP_SEARCH_URL_ACTION_MAX); | |
44 UMA_HISTOGRAM_ENUMERATION("Search.WindowsDesktopSearch.URLAction", action, | |
45 WINDOWS_DESKTOP_SEARCH_URL_ACTION_MAX); | |
46 } | |
47 | |
48 // Detects whether a |url| comes from a Windows desktop search. If so, puts the | |
49 // search terms in |search_terms| and returns true. | |
50 bool DetectWindowsDesktopSearch(const GURL& url, | |
51 const SearchTermsData& search_terms_data, | |
52 base::string16* search_terms) { | |
53 DCHECK(search_terms); | |
54 | |
55 scoped_ptr<TemplateURLData> template_url_data = | |
56 TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine( | |
57 TemplateURLPrepopulateData::bing); | |
58 TemplateURL template_url(*template_url_data); | |
59 | |
60 if (!template_url.ExtractSearchTermsFromURL(url, search_terms_data, | |
61 search_terms)) | |
62 return false; | |
63 | |
64 // Query parameter that tells the source of a Bing search URL, and values | |
65 // associated with Windows desktop search. | |
66 const char kBingSourceQueryKey[] = "form"; | |
67 const char kBingSourceDesktopText[] = "WNSGPH"; | |
68 const char kBingSourceDesktopVoice[] = "WNSBOX"; | |
69 | |
70 for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { | |
71 // Use a case-insensitive comparison because the key is sometimes in capital | |
72 // letters. | |
73 if (base::EqualsCaseInsensitiveASCII(it.GetKey(), kBingSourceQueryKey)) { | |
74 const std::string source = it.GetValue(); | |
75 if (source == kBingSourceDesktopText || source == kBingSourceDesktopVoice) | |
76 return true; | |
77 } | |
78 } | |
79 | |
80 search_terms->clear(); | |
81 return false; | |
82 } | |
83 | |
84 } // namespace | |
85 | |
86 void RegisterWindowsDesktopSearchRedirectionPref( | |
87 user_prefs::PrefRegistrySyncable* registry) { | |
88 registry->RegisterBooleanPref( | |
89 prefs::kWindowsDesktopSearchRedirectionInfobarShownPref, false); | |
90 } | |
91 | |
92 void ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( | |
93 PrefService* pref_service, | |
94 TemplateURLService* template_url_service, | |
95 GURL* url, | |
96 bool* should_show_infobar) { | |
97 DCHECK(pref_service); | |
98 DCHECK(template_url_service); | |
99 DCHECK(url); | |
100 DCHECK(should_show_infobar); | |
101 | |
102 *should_show_infobar = false; | |
103 | |
104 // Check if |url| is a Windows desktop search. | |
105 base::string16 search_terms; | |
106 if (!DetectWindowsDesktopSearch( | |
107 *url, template_url_service->search_terms_data(), &search_terms)) | |
108 return; | |
109 | |
110 // Check if the redirection feature is enabled. | |
111 if (!base::FeatureList::IsEnabled(kWindowsDesktopSearchRedirectionFeature)) { | |
112 RecordWindowsDesktopSearchURLAction(NO_REDIRECTION_FEATURE_DISABLED); | |
113 return; | |
114 } | |
115 | |
116 const TemplateURL* default_search_engine = | |
117 template_url_service->GetDefaultSearchProvider(); | |
118 DCHECK(default_search_engine); | |
119 | |
120 // Check if the default search engine is Bing. | |
121 if (TemplateURLPrepopulateData::GetEngineType( | |
122 *default_search_engine, template_url_service->search_terms_data()) == | |
123 SEARCH_ENGINE_BING) { | |
124 RecordWindowsDesktopSearchURLAction(NO_REDIRECTION_DEFAULT_SEARCH_IS_BING); | |
125 return; | |
126 } | |
127 | |
128 // Replace |url| by a default search engine URL. | |
129 GURL search_url( | |
130 GetDefaultSearchURLForSearchTerms(template_url_service, search_terms)); | |
131 if (!search_url.is_valid()) { | |
132 RecordWindowsDesktopSearchURLAction(NO_REDIRECTION_INVALID_SEARCH_ENGINE); | |
133 return; | |
134 } | |
135 | |
136 *should_show_infobar = !pref_service->GetBoolean( | |
137 prefs::kWindowsDesktopSearchRedirectionInfobarShownPref); | |
138 pref_service->SetBoolean( | |
139 prefs::kWindowsDesktopSearchRedirectionInfobarShownPref, true); | |
140 | |
141 RecordWindowsDesktopSearchURLAction(*should_show_infobar | |
142 ? REDIRECTION_INFOBAR_SHOWN | |
143 : REDIRECTION_NO_INFOBAR); | |
144 | |
145 url->Swap(&search_url); | |
146 } | |
OLD | NEW |