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

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

Issue 2658473002: Refactor BluetoothAllowedDevicesMap (Closed)
Patch Set: set --similarity=20 when did git cl upload 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 // Keeps track of which devices and their services are allowed to access.
scheib 2017/01/26 04:27:36 Tracks the devices and their services that a site
juncai 2017/01/30 20:34:55 Done.
27 // their services.
28 // 26 //
29 // |AddDevice| generates device ids, which are random strings that are unique 27 // |AddDevice| generates device ids, which are random strings that are unique
30 // in the map. 28 // in the map.
31 class CONTENT_EXPORT BluetoothAllowedDevicesMap final { 29 class CONTENT_EXPORT BluetoothAllowedDevices final {
32 public: 30 public:
33 BluetoothAllowedDevicesMap(); 31 BluetoothAllowedDevices();
34 ~BluetoothAllowedDevicesMap(); 32 BluetoothAllowedDevices(const BluetoothAllowedDevices& other);
33 ~BluetoothAllowedDevices();
35 34
36 // Adds the Bluetooth Device with |device_address| to the map of allowed 35 // Adds the Bluetooth Device with |device_address| to the map of allowed
37 // devices for that origin. Generates and returns a device id. Because 36 // devices. Generates and returns a device id.
scheib 2017/01/26 04:27:36 a new random device ID so that devices IDs can not
juncai 2017/01/30 20:34:55 Done.
38 // unique origins generate the same hash, unique origins are not supported.
39 // Calling this function with a unique origin will CHECK-fail.
40 const WebBluetoothDeviceId& AddDevice( 37 const WebBluetoothDeviceId& AddDevice(
41 const url::Origin& origin,
42 const std::string& device_address, 38 const std::string& device_address,
43 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options); 39 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options);
44 40
45 // Removes the Bluetooth Device with |device_address| from the map of allowed 41 // Removes the Bluetooth Device with |device_address| from the map of allowed
46 // devices for |origin|. 42 // devices.
47 void RemoveDevice(const url::Origin& origin, 43 void RemoveDevice(const std::string& device_address);
48 const std::string& device_address);
49 44
50 // Returns the Bluetooth Device's id for |origin| if |origin| is allowed to 45 // Returns the Bluetooth Device's id if the device is allowed to access.
scheib 2017/01/26 04:27:36 id if it has been added previously with |AddDevice
juncai 2017/01/30 20:34:55 Done.
51 // access the device. 46 const WebBluetoothDeviceId* GetDeviceId(const std::string& device_address);
52 const WebBluetoothDeviceId* GetDeviceId(const url::Origin& origin,
53 const std::string& device_address);
54 47
55 // For |device_id| in |origin|, returns the Bluetooth device's address. If 48 // 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. 49 // such |device_id|, returns an empty string.
57 const std::string& GetDeviceAddress(const url::Origin& origin, 50 const std::string& GetDeviceAddress(const WebBluetoothDeviceId& device_id);
58 const WebBluetoothDeviceId& device_id);
59 51
60 // Returns true if the origin has previously been granted access to at least 52 // Returns true if access has previously been granted to at least one
61 // one service. 53 // service.
62 bool IsOriginAllowedToAccessAtLeastOneService( 54 bool IsAllowedToAccessAtLeastOneService(
63 const url::Origin& origin,
64 const WebBluetoothDeviceId& device_id) const; 55 const WebBluetoothDeviceId& device_id) const;
65 56
66 // Returns true if the origin has previously been granted access to 57 // Returns true if access has previously been granted to the service.
67 // the service. 58 bool IsAllowedToAccessService(
68 bool IsOriginAllowedToAccessService(
69 const url::Origin& origin,
70 const WebBluetoothDeviceId& device_id, 59 const WebBluetoothDeviceId& device_id,
71 const device::BluetoothUUID& service_uuid) const; 60 const device::BluetoothUUID& service_uuid) const;
72 61
73 private: 62 private:
74 typedef std::unordered_map<std::string, WebBluetoothDeviceId> 63 typedef std::unordered_map<std::string, WebBluetoothDeviceId>
75 DeviceAddressToIdMap; 64 DeviceAddressToIdMap;
76 typedef std::unordered_map<WebBluetoothDeviceId, 65 typedef std::
77 std::string, 66 unordered_map<WebBluetoothDeviceId, std::string, WebBluetoothDeviceIdHash>
78 WebBluetoothDeviceIdHash> 67 DeviceIdToAddressMap;
79 DeviceIdToAddressMap;
80 typedef std::unordered_map< 68 typedef std::unordered_map<
81 WebBluetoothDeviceId, 69 WebBluetoothDeviceId,
82 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>, 70 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>,
83 WebBluetoothDeviceIdHash> 71 WebBluetoothDeviceIdHash>
84 DeviceIdToServicesMap; 72 DeviceIdToServicesMap;
85 73
86 // Returns an id guaranteed to be unique for the map. The id is randomly 74 // 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. 75 // generated so that an origin can't guess the id used in another origin.
88 WebBluetoothDeviceId GenerateUniqueDeviceId(); 76 WebBluetoothDeviceId GenerateUniqueDeviceId();
89 void AddUnionOfServicesTo( 77 void AddUnionOfServicesTo(
90 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, 78 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options,
91 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>* 79 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>*
92 unionOfServices); 80 unionOfServices);
93 81
94 // TODO(ortuno): Now that there is only one instance of this class per frame 82 DeviceAddressToIdMap device_address_to_id_map_;
95 // and that this map gets destroyed when navigating consider removing the 83 DeviceIdToAddressMap device_id_to_address_map_;
96 // origin mapping. 84 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 85
105 // Keep track of all device_ids in the map. 86 // Keep track of all device_ids in the map.
106 std::unordered_set<WebBluetoothDeviceId, WebBluetoothDeviceIdHash> 87 std::unordered_set<WebBluetoothDeviceId, WebBluetoothDeviceIdHash>
107 device_id_set_; 88 device_id_set_;
108 }; 89 };
109 90
110 } // namespace content 91 } // namespace content
111 92
112 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_ 93 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698