fcm_handler.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #ifndef COMPONENTS_SYNC_INVALIDATIONS_FCM_HANDLER_H_
  5. #define COMPONENTS_SYNC_INVALIDATIONS_FCM_HANDLER_H_
  6. #include <string>
  7. #include "base/containers/circular_deque.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/observer_list.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/timer/timer.h"
  12. #include "components/gcm_driver/gcm_app_handler.h"
  13. #include "components/gcm_driver/instance_id/instance_id.h"
  14. #include "components/keyed_service/core/keyed_service.h"
  15. namespace gcm {
  16. class GCMDriver;
  17. }
  18. namespace instance_id {
  19. class InstanceIDDriver;
  20. }
  21. namespace syncer {
  22. class FCMRegistrationTokenObserver;
  23. class InvalidationsListener;
  24. // This handler is used to register with FCM and to process incoming messages.
  25. class FCMHandler : public gcm::GCMAppHandler {
  26. public:
  27. FCMHandler(gcm::GCMDriver* gcm_driver,
  28. instance_id::InstanceIDDriver* instance_id_driver,
  29. const std::string& sender_id,
  30. const std::string& app_id);
  31. ~FCMHandler() override;
  32. FCMHandler(const FCMHandler&) = delete;
  33. FCMHandler& operator=(const FCMHandler&) = delete;
  34. // Used to start handling incoming invalidations from the server and to obtain
  35. // an FCM token. This method gets called after sign-in, or during browser
  36. // startup if the user is already signed in. Before StartListening() is called
  37. // for the first time, the FCM registration token will be empty.
  38. void StartListening();
  39. // Stop handling incoming invalidations. It doesn't cleanup the FCM
  40. // registration token and doesn't unsubscribe from FCM. All incoming
  41. // invalidations will be dropped. This method gets called during browser
  42. // shutdown.
  43. void StopListening();
  44. // Stop handling incoming invalidations and delete Instance ID. It clears the
  45. // FCM registration token. This method gets called during sign-out.
  46. void StopListeningPermanently();
  47. // Returns if the handler is listening for incoming invalidations.
  48. bool IsListening() const;
  49. // Add a new |listener| which will be notified on each new incoming
  50. // invalidation. |listener| must not be nullptr. Does nothing if the
  51. // |listener| has already been added before. When a new |listener| is added,
  52. // previously received messages will be immediately replayed.
  53. void AddListener(InvalidationsListener* listener);
  54. // Removes |listener|, does nothing if it wasn't added before. |listener| must
  55. // not be nullptr.
  56. void RemoveListener(InvalidationsListener* listener);
  57. // Add or remove an FCM token change observer. |observer| must not be nullptr.
  58. void AddTokenObserver(FCMRegistrationTokenObserver* observer);
  59. void RemoveTokenObserver(FCMRegistrationTokenObserver* observer);
  60. // Used to get an obtained FCM token. Returns empty string if it hasn't
  61. // been received yet, or if the handler has stopped listening permanently.
  62. const std::string& GetFCMRegistrationToken() const;
  63. // Returns true if an FCM registration token has never been retreived after
  64. // the last StartListening() call.
  65. bool IsWaitingForToken() const;
  66. // GCMAppHandler overrides.
  67. void ShutdownHandler() override;
  68. void OnStoreReset() override;
  69. void OnMessage(const std::string& app_id,
  70. const gcm::IncomingMessage& message) override;
  71. void OnMessagesDeleted(const std::string& app_id) override;
  72. void OnSendError(const std::string& app_id,
  73. const gcm::GCMClient::SendErrorDetails& details) override;
  74. void OnSendAcknowledged(const std::string& app_id,
  75. const std::string& message_id) override;
  76. private:
  77. // Called when a subscription token is obtained from the GCM server.
  78. void DidRetrieveToken(const std::string& subscription_token,
  79. instance_id::InstanceID::Result result);
  80. void ScheduleNextTokenValidation();
  81. void StartTokenValidation();
  82. void DidReceiveTokenForValidation(const std::string& new_token,
  83. instance_id::InstanceID::Result result);
  84. void StartTokenFetch(instance_id::InstanceID::GetTokenCallback callback);
  85. SEQUENCE_CHECKER(sequence_checker_);
  86. raw_ptr<gcm::GCMDriver> gcm_driver_ = nullptr;
  87. raw_ptr<instance_id::InstanceIDDriver> instance_id_driver_ = nullptr;
  88. const std::string sender_id_;
  89. const std::string app_id_;
  90. // Contains an FCM registration token if not empty.
  91. std::string fcm_registration_token_;
  92. base::OneShotTimer token_validation_timer_;
  93. bool waiting_for_token_ = false;
  94. // A list of the latest incoming messages, used to replay incoming messages
  95. // whenever a new listener is added.
  96. base::circular_deque<std::string> last_received_messages_;
  97. // Contains all listeners to notify about each incoming message in OnMessage
  98. // method.
  99. base::ObserverList<InvalidationsListener,
  100. /*check_empty=*/true,
  101. /*allow_reentrancy=*/false>
  102. listeners_;
  103. // Contains all FCM token observers to notify about each token change.
  104. base::ObserverList<FCMRegistrationTokenObserver,
  105. /*check_empty=*/true,
  106. /*allow_reentrancy=*/false>
  107. token_observers_;
  108. base::WeakPtrFactory<FCMHandler> weak_ptr_factory_{this};
  109. };
  110. } // namespace syncer
  111. #endif // COMPONENTS_SYNC_INVALIDATIONS_FCM_HANDLER_H_