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