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/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 kWindowsDesktopSearchRedirectionInfobarShownPref[] = | |
23 "windows_desktop_search_redirection_infobar_shown"; | |
24 } // namespace prefs | |
25 | |
26 const base::Feature kWindowsDesktopSearchRedirectionFeature{ | |
27 "WindowsDesktopSearchRedirection", base::FEATURE_DISABLED_BY_DEFAULT}; | |
28 | |
29 namespace { | |
30 | |
31 // Values for the Search.WindowsDesktopSearch.URLAction histogram. | |
32 enum WindowsDesktopSearchURLAction { | |
33 NO_REDIRECTION_FEATURE_DISABLED = 0, | |
34 NO_REDIRECTION_DEFAULT_SEARCH_IS_BING = 1, | |
35 NO_REDIRECTION_INVALID_SEARCH_ENGINE = 2, | |
36 REDIRECTION_INFOBAR_SHOWN = 3, | |
37 REDIRECTION_NO_INFOBAR = 4, | |
38 WINDOWS_DESKTOP_SEARCH_URL_ACTION_MAX | |
39 }; | |
40 | |
41 void RecordWindowsDesktopSearchURLAction(WindowsDesktopSearchURLAction action) { | |
42 DCHECK_LT(action, WINDOWS_DESKTOP_SEARCH_URL_ACTION_MAX); | |
43 UMA_HISTOGRAM_ENUMERATION("Search.WindowsDesktopSearch.URLAction", action, | |
44 WINDOWS_DESKTOP_SEARCH_URL_ACTION_MAX); | |
45 } | |
46 | |
47 // Detects whether a |url| comes from a Windows desktop search. If so, puts the | |
48 // search terms in |search_terms| and returns true. | |
49 bool DetectWindowsDesktopSearch(const GURL& url, | |
50 const SearchTermsData& search_terms_data, | |
51 base::string16* search_terms) { | |
52 DCHECK(search_terms); | |
53 | |
54 scoped_ptr<TemplateURLData> template_url_data = | |
55 TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine( | |
56 TemplateURLPrepopulateData::bing); | |
57 TemplateURL template_url(*template_url_data); | |
58 | |
Peter Kasting
2016/01/20 03:03:02
Nit: I'd remove this blank line and the one on lin
fdoray
2016/01/21 21:02:33
Done.
| |
59 if (!template_url.ExtractSearchTermsFromURL(url, search_terms_data, | |
60 search_terms)) | |
61 return false; | |
62 | |
63 // Query parameter that tells the source of a Bing search URL, and values | |
64 // associated with Windows desktop search. | |
65 const char kBingSourceQueryKey[] = "form"; | |
66 const char kBingSourceDesktopText[] = "WNSGPH"; | |
67 const char kBingSourceDesktopVoice[] = "WNSBOX"; | |
68 | |
69 for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { | |
70 // Use a case-insensitive comparison because the key is sometimes in capital | |
71 // letters. | |
72 if (base::EqualsCaseInsensitiveASCII(it.GetKey(), kBingSourceQueryKey)) { | |
73 const std::string source = it.GetValue(); | |
74 if (source == kBingSourceDesktopText || source == kBingSourceDesktopVoice) | |
75 return true; | |
76 } | |
77 } | |
78 | |
79 search_terms->clear(); | |
Peter Kasting
2016/01/20 03:03:02
This should have been done atop the function to ha
fdoray
2016/01/21 21:02:33
Done.
| |
80 return false; | |
81 } | |
82 | |
83 } // namespace | |
84 | |
85 void RegisterWindowsDesktopSearchRedirectionPref( | |
86 user_prefs::PrefRegistrySyncable* registry) { | |
87 registry->RegisterBooleanPref( | |
88 prefs::kWindowsDesktopSearchRedirectionInfobarShownPref, false); | |
89 } | |
90 | |
91 void ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( | |
92 const PrefService* pref_service, | |
93 TemplateURLService* template_url_service, | |
94 GURL* url, | |
95 bool* should_show_infobar) { | |
96 DCHECK(pref_service); | |
97 DCHECK(template_url_service); | |
98 DCHECK(url); | |
99 DCHECK(should_show_infobar); | |
100 | |
101 *should_show_infobar = false; | |
102 | |
103 // Check if |url| is a Windows desktop search. | |
104 base::string16 search_terms; | |
105 if (!DetectWindowsDesktopSearch( | |
106 *url, template_url_service->search_terms_data(), &search_terms)) | |
107 return; | |
108 | |
109 // Record that the user searched from the desktop. | |
110 base::RecordAction(base::UserMetricsAction("WindowsDesktopSearch")); | |
111 | |
112 // Check if the redirection feature is enabled. | |
113 if (!base::FeatureList::IsEnabled(kWindowsDesktopSearchRedirectionFeature)) { | |
114 RecordWindowsDesktopSearchURLAction(NO_REDIRECTION_FEATURE_DISABLED); | |
115 return; | |
116 } | |
117 | |
118 const TemplateURL* default_search_engine = | |
119 template_url_service->GetDefaultSearchProvider(); | |
120 DCHECK(default_search_engine); | |
121 | |
122 // Check if the default search engine is Bing. | |
123 if (TemplateURLPrepopulateData::GetEngineType( | |
124 *default_search_engine, template_url_service->search_terms_data()) == | |
125 SEARCH_ENGINE_BING) { | |
126 RecordWindowsDesktopSearchURLAction(NO_REDIRECTION_DEFAULT_SEARCH_IS_BING); | |
127 return; | |
128 } | |
129 | |
130 // Replace |url| by a default search engine URL. | |
131 GURL search_url( | |
132 GetDefaultSearchURLForSearchTerms(template_url_service, search_terms)); | |
133 if (!search_url.is_valid()) { | |
134 RecordWindowsDesktopSearchURLAction(NO_REDIRECTION_INVALID_SEARCH_ENGINE); | |
135 return; | |
136 } | |
137 | |
138 *should_show_infobar = !pref_service->GetBoolean( | |
139 prefs::kWindowsDesktopSearchRedirectionInfobarShownPref); | |
140 if (*should_show_infobar) { | |
141 base::RecordAction(base::UserMetricsAction( | |
142 "WindowsDesktopSearchRedirectionInfoBar_Shown")); | |
Peter Kasting
2016/01/20 03:03:02
It seems like it would make more sense to record t
fdoray
2016/01/21 21:02:34
Done. I changed the code to record that the infoba
| |
143 } | |
144 RecordWindowsDesktopSearchURLAction(*should_show_infobar | |
145 ? REDIRECTION_INFOBAR_SHOWN | |
146 : REDIRECTION_NO_INFOBAR); | |
147 | |
148 url->Swap(&search_url); | |
149 } | |
OLD | NEW |