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