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 "base/stl_util.h" |
| 6 #include "sync/notifier/invalidation_notifier_base.h" |
| 7 |
| 8 namespace syncer { |
| 9 |
| 10 InvalidationNotifierBase::InvalidationNotifierBase() {} |
| 11 InvalidationNotifierBase::~InvalidationNotifierBase() {} |
| 12 |
| 13 void InvalidationNotifierBase::AddHandler(SyncNotifierObserver* observer) { |
| 14 observers_.AddObserver(observer); |
| 15 } |
| 16 void InvalidationNotifierBase::RemoveHandler(SyncNotifierObserver* observer) { |
| 17 observers_.RemoveObserver(observer); |
| 18 UpdateObjectIdObserverMap(); |
| 19 } |
| 20 |
| 21 ObjectIdSet InvalidationNotifierBase::UpdateObjectIdObserverMap() { |
| 22 ObjectIdSet ids_in_map; |
| 23 id_to_observer_map_.clear(); |
| 24 if (observers_.might_have_observers()) { |
| 25 ObserverListBase<SyncNotifierObserver>::Iterator observer_it(observers_); |
| 26 SyncNotifierObserver* observer; |
| 27 while ((observer = observer_it.GetNext()) != NULL) { |
| 28 const ObjectIdSet& ids = observer->GetHandledIds(); |
| 29 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); |
| 30 ++it) { |
| 31 DCHECK(!ContainsKey(id_to_observer_map_, *it)); |
| 32 id_to_observer_map_[*it] = observer; |
| 33 ids_in_map.insert(*it); |
| 34 } |
| 35 } |
| 36 } |
| 37 return ids_in_map; |
| 38 } |
| 39 |
| 40 void InvalidationNotifierBase::DispatchInvalidationsToObservers( |
| 41 const ObjectIdPayloadMap& id_payloads, |
| 42 IncomingNotificationSource source) { |
| 43 std::map<SyncNotifierObserver*, ObjectIdPayloadMap> dispatch_map; |
| 44 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); |
| 45 it != id_payloads.end(); ++it) { |
| 46 ObjectIdObserverMap::const_iterator find_it = |
| 47 id_to_observer_map_.find(it->first); |
| 48 // If we get an invalidation for an object ID that we can't map to an |
| 49 // observer, just drop it--the observer was unregistered while the |
| 50 // invalidation was in flight. |
| 51 if (find_it == id_to_observer_map_.end()) |
| 52 continue; |
| 53 dispatch_map[find_it->second].insert(*it); |
| 54 } |
| 55 for (std::map<SyncNotifierObserver*, ObjectIdPayloadMap>::const_iterator it = |
| 56 dispatch_map.begin(); it != dispatch_map.end(); ++it) { |
| 57 DCHECK(observers_.HasObserver(it->first)); |
| 58 it->first->OnIncomingNotification(it->second, source); |
| 59 } |
| 60 } |
| 61 |
| 62 } // namespace syncer |
OLD | NEW |