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

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: 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 #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,
41 const ObjectIdSet& ids) {
42 DCHECK(thread_checker_.CalledOnValidThread());
43 // Remove all existing entries for |handler_name|.
44 for (ObjectIdNameMap::iterator it = id_to_name_map_.begin();
45 it != id_to_name_map_.end(); ) {
46 if (it->second == handler_name) {
47 ObjectIdNameMap::iterator erase_it = it;
29 ++it; 48 ++it;
30 id_to_handler_map_.erase(erase_it); 49 id_to_name_map_.erase(erase_it);
31 } else { 50 } else {
32 registered_ids.insert(it->first);
33 ++it; 51 ++it;
34 } 52 }
35 } 53 }
36 54
37 // Now add the entries for |handler|. We keep track of the last insertion 55 // 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. 56 // insertion point so we only traverse the map once to insert all the new
39 ObjectIdHandlerMap::iterator insert_it = id_to_handler_map_.begin(); 57 // entries.
58 ObjectIdNameMap::iterator insert_it = id_to_name_map_.begin();
40 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { 59 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
41 insert_it = id_to_handler_map_.insert(insert_it, 60 insert_it =
42 std::make_pair(*it, handler)); 61 id_to_name_map_.insert(insert_it, std::make_pair(*it, handler_name));
43 CHECK_EQ(handler, insert_it->second) << "Duplicate registration for " 62 CHECK_EQ(handler_name, insert_it->second)
44 << ObjectIdToString(insert_it->first); 63 << "Duplicate registration: trying to register "
64 << ObjectIdToString(insert_it->first) << " for "
65 << handler_name << " when it's already registered for "
66 << insert_it->second;
45 } 67 }
68
46 if (logging::DEBUG_MODE) { 69 if (logging::DEBUG_MODE) {
47 // The mapping shouldn't contain any handlers that aren't in |handlers_|. 70 // The mapping shouldn't contain any handlers that aren't in |handlers_|.
48 for (ObjectIdHandlerMap::const_iterator it = id_to_handler_map_.begin(); 71 for (ObjectIdNameMap::const_iterator it = id_to_name_map_.begin();
49 it != id_to_handler_map_.end(); ++it) { 72 it != id_to_name_map_.end(); ++it) {
50 CHECK(handlers_.HasObserver(it->second)); 73 SyncNotifierObserver* const handler = HandlerNameToHandler(it->second);
74 if (handler)
75 CHECK(handlers_.HasObserver(handler));
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 to a
65 // If we get an invalidation with a source type that we can't map to an 100 // handler, just drop it -- the handler was unregistered while the
66 // observer, just drop it--the observer was unregistered while the
67 // invalidation was in flight. 101 // invalidation was in flight.
68 if (find_it == id_to_handler_map_.end()) 102 if (!handler)
69 continue; 103 continue;
70 dispatch_map[find_it->second].insert(*it); 104 dispatch_map[handler].insert(*it);
71 } 105 }
72 106
73 if (handlers_.might_have_observers()) { 107 if (handlers_.might_have_observers()) {
74 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_); 108 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_);
75 SyncNotifierObserver* handler = NULL; 109 SyncNotifierObserver* handler = NULL;
76 while ((handler = it.GetNext()) != NULL) { 110 while ((handler = it.GetNext()) != NULL) {
77 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler); 111 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler);
78 if (dispatch_it != dispatch_map.end()) { 112 if (dispatch_it != dispatch_map.end()) {
79 handler->OnIncomingNotification(dispatch_it->second, source); 113 handler->OnIncomingNotification(dispatch_it->second, source);
80 } 114 }
81 } 115 }
82 } 116 }
83 } 117 }
84 118
85 void SyncNotifierHelper::EmitOnNotificationsEnabled() { 119 void SyncNotifierHelper::EmitOnNotificationsEnabled() {
120 DCHECK(thread_checker_.CalledOnValidThread());
86 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled()); 121 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled());
87 } 122 }
88 123
89 void SyncNotifierHelper::EmitOnNotificationsDisabled( 124 void SyncNotifierHelper::EmitOnNotificationsDisabled(
90 NotificationsDisabledReason reason) { 125 NotificationsDisabledReason reason) {
126 DCHECK(thread_checker_.CalledOnValidThread());
91 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, 127 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_,
92 OnNotificationsDisabled(reason)); 128 OnNotificationsDisabled(reason));
93 } 129 }
94 130
131 void SyncNotifierHelper::DetachFromThreadForTest() {
132 DCHECK(thread_checker_.CalledOnValidThread());
133 thread_checker_.DetachFromThread();
134 }
135
136 SyncNotifierObserver* SyncNotifierHelper::HandlerNameToHandler(
137 const std::string& handler_name) const {
138 DCHECK(thread_checker_.CalledOnValidThread());
139 NameHandlerMap::const_iterator it =
140 name_to_handler_map_.find(handler_name);
141 return (it == name_to_handler_map_.end()) ? NULL : it->second;
142 }
143
144 SyncNotifierObserver* SyncNotifierHelper::ObjectIdToHandler(
145 const invalidation::ObjectId& id) const {
146 DCHECK(thread_checker_.CalledOnValidThread());
147 ObjectIdNameMap::const_iterator it = id_to_name_map_.find(id);
148 return
149 (it == id_to_name_map_.end()) ? NULL : HandlerNameToHandler(it->second);
150 }
151
95 } // namespace syncer 152 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698