Index: components/gcm_driver/gcm_account_mapper.cc |
diff --git a/components/gcm_driver/gcm_account_mapper.cc b/components/gcm_driver/gcm_account_mapper.cc |
index 0e54fd502dd1c9882f212bbadde7ae00046555a7..238b2035a3f8728424127c64ff6ff2a5e36c9319 100644 |
--- a/components/gcm_driver/gcm_account_mapper.cc |
+++ b/components/gcm_driver/gcm_account_mapper.cc |
@@ -47,22 +47,22 @@ GCMAccountMapper::~GCMAccountMapper() { |
} |
void GCMAccountMapper::Initialize( |
- const std::vector<AccountMapping>& account_mappings, |
- const std::string& registration_id) { |
+ const std::vector<AccountMapping>& account_mappings) { |
DCHECK(!initialized_); |
initialized_ = true; |
- registration_id_ = registration_id; |
- |
accounts_ = account_mappings; |
- |
gcm_driver_->AddAppHandler(kGCMAccountMapperAppId, this); |
- |
- // TODO(fgorski): if no registration ID, get registration ID. |
+ Register(); |
} |
void GCMAccountMapper::SetAccountTokens( |
const std::vector<GCMClient::AccountTokenInfo> account_tokens) { |
- DCHECK(initialized_); |
+ // If account mapper is not ready to handle tasks yet, save the lastest |
Nicolas Zea
2014/09/18 17:35:14
nit: lastest -> latest
fgorski
2014/09/18 18:08:53
Done.
|
+ // account tokens and return. |
+ if (!IsReady()) { |
+ pending_account_tokens_ = account_tokens; |
+ return; |
+ } |
// Start from removing the old tokens, from all of the known accounts. |
for (AccountMappings::iterator iter = accounts_.begin(); |
@@ -214,6 +214,10 @@ bool GCMAccountMapper::CanHandle(const std::string& app_id) const { |
return app_id.compare(kGCMAccountMapperAppId) == 0; |
} |
+bool GCMAccountMapper::IsReady() { |
+ return initialized_ && !registration_id_.empty(); |
+} |
+ |
void GCMAccountMapper::SendAddMappingMessage(AccountMapping& account_mapping) { |
CreateAndSendMessage(account_mapping); |
} |
@@ -257,7 +261,6 @@ void GCMAccountMapper::CreateAndSendMessage( |
account_mapping.account_id)); |
} |
- |
void GCMAccountMapper::OnSendFinished(const std::string& account_id, |
const std::string& message_id, |
GCMClient::Result result) { |
@@ -280,6 +283,31 @@ void GCMAccountMapper::OnSendFinished(const std::string& account_id, |
gcm_driver_->UpdateAccountMapping(*account_mapping); |
} |
+void GCMAccountMapper::Register() { |
Nicolas Zea
2014/09/18 17:35:14
nit: dcheck !registration_id_? Or do we want to ha
Nicolas Zea
2014/09/18 17:36:10
Come to think of it, do we have any logic to preve
fgorski
2014/09/18 18:08:53
Yes, the async operation pending will be issued im
fgorski
2014/09/18 18:08:53
Done.
|
+ std::vector<std::string> sender_ids; |
+ sender_ids.push_back(kGCMAccountMapperSenderId); |
+ gcm_driver_->Register(kGCMAccountMapperAppId, |
+ sender_ids, |
+ base::Bind(&GCMAccountMapper::OnRegisterFinished, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void GCMAccountMapper::OnRegisterFinished(const std::string& registration_id, |
+ GCMClient::Result result) { |
+ if (result == GCMClient::SUCCESS) |
+ registration_id_ = registration_id; |
+ |
+ if (IsReady()) { |
+ if (!pending_account_tokens_.empty()) { |
+ SetAccountTokens(pending_account_tokens_); |
+ pending_account_tokens_.clear(); |
+ } |
+ } else { |
+ if (result != GCMClient::ASYNC_OPERATION_PENDING) |
+ Register(); |
Nicolas Zea
2014/09/18 17:35:14
how many times will we retry?
fgorski
2014/09/18 18:08:53
Register implements back-off, so it does not matte
Nicolas Zea
2014/09/19 17:41:58
Does the bail approach mean retry when new tokens
|
+ } |
+} |
+ |
bool GCMAccountMapper::CanTriggerUpdate( |
const base::Time& last_update_time) const { |
return last_update_time + |