| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/glue/chrome_sync_notification_bridge.h" | 5 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
| 14 #include "sync/internal_api/public/base/model_type.h" |
| 14 #include "sync/notifier/invalidation_handler.h" | 15 #include "sync/notifier/invalidation_handler.h" |
| 15 #include "sync/notifier/invalidator_registrar.h" | 16 #include "sync/notifier/invalidator_registrar.h" |
| 16 | 17 |
| 17 using content::BrowserThread; | 18 using content::BrowserThread; |
| 18 | 19 |
| 19 namespace browser_sync { | 20 namespace browser_sync { |
| 20 | 21 |
| 21 class ChromeSyncNotificationBridge::Core | 22 class ChromeSyncNotificationBridge::Core |
| 22 : public base::RefCountedThreadSafe<Core> { | 23 : public base::RefCountedThreadSafe<Core> { |
| 23 public: | 24 public: |
| 24 // Created on UI thread. | 25 // Created on UI thread. |
| 25 explicit Core( | 26 explicit Core( |
| 26 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner); | 27 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner); |
| 27 | 28 |
| 28 // All member functions below must be called on the sync task runner. | 29 // All member functions below must be called on the sync task runner. |
| 29 | 30 |
| 30 void InitializeOnSyncThread(); | 31 void InitializeOnSyncThread(); |
| 31 void CleanupOnSyncThread(); | 32 void CleanupOnSyncThread(); |
| 32 | 33 |
| 33 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types); | |
| 34 void RegisterHandler(syncer::InvalidationHandler* handler); | 34 void RegisterHandler(syncer::InvalidationHandler* handler); |
| 35 void UpdateRegisteredIds(syncer::InvalidationHandler* handler, | 35 void UpdateRegisteredIds(syncer::InvalidationHandler* handler, |
| 36 const syncer::ObjectIdSet& ids); | 36 const syncer::ObjectIdSet& ids); |
| 37 void UnregisterHandler(syncer::InvalidationHandler* handler); | 37 void UnregisterHandler(syncer::InvalidationHandler* handler); |
| 38 | 38 |
| 39 void EmitNotification( | 39 void EmitNotification( |
| 40 const syncer::ModelTypeStateMap& state_map, | 40 const syncer::ObjectIdStateMap& state_map, |
| 41 syncer::IncomingNotificationSource notification_source); | 41 syncer::IncomingNotificationSource notification_source); |
| 42 | 42 |
| 43 bool IsHandlerRegisteredForTest(syncer::InvalidationHandler* handler) const; |
| 44 syncer::ObjectIdSet GetRegisteredIdsForTest( |
| 45 syncer::InvalidationHandler* handler) const; |
| 46 |
| 43 private: | 47 private: |
| 44 friend class base::RefCountedThreadSafe<Core>; | 48 friend class base::RefCountedThreadSafe<Core>; |
| 45 | 49 |
| 46 // Destroyed on the UI thread or on |sync_task_runner_|. | 50 // Destroyed on the UI thread or on |sync_task_runner_|. |
| 47 ~Core(); | 51 ~Core(); |
| 48 | 52 |
| 49 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; | 53 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; |
| 50 | 54 |
| 51 // Used only on |sync_task_runner_|. | 55 // Used only on |sync_task_runner_|. |
| 52 syncer::ModelTypeSet enabled_types_; | 56 scoped_ptr<syncer::InvalidatorRegistrar> invalidator_registrar_; |
| 53 scoped_ptr<syncer::InvalidatorRegistrar> notifier_registrar_; | |
| 54 }; | 57 }; |
| 55 | 58 |
| 56 ChromeSyncNotificationBridge::Core::Core( | 59 ChromeSyncNotificationBridge::Core::Core( |
| 57 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 60 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
| 58 : sync_task_runner_(sync_task_runner) { | 61 : sync_task_runner_(sync_task_runner) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 DCHECK(sync_task_runner_.get()); | 63 DCHECK(sync_task_runner_.get()); |
| 61 } | 64 } |
| 62 | 65 |
| 63 ChromeSyncNotificationBridge::Core::~Core() { | 66 ChromeSyncNotificationBridge::Core::~Core() { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 65 sync_task_runner_->RunsTasksOnCurrentThread()); | 68 sync_task_runner_->RunsTasksOnCurrentThread()); |
| 66 DCHECK(!notifier_registrar_.get()); | 69 DCHECK(!invalidator_registrar_.get()); |
| 67 } | 70 } |
| 68 | 71 |
| 69 void ChromeSyncNotificationBridge::Core::InitializeOnSyncThread() { | 72 void ChromeSyncNotificationBridge::Core::InitializeOnSyncThread() { |
| 70 notifier_registrar_.reset(new syncer::InvalidatorRegistrar()); | 73 invalidator_registrar_.reset(new syncer::InvalidatorRegistrar()); |
| 71 } | 74 } |
| 72 | 75 |
| 73 void ChromeSyncNotificationBridge::Core::CleanupOnSyncThread() { | 76 void ChromeSyncNotificationBridge::Core::CleanupOnSyncThread() { |
| 74 notifier_registrar_.reset(); | 77 invalidator_registrar_.reset(); |
| 75 } | |
| 76 | |
| 77 void ChromeSyncNotificationBridge::Core::UpdateEnabledTypes( | |
| 78 syncer::ModelTypeSet types) { | |
| 79 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 80 enabled_types_ = types; | |
| 81 } | 78 } |
| 82 | 79 |
| 83 void ChromeSyncNotificationBridge::Core::RegisterHandler( | 80 void ChromeSyncNotificationBridge::Core::RegisterHandler( |
| 84 syncer::InvalidationHandler* handler) { | 81 syncer::InvalidationHandler* handler) { |
| 85 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 82 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 86 notifier_registrar_->RegisterHandler(handler); | 83 invalidator_registrar_->RegisterHandler(handler); |
| 87 } | 84 } |
| 88 | 85 |
| 89 void ChromeSyncNotificationBridge::Core::UpdateRegisteredIds( | 86 void ChromeSyncNotificationBridge::Core::UpdateRegisteredIds( |
| 90 syncer::InvalidationHandler* handler, | 87 syncer::InvalidationHandler* handler, |
| 91 const syncer::ObjectIdSet& ids) { | 88 const syncer::ObjectIdSet& ids) { |
| 92 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 89 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 93 notifier_registrar_->UpdateRegisteredIds(handler, ids); | 90 invalidator_registrar_->UpdateRegisteredIds(handler, ids); |
| 94 } | 91 } |
| 95 | 92 |
| 96 void ChromeSyncNotificationBridge::Core::UnregisterHandler( | 93 void ChromeSyncNotificationBridge::Core::UnregisterHandler( |
| 97 syncer::InvalidationHandler* handler) { | 94 syncer::InvalidationHandler* handler) { |
| 98 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 95 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 99 notifier_registrar_->UnregisterHandler(handler); | 96 invalidator_registrar_->UnregisterHandler(handler); |
| 100 } | 97 } |
| 101 | 98 |
| 102 void ChromeSyncNotificationBridge::Core::EmitNotification( | 99 void ChromeSyncNotificationBridge::Core::EmitNotification( |
| 103 const syncer::ModelTypeStateMap& state_map, | 100 const syncer::ObjectIdStateMap& state_map, |
| 104 syncer::IncomingNotificationSource notification_source) { | 101 syncer::IncomingNotificationSource notification_source) { |
| 105 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 102 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 106 const syncer::ModelTypeStateMap& effective_state_map = | 103 const syncer::ObjectIdStateMap& effective_state_map = |
| 107 state_map.empty() ? | 104 state_map.empty() ? |
| 108 syncer::ModelTypeSetToStateMap(enabled_types_, std::string()) : | 105 ObjectIdSetToStateMap( |
| 106 invalidator_registrar_->GetAllRegisteredIds(), std::string()) : |
| 109 state_map; | 107 state_map; |
| 110 | 108 |
| 111 notifier_registrar_->DispatchInvalidationsToHandlers( | 109 invalidator_registrar_->DispatchInvalidationsToHandlers( |
| 112 ModelTypeStateMapToObjectIdStateMap(effective_state_map), | 110 effective_state_map, notification_source); |
| 113 notification_source); | 111 } |
| 112 |
| 113 bool ChromeSyncNotificationBridge::Core::IsHandlerRegisteredForTest( |
| 114 syncer::InvalidationHandler* handler) const { |
| 115 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 116 return invalidator_registrar_->IsHandlerRegisteredForTest(handler); |
| 117 } |
| 118 |
| 119 syncer::ObjectIdSet |
| 120 ChromeSyncNotificationBridge::Core::GetRegisteredIdsForTest( |
| 121 syncer::InvalidationHandler* handler) const { |
| 122 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 123 return invalidator_registrar_->GetRegisteredIds(handler); |
| 114 } | 124 } |
| 115 | 125 |
| 116 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( | 126 ChromeSyncNotificationBridge::ChromeSyncNotificationBridge( |
| 117 const Profile* profile, | 127 const Profile* profile, |
| 118 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | 128 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) |
| 119 : sync_task_runner_(sync_task_runner), | 129 : sync_task_runner_(sync_task_runner), |
| 120 core_(new Core(sync_task_runner_)) { | 130 core_(new Core(sync_task_runner_)) { |
| 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 122 DCHECK(profile); | 132 DCHECK(profile); |
| 123 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 133 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
| 124 content::Source<Profile>(profile)); | 134 content::Source<Profile>(profile)); |
| 125 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 135 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
| 126 content::Source<Profile>(profile)); | 136 content::Source<Profile>(profile)); |
| 127 | 137 |
| 128 if (!sync_task_runner_->PostTask( | 138 if (!sync_task_runner_->PostTask( |
| 129 FROM_HERE, base::Bind(&Core::InitializeOnSyncThread, core_))) { | 139 FROM_HERE, base::Bind(&Core::InitializeOnSyncThread, core_))) { |
| 130 NOTREACHED(); | 140 NOTREACHED(); |
| 131 } | 141 } |
| 132 } | 142 } |
| 133 | 143 |
| 134 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} | 144 ChromeSyncNotificationBridge::~ChromeSyncNotificationBridge() {} |
| 135 | 145 |
| 136 void ChromeSyncNotificationBridge::StopForShutdown() { | 146 void ChromeSyncNotificationBridge::StopForShutdown() { |
| 137 if (!sync_task_runner_->PostTask( | 147 if (!sync_task_runner_->PostTask( |
| 138 FROM_HERE, base::Bind(&Core::CleanupOnSyncThread, core_))) { | 148 FROM_HERE, base::Bind(&Core::CleanupOnSyncThread, core_))) { |
| 139 NOTREACHED(); | 149 NOTREACHED(); |
| 140 } | 150 } |
| 141 } | 151 } |
| 142 | 152 |
| 143 void ChromeSyncNotificationBridge::UpdateEnabledTypes( | |
| 144 syncer::ModelTypeSet types) { | |
| 145 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 146 core_->UpdateEnabledTypes(types); | |
| 147 } | |
| 148 | |
| 149 void ChromeSyncNotificationBridge::RegisterHandler( | 153 void ChromeSyncNotificationBridge::RegisterHandler( |
| 150 syncer::InvalidationHandler* handler) { | 154 syncer::InvalidationHandler* handler) { |
| 151 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 155 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 152 core_->RegisterHandler(handler); | 156 core_->RegisterHandler(handler); |
| 153 } | 157 } |
| 154 | 158 |
| 155 void ChromeSyncNotificationBridge::UpdateRegisteredIds( | 159 void ChromeSyncNotificationBridge::UpdateRegisteredIds( |
| 156 syncer::InvalidationHandler* handler, | 160 syncer::InvalidationHandler* handler, |
| 157 const syncer::ObjectIdSet& ids) { | 161 const syncer::ObjectIdSet& ids) { |
| 158 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 162 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 159 core_->UpdateRegisteredIds(handler, ids); | 163 core_->UpdateRegisteredIds(handler, ids); |
| 160 } | 164 } |
| 161 | 165 |
| 162 void ChromeSyncNotificationBridge::UnregisterHandler( | 166 void ChromeSyncNotificationBridge::UnregisterHandler( |
| 163 syncer::InvalidationHandler* handler) { | 167 syncer::InvalidationHandler* handler) { |
| 164 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 168 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 165 core_->UnregisterHandler(handler); | 169 core_->UnregisterHandler(handler); |
| 166 } | 170 } |
| 167 | 171 |
| 172 bool ChromeSyncNotificationBridge::IsHandlerRegisteredForTest( |
| 173 syncer::InvalidationHandler* handler) const { |
| 174 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 175 return core_->IsHandlerRegisteredForTest(handler); |
| 176 } |
| 177 |
| 178 syncer::ObjectIdSet ChromeSyncNotificationBridge::GetRegisteredIdsForTest( |
| 179 syncer::InvalidationHandler* handler) const { |
| 180 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
| 181 return core_->GetRegisteredIdsForTest(handler); |
| 182 } |
| 183 |
| 168 void ChromeSyncNotificationBridge::Observe( | 184 void ChromeSyncNotificationBridge::Observe( |
| 169 int type, | 185 int type, |
| 170 const content::NotificationSource& source, | 186 const content::NotificationSource& source, |
| 171 const content::NotificationDetails& details) { | 187 const content::NotificationDetails& details) { |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 173 | 189 |
| 174 syncer::IncomingNotificationSource notification_source; | 190 syncer::IncomingNotificationSource notification_source; |
| 175 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { | 191 if (type == chrome::NOTIFICATION_SYNC_REFRESH_LOCAL) { |
| 176 notification_source = syncer::LOCAL_NOTIFICATION; | 192 notification_source = syncer::LOCAL_NOTIFICATION; |
| 177 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { | 193 } else if (type == chrome::NOTIFICATION_SYNC_REFRESH_REMOTE) { |
| 178 notification_source = syncer::REMOTE_NOTIFICATION; | 194 notification_source = syncer::REMOTE_NOTIFICATION; |
| 179 } else { | 195 } else { |
| 180 NOTREACHED() << "Unexpected notification type: " << type; | 196 NOTREACHED() << "Unexpected notification type: " << type; |
| 181 return; | 197 return; |
| 182 } | 198 } |
| 183 | 199 |
| 200 // TODO(akalin): Use ObjectIdStateMap here instead. We'll have to |
| 201 // make sure all emitters of the relevant notifications also use |
| 202 // ObjectIdStateMap. |
| 184 content::Details<const syncer::ModelTypeStateMap> | 203 content::Details<const syncer::ModelTypeStateMap> |
| 185 state_details(details); | 204 state_details(details); |
| 186 const syncer::ModelTypeStateMap& state_map = *(state_details.ptr()); | 205 const syncer::ModelTypeStateMap& state_map = *(state_details.ptr()); |
| 187 if (!sync_task_runner_->PostTask( | 206 if (!sync_task_runner_->PostTask( |
| 188 FROM_HERE, | 207 FROM_HERE, |
| 189 base::Bind(&Core::EmitNotification, | 208 base::Bind(&Core::EmitNotification, |
| 190 core_, state_map, notification_source))) { | 209 core_, |
| 210 ModelTypeStateMapToObjectIdStateMap(state_map), |
| 211 notification_source))) { |
| 191 NOTREACHED(); | 212 NOTREACHED(); |
| 192 } | 213 } |
| 193 } | 214 } |
| 194 | 215 |
| 195 } // namespace browser_sync | 216 } // namespace browser_sync |
| OLD | NEW |