[go: nahoru, domu]

blob: 16779b80b2b3ed7d132c2d480c7d337891adc2fb [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2020 The Chromium Authors
Rushan Suleymanov8e8103d2020-07-29 09:45:422// 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_FCM_HANDLER_H_
6#define COMPONENTS_SYNC_INVALIDATIONS_FCM_HANDLER_H_
7
8#include <string>
9
Rushan Suleymanovc2934f92022-05-25 11:21:2810#include "base/containers/circular_deque.h"
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
Rushan Suleymanov74e7ee2592020-08-02 19:30:4012#include "base/observer_list.h"
Rushan Suleymanov8e8103d2020-07-29 09:45:4213#include "base/sequence_checker.h"
Rushan Suleymanovfd3c2692023-09-08 15:09:0014#include "base/time/time.h"
Paula Vidas106640c2020-09-07 11:58:4915#include "base/timer/timer.h"
Rushan Suleymanov8e8103d2020-07-29 09:45:4216#include "components/gcm_driver/gcm_app_handler.h"
17#include "components/gcm_driver/instance_id/instance_id.h"
18#include "components/keyed_service/core/keyed_service.h"
19
20namespace gcm {
21class GCMDriver;
22}
23
24namespace instance_id {
25class InstanceIDDriver;
26}
27
28namespace syncer {
29
Rushan Suleymanov74e7ee2592020-08-02 19:30:4030class FCMRegistrationTokenObserver;
Rushan Suleymanov0320db932020-07-29 10:06:5231class InvalidationsListener;
32
Rushan Suleymanov8e8103d2020-07-29 09:45:4233// This handler is used to register with FCM and to process incoming messages.
34class FCMHandler : public gcm::GCMAppHandler {
35 public:
36 FCMHandler(gcm::GCMDriver* gcm_driver,
37 instance_id::InstanceIDDriver* instance_id_driver,
38 const std::string& sender_id,
39 const std::string& app_id);
40 ~FCMHandler() override;
41 FCMHandler(const FCMHandler&) = delete;
42 FCMHandler& operator=(const FCMHandler&) = delete;
43
44 // Used to start handling incoming invalidations from the server and to obtain
Shabdan Batyrkulov43496572022-09-21 11:05:4345 // an FCM token. This method gets called after data types are configured.
46 // Before StartListening() is called for the first time, the FCM registration
47 // token will be null.
Rushan Suleymanov8e8103d2020-07-29 09:45:4248 void StartListening();
49
50 // Stop handling incoming invalidations. It doesn't cleanup the FCM
51 // registration token and doesn't unsubscribe from FCM. All incoming
Paula Vidasc17e9d12020-09-10 15:22:1952 // invalidations will be dropped. This method gets called during browser
53 // shutdown.
Rushan Suleymanov8e8103d2020-07-29 09:45:4254 void StopListening();
55
Paula Vidas970e36a2020-10-12 15:30:5456 // Stop handling incoming invalidations and delete Instance ID. It clears the
57 // FCM registration token. This method gets called during sign-out.
Paula Vidasc17e9d12020-09-10 15:22:1958 void StopListeningPermanently();
59
60 // Returns if the handler is listening for incoming invalidations.
61 bool IsListening() const;
62
Rushan Suleymanov2d1c817002022-06-09 07:18:4763 // Add a new |listener| which will be notified on each new incoming
64 // invalidation. |listener| must not be nullptr. Does nothing if the
65 // |listener| has already been added before. When a new |listener| is added,
66 // previously received messages will be immediately replayed.
Rushan Suleymanov0320db932020-07-29 10:06:5267 void AddListener(InvalidationsListener* listener);
Rushan Suleymanov2d1c817002022-06-09 07:18:4768
Rushan Suleymanov1d014402023-08-14 11:16:3569 // Returns whether `listener` was added before.
70 bool HasListener(InvalidationsListener* listener);
71
Rushan Suleymanov2d1c817002022-06-09 07:18:4772 // Removes |listener|, does nothing if it wasn't added before. |listener| must
73 // not be nullptr.
Rushan Suleymanov0320db932020-07-29 10:06:5274 void RemoveListener(InvalidationsListener* listener);
75
Rushan Suleymanov74e7ee2592020-08-02 19:30:4076 // Add or remove an FCM token change observer. |observer| must not be nullptr.
77 void AddTokenObserver(FCMRegistrationTokenObserver* observer);
78 void RemoveTokenObserver(FCMRegistrationTokenObserver* observer);
79
Shabdan Batyrkulov43496572022-09-21 11:05:4380 // Used to get an obtained FCM token. Returns null if it doesn't have a token.
81 const absl::optional<std::string>& GetFCMRegistrationToken() const;
Rushan Suleymanov22341312020-12-21 15:07:4882
Rushan Suleymanov8e8103d2020-07-29 09:45:4283 // GCMAppHandler overrides.
84 void ShutdownHandler() override;
85 void OnStoreReset() override;
86 void OnMessage(const std::string& app_id,
87 const gcm::IncomingMessage& message) override;
88 void OnMessagesDeleted(const std::string& app_id) override;
89 void OnSendError(const std::string& app_id,
90 const gcm::GCMClient::SendErrorDetails& details) override;
91 void OnSendAcknowledged(const std::string& app_id,
92 const std::string& message_id) override;
93
94 private:
Rushan Suleymanov8e8103d2020-07-29 09:45:4295 // Called when a subscription token is obtained from the GCM server.
Rushan Suleymanovfd3c2692023-09-08 15:09:0096 void DidRetrieveToken(base::TimeTicks fetch_time_for_metrics,
97 bool is_validation,
98 const std::string& subscription_token,
Rushan Suleymanov8e8103d2020-07-29 09:45:4299 instance_id::InstanceID::Result result);
Paula Vidas106640c2020-09-07 11:58:49100 void ScheduleNextTokenValidation();
101 void StartTokenValidation();
Paula Vidas106640c2020-09-07 11:58:49102
Rushan Suleymanovfd3c2692023-09-08 15:09:00103 void StartTokenFetch(bool is_validation);
Rushan Suleymanov8e8103d2020-07-29 09:45:42104
105 SEQUENCE_CHECKER(sequence_checker_);
106
Keishi Hattori0e45c022021-11-27 09:25:52107 raw_ptr<gcm::GCMDriver> gcm_driver_ = nullptr;
108 raw_ptr<instance_id::InstanceIDDriver> instance_id_driver_ = nullptr;
Rushan Suleymanov8e8103d2020-07-29 09:45:42109 const std::string sender_id_;
110 const std::string app_id_;
111
Shabdan Batyrkulov43496572022-09-21 11:05:43112 // Contains an FCM registration token. Token is null if the experiment is off
113 // or we don't have a valid token yet and contains valid token otherwise.
114 absl::optional<std::string> fcm_registration_token_;
Rushan Suleymanov8e8103d2020-07-29 09:45:42115
Paula Vidas106640c2020-09-07 11:58:49116 base::OneShotTimer token_validation_timer_;
117
Rushan Suleymanovc2934f92022-05-25 11:21:28118 // A list of the latest incoming messages, used to replay incoming messages
119 // whenever a new listener is added.
120 base::circular_deque<std::string> last_received_messages_;
121
Rushan Suleymanov74e7ee2592020-08-02 19:30:40122 // Contains all listeners to notify about each incoming message in OnMessage
123 // method.
124 base::ObserverList<InvalidationsListener,
125 /*check_empty=*/true,
126 /*allow_reentrancy=*/false>
127 listeners_;
128
129 // Contains all FCM token observers to notify about each token change.
130 base::ObserverList<FCMRegistrationTokenObserver,
131 /*check_empty=*/true,
132 /*allow_reentrancy=*/false>
133 token_observers_;
Rushan Suleymanov0320db932020-07-29 10:06:52134
Rushan Suleymanov8e8103d2020-07-29 09:45:42135 base::WeakPtrFactory<FCMHandler> weak_ptr_factory_{this};
136};
137
138} // namespace syncer
139
140#endif // COMPONENTS_SYNC_INVALIDATIONS_FCM_HANDLER_H_