Index: components/gcm_driver/instance_id/instance_id_impl.cc |
diff --git a/components/gcm_driver/instance_id/instance_id_impl.cc b/components/gcm_driver/instance_id/instance_id_impl.cc |
index b77cf3f04fcdf35ab65b09a45bdb7bfbf6f40775..7a03df301033e5bee7d8c6860b9a95cbb7fa2eef 100644 |
--- a/components/gcm_driver/instance_id/instance_id_impl.cc |
+++ b/components/gcm_driver/instance_id/instance_id_impl.cc |
@@ -15,6 +15,32 @@ |
namespace instance_id { |
+namespace { |
+ |
+InstanceID::Result GCMClientResultToInstanceIDResult( |
+ gcm::GCMClient::Result result) { |
+ switch (result) { |
fgorski
2015/05/13 18:32:39
ASYNC_OPERATION_PENDING?
jianli
2015/05/13 22:42:56
Done.
|
+ case gcm::GCMClient::SUCCESS: |
+ return InstanceID::SUCCESS; |
+ case gcm::GCMClient::INVALID_PARAMETER: |
+ return InstanceID::INVALID_PARAMETER; |
+ case gcm::GCMClient::GCM_DISABLED: |
+ return InstanceID::DISABLED; |
+ case gcm::GCMClient::NETWORK_ERROR: |
+ return InstanceID::NETWORK_ERROR; |
+ case gcm::GCMClient::SERVER_ERROR: |
+ return InstanceID::SERVER_ERROR; |
+ case gcm::GCMClient::UNKNOWN_ERROR: |
+ return InstanceID::UNKNOWN_ERROR; |
+ default: |
+ NOTREACHED() << "Unexpected value of result cannot be converted: " |
+ << result; |
+ } |
+ return InstanceID::UNKNOWN_ERROR; |
+} |
+ |
+} // namespace |
+ |
// static |
InstanceID* InstanceID::Create(const std::string& app_id, |
gcm::GCMDriver* gcm_driver) { |
@@ -27,7 +53,7 @@ InstanceIDImpl::InstanceIDImpl(const std::string& app_id, |
gcm_driver_(gcm_driver), |
load_from_store_(false), |
weak_ptr_factory_(this) { |
- gcm_driver_->GetInstanceIDStore()->GetInstanceIDData( |
+ GetInstanceIDHandler()->GetInstanceIDData( |
app_id, |
base::Bind(&InstanceIDImpl::GetInstanceIDDataCompleted, |
weak_ptr_factory_.GetWeakPtr())); |
@@ -75,17 +101,72 @@ void InstanceIDImpl::GetToken( |
const std::string& scope, |
const std::map<std::string, std::string>& options, |
const GetTokenCallback& callback) { |
- NOTIMPLEMENTED(); |
+ if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { |
+ delayed_task_controller_.AddTask( |
+ base::Bind(&InstanceIDImpl::DoGetToken, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ authorized_entity, |
+ scope, |
+ options, |
+ callback)); |
+ return; |
+ } |
+ |
+ DoGetToken(authorized_entity, scope, options, callback); |
+} |
+ |
+void InstanceIDImpl::DoGetToken( |
+ const std::string& authorized_entity, |
+ const std::string& scope, |
+ const std::map<std::string, std::string>& options, |
+ const GetTokenCallback& callback) { |
+ EnsureIDGenerated(); |
+ |
+ GetInstanceIDHandler()->GetToken( |
+ app_id(), |
+ authorized_entity, |
+ scope, |
+ options, |
+ base::Bind(&InstanceIDImpl::OnGetTokenCompleted, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ callback)); |
} |
void InstanceIDImpl::DeleteToken(const std::string& authorized_entity, |
const std::string& scope, |
const DeleteTokenCallback& callback) { |
- NOTIMPLEMENTED(); |
+ if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { |
+ delayed_task_controller_.AddTask( |
+ base::Bind(&InstanceIDImpl::DoDeleteToken, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ authorized_entity, |
+ scope, |
+ callback)); |
+ return; |
+ } |
+ |
+ DoDeleteToken(authorized_entity, scope, callback); |
+} |
+ |
+void InstanceIDImpl::DoDeleteToken( |
+ const std::string& authorized_entity, |
+ const std::string& scope, |
+ const DeleteTokenCallback& callback) { |
+ // Nothing to delete if the ID has not been generated. |
+ if (id_.empty()) |
+ callback.Run(InstanceID::SUCCESS); |
+ |
+ GetInstanceIDHandler()->DeleteToken( |
+ app_id(), |
+ authorized_entity, |
+ scope, |
+ base::Bind(&InstanceIDImpl::OnDeleteTokenCompleted, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ callback)); |
} |
void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) { |
- gcm_driver_->GetInstanceIDStore()->RemoveInstanceIDData(app_id()); |
+ GetInstanceIDHandler()->RemoveInstanceIDData(app_id()); |
id_.clear(); |
creation_time_ = base::Time(); |
@@ -95,12 +176,30 @@ void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) { |
base::Bind(callback, InstanceID::SUCCESS)); |
} |
+void InstanceIDImpl::OnGetTokenCompleted(const GetTokenCallback& callback, |
+ const std::string& token, |
+ gcm::GCMClient::Result result) { |
+ callback.Run(token, GCMClientResultToInstanceIDResult(result)); |
+} |
+ |
+void InstanceIDImpl::OnDeleteTokenCompleted( |
+ const DeleteTokenCallback& callback, |
+ gcm::GCMClient::Result result) { |
+ callback.Run(GCMClientResultToInstanceIDResult(result)); |
+} |
+ |
void InstanceIDImpl::GetInstanceIDDataCompleted( |
const std::string& instance_id_data) { |
Deserialize(instance_id_data); |
delayed_task_controller_.SetReady(); |
} |
+gcm::InstanceIDHandler* InstanceIDImpl::GetInstanceIDHandler() const { |
+ gcm::InstanceIDHandler* handler = gcm_driver_->GetInstanceIDHandler(); |
+ DCHECK(handler); |
+ return handler; |
+} |
+ |
void InstanceIDImpl::EnsureIDGenerated() { |
if (!id_.empty()) |
return; |
@@ -131,7 +230,7 @@ void InstanceIDImpl::EnsureIDGenerated() { |
creation_time_ = base::Time::Now(); |
// Save to the persistent store. |
- gcm_driver_->GetInstanceIDStore()->AddInstanceIDData( |
+ GetInstanceIDHandler()->AddInstanceIDData( |
app_id(), SerializeAsString()); |
} |