Chromium Code Reviews| 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..6c900636e768615b48d3e8e2a63bbb7109624350 |
| --- /dev/null |
| +++ b/components/search_engines/desktop_search_utils_win.cc |
| @@ -0,0 +1,149 @@ |
| +// 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/metrics/user_metrics.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 { |
| +const char kWindowsDesktopSearchRedirectionInfobarShownPref[] = |
| + "windows_desktop_search_redirection_infobar_shown"; |
| +} // namespace prefs |
| + |
| +const base::Feature kWindowsDesktopSearchRedirectionFeature{ |
| + "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); |
| + |
|
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.
|
| + 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(); |
|
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.
|
| + return false; |
| +} |
| + |
| +} // namespace |
| + |
| +void RegisterWindowsDesktopSearchRedirectionPref( |
| + user_prefs::PrefRegistrySyncable* registry) { |
| + registry->RegisterBooleanPref( |
| + prefs::kWindowsDesktopSearchRedirectionInfobarShownPref, false); |
| +} |
| + |
| +void ReplaceWindowsDesktopSearchURLWithDefaultSearchURLIfNeeded( |
| + const 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; |
| + |
| + // Record that the user searched from the desktop. |
| + base::RecordAction(base::UserMetricsAction("WindowsDesktopSearch")); |
| + |
| + // 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); |
| + if (*should_show_infobar) { |
| + base::RecordAction(base::UserMetricsAction( |
| + "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
|
| + } |
| + RecordWindowsDesktopSearchURLAction(*should_show_infobar |
| + ? REDIRECTION_INFOBAR_SHOWN |
| + : REDIRECTION_NO_INFOBAR); |
| + |
| + url->Swap(&search_url); |
| +} |