Chromium Code Reviews| 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..ad62823ec76a396a04a541e8317aa48f6fd33907 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,44 @@ 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 cryptographically secure random number in 8 bytes. |
|
agl
2015/05/04 21:55:54
You can't have a cryptographically secure, 60-bit
jianli
2015/05/04 22:07:20
Updated the comment per discussion.
|
| + uint8 bytes[kInstanceIDByteLength]; |
| + crypto::RandBytes(bytes, kInstanceIDByteLength); |
|
agl
2015/05/04 21:55:54
s/kInstanceIDByteLength/sizeof(bytes)/
jianli
2015/05/04 22:07:20
Done.
|
| + |
| + // 2) Transforms the first 4 bits to 0x7. |
|
agl
2015/05/04 21:55:54
Comment should explain why.
jianli
2015/05/04 22:07:20
Done.
|
| + uint8 b0 = bytes[0]; |
|
agl
2015/05/04 21:55:54
bytes[0] &= 0x0f;
bytes[0] |= 0x70;
?
jianli
2015/05/04 22:07:20
Done.
|
| + b0 = 0x70 + (0xF & b0); |
| + bytes[0] = b0 & 0xFF; |
| + |
| + // 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), |
| + kInstanceIDByteLength), |
| + &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 |