Chromium Code Reviews| 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_helper.h" | 5 #include "sync/notifier/sync_notifier_helper.h" |
| 6 | 6 |
| 7 #include <cstddef> | |
| 8 | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 | 10 |
| 9 namespace syncer { | 11 namespace syncer { |
| 10 | 12 |
| 11 SyncNotifierHelper::SyncNotifierHelper() {} | 13 SyncNotifierHelper::SyncNotifierHelper() {} |
| 12 SyncNotifierHelper::~SyncNotifierHelper() {} | |
| 13 | 14 |
| 14 ObjectIdSet SyncNotifierHelper::UpdateRegisteredIds( | 15 SyncNotifierHelper::~SyncNotifierHelper() { |
| 15 SyncNotifierObserver* handler, const ObjectIdSet& ids) { | 16 DCHECK(thread_checker_.CalledOnValidThread()); |
| 16 if (ids.empty()) { | 17 } |
| 17 handlers_.RemoveObserver(handler); | 18 |
| 18 } else if (!handlers_.HasObserver(handler)) { | 19 void SyncNotifierHelper::SetHandler(const std::string& handler_name, |
| 19 handlers_.AddObserver(handler); | 20 SyncNotifierObserver* handler) { |
| 21 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 22 SyncNotifierObserver* const old_handler = | |
| 23 HandlerNameToHandler(handler_name); | |
| 24 if (old_handler) { | |
| 25 handlers_.RemoveObserver(old_handler); | |
| 20 } | 26 } |
| 21 | 27 |
| 22 ObjectIdSet registered_ids(ids); | 28 if (handler) { |
| 23 // Remove all existing entries for |handler| and fill |registered_ids| with | 29 handlers_.AddObserver(handler); |
| 24 // the rest. | 30 name_to_handler_map_[handler_name] = handler; |
| 25 for (ObjectIdHandlerMap::iterator it = id_to_handler_map_.begin(); | 31 } else { |
| 26 it != id_to_handler_map_.end(); ) { | 32 handlers_.RemoveObserver(old_handler); |
|
dcheng
2012/08/03 06:05:30
I don't think you need this line, since it's alway
akalin
2012/08/03 18:44:10
Done. In fact, the entire else block can be moved
| |
| 27 if (it->second == handler) { | 33 name_to_handler_map_.erase(handler_name); |
| 28 ObjectIdHandlerMap::iterator erase_it = it; | 34 } |
| 35 } | |
| 36 | |
| 37 void SyncNotifierHelper::UpdateRegisteredIds( | |
| 38 const std::string& handler_name, const ObjectIdSet& ids) { | |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 40 // Remove all existing entries for |handler_name|. | |
| 41 for (ObjectIdNameMap::iterator it = id_to_name_map_.begin(); | |
| 42 it != id_to_name_map_.end(); ) { | |
| 43 if (it->second == handler_name) { | |
| 44 ObjectIdNameMap::iterator erase_it = it; | |
| 29 ++it; | 45 ++it; |
| 30 id_to_handler_map_.erase(erase_it); | 46 id_to_name_map_.erase(erase_it); |
| 31 } else { | 47 } else { |
| 32 registered_ids.insert(it->first); | |
| 33 ++it; | 48 ++it; |
| 34 } | 49 } |
| 35 } | 50 } |
| 36 | 51 |
| 37 // Now add the entries for |handler|. We keep track of the last insertion | 52 // Now add the entries for |handler_name|. We keep track of the last |
| 38 // point so we only traverse the map once to insert all the new entries. | 53 // insertion point so we only traverse the map once to insert all |
| 39 ObjectIdHandlerMap::iterator insert_it = id_to_handler_map_.begin(); | 54 // the new entries. |
| 55 ObjectIdNameMap::iterator insert_it = id_to_name_map_.begin(); | |
| 40 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | 56 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { |
| 41 insert_it = id_to_handler_map_.insert(insert_it, | 57 insert_it = |
| 42 std::make_pair(*it, handler)); | 58 id_to_name_map_.insert(insert_it, std::make_pair(*it, handler_name)); |
| 43 CHECK_EQ(handler, insert_it->second) << "Duplicate registration for " | 59 CHECK_EQ(handler_name, insert_it->second) |
| 44 << ObjectIdToString(insert_it->first); | 60 << "Duplicate registration: trying to register " |
| 61 << ObjectIdToString(insert_it->first) << " for " | |
| 62 << handler_name << " when it's already registered for " | |
| 63 << insert_it->second; | |
| 45 } | 64 } |
| 65 | |
| 46 if (logging::DEBUG_MODE) { | 66 if (logging::DEBUG_MODE) { |
| 47 // The mapping shouldn't contain any handlers that aren't in |handlers_|. | 67 // The mapping shouldn't contain any handlers that aren't in |handlers_|. |
| 48 for (ObjectIdHandlerMap::const_iterator it = id_to_handler_map_.begin(); | 68 for (ObjectIdNameMap::const_iterator it = id_to_name_map_.begin(); |
| 49 it != id_to_handler_map_.end(); ++it) { | 69 it != id_to_name_map_.end(); ++it) { |
| 50 CHECK(handlers_.HasObserver(it->second)); | 70 SyncNotifierObserver* const handler = HandlerNameToHandler(it->second); |
| 71 if (handler) { | |
| 72 CHECK(handlers_.HasObserver(handler)); | |
| 73 } | |
| 51 } | 74 } |
| 52 } | 75 } |
| 76 } | |
| 77 | |
| 78 ObjectIdSet SyncNotifierHelper::GetAllRegisteredIds() const { | |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 80 ObjectIdSet registered_ids; | |
| 81 for (ObjectIdNameMap::const_iterator it = id_to_name_map_.begin(); | |
| 82 it != id_to_name_map_.end(); ++it) { | |
| 83 registered_ids.insert(it->first); | |
| 84 } | |
| 53 return registered_ids; | 85 return registered_ids; |
| 54 } | 86 } |
| 55 | 87 |
| 56 void SyncNotifierHelper::DispatchInvalidationsToHandlers( | 88 void SyncNotifierHelper::DispatchInvalidationsToHandlers( |
| 57 const ObjectIdPayloadMap& id_payloads, | 89 const ObjectIdPayloadMap& id_payloads, |
| 58 IncomingNotificationSource source) { | 90 IncomingNotificationSource source) { |
| 91 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 59 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap; | 92 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap; |
| 60 DispatchMap dispatch_map; | 93 DispatchMap dispatch_map; |
| 61 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); | 94 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); |
| 62 it != id_payloads.end(); ++it) { | 95 it != id_payloads.end(); ++it) { |
| 63 ObjectIdHandlerMap::const_iterator find_it = | 96 SyncNotifierObserver* const handler = ObjectIdToHandler(it->first); |
| 64 id_to_handler_map_.find(it->first); | 97 // If we get an invalidation with a source type that we can't map |
| 65 // If we get an invalidation with a source type that we can't map to an | 98 // to an handler, just drop it -- the handler was unregistered |
| 66 // observer, just drop it--the observer was unregistered while the | 99 // while the invalidation was in flight. |
| 67 // invalidation was in flight. | 100 if (!handler) { |
| 68 if (find_it == id_to_handler_map_.end()) | |
| 69 continue; | 101 continue; |
| 70 dispatch_map[find_it->second].insert(*it); | 102 } |
| 103 dispatch_map[handler].insert(*it); | |
| 71 } | 104 } |
| 72 | 105 |
| 73 if (handlers_.might_have_observers()) { | 106 if (handlers_.might_have_observers()) { |
| 74 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); | 107 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); |
| 75 SyncNotifierObserver* handler = NULL; | 108 SyncNotifierObserver* handler = NULL; |
| 76 while ((handler = it.GetNext()) != NULL) { | 109 while ((handler = it.GetNext()) != NULL) { |
| 77 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); | 110 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); |
| 78 if (dispatch_it != dispatch_map.end()) { | 111 if (dispatch_it != dispatch_map.end()) { |
| 79 handler->OnIncomingNotification(dispatch_it->second, source); | 112 handler->OnIncomingNotification(dispatch_it->second, source); |
| 80 } | 113 } |
| 81 } | 114 } |
| 82 } | 115 } |
| 83 } | 116 } |
| 84 | 117 |
| 85 void SyncNotifierHelper::EmitOnNotificationsEnabled() { | 118 void SyncNotifierHelper::EmitOnNotificationsEnabled() { |
| 86 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled()); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
| 120 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, | |
| 121 OnNotificationsEnabled()); | |
| 87 } | 122 } |
| 88 | 123 |
| 89 void SyncNotifierHelper::EmitOnNotificationsDisabled( | 124 void SyncNotifierHelper::EmitOnNotificationsDisabled( |
| 90 NotificationsDisabledReason reason) { | 125 NotificationsDisabledReason reason) { |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 91 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, | 127 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, |
| 92 OnNotificationsDisabled(reason)); | 128 OnNotificationsDisabled(reason)); |
| 93 } | 129 } |
| 94 | 130 |
| 131 void SyncNotifierHelper::DetachFromThreadForTest() { | |
| 132 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 133 thread_checker_.DetachFromThread(); | |
| 134 } | |
| 135 | |
| 136 SyncNotifierObserver* SyncNotifierHelper::HandlerNameToHandler( | |
| 137 const std::string& handler_name) const { | |
| 138 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 139 NameHandlerMap::const_iterator it = | |
| 140 name_to_handler_map_.find(handler_name); | |
| 141 return (it == name_to_handler_map_.end()) ? NULL : it->second; | |
| 142 } | |
| 143 | |
| 144 SyncNotifierObserver* SyncNotifierHelper::ObjectIdToHandler( | |
| 145 const invalidation::ObjectId& id) const { | |
| 146 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 147 ObjectIdNameMap::const_iterator it = id_to_name_map_.find(id); | |
| 148 return | |
| 149 (it == id_to_name_map_.end()) ? NULL : HandlerNameToHandler(it->second); | |
| 150 } | |
| 151 | |
| 95 } // namespace syncer | 152 } // namespace syncer |
| OLD | NEW |