| 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 4d76ad2286cde3fddb590f048170526d1b93b50c..1dfaab78ec9e5a95f1fd39eb076436f1ba1d6ca7 100644
|
| --- a/components/gcm_driver/instance_id/instance_id_impl.cc
|
| +++ b/components/gcm_driver/instance_id/instance_id_impl.cc
|
| @@ -4,7 +4,12 @@
|
|
|
| #include "components/gcm_driver/instance_id/instance_id_impl.h"
|
|
|
| +#include <algorithm>
|
| +#include "base/base64.h"
|
| +#include "base/bind.h"
|
| #include "base/logging.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "crypto/random.h"
|
|
|
| namespace instance_id {
|
|
|
| @@ -21,13 +26,12 @@ InstanceIDImpl::~InstanceIDImpl() {
|
| }
|
|
|
| std::string InstanceIDImpl::GetID() {
|
| - NOTIMPLEMENTED();
|
| - return std::string();
|
| + EnsureIDGenerated();
|
| + return id_;
|
| }
|
|
|
| base::Time InstanceIDImpl::GetCreationTime() {
|
| - NOTIMPLEMENTED();
|
| - return base::Time();
|
| + return creation_time_;
|
| }
|
|
|
| void InstanceIDImpl::GetToken(
|
| @@ -45,7 +49,45 @@ void InstanceIDImpl::DeleteToken(const std::string& audience,
|
| }
|
|
|
| void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) {
|
| - NOTIMPLEMENTED();
|
| + // TODO(jianli): Delete the ID from the store.
|
| + id_.clear();
|
| + creation_time_ = base::Time();
|
| +
|
| + base::MessageLoop::current()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(callback, this, InstanceID::SUCCESS));
|
| +}
|
| +
|
| +void InstanceIDImpl::EnsureIDGenerated() {
|
| + if (!id_.empty())
|
| + return;
|
| +
|
| + // Now produce the ID in the following steps:
|
| +
|
| + // 1) Generates the random number in 8 bytes which is required by the server.
|
| + // We don't want to be strictly cryptographically secure. The server might
|
| + // reject the ID if there is a conflict or problem.
|
| + uint8 bytes[kInstanceIDByteLength];
|
| + crypto::RandBytes(bytes, sizeof(bytes));
|
| +
|
| + // 2) Transforms the first 4 bits to 0x7. Note that this is required by the
|
| + // server.
|
| + bytes[0] &= 0x0f;
|
| + bytes[0] |= 0x70;
|
| +
|
| + // 3) Encode the value in Android-compatible base64 scheme:
|
| + // * URL safe: '/' replaced by '_' and '+' replaced by '-'.
|
| + // * No padding: any trailing '=' will be removed.
|
| + base::Base64Encode(
|
| + base::StringPiece(reinterpret_cast<const char*>(bytes), sizeof(bytes)),
|
| + &id_);
|
| + std::replace(id_.begin(), id_.end(), '+', '-');
|
| + std::replace(id_.begin(), id_.end(), '/', '_');
|
| + id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end());
|
| +
|
| + creation_time_ = base::Time::Now();
|
| +
|
| + // TODO(jianli): Save the ID to the store.
|
| }
|
|
|
| } // namespace instance_id
|
|
|