blob: ef8a932f185cae301fa8cff8ed19f2afc778335b [file] [log] [blame]
fgorskic1047312014-09-04 16:48:541// 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
dchenga77e28eb2016-04-21 21:34:378#include <memory>
fgorskic1047312014-09-04 16:48:549#include <string>
10#include <vector>
11
fgorski47869f02015-02-27 23:16:0312#include "base/callback.h"
fgorskic1047312014-09-04 16:48:5413#include "base/macros.h"
fgorskic1047312014-09-04 16:48:5414#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
19namespace base {
20class Clock;
21}
22
23namespace gcm {
24
25class GCMDriver;
fgorski83afd872014-10-16 01:11:5226extern const char kGCMAccountMapperAppId[];
fgorskic1047312014-09-04 16:48:5427
28// Class for mapping signed-in GAIA accounts to the GCM Device ID.
29class GCMAccountMapper : public GCMAppHandler {
30 public:
31 // List of account mappings.
Reilly Grantaa7bc2f2020-01-31 11:49:2932 using AccountMappings = std::vector<AccountMapping>;
33 using DispatchMessageCallback =
34 base::RepeatingCallback<void(const std::string& app_id,
35 const IncomingMessage& message)>;
fgorskic1047312014-09-04 16:48:5436
37 explicit GCMAccountMapper(GCMDriver* gcm_driver);
dcheng00ea022b2014-10-21 11:24:5638 ~GCMAccountMapper() override;
fgorskic1047312014-09-04 16:48:5439
fgorski47869f02015-02-27 23:16:0340 void Initialize(const AccountMappings& account_mappings,
41 const DispatchMessageCallback& callback);
fgorskic1047312014-09-04 16:48:5442
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(
fgorski58b9dfc2014-09-29 16:46:1846 const std::vector<GCMClient::AccountTokenInfo>& account_tokens);
fgorskic1047312014-09-04 16:48:5447
48 // Implementation of GCMAppHandler:
dcheng00ea022b2014-10-21 11:24:5649 void ShutdownHandler() override;
johnme93ec7932016-11-17 14:26:5850 void OnStoreReset() override;
dcheng00ea022b2014-10-21 11:24:5651 void OnMessage(const std::string& app_id,
mvanouwerkerkf8633deb2015-07-13 11:04:0652 const IncomingMessage& message) override;
dcheng00ea022b2014-10-21 11:24:5653 void OnMessagesDeleted(const std::string& app_id) override;
54 void OnSendError(
fgorskic1047312014-09-04 16:48:5455 const std::string& app_id,
mostynbfe59f482014-10-06 15:04:4656 const GCMClient::SendErrorDetails& send_error_details) override;
dcheng00ea022b2014-10-21 11:24:5657 void OnSendAcknowledged(const std::string& app_id,
58 const std::string& message_id) override;
59 bool CanHandle(const std::string& app_id) const override;
fgorskic1047312014-09-04 16:48:5460
61 private:
62 friend class GCMAccountMapperTest;
63
mvanouwerkerkf8633deb2015-07-13 11:04:0664 typedef std::map<std::string, OutgoingMessage> OutgoingMessages;
fgorskic1047312014-09-04 16:48:5465
fgorskiba729da2014-09-20 20:55:5566 // Checks whether account mapper is ready to process new account tokens.
67 bool IsReady();
68
fgorskic1047312014-09-04 16:48:5469 // 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 Mollikc15f52972019-08-19 17:36:0378 void OnSendFinished(const CoreAccountId& account_id,
fgorskic1047312014-09-04 16:48:5479 const std::string& message_id,
80 GCMClient::Result result);
81
fgorskiba729da2014-09-20 20:55:5582 // 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
fgorskic1047312014-09-04 16:48:5489 // 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 Mollikc15f52972019-08-19 17:36:0399 AccountMapping* FindMappingByAccountId(const CoreAccountId& account_id);
fgorskic1047312014-09-04 16:48:54100 // 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.
tzik48479d92018-03-20 15:20:42106 void SetClockForTesting(base::Clock* clock);
fgorskic1047312014-09-04 16:48:54107
108 // GCMDriver owns GCMAccountMapper.
109 GCMDriver* gcm_driver_;
110
fgorski47869f02015-02-27 23:16:03111 // Callback to GCMDriver to dispatch messages sent to Gaia ID.
112 DispatchMessageCallback dispatch_message_callback_;
113
fgorskic1047312014-09-04 16:48:54114 // Clock for timestamping status changes.
tzik48479d92018-03-20 15:20:42115 base::Clock* clock_;
fgorskic1047312014-09-04 16:48:54116
117 // Currnetly tracked account mappings.
118 AccountMappings accounts_;
119
fgorskiba729da2014-09-20 20:55:55120 std::vector<GCMClient::AccountTokenInfo> pending_account_tokens_;
121
fgorskic1047312014-09-04 16:48:54122 // GCM Registration ID of the account mapper.
123 std::string registration_id_;
124
125 bool initialized_;
126
Jeremy Roman5c341f6d2019-07-15 15:56:10127 base::WeakPtrFactory<GCMAccountMapper> weak_ptr_factory_{this};
fgorskic1047312014-09-04 16:48:54128
129 DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper);
130};
131
132} // namespace gcm
133
134#endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_