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

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: Address jyasskin's comments Created 4 years, 6 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..ac67fc22923d32056b23190f0d100c7ad8420848
--- /dev/null
+++ b/content/common/bluetooth/bluetooth_device_id.cc
@@ -0,0 +1,75 @@
+// 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(std::string device_id)
+ : device_id_(std::move(device_id)) {
+ DCHECK(IsValid(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)) {
+ return false;
+ }
+
+ if (decoded.size() != kDeviceIdLength) {
+ return false;
+ }
+
+ return true;
+}
+
+bool BluetoothDeviceId::operator==(const BluetoothDeviceId& device_id) const {
+ return str() == device_id.str();
+}
+
+bool BluetoothDeviceId::operator!=(const BluetoothDeviceId& device_id) const {
+ return !(*this == device_id);
+}
+
+std::ostream& operator<<(std::ostream& out,
+ const BluetoothDeviceId& device_id) {
+ return out << device_id.str();
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698