Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync / sessions2 / notification_service_sessions_router.cc
blob7c96c6432d7801e55acd4294348127e9cfdfd3d2
1 // Copyright 2013 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/sync/sessions2/notification_service_sessions_router.h"
7 #include "base/logging.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/tab_helper.h"
10 #include "chrome/browser/favicon/favicon_changed_details.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/sync/glue/sync_start_util.h"
13 #include "chrome/browser/sync/glue/synced_tab_delegate.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/web_contents.h"
22 #if defined(ENABLE_MANAGED_USERS)
23 #include "chrome/browser/managed_mode/managed_user_service.h"
24 #include "chrome/browser/managed_mode/managed_user_service_factory.h"
25 #endif
27 using content::NavigationController;
28 using content::WebContents;
30 namespace browser_sync {
32 NotificationServiceSessionsRouter::NotificationServiceSessionsRouter(
33 Profile* profile, const syncer::SyncableService::StartSyncFlare& flare)
34 : handler_(NULL),
35 profile_(profile),
36 flare_(flare),
37 weak_ptr_factory_(this) {
39 registrar_.Add(this, chrome::NOTIFICATION_TAB_PARENTED,
40 content::NotificationService::AllSources());
41 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
42 content::NotificationService::AllSources());
43 registrar_.Add(this, content::NOTIFICATION_NAV_LIST_PRUNED,
44 content::NotificationService::AllSources());
45 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_CHANGED,
46 content::NotificationService::AllSources());
47 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
48 content::NotificationService::AllSources());
49 registrar_.Add(this,
50 chrome::NOTIFICATION_TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
51 content::NotificationService::AllSources());
52 registrar_.Add(this,
53 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
54 content::NotificationService::AllBrowserContextsAndSources());
55 registrar_.Add(this, chrome::NOTIFICATION_FAVICON_CHANGED,
56 content::Source<Profile>(profile_));
57 #if defined(ENABLE_MANAGED_USERS)
58 if (profile_->IsManaged()) {
59 ManagedUserService* managed_user_service =
60 ManagedUserServiceFactory::GetForProfile(profile_);
61 managed_user_service->AddNavigationBlockedCallback(
62 base::Bind(&NotificationServiceSessionsRouter::OnNavigationBlocked,
63 weak_ptr_factory_.GetWeakPtr()));
65 #endif
68 NotificationServiceSessionsRouter::~NotificationServiceSessionsRouter() {}
70 void NotificationServiceSessionsRouter::Observe(
71 int type,
72 const content::NotificationSource& source,
73 const content::NotificationDetails& details) {
74 switch (type) {
75 case chrome::NOTIFICATION_FAVICON_CHANGED: {
76 content::Details<FaviconChangedDetails> favicon_details(details);
77 if (handler_)
78 handler_->OnFaviconPageUrlsUpdated(favicon_details->urls);
79 return;
81 // Source<WebContents>.
82 case chrome::NOTIFICATION_TAB_PARENTED:
83 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME:
84 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: {
85 WebContents* web_contents = content::Source<WebContents>(source).ptr();
86 SyncedTabDelegate* tab =
87 SyncedTabDelegate::ImplFromWebContents(web_contents);
88 if (!tab || tab->profile() != profile_)
89 return;
90 if (handler_)
91 handler_->OnLocalTabModified(tab);
92 break;
94 // Source<NavigationController>.
95 case content::NOTIFICATION_NAV_LIST_PRUNED:
96 case content::NOTIFICATION_NAV_ENTRY_CHANGED:
97 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: {
98 SyncedTabDelegate* tab = SyncedTabDelegate::ImplFromWebContents(
99 content::Source<NavigationController>(source).ptr()->
100 GetWebContents());
101 if (!tab || tab->profile() != profile_)
102 return;
103 if (handler_)
104 handler_->OnLocalTabModified(tab);
105 break;
107 case chrome::NOTIFICATION_TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED: {
108 extensions::TabHelper* extension_tab_helper =
109 content::Source<extensions::TabHelper>(source).ptr();
110 if (extension_tab_helper->web_contents()->GetBrowserContext() !=
111 profile_) {
112 return;
114 if (extension_tab_helper->extension_app()) {
115 SyncedTabDelegate* tab = SyncedTabDelegate::ImplFromWebContents(
116 extension_tab_helper->web_contents());
117 if (handler_)
118 handler_->OnLocalTabModified(tab);
119 break;
121 return;
123 default:
124 LOG(ERROR) << "Received unexpected notification of type " << type;
125 return;
128 if (!flare_.is_null()) {
129 flare_.Run(syncer::SESSIONS);
130 flare_.Reset();
134 void NotificationServiceSessionsRouter::OnNavigationBlocked(
135 content::WebContents* web_contents) {
136 SyncedTabDelegate* tab =
137 SyncedTabDelegate::ImplFromWebContents(web_contents);
138 if (!tab || !handler_)
139 return;
141 DCHECK(tab->profile() == profile_);
142 handler_->OnLocalTabModified(tab);
145 void NotificationServiceSessionsRouter::StartRoutingTo(
146 LocalSessionEventHandler* handler) {
147 DCHECK(!handler_);
148 handler_ = handler;
151 void NotificationServiceSessionsRouter::Stop() {
152 weak_ptr_factory_.InvalidateWeakPtrs();
153 registrar_.RemoveAll();
154 handler_ = NULL;
157 } // namespace browser_sync