[GCM] Adding registration ID request and tests to GCM Account Mapper
[chromium-blink-merge.git] / components / gcm_driver / gcm_account_mapper.h
blob524adbdd3554546ddaab6f09fcb2e9cfd9c0b148
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_
8 #include <string>
9 #include <vector>
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"
18 namespace base {
19 class Clock;
22 namespace gcm {
24 class GCMDriver;
26 // Class for mapping signed-in GAIA accounts to the GCM Device ID.
27 class GCMAccountMapper : public GCMAppHandler {
28 public:
29 // List of account mappings.
30 typedef std::vector<AccountMapping> AccountMappings;
32 explicit GCMAccountMapper(GCMDriver* gcm_driver);
33 virtual ~GCMAccountMapper();
35 void Initialize(const AccountMappings& account_mappings);
37 // Called by AccountTracker, when a new list of account tokens is available.
38 // This will cause a refresh of account mappings and sending updates to GCM.
39 void SetAccountTokens(
40 const std::vector<GCMClient::AccountTokenInfo> account_tokens);
42 // Implementation of GCMAppHandler:
43 virtual void ShutdownHandler() OVERRIDE;
44 virtual void OnMessage(const std::string& app_id,
45 const GCMClient::IncomingMessage& message) OVERRIDE;
46 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
47 virtual void OnSendError(
48 const std::string& app_id,
49 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE;
50 virtual void OnSendAcknowledged(const std::string& app_id,
51 const std::string& message_id) OVERRIDE;
52 virtual bool CanHandle(const std::string& app_id) const OVERRIDE;
54 private:
55 friend class GCMAccountMapperTest;
57 typedef std::map<std::string, GCMClient::OutgoingMessage> OutgoingMessages;
59 // Checks whether account mapper is ready to process new account tokens.
60 bool IsReady();
62 // Informs GCM of an added or refreshed account mapping.
63 void SendAddMappingMessage(AccountMapping& account_mapping);
65 // Informs GCM of a removed account mapping.
66 void SendRemoveMappingMessage(AccountMapping& account_mapping);
68 void CreateAndSendMessage(const AccountMapping& account_mapping);
70 // Callback for sending a message.
71 void OnSendFinished(const std::string& account_id,
72 const std::string& message_id,
73 GCMClient::Result result);
75 // Gets a registration for account mapper from GCM.
76 void GetRegistration();
78 // Callback for registering with GCM.
79 void OnRegisterFinished(const std::string& registration_id,
80 GCMClient::Result result);
82 // Checks whether the update can be triggered now. If the current time is
83 // within reasonable time (6 hours) of when the update is due, we want to
84 // trigger the update immediately to take advantage of a fresh OAuth2 token.
85 bool CanTriggerUpdate(const base::Time& last_update_time) const;
87 // Checks whether last status change is older than a TTL of a message.
88 bool IsLastStatusChangeOlderThanTTL(
89 const AccountMapping& account_mapping) const;
91 // Finds an account mapping in |accounts_| by |account_id|.
92 AccountMapping* FindMappingByAccountId(const std::string& account_id);
93 // Finds an account mapping in |accounts_| by |message_id|.
94 // Returns iterator that can be used to delete the account.
95 AccountMappings::iterator FindMappingByMessageId(
96 const std::string& message_id);
98 // Sets the clock for testing.
99 void SetClockForTesting(scoped_ptr<base::Clock> clock);
101 // GCMDriver owns GCMAccountMapper.
102 GCMDriver* gcm_driver_;
104 // Clock for timestamping status changes.
105 scoped_ptr<base::Clock> clock_;
107 // Currnetly tracked account mappings.
108 AccountMappings accounts_;
110 std::vector<GCMClient::AccountTokenInfo> pending_account_tokens_;
112 // GCM Registration ID of the account mapper.
113 std::string registration_id_;
115 bool initialized_;
117 base::WeakPtrFactory<GCMAccountMapper> weak_ptr_factory_;
119 DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper);
122 } // namespace gcm
124 #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_