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/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
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"
26 extern const char kGCMAccountMapperAppId
[];
28 // Class for mapping signed-in GAIA accounts to the GCM Device ID.
29 class GCMAccountMapper
: public GCMAppHandler
{
31 // List of account mappings.
32 typedef std::vector
<AccountMapping
> AccountMappings
;
33 typedef base::Callback
<void(const std::string
& app_id
,
34 const IncomingMessage
& message
)>
35 DispatchMessageCallback
;
37 explicit GCMAccountMapper(GCMDriver
* gcm_driver
);
38 ~GCMAccountMapper() override
;
40 void Initialize(const AccountMappings
& account_mappings
,
41 const DispatchMessageCallback
& callback
);
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(
46 const std::vector
<GCMClient::AccountTokenInfo
>& account_tokens
);
48 // Implementation of GCMAppHandler:
49 void ShutdownHandler() override
;
50 void OnMessage(const std::string
& app_id
,
51 const IncomingMessage
& message
) override
;
52 void OnMessagesDeleted(const std::string
& app_id
) override
;
54 const std::string
& app_id
,
55 const GCMClient::SendErrorDetails
& send_error_details
) override
;
56 void OnSendAcknowledged(const std::string
& app_id
,
57 const std::string
& message_id
) override
;
58 bool CanHandle(const std::string
& app_id
) const override
;
61 friend class GCMAccountMapperTest
;
63 typedef std::map
<std::string
, OutgoingMessage
> OutgoingMessages
;
65 // Checks whether account mapper is ready to process new account tokens.
68 // Informs GCM of an added or refreshed account mapping.
69 void SendAddMappingMessage(AccountMapping
& account_mapping
);
71 // Informs GCM of a removed account mapping.
72 void SendRemoveMappingMessage(AccountMapping
& account_mapping
);
74 void CreateAndSendMessage(const AccountMapping
& account_mapping
);
76 // Callback for sending a message.
77 void OnSendFinished(const std::string
& account_id
,
78 const std::string
& message_id
,
79 GCMClient::Result result
);
81 // Gets a registration for account mapper from GCM.
82 void GetRegistration();
84 // Callback for registering with GCM.
85 void OnRegisterFinished(const std::string
& registration_id
,
86 GCMClient::Result result
);
88 // Checks whether the update can be triggered now. If the current time is
89 // within reasonable time (6 hours) of when the update is due, we want to
90 // trigger the update immediately to take advantage of a fresh OAuth2 token.
91 bool CanTriggerUpdate(const base::Time
& last_update_time
) const;
93 // Checks whether last status change is older than a TTL of a message.
94 bool IsLastStatusChangeOlderThanTTL(
95 const AccountMapping
& account_mapping
) const;
97 // Finds an account mapping in |accounts_| by |account_id|.
98 AccountMapping
* FindMappingByAccountId(const std::string
& account_id
);
99 // Finds an account mapping in |accounts_| by |message_id|.
100 // Returns iterator that can be used to delete the account.
101 AccountMappings::iterator
FindMappingByMessageId(
102 const std::string
& message_id
);
104 // Sets the clock for testing.
105 void SetClockForTesting(scoped_ptr
<base::Clock
> clock
);
107 // GCMDriver owns GCMAccountMapper.
108 GCMDriver
* gcm_driver_
;
110 // Callback to GCMDriver to dispatch messages sent to Gaia ID.
111 DispatchMessageCallback dispatch_message_callback_
;
113 // Clock for timestamping status changes.
114 scoped_ptr
<base::Clock
> clock_
;
116 // Currnetly tracked account mappings.
117 AccountMappings accounts_
;
119 std::vector
<GCMClient::AccountTokenInfo
> pending_account_tokens_
;
121 // GCM Registration ID of the account mapper.
122 std::string registration_id_
;
126 base::WeakPtrFactory
<GCMAccountMapper
> weak_ptr_factory_
;
128 DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper
);
133 #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_