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