gcm_account_mapper.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_ACCOUNT_MAPPER_H_
  5. #define COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "components/gcm_driver/gcm_app_handler.h"
  13. #include "components/gcm_driver/gcm_client.h"
  14. #include "google_apis/gcm/engine/account_mapping.h"
  15. namespace base {
  16. class Clock;
  17. }
  18. namespace gcm {
  19. class GCMDriver;
  20. extern const char kGCMAccountMapperAppId[];
  21. // Class for mapping signed-in GAIA accounts to the GCM Device ID.
  22. class GCMAccountMapper : public GCMAppHandler {
  23. public:
  24. // List of account mappings.
  25. using AccountMappings = std::vector<AccountMapping>;
  26. using DispatchMessageCallback =
  27. base::RepeatingCallback<void(const std::string& app_id,
  28. const IncomingMessage& message)>;
  29. explicit GCMAccountMapper(GCMDriver* gcm_driver);
  30. GCMAccountMapper(const GCMAccountMapper&) = delete;
  31. GCMAccountMapper& operator=(const GCMAccountMapper&) = delete;
  32. ~GCMAccountMapper() override;
  33. void Initialize(const AccountMappings& account_mappings,
  34. const DispatchMessageCallback& callback);
  35. // Called by AccountTracker, when a new list of account tokens is available.
  36. // This will cause a refresh of account mappings and sending updates to GCM.
  37. void SetAccountTokens(
  38. const std::vector<GCMClient::AccountTokenInfo>& account_tokens);
  39. // Implementation of GCMAppHandler:
  40. void ShutdownHandler() override;
  41. void OnStoreReset() override;
  42. void OnMessage(const std::string& app_id,
  43. const IncomingMessage& message) override;
  44. void OnMessagesDeleted(const std::string& app_id) override;
  45. void OnSendError(
  46. const std::string& app_id,
  47. const GCMClient::SendErrorDetails& send_error_details) override;
  48. void OnSendAcknowledged(const std::string& app_id,
  49. const std::string& message_id) override;
  50. bool CanHandle(const std::string& app_id) const override;
  51. private:
  52. friend class GCMAccountMapperTest;
  53. typedef std::map<std::string, OutgoingMessage> OutgoingMessages;
  54. // Checks whether account mapper is ready to process new account tokens.
  55. bool IsReady();
  56. // Informs GCM of an added or refreshed account mapping.
  57. void SendAddMappingMessage(AccountMapping& account_mapping);
  58. // Informs GCM of a removed account mapping.
  59. void SendRemoveMappingMessage(AccountMapping& account_mapping);
  60. void CreateAndSendMessage(const AccountMapping& account_mapping);
  61. // Callback for sending a message.
  62. void OnSendFinished(const CoreAccountId& account_id,
  63. const std::string& message_id,
  64. GCMClient::Result result);
  65. // Gets a registration for account mapper from GCM.
  66. void GetRegistration();
  67. // Callback for registering with GCM.
  68. void OnRegisterFinished(const std::string& registration_id,
  69. GCMClient::Result result);
  70. // Checks whether the update can be triggered now. If the current time is
  71. // within reasonable time (6 hours) of when the update is due, we want to
  72. // trigger the update immediately to take advantage of a fresh OAuth2 token.
  73. bool CanTriggerUpdate(const base::Time& last_update_time) const;
  74. // Checks whether last status change is older than a TTL of a message.
  75. bool IsLastStatusChangeOlderThanTTL(
  76. const AccountMapping& account_mapping) const;
  77. // Finds an account mapping in |accounts_| by |account_id|.
  78. AccountMapping* FindMappingByAccountId(const CoreAccountId& account_id);
  79. // Finds an account mapping in |accounts_| by |message_id|.
  80. // Returns iterator that can be used to delete the account.
  81. AccountMappings::iterator FindMappingByMessageId(
  82. const std::string& message_id);
  83. // Sets the clock for testing.
  84. void SetClockForTesting(base::Clock* clock);
  85. // GCMDriver owns GCMAccountMapper.
  86. raw_ptr<GCMDriver> gcm_driver_;
  87. // Callback to GCMDriver to dispatch messages sent to Gaia ID.
  88. DispatchMessageCallback dispatch_message_callback_;
  89. // Clock for timestamping status changes.
  90. raw_ptr<base::Clock> clock_;
  91. // Currnetly tracked account mappings.
  92. AccountMappings accounts_;
  93. std::vector<GCMClient::AccountTokenInfo> pending_account_tokens_;
  94. // GCM Registration ID of the account mapper.
  95. std::string registration_id_;
  96. bool initialized_;
  97. base::WeakPtrFactory<GCMAccountMapper> weak_ptr_factory_{this};
  98. };
  99. } // namespace gcm
  100. #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_