OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/sessions/notification_service_sessions_router.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "chrome/browser/chrome_notification_types.h" | |
9 #include "chrome/browser/history/history_service_factory.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/sync/glue/sync_start_util.h" | |
12 #include "chrome/browser/ui/browser.h" | |
13 #include "chrome/browser/ui/sync/browser_synced_window_delegates_getter.h" | |
14 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h" | |
15 #include "chrome/common/features.h" | |
16 #include "components/history/core/browser/history_service.h" | |
17 #include "components/sync_sessions/sync_sessions_client.h" | |
18 #include "components/sync_sessions/synced_tab_delegate.h" | |
19 #include "content/public/browser/navigation_controller.h" | |
20 #include "content/public/browser/navigation_entry.h" | |
21 #include "content/public/browser/notification_details.h" | |
22 #include "content/public/browser/notification_service.h" | |
23 #include "content/public/browser/notification_source.h" | |
24 #include "content/public/browser/web_contents.h" | |
25 #include "extensions/features/features.h" | |
26 | |
27 #if defined(OS_ANDROID) | |
28 #include "chrome/browser/android/tab_android.h" | |
29 #endif | |
30 | |
31 #if BUILDFLAG(ENABLE_SUPERVISED_USERS) | |
32 #include "chrome/browser/supervised_user/supervised_user_service.h" | |
33 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | |
34 #endif | |
35 | |
36 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
37 #include "chrome/browser/extensions/tab_helper.h" | |
38 #endif | |
39 | |
40 using content::NavigationController; | |
41 using content::WebContents; | |
42 | |
43 namespace sync_sessions { | |
44 | |
45 namespace { | |
46 | |
47 SyncedTabDelegate* GetSyncedTabDelegateFromWebContents( | |
48 content::WebContents* web_contents) { | |
49 #if defined(OS_ANDROID) | |
50 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | |
51 return tab ? tab->GetSyncedTabDelegate() : nullptr; | |
52 #else | |
53 SyncedTabDelegate* delegate = | |
54 TabContentsSyncedTabDelegate::FromWebContents(web_contents); | |
55 return delegate; | |
56 #endif | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 NotificationServiceSessionsRouter::NotificationServiceSessionsRouter( | |
62 Profile* profile, | |
63 SyncSessionsClient* sessions_client, | |
64 const syncer::SyncableService::StartSyncFlare& flare) | |
65 : handler_(nullptr), | |
66 profile_(profile), | |
67 sessions_client_(sessions_client), | |
68 flare_(flare), | |
69 weak_ptr_factory_(this) { | |
70 registrar_.Add(this, chrome::NOTIFICATION_TAB_PARENTED, | |
71 content::NotificationService::AllSources()); | |
72 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
73 content::NotificationService::AllSources()); | |
74 registrar_.Add(this, content::NOTIFICATION_NAV_LIST_PRUNED, | |
75 content::NotificationService::AllSources()); | |
76 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_CHANGED, | |
77 content::NotificationService::AllSources()); | |
78 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
79 content::NotificationService::AllSources()); | |
80 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
81 registrar_.Add(this, | |
82 chrome::NOTIFICATION_TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED, | |
83 content::NotificationService::AllSources()); | |
84 #endif | |
85 registrar_.Add(this, | |
86 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | |
87 content::NotificationService::AllBrowserContextsAndSources()); | |
88 history::HistoryService* history_service = | |
89 HistoryServiceFactory::GetForProfile(profile, | |
90 ServiceAccessType::EXPLICIT_ACCESS); | |
91 if (history_service) { | |
92 favicon_changed_subscription_ = history_service->AddFaviconsChangedCallback( | |
93 base::Bind(&NotificationServiceSessionsRouter::OnFaviconsChanged, | |
94 base::Unretained(this))); | |
95 } | |
96 #if BUILDFLAG(ENABLE_SUPERVISED_USERS) | |
97 if (profile_->IsSupervised()) { | |
98 SupervisedUserService* supervised_user_service = | |
99 SupervisedUserServiceFactory::GetForProfile(profile_); | |
100 supervised_user_service->AddNavigationBlockedCallback( | |
101 base::Bind(&NotificationServiceSessionsRouter::OnNavigationBlocked, | |
102 weak_ptr_factory_.GetWeakPtr())); | |
103 } | |
104 #endif | |
105 } | |
106 | |
107 NotificationServiceSessionsRouter::~NotificationServiceSessionsRouter() {} | |
108 | |
109 void NotificationServiceSessionsRouter::Observe( | |
110 int type, | |
111 const content::NotificationSource& source, | |
112 const content::NotificationDetails& details) { | |
113 switch (type) { | |
114 // Source<WebContents>. | |
115 case chrome::NOTIFICATION_TAB_PARENTED: | |
116 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: | |
117 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { | |
118 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | |
119 if (Profile::FromBrowserContext(web_contents->GetBrowserContext()) != | |
120 profile_) | |
121 return; | |
122 SyncedTabDelegate* tab = | |
123 GetSyncedTabDelegateFromWebContents(web_contents); | |
124 if (!tab) | |
125 return; | |
126 if (handler_) | |
127 handler_->OnLocalTabModified(tab); | |
128 if (!tab->ShouldSync(sessions_client_)) | |
129 return; | |
130 break; | |
131 } | |
132 // Source<NavigationController>. | |
133 case content::NOTIFICATION_NAV_LIST_PRUNED: | |
134 case content::NOTIFICATION_NAV_ENTRY_CHANGED: | |
135 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { | |
136 WebContents* web_contents = | |
137 content::Source<NavigationController>(source).ptr()->GetWebContents(); | |
138 if (Profile::FromBrowserContext(web_contents->GetBrowserContext()) != | |
139 profile_) | |
140 return; | |
141 SyncedTabDelegate* tab = | |
142 GetSyncedTabDelegateFromWebContents(web_contents); | |
143 if (!tab) | |
144 return; | |
145 if (handler_) | |
146 handler_->OnLocalTabModified(tab); | |
147 if (!tab->ShouldSync(sessions_client_)) | |
148 return; | |
149 break; | |
150 } | |
151 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
152 case chrome::NOTIFICATION_TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED: { | |
153 extensions::TabHelper* extension_tab_helper = | |
154 content::Source<extensions::TabHelper>(source).ptr(); | |
155 if (Profile::FromBrowserContext( | |
156 extension_tab_helper->web_contents()->GetBrowserContext()) != | |
157 profile_) { | |
158 return; | |
159 } | |
160 if (extension_tab_helper->extension_app()) { | |
161 SyncedTabDelegate* tab = GetSyncedTabDelegateFromWebContents( | |
162 extension_tab_helper->web_contents()); | |
163 if (!tab) | |
164 return; | |
165 if (handler_) | |
166 handler_->OnLocalTabModified(tab); | |
167 if (!tab->ShouldSync(sessions_client_)) | |
168 return; | |
169 break; | |
170 } | |
171 return; | |
172 } | |
173 #endif | |
174 default: | |
175 LOG(ERROR) << "Received unexpected notification of type " << type; | |
176 return; | |
177 } | |
178 | |
179 if (!flare_.is_null()) { | |
180 flare_.Run(syncer::SESSIONS); | |
181 flare_.Reset(); | |
182 } | |
183 } | |
184 | |
185 void NotificationServiceSessionsRouter::OnNavigationBlocked( | |
186 content::WebContents* web_contents) { | |
187 DCHECK_EQ(profile_, | |
188 Profile::FromBrowserContext(web_contents->GetBrowserContext())); | |
189 SyncedTabDelegate* tab = GetSyncedTabDelegateFromWebContents(web_contents); | |
190 if (!tab || !handler_) | |
191 return; | |
192 | |
193 handler_->OnLocalTabModified(tab); | |
194 } | |
195 | |
196 void NotificationServiceSessionsRouter::OnFaviconsChanged( | |
197 const std::set<GURL>& page_urls, | |
198 const GURL& icon_url) { | |
199 if (handler_) | |
200 handler_->OnFaviconsChanged(page_urls, icon_url); | |
201 } | |
202 | |
203 void NotificationServiceSessionsRouter::StartRoutingTo( | |
204 LocalSessionEventHandler* handler) { | |
205 DCHECK(!handler_); | |
206 handler_ = handler; | |
207 } | |
208 | |
209 void NotificationServiceSessionsRouter::Stop() { | |
210 weak_ptr_factory_.InvalidateWeakPtrs(); | |
211 handler_ = nullptr; | |
212 } | |
213 | |
214 } // namespace sync_sessions | |
OLD | NEW |