1 // Copyright 2014 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.
5 #ifndef COMPONENTS_INVALIDATION_IMPL_INVALIDATOR_REGISTRAR_H_
6 #define COMPONENTS_INVALIDATION_IMPL_INVALIDATOR_REGISTRAR_H_
10 #include "base/basictypes.h"
11 #include "base/observer_list.h"
12 #include "base/threading/thread_checker.h"
13 #include "components/invalidation/public/invalidation_export.h"
14 #include "components/invalidation/public/invalidation_handler.h"
15 #include "components/invalidation/public/invalidation_util.h"
17 namespace invalidation
{
19 } // namespace invalidation
23 class ObjectIdInvalidationMap
;
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 INVALIDATION_EXPORT InvalidatorRegistrar
{
31 InvalidatorRegistrar();
33 // It is an error to have registered handlers on destruction.
34 ~InvalidatorRegistrar();
36 // Starts sending notifications to |handler|. |handler| must not be NULL,
37 // and it must already be registered.
38 void RegisterHandler(InvalidationHandler
* handler
);
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. If ID is already registered function returns false.
43 bool UpdateRegisteredIds(InvalidationHandler
* handler
,
44 const ObjectIdSet
& ids
) WARN_UNUSED_RESULT
;
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
);
51 ObjectIdSet
GetRegisteredIds(InvalidationHandler
* handler
) const;
53 // Returns the set of all IDs that are registered to some handler (even
54 // handlers that have been unregistered).
55 ObjectIdSet
GetAllRegisteredIds() const;
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
);
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
);
69 // Returns the current invalidator state. When called from within
70 // InvalidationHandler::OnInvalidatorStateChange(), this returns the
72 InvalidatorState
GetInvalidatorState() const;
74 // Gets a new map for the name of invalidator handlers and their
75 // objects id. This is used by the InvalidatorLogger to be able
76 // to display every registered handlers and its objectsIds.
77 std::map
<std::string
, ObjectIdSet
> GetSanitizedHandlersIdsMap();
79 bool IsHandlerRegisteredForTest(const InvalidationHandler
* handler
) const;
81 // Needed for death tests.
82 void DetachFromThreadForTest();
85 typedef std::map
<InvalidationHandler
*, ObjectIdSet
> HandlerIdsMap
;
87 base::ThreadChecker thread_checker_
;
88 base::ObserverList
<InvalidationHandler
, true> handlers_
;
89 HandlerIdsMap handler_to_ids_map_
;
90 InvalidatorState state_
;
92 DISALLOW_COPY_AND_ASSIGN(InvalidatorRegistrar
);
97 #endif // COMPONENTS_INVALIDATION_IMPL_INVALIDATOR_REGISTRAR_H_