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