Chromium Code Reviews| 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" | |
| 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()) { | |
|
akalin
2012/07/19 00:42:07
given that at most one handler can handle a given
dcheng
2012/07/19 18:31:05
I'm not a fan of the proposed scheme. It's more lo
akalin
2012/07/19 23:07:26
Ah, I see your point. You're optimizing for the D
| |
| 15 handlers_.RemoveObserver(handler); | |
| 16 } else if (!handlers_.HasObserver(handler)) { | |
| 17 handlers_.AddObserver(handler); | |
| 18 } | |
| 19 | |
| 20 ObjectIdSet registered_ids; | |
| 21 for (ObjectIdObserverMap::iterator it = id_to_handler_map_.begin(); | |
| 22 it != id_to_handler_map_.end(); ) { | |
| 23 if (it->second == handler) { | |
| 24 ObjectIdObserverMap::iterator erase_it = it; | |
| 25 ++it; | |
| 26 id_to_handler_map_.erase(erase_it); | |
| 27 } else { | |
| 28 registered_ids.insert(it->first); | |
| 29 ++it; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 // Small optimization so we only traverse the map once when inserting entries. | |
| 34 ObjectIdObserverMap::iterator insert_it = id_to_handler_map_.begin(); | |
| 35 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | |
| 36 insert_it = id_to_handler_map_.insert(insert_it, | |
| 37 std::make_pair(*it, handler)); | |
| 38 } | |
| 39 registered_ids.insert(ids.begin(), ids.end()); | |
| 40 return registered_ids; | |
| 41 } | |
| 42 | |
| 43 void SyncNotifierHelper::DispatchInvalidationsToHandlers( | |
| 44 const ObjectIdPayloadMap& id_payloads, | |
| 45 IncomingNotificationSource source) { | |
| 46 std::map<SyncNotifierObserver*, ObjectIdPayloadMap> dispatch_map; | |
|
akalin
2012/07/19 00:42:07
typedef the map to something? (can be scoped to th
dcheng
2012/07/19 18:31:05
Done.
| |
| 47 for (ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); | |
| 48 it != id_payloads.end(); ++it) { | |
| 49 ObjectIdObserverMap::const_iterator find_it = | |
| 50 id_to_handler_map_.find(it->first); | |
| 51 // If we get an invalidation with a source type that we can't map to an | |
| 52 // observer, just drop it--the observer was unregistered while the | |
| 53 // invalidation was in flight. | |
| 54 if (find_it == id_to_handler_map_.end()) | |
| 55 continue; | |
| 56 dispatch_map[find_it->second].insert(*it); | |
| 57 } | |
| 58 for (std::map<SyncNotifierObserver*, ObjectIdPayloadMap>::const_iterator it = | |
| 59 dispatch_map.begin(); it != dispatch_map.end(); ++it) { | |
| 60 DCHECK(handlers_.HasObserver(it->first)); | |
|
akalin
2012/07/19 00:42:07
this doesn't necessarily hold does it? OnIncoming
dcheng
2012/07/19 18:31:05
That seems like an overreaching handler, and not a
akalin
2012/07/19 23:07:26
Maybe add a comment somewhere mandating that obser
dcheng
2012/07/20 00:57:04
After more thought, I just removed the DCHECK. I a
| |
| 61 it->first->OnIncomingNotification(it->second, source); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 } // namespace syncer | |
| OLD | NEW |