| 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 0ebcdf82d8d48e6dc36e7d72f4b0081d3d35db0a..b77cf3f04fcdf35ab65b09a45bdb7bfbf6f40775 100644
|
| --- a/components/gcm_driver/instance_id/instance_id_impl.cc
|
| +++ b/components/gcm_driver/instance_id/instance_id_impl.cc
|
| @@ -9,6 +9,7 @@
|
| #include "base/bind.h"
|
| #include "base/logging.h"
|
| #include "base/message_loop/message_loop.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| #include "components/gcm_driver/gcm_driver_desktop.h"
|
| #include "crypto/random.h"
|
|
|
| @@ -23,19 +24,50 @@ InstanceID* InstanceID::Create(const std::string& app_id,
|
| InstanceIDImpl::InstanceIDImpl(const std::string& app_id,
|
| gcm::GCMDriver* gcm_driver)
|
| : InstanceID(app_id),
|
| - gcm_driver_(gcm_driver) {
|
| + gcm_driver_(gcm_driver),
|
| + load_from_store_(false),
|
| + weak_ptr_factory_(this) {
|
| + gcm_driver_->GetInstanceIDStore()->GetInstanceIDData(
|
| + app_id,
|
| + base::Bind(&InstanceIDImpl::GetInstanceIDDataCompleted,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| }
|
|
|
| InstanceIDImpl::~InstanceIDImpl() {
|
| }
|
|
|
| -std::string InstanceIDImpl::GetID() {
|
| +void InstanceIDImpl::GetID(const GetIDCallback& callback) {
|
| + if (!delayed_task_controller_.CanRunTaskWithoutDelay()) {
|
| + delayed_task_controller_.AddTask(
|
| + base::Bind(&InstanceIDImpl::DoGetID,
|
| + weak_ptr_factory_.GetWeakPtr(),
|
| + callback));
|
| + return;
|
| + }
|
| +
|
| + DoGetID(callback);
|
| +}
|
| +
|
| +void InstanceIDImpl::DoGetID(const GetIDCallback& callback) {
|
| EnsureIDGenerated();
|
| - return id_;
|
| + callback.Run(id_);
|
| +}
|
| +
|
| +void InstanceIDImpl::GetCreationTime(const GetCreationTimeCallback& callback) {
|
| + if (!delayed_task_controller_.CanRunTaskWithoutDelay()) {
|
| + delayed_task_controller_.AddTask(
|
| + base::Bind(&InstanceIDImpl::DoGetCreationTime,
|
| + weak_ptr_factory_.GetWeakPtr(),
|
| + callback));
|
| + return;
|
| + }
|
| +
|
| + DoGetCreationTime(callback);
|
| }
|
|
|
| -base::Time InstanceIDImpl::GetCreationTime() {
|
| - return creation_time_;
|
| +void InstanceIDImpl::DoGetCreationTime(
|
| + const GetCreationTimeCallback& callback) {
|
| + callback.Run(creation_time_);
|
| }
|
|
|
| void InstanceIDImpl::GetToken(
|
| @@ -53,7 +85,8 @@ void InstanceIDImpl::DeleteToken(const std::string& authorized_entity,
|
| }
|
|
|
| void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) {
|
| - // TODO(jianli): Delete the ID from the store.
|
| + gcm_driver_->GetInstanceIDStore()->RemoveInstanceIDData(app_id());
|
| +
|
| id_.clear();
|
| creation_time_ = base::Time();
|
|
|
| @@ -62,6 +95,12 @@ void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) {
|
| base::Bind(callback, InstanceID::SUCCESS));
|
| }
|
|
|
| +void InstanceIDImpl::GetInstanceIDDataCompleted(
|
| + const std::string& instance_id_data) {
|
| + Deserialize(instance_id_data);
|
| + delayed_task_controller_.SetReady();
|
| +}
|
| +
|
| void InstanceIDImpl::EnsureIDGenerated() {
|
| if (!id_.empty())
|
| return;
|
| @@ -91,7 +130,36 @@ void InstanceIDImpl::EnsureIDGenerated() {
|
|
|
| creation_time_ = base::Time::Now();
|
|
|
| - // TODO(jianli): Save the ID to the store.
|
| + // Save to the persistent store.
|
| + gcm_driver_->GetInstanceIDStore()->AddInstanceIDData(
|
| + app_id(), SerializeAsString());
|
| +}
|
| +
|
| +std::string InstanceIDImpl::SerializeAsString() const {
|
| + std::string serialized_data;
|
| + serialized_data += id_;
|
| + serialized_data += ",";
|
| + serialized_data += base::Int64ToString(creation_time_.ToInternalValue());
|
| + return serialized_data;
|
| +}
|
| +
|
| +void InstanceIDImpl::Deserialize(const std::string& serialized_data) {
|
| + if (serialized_data.empty())
|
| + return;
|
| + std::size_t pos = serialized_data.find(',');
|
| + if (pos == std::string::npos) {
|
| + DVLOG(1) << "Failed to deserialize the InstanceID data: " + serialized_data;
|
| + return;
|
| + }
|
| +
|
| + id_ = serialized_data.substr(0, pos);
|
| +
|
| + int64 time_internal = 0LL;
|
| + if (!base::StringToInt64(serialized_data.substr(pos + 1), &time_internal)) {
|
| + DVLOG(1) << "Failed to deserialize the InstanceID data: " + serialized_data;
|
| + return;
|
| + }
|
| + creation_time_ = base::Time::FromInternalValue(time_internal);
|
| }
|
|
|
| } // namespace instance_id
|
|
|