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; | |
akalin
2012/07/20 19:01:40
why not just initialize registered_ids with ids?
akalin
2012/07/20 19:01:40
Add a comment here like:
// Remove any existing e
dcheng
2012/07/21 00:09:54
Done.
dcheng
2012/07/21 00:09:54
Done.
| |
20 for (ObjectIdObserverMap::iterator it = id_to_handler_map_.begin(); | |
21 it != id_to_handler_map_.end(); ) { | |
22 if (it->second == handler) { | |
23 ObjectIdObserverMap::iterator erase_it = it; | |
24 ++it; | |
25 id_to_handler_map_.erase(erase_it); | |
26 } else { | |
27 registered_ids.insert(it->first); | |
28 ++it; | |
29 } | |
30 } | |
31 | |
32 // Small optimization so we only traverse the map once when inserting entries. | |
akalin
2012/07/20 19:01:40
Then add a comment like:
// Now add the new entri
akalin
2012/07/20 19:01:40
it's a bit unclear what the small optimization is.
dcheng
2012/07/21 00:09:54
Done.
dcheng
2012/07/21 00:09:54
Done.
| |
33 ObjectIdObserverMap::iterator insert_it = id_to_handler_map_.begin(); | |
34 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | |
35 insert_it = id_to_handler_map_.insert(insert_it, | |
akalin
2012/07/20 19:01:40
this is dangerous since if an entry for the id alr
akalin
2012/07/20 19:34:38
Actually, on second thought, I think we should jus
dcheng
2012/07/21 00:09:54
Is there a way to make CHECKS non-fatal for unit t
dcheng
2012/07/21 00:09:54
Done with a variation.
| |
36 std::make_pair(*it, handler)); | |
37 } | |
38 registered_ids.insert(ids.begin(), ids.end()); | |
39 return registered_ids; | |
akalin
2012/07/20 19:01:40
add a DCHECK somewhere (or a test in an "if (DEBUG
dcheng
2012/07/21 00:09:54
Done.
| |
40 } | |
41 | |
42 void SyncNotifierHelper::DispatchInvalidationsToHandlers( | |
43 const ObjectIdPayloadMap& id_payloads, | |
44 IncomingNotificationSource source) { | |
45 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap; | |
46 DispatchMap dispatch_map; | |
47 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); | |
48 it != id_payloads.end(); ++it) { | |
49 ObjectIdObserverMap::const_iterator find_it = | |
50 id_to_handler_map_.find(it->first); | |
51 // If we get an invalidation with a source type that we can't map to an | |
52 // observer, just drop it--the observer was unregistered while the | |
53 // invalidation was in flight. | |
54 if (find_it == id_to_handler_map_.end()) | |
55 continue; | |
56 dispatch_map[find_it->second].insert(*it); | |
57 } | |
58 for (std::map<SyncNotifierObserver*, ObjectIdPayloadMap>::const_iterator it = | |
akalin
2012/07/20 19:01:40
i think it's worth it to do a FOR_EACH_OBSERVER-st
akalin
2012/07/20 19:01:40
use the typedef here
dcheng
2012/07/21 00:09:54
Done.
dcheng
2012/07/21 00:09:54
I'd prefer not to dispatch invalidations with no a
| |
59 dispatch_map.begin(); it != dispatch_map.end(); ++it) { | |
60 it->first->OnIncomingNotification(it->second, source); | |
61 } | |
62 } | |
63 | |
64 void SyncNotifierHelper::OnNotificationsEnabled() { | |
65 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled()); | |
66 } | |
67 | |
68 void SyncNotifierHelper::OnNotificationsDisabled( | |
69 NotificationsDisabledReason reason) { | |
70 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, | |
71 OnNotificationsDisabled(reason)); | |
72 } | |
73 | |
74 } // namespace syncer | |
OLD | NEW |