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