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

Side by Side Diff: content/browser/bluetooth/bluetooth_allowed_devices_map.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 2017 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_H_
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_ 6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_
7 7
8 #include <memory> 8 #include <map>
9 #include <string>
10 #include <unordered_map>
11 #include <unordered_set>
12 #include <vector>
13 9
14 #include "base/optional.h" 10 #include "base/macros.h"
15 #include "content/common/bluetooth/web_bluetooth_device_id.h" 11 #include "base/memory/ref_counted.h"
16 #include "content/common/content_export.h" 12 #include "content/common/content_export.h"
17 #include "third_party/WebKit/public/platform/modules/bluetooth/web_bluetooth.moj om.h"
18 #include "url/origin.h" 13 #include "url/origin.h"
19 14
20 namespace device {
21 class BluetoothUUID;
22 }
23
24 namespace content { 15 namespace content {
25 16
26 // Keeps track of which origins are allowed to access which devices and 17 class BluetoothAllowedDevices;
27 // their services. 18
28 // 19 // Class for keeping track of which origins are allowed to access which
29 // |AddDevice| generates device ids, which are random strings that are unique 20 // Bluetooth devices and their services.
30 // in the map. 21 class CONTENT_EXPORT BluetoothAllowedDevicesMap
31 class CONTENT_EXPORT BluetoothAllowedDevicesMap final { 22 : public base::RefCountedThreadSafe<BluetoothAllowedDevicesMap> {
32 public: 23 public:
33 BluetoothAllowedDevicesMap(); 24 BluetoothAllowedDevicesMap();
34 ~BluetoothAllowedDevicesMap();
35 25
36 // Adds the Bluetooth Device with |device_address| to the map of allowed 26 // Gets a BluetoothAllowedDevices for each origin; creates one if it doesn't
37 // devices for that origin. Generates and returns a device id. Because 27 // exist.
38 // unique origins generate the same hash, unique origins are not supported. 28 content::BluetoothAllowedDevices& GetOrCreateAllowedDevices(
39 // Calling this function with a unique origin will CHECK-fail. 29 const url::Origin& origin);
40 const WebBluetoothDeviceId& AddDevice(
41 const url::Origin& origin,
42 const std::string& device_address,
43 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options);
44 30
45 // Removes the Bluetooth Device with |device_address| from the map of allowed 31 // Clears the data in |origin_to_allowed_devices_map_|.
46 // devices for |origin|. 32 void Clear();
47 void RemoveDevice(const url::Origin& origin,
48 const std::string& device_address);
49
50 // Returns the Bluetooth Device's id for |origin| if |origin| is allowed to
51 // access the device.
52 const WebBluetoothDeviceId* GetDeviceId(const url::Origin& origin,
53 const std::string& device_address);
54
55 // 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.
57 const std::string& GetDeviceAddress(const url::Origin& origin,
58 const WebBluetoothDeviceId& device_id);
59
60 // Returns true if the origin has previously been granted access to at least
61 // one service.
62 bool IsOriginAllowedToAccessAtLeastOneService(
63 const url::Origin& origin,
64 const WebBluetoothDeviceId& device_id) const;
65
66 // Returns true if the origin has previously been granted access to
67 // the service.
68 bool IsOriginAllowedToAccessService(
69 const url::Origin& origin,
70 const WebBluetoothDeviceId& device_id,
71 const device::BluetoothUUID& service_uuid) const;
72 33
73 private: 34 private:
74 typedef std::unordered_map<std::string, WebBluetoothDeviceId> 35 friend class base::RefCountedThreadSafe<BluetoothAllowedDevicesMap>;
75 DeviceAddressToIdMap; 36 ~BluetoothAllowedDevicesMap();
76 typedef std::unordered_map<WebBluetoothDeviceId, 37 std::map<url::Origin, content::BluetoothAllowedDevices>
77 std::string, 38 origin_to_allowed_devices_map_;
78 WebBluetoothDeviceIdHash>
79 DeviceIdToAddressMap;
80 typedef std::unordered_map<
81 WebBluetoothDeviceId,
82 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>,
83 WebBluetoothDeviceIdHash>
84 DeviceIdToServicesMap;
85 39
86 // Returns an id guaranteed to be unique for the map. The id is randomly 40 DISALLOW_COPY_AND_ASSIGN(BluetoothAllowedDevicesMap);
87 // generated so that an origin can't guess the id used in another origin.
88 WebBluetoothDeviceId GenerateUniqueDeviceId();
89 void AddUnionOfServicesTo(
90 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options,
91 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>*
92 unionOfServices);
93
94 // TODO(ortuno): Now that there is only one instance of this class per frame
95 // and that this map gets destroyed when navigating consider removing the
96 // origin mapping.
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
105 // Keep track of all device_ids in the map.
106 std::unordered_set<WebBluetoothDeviceId, WebBluetoothDeviceIdHash>
107 device_id_set_;
108 }; 41 };
109 42
110 } // namespace content 43 } // namespace content
111 44
112 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_ 45 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698