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

Side by Side Diff: components/web_bluetooth/web_bluetooth_device_id.h

Issue 2019853002: bluetooth: Use WebBluetoothDeviceId instead of string (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-uuid-typemap
Patch Set: Fix build Created 4 years, 5 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 #ifndef COMPONENTS_WEB_BLUETOOTH_WEB_BLUETOOTH_DEVICE_ID_H_
6 #define COMPONENTS_WEB_BLUETOOTH_WEB_BLUETOOTH_DEVICE_ID_H_
7
8 #include <string>
9
10 namespace web_bluetooth {
11
12 // Used to uniquely identify a Bluetooth Device for an Origin.
13 // A WebBluetoothDeviceId is generated by base64-encoding a 128bit
14 // string.
15 class WebBluetoothDeviceId {
16 public:
17 // Default constructor that creates an invalid id. We implement it so that
18 // instances of this class in a container, e.g. std::unordered_map, can be
19 // accessed through the [] operator. Trying to call any function of the
20 // resulting object will DCHECK-fail.
21 WebBluetoothDeviceId();
22
23 // DCHECKS that |device_id| is valid.
24 explicit WebBluetoothDeviceId(std::string device_id);
25 ~WebBluetoothDeviceId();
26
27 // Returns the string that represents this WebBluetoothDeviceId.
28 const std::string& str() const;
29
30 // The returned WebBluetoothDeviceId is generated by creating a random 128bit
31 // string and base64-encoding it.
32 static WebBluetoothDeviceId Create();
33
34 // Returns true if base64-decoding |device_id| results in a 128bit string.
35 static bool IsValid(const std::string& device_id);
36
37 bool operator==(const WebBluetoothDeviceId& device_id) const;
38 bool operator!=(const WebBluetoothDeviceId& device_id) const;
39
40 private:
41 std::string device_id_;
42 };
43
44 std::ostream& operator<<(std::ostream& out,
45 const WebBluetoothDeviceId& device_id);
46
47 struct WebBluetoothDeviceIdHash {
48 size_t operator()(const WebBluetoothDeviceId& device_id) const {
49 return std::hash<std::string>()(device_id.str());
50 }
51 };
52
53 } // namespace web_bluetooth
54
55 #endif // COMPONENTS_WEB_BLUETOOTH_WEB_BLUETOOTH_DEVICE_ID_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698