fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 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 | |
| 5 | #ifndef COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ |
| 6 | #define COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ |
| 7 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 8 | #include <memory> |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
fgorski | 47869f0 | 2015-02-27 23:16:03 | [diff] [blame] | 12 | #include "base/callback.h" |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 13 | #include "base/macros.h" |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
| 15 | #include "components/gcm_driver/gcm_app_handler.h" |
| 16 | #include "components/gcm_driver/gcm_client.h" |
| 17 | #include "google_apis/gcm/engine/account_mapping.h" |
| 18 | |
| 19 | namespace base { |
| 20 | class Clock; |
| 21 | } |
| 22 | |
| 23 | namespace gcm { |
| 24 | |
| 25 | class GCMDriver; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 26 | extern const char kGCMAccountMapperAppId[]; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 27 | |
| 28 | // Class for mapping signed-in GAIA accounts to the GCM Device ID. |
| 29 | class GCMAccountMapper : public GCMAppHandler { |
| 30 | public: |
| 31 | // List of account mappings. |
Reilly Grant | aa7bc2f | 2020-01-31 11:49:29 | [diff] [blame] | 32 | using AccountMappings = std::vector<AccountMapping>; |
| 33 | using DispatchMessageCallback = |
| 34 | base::RepeatingCallback<void(const std::string& app_id, |
| 35 | const IncomingMessage& message)>; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 36 | |
| 37 | explicit GCMAccountMapper(GCMDriver* gcm_driver); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 38 | ~GCMAccountMapper() override; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 39 | |
fgorski | 47869f0 | 2015-02-27 23:16:03 | [diff] [blame] | 40 | void Initialize(const AccountMappings& account_mappings, |
| 41 | const DispatchMessageCallback& callback); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 42 | |
| 43 | // Called by AccountTracker, when a new list of account tokens is available. |
| 44 | // This will cause a refresh of account mappings and sending updates to GCM. |
| 45 | void SetAccountTokens( |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 46 | const std::vector<GCMClient::AccountTokenInfo>& account_tokens); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 47 | |
| 48 | // Implementation of GCMAppHandler: |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 49 | void ShutdownHandler() override; |
johnme | 93ec793 | 2016-11-17 14:26:58 | [diff] [blame] | 50 | void OnStoreReset() override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 51 | void OnMessage(const std::string& app_id, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 52 | const IncomingMessage& message) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 53 | void OnMessagesDeleted(const std::string& app_id) override; |
| 54 | void OnSendError( |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 55 | const std::string& app_id, |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 56 | const GCMClient::SendErrorDetails& send_error_details) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 57 | void OnSendAcknowledged(const std::string& app_id, |
| 58 | const std::string& message_id) override; |
| 59 | bool CanHandle(const std::string& app_id) const override; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | friend class GCMAccountMapperTest; |
| 63 | |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 64 | typedef std::map<std::string, OutgoingMessage> OutgoingMessages; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 65 | |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 66 | // Checks whether account mapper is ready to process new account tokens. |
| 67 | bool IsReady(); |
| 68 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 69 | // Informs GCM of an added or refreshed account mapping. |
| 70 | void SendAddMappingMessage(AccountMapping& account_mapping); |
| 71 | |
| 72 | // Informs GCM of a removed account mapping. |
| 73 | void SendRemoveMappingMessage(AccountMapping& account_mapping); |
| 74 | |
| 75 | void CreateAndSendMessage(const AccountMapping& account_mapping); |
| 76 | |
| 77 | // Callback for sending a message. |
Tanmoy Mollik | c15f5297 | 2019-08-19 17:36:03 | [diff] [blame] | 78 | void OnSendFinished(const CoreAccountId& account_id, |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 79 | const std::string& message_id, |
| 80 | GCMClient::Result result); |
| 81 | |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 82 | // Gets a registration for account mapper from GCM. |
| 83 | void GetRegistration(); |
| 84 | |
| 85 | // Callback for registering with GCM. |
| 86 | void OnRegisterFinished(const std::string& registration_id, |
| 87 | GCMClient::Result result); |
| 88 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 89 | // Checks whether the update can be triggered now. If the current time is |
| 90 | // within reasonable time (6 hours) of when the update is due, we want to |
| 91 | // trigger the update immediately to take advantage of a fresh OAuth2 token. |
| 92 | bool CanTriggerUpdate(const base::Time& last_update_time) const; |
| 93 | |
| 94 | // Checks whether last status change is older than a TTL of a message. |
| 95 | bool IsLastStatusChangeOlderThanTTL( |
| 96 | const AccountMapping& account_mapping) const; |
| 97 | |
| 98 | // Finds an account mapping in |accounts_| by |account_id|. |
Tanmoy Mollik | c15f5297 | 2019-08-19 17:36:03 | [diff] [blame] | 99 | AccountMapping* FindMappingByAccountId(const CoreAccountId& account_id); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 100 | // Finds an account mapping in |accounts_| by |message_id|. |
| 101 | // Returns iterator that can be used to delete the account. |
| 102 | AccountMappings::iterator FindMappingByMessageId( |
| 103 | const std::string& message_id); |
| 104 | |
| 105 | // Sets the clock for testing. |
tzik | 48479d9 | 2018-03-20 15:20:42 | [diff] [blame] | 106 | void SetClockForTesting(base::Clock* clock); |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 107 | |
| 108 | // GCMDriver owns GCMAccountMapper. |
| 109 | GCMDriver* gcm_driver_; |
| 110 | |
fgorski | 47869f0 | 2015-02-27 23:16:03 | [diff] [blame] | 111 | // Callback to GCMDriver to dispatch messages sent to Gaia ID. |
| 112 | DispatchMessageCallback dispatch_message_callback_; |
| 113 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 114 | // Clock for timestamping status changes. |
tzik | 48479d9 | 2018-03-20 15:20:42 | [diff] [blame] | 115 | base::Clock* clock_; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 116 | |
| 117 | // Currnetly tracked account mappings. |
| 118 | AccountMappings accounts_; |
| 119 | |
fgorski | ba729da | 2014-09-20 20:55:55 | [diff] [blame] | 120 | std::vector<GCMClient::AccountTokenInfo> pending_account_tokens_; |
| 121 | |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 122 | // GCM Registration ID of the account mapper. |
| 123 | std::string registration_id_; |
| 124 | |
| 125 | bool initialized_; |
| 126 | |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 127 | base::WeakPtrFactory<GCMAccountMapper> weak_ptr_factory_{this}; |
fgorski | c104731 | 2014-09-04 16:48:54 | [diff] [blame] | 128 | |
| 129 | DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper); |
| 130 | }; |
| 131 | |
| 132 | } // namespace gcm |
| 133 | |
| 134 | #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ |