Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: components/gcm_driver/instance_id/instance_id_impl.cc

Issue 1113213002: Generate Instance ID locally. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address one more comment Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/gcm_driver/instance_id/instance_id_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.
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.
68 uint8 bytes[kInstanceIDByteLength];
69 crypto::RandBytes(bytes, kInstanceIDByteLength);
agl 2015/05/04 21:55:54 s/kInstanceIDByteLength/sizeof(bytes)/
jianli 2015/05/04 22:07:20 Done.
70
71 // 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.
72 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.
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),
81 kInstanceIDByteLength),
82 &id_);
83 std::replace(id_.begin(), id_.end(), '+', '-');
84 std::replace(id_.begin(), id_.end(), '/', '_');
85 id_.erase(std::remove(id_.begin(), id_.end(), '='), id_.end());
86
87 creation_time_ = base::Time::Now();
88
89 // TODO(jianli): Save the ID to the store.
49 } 90 }
50 91
51 } // namespace instance_id 92 } // namespace instance_id
OLDNEW
« no previous file with comments | « components/gcm_driver/instance_id/instance_id_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698