Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: sync/notifier/sync_notifier_helper.cc

Issue 10824161: [Sync] Avoid unregistering object IDs on shutdown (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile, address comments Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
7 #include "base/logging.h" 9 #include "base/logging.h"
8 10
9 namespace syncer { 11 namespace syncer {
10 12
11 SyncNotifierHelper::SyncNotifierHelper() {} 13 SyncNotifierHelper::SyncNotifierHelper() {}
12 SyncNotifierHelper::~SyncNotifierHelper() {}
13 14
14 ObjectIdSet SyncNotifierHelper::UpdateRegisteredIds( 15 SyncNotifierHelper::~SyncNotifierHelper() {
15 SyncNotifierObserver* handler, const ObjectIdSet& ids) { 16 DCHECK(thread_checker_.CalledOnValidThread());
16 if (ids.empty()) { 17 }
17 handlers_.RemoveObserver(handler); 18
18 } else if (!handlers_.HasObserver(handler)) { 19 void SyncNotifierHelper::SetHandler(const std::string& handler_name,
19 handlers_.AddObserver(handler); 20 SyncNotifierObserver* handler) {
21 DCHECK(thread_checker_.CalledOnValidThread());
22 SyncNotifierObserver* const old_handler =
23 HandlerNameToHandler(handler_name);
24 if (old_handler) {
25 handlers_.RemoveObserver(old_handler);
26 name_to_handler_map_.erase(handler_name);
20 } 27 }
21 28
22 ObjectIdSet registered_ids(ids); 29 if (handler) {
23 // Remove all existing entries for |handler| and fill |registered_ids| with 30 handlers_.AddObserver(handler);
24 // the rest. 31 name_to_handler_map_[handler_name] = handler;
dcheng 2012/08/03 20:34:14 Nit: Though insert is more verbose, I prefer it ov
akalin 2012/08/03 20:48:39 Done.
25 for (ObjectIdHandlerMap::iterator it = id_to_handler_map_.begin(); 32 }
26 it != id_to_handler_map_.end(); ) { 33 }
27 if (it->second == handler) { 34
28 ObjectIdHandlerMap::iterator erase_it = it; 35 void SyncNotifierHelper::UpdateRegisteredIds(
36 const std::string& handler_name, const ObjectIdSet& ids) {
37 DCHECK(thread_checker_.CalledOnValidThread());
38 // Remove all existing entries for |handler_name|.
39 for (ObjectIdNameMap::iterator it = id_to_name_map_.begin();
40 it != id_to_name_map_.end(); ) {
41 if (it->second == handler_name) {
42 ObjectIdNameMap::iterator erase_it = it;
29 ++it; 43 ++it;
30 id_to_handler_map_.erase(erase_it); 44 id_to_name_map_.erase(erase_it);
31 } else { 45 } else {
32 registered_ids.insert(it->first);
33 ++it; 46 ++it;
34 } 47 }
35 } 48 }
36 49
37 // Now add the entries for |handler|. We keep track of the last insertion 50 // Now add the entries for |handler_name|. We keep track of the last
38 // point so we only traverse the map once to insert all the new entries. 51 // insertion point so we only traverse the map once to insert all
39 ObjectIdHandlerMap::iterator insert_it = id_to_handler_map_.begin(); 52 // the new entries.
53 ObjectIdNameMap::iterator insert_it = id_to_name_map_.begin();
40 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { 54 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
41 insert_it = id_to_handler_map_.insert(insert_it, 55 insert_it =
42 std::make_pair(*it, handler)); 56 id_to_name_map_.insert(insert_it, std::make_pair(*it, handler_name));
43 CHECK_EQ(handler, insert_it->second) << "Duplicate registration for " 57 CHECK_EQ(handler_name, insert_it->second)
44 << ObjectIdToString(insert_it->first); 58 << "Duplicate registration: trying to register "
59 << ObjectIdToString(insert_it->first) << " for "
60 << handler_name << " when it's already registered for "
61 << insert_it->second;
45 } 62 }
63
46 if (logging::DEBUG_MODE) { 64 if (logging::DEBUG_MODE) {
47 // The mapping shouldn't contain any handlers that aren't in |handlers_|. 65 // The mapping shouldn't contain any handlers that aren't in |handlers_|.
48 for (ObjectIdHandlerMap::const_iterator it = id_to_handler_map_.begin(); 66 for (ObjectIdNameMap::const_iterator it = id_to_name_map_.begin();
49 it != id_to_handler_map_.end(); ++it) { 67 it != id_to_name_map_.end(); ++it) {
50 CHECK(handlers_.HasObserver(it->second)); 68 SyncNotifierObserver* const handler = HandlerNameToHandler(it->second);
69 if (handler) {
70 CHECK(handlers_.HasObserver(handler));
71 }
51 } 72 }
52 } 73 }
74 }
75
76 ObjectIdSet SyncNotifierHelper::GetAllRegisteredIds() const {
77 DCHECK(thread_checker_.CalledOnValidThread());
78 ObjectIdSet registered_ids;
79 for (ObjectIdNameMap::const_iterator it = id_to_name_map_.begin();
80 it != id_to_name_map_.end(); ++it) {
81 registered_ids.insert(it->first);
82 }
53 return registered_ids; 83 return registered_ids;
54 } 84 }
55 85
56 void SyncNotifierHelper::DispatchInvalidationsToHandlers( 86 void SyncNotifierHelper::DispatchInvalidationsToHandlers(
57 const ObjectIdPayloadMap& id_payloads, 87 const ObjectIdPayloadMap& id_payloads,
58 IncomingNotificationSource source) { 88 IncomingNotificationSource source) {
89 DCHECK(thread_checker_.CalledOnValidThread());
59 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap; 90 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap;
60 DispatchMap dispatch_map; 91 DispatchMap dispatch_map;
61 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); 92 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin();
62 it != id_payloads.end(); ++it) { 93 it != id_payloads.end(); ++it) {
63 ObjectIdHandlerMap::const_iterator find_it = 94 SyncNotifierObserver* const handler = ObjectIdToHandler(it->first);
64 id_to_handler_map_.find(it->first); 95 // 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 96 // to an handler, just drop it -- the handler was unregistered
66 // observer, just drop it--the observer was unregistered while the 97 // while the invalidation was in flight.
67 // invalidation was in flight. 98 if (!handler) {
68 if (find_it == id_to_handler_map_.end())
69 continue; 99 continue;
70 dispatch_map[find_it->second].insert(*it); 100 }
101 dispatch_map[handler].insert(*it);
71 } 102 }
72 103
73 if (handlers_.might_have_observers()) { 104 if (handlers_.might_have_observers()) {
74 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); 105 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_);
75 SyncNotifierObserver* handler = NULL; 106 SyncNotifierObserver* handler = NULL;
76 while ((handler = it.GetNext()) != NULL) { 107 while ((handler = it.GetNext()) != NULL) {
77 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); 108 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler);
78 if (dispatch_it != dispatch_map.end()) { 109 if (dispatch_it != dispatch_map.end()) {
79 handler->OnIncomingNotification(dispatch_it->second, source); 110 handler->OnIncomingNotification(dispatch_it->second, source);
80 } 111 }
81 } 112 }
82 } 113 }
83 } 114 }
84 115
85 void SyncNotifierHelper::EmitOnNotificationsEnabled() { 116 void SyncNotifierHelper::EmitOnNotificationsEnabled() {
86 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled()); 117 DCHECK(thread_checker_.CalledOnValidThread());
118 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_,
119 OnNotificationsEnabled());
87 } 120 }
88 121
89 void SyncNotifierHelper::EmitOnNotificationsDisabled( 122 void SyncNotifierHelper::EmitOnNotificationsDisabled(
90 NotificationsDisabledReason reason) { 123 NotificationsDisabledReason reason) {
124 DCHECK(thread_checker_.CalledOnValidThread());
91 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, 125 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_,
92 OnNotificationsDisabled(reason)); 126 OnNotificationsDisabled(reason));
93 } 127 }
94 128
129 void SyncNotifierHelper::DetachFromThreadForTest() {
130 DCHECK(thread_checker_.CalledOnValidThread());
131 thread_checker_.DetachFromThread();
132 }
133
134 SyncNotifierObserver* SyncNotifierHelper::HandlerNameToHandler(
135 const std::string& handler_name) const {
136 DCHECK(thread_checker_.CalledOnValidThread());
137 NameHandlerMap::const_iterator it =
138 name_to_handler_map_.find(handler_name);
139 return (it == name_to_handler_map_.end()) ? NULL : it->second;
140 }
141
142 SyncNotifierObserver* SyncNotifierHelper::ObjectIdToHandler(
143 const invalidation::ObjectId& id) const {
144 DCHECK(thread_checker_.CalledOnValidThread());
145 ObjectIdNameMap::const_iterator it = id_to_name_map_.find(id);
146 return
147 (it == id_to_name_map_.end()) ? NULL : HandlerNameToHandler(it->second);
148 }
149
95 } // namespace syncer 150 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698