Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/gcm_driver/instance_id/instance_id_impl.h" | 5 #include "components/gcm_driver/instance_id/instance_id_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include "base/base64.h" | |
| 9 #include "base/bind.h" | |
| 7 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "crypto/random.h" | |
| 8 | 13 |
| 9 namespace instance_id { | 14 namespace instance_id { |
| 10 | 15 |
| 11 // static | 16 // static |
| 12 InstanceID* InstanceID::Create(const std::string& app_id) { | 17 InstanceID* InstanceID::Create(const std::string& app_id) { |
| 13 return new InstanceIDImpl(app_id); | 18 return new InstanceIDImpl(app_id); |
| 14 } | 19 } |
| 15 | 20 |
| 16 InstanceIDImpl::InstanceIDImpl(const std::string& app_id) | 21 InstanceIDImpl::InstanceIDImpl(const std::string& app_id) |
| 17 : InstanceID(app_id) { | 22 : InstanceID(app_id) { |
| 18 } | 23 } |
| 19 | 24 |
| 20 InstanceIDImpl::~InstanceIDImpl() { | 25 InstanceIDImpl::~InstanceIDImpl() { |
| 21 } | 26 } |
| 22 | 27 |
| 23 std::string InstanceIDImpl::GetID() { | 28 std::string InstanceIDImpl::GetID() { |
| 24 NOTIMPLEMENTED(); | 29 EnsureIDGenerated(); |
| 25 return std::string(); | 30 return id_; |
| 26 } | 31 } |
| 27 | 32 |
| 28 base::Time InstanceIDImpl::GetCreationTime() { | 33 base::Time InstanceIDImpl::GetCreationTime() { |
| 29 NOTIMPLEMENTED(); | 34 return creation_time_; |
| 30 return base::Time(); | |
| 31 } | 35 } |
| 32 | 36 |
| 33 void InstanceIDImpl::GetToken( | 37 void InstanceIDImpl::GetToken( |
| 34 const std::string& audience, | 38 const std::string& audience, |
| 35 const std::string& scope, | 39 const std::string& scope, |
| 36 const std::map<std::string, std::string>& options, | 40 const std::map<std::string, std::string>& options, |
| 37 const GetTokenCallback& callback) { | 41 const GetTokenCallback& callback) { |
| 38 NOTIMPLEMENTED(); | 42 NOTIMPLEMENTED(); |
| 39 } | 43 } |
| 40 | 44 |
| 41 void InstanceIDImpl::DeleteToken(const std::string& audience, | 45 void InstanceIDImpl::DeleteToken(const std::string& audience, |
| 42 const std::string& scope, | 46 const std::string& scope, |
| 43 const DeleteTokenCallback& callback) { | 47 const DeleteTokenCallback& callback) { |
| 44 NOTIMPLEMENTED(); | 48 NOTIMPLEMENTED(); |
| 45 } | 49 } |
| 46 | 50 |
| 47 void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) { | 51 void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) { |
| 48 NOTIMPLEMENTED(); | 52 // TODO(jianli): Delete the ID from the store. |
| 53 id_.clear(); | |
| 54 creation_time_ = base::Time(); | |
| 55 | |
| 56 base::MessageLoop::current()->PostTask( | |
| 57 FROM_HERE, | |
| 58 base::Bind(callback, this, InstanceID::SUCCESS)); | |
| 59 } | |
| 60 | |
| 61 void InstanceIDImpl::EnsureIDGenerated() { | |
| 62 if (!id_.empty()) | |
| 63 return; | |
| 64 | |
| 65 // Now produce the ID in the following steps: | |
| 66 | |
| 67 // 1) Generates the cryptographically secure random number in 8 bytes. | |
| 68 uint8 bytes[kInstanceIDByteLength]; | |
| 69 crypto::RandBytes(bytes, kInstanceIDByteLength); | |
| 70 | |
| 71 // 2) Transforms the first 4 bits to 0x7. | |
| 72 uint8 b0 = bytes[0]; | |
| 73 b0 = 0x70 + (0xF & b0); | |
| 74 bytes[0] = b0 & 0xFF; | |
| 75 | |
| 76 // 3) Encode the value in Android-compatible base64 scheme: | |
| 77 // * URL safe: '/' replaced by '_' and '+' replaced by '-'. | |
| 78 // * No padding: any trailing '=' will be removed. | |
| 79 base::Base64Encode( | |
| 80 base::StringPiece(reinterpret_cast<const char*>(bytes), 8), &id_); | |
|
fgorski
2015/05/04 20:22:48
s/8/kInstanceIDByteLength/
| |
| 81 std::replace(id_.begin(), id_.end(), '+', '-'); | |
| 82 std::replace(id_.begin(), id_.end(), '/', '_'); | |
| 83 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end()); | |
| 84 | |
| 85 creation_time_ = base::Time::Now(); | |
| 86 | |
| 87 // TODO(jianli): Save the ID to the store. | |
| 49 } | 88 } |
| 50 | 89 |
| 51 } // namespace instance_id | 90 } // namespace instance_id |
| OLD | NEW |