Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/common/bluetooth/bluetooth_device_id.h" | |
| 6 | |
| 7 #include "base/base64.h" | |
| 8 #include "base/strings/string_util.h" | |
| 9 #include "crypto/random.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 enum { kDeviceIdLength = 16 /* 128 bits */ }; | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 19 BluetoothDeviceId::BluetoothDeviceId() {} | |
| 20 | |
| 21 BluetoothDeviceId::BluetoothDeviceId(const std::string& device_id) { | |
|
Jeffrey Yasskin
2016/06/03 17:22:05
Take the string by value, and then move it into de
ortuno
2016/06/06 22:22:59
Done. Why is it better? Either way results in only
Jeffrey Yasskin
2016/06/06 23:44:02
Not quite: in the call at https://codereview.chrom
ortuno
2016/06/24 17:39:43
Ah right. Forgot about the move in the struct trai
| |
| 22 DCHECK(IsValid(device_id)); | |
| 23 device_id_ = device_id; | |
| 24 } | |
| 25 | |
| 26 BluetoothDeviceId::~BluetoothDeviceId() {} | |
| 27 | |
| 28 const std::string& BluetoothDeviceId::str() const { | |
| 29 DCHECK(IsValid(device_id_)); | |
| 30 return device_id_; | |
| 31 } | |
| 32 | |
| 33 // static | |
| 34 BluetoothDeviceId BluetoothDeviceId::Create() { | |
| 35 std::string bytes( | |
| 36 kDeviceIdLength + 1 /* to avoid bytes being reallocated by WriteInto */, | |
| 37 '\0'); | |
| 38 | |
| 39 crypto::RandBytes(base::WriteInto(&bytes /* str */, | |
| 40 kDeviceIdLength + 1 /* length_with_null */), | |
| 41 kDeviceIdLength); | |
| 42 | |
| 43 base::Base64Encode(bytes, &bytes); | |
| 44 | |
| 45 return BluetoothDeviceId(bytes); | |
| 46 } | |
| 47 | |
| 48 // static | |
| 49 bool BluetoothDeviceId::IsValid(const std::string& device_id) { | |
| 50 std::string decoded; | |
| 51 if (!base::Base64Decode(device_id, &decoded)) { | |
|
Jeffrey Yasskin
2016/06/03 17:22:04
There are some strings that will successfully deco
ortuno
2016/06/06 22:22:59
I can't see how someone could use that for any typ
Jeffrey Yasskin
2016/06/06 23:44:02
Right. I don't think we need to check it, but we s
ortuno
2016/06/24 17:39:43
Added an if statement for it.
| |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 if (decoded.size() != kDeviceIdLength) { | |
| 56 return false; | |
| 57 } | |
| 58 | |
| 59 // Expected length of a 128bit string encoded to Base64. | |
| 60 DCHECK_EQ(24u, device_id.size()); | |
|
Jeffrey Yasskin
2016/06/03 17:22:05
I think we don't need these: the Base64Decode won'
ortuno
2016/06/06 22:22:59
Done. This was just me being paranoid.
| |
| 61 // Expected padding characters for a 128bit string encoded to Base64. | |
| 62 DCHECK(device_id[22] == '=' && (device_id[23] == '=')); | |
| 63 | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 bool BluetoothDeviceId::operator==(const BluetoothDeviceId& device_id) const { | |
| 68 DCHECK(IsValid(device_id_) && IsValid(device_id.device_id_)); | |
|
Jeffrey Yasskin
2016/06/03 17:22:05
Since str() does these DCHECKs again, I'd compare
ortuno
2016/06/06 22:22:59
Done.
| |
| 69 | |
| 70 return str() == device_id.str(); | |
| 71 } | |
| 72 | |
| 73 bool BluetoothDeviceId::operator!=(const BluetoothDeviceId& device_id) const { | |
|
Jeffrey Yasskin
2016/06/03 17:22:04
Generally implement != as "return !(*this == devic
ortuno
2016/06/06 22:22:59
Ok! Done.
| |
| 74 DCHECK(IsValid(device_id_) && IsValid(device_id.device_id_)); | |
| 75 | |
| 76 return str() != device_id.str(); | |
| 77 } | |
| 78 | |
| 79 void PrintTo(const BluetoothDeviceId& device_id, std::ostream* out) { | |
| 80 *out << device_id.str(); | |
| 81 } | |
| 82 | |
| 83 } // namespace content | |
| OLD | NEW |