Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: content/browser/bluetooth/bluetooth_allowed_devices.h

Issue 2658473002: Refactor BluetoothAllowedDevicesMap (Closed)
Patch Set: cleaned up layout test code Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_H_
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_ 6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <unordered_map> 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" 14 #include "base/optional.h"
15 #include "content/common/bluetooth/web_bluetooth_device_id.h" 15 #include "content/common/bluetooth/web_bluetooth_device_id.h"
16 #include "content/common/content_export.h" 16 #include "content/common/content_export.h"
17 #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"
18 #include "url/origin.h"
19 18
20 namespace device { 19 namespace device {
21 class BluetoothUUID; 20 class BluetoothUUID;
22 } 21 }
23 22
24 namespace content { 23 namespace content {
25 24
26 // Keeps track of which origins are allowed to access which devices and 25 // Tracks the devices and their services that a site has been granted
27 // their services. 26 // access to.
28 // 27 //
29 // |AddDevice| generates device ids, which are random strings that are unique 28 // |AddDevice| generates device ids, which are random strings that are unique
30 // in the map. 29 // in the map.
31 class CONTENT_EXPORT BluetoothAllowedDevicesMap final { 30 class CONTENT_EXPORT BluetoothAllowedDevices final {
32 public: 31 public:
33 BluetoothAllowedDevicesMap(); 32 BluetoothAllowedDevices();
34 ~BluetoothAllowedDevicesMap(); 33 BluetoothAllowedDevices(const BluetoothAllowedDevices& other);
34 ~BluetoothAllowedDevices();
35 35
36 // 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
37 // devices for that origin. Generates and returns a device id. Because 37 // devices. Generates and returns a new random device ID so that devices
38 // unique origins generate the same hash, unique origins are not supported. 38 // IDs can not be compared between sites.
39 // Calling this function with a unique origin will CHECK-fail.
40 const WebBluetoothDeviceId& AddDevice( 39 const WebBluetoothDeviceId& AddDevice(
41 const url::Origin& origin,
42 const std::string& device_address, 40 const std::string& device_address,
43 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options); 41 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options);
44 42
45 // Removes the Bluetooth Device with |device_address| from the map of allowed 43 // Removes the Bluetooth Device with |device_address| from the map of allowed
46 // devices for |origin|. 44 // devices.
47 void RemoveDevice(const url::Origin& origin, 45 void RemoveDevice(const std::string& device_address);
48 const std::string& device_address);
49 46
50 // Returns the Bluetooth Device's id for |origin| if |origin| is allowed to 47 // Returns the Bluetooth Device's id if it has been added previously with
51 // access the device. 48 // |AddDevice|.
52 const WebBluetoothDeviceId* GetDeviceId(const url::Origin& origin, 49 const WebBluetoothDeviceId* GetDeviceId(const std::string& device_address);
53 const std::string& device_address);
54 50
55 // For |device_id| in |origin|, returns the Bluetooth device's address. If 51 // For |device_id|, returns the Bluetooth device's address. If there is no
56 // there is no such |device_id| in |origin|, returns an empty string. 52 // such |device_id|, returns an empty string.
57 const std::string& GetDeviceAddress(const url::Origin& origin, 53 const std::string& GetDeviceAddress(const WebBluetoothDeviceId& device_id);
58 const WebBluetoothDeviceId& device_id);
59 54
60 // Returns true if the origin has previously been granted access to at least 55 // Returns true if access has previously been granted to at least one
61 // one service. 56 // service.
62 bool IsOriginAllowedToAccessAtLeastOneService( 57 bool IsAllowedToAccessAtLeastOneService(
63 const url::Origin& origin,
64 const WebBluetoothDeviceId& device_id) const; 58 const WebBluetoothDeviceId& device_id) const;
65 59
66 // Returns true if the origin has previously been granted access to 60 // Returns true if access has previously been granted to the service.
67 // the service. 61 bool IsAllowedToAccessService(
68 bool IsOriginAllowedToAccessService(
69 const url::Origin& origin,
70 const WebBluetoothDeviceId& device_id, 62 const WebBluetoothDeviceId& device_id,
71 const device::BluetoothUUID& service_uuid) const; 63 const device::BluetoothUUID& service_uuid) const;
72 64
73 private: 65 private:
74 typedef std::unordered_map<std::string, WebBluetoothDeviceId> 66 typedef std::unordered_map<std::string, WebBluetoothDeviceId>
75 DeviceAddressToIdMap; 67 DeviceAddressToIdMap;
76 typedef std::unordered_map<WebBluetoothDeviceId, 68 typedef std::
77 std::string, 69 unordered_map<WebBluetoothDeviceId, std::string, WebBluetoothDeviceIdHash>
78 WebBluetoothDeviceIdHash> 70 DeviceIdToAddressMap;
79 DeviceIdToAddressMap;
80 typedef std::unordered_map< 71 typedef std::unordered_map<
81 WebBluetoothDeviceId, 72 WebBluetoothDeviceId,
82 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>, 73 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>,
83 WebBluetoothDeviceIdHash> 74 WebBluetoothDeviceIdHash>
84 DeviceIdToServicesMap; 75 DeviceIdToServicesMap;
85 76
86 // Returns an id guaranteed to be unique for the map. The id is randomly 77 // Returns an id guaranteed to be unique for the map. The id is randomly
87 // generated so that an origin can't guess the id used in another origin. 78 // generated so that an origin can't guess the id used in another origin.
88 WebBluetoothDeviceId GenerateUniqueDeviceId(); 79 WebBluetoothDeviceId GenerateUniqueDeviceId();
89 void AddUnionOfServicesTo( 80 void AddUnionOfServicesTo(
90 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, 81 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options,
91 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>* 82 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>*
92 unionOfServices); 83 unionOfServices);
93 84
94 // TODO(ortuno): Now that there is only one instance of this class per frame 85 DeviceAddressToIdMap device_address_to_id_map_;
95 // and that this map gets destroyed when navigating consider removing the 86 DeviceIdToAddressMap device_id_to_address_map_;
96 // origin mapping. 87 DeviceIdToServicesMap device_id_to_services_map_;
97 // http://crbug.com/610343
98 std::map<url::Origin, DeviceAddressToIdMap>
99 origin_to_device_address_to_id_map_;
100 std::map<url::Origin, DeviceIdToAddressMap>
101 origin_to_device_id_to_address_map_;
102 std::map<url::Origin, DeviceIdToServicesMap>
103 origin_to_device_id_to_services_map_;
104 88
105 // Keep track of all device_ids in the map. 89 // Keep track of all device_ids in the map.
106 std::unordered_set<WebBluetoothDeviceId, WebBluetoothDeviceIdHash> 90 std::unordered_set<WebBluetoothDeviceId, WebBluetoothDeviceIdHash>
107 device_id_set_; 91 device_id_set_;
108 }; 92 };
109 93
110 } // namespace content 94 } // namespace content
111 95
112 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_ 96 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698