bluetooth_delegate_impl.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2021 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. #include "components/permissions/bluetooth_delegate_impl.h"
  5. #include "base/observer_list.h"
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "components/permissions/contexts/bluetooth_chooser_context.h"
  8. #include "content/public/browser/render_frame_host.h"
  9. #include "device/bluetooth/bluetooth_device.h"
  10. #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
  11. #include "third_party/blink/public/common/bluetooth/web_bluetooth_device_id.h"
  12. #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom.h"
  13. using blink::WebBluetoothDeviceId;
  14. using content::RenderFrameHost;
  15. using device::BluetoothUUID;
  16. namespace permissions {
  17. BluetoothDelegateImpl::BluetoothDelegateImpl(std::unique_ptr<Client> client)
  18. : client_(std::move(client)) {}
  19. BluetoothDelegateImpl::~BluetoothDelegateImpl() = default;
  20. std::unique_ptr<content::BluetoothChooser>
  21. BluetoothDelegateImpl::RunBluetoothChooser(
  22. content::RenderFrameHost* frame,
  23. const content::BluetoothChooser::EventHandler& event_handler) {
  24. return client_->RunBluetoothChooser(frame, event_handler);
  25. }
  26. std::unique_ptr<content::BluetoothScanningPrompt>
  27. BluetoothDelegateImpl::ShowBluetoothScanningPrompt(
  28. content::RenderFrameHost* frame,
  29. const content::BluetoothScanningPrompt::EventHandler& event_handler) {
  30. return client_->ShowBluetoothScanningPrompt(frame, event_handler);
  31. }
  32. void BluetoothDelegateImpl::ShowDevicePairPrompt(
  33. RenderFrameHost* frame,
  34. const std::u16string& device_identifier,
  35. PairPromptCallback callback,
  36. PairingKind pairing_kind,
  37. const absl::optional<std::u16string>& pin) {
  38. client_->ShowBluetoothDevicePairDialog(
  39. frame, device_identifier, std::move(callback), pairing_kind, pin);
  40. }
  41. WebBluetoothDeviceId BluetoothDelegateImpl::GetWebBluetoothDeviceId(
  42. RenderFrameHost* frame,
  43. const std::string& device_address) {
  44. return client_->GetBluetoothChooserContext(frame)->GetWebBluetoothDeviceId(
  45. frame->GetMainFrame()->GetLastCommittedOrigin(), device_address);
  46. }
  47. std::string BluetoothDelegateImpl::GetDeviceAddress(
  48. RenderFrameHost* frame,
  49. const WebBluetoothDeviceId& device_id) {
  50. return client_->GetBluetoothChooserContext(frame)->GetDeviceAddress(
  51. frame->GetMainFrame()->GetLastCommittedOrigin(), device_id);
  52. }
  53. WebBluetoothDeviceId BluetoothDelegateImpl::AddScannedDevice(
  54. RenderFrameHost* frame,
  55. const std::string& device_address) {
  56. return client_->GetBluetoothChooserContext(frame)->AddScannedDevice(
  57. frame->GetMainFrame()->GetLastCommittedOrigin(), device_address);
  58. }
  59. WebBluetoothDeviceId BluetoothDelegateImpl::GrantServiceAccessPermission(
  60. RenderFrameHost* frame,
  61. const device::BluetoothDevice* device,
  62. const blink::mojom::WebBluetoothRequestDeviceOptions* options) {
  63. return client_->GetBluetoothChooserContext(frame)
  64. ->GrantServiceAccessPermission(
  65. frame->GetMainFrame()->GetLastCommittedOrigin(), device, options);
  66. }
  67. bool BluetoothDelegateImpl::HasDevicePermission(
  68. RenderFrameHost* frame,
  69. const WebBluetoothDeviceId& device_id) {
  70. return client_->GetBluetoothChooserContext(frame)->HasDevicePermission(
  71. frame->GetMainFrame()->GetLastCommittedOrigin(), device_id);
  72. }
  73. void BluetoothDelegateImpl::RevokeDevicePermissionWebInitiated(
  74. RenderFrameHost* frame,
  75. const WebBluetoothDeviceId& device_id) {
  76. client_->GetBluetoothChooserContext(frame)
  77. ->RevokeDevicePermissionWebInitiated(
  78. frame->GetMainFrame()->GetLastCommittedOrigin(), device_id);
  79. }
  80. bool BluetoothDelegateImpl::IsAllowedToAccessService(
  81. RenderFrameHost* frame,
  82. const WebBluetoothDeviceId& device_id,
  83. const BluetoothUUID& service) {
  84. return client_->GetBluetoothChooserContext(frame)->IsAllowedToAccessService(
  85. frame->GetMainFrame()->GetLastCommittedOrigin(), device_id, service);
  86. }
  87. bool BluetoothDelegateImpl::IsAllowedToAccessAtLeastOneService(
  88. RenderFrameHost* frame,
  89. const WebBluetoothDeviceId& device_id) {
  90. return client_->GetBluetoothChooserContext(frame)
  91. ->IsAllowedToAccessAtLeastOneService(
  92. frame->GetMainFrame()->GetLastCommittedOrigin(), device_id);
  93. }
  94. bool BluetoothDelegateImpl::IsAllowedToAccessManufacturerData(
  95. RenderFrameHost* frame,
  96. const WebBluetoothDeviceId& device_id,
  97. uint16_t manufacturer_code) {
  98. return client_->GetBluetoothChooserContext(frame)
  99. ->IsAllowedToAccessManufacturerData(
  100. frame->GetMainFrame()->GetLastCommittedOrigin(), device_id,
  101. manufacturer_code);
  102. }
  103. void BluetoothDelegateImpl::AddFramePermissionObserver(
  104. FramePermissionObserver* observer) {
  105. std::unique_ptr<ChooserContextPermissionObserver>& chooser_observer =
  106. chooser_observers_[observer->GetRenderFrameHost()];
  107. if (!chooser_observer) {
  108. chooser_observer = std::make_unique<ChooserContextPermissionObserver>(
  109. this,
  110. client_->GetBluetoothChooserContext(observer->GetRenderFrameHost()));
  111. }
  112. chooser_observer->AddFramePermissionObserver(observer);
  113. }
  114. void BluetoothDelegateImpl::RemoveFramePermissionObserver(
  115. FramePermissionObserver* observer) {
  116. auto it = chooser_observers_.find(observer->GetRenderFrameHost());
  117. if (it == chooser_observers_.end())
  118. return;
  119. it->second->RemoveFramePermissionObserver(observer);
  120. }
  121. std::vector<blink::mojom::WebBluetoothDevicePtr>
  122. BluetoothDelegateImpl::GetPermittedDevices(content::RenderFrameHost* frame) {
  123. auto* context = client_->GetBluetoothChooserContext(frame);
  124. std::vector<std::unique_ptr<ObjectPermissionContextBase::Object>> objects =
  125. context->GetGrantedObjects(
  126. frame->GetMainFrame()->GetLastCommittedOrigin());
  127. std::vector<blink::mojom::WebBluetoothDevicePtr> permitted_devices;
  128. for (const auto& object : objects) {
  129. auto permitted_device = blink::mojom::WebBluetoothDevice::New();
  130. permitted_device->id =
  131. BluetoothChooserContext::GetObjectDeviceId(object->value);
  132. permitted_device->name =
  133. base::UTF16ToUTF8(context->GetObjectDisplayName(object->value));
  134. permitted_devices.push_back(std::move(permitted_device));
  135. }
  136. return permitted_devices;
  137. }
  138. BluetoothDelegateImpl::ChooserContextPermissionObserver::
  139. ChooserContextPermissionObserver(BluetoothDelegateImpl* owning_delegate,
  140. ObjectPermissionContextBase* context)
  141. : owning_delegate_(owning_delegate) {
  142. observer_.Observe(context);
  143. }
  144. BluetoothDelegateImpl::ChooserContextPermissionObserver::
  145. ~ChooserContextPermissionObserver() = default;
  146. void BluetoothDelegateImpl::ChooserContextPermissionObserver::
  147. OnPermissionRevoked(const url::Origin& origin) {
  148. observers_pending_removal_.clear();
  149. is_traversing_observers_ = true;
  150. for (auto& observer : observer_list_)
  151. observer.OnPermissionRevoked(origin);
  152. is_traversing_observers_ = false;
  153. for (FramePermissionObserver* observer : observers_pending_removal_)
  154. RemoveFramePermissionObserver(observer);
  155. }
  156. void BluetoothDelegateImpl::ChooserContextPermissionObserver::
  157. AddFramePermissionObserver(FramePermissionObserver* observer) {
  158. observer_list_.AddObserver(observer);
  159. }
  160. void BluetoothDelegateImpl::ChooserContextPermissionObserver::
  161. RemoveFramePermissionObserver(FramePermissionObserver* observer) {
  162. if (is_traversing_observers_) {
  163. observers_pending_removal_.emplace_back(observer);
  164. return;
  165. }
  166. observer_list_.RemoveObserver(observer);
  167. if (observer_list_.empty())
  168. owning_delegate_->chooser_observers_.erase(observer->GetRenderFrameHost());
  169. // Previous call destructed this instance. Don't add code after this.
  170. }
  171. } // namespace permissions