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_); | |
Nicolas Zea
2014/10/08 00:36:35
can this be initialized more than once now?
fgorski
2014/10/08 18:04:27
Initially it was a problem, as we discovered and f
| |
52 initialized_ = true; | 52 initialized_ = true; |
53 accounts_ = account_mappings; | 53 accounts_ = account_mappings; |
54 gcm_driver_->AddAppHandler(kGCMAccountMapperAppId, this); | |
55 GetRegistration(); | 54 GetRegistration(); |
56 } | 55 } |
57 | 56 |
58 void GCMAccountMapper::SetAccountTokens( | 57 void GCMAccountMapper::SetAccountTokens( |
59 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { | 58 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
59 DVLOG(1) << "GCMAccountMapper::SetAccountTokens called with " | |
60 << account_tokens.size() << " accounts."; | |
61 | |
60 // If account mapper is not ready to handle tasks yet, save the latest | 62 // If account mapper is not ready to handle tasks yet, save the latest |
61 // account tokens and return. | 63 // account tokens and return. |
62 if (!IsReady()) { | 64 if (!IsReady()) { |
63 pending_account_tokens_ = account_tokens; | 65 pending_account_tokens_ = account_tokens; |
64 // If mapper is initialized, but still does not have registration ID, | 66 // If mapper is initialized, but still does not have registration ID, |
65 // maybe the registration gave up. Retrying in case. | 67 // maybe the registration gave up. Retrying in case. |
66 if (initialized_) | 68 if (initialized_ && gcm_driver_->IsStarted()) |
67 GetRegistration(); | 69 GetRegistration(); |
68 return; | 70 return; |
69 } | 71 } |
70 | 72 |
71 // Start from removing the old tokens, from all of the known accounts. | 73 // Start from removing the old tokens, from all of the known accounts. |
72 for (AccountMappings::iterator iter = accounts_.begin(); | 74 for (AccountMappings::iterator iter = accounts_.begin(); |
73 iter != accounts_.end(); | 75 iter != accounts_.end(); |
74 ++iter) { | 76 ++iter) { |
75 iter->access_token.clear(); | 77 iter->access_token.clear(); |
76 } | 78 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 (mappings_iter->status == AccountMapping::MAPPED && | 129 (mappings_iter->status == AccountMapping::MAPPED && |
128 CanTriggerUpdate(mappings_iter->status_change_timestamp))) { | 130 CanTriggerUpdate(mappings_iter->status_change_timestamp))) { |
129 mappings_iter->last_message_id.clear(); | 131 mappings_iter->last_message_id.clear(); |
130 SendAddMappingMessage(*mappings_iter); | 132 SendAddMappingMessage(*mappings_iter); |
131 } | 133 } |
132 } | 134 } |
133 } | 135 } |
134 } | 136 } |
135 | 137 |
136 void GCMAccountMapper::ShutdownHandler() { | 138 void GCMAccountMapper::ShutdownHandler() { |
137 gcm_driver_->RemoveAppHandler(kGCMAccountMapperAppId); | 139 initialized_ = false; |
140 accounts_.clear(); | |
141 registration_id_.clear(); | |
138 } | 142 } |
139 | 143 |
140 void GCMAccountMapper::OnMessage(const std::string& app_id, | 144 void GCMAccountMapper::OnMessage(const std::string& app_id, |
141 const GCMClient::IncomingMessage& message) { | 145 const GCMClient::IncomingMessage& message) { |
142 // Account message does not expect messages right now. | 146 // Account message does not expect messages right now. |
143 } | 147 } |
144 | 148 |
145 void GCMAccountMapper::OnMessagesDeleted(const std::string& app_id) { | 149 void GCMAccountMapper::OnMessagesDeleted(const std::string& app_id) { |
146 // Account message does not expect messages right now. | 150 // Account message does not expect messages right now. |
147 } | 151 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 | 216 |
213 gcm_driver_->UpdateAccountMapping(*account_mapping_it); | 217 gcm_driver_->UpdateAccountMapping(*account_mapping_it); |
214 } | 218 } |
215 } | 219 } |
216 | 220 |
217 bool GCMAccountMapper::CanHandle(const std::string& app_id) const { | 221 bool GCMAccountMapper::CanHandle(const std::string& app_id) const { |
218 return app_id.compare(kGCMAccountMapperAppId) == 0; | 222 return app_id.compare(kGCMAccountMapperAppId) == 0; |
219 } | 223 } |
220 | 224 |
221 bool GCMAccountMapper::IsReady() { | 225 bool GCMAccountMapper::IsReady() { |
222 return initialized_ && !registration_id_.empty(); | 226 return initialized_ && gcm_driver_->IsStarted() && !registration_id_.empty(); |
223 } | 227 } |
224 | 228 |
225 void GCMAccountMapper::SendAddMappingMessage(AccountMapping& account_mapping) { | 229 void GCMAccountMapper::SendAddMappingMessage(AccountMapping& account_mapping) { |
226 CreateAndSendMessage(account_mapping); | 230 CreateAndSendMessage(account_mapping); |
227 } | 231 } |
228 | 232 |
229 void GCMAccountMapper::SendRemoveMappingMessage( | 233 void GCMAccountMapper::SendRemoveMappingMessage( |
230 AccountMapping& account_mapping) { | 234 AccountMapping& account_mapping) { |
231 // We want to persist an account that is being removed as quickly as possible | 235 // We want to persist an account that is being removed as quickly as possible |
232 // as well as clean up the last message information. | 236 // as well as clean up the last message information. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 } | 352 } |
349 | 353 |
350 return accounts_.end(); | 354 return accounts_.end(); |
351 } | 355 } |
352 | 356 |
353 void GCMAccountMapper::SetClockForTesting(scoped_ptr<base::Clock> clock) { | 357 void GCMAccountMapper::SetClockForTesting(scoped_ptr<base::Clock> clock) { |
354 clock_ = clock.Pass(); | 358 clock_ = clock.Pass(); |
355 } | 359 } |
356 | 360 |
357 } // namespace gcm | 361 } // namespace gcm |
OLD | NEW |