blob: 1401ea2acd93e6634c9fc1749cfe22c77dc63974 [file] [log] [blame]
Ovidio Henriquez3b2827132019-12-04 17:44:301// Copyright 2019 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 CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_
6#define CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_
7
Ovidio Henriquezefc822a32020-01-14 23:44:138#include <map>
Ovidio Henriquez3b2827132019-12-04 17:44:309#include <string>
Ovidio Henriquez3d729f62020-02-07 00:43:2910#include <utility>
Ovidio Henriquez3b2827132019-12-04 17:44:3011
12#include "base/containers/flat_set.h"
13#include "chrome/browser/permissions/chooser_context_base.h"
Ovidio Henriquezefc822a32020-01-14 23:44:1314#include "device/bluetooth/bluetooth_adapter.h"
15#include "device/bluetooth/bluetooth_device.h"
Ovidio Henriquez3b2827132019-12-04 17:44:3016#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
17#include "third_party/blink/public/common/bluetooth/web_bluetooth_device_id.h"
Ovidio Henriquezefc822a32020-01-14 23:44:1318#include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h"
Ovidio Henriquez3b2827132019-12-04 17:44:3019
20namespace base {
21class Value;
22} // namespace base
23
24namespace url {
25class Origin;
26} // namespace url
27
28// Manages the permissions for Web Bluetooth device objects. A Web Bluetooth
29// permission object consists of its WebBluetoothDeviceId and set of Bluetooth
30// service UUIDs. The WebBluetoothDeviceId is generated randomly by this class
31// and is unique for a given Bluetooth device address and origin pair, so this
32// class stores this mapping and provides utility methods to convert between
33// the WebBluetoothDeviceId and Bluetooth device address.
34class BluetoothChooserContext : public ChooserContextBase {
35 public:
36 explicit BluetoothChooserContext(Profile* profile);
37 ~BluetoothChooserContext() override;
38
39 // Set class as move-only.
40 BluetoothChooserContext(const BluetoothChooserContext&) = delete;
41 BluetoothChooserContext& operator=(const BluetoothChooserContext&) = delete;
42
43 // Helper methods for converting between a WebBluetoothDeviceId and a
44 // Bluetooth device address string for a given origin pair.
Ovidio Henriquezefc822a32020-01-14 23:44:1345 blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
Ovidio Henriquez3b2827132019-12-04 17:44:3046 const url::Origin& requesting_origin,
47 const url::Origin& embedding_origin,
48 const std::string& device_address);
Ovidio Henriquezefc822a32020-01-14 23:44:1349 std::string GetDeviceAddress(const url::Origin& requesting_origin,
50 const url::Origin& embedding_origin,
51 const blink::WebBluetoothDeviceId& device_id);
52
53 // Bluetooth scanning specific interface for generating WebBluetoothDeviceIds
54 // for scanned devices.
55 blink::WebBluetoothDeviceId AddScannedDevice(
Ovidio Henriquez3b2827132019-12-04 17:44:3056 const url::Origin& requesting_origin,
57 const url::Origin& embedding_origin,
Ovidio Henriquezefc822a32020-01-14 23:44:1358 const std::string& device_address);
Ovidio Henriquez3b2827132019-12-04 17:44:3059
60 // Bluetooth-specific interface for granting and checking permissions.
Ovidio Henriquezefc822a32020-01-14 23:44:1361 blink::WebBluetoothDeviceId GrantServiceAccessPermission(
Ovidio Henriquez3b2827132019-12-04 17:44:3062 const url::Origin& requesting_origin,
63 const url::Origin& embedding_origin,
Ovidio Henriquezefc822a32020-01-14 23:44:1364 const device::BluetoothDevice* device,
Ovidio Henriquez3d729f62020-02-07 00:43:2965 const blink::mojom::WebBluetoothRequestDeviceOptions* options);
Ovidio Henriquez3b2827132019-12-04 17:44:3066 bool HasDevicePermission(const url::Origin& requesting_origin,
67 const url::Origin& embedding_origin,
68 const blink::WebBluetoothDeviceId& device_id);
69 bool IsAllowedToAccessAtLeastOneService(
70 const url::Origin& requesting_origin,
71 const url::Origin& embedding_origin,
72 const blink::WebBluetoothDeviceId& device_id);
73 bool IsAllowedToAccessService(const url::Origin& requesting_origin,
74 const url::Origin& embedding_origin,
75 const blink::WebBluetoothDeviceId& device_id,
76 device::BluetoothUUID service);
77
78 protected:
79 // ChooserContextBase implementation;
80 bool IsValidObject(const base::Value& object) override;
Ovidio Henriquezefc822a32020-01-14 23:44:1381
82 private:
83 // This map records the generated Web Bluetooth IDs for devices discovered via
84 // the Scanning API. Each requesting/embedding origin pair has its own version
85 // of this map so that IDs cannot be correlated between cross-origin sites.
86 using DeviceAddressToIdMap =
87 std::map<std::string, blink::WebBluetoothDeviceId>;
88 std::map<std::pair<url::Origin, url::Origin>, DeviceAddressToIdMap>
89 scanned_devices_;
Ovidio Henriquez3b2827132019-12-04 17:44:3090};
91
92#endif // CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_