Chromium Code Reviews| Index: sync/notifier/invalidation_notifier_base.cc |
| diff --git a/sync/notifier/invalidation_notifier_base.cc b/sync/notifier/invalidation_notifier_base.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bc930fcfe578b1a3ae628e6b2bc2ba1bc44323e8 |
| --- /dev/null |
| +++ b/sync/notifier/invalidation_notifier_base.cc |
| @@ -0,0 +1,63 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "sync/notifier/invalidation_notifier_base.h" |
| + |
| +#include "base/stl_util.h" |
| + |
| +namespace syncer { |
| + |
| +InvalidationNotifierBase::InvalidationNotifierBase() {} |
| +InvalidationNotifierBase::~InvalidationNotifierBase() {} |
| + |
| +void InvalidationNotifierBase::AddHandler(SyncNotifierObserver* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| +void InvalidationNotifierBase::RemoveHandler(SyncNotifierObserver* observer) { |
| + observers_.RemoveObserver(observer); |
| + UpdateObjectIdObserverMap(); |
| +} |
| + |
| +ObjectIdSet InvalidationNotifierBase::UpdateObjectIdObserverMap() { |
| + ObjectIdSet ids_in_map; |
| + id_to_observer_map_.clear(); |
| + if (observers_.might_have_observers()) { |
|
akalin
2012/07/11 00:10:24
why avoid the FOR_EACH_OBSERVER macro here?
dcheng
2012/07/11 05:45:28
We need the return values. It might be possible to
akalin
2012/07/11 20:27:02
Ah, I missed that. Yeah, that's another reason wh
|
| + ObserverListBase<SyncNotifierObserver>::Iterator observer_it(observers_); |
| + SyncNotifierObserver* observer; |
| + while ((observer = observer_it.GetNext()) != NULL) { |
| + const ObjectIdSet& ids = observer->GetHandledIds(); |
| + for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); |
| + ++it) { |
| + DCHECK(!ContainsKey(id_to_observer_map_, *it)); |
| + id_to_observer_map_[*it] = observer; |
| + ids_in_map.insert(*it); |
| + } |
| + } |
| + } |
| + return ids_in_map; |
| +} |
| + |
| +void InvalidationNotifierBase::DispatchInvalidationsToObservers( |
| + const ObjectIdPayloadMap& id_payloads, |
| + IncomingNotificationSource source) { |
| + std::map<SyncNotifierObserver*, ObjectIdPayloadMap> dispatch_map; |
| + for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); |
| + it != id_payloads.end(); ++it) { |
| + ObjectIdObserverMap::const_iterator find_it = |
| + id_to_observer_map_.find(it->first); |
| + // If we get an invalidation for an object ID that we can't map to an |
| + // observer, just drop it--the observer was unregistered while the |
| + // invalidation was in flight. |
| + if (find_it == id_to_observer_map_.end()) |
| + continue; |
| + dispatch_map[find_it->second].insert(*it); |
| + } |
| + for (std::map<SyncNotifierObserver*, ObjectIdPayloadMap>::const_iterator it = |
| + dispatch_map.begin(); it != dispatch_map.end(); ++it) { |
| + DCHECK(observers_.HasObserver(it->first)); |
| + it->first->OnIncomingNotification(it->second, source); |
| + } |
| +} |
| + |
| +} // namespace syncer |