| 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" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 const int kGCMUpdateIntervalHours = 24; | 22 const int kGCMUpdateIntervalHours = 24; |
| 23 // Because adding an account mapping dependents on a fresh OAuth2 token, we | 23 // 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 | 24 // 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. | 25 // the early start time to take advantage of that token. |
| 26 const int kGCMUpdateEarlyStartHours = 6; | 26 const int kGCMUpdateEarlyStartHours = 6; |
| 27 const char kRegistrationIdMessgaeKey[] = "id"; | 27 const char kRegistrationIdMessgaeKey[] = "id"; |
| 28 const char kTokenMessageKey[] = "t"; | 28 const char kTokenMessageKey[] = "t"; |
| 29 const char kAccountMessageKey[] = "a"; | 29 const char kAccountMessageKey[] = "a"; |
| 30 const char kRemoveAccountKey[] = "r"; | 30 const char kRemoveAccountKey[] = "r"; |
| 31 const char kRemoveAccountValue[] = "1"; | 31 const char kRemoveAccountValue[] = "1"; |
| 32 // Use to handle send to Gaia ID scenario: |
| 33 const char kGCMSendToGaiaIdAppIdKey[] = "gcmb"; |
| 34 |
| 32 | 35 |
| 33 std::string GenerateMessageID() { | 36 std::string GenerateMessageID() { |
| 34 return base::GenerateGUID(); | 37 return base::GenerateGUID(); |
| 35 } | 38 } |
| 36 | 39 |
| 37 } // namespace | 40 } // namespace |
| 38 | 41 |
| 39 const char kGCMAccountMapperAppId[] = "com.google.android.gms"; | 42 const char kGCMAccountMapperAppId[] = "com.google.android.gms"; |
| 40 | 43 |
| 41 GCMAccountMapper::GCMAccountMapper(GCMDriver* gcm_driver) | 44 GCMAccountMapper::GCMAccountMapper(GCMDriver* gcm_driver) |
| 42 : gcm_driver_(gcm_driver), | 45 : gcm_driver_(gcm_driver), |
| 43 clock_(new base::DefaultClock), | 46 clock_(new base::DefaultClock), |
| 44 initialized_(false), | 47 initialized_(false), |
| 45 weak_ptr_factory_(this) { | 48 weak_ptr_factory_(this) { |
| 46 } | 49 } |
| 47 | 50 |
| 48 GCMAccountMapper::~GCMAccountMapper() { | 51 GCMAccountMapper::~GCMAccountMapper() { |
| 49 } | 52 } |
| 50 | 53 |
| 51 void GCMAccountMapper::Initialize( | 54 void GCMAccountMapper::Initialize(const AccountMappings& account_mappings, |
| 52 const std::vector<AccountMapping>& account_mappings) { | 55 const DispatchMessageCallback& callback) { |
| 53 DCHECK(!initialized_); | 56 DCHECK(!initialized_); |
| 54 initialized_ = true; | 57 initialized_ = true; |
| 55 accounts_ = account_mappings; | 58 accounts_ = account_mappings; |
| 59 dispatch_message_callback_ = callback; |
| 56 GetRegistration(); | 60 GetRegistration(); |
| 57 } | 61 } |
| 58 | 62 |
| 59 void GCMAccountMapper::SetAccountTokens( | 63 void GCMAccountMapper::SetAccountTokens( |
| 60 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { | 64 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
| 61 DVLOG(1) << "GCMAccountMapper::SetAccountTokens called with " | 65 DVLOG(1) << "GCMAccountMapper::SetAccountTokens called with " |
| 62 << account_tokens.size() << " accounts."; | 66 << account_tokens.size() << " accounts."; |
| 63 | 67 |
| 64 // If account mapper is not ready to handle tasks yet, save the latest | 68 // If account mapper is not ready to handle tasks yet, save the latest |
| 65 // account tokens and return. | 69 // account tokens and return. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 SendAddMappingMessage(*mappings_iter); | 138 SendAddMappingMessage(*mappings_iter); |
| 135 } | 139 } |
| 136 } | 140 } |
| 137 } | 141 } |
| 138 } | 142 } |
| 139 | 143 |
| 140 void GCMAccountMapper::ShutdownHandler() { | 144 void GCMAccountMapper::ShutdownHandler() { |
| 141 initialized_ = false; | 145 initialized_ = false; |
| 142 accounts_.clear(); | 146 accounts_.clear(); |
| 143 registration_id_.clear(); | 147 registration_id_.clear(); |
| 148 dispatch_message_callback_.Reset(); |
| 144 } | 149 } |
| 145 | 150 |
| 146 void GCMAccountMapper::OnMessage(const std::string& app_id, | 151 void GCMAccountMapper::OnMessage(const std::string& app_id, |
| 147 const GCMClient::IncomingMessage& message) { | 152 const GCMClient::IncomingMessage& message) { |
| 148 // Account message does not expect messages right now. | 153 DCHECK_EQ(app_id, kGCMAccountMapperAppId); |
| 154 // TODO(fgorski): Report Send to Gaia ID failures using UMA. |
| 155 |
| 156 if (dispatch_message_callback_.is_null()) { |
| 157 DVLOG(1) << "dispatch_message_callback_ missing in GCMAccountMapper"; |
| 158 return; |
| 159 } |
| 160 |
| 161 GCMClient::MessageData::const_iterator it = |
| 162 message.data.find(kGCMSendToGaiaIdAppIdKey); |
| 163 if (it == message.data.end()) { |
| 164 DVLOG(1) << "Send to Gaia ID failure: Embedded app ID missing."; |
| 165 return; |
| 166 } |
| 167 |
| 168 std::string embedded_app_id = it->second; |
| 169 if (embedded_app_id.empty()) { |
| 170 DVLOG(1) << "Send to Gaia ID failure: Embedded app ID is empty."; |
| 171 return; |
| 172 } |
| 173 |
| 174 // Ensuring the message does not carry the embedded app ID. |
| 175 GCMClient::IncomingMessage new_message = message; |
| 176 new_message.data.erase(new_message.data.find(kGCMSendToGaiaIdAppIdKey)); |
| 177 dispatch_message_callback_.Run(embedded_app_id, new_message); |
| 149 } | 178 } |
| 150 | 179 |
| 151 void GCMAccountMapper::OnMessagesDeleted(const std::string& app_id) { | 180 void GCMAccountMapper::OnMessagesDeleted(const std::string& app_id) { |
| 152 // Account message does not expect messages right now. | 181 // Account message does not expect messages right now. |
| 153 } | 182 } |
| 154 | 183 |
| 155 void GCMAccountMapper::OnSendError( | 184 void GCMAccountMapper::OnSendError( |
| 156 const std::string& app_id, | 185 const std::string& app_id, |
| 157 const GCMClient::SendErrorDetails& send_error_details) { | 186 const GCMClient::SendErrorDetails& send_error_details) { |
| 158 DCHECK_EQ(app_id, kGCMAccountMapperAppId); | 187 DCHECK_EQ(app_id, kGCMAccountMapperAppId); |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 } | 383 } |
| 355 | 384 |
| 356 return accounts_.end(); | 385 return accounts_.end(); |
| 357 } | 386 } |
| 358 | 387 |
| 359 void GCMAccountMapper::SetClockForTesting(scoped_ptr<base::Clock> clock) { | 388 void GCMAccountMapper::SetClockForTesting(scoped_ptr<base::Clock> clock) { |
| 360 clock_ = clock.Pass(); | 389 clock_ = clock.Pass(); |
| 361 } | 390 } |
| 362 | 391 |
| 363 } // namespace gcm | 392 } // namespace gcm |
| OLD | NEW |