Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 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_FCM_HANDLER_H_ |
| 6 | #define COMPONENTS_SYNC_INVALIDATIONS_FCM_HANDLER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
Rushan Suleymanov | c2934f9 | 2022-05-25 11:21:28 | [diff] [blame] | 10 | #include "base/containers/circular_deque.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
Rushan Suleymanov | 74e7ee259 | 2020-08-02 19:30:40 | [diff] [blame] | 12 | #include "base/observer_list.h" |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 13 | #include "base/sequence_checker.h" |
Rushan Suleymanov | fd3c269 | 2023-09-08 15:09:00 | [diff] [blame] | 14 | #include "base/time/time.h" |
Paula Vidas | 106640c | 2020-09-07 11:58:49 | [diff] [blame] | 15 | #include "base/timer/timer.h" |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 16 | #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 | |
| 20 | namespace gcm { |
| 21 | class GCMDriver; |
| 22 | } |
| 23 | |
| 24 | namespace instance_id { |
| 25 | class InstanceIDDriver; |
| 26 | } |
| 27 | |
| 28 | namespace syncer { |
| 29 | |
Rushan Suleymanov | 74e7ee259 | 2020-08-02 19:30:40 | [diff] [blame] | 30 | class FCMRegistrationTokenObserver; |
Rushan Suleymanov | 0320db93 | 2020-07-29 10:06:52 | [diff] [blame] | 31 | class InvalidationsListener; |
| 32 | |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 33 | // This handler is used to register with FCM and to process incoming messages. |
| 34 | class 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 Batyrkulov | 4349657 | 2022-09-21 11:05:43 | [diff] [blame] | 45 | // 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 Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 48 | 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 Vidas | c17e9d1 | 2020-09-10 15:22:19 | [diff] [blame] | 52 | // invalidations will be dropped. This method gets called during browser |
| 53 | // shutdown. |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 54 | void StopListening(); |
| 55 | |
Paula Vidas | 970e36a | 2020-10-12 15:30:54 | [diff] [blame] | 56 | // Stop handling incoming invalidations and delete Instance ID. It clears the |
| 57 | // FCM registration token. This method gets called during sign-out. |
Paula Vidas | c17e9d1 | 2020-09-10 15:22:19 | [diff] [blame] | 58 | void StopListeningPermanently(); |
| 59 | |
| 60 | // Returns if the handler is listening for incoming invalidations. |
| 61 | bool IsListening() const; |
| 62 | |
Rushan Suleymanov | 2d1c81700 | 2022-06-09 07:18:47 | [diff] [blame] | 63 | // 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 Suleymanov | 0320db93 | 2020-07-29 10:06:52 | [diff] [blame] | 67 | void AddListener(InvalidationsListener* listener); |
Rushan Suleymanov | 2d1c81700 | 2022-06-09 07:18:47 | [diff] [blame] | 68 | |
Rushan Suleymanov | 1d01440 | 2023-08-14 11:16:35 | [diff] [blame] | 69 | // Returns whether `listener` was added before. |
| 70 | bool HasListener(InvalidationsListener* listener); |
| 71 | |
Rushan Suleymanov | 2d1c81700 | 2022-06-09 07:18:47 | [diff] [blame] | 72 | // Removes |listener|, does nothing if it wasn't added before. |listener| must |
| 73 | // not be nullptr. |
Rushan Suleymanov | 0320db93 | 2020-07-29 10:06:52 | [diff] [blame] | 74 | void RemoveListener(InvalidationsListener* listener); |
| 75 | |
Rushan Suleymanov | 74e7ee259 | 2020-08-02 19:30:40 | [diff] [blame] | 76 | // 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 Batyrkulov | 4349657 | 2022-09-21 11:05:43 | [diff] [blame] | 80 | // 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 Suleymanov | 2234131 | 2020-12-21 15:07:48 | [diff] [blame] | 82 | |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 83 | // 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 Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 95 | // Called when a subscription token is obtained from the GCM server. |
Rushan Suleymanov | fd3c269 | 2023-09-08 15:09:00 | [diff] [blame] | 96 | void DidRetrieveToken(base::TimeTicks fetch_time_for_metrics, |
| 97 | bool is_validation, |
| 98 | const std::string& subscription_token, |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 99 | instance_id::InstanceID::Result result); |
Paula Vidas | 106640c | 2020-09-07 11:58:49 | [diff] [blame] | 100 | void ScheduleNextTokenValidation(); |
| 101 | void StartTokenValidation(); |
Paula Vidas | 106640c | 2020-09-07 11:58:49 | [diff] [blame] | 102 | |
Rushan Suleymanov | fd3c269 | 2023-09-08 15:09:00 | [diff] [blame] | 103 | void StartTokenFetch(bool is_validation); |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 104 | |
| 105 | SEQUENCE_CHECKER(sequence_checker_); |
| 106 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 107 | raw_ptr<gcm::GCMDriver> gcm_driver_ = nullptr; |
| 108 | raw_ptr<instance_id::InstanceIDDriver> instance_id_driver_ = nullptr; |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 109 | const std::string sender_id_; |
| 110 | const std::string app_id_; |
| 111 | |
Shabdan Batyrkulov | 4349657 | 2022-09-21 11:05:43 | [diff] [blame] | 112 | // 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 Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 115 | |
Paula Vidas | 106640c | 2020-09-07 11:58:49 | [diff] [blame] | 116 | base::OneShotTimer token_validation_timer_; |
| 117 | |
Rushan Suleymanov | c2934f9 | 2022-05-25 11:21:28 | [diff] [blame] | 118 | // 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 Suleymanov | 74e7ee259 | 2020-08-02 19:30:40 | [diff] [blame] | 122 | // 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 Suleymanov | 0320db93 | 2020-07-29 10:06:52 | [diff] [blame] | 134 | |
Rushan Suleymanov | 8e8103d | 2020-07-29 09:45:42 | [diff] [blame] | 135 | base::WeakPtrFactory<FCMHandler> weak_ptr_factory_{this}; |
| 136 | }; |
| 137 | |
| 138 | } // namespace syncer |
| 139 | |
| 140 | #endif // COMPONENTS_SYNC_INVALIDATIONS_FCM_HANDLER_H_ |