blob: 8d09e0325fa14f326569aae221d647f7531d4606 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2019 The Chromium Authors
Ovidio Henriquez3b2827132019-12-04 17:44:302// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Michael van Ouwerkerkc7639b62021-05-20 12:08:235#ifndef COMPONENTS_PERMISSIONS_CONTEXTS_BLUETOOTH_CHOOSER_CONTEXT_H_
6#define COMPONENTS_PERMISSIONS_CONTEXTS_BLUETOOTH_CHOOSER_CONTEXT_H_
Ovidio Henriquez3b2827132019-12-04 17:44:307
Ovidio Henriquezefc822a32020-01-14 23:44:138#include <map>
Arthur Sonzognic571efb2024-01-26 20:26:189#include <optional>
Ovidio Henriquez3b2827132019-12-04 17:44:3010#include <string>
Ovidio Henriquez3d729f62020-02-07 00:43:2911#include <utility>
Ovidio Henriquez3b2827132019-12-04 17:44:3012
Ken Buchanan76365d12021-04-20 18:46:1913#include "components/permissions/object_permission_context_base.h"
Ovidio Henriquez3b2827132019-12-04 17:44:3014#include "third_party/blink/public/common/bluetooth/web_bluetooth_device_id.h"
Ovidio Henriquezefc822a32020-01-14 23:44:1315#include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h"
Ovidio Henriquez3b2827132019-12-04 17:44:3016
17namespace base {
18class Value;
19} // namespace base
20
Michael van Ouwerkerkc7639b62021-05-20 12:08:2321namespace content {
22class BrowserContext;
23} // namespace content
24
25namespace device {
26class BluetoothDevice;
27class BluetoothUUID;
28} // namespace device
29
Ovidio Henriquez3b2827132019-12-04 17:44:3030namespace url {
31class Origin;
32} // namespace url
33
Michael van Ouwerkerkc7639b62021-05-20 12:08:2334namespace permissions {
35
Ovidio Henriquez3b2827132019-12-04 17:44:3036// Manages the permissions for Web Bluetooth device objects. A Web Bluetooth
37// permission object consists of its WebBluetoothDeviceId and set of Bluetooth
38// service UUIDs. The WebBluetoothDeviceId is generated randomly by this class
39// and is unique for a given Bluetooth device address and origin pair, so this
40// class stores this mapping and provides utility methods to convert between
41// the WebBluetoothDeviceId and Bluetooth device address.
Michael van Ouwerkerkc7639b62021-05-20 12:08:2342class BluetoothChooserContext : public ObjectPermissionContextBase {
Ovidio Henriquez3b2827132019-12-04 17:44:3043 public:
Michael van Ouwerkerkc7639b62021-05-20 12:08:2344 explicit BluetoothChooserContext(content::BrowserContext* browser_context);
Ovidio Henriquez3b2827132019-12-04 17:44:3045 ~BluetoothChooserContext() override;
46
47 // Set class as move-only.
48 BluetoothChooserContext(const BluetoothChooserContext&) = delete;
49 BluetoothChooserContext& operator=(const BluetoothChooserContext&) = delete;
50
Jack Hsieh794560112023-01-26 15:58:5151 static base::Value::Dict DeviceInfoToValue(
52 const device::BluetoothDevice* device,
53 const blink::mojom::WebBluetoothRequestDeviceOptions* options,
54 const blink::WebBluetoothDeviceId& device_id);
55
Ovidio Henriquez3b2827132019-12-04 17:44:3056 // Helper methods for converting between a WebBluetoothDeviceId and a
57 // Bluetooth device address string for a given origin pair.
Ovidio Henriquezefc822a32020-01-14 23:44:1358 blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
Andy Paicu13f3f462021-03-09 12:14:5659 const url::Origin& origin,
Ovidio Henriquez3b2827132019-12-04 17:44:3060 const std::string& device_address);
Andy Paicu13f3f462021-03-09 12:14:5661 std::string GetDeviceAddress(const url::Origin& origin,
Ovidio Henriquezefc822a32020-01-14 23:44:1362 const blink::WebBluetoothDeviceId& device_id);
63
64 // Bluetooth scanning specific interface for generating WebBluetoothDeviceIds
65 // for scanned devices.
66 blink::WebBluetoothDeviceId AddScannedDevice(
Andy Paicu13f3f462021-03-09 12:14:5667 const url::Origin& origin,
Ovidio Henriquezefc822a32020-01-14 23:44:1368 const std::string& device_address);
Ovidio Henriquez3b2827132019-12-04 17:44:3069
70 // Bluetooth-specific interface for granting and checking permissions.
Ovidio Henriquezefc822a32020-01-14 23:44:1371 blink::WebBluetoothDeviceId GrantServiceAccessPermission(
Andy Paicu13f3f462021-03-09 12:14:5672 const url::Origin& origin,
Ovidio Henriquezefc822a32020-01-14 23:44:1373 const device::BluetoothDevice* device,
Ovidio Henriquez3d729f62020-02-07 00:43:2974 const blink::mojom::WebBluetoothRequestDeviceOptions* options);
Andy Paicu13f3f462021-03-09 12:14:5675 bool HasDevicePermission(const url::Origin& origin,
Ovidio Henriquez3b2827132019-12-04 17:44:3076 const blink::WebBluetoothDeviceId& device_id);
François Beaufortf100253a82022-03-10 11:26:3177 void RevokeDevicePermissionWebInitiated(
78 const url::Origin& origin,
79 const blink::WebBluetoothDeviceId& device_id);
Ovidio Henriquez3b2827132019-12-04 17:44:3080 bool IsAllowedToAccessAtLeastOneService(
Andy Paicu13f3f462021-03-09 12:14:5681 const url::Origin& origin,
Ovidio Henriquez3b2827132019-12-04 17:44:3082 const blink::WebBluetoothDeviceId& device_id);
Andy Paicu13f3f462021-03-09 12:14:5683 bool IsAllowedToAccessService(const url::Origin& origin,
Ovidio Henriquez3b2827132019-12-04 17:44:3084 const blink::WebBluetoothDeviceId& device_id,
Ovidio Henriquezbbc7853c2020-09-17 22:36:5685 const device::BluetoothUUID& service);
86 bool IsAllowedToAccessManufacturerData(
Andy Paicu13f3f462021-03-09 12:14:5687 const url::Origin& origin,
Ovidio Henriquezbbc7853c2020-09-17 22:36:5688 const blink::WebBluetoothDeviceId& device_id,
89 uint16_t manufacturer_code);
Ovidio Henriquez3b2827132019-12-04 17:44:3090
Ovidio Henriquezab1ae302020-03-10 04:45:3891 static blink::WebBluetoothDeviceId GetObjectDeviceId(
Yoichi Osatoc9188252023-04-27 01:21:26