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.
5 #ifndef COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_
6 #define COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/gcm_driver/gcm_app_handler.h"
15 #include "components/gcm_driver/gcm_client.h"
16 #include "google_apis/gcm/engine/account_mapping.h"
25 extern const char kGCMAccountMapperAppId
[];
27 // Class for mapping signed-in GAIA accounts to the GCM Device ID.
28 class GCMAccountMapper
: public GCMAppHandler
{
30 // List of account mappings.
31 typedef std::vector
<AccountMapping
> AccountMappings
;
33 explicit GCMAccountMapper(GCMDriver
* gcm_driver
);
34 ~GCMAccountMapper() override
;
36 void Initialize(const AccountMappings
& account_mappings
);
38 // Called by AccountTracker, when a new list of account tokens is available.
39 // This will cause a refresh of account mappings and sending updates to GCM.
40 void SetAccountTokens(
41 const std::vector
<GCMClient::AccountTokenInfo
>& account_tokens
);
43 // Implementation of GCMAppHandler:
44 void ShutdownHandler() override
;
45 void OnMessage(const std::string
& app_id
,
46 const GCMClient::IncomingMessage
& message
) override
;
47 void OnMessagesDeleted(const std::string
& app_id
) override
;
49 const std::string
& app_id
,
50 const GCMClient::SendErrorDetails
& send_error_details
) override
;
51 void OnSendAcknowledged(const std::string
& app_id
,
52 const std::string
& message_id
) override
;
53 bool CanHandle(const std::string
& app_id
) const override
;
56 friend class GCMAccountMapperTest
;
58 typedef std::map
<std::string
, GCMClient::OutgoingMessage
> OutgoingMessages
;
60 // Checks whether account mapper is ready to process new account tokens.
63 // Informs GCM of an added or refreshed account mapping.
64 void SendAddMappingMessage(AccountMapping
& account_mapping
);
66 // Informs GCM of a removed account mapping.
67 void SendRemoveMappingMessage(AccountMapping
& account_mapping
);
69 void CreateAndSendMessage(const AccountMapping
& account_mapping
);
71 // Callback for sending a message.
72 void OnSendFinished(const std::string
& account_id
,
73 const std::string
& message_id
,
74 GCMClient::Result result
);
76 // Gets a registration for account mapper from GCM.
77 void GetRegistration();
79 // Callback for registering with GCM.
80 void OnRegisterFinished(const std::string
& registration_id
,
81 GCMClient::Result result
);
83 // Checks whether the update can be triggered now. If the current time is
84 // within reasonable time (6 hours) of when the update is due, we want to
85 // trigger the update immediately to take advantage of a fresh OAuth2 token.
86 bool CanTriggerUpdate(const base::Time
& last_update_time
) const;
88 // Checks whether last status change is older than a TTL of a message.
89 bool IsLastStatusChangeOlderThanTTL(
90 const AccountMapping
& account_mapping
) const;
92 // Finds an account mapping in |accounts_| by |account_id|.
93 AccountMapping
* FindMappingByAccountId(const std::string
& account_id
);
94 // Finds an account mapping in |accounts_| by |message_id|.
95 // Returns iterator that can be used to delete the account.
96 AccountMappings::iterator
FindMappingByMessageId(
97 const std::string
& message_id
);
99 // Sets the clock for testing.
100 void SetClockForTesting(scoped_ptr
<base::Clock
> clock
);
102 // GCMDriver owns GCMAccountMapper.
103 GCMDriver
* gcm_driver_
;
105 // Clock for timestamping status changes.
106 scoped_ptr
<base::Clock
> clock_
;
108 // Currnetly tracked account mappings.
109 AccountMappings accounts_
;
111 std::vector
<GCMClient::AccountTokenInfo
> pending_account_tokens_
;
113 // GCM Registration ID of the account mapper.
114 std::string registration_id_
;
118 base::WeakPtrFactory
<GCMAccountMapper
> weak_ptr_factory_
;
120 DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper
);
125 #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_