Home | History | Annotate | Download | only in notifier
      1 // Copyright 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_INVALIDATOR_REGISTRAR_H_
      6 #define SYNC_NOTIFIER_INVALIDATOR_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/base/sync_export.h"
     14 #include "sync/notifier/invalidation_handler.h"
     15 #include "sync/notifier/invalidation_util.h"
     16 #include "sync/notifier/object_id_invalidation_map.h"
     17 
     18 namespace invalidation {
     19 class ObjectId;
     20 }  // namespace invalidation
     21 
     22 namespace syncer {
     23 
     24 // A helper class for implementations of the Invalidator interface.  It helps
     25 // keep track of registered handlers and which object ID registrations are
     26 // associated with which handlers, so implementors can just reuse the logic
     27 // here to dispatch invalidations and other interesting notifications.
     28 class SYNC_EXPORT InvalidatorRegistrar {
     29  public:
     30   InvalidatorRegistrar();
     31 
     32   // It is an error to have registered handlers on destruction.
     33   ~InvalidatorRegistrar();
     34 
     35   // Starts sending notifications to |handler|.  |handler| must not be NULL,
     36   // and it must already be registered.
     37   void RegisterHandler(InvalidationHandler* handler);
     38 
     39   // Updates the set of ObjectIds associated with |handler|.  |handler| must
     40   // not be NULL, and must already be registered.  An ID must be registered for
     41   // at most one handler.
     42   void UpdateRegisteredIds(InvalidationHandler* handler,
     43                            const ObjectIdSet& ids);
     44 
     45   // Stops sending notifications to |handler|.  |handler| must not be NULL, and
     46   // it must already be registered.  Note that this doesn't unregister the IDs
     47   // associated with |handler|.
     48   void UnregisterHandler(InvalidationHandler* handler);
     49 
     50   ObjectIdSet GetRegisteredIds(InvalidationHandler* handler) const;
     51 
     52   // Returns the set of all IDs that are registered to some handler (even
     53   // handlers that have been unregistered).
     54   ObjectIdSet GetAllRegisteredIds() const;
     55 
     56   // Sorts incoming invalidations into a bucket for each handler and then
     57   // dispatches the batched invalidations to the corresponding handler.
     58   // Invalidations for IDs with no corresponding handler are dropped, as are
     59   // invalidations for handlers that are not added.
     60   void DispatchInvalidationsToHandlers(
     61       const ObjectIdInvalidationMap& invalidation_map);
     62 
     63   // Updates the invalidator state to the given one and then notifies
     64   // all handlers.  Note that the order is important; handlers that
     65   // call GetInvalidatorState() when notified will see the new state.
     66   void UpdateInvalidatorState(InvalidatorState state);
     67 
     68   // Returns the current invalidator state.  When called from within
     69   // InvalidationHandler::OnInvalidatorStateChange(), this returns the
     70   // updated state.
     71   InvalidatorState GetInvalidatorState() const;
     72 
     73   bool IsHandlerRegisteredForTest(InvalidationHandler* handler) const;
     74 
     75   // Needed for death tests.
     76   void DetachFromThreadForTest();
     77 
     78  private:
     79   typedef std::map<invalidation::ObjectId, InvalidationHandler*,
     80                    ObjectIdLessThan>
     81       IdHandlerMap;
     82 
     83   InvalidationHandler* ObjectIdToHandler(const invalidation::ObjectId& id);
     84 
     85   base::ThreadChecker thread_checker_;
     86   ObserverList<InvalidationHandler> handlers_;
     87   IdHandlerMap id_to_handler_map_;
     88   InvalidatorState state_;
     89 
     90   DISALLOW_COPY_AND_ASSIGN(InvalidatorRegistrar);
     91 };
     92 
     93 }  // namespace syncer
     94 
     95 #endif  // SYNC_NOTIFIER_INVALIDATOR_REGISTRAR_H_
     96