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 #ifndef CONTENT_COMMON_BLUETOOTH_WEB_BLUETOOTH_DEVICE_ID_H_ | |
6 #define CONTENT_COMMON_BLUETOOTH_WEB_BLUETOOTH_DEVICE_ID_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "content/common/content_export.h" | |
11 | |
12 namespace content { | |
13 | |
14 // Used to uniquely identify a Bluetooth Device for an Origin. | |
15 // A WebBluetoothDeviceId is generated by base64-encoding a 128bit | |
16 // string. | |
17 class CONTENT_EXPORT WebBluetoothDeviceId { | |
18 public: | |
19 // Default constructor that creates an invalid id. We implement it so that | |
20 // instances of this class in a container, e.g. std::unordered_map, can be | |
21 // accessed through the [] operator. Trying to call any function of the | |
22 // resulting object will DCHECK-fail. | |
23 WebBluetoothDeviceId(); | |
24 | |
25 // DCHECKS that |device_id| is valid. | |
26 explicit WebBluetoothDeviceId(std::string device_id); | |
27 ~WebBluetoothDeviceId(); | |
28 | |
29 // Returns the string that represents this WebBluetoothDeviceId. | |
30 const std::string& str() const; | |
31 | |
32 // The returned WebBluetoothDeviceId is generated by creating a random 128bit | |
33 // string and base64-encoding it. | |
34 static WebBluetoothDeviceId Create(); | |
35 | |
36 // Returns true if base64-decoding |device_id| results in a 128bit string. | |
37 static bool IsValid(const std::string& device_id); | |
38 | |
39 bool operator==(const WebBluetoothDeviceId& device_id) const; | |
40 bool operator!=(const WebBluetoothDeviceId& device_id) const; | |
41 | |
42 private: | |
43 std::string device_id_; | |
44 }; | |
45 | |
46 // This is required by gtest to print a readable output on test failures. | |
47 CONTENT_EXPORT std::ostream& operator<<(std::ostream& out, | |
48 const WebBluetoothDeviceId& device_id); | |
49 | |
50 struct WebBluetoothDeviceIdHash { | |
51 size_t operator()(const WebBluetoothDeviceId& device_id) const { | |
52 return std::hash<std::string>()(device_id.str()); | |
53 } | |
54 }; | |
55 | |
56 } // namespace content | |
57 | |
58 #endif // CONTENT_COMMON_BLUETOOTH_WEB_BLUETOOTH_DEVICE_ID_H_ | |
OLD | NEW |