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_ |
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_ | 6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
14 #include "url/origin.h" | 14 #include "url/origin.h" |
15 | 15 |
16 namespace device { | 16 namespace device { |
17 class BluetoothUUID; | 17 class BluetoothUUID; |
18 } | 18 } |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 struct BluetoothScanFilter; | 22 struct BluetoothScanFilter; |
23 | 23 |
24 // Keeps track of which origins are allowed to access which devices and | 24 // Keeps track of which origins are allowed to access which devices and |
25 // their services. | 25 // their services. |
26 // | 26 // |
27 // |AddDevice| generates device ids, which are random strings that are unique | 27 // |AddDevice| generates device ids, which are random strings that are unique |
28 // for each (origin, device address) pair. | 28 // in the map. |
29 class CONTENT_EXPORT BluetoothAllowedDevicesMap final { | 29 class CONTENT_EXPORT BluetoothAllowedDevicesMap final { |
30 public: | 30 public: |
31 BluetoothAllowedDevicesMap(); | 31 BluetoothAllowedDevicesMap(); |
32 ~BluetoothAllowedDevicesMap(); | 32 ~BluetoothAllowedDevicesMap(); |
33 | 33 |
34 // Adds the Bluetooth Device with |device_address| to the map of allowed | 34 // Adds the Bluetooth Device with |device_address| to the map of allowed |
35 // devices for that origin. Generates and returns a device id for the | 35 // devices for that origin. Generates and returns a device id. |
36 // (|origin|, |device_address|) pair. | |
37 const std::string& AddDevice( | 36 const std::string& AddDevice( |
38 const url::Origin& origin, | 37 const url::Origin& origin, |
39 const std::string& device_address, | 38 const std::string& device_address, |
40 const std::vector<BluetoothScanFilter>& filters, | 39 const std::vector<BluetoothScanFilter>& filters, |
41 const std::vector<device::BluetoothUUID>& optional_services); | 40 const std::vector<device::BluetoothUUID>& optional_services); |
42 | 41 |
43 // Removes the Bluetooth Device with |device_address| from the map of allowed | 42 // Removes the Bluetooth Device with |device_address| from the map of allowed |
44 // devices for |origin|. | 43 // devices for |origin|. |
45 void RemoveDevice(const url::Origin& origin, | 44 void RemoveDevice(const url::Origin& origin, |
46 const std::string& device_address); | 45 const std::string& device_address); |
47 | 46 |
48 // TODO(ortuno): Add function to check if origin is allowed to access | 47 // TODO(ortuno): Add function to check if origin is allowed to access |
49 // a device's service and add tests for that function. | 48 // a device's service and add tests for that function. |
50 // https://crbug.com/493460 | 49 // https://crbug.com/493460 |
51 | 50 |
52 // Returns the Bluetooth Device's id for |origin|. Returns an empty string | 51 // Returns the Bluetooth Device's id for |origin|. Returns an empty string |
53 // if the origin is not allowed to access the device. | 52 // if |origin| is not allowed to access the device. |
54 const std::string& GetDeviceId(const url::Origin& origin, | 53 const std::string& GetDeviceId(const url::Origin& origin, |
55 const std::string& device_address); | 54 const std::string& device_address); |
56 | 55 |
57 // For |device_id| in |origin|, returns the Bluetooth device's address. If | 56 // 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. | 57 // there is no such |device_id| in |origin|, returns an empty string. |
59 const std::string& GetDeviceAddress(const url::Origin& origin, | 58 const std::string& GetDeviceAddress(const url::Origin& origin, |
60 const std::string& device_id); | 59 const std::string& device_id); |
61 | 60 |
62 private: | 61 private: |
63 typedef std::map<std::string, std::string> DeviceAddressToIdMap; | 62 typedef std::map<std::string, std::string> DeviceAddressToIdMap; |
64 typedef std::map<std::string, std::string> DeviceIdToAddressMap; | 63 typedef std::map<std::string, std::string> DeviceIdToAddressMap; |
65 typedef std::map<std::string, std::set<std::string>> DeviceIdToServicesMap; | 64 typedef std::map<std::string, std::set<std::string>> DeviceIdToServicesMap; |
66 | 65 |
67 // Returns an id guaranteed to be unique for the origin. The id is randomly | 66 // Returns an id guaranteed to be unique for the map. The id is randomly |
68 // generated so that an origin can't guess the id used in another origin. | 67 // generated so that an origin can't guess the id used in another origin. |
69 std::string GenerateDeviceId(const url::Origin& origin); | 68 std::string GenerateDeviceId(); |
70 std::set<std::string> UnionOfServices( | 69 std::set<std::string> UnionOfServices( |
71 const std::vector<BluetoothScanFilter>& filters, | 70 const std::vector<BluetoothScanFilter>& filters, |
72 const std::vector<device::BluetoothUUID>& optional_services); | 71 const std::vector<device::BluetoothUUID>& optional_services); |
73 | 72 |
74 std::map<url::Origin, DeviceAddressToIdMap> | 73 std::map<url::Origin, DeviceAddressToIdMap> |
75 origin_to_device_address_to_id_map_; | 74 origin_to_device_address_to_id_map_; |
76 std::map<url::Origin, DeviceIdToAddressMap> | 75 std::map<url::Origin, DeviceIdToAddressMap> |
77 origin_to_device_id_to_address_map_; | 76 origin_to_device_id_to_address_map_; |
78 std::map<url::Origin, DeviceIdToServicesMap> | 77 std::map<url::Origin, DeviceIdToServicesMap> |
79 origin_to_device_id_to_services_map_; | 78 origin_to_device_id_to_services_map_; |
| 79 |
| 80 // Keep track of all device_ids in the map. |
| 81 std::set<std::string> device_id_set_; |
80 }; | 82 }; |
81 | 83 |
82 } // namespace content | 84 } // namespace content |
83 | 85 |
84 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_ | 86 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_ |
OLD | NEW |