1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/google/google_url_tracker_navigation_helper_impl.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/google/google_url_tracker.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "content/public/browser/navigation_controller.h"
11 #include "content/public/browser/navigation_entry.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/browser/web_contents.h"
15 GoogleURLTrackerNavigationHelperImpl::
16 GoogleURLTrackerNavigationHelperImpl() : tracker_(NULL
) {
19 GoogleURLTrackerNavigationHelperImpl::
20 ~GoogleURLTrackerNavigationHelperImpl() {
23 void GoogleURLTrackerNavigationHelperImpl::SetGoogleURLTracker(
24 GoogleURLTracker
* tracker
) {
29 void GoogleURLTrackerNavigationHelperImpl::SetListeningForNavigationCommit(
30 const content::NavigationController
* nav_controller
,
32 content::NotificationSource navigation_controller_source
=
33 content::Source
<content::NavigationController
>(nav_controller
);
35 registrar_
.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
36 navigation_controller_source
);
38 registrar_
.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
39 navigation_controller_source
);
43 bool GoogleURLTrackerNavigationHelperImpl::IsListeningForNavigationCommit(
44 const content::NavigationController
* nav_controller
) {
45 content::NotificationSource navigation_controller_source
=
46 content::Source
<content::NavigationController
>(nav_controller
);
47 return registrar_
.IsRegistered(
48 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
49 navigation_controller_source
);
52 void GoogleURLTrackerNavigationHelperImpl::SetListeningForTabDestruction(
53 const content::NavigationController
* nav_controller
,
55 content::NotificationSource navigation_controller_source
=
56 content::Source
<content::NavigationController
>(nav_controller
);
58 registrar_
.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED
,
59 GetWebContentsSource(navigation_controller_source
));
61 registrar_
.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED
,
62 GetWebContentsSource(navigation_controller_source
));
66 bool GoogleURLTrackerNavigationHelperImpl::IsListeningForTabDestruction(
67 const content::NavigationController
* nav_controller
) {
68 content::NotificationSource navigation_controller_source
=
69 content::Source
<content::NavigationController
>(nav_controller
);
70 return registrar_
.IsRegistered(
72 content::NOTIFICATION_WEB_CONTENTS_DESTROYED
,
73 GetWebContentsSource(navigation_controller_source
));
76 content::NotificationSource
77 GoogleURLTrackerNavigationHelperImpl::GetWebContentsSource(
78 const content::NotificationSource
& nav_controller_source
) {
79 content::NavigationController
* controller
=
80 content::Source
<content::NavigationController
>(
81 nav_controller_source
).ptr();
82 content::WebContents
* web_contents
= controller
->GetWebContents();
83 return content::Source
<content::WebContents
>(web_contents
);
86 void GoogleURLTrackerNavigationHelperImpl::Observe(
88 const content::NotificationSource
& source
,
89 const content::NotificationDetails
& details
) {
91 case content::NOTIFICATION_NAV_ENTRY_COMMITTED
: {
92 content::NavigationController
* controller
=
93 content::Source
<content::NavigationController
>(source
).ptr();
94 // Here we're only listening to notifications where we already know
95 // there's an associated InfoBarService.
96 content::WebContents
* web_contents
= controller
->GetWebContents();
97 InfoBarService
* infobar_service
=
98 InfoBarService::FromWebContents(web_contents
);
99 DCHECK(infobar_service
);
100 const GURL
& search_url
= controller
->GetActiveEntry()->GetURL();
101 if (!search_url
.is_valid()) // Not clear if this can happen.
102 tracker_
->OnTabClosed(controller
);
103 tracker_
->OnNavigationCommitted(infobar_service
, search_url
);
107 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED
: {
108 content::WebContents
* web_contents
=
109 content::Source
<content::WebContents
>(source
).ptr();
110 tracker_
->OnTabClosed(&web_contents
->GetController());
115 NOTREACHED() << "Unknown notification received:" << type
;