Chromium Code Reviews| 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 "sync/notifier/sync_notifier_registrar.h" | |
| 6 | |
| 7 #include <cstddef> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 | |
| 12 namespace syncer { | |
| 13 | |
| 14 SyncNotifierRegistrar::SyncNotifierRegistrar() {} | |
| 15 | |
| 16 SyncNotifierRegistrar::~SyncNotifierRegistrar() { | |
| 17 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 18 } | |
| 19 | |
| 20 void SyncNotifierRegistrar::RegisterHandler(SyncNotifierObserver* handler) { | |
| 21 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 22 CHECK(handler); | |
| 23 CHECK(!handlers_.HasObserver(handler)); | |
| 24 handlers_.AddObserver(handler); | |
| 25 } | |
| 26 | |
| 27 void SyncNotifierRegistrar::UpdateRegisteredIds( | |
| 28 SyncNotifierObserver* handler, | |
| 29 const ObjectIdSet& ids) { | |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 31 CHECK(handler); | |
| 32 CHECK(handlers_.HasObserver(handler)); | |
| 33 // Remove all existing entries for |handler|. | |
| 34 for (IdHandlerMap::iterator it = id_to_handler_map_.begin(); | |
| 35 it != id_to_handler_map_.end(); ) { | |
| 36 if (it->second == handler) { | |
| 37 IdHandlerMap::iterator erase_it = it; | |
| 38 ++it; | |
| 39 id_to_handler_map_.erase(erase_it); | |
| 40 } else { | |
| 41 ++it; | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 // Now add the entries for |handler|. We keep track of the last insertion | |
| 46 // point so we only traverse the map once to insert all the new entries. | |
| 47 IdHandlerMap::iterator insert_it = id_to_handler_map_.begin(); | |
| 48 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | |
| 49 insert_it = | |
| 50 id_to_handler_map_.insert(insert_it, std::make_pair(*it, handler)); | |
| 51 CHECK_EQ(handler, insert_it->second) | |
| 52 << "Duplicate registration: trying to register " | |
| 53 << ObjectIdToString(insert_it->first) << " for " | |
| 54 << handler << " when it's already registered for " | |
| 55 << insert_it->second; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 void SyncNotifierRegistrar::UnregisterHandler(SyncNotifierObserver* handler) { | |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 61 CHECK(handler); | |
| 62 CHECK(handlers_.HasObserver(handler)); | |
| 63 handlers_.RemoveObserver(handler); | |
| 64 } | |
| 65 | |
| 66 ObjectIdSet SyncNotifierRegistrar::GetAllRegisteredIds() const { | |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 68 ObjectIdSet registered_ids; | |
| 69 for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin(); | |
| 70 it != id_to_handler_map_.end(); ++it) { | |
| 71 registered_ids.insert(it->first); | |
| 72 } | |
| 73 return registered_ids; | |
| 74 } | |
| 75 | |
| 76 void SyncNotifierRegistrar::DispatchInvalidationsToHandlers( | |
| 77 const ObjectIdPayloadMap& id_payloads, | |
| 78 IncomingNotificationSource source) { | |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 80 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap; | |
| 81 DispatchMap dispatch_map; | |
| 82 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); | |
| 83 it != id_payloads.end(); ++it) { | |
| 84 SyncNotifierObserver* const handler = ObjectIdToHandler(it->first); | |
| 85 // Filter out invalidations for IDs with no handler. | |
| 86 if (handler) | |
| 87 dispatch_map[handler].insert(*it); | |
| 88 } | |
| 89 | |
| 90 // Emit invalidations only for handlers in |handlers_|. | |
| 91 if (handlers_.might_have_observers()) { | |
|
msw
2012/08/09 05:20:26
nit: consider making this an early return up top,
akalin
2012/08/10 01:28:08
Done.
| |
| 92 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); | |
| 93 SyncNotifierObserver* handler = NULL; | |
| 94 while ((handler = it.GetNext()) != NULL) { | |
| 95 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); | |
| 96 if (dispatch_it != dispatch_map.end()) { | |
|
msw
2012/08/09 05:20:26
nit: remove unnecessary {}
akalin
2012/08/10 01:28:08
Done.
| |
| 97 handler->OnIncomingNotification(dispatch_it->second, source); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void SyncNotifierRegistrar::EmitOnNotificationsEnabled() { | |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 105 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled()); | |
| 106 } | |
| 107 | |
| 108 void SyncNotifierRegistrar::EmitOnNotificationsDisabled( | |
| 109 NotificationsDisabledReason reason) { | |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 111 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, | |
| 112 OnNotificationsDisabled(reason)); | |
| 113 } | |
| 114 | |
| 115 void SyncNotifierRegistrar::DetachFromThreadForTest() { | |
| 116 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 117 thread_checker_.DetachFromThread(); | |
| 118 } | |
| 119 | |
| 120 SyncNotifierObserver* SyncNotifierRegistrar::ObjectIdToHandler( | |
| 121 const invalidation::ObjectId& id) { | |
| 122 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 123 IdHandlerMap::const_iterator it = id_to_handler_map_.find(id); | |
| 124 return (it == id_to_handler_map_.end()) ? NULL : it->second; | |
| 125 } | |
| 126 | |
| 127 } // namespace syncer | |
| OLD | NEW |