| 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/android_invalidator_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 AndroidInvalidatorBridge::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 | |
| 42 bool IsHandlerRegisteredForTest(syncer::InvalidationHandler* handler) const; | |
| 43 syncer::ObjectIdSet GetRegisteredIdsForTest( | |
| 44 syncer::InvalidationHandler* handler) const; | |
| 45 | |
| 46 private: | |
| 47 friend class base::RefCountedThreadSafe<Core>; | |
| 48 | |
| 49 // Destroyed on the UI thread or on |sync_task_runner_|. | |
| 50 ~Core(); | |
| 51 | |
| 52 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; | |
| 53 | |
| 54 // Used only on |sync_task_runner_|. | |
| 55 scoped_ptr<syncer::InvalidatorRegistrar> invalidator_registrar_; | |
| 56 }; | |
| 57 | |
| 58 AndroidInvalidatorBridge::Core::Core( | |
| 59 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | |
| 60 : sync_task_runner_(sync_task_runner) { | |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 62 DCHECK(sync_task_runner_.get()); | |
| 63 } | |
| 64 | |
| 65 AndroidInvalidatorBridge::Core::~Core() { | |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | |
| 67 sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 68 DCHECK(!invalidator_registrar_.get()); | |
| 69 } | |
| 70 | |
| 71 void AndroidInvalidatorBridge::Core::InitializeOnSyncThread() { | |
| 72 invalidator_registrar_.reset(new syncer::InvalidatorRegistrar()); | |
| 73 } | |
| 74 | |
| 75 void AndroidInvalidatorBridge::Core::CleanupOnSyncThread() { | |
| 76 invalidator_registrar_.reset(); | |
| 77 } | |
| 78 | |
| 79 void AndroidInvalidatorBridge::Core::RegisterHandler( | |
| 80 syncer::InvalidationHandler* handler) { | |
| 81 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 82 invalidator_registrar_->RegisterHandler(handler); | |
| 83 } | |
| 84 | |
| 85 void AndroidInvalidatorBridge::Core::UpdateRegisteredIds( | |
| 86 syncer::InvalidationHandler* handler, | |
| 87 const syncer::ObjectIdSet& ids) { | |
| 88 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 89 invalidator_registrar_->UpdateRegisteredIds(handler, ids); | |
| 90 } | |
| 91 | |
| 92 void AndroidInvalidatorBridge::Core::UnregisterHandler( | |
| 93 syncer::InvalidationHandler* handler) { | |
| 94 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 95 invalidator_registrar_->UnregisterHandler(handler); | |
| 96 } | |
| 97 | |
| 98 void AndroidInvalidatorBridge::Core::EmitInvalidation( | |
| 99 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
| 100 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 101 const syncer::ObjectIdInvalidationMap& effective_invalidation_map = | |
| 102 invalidation_map.empty() ? | |
| 103 ObjectIdSetToInvalidationMap( | |
| 104 invalidator_registrar_->GetAllRegisteredIds(), std::string()) : | |
| 105 invalidation_map; | |
| 106 | |
| 107 invalidator_registrar_->DispatchInvalidationsToHandlers( | |
| 108 effective_invalidation_map); | |
| 109 } | |
| 110 | |
| 111 bool AndroidInvalidatorBridge::Core::IsHandlerRegisteredForTest( | |
| 112 syncer::InvalidationHandler* handler) const { | |
| 113 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 114 return invalidator_registrar_->IsHandlerRegisteredForTest(handler); | |
| 115 } | |
| 116 | |
| 117 syncer::ObjectIdSet | |
| 118 AndroidInvalidatorBridge::Core::GetRegisteredIdsForTest( | |
| 119 syncer::InvalidationHandler* handler) const { | |
| 120 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 121 return invalidator_registrar_->GetRegisteredIds(handler); | |
| 122 } | |
| 123 | |
| 124 AndroidInvalidatorBridge::AndroidInvalidatorBridge( | |
| 125 const Profile* profile, | |
| 126 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | |
| 127 : sync_task_runner_(sync_task_runner), | |
| 128 core_(new Core(sync_task_runner_)) { | |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 130 DCHECK(profile); | |
| 131 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | |
| 132 content::Source<Profile>(profile)); | |
| 133 | |
| 134 if (!sync_task_runner_->PostTask( | |
| 135 FROM_HERE, base::Bind(&Core::InitializeOnSyncThread, core_))) { | |
| 136 NOTREACHED(); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 AndroidInvalidatorBridge::~AndroidInvalidatorBridge() {} | |
| 141 | |
| 142 void AndroidInvalidatorBridge::StopForShutdown() { | |
| 143 if (!sync_task_runner_->PostTask( | |
| 144 FROM_HERE, base::Bind(&Core::CleanupOnSyncThread, core_))) { | |
| 145 NOTREACHED(); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 void AndroidInvalidatorBridge::RegisterHandler( | |
| 150 syncer::InvalidationHandler* handler) { | |
| 151 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 152 core_->RegisterHandler(handler); | |
| 153 } | |
| 154 | |
| 155 void AndroidInvalidatorBridge::UpdateRegisteredIds( | |
| 156 syncer::InvalidationHandler* handler, | |
| 157 const syncer::ObjectIdSet& ids) { | |
| 158 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 159 core_->UpdateRegisteredIds(handler, ids); | |
| 160 } | |
| 161 | |
| 162 void AndroidInvalidatorBridge::UnregisterHandler( | |
| 163 syncer::InvalidationHandler* handler) { | |
| 164 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 165 core_->UnregisterHandler(handler); | |
| 166 } | |
| 167 | |
| 168 void AndroidInvalidatorBridge::Acknowledge( | |
| 169 const invalidation::ObjectId& id, const syncer::AckHandle& ack_handle) { | |
| 170 // Do nothing. | |
| 171 } | |
| 172 | |
| 173 syncer::InvalidatorState AndroidInvalidatorBridge::GetInvalidatorState() const { | |
| 174 return syncer::INVALIDATIONS_ENABLED; | |
| 175 } | |
| 176 | |
| 177 void AndroidInvalidatorBridge::UpdateCredentials( | |
| 178 const std::string& email, const std::string& token) { } | |
| 179 | |
| 180 void AndroidInvalidatorBridge::SendInvalidation( | |
| 181 const syncer::ObjectIdInvalidationMap& invalidation_map) { } | |
| 182 | |
| 183 bool AndroidInvalidatorBridge::IsHandlerRegisteredForTest( | |
| 184 syncer::InvalidationHandler* handler) const { | |
| 185 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 186 return core_->IsHandlerRegisteredForTest(handler); | |
| 187 } | |
| 188 | |
| 189 syncer::ObjectIdSet AndroidInvalidatorBridge::GetRegisteredIdsForTest( | |
| 190 syncer::InvalidationHandler* handler) const { | |
| 191 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | |
| 192 return core_->GetRegisteredIdsForTest(handler); | |
| 193 } | |
| 194 | |
| 195 void AndroidInvalidatorBridge::Observe( | |
| 196 int type, | |
| 197 const content::NotificationSource& source, | |
| 198 const content::NotificationDetails& details) { | |
| 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 200 DCHECK_EQ(type, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE); | |
| 201 | |
| 202 // TODO(akalin): Use ObjectIdInvalidationMap here instead. We'll have to | |
| 203 // make sure all emitters of the relevant notifications also use | |
| 204 // ObjectIdInvalidationMap. | |
| 205 content::Details<const syncer::ModelTypeInvalidationMap> | |
| 206 state_details(details); | |
| 207 const syncer::ModelTypeInvalidationMap& invalidation_map = | |
| 208 *(state_details.ptr()); | |
| 209 if (!sync_task_runner_->PostTask( | |
| 210 FROM_HERE, | |
| 211 base::Bind(&Core::EmitInvalidation, | |
| 212 core_, | |
| 213 ModelTypeInvalidationMapToObjectIdInvalidationMap( | |
| 214 invalidation_map)))) { | |
| 215 NOTREACHED(); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 } // namespace browser_sync | |
| OLD | NEW |