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