fcm_sync_network_channel.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_SYNC_NETWORK_CHANNEL_H_
  5. #define COMPONENTS_INVALIDATION_IMPL_FCM_SYNC_NETWORK_CHANNEL_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/observer_list.h"
  9. #include "base/values.h"
  10. #include "components/invalidation/impl/channels_states.h"
  11. namespace invalidation {
  12. // FCMSyncNetworkChannel implements common tasks needed from the network by
  13. // client:
  14. // - registering message callbacks
  15. // - notifying on network problems
  16. class FCMSyncNetworkChannel {
  17. public:
  18. class Observer {
  19. public:
  20. virtual void OnFCMChannelStateChanged(
  21. FcmChannelState invalidator_state) = 0;
  22. };
  23. // See SetMessageReceiver below.
  24. // payload - additional info specific to the invalidation
  25. // private_topic - the internal (to FCM) representation for the public topic
  26. // public_topic - the topic which was invalidated, e.g. in case of Chrome
  27. // Sync it'll be BOOKMARK or PASSWORD
  28. // version - version number of the invalidation
  29. using MessageCallback =
  30. base::RepeatingCallback<void(const std::string& payload,
  31. const std::string& private_topic,
  32. const std::string& public_topic,
  33. int64_t version)>;
  34. using TokenCallback = base::RepeatingCallback<void(const std::string& token)>;
  35. FCMSyncNetworkChannel();
  36. virtual ~FCMSyncNetworkChannel();
  37. virtual void StartListening() = 0;
  38. virtual void StopListening() = 0;
  39. // Sets the receiver to which messages from the data center will be delivered.
  40. // The callback will be invoked whenever an invalidation message is received
  41. // from FCM. It is *not* guaranteed to be invoked exactly once or in-order
  42. // (with respect to the invalidation's version number).
  43. void SetMessageReceiver(MessageCallback incoming_receiver);
  44. // Sets the receiver to which FCM registration token will be delivered.
  45. // The callback will be invoked whenever a new InstanceID token becomes
  46. // available.
  47. void SetTokenReceiver(TokenCallback token_receiver);
  48. // Classes interested in network channel state changes should implement
  49. // FCMSyncNetworkChannel::Observer and register here.
  50. void AddObserver(Observer* observer);
  51. void RemoveObserver(Observer* observer);
  52. // Subclass should implement RequestDetailedStatus to provide debugging
  53. // information.
  54. virtual void RequestDetailedStatus(
  55. const base::RepeatingCallback<void(base::Value::Dict)>& callback) = 0;
  56. protected:
  57. // Subclass should notify about connection state through
  58. // NotifyChannelStateChange. If communication doesn't work and it is possible
  59. // that invalidations from server will not reach this client then channel
  60. // should call this function with TRANSIENT_INVALIDATION_ERROR.
  61. void NotifyChannelStateChange(FcmChannelState invalidator_state);
  62. // Subclass should call DeliverIncomingMessage for message to reach
  63. // invalidations library.
  64. bool DeliverIncomingMessage(const std::string& payload,
  65. const std::string& private_topic,
  66. const std::string& public_topic,
  67. int64_t version);
  68. // Subclass should call DeliverToken for token to reach registration
  69. // manager.
  70. bool DeliverToken(const std::string& token);
  71. private:
  72. // Callbacks into invalidation library
  73. MessageCallback incoming_receiver_;
  74. TokenCallback token_receiver_;
  75. int received_messages_count_;
  76. std::string token_;
  77. base::ObserverList<Observer>::Unchecked observers_;
  78. };
  79. } // namespace invalidation
  80. #endif // COMPONENTS_INVALIDATION_IMPL_FCM_SYNC_NETWORK_CHANNEL_H_