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

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

Issue 2658473002: Refactor BluetoothAllowedDevicesMap (Closed)
Patch Set: updated 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_
7
8 #include <memory>
9 #include <string>
10 #include <unordered_map>
11 #include <unordered_set>
12 #include <vector>
13
14 #include "base/optional.h"
15 #include "content/common/bluetooth/web_bluetooth_device_id.h"
16 #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"
19
20 namespace device {
21 class BluetoothUUID;
22 }
23
24 namespace content {
25
26 // Keeps track of which origins are allowed to access which devices and
27 // their services.
28 //
29 // |AddDevice| generates device ids, which are random strings that are unique
30 // in the map.
31 class CONTENT_EXPORT BluetoothAllowedDevicesMap final {
32 public:
33 BluetoothAllowedDevicesMap();
34 ~BluetoothAllowedDevicesMap();
35
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
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(
41 const url::Origin& origin,
42 const std::string& device_address,
43 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options);
44
45 // Removes the Bluetooth Device with |device_address| from the map of allowed
46 // devices for |origin|.
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
73 private:
74 typedef std::unordered_map<std::string, WebBluetoothDeviceId>
75 DeviceAddressToIdMap;
76 typedef std::unordered_map<WebBluetoothDeviceId,
77 std::string,
78 WebBluetoothDeviceIdHash>
79 DeviceIdToAddressMap;
80 typedef std::unordered_map<
81 WebBluetoothDeviceId,
82 std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>,
83 WebBluetoothDeviceIdHash>
84 DeviceIdToServicesMap;
85
86 // 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.
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 };
109
110 } // namespace content
111
112 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_MAP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698