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.
4
5#ifndef COMPONENTS_INVALIDATION_INVALIDATOR_REGISTRAR_H_
6#define COMPONENTS_INVALIDATION_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 "components/invalidation/invalidation_export.h"
14#include "components/invalidation/invalidation_handler.h"
15#include "components/invalidation/invalidation_util.h"
16
17namespace invalidation {
18class ObjectId;
19}  // namespace invalidation
20
21namespace syncer {
22
23class 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.
29class INVALIDATION_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  // 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();
78
79  bool IsHandlerRegisteredForTest(InvalidationHandler* handler) const;
80
81  // Needed for death tests.
82  void DetachFromThreadForTest();
83
84 private:
85  typedef std::map<InvalidationHandler*, ObjectIdSet> HandlerIdsMap;
86
87  base::ThreadChecker thread_checker_;
88  ObserverList<InvalidationHandler, true> handlers_;
89  HandlerIdsMap handler_to_ids_map_;
90  InvalidatorState state_;
91
92  DISALLOW_COPY_AND_ASSIGN(InvalidatorRegistrar);
93};
94
95}  // namespace syncer
96
97#endif  // COMPONENTS_INVALIDATION_INVALIDATOR_REGISTRAR_H_
98