OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_ | 5 #ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_ |
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_ | 6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_ |
7 | 7 |
8 #include <map> | |
9 #include <memory> | 8 #include <memory> |
10 #include <set> | 9 #include <string> |
| 10 #include <unordered_map> |
11 #include <unordered_set> | 11 #include <unordered_set> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
| 14 #include "base/optional.h" |
| 15 #include "content/common/bluetooth/web_bluetooth_device_id.h" |
14 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
15 #include "third_party/WebKit/public/platform/modules/bluetooth/web_bluetooth.moj
om.h" | 17 #include "third_party/WebKit/public/platform/modules/bluetooth/web_bluetooth.moj
om.h" |
16 #include "url/origin.h" | 18 #include "url/origin.h" |
17 | 19 |
18 namespace device { | 20 namespace device { |
19 class BluetoothUUID; | 21 class BluetoothUUID; |
20 } | 22 } |
21 | 23 |
22 namespace content { | 24 namespace content { |
23 | 25 |
24 struct BluetoothScanFilter; | 26 struct BluetoothScanFilter; |
25 | 27 |
26 // Keeps track of which origins are allowed to access which devices and | 28 // Keeps track of which origins are allowed to access which devices and |
27 // their services. | 29 // their services. |
28 // | 30 // |
29 // |AddDevice| generates device ids, which are random strings that are unique | 31 // |AddDevice| generates device ids, which are random strings that are unique |
30 // in the map. | 32 // in the map. |
31 class CONTENT_EXPORT BluetoothAllowedDevicesMap final { | 33 class CONTENT_EXPORT BluetoothAllowedDevicesMap final { |
32 public: | 34 public: |
33 BluetoothAllowedDevicesMap(); | 35 BluetoothAllowedDevicesMap(); |
34 ~BluetoothAllowedDevicesMap(); | 36 ~BluetoothAllowedDevicesMap(); |
35 | 37 |
36 // Adds the Bluetooth Device with |device_address| to the map of allowed | 38 // Adds the Bluetooth Device with |device_address| to the map of allowed |
37 // devices for that origin. Generates and returns a device id. Because | 39 // devices for that origin. Generates and returns a device id. Because |
38 // unique origins generate the same hash, unique origins are not supported. | 40 // unique origins generate the same hash, unique origins are not supported. |
39 // Calling this function with a unique origin will CHECK-fail. | 41 // Calling this function with a unique origin will CHECK-fail. |
40 const std::string& AddDevice( | 42 const WebBluetoothDeviceId& AddDevice( |
41 const url::Origin& origin, | 43 const url::Origin& origin, |
42 const std::string& device_address, | 44 const std::string& device_address, |
43 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options); | 45 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options); |
44 | 46 |
45 // Removes the Bluetooth Device with |device_address| from the map of allowed | 47 // Removes the Bluetooth Device with |device_address| from the map of allowed |
46 // devices for |origin|. | 48 // devices for |origin|. |
47 void RemoveDevice(const url::Origin& origin, | 49 void RemoveDevice(const url::Origin& origin, |
48 const std::string& device_address); | 50 const std::string& device_address); |
49 | 51 |
50 // Returns the Bluetooth Device's id for |origin|. Returns an empty string | 52 // Returns the Bluetooth Device's id for |origin| if |origin| is allowed to |
51 // if |origin| is not allowed to access the device. | 53 // access the device. |
52 const std::string& GetDeviceId(const url::Origin& origin, | 54 const WebBluetoothDeviceId* GetDeviceId(const url::Origin& origin, |
53 const std::string& device_address); | 55 const std::string& device_address); |
54 | 56 |
55 // For |device_id| in |origin|, returns the Bluetooth device's address. If | 57 // For |device_id| in |origin|, returns the Bluetooth device's address. If |
56 // there is no such |device_id| in |origin|, returns an empty string. | 58 // there is no such |device_id| in |origin|, returns an empty string. |
57 const std::string& GetDeviceAddress(const url::Origin& origin, | 59 const std::string& GetDeviceAddress(const url::Origin& origin, |
58 const std::string& device_id); | 60 const WebBluetoothDeviceId& device_id); |
59 | 61 |
60 // Returns true if the origin has previously been granted access to | 62 // Returns true if the origin has previously been granted access to |
61 // the service. | 63 // the service. |
62 bool IsOriginAllowedToAccessService( | 64 bool IsOriginAllowedToAccessService( |
63 const url::Origin& origin, | 65 const url::Origin& origin, |
64 const std::string& device_id, | 66 const WebBluetoothDeviceId& device_id, |
65 const device::BluetoothUUID& service_uuid) const; | 67 const device::BluetoothUUID& service_uuid) const; |
66 | 68 |
67 private: | 69 private: |
68 typedef std::map<std::string, std::string> DeviceAddressToIdMap; | 70 typedef std::unordered_map<std::string, WebBluetoothDeviceId> |
69 typedef std::map<std::string, std::string> DeviceIdToAddressMap; | 71 DeviceAddressToIdMap; |
70 typedef std::map< | 72 typedef std::unordered_map<WebBluetoothDeviceId, |
71 std::string, | 73 std::string, |
72 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>> | 74 WebBluetoothDeviceIdHash> |
| 75 DeviceIdToAddressMap; |
| 76 typedef std::unordered_map< |
| 77 WebBluetoothDeviceId, |
| 78 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>, |
| 79 WebBluetoothDeviceIdHash> |
73 DeviceIdToServicesMap; | 80 DeviceIdToServicesMap; |
74 | 81 |
75 // Returns an id guaranteed to be unique for the map. The id is randomly | 82 // Returns an id guaranteed to be unique for the map. The id is randomly |
76 // generated so that an origin can't guess the id used in another origin. | 83 // generated so that an origin can't guess the id used in another origin. |
77 std::string GenerateDeviceId(); | 84 WebBluetoothDeviceId GenerateUniqueDeviceId(); |
78 void AddUnionOfServicesTo( | 85 void AddUnionOfServicesTo( |
79 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 86 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
80 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>* | 87 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>* |
81 unionOfServices); | 88 unionOfServices); |
82 | 89 |
83 // TODO(ortuno): Now that there is only one instance of this class per frame | 90 // TODO(ortuno): Now that there is only one instance of this class per frame |
84 // and that this map gets destroyed when navigating consider removing the | 91 // and that this map gets destroyed when navigating consider removing the |
85 // origin mapping. | 92 // origin mapping. |
86 // http://crbug.com/610343 | 93 // http://crbug.com/610343 |
87 std::map<url::Origin, DeviceAddressToIdMap> | 94 std::map<url::Origin, DeviceAddressToIdMap> |
88 origin_to_device_address_to_id_map_; | 95 origin_to_device_address_to_id_map_; |
89 std::map<url::Origin, DeviceIdToAddressMap> | 96 std::map<url::Origin, DeviceIdToAddressMap> |
90 origin_to_device_id_to_address_map_; | 97 origin_to_device_id_to_address_map_; |
91 std::map<url::Origin, DeviceIdToServicesMap> | 98 std::map<url::Origin, DeviceIdToServicesMap> |
92 origin_to_device_id_to_services_map_; | 99 origin_to_device_id_to_services_map_; |
93 | 100 |
94 // Keep track of all device_ids in the map. | 101 // Keep track of all device_ids in the map. |
95 std::set<std::string> device_id_set_; | 102 std::unordered_set<WebBluetoothDeviceId, WebBluetoothDeviceIdHash> |
| 103 device_id_set_; |
96 }; | 104 }; |
97 | 105 |
98 } // namespace content | 106 } // namespace content |
99 | 107 |
100 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_ | 108 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_ |
OLD | NEW |