1 // Copyright 2020 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_SYNC_INVALIDATIONS_SYNC_INVALIDATIONS_SERVICE_H_
6 #define COMPONENTS_SYNC_INVALIDATIONS_SYNC_INVALIDATIONS_SERVICE_H_
7 
8 #include <string>
9 
10 #include "base/callback.h"
11 #include "components/keyed_service/core/keyed_service.h"
12 #include "components/sync/base/model_type.h"
13 
14 namespace syncer {
15 class FCMRegistrationTokenObserver;
16 class InvalidationsListener;
17 class InterestedDataTypesHandler;
18 
19 // Service which is used to register with FCM. It is used to obtain an FCM token
20 // which is used to send invalidations from the server. The service also
21 // provides incoming invalidations handling and an interface to subscribe to
22 // invalidations. To subscribe for invalidations a new InvalidationsListener
23 // should be added.
24 class SyncInvalidationsService : public KeyedService {
25  public:
26   // Data types which are newly marked as interesting will be passed to the
27   // callback.
28   using InterestedDataTypesAppliedCallback =
29       base::OnceCallback<void(const ModelTypeSet&)>;
30 
31   // Start or stop listening to invalidations.
32   virtual void SetActive(bool active) = 0;
33 
34   // Add or remove a new listener which will be notified on each new incoming
35   // invalidation. |listener| must not be nullptr. If there is no such
36   // |listener| then RemoveListener will do nothing.
37   virtual void AddListener(InvalidationsListener* listener) = 0;
38   virtual void RemoveListener(InvalidationsListener* listener) = 0;
39 
40   // Add or remove an FCM token change observer. |observer| must not be nullptr.
41   virtual void AddTokenObserver(FCMRegistrationTokenObserver* observer) = 0;
42   virtual void RemoveTokenObserver(FCMRegistrationTokenObserver* observer) = 0;
43 
44   // Used to get an obtained FCM token. Returns empty string if it hasn't been
45   // received yet, or if the device has stopped listening to invalidations.
46   virtual const std::string& GetFCMRegistrationToken() const = 0;
47 
48   // Set the interested data types change handler. |handler| can be nullptr to
49   // unregister any existing handler. There can be at most one handler.
50   virtual void SetInterestedDataTypesHandler(
51       InterestedDataTypesHandler* handler) = 0;
52 
53   // Get or set for which data types should the device receive invalidations.
54   virtual const ModelTypeSet& GetInterestedDataTypes() const = 0;
55   virtual void SetInterestedDataTypes(
56       const ModelTypeSet& data_types,
57       InterestedDataTypesAppliedCallback callback) = 0;
58 };
59 
60 }  // namespace syncer
61 
62 #endif  // COMPONENTS_SYNC_INVALIDATIONS_SYNC_INVALIDATIONS_SERVICE_H_
63