| 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 #ifndef SYNC_NOTIFIER_SYNC_NOTIFIER_REGISTRAR_H_ | |
| 6 #define SYNC_NOTIFIER_SYNC_NOTIFIER_REGISTRAR_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/observer_list.h" | |
| 12 #include "base/threading/thread_checker.h" | |
| 13 #include "sync/notifier/invalidation_util.h" | |
| 14 #include "sync/notifier/object_id_state_map.h" | |
| 15 #include "sync/notifier/sync_notifier_observer.h" | |
| 16 | |
| 17 namespace invalidation { | |
| 18 class ObjectId; | |
| 19 } // namespace invalidation | |
| 20 | |
| 21 namespace syncer { | |
| 22 | |
| 23 // A helper class for implementations of the SyncNotifier interface. It helps | |
| 24 // keep track of registered handlers and which object ID registrations are | |
| 25 // associated with which handlers, so implementors can just reuse the logic | |
| 26 // here to dispatch invalidations and other interesting notifications. | |
| 27 class SyncNotifierRegistrar { | |
| 28 public: | |
| 29 SyncNotifierRegistrar(); | |
| 30 ~SyncNotifierRegistrar(); | |
| 31 | |
| 32 // Starts sending notifications to |handler|. |handler| must not be NULL, | |
| 33 // and it must already be registered. | |
| 34 void RegisterHandler(SyncNotifierObserver* handler); | |
| 35 | |
| 36 | |
| 37 // Updates the set of ObjectIds associated with |handler|. |handler| must | |
| 38 // not be NULL, and must already be registered. An ID must be registered for | |
| 39 // at most one handler. | |
| 40 void UpdateRegisteredIds(SyncNotifierObserver* handler, | |
| 41 const ObjectIdSet& ids); | |
| 42 | |
| 43 // Stops sending notifications to |handler|. |handler| must not be NULL, and | |
| 44 // it must already be registered. Note that this doesn't unregister the IDs | |
| 45 // associated with |handler|. | |
| 46 void UnregisterHandler(SyncNotifierObserver* handler); | |
| 47 | |
| 48 // Returns the set of all IDs that are registered to some handler (even | |
| 49 // handlers that have been unregistered). | |
| 50 ObjectIdSet GetAllRegisteredIds() const; | |
| 51 | |
| 52 // Sorts incoming invalidations into a bucket for each handler and then | |
| 53 // dispatches the batched invalidations to the corresponding handler. | |
| 54 // Invalidations for IDs with no corresponding handler are dropped, as are | |
| 55 // invalidations for handlers that are not added. | |
| 56 void DispatchInvalidationsToHandlers(const ObjectIdStateMap& id_state_map, | |
| 57 IncomingNotificationSource source); | |
| 58 | |
| 59 // Calls the given handler method for each handler that has registered IDs. | |
| 60 void EmitOnNotificationsEnabled(); | |
| 61 void EmitOnNotificationsDisabled(NotificationsDisabledReason reason); | |
| 62 | |
| 63 bool IsHandlerRegisteredForTest(SyncNotifierObserver* handler) const; | |
| 64 ObjectIdSet GetRegisteredIdsForTest(SyncNotifierObserver* handler) const; | |
| 65 | |
| 66 // Needed for death tests. | |
| 67 void DetachFromThreadForTest(); | |
| 68 | |
| 69 private: | |
| 70 typedef std::map<invalidation::ObjectId, SyncNotifierObserver*, | |
| 71 ObjectIdLessThan> | |
| 72 IdHandlerMap; | |
| 73 | |
| 74 SyncNotifierObserver* ObjectIdToHandler(const invalidation::ObjectId& id); | |
| 75 | |
| 76 base::ThreadChecker thread_checker_; | |
| 77 ObserverList<SyncNotifierObserver> handlers_; | |
| 78 IdHandlerMap id_to_handler_map_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(SyncNotifierRegistrar); | |
| 81 }; | |
| 82 | |
| 83 } // namespace syncer | |
| 84 | |
| 85 #endif // SYNC_NOTIFIER_SYNC_NOTIFIER_REGISTRAR_H_ | |
| OLD | NEW |