| 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 random number in 8 bytes which is required by the server. |
| 68 // We don't want to be strictly cryptographically secure. The server might |
| 69 // reject the ID if there is a conflict or problem. |
| 70 uint8 bytes[kInstanceIDByteLength]; |
| 71 crypto::RandBytes(bytes, sizeof(bytes)); |
| 72 |
| 73 // 2) Transforms the first 4 bits to 0x7. Note that this is required by the |
| 74 // server. |
| 75 bytes[0] &= 0x0f; |
| 76 bytes[0] |= 0x70; |
| 77 |
| 78 // 3) Encode the value in Android-compatible base64 scheme: |
| 79 // * URL safe: '/' replaced by '_' and '+' replaced by '-'. |
| 80 // * No padding: any trailing '=' will be removed. |
| 81 base::Base64Encode( |
| 82 base::StringPiece(reinterpret_cast<const char*>(bytes), sizeof(bytes)), |
| 83 &id_); |
| 84 std::replace(id_.begin(), id_.end(), '+', '-'); |
| 85 std::replace(id_.begin(), id_.end(), '/', '_'); |
| 86 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end()); |
| 87 |
| 88 creation_time_ = base::Time::Now(); |
| 89 |
| 90 // TODO(jianli): Save the ID to the store. |
| 49 } | 91 } |
| 50 | 92 |
| 51 } // namespace instance_id | 93 } // namespace instance_id |
| OLD | NEW |