Commit ba729da9 authored by fgorski's avatar fgorski Committed by Commit bot

[GCM] Adding registration ID request and tests to GCM Account Mapper

Adding registration ID request and tests to GCM Account Mapper
* Adding code to get registration ID from GCM
* Making sure account tokens are only processed when reg ID is present
* Remembering only the last assigned set of account tokens
* Fixing tests that require registration ID
* Adding tests specific to registration ID setting

BUG=374969
R=zea@chromium.org

Review URL: https://codereview.chromium.org/580603002

Cr-Commit-Position: refs/heads/master@{#295890}
parent ed3e65c7
...@@ -47,22 +47,26 @@ GCMAccountMapper::~GCMAccountMapper() { ...@@ -47,22 +47,26 @@ GCMAccountMapper::~GCMAccountMapper() {
} }
void GCMAccountMapper::Initialize( void GCMAccountMapper::Initialize(
const std::vector<AccountMapping>& account_mappings, const std::vector<AccountMapping>& account_mappings) {
const std::string& registration_id) {
DCHECK(!initialized_); DCHECK(!initialized_);
initialized_ = true; initialized_ = true;
registration_id_ = registration_id;
accounts_ = account_mappings; accounts_ = account_mappings;
gcm_driver_->AddAppHandler(kGCMAccountMapperAppId, this); gcm_driver_->AddAppHandler(kGCMAccountMapperAppId, this);
GetRegistration();
// TODO(fgorski): if no registration ID, get registration ID.
} }
void GCMAccountMapper::SetAccountTokens( void GCMAccountMapper::SetAccountTokens(
const std::vector<GCMClient::AccountTokenInfo> account_tokens) { const std::vector<GCMClient::AccountTokenInfo> account_tokens) {
DCHECK(initialized_); // If account mapper is not ready to handle tasks yet, save the latest
// account tokens and return.
if (!IsReady()) {
pending_account_tokens_ = account_tokens;
// If mapper is initialized, but still does not have registration ID,
// maybe the registration gave up. Retrying in case.
if (initialized_)
GetRegistration();
return;
}
// Start from removing the old tokens, from all of the known accounts. // Start from removing the old tokens, from all of the known accounts.
for (AccountMappings::iterator iter = accounts_.begin(); for (AccountMappings::iterator iter = accounts_.begin();
...@@ -214,6 +218,10 @@ bool GCMAccountMapper::CanHandle(const std::string& app_id) const { ...@@ -214,6 +218,10 @@ bool GCMAccountMapper::CanHandle(const std::string& app_id) const {
return app_id.compare(kGCMAccountMapperAppId) == 0; return app_id.compare(kGCMAccountMapperAppId) == 0;
} }
bool GCMAccountMapper::IsReady() {
return initialized_ && !registration_id_.empty();
}
void GCMAccountMapper::SendAddMappingMessage(AccountMapping& account_mapping) { void GCMAccountMapper::SendAddMappingMessage(AccountMapping& account_mapping) {
CreateAndSendMessage(account_mapping); CreateAndSendMessage(account_mapping);
} }
...@@ -257,7 +265,6 @@ void GCMAccountMapper::CreateAndSendMessage( ...@@ -257,7 +265,6 @@ void GCMAccountMapper::CreateAndSendMessage(
account_mapping.account_id)); account_mapping.account_id));
} }
void GCMAccountMapper::OnSendFinished(const std::string& account_id, void GCMAccountMapper::OnSendFinished(const std::string& account_id,
const std::string& message_id, const std::string& message_id,
GCMClient::Result result) { GCMClient::Result result) {
...@@ -280,6 +287,29 @@ void GCMAccountMapper::OnSendFinished(const std::string& account_id, ...@@ -280,6 +287,29 @@ void GCMAccountMapper::OnSendFinished(const std::string& account_id,
gcm_driver_->UpdateAccountMapping(*account_mapping); gcm_driver_->UpdateAccountMapping(*account_mapping);
} }
void GCMAccountMapper::GetRegistration() {
DCHECK(registration_id_.empty());
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();
}
}
}
bool GCMAccountMapper::CanTriggerUpdate( bool GCMAccountMapper::CanTriggerUpdate(
const base::Time& last_update_time) const { const base::Time& last_update_time) const {
return last_update_time + return last_update_time +
......
...@@ -32,8 +32,7 @@ class GCMAccountMapper : public GCMAppHandler { ...@@ -32,8 +32,7 @@ class GCMAccountMapper : public GCMAppHandler {
explicit GCMAccountMapper(GCMDriver* gcm_driver); explicit GCMAccountMapper(GCMDriver* gcm_driver);
virtual ~GCMAccountMapper(); virtual ~GCMAccountMapper();
void Initialize(const AccountMappings& account_mappings, void Initialize(const AccountMappings& account_mappings);
const std::string& registration_id);
// Called by AccountTracker, when a new list of account tokens is available. // Called by AccountTracker, when a new list of account tokens is available.
// This will cause a refresh of account mappings and sending updates to GCM. // This will cause a refresh of account mappings and sending updates to GCM.
...@@ -57,6 +56,9 @@ class GCMAccountMapper : public GCMAppHandler { ...@@ -57,6 +56,9 @@ class GCMAccountMapper : public GCMAppHandler {
typedef std::map<std::string, GCMClient::OutgoingMessage> OutgoingMessages; typedef std::map<std::string, GCMClient::OutgoingMessage> OutgoingMessages;
// Checks whether account mapper is ready to process new account tokens.
bool IsReady();
// Informs GCM of an added or refreshed account mapping. // Informs GCM of an added or refreshed account mapping.
void SendAddMappingMessage(AccountMapping& account_mapping); void SendAddMappingMessage(AccountMapping& account_mapping);
...@@ -70,6 +72,13 @@ class GCMAccountMapper : public GCMAppHandler { ...@@ -70,6 +72,13 @@ class GCMAccountMapper : public GCMAppHandler {
const std::string& message_id, const std::string& message_id,
GCMClient::Result result); GCMClient::Result result);
// Gets a registration for account mapper from GCM.
void GetRegistration();
// Callback for registering with GCM.
void OnRegisterFinished(const std::string& registration_id,
GCMClient::Result result);
// Checks whether the update can be triggered now. If the current time is // Checks whether the update can be triggered now. If the current time is
// within reasonable time (6 hours) of when the update is due, we want to // within reasonable time (6 hours) of when the update is due, we want to
// trigger the update immediately to take advantage of a fresh OAuth2 token. // trigger the update immediately to take advantage of a fresh OAuth2 token.
...@@ -98,6 +107,8 @@ class GCMAccountMapper : public GCMAppHandler { ...@@ -98,6 +107,8 @@ class GCMAccountMapper : public GCMAppHandler {
// Currnetly tracked account mappings. // Currnetly tracked account mappings.
AccountMappings accounts_; AccountMappings accounts_;
std::vector<GCMClient::AccountTokenInfo> pending_account_tokens_;
// GCM Registration ID of the account mapper. // GCM Registration ID of the account mapper.
std::string registration_id_; std::string registration_id_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment