Index: components/search_engines/desktop_search_utils_win.cc |
diff --git a/components/search_engines/desktop_search_utils_win.cc b/components/search_engines/desktop_search_utils_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..af0f54047768119d1d5b182c38887f8f3ed84516 |
--- /dev/null |
+++ b/components/search_engines/desktop_search_utils_win.cc |
@@ -0,0 +1,146 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/search_engines/desktop_search_utils_win.h" |
+ |
+#include <string> |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/metrics/histogram_macros.h" |
+#include "base/prefs/pref_service.h" |
+#include "base/strings/string_util.h" |
+#include "components/pref_registry/pref_registry_syncable.h" |
+#include "components/search_engines/prepopulated_engines.h" |
+#include "components/search_engines/template_url_prepopulate_data.h" |
+#include "components/search_engines/template_url_service.h" |
+#include "components/search_engines/util.h" |
+#include "net/base/url_util.h" |
+ |
+namespace prefs { |
+// Name of the preference keeping track of whether the Windows desktop search |
+// redirection infobar has already been shown. |
+const char kWindowsDesktopSearchRedirectionInfobarShownPref[] = |
+ "windows_desktop_search_redirection_infobar_shown"; |
+} // namespace prefs |
+ |
+const base::Feature kWindowsDesktopSearchRedirectionFeature = { |
Alexei Svitkine (slow)
2016/01/18 16:08:52
Nit: No =
fdoray
2016/01/18 18:51:45
Done.
|
+ "WindowsDesktopSearchRedirection", base::FEATURE_DISABLED_BY_DEFAULT}; |
+ |
+namespace { |
+ |
+// Values for the Search.WindowsDesktopSearch.URLAction histogram. |
+enum WindowsDesktopSearchURLAction { |
+ NO_REDIRECTION_FEATURE_DISABLED = 0, |
+ NO_REDIRECTION_DEFAULT_SEARCH_IS_BING = 1, |
+ NO_REDIRECTION_INVALID_SEARCH_ENGINE = 2, |
+ REDIRECTION_INFOBAR_SHOWN = 3, |
+ REDIRECTION_NO_INFOBAR = 4, |
+ WINDOWS_DESKTOP_SEARCH_URL_ACTION_MAX |
+}; |
+ |
+void RecordWindowsDesktopSearchURLAction(WindowsDesktopSearchURLAction action) { |
+ DCHECK_LT(action, WINDOWS_DESKTOP_SEARCH_URL_ACTION_MAX); |
+ UMA_HISTOGRAM_ENUMERATION("Search.WindowsDesktopSearch.URLAction", action, |
+ WINDOWS_DESKTOP_SEARCH_URL_ACTION_MAX); |
+} |
+ |
+// Detects whether a |url| comes from a Windows desktop search. If so, puts the |
+// search terms in |search_terms| and returns true. |
+bool DetectWindowsDesktopSearch(const GURL& url, |
+ const SearchTermsData& search_terms_data, |
+ base::string16* search_terms) { |
+ DCHECK(search_terms); |
+ |
+ scoped_ptr<TemplateURLData> template_url_data = |
+ TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine( |
+ TemplateURLPrepopulateData::bing); |
+ TemplateURL template_url(*template_url_data); |
+ |
+ if (!template_url.ExtractSearchTermsFromURL(url, search_terms_data, |
+ search_terms)) |
+ return false; |
+ |
+ // Query parameter that tells the source of a Bing search URL, and values |
+ // associated with Windows desktop search. |
+ const char kBingSourceQueryKey[] = "form"; |
+ const char kBingSourceDesktopText[] = "WNSGPH"; |
+ const char kBingSourceDesktopVoice[] = "WNSBOX"; |
+ |
+ for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) { |
+ // Use a case-insensitive comparison because the key is sometimes in capital |
+ // letters. |
+ if (base::EqualsCaseInsensitiveASCII(it.GetKey(), kBingSourceQueryKey)) { |
+ const std::string source = it.GetValue(); |
+ if (source == kBingSourceDesktopText || source == kBingSourceDesktopVoice) |
+ return true; |
+ } |
+ } |
+ |
+ search_terms->clear(); |
+ return false; |
+} |
+ |
+} // namespace |
+ |
+void RegisterWindowsDesktopSearchRedirectionPref( |
+ user_prefs::PrefRegistrySyncable* registry) { |
+ registry->RegisterBooleanPref( |
+ prefs::kWindowsDesktopSearchRedirectionInfobarShownPref, false); |
+} |
+ |
+void ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
+ PrefService* pref_service, |
+ TemplateURLService* template_url_service, |
+ GURL* url, |
+ bool* should_show_infobar) { |
+ DCHECK(pref_service); |
+ DCHECK(template_url_service); |
+ DCHECK(url); |
+ DCHECK(should_show_infobar); |
+ |
+ *should_show_infobar = false; |
+ |
+ // Check if |url| is a Windows desktop search. |
+ base::string16 search_terms; |
+ if (!DetectWindowsDesktopSearch( |
+ *url, template_url_service->search_terms_data(), &search_terms)) |
+ return; |
+ |
+ // Check if the redirection feature is enabled. |
+ if (!base::FeatureList::IsEnabled(kWindowsDesktopSearchRedirectionFeature)) { |
+ RecordWindowsDesktopSearchURLAction(NO_REDIRECTION_FEATURE_DISABLED); |
+ return; |
+ } |
+ |
+ const TemplateURL* default_search_engine = |
+ template_url_service->GetDefaultSearchProvider(); |
+ DCHECK(default_search_engine); |
+ |
+ // Check if the default search engine is Bing. |
+ if (TemplateURLPrepopulateData::GetEngineType( |
+ *default_search_engine, template_url_service->search_terms_data()) == |
+ SEARCH_ENGINE_BING) { |
+ RecordWindowsDesktopSearchURLAction(NO_REDIRECTION_DEFAULT_SEARCH_IS_BING); |
+ return; |
+ } |
+ |
+ // Replace |url| by a default search engine URL. |
+ GURL search_url( |
+ GetDefaultSearchURLForSearchTerms(template_url_service, search_terms)); |
+ if (!search_url.is_valid()) { |
+ RecordWindowsDesktopSearchURLAction(NO_REDIRECTION_INVALID_SEARCH_ENGINE); |
+ return; |
+ } |
+ |
+ *should_show_infobar = !pref_service->GetBoolean( |
+ prefs::kWindowsDesktopSearchRedirectionInfobarShownPref); |
+ pref_service->SetBoolean( |
+ prefs::kWindowsDesktopSearchRedirectionInfobarShownPref, true); |
+ |
+ RecordWindowsDesktopSearchURLAction(*should_show_infobar |
+ ? REDIRECTION_INFOBAR_SHOWN |
+ : REDIRECTION_NO_INFOBAR); |
+ |
+ url->Swap(&search_url); |
+} |