gcm_driver_desktop.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright 2014 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_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_
  5. #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <tuple>
  10. #include <vector>
  11. #include "base/compiler_specific.h"
  12. #include "base/containers/queue.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/observer_list.h"
  16. #include "base/time/time.h"
  17. #include "base/tuple.h"
  18. #include "components/gcm_driver/crypto/gcm_decryption_result.h"
  19. #include "components/gcm_driver/gcm_client.h"
  20. #include "components/gcm_driver/gcm_connection_observer.h"
  21. #include "components/gcm_driver/gcm_driver.h"
  22. #include "mojo/public/cpp/bindings/pending_receiver.h"
  23. #include "services/network/public/mojom/proxy_resolving_socket.mojom.h"
  24. class PrefService;
  25. namespace base {
  26. class FilePath;
  27. class SequencedTaskRunner;
  28. }
  29. namespace network {
  30. class NetworkConnectionTracker;
  31. class SharedURLLoaderFactory;
  32. }
  33. namespace gcm {
  34. class GCMAccountMapper;
  35. class GCMAppHandler;
  36. class GCMClientFactory;
  37. enum class GCMDecryptionResult;
  38. class GCMDelayedTaskController;
  39. // GCMDriver implementation for desktop and Chrome OS, using GCMClient.
  40. class GCMDriverDesktop : public GCMDriver,
  41. protected InstanceIDHandler {
  42. public:
  43. // |remove_account_mappings_with_email_key| indicates whether account mappings
  44. // having email as account key should be removed while loading. This is
  45. // required during the migration of account identifier from email to Gaia ID.
  46. GCMDriverDesktop(
  47. std::unique_ptr<GCMClientFactory> gcm_client_factory,
  48. const GCMClient::ChromeBuildInfo& chrome_build_info,
  49. PrefService* prefs,
  50. const base::FilePath& store_path,
  51. bool remove_account_mappings_with_email_key,
  52. base::RepeatingCallback<void(
  53. mojo::PendingReceiver<network::mojom::ProxyResolvingSocketFactory>)>
  54. get_socket_factory_callback,
  55. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_for_ui,
  56. network::NetworkConnectionTracker* network_connection_tracker,
  57. const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
  58. const scoped_refptr<base::SequencedTaskRunner>& io_thread,
  59. const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
  60. GCMDriverDesktop(const GCMDriverDesktop&) = delete;
  61. GCMDriverDesktop& operator=(const GCMDriverDesktop&) = delete;
  62. ~GCMDriverDesktop() override;
  63. // GCMDriver implementation:
  64. void ValidateRegistration(const std::string& app_id,
  65. const std::vector<std::string>& sender_ids,
  66. const std::string& registration_id,
  67. ValidateRegistrationCallback callback) override;
  68. void Shutdown() override;
  69. void OnSignedIn() override;
  70. void OnSignedOut() override;
  71. void AddAppHandler(const std::string& app_id,
  72. GCMAppHandler* handler) override;
  73. void RemoveAppHandler(const std::string& app_id) override;
  74. void AddConnectionObserver(GCMConnectionObserver* observer) override;
  75. void RemoveConnectionObserver(GCMConnectionObserver* observer) override;
  76. GCMClient* GetGCMClientForTesting() const override;
  77. bool IsStarted() const override;
  78. bool IsConnected() const override;
  79. void GetGCMStatistics(GetGCMStatisticsCallback callback,
  80. ClearActivityLogs clear_logs) override;
  81. void SetGCMRecording(const GCMStatisticsRecordingCallback& callback,
  82. bool recording) override;
  83. void SetAccountTokens(
  84. const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override;
  85. void UpdateAccountMapping(const AccountMapping& account_mapping) override;
  86. void RemoveAccountMapping(const CoreAccountId& account_id) override;
  87. base::Time GetLastTokenFetchTime() override;
  88. void SetLastTokenFetchTime(const base::Time& time) override;
  89. InstanceIDHandler* GetInstanceIDHandlerInternal() override;
  90. void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
  91. void RemoveHeartbeatInterval(const std::string& scope) override;
  92. protected:
  93. // GCMDriver implementation:
  94. GCMClient::Result EnsureStarted(GCMClient::StartMode start_mode) override;
  95. void RegisterImpl(const std::string& app_id,
  96. const std::vector<std::string>& sender_ids) override;
  97. void UnregisterImpl(const std::string& app_id) override;
  98. void SendImpl(const std::string& app_id,
  99. const std::string& receiver_id,
  100. const OutgoingMessage& message) override;
  101. void RecordDecryptionFailure(const std::string& app_id,
  102. GCMDecryptionResult result) override;
  103. // InstanceIDHandler implementation:
  104. void GetToken(const std::string& app_id,
  105. const std::string& authorized_entity,
  106. const std::string& scope,
  107. base::TimeDelta time_to_live,
  108. GetTokenCallback callback) override;
  109. void ValidateToken(const std::string& app_id,
  110. const std::string& authorized_entity,
  111. const std::string& scope,
  112. const std::string& token,
  113. ValidateTokenCallback callback) override;
  114. void DeleteToken(const std::string& app_id,
  115. const std::string& authorized_entity,
  116. const std::string& scope,
  117. DeleteTokenCallback callback) override;
  118. void AddInstanceIDData(const std::string& app_id,
  119. const std::string& instance_id,
  120. const std::string& extra_data) override;
  121. void RemoveInstanceIDData(const std::string& app_id) override;
  122. void GetInstanceIDData(const std::string& app_id,
  123. GetInstanceIDDataCallback callback) override;
  124. private:
  125. class IOWorker;
  126. typedef std::tuple<std::string, std::string, std::string> TokenTuple;
  127. struct TokenTupleComparer {
  128. bool operator()(const TokenTuple& a, const TokenTuple& b) const;
  129. };
  130. void DoValidateRegistration(scoped_refptr<RegistrationInfo> registration_info,
  131. const std::string& registration_id,
  132. ValidateRegistrationCallback callback);
  133. // Stops the GCM service. It can be restarted by calling EnsureStarted again.
  134. void Stop();
  135. // Remove cached data when GCM service is stopped.
  136. void RemoveCachedData();
  137. void DoRegister(const std::string& app_id,
  138. const std::vector<std::string>& sender_ids);
  139. void DoUnregister(const std::string& app_id);
  140. void DoSend(const std::string& app_id,
  141. const std::string& receiver_id,
  142. const OutgoingMessage& message);
  143. void DoAddInstanceIDData(const std::string& app_id,
  144. const std::string& instance_id,
  145. const std::string& extra_data);
  146. void DoRemoveInstanceIDData(const std::string& app_id);
  147. void DoGetInstanceIDData(const std::string& app_id);
  148. void DoGetToken(const std::string& app_id,
  149. const std::string& authorized_entity,
  150. const std::string& scope,
  151. base::TimeDelta time_to_live);
  152. void DoDeleteToken(const std::string& app_id,
  153. const std::string& authorized_entity,
  154. const std::string& scope);
  155. // Callbacks posted from IO thread to UI thread.
  156. void MessageReceived(const std::string& app_id,
  157. const IncomingMessage& message);
  158. void MessagesDeleted(const std::string& app_id);
  159. void MessageSendError(const std::string& app_id,
  160. const GCMClient::SendErrorDetails& send_error_details);
  161. void SendAcknowledged(const std::string& app_id,
  162. const std::string& message_id);
  163. void GCMClientReady(const std::vector<AccountMapping>& account_mappings,
  164. const base::Time& last_token_fetch_time);
  165. void OnConnected(const net::IPEndPoint& ip_endpoint);
  166. void OnDisconnected();
  167. void OnStoreReset();
  168. void OnActivityRecorded(const GCMClient::GCMStatistics& stats);
  169. void GetInstanceIDDataFinished(const std::string& app_id,
  170. const std::string& instance_id,
  171. const std::string& extra_data);
  172. void GetTokenFinished(const std::string& app_id,
  173. const std::string& authorized_entity,
  174. const std::string& scope,
  175. const std::string& token,
  176. GCMClient::Result result);
  177. void DeleteTokenFinished(const std::string& app_id,
  178. const std::string& authorized_entity,
  179. const std::string& scope,
  180. GCMClient::Result result);
  181. // Flag to indicate whether the user is signed in to a GAIA account.
  182. bool signed_in_;
  183. // Flag to indicate if GCM is started.
  184. bool gcm_started_;
  185. // Flag to indicate the last known state of the GCM client. Because this
  186. // flag lives on the UI thread, while the GCM client lives on the IO thread,
  187. // it may be out of date while connection changes are happening.
  188. bool connected_;
  189. // List of observers to notify when connection state changes.
  190. base::ObserverList<GCMConnectionObserver, false>::Unchecked
  191. connection_observer_list_;
  192. // Account mapper. Only works when user is signed in.
  193. std::unique_ptr<GCMAccountMapper> account_mapper_;
  194. // Time of last token fetching.
  195. base::Time last_token_fetch_time_;
  196. scoped_refptr<base::SequencedTaskRunner> ui_thread_;
  197. scoped_refptr<base::SequencedTaskRunner> io_thread_;
  198. std::unique_ptr<GCMDelayedTaskController> delayed_task_controller_;
  199. // For all the work occurring on the IO thread. Must be destroyed on the IO
  200. // thread.
  201. std::unique_ptr<IOWorker> io_worker_;
  202. // Callback for SetGCMRecording.
  203. GCMStatisticsRecordingCallback gcm_statistics_recording_callback_;
  204. // Callbacks for GetInstanceIDData. Initializing InstanceID is asynchronous,
  205. // which leads to a race condition when recreating an InstanceID before such
  206. // initialization has finished, causing multiple callbacks to be in flight.
  207. // Expecting all InstanceID users to care for that is fragile and complicated,
  208. // so allow for a queue of callbacks to be stored here instead.
  209. //
  210. // Note that other InstanceID callbacks don't have this concern, as they all
  211. // wait for initialization of the InstanceID instance to have completed.
  212. std::map<std::string, base::queue<GetInstanceIDDataCallback>>
  213. get_instance_id_data_callbacks_;
  214. // Callbacks for GetToken/DeleteToken.
  215. std::map<TokenTuple, GetTokenCallback, TokenTupleComparer>
  216. get_token_callbacks_;
  217. std::map<TokenTuple, DeleteTokenCallback, TokenTupleComparer>
  218. delete_token_callbacks_;
  219. // Used to pass a weak pointer to the IO worker.
  220. base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_{this};
  221. };
  222. } // namespace gcm
  223. #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_