OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/notifier/sync_notifier_helper.h" | 5 #include "sync/notifier/sync_notifier_helper.h" |
6 | 6 |
7 #include <cstddef> | |
8 #include <utility> | |
9 | |
7 #include "base/logging.h" | 10 #include "base/logging.h" |
8 | 11 |
9 namespace syncer { | 12 namespace syncer { |
10 | 13 |
11 SyncNotifierHelper::SyncNotifierHelper() {} | 14 SyncNotifierHelper::SyncNotifierHelper() {} |
12 SyncNotifierHelper::~SyncNotifierHelper() {} | |
13 | 15 |
14 ObjectIdSet SyncNotifierHelper::UpdateRegisteredIds( | 16 SyncNotifierHelper::~SyncNotifierHelper() { |
15 SyncNotifierObserver* handler, const ObjectIdSet& ids) { | 17 DCHECK(thread_checker_.CalledOnValidThread()); |
16 if (ids.empty()) { | 18 } |
17 handlers_.RemoveObserver(handler); | 19 |
18 } else if (!handlers_.HasObserver(handler)) { | 20 void SyncNotifierHelper::SetHandler(const std::string& handler_name, |
19 handlers_.AddObserver(handler); | 21 SyncNotifierObserver* handler) { |
22 DCHECK(thread_checker_.CalledOnValidThread()); | |
23 SyncNotifierObserver* const old_handler = | |
24 HandlerNameToHandler(handler_name); | |
25 if (old_handler) { | |
26 handlers_.RemoveObserver(old_handler); | |
27 name_to_handler_map_.erase(handler_name); | |
20 } | 28 } |
21 | 29 |
22 ObjectIdSet registered_ids(ids); | 30 if (handler) { |
23 // Remove all existing entries for |handler| and fill |registered_ids| with | 31 handlers_.AddObserver(handler); |
24 // the rest. | 32 bool inserted = |
25 for (ObjectIdHandlerMap::iterator it = id_to_handler_map_.begin(); | 33 name_to_handler_map_.insert( |
26 it != id_to_handler_map_.end(); ) { | 34 std::make_pair(handler_name, handler)).second; |
27 if (it->second == handler) { | 35 DCHECK(inserted); |
28 ObjectIdHandlerMap::iterator erase_it = it; | 36 } |
37 } | |
38 | |
39 void SyncNotifierHelper::UpdateRegisteredIds( | |
40 const std::string& handler_name, const ObjectIdSet& ids) { | |
msw
2012/08/03 23:30:46
one param per line
akalin
2012/08/07 07:25:19
Done.
| |
41 DCHECK(thread_checker_.CalledOnValidThread()); | |
42 // Remove all existing entries for |handler_name|. | |
43 for (ObjectIdNameMap::iterator it = id_to_name_map_.begin(); | |
44 it != id_to_name_map_.end(); ) { | |
45 if (it->second == handler_name) { | |
46 ObjectIdNameMap::iterator erase_it = it; | |
29 ++it; | 47 ++it; |
30 id_to_handler_map_.erase(erase_it); | 48 id_to_name_map_.erase(erase_it); |
31 } else { | 49 } else { |
32 registered_ids.insert(it->first); | |
33 ++it; | 50 ++it; |
34 } | 51 } |
35 } | 52 } |
36 | 53 |
37 // Now add the entries for |handler|. We keep track of the last insertion | 54 // Now add the entries for |handler_name|. We keep track of the last |
msw
2012/08/03 23:30:46
nit: line breaks :)
akalin
2012/08/07 07:25:19
Done.
| |
38 // point so we only traverse the map once to insert all the new entries. | 55 // insertion point so we only traverse the map once to insert all |
39 ObjectIdHandlerMap::iterator insert_it = id_to_handler_map_.begin(); | 56 // the new entries. |
57 ObjectIdNameMap::iterator insert_it = id_to_name_map_.begin(); | |
40 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | 58 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { |
41 insert_it = id_to_handler_map_.insert(insert_it, | 59 insert_it = |
42 std::make_pair(*it, handler)); | 60 id_to_name_map_.insert(insert_it, std::make_pair(*it, handler_name)); |
43 CHECK_EQ(handler, insert_it->second) << "Duplicate registration for " | 61 CHECK_EQ(handler_name, insert_it->second) |
44 << ObjectIdToString(insert_it->first); | 62 << "Duplicate registration: trying to register " |
63 << ObjectIdToString(insert_it->first) << " for " | |
64 << handler_name << " when it's already registered for " | |
65 << insert_it->second; | |
45 } | 66 } |
67 | |
46 if (logging::DEBUG_MODE) { | 68 if (logging::DEBUG_MODE) { |
47 // The mapping shouldn't contain any handlers that aren't in |handlers_|. | 69 // The mapping shouldn't contain any handlers that aren't in |handlers_|. |
48 for (ObjectIdHandlerMap::const_iterator it = id_to_handler_map_.begin(); | 70 for (ObjectIdNameMap::const_iterator it = id_to_name_map_.begin(); |
49 it != id_to_handler_map_.end(); ++it) { | 71 it != id_to_name_map_.end(); ++it) { |
50 CHECK(handlers_.HasObserver(it->second)); | 72 SyncNotifierObserver* const handler = HandlerNameToHandler(it->second); |
73 if (handler) { | |
msw
2012/08/03 23:30:46
nit: remove unnecessary {}
akalin
2012/08/07 07:25:19
Done.
| |
74 CHECK(handlers_.HasObserver(handler)); | |
75 } | |
51 } | 76 } |
52 } | 77 } |
78 } | |
79 | |
80 ObjectIdSet SyncNotifierHelper::GetAllRegisteredIds() const { | |
81 DCHECK(thread_checker_.CalledOnValidThread()); | |
82 ObjectIdSet registered_ids; | |
83 for (ObjectIdNameMap::const_iterator it = id_to_name_map_.begin(); | |
84 it != id_to_name_map_.end(); ++it) { | |
85 registered_ids.insert(it->first); | |
86 } | |
53 return registered_ids; | 87 return registered_ids; |
54 } | 88 } |
55 | 89 |
56 void SyncNotifierHelper::DispatchInvalidationsToHandlers( | 90 void SyncNotifierHelper::DispatchInvalidationsToHandlers( |
57 const ObjectIdPayloadMap& id_payloads, | 91 const ObjectIdPayloadMap& id_payloads, |
58 IncomingNotificationSource source) { | 92 IncomingNotificationSource source) { |
93 DCHECK(thread_checker_.CalledOnValidThread()); | |
59 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap; | 94 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap; |
60 DispatchMap dispatch_map; | 95 DispatchMap dispatch_map; |
61 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); | 96 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); |
62 it != id_payloads.end(); ++it) { | 97 it != id_payloads.end(); ++it) { |
63 ObjectIdHandlerMap::const_iterator find_it = | 98 SyncNotifierObserver* const handler = ObjectIdToHandler(it->first); |
64 id_to_handler_map_.find(it->first); | 99 // If we get an invalidation with a source type that we can't map |
65 // If we get an invalidation with a source type that we can't map to an | 100 // to an handler, just drop it -- the handler was unregistered |
msw
2012/08/03 23:30:46
nit: "a handler" and line breaks :)
akalin
2012/08/07 07:25:19
Done.
| |
66 // observer, just drop it--the observer was unregistered while the | 101 // while the invalidation was in flight. |
67 // invalidation was in flight. | 102 if (!handler) { |
msw
2012/08/03 23:30:46
nit: just perform the insert if (handler), or remo
akalin
2012/08/07 07:25:19
Done.
| |
68 if (find_it == id_to_handler_map_.end()) | |
69 continue; | 103 continue; |
70 dispatch_map[find_it->second].insert(*it); | 104 } |
105 dispatch_map[handler].insert(*it); | |
71 } | 106 } |
72 | 107 |
73 if (handlers_.might_have_observers()) { | 108 if (handlers_.might_have_observers()) { |
74 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); | 109 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); |
75 SyncNotifierObserver* handler = NULL; | 110 SyncNotifierObserver* handler = NULL; |
76 while ((handler = it.GetNext()) != NULL) { | 111 while ((handler = it.GetNext()) != NULL) { |
77 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); | 112 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); |
78 if (dispatch_it != dispatch_map.end()) { | 113 if (dispatch_it != dispatch_map.end()) { |
79 handler->OnIncomingNotification(dispatch_it->second, source); | 114 handler->OnIncomingNotification(dispatch_it->second, source); |
80 } | 115 } |
81 } | 116 } |
82 } | 117 } |
83 } | 118 } |
84 | 119 |
85 void SyncNotifierHelper::EmitOnNotificationsEnabled() { | 120 void SyncNotifierHelper::EmitOnNotificationsEnabled() { |
86 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled()); | 121 DCHECK(thread_checker_.CalledOnValidThread()); |
122 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, | |
msw
2012/08/03 23:30:46
nit: line breaks :)
akalin
2012/08/07 07:25:19
Done.
| |
123 OnNotificationsEnabled()); | |
87 } | 124 } |
88 | 125 |
89 void SyncNotifierHelper::EmitOnNotificationsDisabled( | 126 void SyncNotifierHelper::EmitOnNotificationsDisabled( |
90 NotificationsDisabledReason reason) { | 127 NotificationsDisabledReason reason) { |
128 DCHECK(thread_checker_.CalledOnValidThread()); | |
91 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, | 129 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, |
92 OnNotificationsDisabled(reason)); | 130 OnNotificationsDisabled(reason)); |
93 } | 131 } |
94 | 132 |
133 void SyncNotifierHelper::DetachFromThreadForTest() { | |
134 DCHECK(thread_checker_.CalledOnValidThread()); | |
135 thread_checker_.DetachFromThread(); | |
136 } | |
137 | |
138 SyncNotifierObserver* SyncNotifierHelper::HandlerNameToHandler( | |
139 const std::string& handler_name) const { | |
140 DCHECK(thread_checker_.CalledOnValidThread()); | |
141 NameHandlerMap::const_iterator it = | |
142 name_to_handler_map_.find(handler_name); | |
143 return (it == name_to_handler_map_.end()) ? NULL : it->second; | |
144 } | |
145 | |
146 SyncNotifierObserver* SyncNotifierHelper::ObjectIdToHandler( | |
147 const invalidation::ObjectId& id) const { | |
148 DCHECK(thread_checker_.CalledOnValidThread()); | |
149 ObjectIdNameMap::const_iterator it = id_to_name_map_.find(id); | |
150 return | |
151 (it == id_to_name_map_.end()) ? NULL : HandlerNameToHandler(it->second); | |
152 } | |
153 | |
95 } // namespace syncer | 154 } // namespace syncer |
OLD | NEW |