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

Side by Side Diff: content/common/bluetooth/web_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: Rebase Created 4 years, 4 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
OLDNEW
(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/web_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 WebBluetoothDeviceId::WebBluetoothDeviceId() {}
20
21 WebBluetoothDeviceId::WebBluetoothDeviceId(std::string device_id)
22 : device_id_(std::move(device_id)) {
23 CHECK(IsValid(device_id_));
24 }
25
26 WebBluetoothDeviceId::~WebBluetoothDeviceId() {}
27
28 const std::string& WebBluetoothDeviceId::str() const {
29 CHECK(IsValid(device_id_));
30 return device_id_;
31 }
32
33 // static
34 WebBluetoothDeviceId WebBluetoothDeviceId::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 WebBluetoothDeviceId(std::move(bytes));
46 }
47
48 // static
49 bool WebBluetoothDeviceId::IsValid(const std::string& device_id) {
50 std::string decoded;
51 if (!base::Base64Decode(device_id, &decoded)) {
52 return false;
53 }
54
55 if (decoded.size() != kDeviceIdLength) {
56 return false;
57 }
58
59 // When base64-encoding a 128bit string, only the two MSB are used for
60 // the 3rd-to-last character. Because of this, the 3rd-to-last character
61 // can only be one of this four characters.
62 if (!(device_id[device_id.size() - 3] == 'A' ||
63 device_id[device_id.size() - 3] == 'Q' ||
64 device_id[device_id.size() - 3] == 'g' ||
65 device_id[device_id.size() - 3] == 'w')) {
66 return false;
67 }
68
69 return true;
70 }
71
72 bool WebBluetoothDeviceId::operator==(
73 const WebBluetoothDeviceId& device_id) const {
74 return str() == device_id.str();
75 }
76
77 bool WebBluetoothDeviceId::operator!=(
78 const WebBluetoothDeviceId& device_id) const {
79 return !(*this == device_id);
80 }
81
82 std::ostream& operator<<(std::ostream& out,
83 const WebBluetoothDeviceId& device_id) {
84 return out << device_id.str();
85 }
86
87 } // namespace content
OLDNEW
« no previous file with comments | « content/common/bluetooth/web_bluetooth_device_id.h ('k') | content/common/bluetooth/web_bluetooth_device_id.typemap » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698