Fix emulator build error
[platform/framework/web/chromium-efl.git] / components / permissions / bluetooth_delegate_impl.h
1 // Copyright 2021 The Chromium Authors
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 COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_
6 #define COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_
7
8 #include <list>
9 #include <map>
10 #include <memory>
11 #include <string>
12 #include <vector>
13
14 #include "base/memory/raw_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/scoped_observation.h"
17 #include "components/permissions/object_permission_context_base.h"
18 #include "content/public/browser/bluetooth_delegate.h"
19 #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h"
20
21 namespace blink {
22 class WebBluetoothDeviceId;
23 }  // namespace blink
24
25 namespace content {
26 class RenderFrameHost;
27 }  // namespace content
28
29 namespace device {
30 class BluetoothDevice;
31 class BluetoothUUID;
32 }  // namespace device
33
34 namespace permissions {
35
36 class BluetoothChooserContext;
37
38 // Provides an interface for managing device permissions for Web Bluetooth and
39 // Web Bluetooth Scanning API.
40 class BluetoothDelegateImpl : public content::BluetoothDelegate {
41  public:
42   // Provides embedder-level functionality to BluetoothDelegateImpl.
43   class Client {
44    public:
45     Client() = default;
46     virtual ~Client() = default;
47
48     Client(const Client&) = delete;
49     Client& operator=(const Client&) = delete;
50
51     // Provides access to a BluetoothChooserContext without transferring
52     // ownership.
53     virtual permissions::BluetoothChooserContext* GetBluetoothChooserContext(
54         content::RenderFrameHost* frame) = 0;
55
56     // See content::BluetoothDelegate::RunBluetoothChooser.
57     virtual std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
58         content::RenderFrameHost* frame,
59         const content::BluetoothChooser::EventHandler& event_handler) = 0;
60
61     // See content::BluetoothDelegate::ShowBluetoothScanningPrompt.
62     virtual std::unique_ptr<content::BluetoothScanningPrompt>
63     ShowBluetoothScanningPrompt(
64         content::RenderFrameHost* frame,
65         const content::BluetoothScanningPrompt::EventHandler&
66             event_handler) = 0;
67
68     // Prompt the user for pairing Bluetooth device.
69     //
70     // The |device_identifier| is a localized string (device name, address,
71     // etc.) displayed to the user for identification purposes. When the
72     // prompt is complete |callback| is called with the result.
73     // |pairing_kind| is to determine which pairing kind of prompt should be
74     // shown.
75     virtual void ShowBluetoothDevicePairDialog(
76         content::RenderFrameHost* frame,
77         const std::u16string& device_identifier,
78         content::BluetoothDelegate::PairPromptCallback callback,
79         content::BluetoothDelegate::PairingKind pairing_kind,
80         const absl::optional<std::u16string>& pin) = 0;
81   };
82
83   explicit BluetoothDelegateImpl(std::unique_ptr<Client> client);
84   ~BluetoothDelegateImpl() override;
85
86   BluetoothDelegateImpl(const BluetoothDelegateImpl&) = delete;
87   BluetoothDelegateImpl& operator=(const BluetoothDelegateImpl&) = delete;
88
89   // BluetoothDelegate implementation:
90   std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
91       content::RenderFrameHost* frame,
92       const content::BluetoothChooser::EventHandler& event_handler) override;
93   std::unique_ptr<content::BluetoothScanningPrompt> ShowBluetoothScanningPrompt(
94       content::RenderFrameHost* frame,
95       const content::BluetoothScanningPrompt::EventHandler& event_handler)
96       override;
97
98   void ShowDevicePairPrompt(content::RenderFrameHost* frame,
99                             const std::u16string& device_identifier,
100                             PairPromptCallback callback,
101                             PairingKind pairing_kind,
102                             const absl::optional<std::u16string>& pin) override;
103
104   blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
105       content::RenderFrameHost* frame,
106       const std::string& device_address) override;
107   std::string GetDeviceAddress(
108       content::RenderFrameHost* frame,
109       const blink::WebBluetoothDeviceId& device_id) override;
110   blink::WebBluetoothDeviceId AddScannedDevice(
111       content::RenderFrameHost* frame,
112       const std::string& device_address) override;
113   blink::WebBluetoothDeviceId GrantServiceAccessPermission(
114       content::RenderFrameHost* frame,
115       const device::BluetoothDevice* device,
116       const blink::mojom::WebBluetoothRequestDeviceOptions* options) override;
117   bool HasDevicePermission(
118       content::RenderFrameHost* frame,
119       const blink::WebBluetoothDeviceId& device_id) override;
120   void RevokeDevicePermissionWebInitiated(
121       content::RenderFrameHost* frame,
122       const blink::WebBluetoothDeviceId& device_id) override;
123   bool IsAllowedToAccessService(content::RenderFrameHost* frame,
124                                 const blink::WebBluetoothDeviceId& device_id,
125                                 const device::BluetoothUUID& service) override;
126   bool IsAllowedToAccessAtLeastOneService(
127       content::RenderFrameHost* frame,
128       const blink::WebBluetoothDeviceId& device_id) override;
129   bool IsAllowedToAccessManufacturerData(
130       content::RenderFrameHost* frame,
131       const blink::WebBluetoothDeviceId& device_id,
132       uint16_t manufacturer_code) override;
133   std::vector<blink::mojom::WebBluetoothDevicePtr> GetPermittedDevices(
134       content::RenderFrameHost* frame) override;
135   void AddFramePermissionObserver(FramePermissionObserver* observer) override;
136   void RemoveFramePermissionObserver(
137       FramePermissionObserver* observer) override;
138
139  private:
140   // Manages the FramePermissionObserver list for a particular RFH. Will
141   // self-delete when the last observer is removed from the |owning_delegate|'s
142   // |chooser_observers_| map.
143   class ChooserContextPermissionObserver
144       : public ObjectPermissionContextBase::PermissionObserver {
145    public:
146     explicit ChooserContextPermissionObserver(
147         BluetoothDelegateImpl* owning_delegate,
148         ObjectPermissionContextBase* context);
149     ~ChooserContextPermissionObserver() override;
150
151     ChooserContextPermissionObserver(const ChooserContextPermissionObserver&) =
152         delete;
153     ChooserContextPermissionObserver& operator=(
154         const ChooserContextPermissionObserver) = delete;
155
156     // ObjectPermissionContextBase::PermissionObserver:
157     void OnPermissionRevoked(const url::Origin& origin) override;
158
159     void AddFramePermissionObserver(FramePermissionObserver* observer);
160     void RemoveFramePermissionObserver(FramePermissionObserver* observer);
161
162    private:
163     raw_ptr<BluetoothDelegateImpl> owning_delegate_;
164     base::ObserverList<FramePermissionObserver> observer_list_;
165     std::list<FramePermissionObserver*> observers_pending_removal_;
166     bool is_traversing_observers_ = false;
167     base::ScopedObservation<ObjectPermissionContextBase,
168                             ObjectPermissionContextBase::PermissionObserver>
169         observer_{this};
170   };
171
172   std::unique_ptr<Client> client_;
173
174   std::map<content::RenderFrameHost*,
175            std::unique_ptr<ChooserContextPermissionObserver>>
176       chooser_observers_;
177 };
178
179 }  // namespace permissions
180
181 #endif  // COMPONENTS_PERMISSIONS_BLUETOOTH_DELEGATE_IMPL_H_