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