Skip to content

Commit

Permalink
Create GoogleURLTrackerClient interface and //chrome implementation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
blundell@chromium.org committed May 20, 2014
1 parent 61b0d48 commit 6047f19
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 81 deletions.
60 changes: 60 additions & 0 deletions chrome/browser/google/chrome_google_url_tracker_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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());
}
}
33 changes: 33 additions & 0 deletions chrome/browser/google/chrome_google_url_tracker_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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_
18 changes: 12 additions & 6 deletions chrome/browser/google/google_url_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -199,6 +204,7 @@ void GoogleURLTracker::OnIPAddressChanged() {
}

void GoogleURLTracker::Shutdown() {
client_.reset();
nav_helper_.reset();
fetcher_.reset();
weak_ptr_factory_.InvalidateWeakPtrs();
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
16 changes: 10 additions & 6 deletions chrome/browser/google/google_url_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "net/url_request/url_fetcher_delegate.h"
#include "url/gurl.h"

class GoogleURLTrackerClient;
class GoogleURLTrackerNavigationHelper;
class PrefService;
class Profile;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion chrome/browser/google/google_url_tracker_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}

Expand Down
10 changes: 1 addition & 9 deletions chrome/browser/google/google_url_tracker_navigation_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 0 additions & 33 deletions chrome/browser/google/google_url_tracker_navigation_helper_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 6047f19

Please sign in to comment.