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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_
#define COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_
#include <list>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "components/permissions/object_permission_context_base.h"
#include "content/public/browser/bluetooth_delegate.h"
#include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h"
namespace blink {
class WebBluetoothDeviceId;
} // namespace blink
namespace content {
class RenderFrameHost;
} // namespace content
namespace device {
class BluetoothDevice;
class BluetoothUUID;
} // namespace device
namespace permissions {
class BluetoothChooserContext;
// Provides an interface for managing device permissions for Web Bluetooth and
// Web Bluetooth Scanning API.
class BluetoothDelegateImpl : public content::BluetoothDelegate {
public:
// Provides embedder-level functionality to BluetoothDelegateImpl.
class Client {
public:
Client() = default;
virtual ~Client() = default;
Client(const Client&) = delete;
Client& operator=(const Client&) = delete;
// Provides access to a BluetoothChooserContext without transferring
// ownership.
virtual permissions::BluetoothChooserContext* GetBluetoothChooserContext(
content::RenderFrameHost* frame) = 0;
// See content::BluetoothDelegate::RunBluetoothChooser.
virtual std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
content::RenderFrameHost* frame,
const content::BluetoothChooser::EventHandler& event_handler) = 0;
// See content::BluetoothDelegate::ShowBluetoothScanningPrompt.
virtual std::unique_ptr<content::BluetoothScanningPrompt>
ShowBluetoothScanningPrompt(
content::RenderFrameHost* frame,
const content::BluetoothScanningPrompt::EventHandler&
event_handler) = 0;
// Prompt the user for pairing Bluetooth device.
//
// The |device_identifier| is a localized string (device name, address,
// etc.) displayed to the user for identification purposes. When the
// prompt is complete |callback| is called with the result.
// |pairing_kind| is to determine which pairing kind of prompt should be
// shown.
virtual void ShowBluetoothDevicePairDialog(
content::RenderFrameHost* frame,
const std::u16string& device_identifier,
content::BluetoothDelegate::PairPromptCallback callback,
content::BluetoothDelegate::PairingKind pairing_kind,
const absl::optional<std::u16string>& pin) = 0;
};
explicit BluetoothDelegateImpl(std::unique_ptr<Client> client);
~BluetoothDelegateImpl() override;
BluetoothDelegateImpl(const BluetoothDelegateImpl&) = delete;
BluetoothDelegateImpl& operator=(const BluetoothDelegateImpl&) = delete;
// BluetoothDelegate implementation:
std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
content::RenderFrameHost* frame,
const content::BluetoothChooser::EventHandler& event_handler) override;
std::unique_ptr<content::BluetoothScanningPrompt> ShowBluetoothScanningPrompt(
content::RenderFrameHost* frame,
const content::BluetoothScanningPrompt::EventHandler& event_handler)
override;
void ShowDevicePairPrompt(content::RenderFrameHost* frame,
const std::u16string& device_identifier,
PairPromptCallback callback,
PairingKind pairing_kind,
const absl::optional<std::u16string>& pin) override;
blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
content::RenderFrameHost* frame,
const std::string& device_address) override;
std::string GetDeviceAddress(
content::RenderFrameHost* frame,
const blink::WebBluetoothDeviceId& device_id) override;
blink::WebBluetoothDeviceId AddScannedDevice(
content::RenderFrameHost* frame,
const std::string& device_address) override;
blink::WebBluetoothDeviceId GrantServiceAccessPermission(
content::RenderFrameHost* frame,
const device::BluetoothDevice* device,
const blink::mojom::WebBluetoothRequestDeviceOptions* options) override;
bool HasDevicePermission(
content::RenderFrameHost* frame,
const blink::WebBluetoothDeviceId& device_id) override;
void RevokeDevicePermissionWebInitiated(
content::RenderFrameHost* frame,
const blink::WebBluetoothDeviceId& device_id) override;
bool IsAllowedToAccessService(content::RenderFrameHost* frame,
const blink::WebBluetoothDeviceId& device_id,
const device::BluetoothUUID& service) override;
bool IsAllowedToAccessAtLeastOneService(
content::RenderFrameHost* frame,
const blink::WebBluetoothDeviceId& device_id) override;
bool IsAllowedToAccessManufacturerData(
content::RenderFrameHost* frame,
const blink::WebBluetoothDeviceId& device_id,
uint16_t manufacturer_code) override;
std::vector<blink::mojom::WebBluetoothDevicePtr> GetPermittedDevices(
content::RenderFrameHost* frame) override;
void AddFramePermissionObserver(FramePermissionObserver* observer) override;
void RemoveFramePermissionObserver(
FramePermissionObserver* observer) override;
private:
// Manages the FramePermissionObserver list for a particular RFH. Will
// self-delete when the last observer is removed from the |owning_delegate|'s
// |chooser_observers_| map.
class ChooserContextPermissionObserver
: public ObjectPermissionContextBase::PermissionObserver {
public:
explicit ChooserContextPermissionObserver(
BluetoothDelegateImpl* owning_delegate,
ObjectPermissionContextBase* context);
~ChooserContextPermissionObserver() override;
ChooserContextPermissionObserver(const ChooserContextPermissionObserver&) =
delete;
ChooserContextPermissionObserver& operator=(
const ChooserContextPermissionObserver) = delete;
// ObjectPermissionContextBase::PermissionObserver:
void OnPermissionRevoked(const url::Origin& origin) override;
void AddFramePermissionObserver(FramePermissionObserver* observer);
void RemoveFramePermissionObserver(FramePermissionObserver* observer);
private:
raw_ptr<BluetoothDelegateImpl> owning_delegate_;
base::ObserverList<FramePermissionObserver> observer_list_;
std::list<FramePermissionObserver*> observers_pending_removal_;
bool is_traversing_observers_ = false;
base::ScopedObservation<ObjectPermissionContextBase,
ObjectPermissionContextBase::PermissionObserver>
observer_{this};
};
std::unique_ptr<Client> client_;
std::map<content::RenderFrameHost*,
std::unique_ptr<ChooserContextPermissionObserver>>
chooser_observers_;
};
} // namespace permissions
#endif // COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_
|