Commit 6047f192 authored by blundell@chromium.org's avatar blundell@chromium.org

Create GoogleURLTrackerClient interface and //chrome implementation

GoogleURLTrackerClient is the interface via which GoogleURLTracker will talk to
its embedder. This CL creates that interface as well as
ChromeGoogleURLTrackerClient, the //chrome implementation. The interface and
implementation are carved out of GoogleURLTrackerNavigationTracker(Impl),
specifically the parts that are not per-tab. A followup CL will modify
GoogleURLTrackerNavigationTracker to the driver interface for
GoogleURLTracker.

BUG=373220

Review URL: https://codereview.chromium.org/285193002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271638 0039d316-1c4b-4281-b951-d872f2087c98
parent 61b0d48b
// Copyright 2014 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/google/chrome_google_url_tracker_client.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/google/google_url_tracker.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
ChromeGoogleURLTrackerClient::ChromeGoogleURLTrackerClient() {
}
ChromeGoogleURLTrackerClient::~ChromeGoogleURLTrackerClient() {
}
void ChromeGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) {
if (listen) {
registrar_.Add(
this,
content::NOTIFICATION_NAV_ENTRY_PENDING,
content::NotificationService::AllBrowserContextsAndSources());
} else {
registrar_.Remove(
this,
content::NOTIFICATION_NAV_ENTRY_PENDING,
content::NotificationService::AllBrowserContextsAndSources());
}
}
bool ChromeGoogleURLTrackerClient::IsListeningForNavigationStart() {
return registrar_.IsRegistered(
this,
content::NOTIFICATION_NAV_ENTRY_PENDING,
content::NotificationService::AllBrowserContextsAndSources());
}
void ChromeGoogleURLTrackerClient::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_PENDING, type);
content::NavigationController* controller =
content::Source<content::NavigationController>(source).ptr();
InfoBarService* infobar_service =
InfoBarService::FromWebContents(controller->GetWebContents());
// Because we're listening to all sources, there may be no
// InfoBarService for some notifications, e.g. navigations in
// bubbles/balloons etc.
if (infobar_service) {
google_url_tracker()->OnNavigationPending(
controller,
infobar_service,
controller->GetPendingEntry()->GetUniqueID());
}
}
// Copyright 2014 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.
#ifndef CHROME_BROWSER_GOOGLE_CHROME_GOOGLE_URL_TRACKER_CLIENT_H_
#define CHROME_BROWSER_GOOGLE_CHROME_GOOGLE_URL_TRACKER_CLIENT_H_
#include "components/google/core/browser/google_url_tracker_client.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
class ChromeGoogleURLTrackerClient : public GoogleURLTrackerClient,
public content::NotificationObserver {
public:
ChromeGoogleURLTrackerClient();
virtual ~ChromeGoogleURLTrackerClient();
// GoogleURLTrackerClient:
virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
virtual bool IsListeningForNavigationStart() OVERRIDE;
private:
// content::NotificationObserver:
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(ChromeGoogleURLTrackerClient);
};
#endif // CHROME_BROWSER_GOOGLE_CHROME_GOOGLE_URL_TRACKER_CLIENT_H_
......@@ -17,6 +17,7 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/google/core/browser/google_url_tracker_client.h"
#include "components/infobars/core/infobar.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
......@@ -34,12 +35,15 @@ const char GoogleURLTracker::kSearchDomainCheckURL[] =
GoogleURLTracker::GoogleURLTracker(
Profile* profile,
scoped_ptr<GoogleURLTrackerClient> client,
scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper,
Mode mode)
: profile_(profile),
client_(client.Pass()),
nav_helper_(nav_helper.Pass()),
infobar_creator_(base::Bind(&GoogleURLTrackerInfoBarDelegate::Create)),
google_url_(mode == UNIT_TEST_MODE ? kDefaultGoogleHomepage :
google_url_(mode == UNIT_TEST_MODE ?
kDefaultGoogleHomepage :
profile->GetPrefs()->GetString(prefs::kLastKnownGoogleURL)),
fetcher_id_(0),
in_startup_sleep_(true),
......@@ -49,6 +53,7 @@ GoogleURLTracker::GoogleURLTracker(
search_committed_(false),
weak_ptr_factory_(this) {
net::NetworkChangeNotifier::AddIPAddressObserver(this);
client_->set_google_url_tracker(this);
nav_helper_->SetGoogleURLTracker(this);
// Because this function can be called during startup, when kicking off a URL
......@@ -199,6 +204,7 @@ void GoogleURLTracker::OnIPAddressChanged() {
}
void GoogleURLTracker::Shutdown() {
client_.reset();
nav_helper_.reset();
fetcher_.reset();
weak_ptr_factory_.InvalidateWeakPtrs();
......@@ -269,8 +275,8 @@ void GoogleURLTracker::SearchCommitted() {
search_committed_ = true;
// These notifications will fire a bit later in the same call chain we're
// currently in.
if (!nav_helper_->IsListeningForNavigationStart())
nav_helper_->SetListeningForNavigationStart(true);
if (!client_->IsListeningForNavigationStart())
client_->SetListeningForNavigationStart(true);
}
}
......@@ -415,13 +421,13 @@ void GoogleURLTracker::UnregisterForEntrySpecificNotifications(
++i) {
if (nav_helper_->IsListeningForNavigationCommit(
i->second->navigation_controller())) {
DCHECK(nav_helper_->IsListeningForNavigationStart());
DCHECK(client_->IsListeningForNavigationStart());
return;
}
}
if (nav_helper_->IsListeningForNavigationStart()) {
if (client_->IsListeningForNavigationStart()) {
DCHECK(!search_committed_);
nav_helper_->SetListeningForNavigationStart(false);
client_->SetListeningForNavigationStart(false);
}
}
......
......@@ -21,6 +21,7 @@
#include "net/url_request/url_fetcher_delegate.h"
#include "url/gurl.h"
class GoogleURLTrackerClient;
class GoogleURLTrackerNavigationHelper;
class PrefService;
class Profile;
......@@ -70,6 +71,7 @@ class GoogleURLTracker : public net::URLFetcherDelegate,
// than the GoogleURLTracker itself should actually use
// GoogleURLTrackerFactory::GetForProfile().
GoogleURLTracker(Profile* profile,
scoped_ptr<GoogleURLTrackerClient> client,
scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper,
Mode mode);
......@@ -109,12 +111,12 @@ class GoogleURLTracker : public net::URLFetcherDelegate,
// No one but GoogleURLTrackerMapEntry should call this.
void DeleteMapEntryForService(const InfoBarService* infobar_service);
// Called by the navigation observer after SearchCommitted() registers
// listeners, to indicate that we've received the "load now pending"
// notification. |navigation_controller| is the NavigationController for this
// load; |infobar_service| is the InfoBarService of the associated tab; and
// |pending_id| is the unique ID of the newly pending NavigationEntry.
// If there is already a visible GoogleURLTracker infobar for this tab, this
// Called by the client after SearchCommitted() registers listeners, to
// indicate that we've received the "load now pending" notification.
// |navigation_controller| is the NavigationController for this load;
// |infobar_service| is the InfoBarService of the associated tab; and
// |pending_id| is the unique ID of the newly pending NavigationEntry. If
// there is already a visible GoogleURLTracker infobar for this tab, this
// function resets its associated pending entry ID to the new ID. Otherwise
// this function creates a map entry for the associated tab.
virtual void OnNavigationPending(
......@@ -188,6 +190,8 @@ class GoogleURLTracker : public net::URLFetcherDelegate,
Profile* profile_;
scoped_ptr<GoogleURLTrackerClient> client_;
scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper_;
// Creates an infobar and adds it to the provided InfoBarService. Returns the
......
......@@ -5,6 +5,7 @@
#include "chrome/browser/google/google_url_tracker_factory.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/google/chrome_google_url_tracker_client.h"
#include "chrome/browser/google/google_url_tracker.h"
#include "chrome/browser/google/google_url_tracker_navigation_helper_impl.h"
#include "chrome/browser/profiles/incognito_helpers.h"
......@@ -36,9 +37,12 @@ GoogleURLTrackerFactory::~GoogleURLTrackerFactory() {
KeyedService* GoogleURLTrackerFactory::BuildServiceInstanceFor(
content::BrowserContext* profile) const {
scoped_ptr<GoogleURLTrackerClient> client(new ChromeGoogleURLTrackerClient());
scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(
new GoogleURLTrackerNavigationHelperImpl());
return new GoogleURLTracker(static_cast<Profile*>(profile), nav_helper.Pass(),
return new GoogleURLTracker(static_cast<Profile*>(profile),
client.Pass(),
nav_helper.Pass(),
GoogleURLTracker::NORMAL_MODE);
}
......
......@@ -19,17 +19,9 @@ class GoogleURLTrackerNavigationHelper {
public:
virtual ~GoogleURLTrackerNavigationHelper() {}
// Sets the GoogleURLTracker that should receive callbacks from this observer.
// Sets the GoogleURLTracker that is associated with this object.
virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) = 0;
// Enables or disables listening for navigation starts. OnNavigationPending
// will be called for each navigation start if listening is enabled.
virtual void SetListeningForNavigationStart(bool listen) = 0;
// Returns whether or not the observer is currently listening for navigation
// starts.
virtual bool IsListeningForNavigationStart() = 0;
// Enables or disables listening for navigation commits for the given
// NavigationController. OnNavigationCommitted will be called for each
// navigation commit if listening is enabled.
......
......@@ -26,22 +26,6 @@ void GoogleURLTrackerNavigationHelperImpl::SetGoogleURLTracker(
tracker_ = tracker;
}
void GoogleURLTrackerNavigationHelperImpl::SetListeningForNavigationStart(
bool listen) {
if (listen) {
registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING,
content::NotificationService::AllBrowserContextsAndSources());
} else {
registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_PENDING,
content::NotificationService::AllBrowserContextsAndSources());
}
}
bool GoogleURLTrackerNavigationHelperImpl::IsListeningForNavigationStart() {
return registrar_.IsRegistered(this, content::NOTIFICATION_NAV_ENTRY_PENDING,
content::NotificationService::AllBrowserContextsAndSources());
}
void GoogleURLTrackerNavigationHelperImpl::SetListeningForNavigationCommit(
const content::NavigationController* nav_controller,
bool listen) {
......@@ -104,23 +88,6 @@ void GoogleURLTrackerNavigationHelperImpl::Observe(
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case content::NOTIFICATION_NAV_ENTRY_PENDING: {
content::NavigationController* controller =
content::Source<content::NavigationController>(source).ptr();
content::WebContents* web_contents = controller->GetWebContents();
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents);
// Because we're listening to all sources, there may be no
// InfoBarService for some notifications, e.g. navigations in
// bubbles/balloons etc.
if (infobar_service) {
tracker_->OnNavigationPending(
controller, infobar_service,
controller->GetPendingEntry()->GetUniqueID());
}
break;
}
case content::NOTIFICATION_NAV_ENTRY_COMMITTED: {
content::NavigationController* controller =
content::Source<content::NavigationController>(source).ptr();
......
......@@ -19,8 +19,6 @@ class GoogleURLTrackerNavigationHelperImpl
// GoogleURLTrackerNavigationHelper.
virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE;
virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
virtual bool IsListeningForNavigationStart() OVERRIDE;
virtual void SetListeningForNavigationCommit(
const content::NavigationController* nav_controller,
bool listen) OVERRIDE;
......@@ -33,8 +31,6 @@ class GoogleURLTrackerNavigationHelperImpl
const content::NavigationController* nav_controller) OVERRIDE;
private:
friend class GoogleURLTrackerNavigationHelperTest;
// content::NotificationObserver:
virtual void Observe(int type,
const content::NotificationSource& source,
......
......@@ -15,6 +15,7 @@
#include "chrome/browser/google/google_url_tracker_navigation_helper.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/google/core/browser/google_url_tracker_client.h"
#include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_delegate.h"
#include "content/public/browser/notification_service.h"
......@@ -104,7 +105,39 @@ void TestCallbackListener::RegisterCallback(
&TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this)));
}
// TestGoogleURLTrackerNavigationHelper -------------------------------------
// TestGoogleURLTrackerClient -------------------------------------------------
class TestGoogleURLTrackerClient : public GoogleURLTrackerClient {
public:
TestGoogleURLTrackerClient();
virtual ~TestGoogleURLTrackerClient();
virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
virtual bool IsListeningForNavigationStart() OVERRIDE;
private:
bool observe_nav_start_;
DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient);
};
TestGoogleURLTrackerClient::TestGoogleURLTrackerClient()
: observe_nav_start_(false) {
}
TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() {
}
void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) {
observe_nav_start_ = listen;
}
bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() {
return observe_nav_start_;
}
// TestGoogleURLTrackerNavigationHelper ---------------------------------------
class TestGoogleURLTrackerNavigationHelper
: public GoogleURLTrackerNavigationHelper {
......@@ -113,8 +146,6 @@ class TestGoogleURLTrackerNavigationHelper
virtual ~TestGoogleURLTrackerNavigationHelper();
virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE;
virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
virtual bool IsListeningForNavigationStart() OVERRIDE;
virtual void SetListeningForNavigationCommit(
const content::NavigationController* nav_controller,
bool listen) OVERRIDE;
......@@ -128,7 +159,6 @@ class TestGoogleURLTrackerNavigationHelper
private:
GoogleURLTracker* tracker_;
bool observe_nav_start_;
std::set<const content::NavigationController*>
nav_controller_commit_listeners_;
std::set<const content::NavigationController*>
......@@ -136,8 +166,7 @@ class TestGoogleURLTrackerNavigationHelper
};
TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper()
: tracker_(NULL),
observe_nav_start_(false) {
: tracker_(NULL) {
}
TestGoogleURLTrackerNavigationHelper::
......@@ -149,15 +178,6 @@ void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker(
tracker_ = tracker;
}
void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationStart(
bool listen) {
observe_nav_start_ = listen;
}
bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationStart() {
return observe_nav_start_;
}
void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit(
const content::NavigationController* nav_controller,
bool listen) {
......@@ -261,6 +281,7 @@ class GoogleURLTrackerTest : public testing::Test {
// net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests().
scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
net::TestURLFetcherFactory fetcher_factory_;
GoogleURLTrackerClient* client_;
GoogleURLTrackerNavigationHelper* nav_helper_;
TestingProfile profile_;
scoped_ptr<GoogleURLTracker> google_url_tracker_;
......@@ -300,12 +321,16 @@ GoogleURLTrackerTest::~GoogleURLTrackerTest() {
void GoogleURLTrackerTest::SetUp() {
network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
// Ownership is passed to google_url_tracker_, but a weak pointer is kept;
// this is safe since GoogleURLTracker keeps the observer for its lifetime.
// Ownership is passed to google_url_tracker_, but weak pointers are kept;
// this is safe since GoogleURLTracker keeps these objects for its lifetime.
client_ = new TestGoogleURLTrackerClient();
nav_helper_ = new TestGoogleURLTrackerNavigationHelper();
scoped_ptr<GoogleURLTrackerClient> client(client_);
scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_);
google_url_tracker_.reset(
new GoogleURLTracker(&profile_, nav_helper.Pass(),
new GoogleURLTracker(&profile_,
client.Pass(),
nav_helper.Pass(),
GoogleURLTracker::UNIT_TEST_MODE));
google_url_tracker_->infobar_creator_ = base::Bind(
&GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this));
......@@ -314,9 +339,6 @@ void GoogleURLTrackerTest::SetUp() {
void GoogleURLTrackerTest::TearDown() {
while (!unique_ids_seen_.empty())
CloseTab(*unique_ids_seen_.begin());
nav_helper_ = NULL;
network_change_notifier_.reset();
}
net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
......@@ -371,7 +393,7 @@ void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id,
// for navigation starts if the searchdomaincheck response was bogus.
}
unique_ids_seen_.insert(unique_id);
if (nav_helper_->IsListeningForNavigationStart()) {
if (client_->IsListeningForNavigationStart()) {
google_url_tracker_->OnNavigationPending(
reinterpret_cast<content::NavigationController*>(unique_id),
reinterpret_cast<InfoBarService*>(unique_id), unique_id);
......
......@@ -681,6 +681,8 @@
'browser/geolocation/geolocation_prefs.h',
'browser/global_keyboard_shortcuts_mac.h',
'browser/global_keyboard_shortcuts_mac.mm',
'browser/google/chrome_google_url_tracker_client.cc',
'browser/google/chrome_google_url_tracker_client.h',
'browser/google/google_search_counter.cc',
'browser/google/google_search_counter.h',
'browser/google/google_update_settings_posix.cc',
......
......@@ -16,6 +16,8 @@
'sources': [
'google/core/browser/google_search_metrics.cc',
'google/core/browser/google_search_metrics.h',
'google/core/browser/google_url_tracker_client.cc',
'google/core/browser/google_url_tracker_client.h',
],
},
],
......
// Copyright 2014 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/google/core/browser/google_url_tracker_client.h"
GoogleURLTrackerClient::GoogleURLTrackerClient() : google_url_tracker_(NULL) {
}
GoogleURLTrackerClient::~GoogleURLTrackerClient() {
}
// Copyright 2014 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.
#ifndef COMPONENTS_GOOGLE_GOOGLE_URL_TRACKER_CLIENT_H_
#define COMPONENTS_GOOGLE_GOOGLE_URL_TRACKER_CLIENT_H_
#include "base/macros.h"
class GoogleURLTracker;
// Interface by which GoogleURLTracker communicates with its embedder.
class GoogleURLTrackerClient {
public:
GoogleURLTrackerClient();
virtual ~GoogleURLTrackerClient();
// Sets the GoogleURLTracker that is associated with this client.
void set_google_url_tracker(GoogleURLTracker* google_url_tracker) {
google_url_tracker_ = google_url_tracker;
}
// Enables or disables listening for navigation starts. OnNavigationPending
// will be called for each navigation start if listening is enabled.
virtual void SetListeningForNavigationStart(bool listen) = 0;
// Returns whether or not the client is currently listening for navigation
// starts.
virtual bool IsListeningForNavigationStart() = 0;
protected:
GoogleURLTracker* google_url_tracker() { return google_url_tracker_; }
private:
GoogleURLTracker* google_url_tracker_;
DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerClient);
};
#endif // COMPONENTS_GOOGLE_GOOGLE_URL_TRACKER_CLIENT_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment