10de6073388f4e2780db8536178b129cd8f6ab386Torne (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)
50de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#ifndef COMPONENTS_INVALIDATION_INVALIDATION_SERVICE_H_
60de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#define COMPONENTS_INVALIDATION_INVALIDATION_SERVICE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/callback_forward.h"
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/invalidation_util.h"
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/invalidator_state.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass IdentityProvider;
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace syncer {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class InvalidationHandler;
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace syncer
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace invalidation {
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class InvalidationLogger;
20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Interface for classes that handle invalidation registrations and send out
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// invalidations to register handlers.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Invalidation clients should follow the pattern below:
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When starting the client:
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   frontend->RegisterInvalidationHandler(client_handler);
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When the set of IDs to register changes for the client during its lifetime
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (i.e., between calls to RegisterInvalidationHandler(client_handler) and
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// UnregisterInvalidationHandler(client_handler):
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   frontend->UpdateRegisteredInvalidationIds(client_handler, client_ids);
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// When shutting down the client for browser shutdown:
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   frontend->UnregisterInvalidationHandler(client_handler);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Note that there's no call to UpdateRegisteredIds() -- this is because the
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// invalidation API persists registrations across browser restarts.
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// When permanently shutting down the client, e.g. when disabling the related
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// feature:
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//   frontend->UpdateRegisteredInvalidationIds(client_handler, ObjectIdSet());
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//   frontend->UnregisterInvalidationHandler(client_handler);
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If an invalidation handler cares about the invalidator state, it should also
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// do the following when starting the client:
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   invalidator_state = frontend->GetInvalidatorState();
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It can also do the above in OnInvalidatorStateChange(), or it can use the
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// argument to OnInvalidatorStateChange().
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// It is an error to have registered handlers when an
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// InvalidationFrontend is shut down; clients must ensure that they
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// unregister themselves before then. (Depending on the
60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// InvalidationFrontend, shutdown may be equivalent to destruction, or
61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// a separate function call like Shutdown()).
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTE(akalin): Invalidations that come in during browser shutdown may get
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// dropped.  This won't matter once we have an Acknowledge API, though: see
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://crbug.com/78462 and http://crbug.com/124149.
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class InvalidationService {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
68f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual ~InvalidationService() {}
69f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Starts sending notifications to |handler|.  |handler| must not be NULL,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and it must not already be registered.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handler registrations are persisted across restarts of sync.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RegisterInvalidationHandler(
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      syncer::InvalidationHandler* handler) = 0;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates the set of ObjectIds associated with |handler|.  |handler| must
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not be NULL, and must already be registered.  An ID must be registered for
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // at most one handler.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Registered IDs are persisted across restarts of sync.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UpdateRegisteredInvalidationIds(
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      syncer::InvalidationHandler* handler,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const syncer::ObjectIdSet& ids) = 0;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stops sending notifications to |handler|.  |handler| must not be NULL, and
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it must already be registered.  Note that this doesn't unregister the IDs
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // associated with |handler|.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handler registrations are persisted across restarts of sync.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UnregisterInvalidationHandler(
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      syncer::InvalidationHandler* handler) = 0;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the current invalidator state.  When called from within
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // InvalidationHandler::OnInvalidatorStateChange(), this must return
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the updated state.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual syncer::InvalidatorState GetInvalidatorState() const = 0;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Returns the ID belonging to this invalidation client.  Can be used to
100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // prevent the receipt of notifications of our own changes.
101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual std::string GetInvalidatorClientId() const = 0;
102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Return the logger used to debug invalidations
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual InvalidationLogger* GetInvalidationLogger() = 0;
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Triggers requests of internal status.
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void RequestDetailedStatus(
108effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      base::Callback<void(const base::DictionaryValue&)> post_caller) const = 0;
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Returns the identity provider.
1110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual IdentityProvider* GetIdentityProvider() = 0;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
114a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}  // namespace invalidation
115a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1160de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#endif  // COMPONENTS_INVALIDATION_INVALIDATION_SERVICE_H_
117