OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/sync/sessions2/notification_service_sessions_router.h" | |
6 | |
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" | |
21 | |
Nicolas Zea
2013/11/21 02:34:52
nit: remove extra newline
tim (not reviewing)
2013/11/21 21:38:59
Done.
| |
22 | |
23 #if defined(ENABLE_MANAGED_USERS) | |
24 #include "chrome/browser/managed_mode/managed_user_service.h" | |
25 #include "chrome/browser/managed_mode/managed_user_service_factory.h" | |
26 #endif | |
27 | |
28 using content::NavigationController; | |
29 using content::WebContents; | |
30 | |
31 namespace browser_sync { | |
32 | |
33 NotificationServiceSessionsRouter::NotificationServiceSessionsRouter( | |
34 Profile* profile) | |
35 : handler_(NULL), profile_(profile), weak_ptr_factory_(this) { | |
36 | |
37 set_flare(sync_start_util::GetFlareForSyncableService( | |
38 profile->GetPath())); | |
39 | |
40 registrar_.Add(this, chrome::NOTIFICATION_TAB_PARENTED, | |
41 content::NotificationService::AllSources()); | |
42 registrar_.Add(this, | |
43 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
44 content::NotificationService::AllSources()); | |
45 registrar_.Add(this, content::NOTIFICATION_NAV_LIST_PRUNED, | |
46 content::NotificationService::AllSources()); | |
47 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_CHANGED, | |
48 content::NotificationService::AllSources()); | |
49 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
50 content::NotificationService::AllSources()); | |
51 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED, | |
52 content::NotificationService::AllBrowserContextsAndSources()); | |
53 registrar_.Add(this, | |
54 chrome::NOTIFICATION_TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED, | |
55 content::NotificationService::AllSources()); | |
56 registrar_.Add(this, | |
57 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | |
58 content::NotificationService::AllBrowserContextsAndSources()); | |
59 registrar_.Add(this, chrome::NOTIFICATION_FAVICON_CHANGED, | |
60 content::Source<Profile>(profile_)); | |
61 #if defined(ENABLE_MANAGED_USERS) | |
62 if (profile_->IsManaged()) { | |
63 ManagedUserService* managed_user_service = | |
64 ManagedUserServiceFactory::GetForProfile(profile_); | |
65 managed_user_service->AddNavigationBlockedCallback( | |
66 base::Bind(&NotificationServiceSessionsRouter::OnNavigationBlocked, | |
67 weak_ptr_factory_.GetWeakPtr())); | |
68 } | |
69 #endif | |
70 } | |
71 | |
72 NotificationServiceSessionsRouter::~NotificationServiceSessionsRouter() {} | |
73 | |
74 void NotificationServiceSessionsRouter::Observe( | |
75 int type, | |
76 const content::NotificationSource& source, | |
77 const content::NotificationDetails& details) { | |
78 switch (type) { | |
79 case chrome::NOTIFICATION_FAVICON_CHANGED: { | |
80 content::Details<FaviconChangedDetails> favicon_details(details); | |
81 if (handler_) | |
82 handler_->OnFaviconPageUrlsUpdated(favicon_details->urls); | |
83 return; | |
84 } | |
85 case chrome::NOTIFICATION_BROWSER_OPENED: { | |
86 Browser* browser = content::Source<Browser>(source).ptr(); | |
87 if (!browser || browser->profile() != profile_) { | |
88 return; | |
89 } | |
90 if (handler_) | |
91 handler_->OnBrowserOpened(); | |
92 break; | |
93 } | |
94 // Source<WebContents>. | |
95 case chrome::NOTIFICATION_TAB_PARENTED: | |
96 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: | |
97 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { | |
98 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | |
99 SyncedTabDelegate* tab = | |
100 SyncedTabDelegate::ImplFromWebContents(web_contents); | |
101 if (!tab || tab->profile() != profile_) | |
102 return; | |
103 if (handler_) | |
104 handler_->OnLocalTabModified(tab); | |
105 break; | |
106 } | |
107 // Source<NavigationController>. | |
108 case content::NOTIFICATION_NAV_LIST_PRUNED: | |
109 case content::NOTIFICATION_NAV_ENTRY_CHANGED: | |
110 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { | |
111 SyncedTabDelegate* tab = SyncedTabDelegate::ImplFromWebContents( | |
112 content::Source<NavigationController>(source).ptr()-> | |
113 GetWebContents()); | |
114 if (!tab || tab->profile() != profile_) | |
115 return; | |
116 if (handler_) | |
117 handler_->OnLocalTabModified(tab); | |
118 break; | |
119 } | |
120 case chrome::NOTIFICATION_TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED: { | |
121 extensions::TabHelper* extension_tab_helper = | |
122 content::Source<extensions::TabHelper>(source).ptr(); | |
123 if (extension_tab_helper->web_contents()->GetBrowserContext() != | |
124 profile_) { | |
125 return; | |
126 } | |
127 if (extension_tab_helper->extension_app()) { | |
128 SyncedTabDelegate* tab = SyncedTabDelegate::ImplFromWebContents( | |
129 extension_tab_helper->web_contents()); | |
130 if (handler_) | |
131 handler_->OnLocalTabModified(tab); | |
132 break; | |
133 } | |
134 return; | |
135 } | |
136 default: | |
137 LOG(ERROR) << "Received unexpected notification of type " | |
138 << type; | |
139 return; | |
140 } | |
141 | |
142 if (!flare_.is_null()) { | |
143 flare_.Run(syncer::SESSIONS); | |
144 flare_.Reset(); | |
145 } | |
146 } | |
147 | |
148 void NotificationServiceSessionsRouter::OnNavigationBlocked( | |
149 content::WebContents* web_contents) { | |
150 SyncedTabDelegate* tab = | |
151 SyncedTabDelegate::ImplFromWebContents(web_contents); | |
152 if (!tab) | |
153 return; | |
154 | |
155 DCHECK(tab->profile() == profile_); | |
156 handler_->OnLocalTabModified(tab); | |
157 } | |
158 | |
159 void NotificationServiceSessionsRouter::StartRoutingTo( | |
160 LocalSessionEventHandler* handler) { | |
161 DCHECK(!handler_); | |
162 handler_ = handler; | |
163 } | |
164 | |
165 void NotificationServiceSessionsRouter::set_flare( | |
166 const syncer::SyncableService::StartSyncFlare& flare) { | |
167 flare_ = flare; | |
168 } | |
169 | |
170 void NotificationServiceSessionsRouter::Stop() { | |
171 weak_ptr_factory_.InvalidateWeakPtrs(); | |
172 registrar_.RemoveAll(); | |
173 handler_ = NULL; | |
174 } | |
175 | |
176 } // namespace browser_sync | |
OLD | NEW |