| 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 "components/web_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 web_bluetooth::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 web_bluetooth::WebBluetoothDeviceId* GetDeviceId( |
| 53 const std::string& device_address); | 55 const url::Origin& origin, |
| 56 const std::string& device_address); |
| 54 | 57 |
| 55 // For |device_id| in |origin|, returns the Bluetooth device's address. If | 58 // 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. | 59 // there is no such |device_id| in |origin|, returns an empty string. |
| 57 const std::string& GetDeviceAddress(const url::Origin& origin, | 60 const std::string& GetDeviceAddress( |
| 58 const std::string& device_id); | 61 const url::Origin& origin, |
| 62 const web_bluetooth::WebBluetoothDeviceId& device_id); |
| 59 | 63 |
| 60 // Returns true if the origin has previously been granted access to | 64 // Returns true if the origin has previously been granted access to |
| 61 // the service. | 65 // the service. |
| 62 bool IsOriginAllowedToAccessService( | 66 bool IsOriginAllowedToAccessService( |
| 63 const url::Origin& origin, | 67 const url::Origin& origin, |
| 64 const std::string& device_id, | 68 const web_bluetooth::WebBluetoothDeviceId& device_id, |
| 65 const device::BluetoothUUID& service_uuid) const; | 69 const device::BluetoothUUID& service_uuid) const; |
| 66 | 70 |
| 67 private: | 71 private: |
| 68 typedef std::map<std::string, std::string> DeviceAddressToIdMap; | 72 typedef std::unordered_map<std::string, web_bluetooth::WebBluetoothDeviceId> |
| 69 typedef std::map<std::string, std::string> DeviceIdToAddressMap; | 73 DeviceAddressToIdMap; |
| 70 typedef std::map< | 74 typedef std::unordered_map<web_bluetooth::WebBluetoothDeviceId, |
| 71 std::string, | 75 std::string, |
| 72 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>> | 76 web_bluetooth::WebBluetoothDeviceIdHash> |
| 77 DeviceIdToAddressMap; |
| 78 typedef std::unordered_map< |
| 79 web_bluetooth::WebBluetoothDeviceId, |
| 80 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>, |
| 81 web_bluetooth::WebBluetoothDeviceIdHash> |
| 73 DeviceIdToServicesMap; | 82 DeviceIdToServicesMap; |
| 74 | 83 |
| 75 // Returns an id guaranteed to be unique for the map. The id is randomly | 84 // 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. | 85 // generated so that an origin can't guess the id used in another origin. |
| 77 std::string GenerateDeviceId(); | 86 web_bluetooth::WebBluetoothDeviceId GenerateUniqueDeviceId(); |
| 78 void AddUnionOfServicesTo( | 87 void AddUnionOfServicesTo( |
| 79 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 88 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
| 80 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>* | 89 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>* |
| 81 unionOfServices); | 90 unionOfServices); |
| 82 | 91 |
| 83 // TODO(ortuno): Now that there is only one instance of this class per frame | 92 // 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 | 93 // and that this map gets destroyed when navigating consider removing the |
| 85 // origin mapping. | 94 // origin mapping. |
| 86 // http://crbug.com/610343 | 95 // http://crbug.com/610343 |
| 87 std::map<url::Origin, DeviceAddressToIdMap> | 96 std::map<url::Origin, DeviceAddressToIdMap> |
| 88 origin_to_device_address_to_id_map_; | 97 origin_to_device_address_to_id_map_; |
| 89 std::map<url::Origin, DeviceIdToAddressMap> | 98 std::map<url::Origin, DeviceIdToAddressMap> |
| 90 origin_to_device_id_to_address_map_; | 99 origin_to_device_id_to_address_map_; |
| 91 std::map<url::Origin, DeviceIdToServicesMap> | 100 std::map<url::Origin, DeviceIdToServicesMap> |
| 92 origin_to_device_id_to_services_map_; | 101 origin_to_device_id_to_services_map_; |
| 93 | 102 |
| 94 // Keep track of all device_ids in the map. | 103 // Keep track of all device_ids in the map. |
| 95 std::set<std::string> device_id_set_; | 104 std::unordered_set<web_bluetooth::WebBluetoothDeviceId, |
| 105 web_bluetooth::WebBluetoothDeviceIdHash> |
| 106 device_id_set_; |
| 96 }; | 107 }; |
| 97 | 108 |
| 98 } // namespace content | 109 } // namespace content |
| 99 | 110 |
| 100 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_ | 111 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_ |
| OLD | NEW |