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