fcm_network_handler.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2018 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_INVALIDATION_IMPL_FCM_NETWORK_HANDLER_H_
  5. #define COMPONENTS_INVALIDATION_IMPL_FCM_NETWORK_HANDLER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/time/clock.h"
  9. #include "base/time/time.h"
  10. #include "base/timer/timer.h"
  11. #include "base/values.h"
  12. #include "components/gcm_driver/gcm_app_handler.h"
  13. #include "components/gcm_driver/instance_id/instance_id.h"
  14. #include "components/invalidation/impl/channels_states.h"
  15. #include "components/invalidation/impl/fcm_sync_network_channel.h"
  16. #include "components/prefs/pref_registry_simple.h"
  17. #include "components/prefs/pref_service.h"
  18. namespace gcm {
  19. class GCMDriver;
  20. }
  21. namespace instance_id {
  22. class InstanceIDDriver;
  23. }
  24. namespace invalidation {
  25. /*
  26. * The class responsible for communication via GCM channel:
  27. * - It retrieves the token required for the subscription
  28. * and passes it by invoking token callback.
  29. * - It receives the messages and passes them to the
  30. * invalidation infrustructure, so they can be converted to the
  31. * invalidations and consumed by listeners.
  32. */
  33. class FCMNetworkHandler : public gcm::GCMAppHandler,
  34. public FCMSyncNetworkChannel {
  35. public:
  36. FCMNetworkHandler(gcm::GCMDriver* gcm_driver,
  37. instance_id::InstanceIDDriver* instance_id_driver,
  38. const std::string& sender_id,
  39. const std::string& app_id);
  40. FCMNetworkHandler(const FCMNetworkHandler& other) = delete;
  41. FCMNetworkHandler& operator=(const FCMNetworkHandler& other) = delete;
  42. ~FCMNetworkHandler() override;
  43. // Just calls std::make_unique. For ease of base::Bind'ing.
  44. static std::unique_ptr<FCMNetworkHandler> Create(
  45. gcm::GCMDriver* gcm_driver,
  46. instance_id::InstanceIDDriver* instance_id_driver,
  47. const std::string& sender_id,
  48. const std::string& app_id);
  49. bool IsListening() const;
  50. void UpdateChannelState(FcmChannelState state);
  51. // FCMSyncNetworkChannel overrides.
  52. void StartListening() override;
  53. void StopListening() override;
  54. // GCMAppHandler overrides.
  55. void ShutdownHandler() override;
  56. void OnStoreReset() override;
  57. void OnMessage(const std::string& app_id,
  58. const gcm::IncomingMessage& message) override;
  59. void OnMessagesDeleted(const std::string& app_id) override;
  60. void OnSendError(const std::string& app_id,
  61. const gcm::GCMClient::SendErrorDetails& details) override;
  62. void OnSendAcknowledged(const std::string& app_id,
  63. const std::string& message_id) override;
  64. void SetTokenValidationTimerForTesting(
  65. std::unique_ptr<base::OneShotTimer> token_validation_timer);
  66. void RequestDetailedStatus(
  67. const base::RepeatingCallback<void(base::Value::Dict)>& callback)
  68. override;
  69. private:
  70. struct FCMNetworkHandlerDiagnostic {
  71. FCMNetworkHandlerDiagnostic();
  72. // Collect all the internal variables in a single readable dictionary.
  73. base::Value::Dict CollectDebugData() const;
  74. std::string RegistrationResultToString(
  75. const instance_id::InstanceID::Result result) const;
  76. std::string token;
  77. instance_id::InstanceID::Result registration_result =
  78. instance_id::InstanceID::UNKNOWN_ERROR;
  79. instance_id::InstanceID::Result token_verification_result =
  80. instance_id::InstanceID::UNKNOWN_ERROR;
  81. bool token_changed = false;
  82. base::Time instance_id_token_requested;
  83. base::Time instance_id_token_was_received;
  84. base::Time instance_id_token_verification_requested;
  85. base::Time instance_id_token_verified;
  86. int token_validation_requested_num = 0;
  87. };
  88. // Called when a subscription token is obtained from the GCM server.
  89. void DidRetrieveToken(const std::string& subscription_token,
  90. instance_id::InstanceID::Result result);
  91. void ScheduleNextTokenValidation();
  92. void StartTokenValidation();
  93. void DidReceiveTokenForValidation(const std::string& new_token,
  94. instance_id::InstanceID::Result result);
  95. const raw_ptr<gcm::GCMDriver> gcm_driver_;
  96. const raw_ptr<instance_id::InstanceIDDriver> instance_id_driver_;
  97. FcmChannelState channel_state_ = FcmChannelState::NOT_STARTED;
  98. std::string token_;
  99. std::unique_ptr<base::OneShotTimer> token_validation_timer_;
  100. const std::string sender_id_;
  101. const std::string app_id_;
  102. FCMNetworkHandlerDiagnostic diagnostic_info_;
  103. base::WeakPtrFactory<FCMNetworkHandler> weak_ptr_factory_{this};
  104. };
  105. } // namespace invalidation
  106. #endif // COMPONENTS_INVALIDATION_IMPL_FCM_NETWORK_HANDLER_H_