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