OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ | 5 #ifndef COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ |
6 #define COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ | 6 #define COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 // Class for mapping signed-in GAIA accounts to the GCM Device ID. | 26 // Class for mapping signed-in GAIA accounts to the GCM Device ID. |
27 class GCMAccountMapper : public GCMAppHandler { | 27 class GCMAccountMapper : public GCMAppHandler { |
28 public: | 28 public: |
29 // List of account mappings. | 29 // List of account mappings. |
30 typedef std::vector<AccountMapping> AccountMappings; | 30 typedef std::vector<AccountMapping> AccountMappings; |
31 | 31 |
32 explicit GCMAccountMapper(GCMDriver* gcm_driver); | 32 explicit GCMAccountMapper(GCMDriver* gcm_driver); |
33 virtual ~GCMAccountMapper(); | 33 virtual ~GCMAccountMapper(); |
34 | 34 |
35 void Initialize(const AccountMappings& account_mappings, | 35 void Initialize(const AccountMappings& account_mappings); |
36 const std::string& registration_id); | |
37 | 36 |
38 // Called by AccountTracker, when a new list of account tokens is available. | 37 // 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. | 38 // This will cause a refresh of account mappings and sending updates to GCM. |
40 void SetAccountTokens( | 39 void SetAccountTokens( |
41 const std::vector<GCMClient::AccountTokenInfo> account_tokens); | 40 const std::vector<GCMClient::AccountTokenInfo> account_tokens); |
42 | 41 |
43 // Implementation of GCMAppHandler: | 42 // Implementation of GCMAppHandler: |
44 virtual void ShutdownHandler() OVERRIDE; | 43 virtual void ShutdownHandler() OVERRIDE; |
45 virtual void OnMessage(const std::string& app_id, | 44 virtual void OnMessage(const std::string& app_id, |
46 const GCMClient::IncomingMessage& message) OVERRIDE; | 45 const GCMClient::IncomingMessage& message) OVERRIDE; |
47 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | 46 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
48 virtual void OnSendError( | 47 virtual void OnSendError( |
49 const std::string& app_id, | 48 const std::string& app_id, |
50 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; | 49 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; |
51 virtual void OnSendAcknowledged(const std::string& app_id, | 50 virtual void OnSendAcknowledged(const std::string& app_id, |
52 const std::string& message_id) OVERRIDE; | 51 const std::string& message_id) OVERRIDE; |
53 virtual bool CanHandle(const std::string& app_id) const OVERRIDE; | 52 virtual bool CanHandle(const std::string& app_id) const OVERRIDE; |
54 | 53 |
55 private: | 54 private: |
56 friend class GCMAccountMapperTest; | 55 friend class GCMAccountMapperTest; |
57 | 56 |
58 typedef std::map<std::string, GCMClient::OutgoingMessage> OutgoingMessages; | 57 typedef std::map<std::string, GCMClient::OutgoingMessage> OutgoingMessages; |
59 | 58 |
| 59 // Checks whether account mapper is ready to process new account tokens. |
| 60 bool IsReady(); |
| 61 |
60 // Informs GCM of an added or refreshed account mapping. | 62 // Informs GCM of an added or refreshed account mapping. |
61 void SendAddMappingMessage(AccountMapping& account_mapping); | 63 void SendAddMappingMessage(AccountMapping& account_mapping); |
62 | 64 |
63 // Informs GCM of a removed account mapping. | 65 // Informs GCM of a removed account mapping. |
64 void SendRemoveMappingMessage(AccountMapping& account_mapping); | 66 void SendRemoveMappingMessage(AccountMapping& account_mapping); |
65 | 67 |
66 void CreateAndSendMessage(const AccountMapping& account_mapping); | 68 void CreateAndSendMessage(const AccountMapping& account_mapping); |
67 | 69 |
68 // Callback for sending a message. | 70 // Callback for sending a message. |
69 void OnSendFinished(const std::string& account_id, | 71 void OnSendFinished(const std::string& account_id, |
70 const std::string& message_id, | 72 const std::string& message_id, |
71 GCMClient::Result result); | 73 GCMClient::Result result); |
72 | 74 |
| 75 // Gets a registration for account mapper from GCM. |
| 76 void GetRegistration(); |
| 77 |
| 78 // Callback for registering with GCM. |
| 79 void OnRegisterFinished(const std::string& registration_id, |
| 80 GCMClient::Result result); |
| 81 |
73 // Checks whether the update can be triggered now. If the current time is | 82 // Checks whether the update can be triggered now. If the current time is |
74 // within reasonable time (6 hours) of when the update is due, we want to | 83 // within reasonable time (6 hours) of when the update is due, we want to |
75 // trigger the update immediately to take advantage of a fresh OAuth2 token. | 84 // trigger the update immediately to take advantage of a fresh OAuth2 token. |
76 bool CanTriggerUpdate(const base::Time& last_update_time) const; | 85 bool CanTriggerUpdate(const base::Time& last_update_time) const; |
77 | 86 |
78 // Checks whether last status change is older than a TTL of a message. | 87 // Checks whether last status change is older than a TTL of a message. |
79 bool IsLastStatusChangeOlderThanTTL( | 88 bool IsLastStatusChangeOlderThanTTL( |
80 const AccountMapping& account_mapping) const; | 89 const AccountMapping& account_mapping) const; |
81 | 90 |
82 // Finds an account mapping in |accounts_| by |account_id|. | 91 // Finds an account mapping in |accounts_| by |account_id|. |
83 AccountMapping* FindMappingByAccountId(const std::string& account_id); | 92 AccountMapping* FindMappingByAccountId(const std::string& account_id); |
84 // Finds an account mapping in |accounts_| by |message_id|. | 93 // Finds an account mapping in |accounts_| by |message_id|. |
85 // Returns iterator that can be used to delete the account. | 94 // Returns iterator that can be used to delete the account. |
86 AccountMappings::iterator FindMappingByMessageId( | 95 AccountMappings::iterator FindMappingByMessageId( |
87 const std::string& message_id); | 96 const std::string& message_id); |
88 | 97 |
89 // Sets the clock for testing. | 98 // Sets the clock for testing. |
90 void SetClockForTesting(scoped_ptr<base::Clock> clock); | 99 void SetClockForTesting(scoped_ptr<base::Clock> clock); |
91 | 100 |
92 // GCMDriver owns GCMAccountMapper. | 101 // GCMDriver owns GCMAccountMapper. |
93 GCMDriver* gcm_driver_; | 102 GCMDriver* gcm_driver_; |
94 | 103 |
95 // Clock for timestamping status changes. | 104 // Clock for timestamping status changes. |
96 scoped_ptr<base::Clock> clock_; | 105 scoped_ptr<base::Clock> clock_; |
97 | 106 |
98 // Currnetly tracked account mappings. | 107 // Currnetly tracked account mappings. |
99 AccountMappings accounts_; | 108 AccountMappings accounts_; |
100 | 109 |
| 110 std::vector<GCMClient::AccountTokenInfo> pending_account_tokens_; |
| 111 |
101 // GCM Registration ID of the account mapper. | 112 // GCM Registration ID of the account mapper. |
102 std::string registration_id_; | 113 std::string registration_id_; |
103 | 114 |
104 bool initialized_; | 115 bool initialized_; |
105 | 116 |
106 base::WeakPtrFactory<GCMAccountMapper> weak_ptr_factory_; | 117 base::WeakPtrFactory<GCMAccountMapper> weak_ptr_factory_; |
107 | 118 |
108 DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper); | 119 DISALLOW_COPY_AND_ASSIGN(GCMAccountMapper); |
109 }; | 120 }; |
110 | 121 |
111 } // namespace gcm | 122 } // namespace gcm |
112 | 123 |
113 #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ | 124 #endif // COMPONENTS_GCM_DRIVER_GCM_ACCOUNT_MAPPER_H_ |
OLD | NEW |