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 #include "components/gcm_driver/gcm_account_mapper.h" | 5 #include "components/gcm_driver/gcm_account_mapper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/time/clock.h" | 9 #include "base/time/clock.h" |
10 #include "base/time/default_clock.h" | 10 #include "base/time/default_clock.h" |
11 #include "components/gcm_driver/gcm_driver_desktop.h" | 11 #include "components/gcm_driver/gcm_driver_desktop.h" |
12 #include "google_apis/gcm/engine/gcm_store.h" | 12 #include "google_apis/gcm/engine/gcm_store.h" |
13 | 13 |
14 namespace gcm { | 14 namespace gcm { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const char kGCMAccountMapperSenderId[] = "745476177629"; | 18 const char kGCMAccountMapperSenderId[] = "745476177629"; |
19 const char kGCMAccountMapperAppId[] = "com.google.android.gms"; | |
20 const int kGCMAddMappingMessageTTL = 30 * 60; // 0.5 hours in seconds. | 19 const int kGCMAddMappingMessageTTL = 30 * 60; // 0.5 hours in seconds. |
21 const int kGCMRemoveMappingMessageTTL = 24 * 60 * 60; // 1 day in seconds. | 20 const int kGCMRemoveMappingMessageTTL = 24 * 60 * 60; // 1 day in seconds. |
22 const int kGCMUpdateIntervalHours = 24; | 21 const int kGCMUpdateIntervalHours = 24; |
23 // Because adding an account mapping dependents on a fresh OAuth2 token, we | 22 // Because adding an account mapping dependents on a fresh OAuth2 token, we |
24 // allow the update to happen earlier than update due time, if it is within | 23 // allow the update to happen earlier than update due time, if it is within |
25 // the early start time to take advantage of that token. | 24 // the early start time to take advantage of that token. |
26 const int kGCMUpdateEarlyStartHours = 6; | 25 const int kGCMUpdateEarlyStartHours = 6; |
27 const char kRegistrationIdMessgaeKey[] = "id"; | 26 const char kRegistrationIdMessgaeKey[] = "id"; |
28 const char kTokenMessageKey[] = "t"; | 27 const char kTokenMessageKey[] = "t"; |
29 const char kAccountMessageKey[] = "a"; | 28 const char kAccountMessageKey[] = "a"; |
30 const char kRemoveAccountKey[] = "r"; | 29 const char kRemoveAccountKey[] = "r"; |
31 const char kRemoveAccountValue[] = "1"; | 30 const char kRemoveAccountValue[] = "1"; |
32 | 31 |
33 std::string GenerateMessageID() { | 32 std::string GenerateMessageID() { |
34 return base::GenerateGUID(); | 33 return base::GenerateGUID(); |
35 } | 34 } |
36 | 35 |
37 } // namespace | 36 } // namespace |
38 | 37 |
| 38 const char kGCMAccountMapperAppId[] = "com.google.android.gms"; |
| 39 |
39 GCMAccountMapper::GCMAccountMapper(GCMDriver* gcm_driver) | 40 GCMAccountMapper::GCMAccountMapper(GCMDriver* gcm_driver) |
40 : gcm_driver_(gcm_driver), | 41 : gcm_driver_(gcm_driver), |
41 clock_(new base::DefaultClock), | 42 clock_(new base::DefaultClock), |
42 initialized_(false), | 43 initialized_(false), |
43 weak_ptr_factory_(this) { | 44 weak_ptr_factory_(this) { |
44 } | 45 } |
45 | 46 |
46 GCMAccountMapper::~GCMAccountMapper() { | 47 GCMAccountMapper::~GCMAccountMapper() { |
47 } | 48 } |
48 | 49 |
49 void GCMAccountMapper::Initialize( | 50 void GCMAccountMapper::Initialize( |
50 const std::vector<AccountMapping>& account_mappings) { | 51 const std::vector<AccountMapping>& account_mappings) { |
51 DCHECK(!initialized_); | 52 DCHECK(!initialized_); |
52 initialized_ = true; | 53 initialized_ = true; |
53 accounts_ = account_mappings; | 54 accounts_ = account_mappings; |
54 gcm_driver_->AddAppHandler(kGCMAccountMapperAppId, this); | |
55 GetRegistration(); | 55 GetRegistration(); |
56 } | 56 } |
57 | 57 |
58 void GCMAccountMapper::SetAccountTokens( | 58 void GCMAccountMapper::SetAccountTokens( |
59 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { | 59 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
| 60 DVLOG(1) << "GCMAccountMapper::SetAccountTokens called with " |
| 61 << account_tokens.size() << " accounts."; |
| 62 |
60 // If account mapper is not ready to handle tasks yet, save the latest | 63 // If account mapper is not ready to handle tasks yet, save the latest |
61 // account tokens and return. | 64 // account tokens and return. |
62 if (!IsReady()) { | 65 if (!IsReady()) { |
63 pending_account_tokens_ = account_tokens; | 66 pending_account_tokens_ = account_tokens; |
64 // If mapper is initialized, but still does not have registration ID, | 67 // If mapper is initialized, but still does not have registration ID, |
65 // maybe the registration gave up. Retrying in case. | 68 // maybe the registration gave up. Retrying in case. |
66 if (initialized_) | 69 if (initialized_ && gcm_driver_->IsStarted()) |
67 GetRegistration(); | 70 GetRegistration(); |
68 return; | 71 return; |
69 } | 72 } |
70 | 73 |
71 // Start from removing the old tokens, from all of the known accounts. | 74 // Start from removing the old tokens, from all of the known accounts. |
72 for (AccountMappings::iterator iter = accounts_.begin(); | 75 for (AccountMappings::iterator iter = accounts_.begin(); |
73 iter != accounts_.end(); | 76 iter != accounts_.end(); |
74 ++iter) { | 77 ++iter) { |
75 iter->access_token.clear(); | 78 iter->access_token.clear(); |
76 } | 79 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 (mappings_iter->status == AccountMapping::MAPPED && | 130 (mappings_iter->status == AccountMapping::MAPPED && |
128 CanTriggerUpdate(mappings_iter->status_change_timestamp))) { | 131 CanTriggerUpdate(mappings_iter->status_change_timestamp))) { |
129 mappings_iter->last_message_id.clear(); | 132 mappings_iter->last_message_id.clear(); |
130 SendAddMappingMessage(*mappings_iter); | 133 SendAddMappingMessage(*mappings_iter); |
131 } | 134 } |
132 } | 135 } |
133 } | 136 } |
134 } | 137 } |
135 | 138 |
136 void GCMAccountMapper::ShutdownHandler() { | 139 void GCMAccountMapper::ShutdownHandler() { |
137 gcm_driver_->RemoveAppHandler(kGCMAccountMapperAppId); | 140 initialized_ = false; |
| 141 accounts_.clear(); |
| 142 registration_id_.clear(); |
138 } | 143 } |
139 | 144 |
140 void GCMAccountMapper::OnMessage(const std::string& app_id, | 145 void GCMAccountMapper::OnMessage(const std::string& app_id, |
141 const GCMClient::IncomingMessage& message) { | 146 const GCMClient::IncomingMessage& message) { |
142 // Account message does not expect messages right now. | 147 // Account message does not expect messages right now. |
143 } | 148 } |
144 | 149 |
145 void GCMAccountMapper::OnMessagesDeleted(const std::string& app_id) { | 150 void GCMAccountMapper::OnMessagesDeleted(const std::string& app_id) { |
146 // Account message does not expect messages right now. | 151 // Account message does not expect messages right now. |
147 } | 152 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 217 |
213 gcm_driver_->UpdateAccountMapping(*account_mapping_it); | 218 gcm_driver_->UpdateAccountMapping(*account_mapping_it); |
214 } | 219 } |
215 } | 220 } |
216 | 221 |
217 bool GCMAccountMapper::CanHandle(const std::string& app_id) const { | 222 bool GCMAccountMapper::CanHandle(const std::string& app_id) const { |
218 return app_id.compare(kGCMAccountMapperAppId) == 0; | 223 return app_id.compare(kGCMAccountMapperAppId) == 0; |
219 } | 224 } |
220 | 225 |
221 bool GCMAccountMapper::IsReady() { | 226 bool GCMAccountMapper::IsReady() { |
222 return initialized_ && !registration_id_.empty(); | 227 return initialized_ && gcm_driver_->IsStarted() && !registration_id_.empty(); |
223 } | 228 } |
224 | 229 |
225 void GCMAccountMapper::SendAddMappingMessage(AccountMapping& account_mapping) { | 230 void GCMAccountMapper::SendAddMappingMessage(AccountMapping& account_mapping) { |
226 CreateAndSendMessage(account_mapping); | 231 CreateAndSendMessage(account_mapping); |
227 } | 232 } |
228 | 233 |
229 void GCMAccountMapper::SendRemoveMappingMessage( | 234 void GCMAccountMapper::SendRemoveMappingMessage( |
230 AccountMapping& account_mapping) { | 235 AccountMapping& account_mapping) { |
231 // We want to persist an account that is being removed as quickly as possible | 236 // We want to persist an account that is being removed as quickly as possible |
232 // as well as clean up the last message information. | 237 // as well as clean up the last message information. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 } | 353 } |
349 | 354 |
350 return accounts_.end(); | 355 return accounts_.end(); |
351 } | 356 } |
352 | 357 |
353 void GCMAccountMapper::SetClockForTesting(scoped_ptr<base::Clock> clock) { | 358 void GCMAccountMapper::SetClockForTesting(scoped_ptr<base::Clock> clock) { |
354 clock_ = clock.Pass(); | 359 clock_ = clock.Pass(); |
355 } | 360 } |
356 | 361 |
357 } // namespace gcm | 362 } // namespace gcm |
OLD | NEW |