| 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 ObjectIdStateMap& id_state_map, | |
| 78 IncomingNotificationSource source) { | |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 80 // If we have no handlers, there's nothing to do. | |
| 81 if (!handlers_.might_have_observers()) { | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 typedef std::map<SyncNotifierObserver*, ObjectIdStateMap> DispatchMap; | |
| 86 DispatchMap dispatch_map; | |
| 87 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); | |
| 88 it != id_state_map.end(); ++it) { | |
| 89 SyncNotifierObserver* const handler = ObjectIdToHandler(it->first); | |
| 90 // Filter out invalidations for IDs with no handler. | |
| 91 if (handler) | |
| 92 dispatch_map[handler].insert(*it); | |
| 93 } | |
| 94 | |
| 95 // Emit invalidations only for handlers in |handlers_|. | |
| 96 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); | |
| 97 SyncNotifierObserver* handler = NULL; | |
| 98 while ((handler = it.GetNext()) != NULL) { | |
| 99 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); | |
| 100 if (dispatch_it != dispatch_map.end()) | |
| 101 handler->OnIncomingNotification(dispatch_it->second, source); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void SyncNotifierRegistrar::EmitOnNotificationsEnabled() { | |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 107 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled()); | |
| 108 } | |
| 109 | |
| 110 void SyncNotifierRegistrar::EmitOnNotificationsDisabled( | |
| 111 NotificationsDisabledReason reason) { | |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 113 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, | |
| 114 OnNotificationsDisabled(reason)); | |
| 115 } | |
| 116 | |
| 117 bool SyncNotifierRegistrar::IsHandlerRegisteredForTest( | |
| 118 SyncNotifierObserver* handler) const { | |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 120 return handlers_.HasObserver(handler); | |
| 121 } | |
| 122 | |
| 123 ObjectIdSet SyncNotifierRegistrar::GetRegisteredIdsForTest( | |
| 124 SyncNotifierObserver* handler) const { | |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 126 ObjectIdSet registered_ids; | |
| 127 for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin(); | |
| 128 it != id_to_handler_map_.end(); ++it) { | |
| 129 if (it->second == handler) { | |
| 130 registered_ids.insert(it->first); | |
| 131 } | |
| 132 } | |
| 133 return registered_ids; | |
| 134 } | |
| 135 | |
| 136 void SyncNotifierRegistrar::DetachFromThreadForTest() { | |
| 137 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 138 thread_checker_.DetachFromThread(); | |
| 139 } | |
| 140 | |
| 141 SyncNotifierObserver* SyncNotifierRegistrar::ObjectIdToHandler( | |
| 142 const invalidation::ObjectId& id) { | |
| 143 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 144 IdHandlerMap::const_iterator it = id_to_handler_map_.find(id); | |
| 145 return (it == id_to_handler_map_.end()) ? NULL : it->second; | |
| 146 } | |
| 147 | |
| 148 } // namespace syncer | |
| OLD | NEW |