| 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 "sync/notifier/sync_notifier_registrar.h" | 5 #include "sync/notifier/invalidator_registrar.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 | 11 |
| 12 namespace syncer { | 12 namespace syncer { |
| 13 | 13 |
| 14 SyncNotifierRegistrar::SyncNotifierRegistrar() {} | 14 InvalidatorRegistrar::InvalidatorRegistrar() {} |
| 15 | 15 |
| 16 SyncNotifierRegistrar::~SyncNotifierRegistrar() { | 16 InvalidatorRegistrar::~InvalidatorRegistrar() { |
| 17 DCHECK(thread_checker_.CalledOnValidThread()); | 17 DCHECK(thread_checker_.CalledOnValidThread()); |
| 18 } | 18 } |
| 19 | 19 |
| 20 void SyncNotifierRegistrar::RegisterHandler(SyncNotifierObserver* handler) { | 20 void InvalidatorRegistrar::RegisterHandler(InvalidationHandler* handler) { |
| 21 DCHECK(thread_checker_.CalledOnValidThread()); | 21 DCHECK(thread_checker_.CalledOnValidThread()); |
| 22 CHECK(handler); | 22 CHECK(handler); |
| 23 CHECK(!handlers_.HasObserver(handler)); | 23 CHECK(!handlers_.HasObserver(handler)); |
| 24 handlers_.AddObserver(handler); | 24 handlers_.AddObserver(handler); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void SyncNotifierRegistrar::UpdateRegisteredIds( | 27 void InvalidatorRegistrar::UpdateRegisteredIds( |
| 28 SyncNotifierObserver* handler, | 28 InvalidationHandler* handler, |
| 29 const ObjectIdSet& ids) { | 29 const ObjectIdSet& ids) { |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); | 30 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 CHECK(handler); | 31 CHECK(handler); |
| 32 CHECK(handlers_.HasObserver(handler)); | 32 CHECK(handlers_.HasObserver(handler)); |
| 33 // Remove all existing entries for |handler|. | 33 // Remove all existing entries for |handler|. |
| 34 for (IdHandlerMap::iterator it = id_to_handler_map_.begin(); | 34 for (IdHandlerMap::iterator it = id_to_handler_map_.begin(); |
| 35 it != id_to_handler_map_.end(); ) { | 35 it != id_to_handler_map_.end(); ) { |
| 36 if (it->second == handler) { | 36 if (it->second == handler) { |
| 37 IdHandlerMap::iterator erase_it = it; | 37 IdHandlerMap::iterator erase_it = it; |
| 38 ++it; | 38 ++it; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 49 insert_it = | 49 insert_it = |
| 50 id_to_handler_map_.insert(insert_it, std::make_pair(*it, handler)); | 50 id_to_handler_map_.insert(insert_it, std::make_pair(*it, handler)); |
| 51 CHECK_EQ(handler, insert_it->second) | 51 CHECK_EQ(handler, insert_it->second) |
| 52 << "Duplicate registration: trying to register " | 52 << "Duplicate registration: trying to register " |
| 53 << ObjectIdToString(insert_it->first) << " for " | 53 << ObjectIdToString(insert_it->first) << " for " |
| 54 << handler << " when it's already registered for " | 54 << handler << " when it's already registered for " |
| 55 << insert_it->second; | 55 << insert_it->second; |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 void SyncNotifierRegistrar::UnregisterHandler(SyncNotifierObserver* handler) { | 59 void InvalidatorRegistrar::UnregisterHandler(InvalidationHandler* handler) { |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | 60 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 CHECK(handler); | 61 CHECK(handler); |
| 62 CHECK(handlers_.HasObserver(handler)); | 62 CHECK(handlers_.HasObserver(handler)); |
| 63 handlers_.RemoveObserver(handler); | 63 handlers_.RemoveObserver(handler); |
| 64 } | 64 } |
| 65 | 65 |
| 66 ObjectIdSet SyncNotifierRegistrar::GetAllRegisteredIds() const { | 66 ObjectIdSet InvalidatorRegistrar::GetAllRegisteredIds() const { |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); | 67 DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 ObjectIdSet registered_ids; | 68 ObjectIdSet registered_ids; |
| 69 for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin(); | 69 for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin(); |
| 70 it != id_to_handler_map_.end(); ++it) { | 70 it != id_to_handler_map_.end(); ++it) { |
| 71 registered_ids.insert(it->first); | 71 registered_ids.insert(it->first); |
| 72 } | 72 } |
| 73 return registered_ids; | 73 return registered_ids; |
| 74 } | 74 } |
| 75 | 75 |
| 76 void SyncNotifierRegistrar::DispatchInvalidationsToHandlers( | 76 void InvalidatorRegistrar::DispatchInvalidationsToHandlers( |
| 77 const ObjectIdStateMap& id_state_map, | 77 const ObjectIdStateMap& id_state_map, |
| 78 IncomingNotificationSource source) { | 78 IncomingNotificationSource source) { |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); | 79 DCHECK(thread_checker_.CalledOnValidThread()); |
| 80 // If we have no handlers, there's nothing to do. | 80 // If we have no handlers, there's nothing to do. |
| 81 if (!handlers_.might_have_observers()) { | 81 if (!handlers_.might_have_observers()) { |
| 82 return; | 82 return; |
| 83 } | 83 } |
| 84 | 84 |
| 85 typedef std::map<SyncNotifierObserver*, ObjectIdStateMap> DispatchMap; | 85 typedef std::map<InvalidationHandler*, ObjectIdStateMap> DispatchMap; |
| 86 DispatchMap dispatch_map; | 86 DispatchMap dispatch_map; |
| 87 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); | 87 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); |
| 88 it != id_state_map.end(); ++it) { | 88 it != id_state_map.end(); ++it) { |
| 89 SyncNotifierObserver* const handler = ObjectIdToHandler(it->first); | 89 InvalidationHandler* const handler = ObjectIdToHandler(it->first); |
| 90 // Filter out invalidations for IDs with no handler. | 90 // Filter out invalidations for IDs with no handler. |
| 91 if (handler) | 91 if (handler) |
| 92 dispatch_map[handler].insert(*it); | 92 dispatch_map[handler].insert(*it); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Emit invalidations only for handlers in |handlers_|. | 95 // Emit invalidations only for handlers in |handlers_|. |
| 96 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); | 96 ObserverListBase<InvalidationHandler>::Iterator it(handlers_); |
| 97 SyncNotifierObserver* handler = NULL; | 97 InvalidationHandler* handler = NULL; |
| 98 while ((handler = it.GetNext()) != NULL) { | 98 while ((handler = it.GetNext()) != NULL) { |
| 99 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); | 99 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); |
| 100 if (dispatch_it != dispatch_map.end()) | 100 if (dispatch_it != dispatch_map.end()) |
| 101 handler->OnIncomingNotification(dispatch_it->second, source); | 101 handler->OnIncomingNotification(dispatch_it->second, source); |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 void SyncNotifierRegistrar::EmitOnNotificationsEnabled() { | 105 void InvalidatorRegistrar::EmitOnNotificationsEnabled() { |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
| 107 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled()); | 107 FOR_EACH_OBSERVER(InvalidationHandler, handlers_, OnNotificationsEnabled()); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void SyncNotifierRegistrar::EmitOnNotificationsDisabled( | 110 void InvalidatorRegistrar::EmitOnNotificationsDisabled( |
| 111 NotificationsDisabledReason reason) { | 111 NotificationsDisabledReason reason) { |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 113 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, | 113 FOR_EACH_OBSERVER(InvalidationHandler, handlers_, |
| 114 OnNotificationsDisabled(reason)); | 114 OnNotificationsDisabled(reason)); |
| 115 } | 115 } |
| 116 | 116 |
| 117 bool SyncNotifierRegistrar::IsHandlerRegisteredForTest( | 117 bool InvalidatorRegistrar::IsHandlerRegisteredForTest( |
| 118 SyncNotifierObserver* handler) const { | 118 InvalidationHandler* handler) const { |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
| 120 return handlers_.HasObserver(handler); | 120 return handlers_.HasObserver(handler); |
| 121 } | 121 } |
| 122 | 122 |
| 123 ObjectIdSet SyncNotifierRegistrar::GetRegisteredIdsForTest( | 123 ObjectIdSet InvalidatorRegistrar::GetRegisteredIdsForTest( |
| 124 SyncNotifierObserver* handler) const { | 124 InvalidationHandler* handler) const { |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 ObjectIdSet registered_ids; | 126 ObjectIdSet registered_ids; |
| 127 for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin(); | 127 for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin(); |
| 128 it != id_to_handler_map_.end(); ++it) { | 128 it != id_to_handler_map_.end(); ++it) { |
| 129 if (it->second == handler) { | 129 if (it->second == handler) { |
| 130 registered_ids.insert(it->first); | 130 registered_ids.insert(it->first); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 return registered_ids; | 133 return registered_ids; |
| 134 } | 134 } |
| 135 | 135 |
| 136 void SyncNotifierRegistrar::DetachFromThreadForTest() { | 136 void InvalidatorRegistrar::DetachFromThreadForTest() { |
| 137 DCHECK(thread_checker_.CalledOnValidThread()); | 137 DCHECK(thread_checker_.CalledOnValidThread()); |
| 138 thread_checker_.DetachFromThread(); | 138 thread_checker_.DetachFromThread(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 SyncNotifierObserver* SyncNotifierRegistrar::ObjectIdToHandler( | 141 InvalidationHandler* InvalidatorRegistrar::ObjectIdToHandler( |
| 142 const invalidation::ObjectId& id) { | 142 const invalidation::ObjectId& id) { |
| 143 DCHECK(thread_checker_.CalledOnValidThread()); | 143 DCHECK(thread_checker_.CalledOnValidThread()); |
| 144 IdHandlerMap::const_iterator it = id_to_handler_map_.find(id); | 144 IdHandlerMap::const_iterator it = id_to_handler_map_.find(id); |
| 145 return (it == id_to_handler_map_.end()) ? NULL : it->second; | 145 return (it == id_to_handler_map_.end()) ? NULL : it->second; |
| 146 } | 146 } |
| 147 | 147 |
| 148 } // namespace syncer | 148 } // namespace syncer |
| OLD | NEW |