146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#ifndef COMPONENTS_INVALIDATION_INVALIDATOR_REGISTRAR_H_
646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#define COMPONENTS_INVALIDATION_INVALIDATOR_REGISTRAR_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/observer_list.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread_checker.h"
1346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/invalidation/invalidation_export.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/invalidation/invalidation_handler.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/invalidation_util.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace invalidation {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ObjectId;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace invalidation
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace syncer {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class ObjectIdInvalidationMap;
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A helper class for implementations of the Invalidator interface.  It helps
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// keep track of registered handlers and which object ID registrations are
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// associated with which handlers, so implementors can just reuse the logic
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// here to dispatch invalidations and other interesting notifications.
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class INVALIDATION_EXPORT InvalidatorRegistrar {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InvalidatorRegistrar();
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is an error to have registered handlers on destruction.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~InvalidatorRegistrar();
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Starts sending notifications to |handler|.  |handler| must not be NULL,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and it must already be registered.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RegisterHandler(InvalidationHandler* handler);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates the set of ObjectIds associated with |handler|.  |handler| must
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not be NULL, and must already be registered.  An ID must be registered for
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // at most one handler.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateRegisteredIds(InvalidationHandler* handler,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           const ObjectIdSet& ids);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stops sending notifications to |handler|.  |handler| must not be NULL, and
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it must already be registered.  Note that this doesn't unregister the IDs
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // associated with |handler|.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UnregisterHandler(InvalidationHandler* handler);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ObjectIdSet GetRegisteredIds(InvalidationHandler* handler) const;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the set of all IDs that are registered to some handler (even
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // handlers that have been unregistered).
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ObjectIdSet GetAllRegisteredIds() const;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sorts incoming invalidations into a bucket for each handler and then
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // dispatches the batched invalidations to the corresponding handler.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Invalidations for IDs with no corresponding handler are dropped, as are
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // invalidations for handlers that are not added.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DispatchInvalidationsToHandlers(
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const ObjectIdInvalidationMap& invalidation_map);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates the invalidator state to the given one and then notifies
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // all handlers.  Note that the order is important; handlers that
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // call GetInvalidatorState() when notified will see the new state.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateInvalidatorState(InvalidatorState state);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the current invalidator state.  When called from within
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // InvalidationHandler::OnInvalidatorStateChange(), this returns the
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // updated state.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InvalidatorState GetInvalidatorState() const;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Gets a new map for the name of invalidator handlers and their
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // objects id. This is used by the InvalidatorLogger to be able
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // to display every registered handlers and its objectsIds.
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::map<std::string, ObjectIdSet> GetSanitizedHandlersIdsMap();
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsHandlerRegisteredForTest(InvalidationHandler* handler) const;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Needed for death tests.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DetachFromThreadForTest();
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  typedef std::map<InvalidationHandler*, ObjectIdSet> HandlerIdsMap;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::ThreadChecker thread_checker_;
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ObserverList<InvalidationHandler, true> handlers_;
894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  HandlerIdsMap handler_to_ids_map_;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InvalidatorState state_;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(InvalidatorRegistrar);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace syncer
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif  // COMPONENTS_INVALIDATION_INVALIDATOR_REGISTRAR_H_
98