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

Unified Diff: content/common/bluetooth/bluetooth_device_id.cc

Issue 2019853002: bluetooth: Use WebBluetoothDeviceId instead of string (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-uuid-typemap
Patch Set: Lint Created 4 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 side-by-side diff with in-line comments
Download patch
Index: content/common/bluetooth/bluetooth_device_id.cc
diff --git a/content/common/bluetooth/bluetooth_device_id.cc b/content/common/bluetooth/bluetooth_device_id.cc
new file mode 100644
index 0000000000000000000000000000000000000000..84415ee1491637224bca0e29577b0c93064b2800
--- /dev/null
+++ b/content/common/bluetooth/bluetooth_device_id.cc
@@ -0,0 +1,83 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/common/bluetooth/bluetooth_device_id.h"
+
+#include "base/base64.h"
+#include "base/strings/string_util.h"
+#include "crypto/random.h"
+
+namespace content {
+
+namespace {
+
+enum { kDeviceIdLength = 16 /* 128 bits */ };
+
+} // namespace
+
+BluetoothDeviceId::BluetoothDeviceId() {}
+
+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
+ DCHECK(IsValid(device_id));
+ device_id_ = device_id;
+}
+
+BluetoothDeviceId::~BluetoothDeviceId() {}
+
+const std::string& BluetoothDeviceId::str() const {
+ DCHECK(IsValid(device_id_));
+ return device_id_;
+}
+
+// static
+BluetoothDeviceId BluetoothDeviceId::Create() {
+ std::string bytes(
+ kDeviceIdLength + 1 /* to avoid bytes being reallocated by WriteInto */,
+ '\0');
+
+ crypto::RandBytes(base::WriteInto(&bytes /* str */,
+ kDeviceIdLength + 1 /* length_with_null */),
+ kDeviceIdLength);
+
+ base::Base64Encode(bytes, &bytes);
+
+ return BluetoothDeviceId(bytes);
+}
+
+// static
+bool BluetoothDeviceId::IsValid(const std::string& device_id) {
+ std::string decoded;
+ 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.
+ return false;
+ }
+
+ if (decoded.size() != kDeviceIdLength) {
+ return false;
+ }
+
+ // Expected length of a 128bit string encoded to Base64.
+ 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.
+ // Expected padding characters for a 128bit string encoded to Base64.
+ DCHECK(device_id[22] == '=' && (device_id[23] == '='));
+
+ return true;
+}
+
+bool BluetoothDeviceId::operator==(const BluetoothDeviceId& device_id) const {
+ 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.
+
+ return str() == device_id.str();
+}
+
+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.
+ DCHECK(IsValid(device_id_) && IsValid(device_id.device_id_));
+
+ return str() != device_id.str();
+}
+
+void PrintTo(const BluetoothDeviceId& device_id, std::ostream* out) {
+ *out << device_id.str();
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698