| 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/invalidator_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 InvalidatorRegistrar::InvalidatorRegistrar() {} | 14 InvalidatorRegistrar::InvalidatorRegistrar() |
| 15 : state_(DEFAULT_INVALIDATION_ERROR) {} |
| 15 | 16 |
| 16 InvalidatorRegistrar::~InvalidatorRegistrar() { | 17 InvalidatorRegistrar::~InvalidatorRegistrar() { |
| 17 DCHECK(thread_checker_.CalledOnValidThread()); | 18 DCHECK(thread_checker_.CalledOnValidThread()); |
| 18 } | 19 } |
| 19 | 20 |
| 20 void InvalidatorRegistrar::RegisterHandler(InvalidationHandler* handler) { | 21 void InvalidatorRegistrar::RegisterHandler(InvalidationHandler* handler) { |
| 21 DCHECK(thread_checker_.CalledOnValidThread()); | 22 DCHECK(thread_checker_.CalledOnValidThread()); |
| 22 CHECK(handler); | 23 CHECK(handler); |
| 23 CHECK(!handlers_.HasObserver(handler)); | 24 CHECK(!handlers_.HasObserver(handler)); |
| 24 handlers_.AddObserver(handler); | 25 handlers_.AddObserver(handler); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 ObjectIdSet registered_ids; | 82 ObjectIdSet registered_ids; |
| 82 for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin(); | 83 for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin(); |
| 83 it != id_to_handler_map_.end(); ++it) { | 84 it != id_to_handler_map_.end(); ++it) { |
| 84 registered_ids.insert(it->first); | 85 registered_ids.insert(it->first); |
| 85 } | 86 } |
| 86 return registered_ids; | 87 return registered_ids; |
| 87 } | 88 } |
| 88 | 89 |
| 89 void InvalidatorRegistrar::DispatchInvalidationsToHandlers( | 90 void InvalidatorRegistrar::DispatchInvalidationsToHandlers( |
| 90 const ObjectIdStateMap& id_state_map, | 91 const ObjectIdStateMap& id_state_map, |
| 91 IncomingNotificationSource source) { | 92 IncomingInvalidationSource source) { |
| 92 DCHECK(thread_checker_.CalledOnValidThread()); | 93 DCHECK(thread_checker_.CalledOnValidThread()); |
| 93 // If we have no handlers, there's nothing to do. | 94 // If we have no handlers, there's nothing to do. |
| 94 if (!handlers_.might_have_observers()) { | 95 if (!handlers_.might_have_observers()) { |
| 95 return; | 96 return; |
| 96 } | 97 } |
| 97 | 98 |
| 98 typedef std::map<InvalidationHandler*, ObjectIdStateMap> DispatchMap; | 99 typedef std::map<InvalidationHandler*, ObjectIdStateMap> DispatchMap; |
| 99 DispatchMap dispatch_map; | 100 DispatchMap dispatch_map; |
| 100 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); | 101 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); |
| 101 it != id_state_map.end(); ++it) { | 102 it != id_state_map.end(); ++it) { |
| 102 InvalidationHandler* const handler = ObjectIdToHandler(it->first); | 103 InvalidationHandler* const handler = ObjectIdToHandler(it->first); |
| 103 // Filter out invalidations for IDs with no handler. | 104 // Filter out invalidations for IDs with no handler. |
| 104 if (handler) | 105 if (handler) |
| 105 dispatch_map[handler].insert(*it); | 106 dispatch_map[handler].insert(*it); |
| 106 } | 107 } |
| 107 | 108 |
| 108 // Emit invalidations only for handlers in |handlers_|. | 109 // Emit invalidations only for handlers in |handlers_|. |
| 109 ObserverListBase<InvalidationHandler>::Iterator it(handlers_); | 110 ObserverListBase<InvalidationHandler>::Iterator it(handlers_); |
| 110 InvalidationHandler* handler = NULL; | 111 InvalidationHandler* handler = NULL; |
| 111 while ((handler = it.GetNext()) != NULL) { | 112 while ((handler = it.GetNext()) != NULL) { |
| 112 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); | 113 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); |
| 113 if (dispatch_it != dispatch_map.end()) | 114 if (dispatch_it != dispatch_map.end()) |
| 114 handler->OnIncomingNotification(dispatch_it->second, source); | 115 handler->OnIncomingInvalidation(dispatch_it->second, source); |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 | 118 |
| 118 void InvalidatorRegistrar::EmitOnNotificationsEnabled() { | 119 void InvalidatorRegistrar::UpdateInvalidatorState(InvalidatorState state) { |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | 120 DCHECK(thread_checker_.CalledOnValidThread()); |
| 120 FOR_EACH_OBSERVER(InvalidationHandler, handlers_, OnNotificationsEnabled()); | 121 state_ = state; |
| 122 FOR_EACH_OBSERVER(InvalidationHandler, handlers_, |
| 123 OnInvalidatorStateChange(state)); |
| 121 } | 124 } |
| 122 | 125 |
| 123 void InvalidatorRegistrar::EmitOnNotificationsDisabled( | 126 InvalidatorState InvalidatorRegistrar::GetInvalidatorState() const { |
| 124 NotificationsDisabledReason reason) { | |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | 127 DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 FOR_EACH_OBSERVER(InvalidationHandler, handlers_, | 128 return state_; |
| 127 OnNotificationsDisabled(reason)); | |
| 128 } | 129 } |
| 129 | 130 |
| 130 bool InvalidatorRegistrar::IsHandlerRegisteredForTest( | 131 bool InvalidatorRegistrar::IsHandlerRegisteredForTest( |
| 131 InvalidationHandler* handler) const { | 132 InvalidationHandler* handler) const { |
| 132 DCHECK(thread_checker_.CalledOnValidThread()); | 133 DCHECK(thread_checker_.CalledOnValidThread()); |
| 133 return handlers_.HasObserver(handler); | 134 return handlers_.HasObserver(handler); |
| 134 } | 135 } |
| 135 | 136 |
| 136 void InvalidatorRegistrar::DetachFromThreadForTest() { | 137 void InvalidatorRegistrar::DetachFromThreadForTest() { |
| 137 DCHECK(thread_checker_.CalledOnValidThread()); | 138 DCHECK(thread_checker_.CalledOnValidThread()); |
| 138 thread_checker_.DetachFromThread(); | 139 thread_checker_.DetachFromThread(); |
| 139 } | 140 } |
| 140 | 141 |
| 141 InvalidationHandler* InvalidatorRegistrar::ObjectIdToHandler( | 142 InvalidationHandler* InvalidatorRegistrar::ObjectIdToHandler( |
| 142 const invalidation::ObjectId& id) { | 143 const invalidation::ObjectId& id) { |
| 143 DCHECK(thread_checker_.CalledOnValidThread()); | 144 DCHECK(thread_checker_.CalledOnValidThread()); |
| 144 IdHandlerMap::const_iterator it = id_to_handler_map_.find(id); | 145 IdHandlerMap::const_iterator it = id_to_handler_map_.find(id); |
| 145 return (it == id_to_handler_map_.end()) ? NULL : it->second; | 146 return (it == id_to_handler_map_.end()) ? NULL : it->second; |
| 146 } | 147 } |
| 147 | 148 |
| 148 } // namespace syncer | 149 } // namespace syncer |
| OLD | NEW |