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

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

Issue 10824161: [Sync] Avoid unregistering object IDs on shutdown (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Work around brittle unit test 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
(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_registrar.h"
6
7 #include <cstddef>
8 #include <utility>
9
10 #include "base/logging.h"
11
12 namespace syncer {
13
14 SyncNotifierRegistrar::SyncNotifierRegistrar() {}
15
16 SyncNotifierRegistrar::~SyncNotifierRegistrar() {
17 DCHECK(thread_checker_.CalledOnValidThread());
18 }
19
20 void SyncNotifierRegistrar::RegisterHandler(SyncNotifierObserver* handler) {
21 DCHECK(thread_checker_.CalledOnValidThread());
22 CHECK(handler);
23 CHECK(!handlers_.HasObserver(handler));
24 handlers_.AddObserver(handler);
25 }
26
27 void SyncNotifierRegistrar::UpdateRegisteredIds(
28 SyncNotifierObserver* handler,
29 const ObjectIdSet& ids) {
30 DCHECK(thread_checker_.CalledOnValidThread());
31 CHECK(handler);
32 CHECK(handlers_.HasObserver(handler));
33 // Remove all existing entries for |handler|.
34 for (IdHandlerMap::iterator it = id_to_handler_map_.begin();
35 it != id_to_handler_map_.end(); ) {
36 if (it->second == handler) {
37 IdHandlerMap::iterator erase_it = it;
38 ++it;
39 id_to_handler_map_.erase(erase_it);
40 } else {
41 ++it;
42 }
43 }
44
45 // Now add the entries for |handler|. We keep track of the last insertion
46 // point so we only traverse the map once to insert all the new entries.
47 IdHandlerMap::iterator insert_it = id_to_handler_map_.begin();
48 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
49 insert_it =
50 id_to_handler_map_.insert(insert_it, std::make_pair(*it, handler));
51 CHECK_EQ(handler, insert_it->second)
52 << "Duplicate registration: trying to register "
53 << ObjectIdToString(insert_it->first) << " for "
54 << handler << " when it's already registered for "
55 << insert_it->second;
56 }
57 }
58
59 void SyncNotifierRegistrar::UnregisterHandler(SyncNotifierObserver* handler) {
60 DCHECK(thread_checker_.CalledOnValidThread());
61 CHECK(handler);
62 CHECK(handlers_.HasObserver(handler));
63 handlers_.RemoveObserver(handler);
64 }
65
66 ObjectIdSet SyncNotifierRegistrar::GetAllRegisteredIds() const {
67 DCHECK(thread_checker_.CalledOnValidThread());
68 ObjectIdSet registered_ids;
69 for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin();
70 it != id_to_handler_map_.end(); ++it) {
71 registered_ids.insert(it->first);
72 }
73 return registered_ids;
74 }
75
76 void SyncNotifierRegistrar::DispatchInvalidationsToHandlers(
77 const ObjectIdPayloadMap& id_payloads,
78 IncomingNotificationSource source) {
79 DCHECK(thread_checker_.CalledOnValidThread());
80 // If we have no handlers, there's nothing to do.
81 if (!handlers_.might_have_observers()) {
82 return;
83 }
84
85 typedef std::map<SyncNotifierObserver*, ObjectIdPayloadMap> DispatchMap;
86 DispatchMap dispatch_map;
87 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin();
88 it != id_payloads.end(); ++it) {
89 SyncNotifierObserver* const handler = ObjectIdToHandler(it->first);
90 // Filter out invalidations for IDs with no handler.
91 if (handler)
92 dispatch_map[handler].insert(*it);
93 }
94
95 // Emit invalidations only for handlers in |handlers_|.
96 ObserverListBase<SyncNotifierObserver>::Iterator it(handlers_);
97 SyncNotifierObserver* handler = NULL;
98 while ((handler = it.GetNext()) != NULL) {
99 DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler);
100 if (dispatch_it != dispatch_map.end())
101 handler->OnIncomingNotification(dispatch_it->second, source);
102 }
103 }
104
105 void SyncNotifierRegistrar::EmitOnNotificationsEnabled() {
106 DCHECK(thread_checker_.CalledOnValidThread());
107 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_, OnNotificationsEnabled());
108 }
109
110 void SyncNotifierRegistrar::EmitOnNotificationsDisabled(
111 NotificationsDisabledReason reason) {
112 DCHECK(thread_checker_.CalledOnValidThread());
113 FOR_EACH_OBSERVER(SyncNotifierObserver, handlers_,
114 OnNotificationsDisabled(reason));
115 }
116
117 void SyncNotifierRegistrar::DetachFromThreadForTest() {
118 DCHECK(thread_checker_.CalledOnValidThread());
119 thread_checker_.DetachFromThread();
120 }
121
122 SyncNotifierObserver* SyncNotifierRegistrar::ObjectIdToHandler(
123 const invalidation::ObjectId& id) {
124 DCHECK(thread_checker_.CalledOnValidThread());
125 IdHandlerMap::const_iterator it = id_to_handler_map_.find(id);
126 return (it == id_to_handler_map_.end()) ? NULL : it->second;
127 }
128
129 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/notifier/sync_notifier_registrar.h ('k') | sync/notifier/sync_notifier_registrar_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698