diff options
| author | sdefresne <sdefresne@chromium.org> | 2015-07-09 07:56:19 -0700 |
|---|---|---|
| committer | Commit bot <commit-bot@chromium.org> | 2015-07-09 14:56:44 +0000 |
| commit | 311a46585ee5b77486ef42d947bc7cc7566558b1 (patch) | |
| tree | d15f84966fbd2c4ade8527e74f99817974cc1979 /chrome/browser/rlz/chrome_rlz_tracker_delegate.cc | |
| parent | ec894a5d76eadbe3cb489faed6469ff78e00aec8 (diff) | |
| download | chromium_src-311a46585ee5b77486ef42d947bc7cc7566558b1.zip chromium_src-311a46585ee5b77486ef42d947bc7cc7566558b1.tar.gz chromium_src-311a46585ee5b77486ef42d947bc7cc7566558b1.tar.bz2 | |
Componentize chrome/browser/rlz
Add new class RLZTrackerDelegate that abstract access to embedder
specific singletons and informations and provide an implementation for
Chrome based on the previous implementation.
Split rlz_unittest.cc in tests of the RLZTracker and Chrome
implementation of the RLZTrackerDelegate interface and move the
RLZTracker tests into the component.
Add a new gyp/gn variable "enable_rlz_support". This variable is true on
the platforms that support RLZ (currently Windows, Mac, iOS and
ChromeOS). Use it to build library and unit tests even when the RLZ
support is not enabled in the Chrome binary (this is still controlled by
"enable_rlz" and depends on the branding).
Enable the tests on iOS and convert rlz_tracker_ios.mm to a C++ file.
TEST=Run unit_tests and components_unittests on a platform that supports
RLZ (Windows, Mac, iOS or ChromeOS) and check that they pass. Then build
Chrome with "enable_rlz" and manually checks that RLZ are sent with
searches as expected.
BUG=504841,508148
Review URL: https://codereview.chromium.org/1212163011
Cr-Commit-Position: refs/heads/master@{#338040}
Diffstat (limited to 'chrome/browser/rlz/chrome_rlz_tracker_delegate.cc')
| -rw-r--r-- | chrome/browser/rlz/chrome_rlz_tracker_delegate.cc | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/chrome/browser/rlz/chrome_rlz_tracker_delegate.cc b/chrome/browser/rlz/chrome_rlz_tracker_delegate.cc new file mode 100644 index 0000000..091bac1 --- /dev/null +++ b/chrome/browser/rlz/chrome_rlz_tracker_delegate.cc @@ -0,0 +1,236 @@ +// Copyright 2015 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 "chrome/browser/rlz/chrome_rlz_tracker_delegate.h" + +#include "base/command_line.h" +#include "base/prefs/pref_service.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/chrome_notification_types.h" +#include "chrome/browser/google/google_brand.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/search_engines/template_url_service_factory.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/pref_names.h" +#include "components/google/core/browser/google_util.h" +#include "components/omnibox/browser/omnibox_log.h" +#include "components/search_engines/template_url.h" +#include "components/search_engines/template_url_service.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/navigation_controller.h" +#include "content/public/browser/navigation_details.h" +#include "content/public/browser/navigation_entry.h" +#include "content/public/browser/notification_details.h" +#include "content/public/browser/notification_service.h" +#include "content/public/browser/notification_source.h" + +#if defined(OS_WIN) +#include "chrome/installer/util/google_update_settings.h" +#endif + +#if !defined(OS_IOS) +#include "chrome/browser/prefs/session_startup_pref.h" +#include "chrome/browser/ui/startup/startup_browser_creator.h" +#endif + +ChromeRLZTrackerDelegate::ChromeRLZTrackerDelegate() { +} + +ChromeRLZTrackerDelegate::~ChromeRLZTrackerDelegate() { +} + +// static +bool ChromeRLZTrackerDelegate::IsGoogleDefaultSearch(Profile* profile) { + bool is_google_default_search = false; + TemplateURLService* template_url_service = + TemplateURLServiceFactory::GetForProfile(profile); + if (template_url_service) { + const TemplateURL* url_template = + template_url_service->GetDefaultSearchProvider(); + is_google_default_search = url_template && + url_template->url_ref().HasGoogleBaseURLs( + template_url_service->search_terms_data()); + } + return is_google_default_search; +} + +// static +bool ChromeRLZTrackerDelegate::IsGoogleHomepage(Profile* profile) { + return google_util::IsGoogleHomePageUrl( + GURL(profile->GetPrefs()->GetString(prefs::kHomePage))); +} + +// static +bool ChromeRLZTrackerDelegate::IsGoogleInStartpages(Profile* profile) { +#if !defined(OS_IOS) + bool is_google_in_startpages = false; + SessionStartupPref session_startup_prefs = + StartupBrowserCreator::GetSessionStartupPref( + *base::CommandLine::ForCurrentProcess(), profile); + if (session_startup_prefs.type == SessionStartupPref::URLS) { + is_google_in_startpages = + std::count_if(session_startup_prefs.urls.begin(), + session_startup_prefs.urls.end(), + google_util::IsGoogleHomePageUrl) > 0; + } + return is_google_in_startpages; +#else + // iOS does not have a notion of startpages. + return false; +#endif +} + +void ChromeRLZTrackerDelegate::Cleanup() { + registrar_.RemoveAll(); + on_omnibox_search_callback_.Reset(); + on_homepage_search_callback_.Reset(); +} + +bool ChromeRLZTrackerDelegate::IsOnUIThread() { + return content::BrowserThread::CurrentlyOn(content::BrowserThread::UI); +} + +base::SequencedWorkerPool* ChromeRLZTrackerDelegate::GetBlockingPool() { + return content::BrowserThread::GetBlockingPool(); +} + +net::URLRequestContextGetter* ChromeRLZTrackerDelegate::GetRequestContext() { + return g_browser_process->system_request_context(); +} + +bool ChromeRLZTrackerDelegate::GetBrand(std::string* brand) { + return google_brand::GetBrand(brand); +} + +bool ChromeRLZTrackerDelegate::IsBrandOrganic(const std::string& brand) { + return brand.empty() || google_brand::IsOrganic(brand); +} + +bool ChromeRLZTrackerDelegate::GetReactivationBrand(std::string* brand) { + return google_brand::GetReactivationBrand(brand); +} + +bool ChromeRLZTrackerDelegate::ShouldEnableZeroDelayForTesting() { + return base::CommandLine::ForCurrentProcess()->HasSwitch( + ::switches::kTestType); +} + +bool ChromeRLZTrackerDelegate::GetLanguage(base::string16* language) { +#if defined(OS_WIN) + return GoogleUpdateSettings::GetLanguage(language); +#else + // TODO(thakis): Implement. + NOTIMPLEMENTED(); + return false; +#endif +} + +bool ChromeRLZTrackerDelegate::GetReferral(base::string16* referral) { +#if defined(OS_WIN) + return GoogleUpdateSettings::GetReferral(referral); +#else + // The referral program is defunct and not used. No need to implement this + // function on non-Win platforms. + return true; +#endif +} + +bool ChromeRLZTrackerDelegate::ClearReferral() { +#if defined(OS_WIN) + return GoogleUpdateSettings::ClearReferral(); +#else + // The referral program is defunct and not used. No need to implement this + // function on non-Win platforms. + return true; +#endif +} + +void ChromeRLZTrackerDelegate::SetOmniboxSearchCallback( + const base::Closure& callback) { + DCHECK(!callback.is_null()); + registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, + content::NotificationService::AllSources()); + on_omnibox_search_callback_ = callback; +} + +void ChromeRLZTrackerDelegate::SetHomepageSearchCallback( + const base::Closure& callback) { +#if !defined(OS_IOS) + DCHECK(!callback.is_null()); + registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, + content::NotificationService::AllSources()); + on_homepage_search_callback_ = callback; +#endif +} + +void ChromeRLZTrackerDelegate::Observe( + int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { + using std::swap; + base::Closure callback_to_run; + switch (type) { + case chrome::NOTIFICATION_OMNIBOX_OPENED_URL: + // In M-36, we made NOTIFICATION_OMNIBOX_OPENED_URL fire more often than + // it did previously. The RLZ folks want RLZ's "first search" detection + // to remain as unaffected as possible by this change. This test is + // there to keep the old behavior. + if (!content::Details<OmniboxLog>(details).ptr()->is_popup_open) + break; + registrar_.Remove(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, + content::NotificationService::AllSources()); + swap(callback_to_run, on_omnibox_search_callback_); + break; + +#if !defined(OS_IOS) + case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { + // Firstly check if it is a Google search. + content::LoadCommittedDetails* load_details = + content::Details<content::LoadCommittedDetails>(details).ptr(); + if (load_details == nullptr) + break; + + content::NavigationEntry* entry = load_details->entry; + if (entry == nullptr) + break; + + if (google_util::IsGoogleSearchUrl(entry->GetURL())) { + // If it is a Google search, check if it originates from HOMEPAGE by + // getting the previous NavigationEntry. + content::NavigationController* controller = + content::Source<content::NavigationController>(source).ptr(); + if (controller == nullptr) + break; + + int entry_index = controller->GetLastCommittedEntryIndex(); + if (entry_index < 1) + break; + + const content::NavigationEntry* previous_entry = + controller->GetEntryAtIndex(entry_index - 1); + + if (previous_entry == nullptr) + break; + + // Make sure it is a Google web page originated from HOMEPAGE. + if (google_util::IsGoogleHomePageUrl(previous_entry->GetURL()) && + ((previous_entry->GetTransitionType() & + ui::PAGE_TRANSITION_HOME_PAGE) != 0)) { + registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, + content::NotificationService::AllSources()); + swap(callback_to_run, on_homepage_search_callback_); + } + } + break; + } +#endif // !defined(OS_IOS) + + default: + NOTREACHED(); + break; + } + + if (!callback_to_run.is_null()) + callback_to_run.Run(); +} |
