File: bluetooth_allowed_devices.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_H_
#define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_H_

#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "base/containers/flat_set.h"
#include "content/common/content_export.h"
#include "third_party/blink/public/common/bluetooth/web_bluetooth_device_id.h"
#include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom.h"

namespace device {
class BluetoothUUID;
}

namespace content {

// Tracks the devices and their services that a site has been granted
// access to.
//
// |AddDevice| generates device ids, which are random strings that are unique
// in the map.
class CONTENT_EXPORT BluetoothAllowedDevices final {
 public:
  BluetoothAllowedDevices();
  BluetoothAllowedDevices(const BluetoothAllowedDevices& other);
  ~BluetoothAllowedDevices();

  // Adds the Bluetooth Device with |device_address| to the map of allowed
  // devices. Generates and returns a new random device ID so that devices
  // IDs can not be compared between sites.
  const blink::WebBluetoothDeviceId& AddDevice(
      const std::string& device_address,
      const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options);

  // Same as the above version of |AddDevice| but does not add any services to
  // the device id -> services map.
  const blink::WebBluetoothDeviceId& AddDevice(
      const std::string& device_address);

  // Removes the Bluetooth Device with |device_address| from the map of allowed
  // devices.
  void RemoveDevice(const std::string& device_address);

  // Returns the Bluetooth Device's id if it has been added previously with
  // |AddDevice|.
  const blink::WebBluetoothDeviceId* GetDeviceId(
      const std::string& device_address);

  // For |device_id|, returns the Bluetooth device's address. If there is no
  // such |device_id|, returns an empty string.
  const std::string& GetDeviceAddress(
      const blink::WebBluetoothDeviceId& device_id);

  // Returns true if access has previously been granted to at least one
  // service.
  bool IsAllowedToAccessAtLeastOneService(
      const blink::WebBluetoothDeviceId& device_id) const;

  // Returns true if access has previously been granted to the service.
  bool IsAllowedToAccessService(
      const blink::WebBluetoothDeviceId& device_id,
      const device::BluetoothUUID& service_uuid) const;

  // Returns true if access has been previously granted for manufacturer data
  // corresponding to |manufacturer_code|.
  bool IsAllowedToAccessManufacturerData(
      const blink::WebBluetoothDeviceId& device_id,
      const uint16_t manufacturer_code) const;

  bool IsAllowedToGATTConnect(
      const blink::WebBluetoothDeviceId& device_id) const;

 private:
  typedef std::unordered_map<std::string, blink::WebBluetoothDeviceId>
      DeviceAddressToIdMap;
  typedef std::unordered_map<blink::WebBluetoothDeviceId,
                             std::string,
                             blink::WebBluetoothDeviceIdHash>
      DeviceIdToAddressMap;
  typedef std::unordered_map<
      blink::WebBluetoothDeviceId,
      std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>,
      blink::WebBluetoothDeviceIdHash>
      DeviceIdToServicesMap;
  typedef std::unordered_map<blink::WebBluetoothDeviceId,
                             bool,
                             blink::WebBluetoothDeviceIdHash>
      DeviceIdToConnectableMap;
  using DeviceIdToManufacturerDataMap =
      std::unordered_map<blink::WebBluetoothDeviceId,
                         base::flat_set<uint16_t>,
                         blink::WebBluetoothDeviceIdHash>;

  // Returns an id guaranteed to be unique for the map. The id is randomly
  // generated so that an origin can't guess the id used in another origin.
  blink::WebBluetoothDeviceId GenerateUniqueDeviceId();
  void AddUnionOfServicesTo(
      const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options,
      std::unordered_set<device::BluetoothUUID, device::BluetoothUUIDHash>*
          unionOfServices);
  void AddManufacturerDataTo(
      const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options,
      base::flat_set<uint16_t>* manufacturer_codes);

  DeviceAddressToIdMap device_address_to_id_map_;
  DeviceIdToAddressMap device_id_to_address_map_;
  DeviceIdToServicesMap device_id_to_services_map_;
  DeviceIdToManufacturerDataMap device_id_to_manufacturers_map_;
  DeviceIdToConnectableMap device_id_to_connectable_map_;

  // Keep track of all device_ids in the map.
  std::unordered_set<blink::WebBluetoothDeviceId,
                     blink::WebBluetoothDeviceIdHash>
      device_id_set_;
};

}  //  namespace content

#endif  // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_ALLOWED_DEVICES_H_