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_helper.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace syncer { |
| 10 |
| 11 SyncNotifierHelper::SyncNotifierHelper() {} |
| 12 SyncNotifierHelper::~SyncNotifierHelper() {} |
| 13 |
| 14 ObjectIdSet SyncNotifierHelper::UpdateRegisteredIds( |
| 15 SyncNotifierObserver* handler, const ObjectIdSet& ids) { |
| 16 if (ids.empty()) { |
| 17 handlers_.RemoveObserver(handler); |
| 18 } else if (!handlers_.HasObserver(handler)) { |
| 19 handlers_.AddObserver(handler); |
| 20 } |
| 21 |
| 22 ObjectIdSet registered_ids(ids); |
| 23 // Remove all existing entries for |handler| and fill |registered_ids| with |
| 24 // the rest. |
| 25 for (ObjectIdHandlerMap::iterator it = id_to_handler_map_.begin(); |
| 26 it != id_to_handler_map_.end(); ) { |
| 27 if (it->second == handler) { |
| 28 ObjectIdHandlerMap::iterator erase_it = it; |
| 29 ++it; |
| 30 id_to_handler_map_.erase(erase_it); |
| 31 } else { |
| 32 registered_ids.insert(it->first); |
| 33 ++it; |
| 34 } |
| 35 } |
| 36 |
| 37 // Now add the entries for |handler|. We keep track of the last insertion |
| 38 // point so we only traverse the map once to insert all the new entries. |
| 39 ObjectIdHandlerMap::iterator insert_it = id_to_handler_map_.begin(); |
| 40 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { |
| 41 insert_it = id_to_handler_map_.insert(insert_it, |
| 42 std::make_pair(*it, handler)); |
| 43 CHECK_EQ(handler, insert_it->second) << "Duplicate registration for " |
| 44 << ObjectIdToString(insert_it->first); |
| 45 } |
| 46 if (logging::DEBUG_MODE) { |
| 47 // The mapping shouldn't contain any handlers that aren't in |handlers_|. |
| 48 for (ObjectIdHandlerMap::const_iterator it = id_to_handler_map_.begin(); |
| 49 it != id_to_handler_map_.end(); ++it) { |
| 50 CHECK(handlers_.HasObserver(it->second)); |
| 51 } |
| 52 } |
| 53 return registered_ids; |
| 54 } |
| 55 |
| 56 void SyncNotifierHelper::DispatchInvalidationsToHandlers( |
| 57 const ObjectIdPayloadMap& id_payloads, |
| 58 IncomingNotificationSource source) { |
| 59 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap; |
| 60 DispatchMap dispatch_map; |
| 61 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); |
| 62 it != id_payloads.end(); ++it) { |
| 63 ObjectIdHandlerMap::const_iterator find_it = |
| 64 id_to_handler_map_.find(it->first); |
| 65 // If we get an invalidation with a source type that we can't map to an |
| 66 // observer, just drop it--the observer was unregistered while the |
| 67 // invalidation was in flight. |
| 68 if (find_it == id_to_handler_map_.end()) |
| 69 continue; |
| 70 dispatch_map[find_it->second].insert(*it); |
| 71 } |
| 72 |
| 73 if (handlers_.might_have_observers()) { |
| 74 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); |
| 75 SyncNotifierObserver* handler = NULL; |
| 76 while ((handler = it.GetNext()) != NULL) { |
| 77 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); |
| 78 if (dispatch_it != dispatch_map.end()) { |
| 79 handler->OnIncomingNotification(dispatch_it->second, source); |
| 80 } |
| 81 } |
| 82 } |
| 83 } |
| 84 |
| 85 void SyncNotifierHelper::EmitOnNotificationsEnabled() { |
| 86 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled()); |
| 87 } |
| 88 |
| 89 void SyncNotifierHelper::EmitOnNotificationsDisabled( |
| 90 NotificationsDisabledReason reason) { |
| 91 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, |
| 92 OnNotificationsDisabled(reason)); |
| 93 } |
| 94 |
| 95 } // namespace syncer |
OLD | NEW |