Commit f3e98a24 authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Merge NavigatorBluetooth and Bluetooth

Change-Id: I47f74861053c14ddb9cc78ddfd360048968390e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2523065
Commit-Queue: Nate Chapin <japhet@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Auto-Submit: Nate Chapin <japhet@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825648}
parent 12f51903
...@@ -36,8 +36,6 @@ blink_modules_sources("bluetooth") { ...@@ -36,8 +36,6 @@ blink_modules_sources("bluetooth") {
"bluetooth_service_data_map.h", "bluetooth_service_data_map.h",
"bluetooth_uuid.cc", "bluetooth_uuid.cc",
"bluetooth_uuid.h", "bluetooth_uuid.h",
"navigator_bluetooth.cc",
"navigator_bluetooth.h",
] ]
deps = [ deps = [
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "third_party/blink/renderer/core/frame/frame.h" #include "third_party/blink/renderer/core/frame/frame.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h" #include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h" #include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/core/inspector/console_message.h" #include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/modules/bluetooth/bluetooth_device.h" #include "third_party/blink/renderer/modules/bluetooth/bluetooth_device.h"
#include "third_party/blink/renderer/modules/bluetooth/bluetooth_error.h" #include "third_party/blink/renderer/modules/bluetooth/bluetooth_error.h"
...@@ -172,13 +173,14 @@ static void ConvertRequestDeviceOptions( ...@@ -172,13 +173,14 @@ static void ConvertRequestDeviceOptions(
ScriptPromise Bluetooth::getAvailability(ScriptState* script_state, ScriptPromise Bluetooth::getAvailability(ScriptState* script_state,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (window_->IsContextDestroyed()) { LocalDOMWindow* window = GetSupplementable()->DomWindow();
if (!window) {
exception_state.ThrowTypeError(kInactiveDocumentError); exception_state.ThrowTypeError(kInactiveDocumentError);
return ScriptPromise(); return ScriptPromise();
} }
CHECK(window_->IsSecureContext()); CHECK(window->IsSecureContext());
EnsureServiceConnection(window_); EnsureServiceConnection(window);
// Subsequent steps are handled in the browser process. // Subsequent steps are handled in the browser process.
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state); auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
...@@ -227,15 +229,16 @@ void Bluetooth::RequestDeviceCallback( ...@@ -227,15 +229,16 @@ void Bluetooth::RequestDeviceCallback(
ScriptPromise Bluetooth::getDevices(ScriptState* script_state, ScriptPromise Bluetooth::getDevices(ScriptState* script_state,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (window_->IsContextDestroyed()) { LocalDOMWindow* window = GetSupplementable()->DomWindow();
if (!window) {
exception_state.ThrowTypeError(kInactiveDocumentError); exception_state.ThrowTypeError(kInactiveDocumentError);
return ScriptPromise(); return ScriptPromise();
} }
AddUnsupportedPlatformConsoleMessage(window_); AddUnsupportedPlatformConsoleMessage(window);
CHECK(window_->IsSecureContext()); CHECK(window->IsSecureContext());
EnsureServiceConnection(window_); EnsureServiceConnection(window);
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state); auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise promise = resolver->Promise(); ScriptPromise promise = resolver->Promise();
...@@ -249,24 +252,25 @@ ScriptPromise Bluetooth::getDevices(ScriptState* script_state, ...@@ -249,24 +252,25 @@ ScriptPromise Bluetooth::getDevices(ScriptState* script_state,
ScriptPromise Bluetooth::requestDevice(ScriptState* script_state, ScriptPromise Bluetooth::requestDevice(ScriptState* script_state,
const RequestDeviceOptions* options, const RequestDeviceOptions* options,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (window_->IsContextDestroyed()) { LocalDOMWindow* window = GetSupplementable()->DomWindow();
if (!window) {
exception_state.ThrowTypeError(kInactiveDocumentError); exception_state.ThrowTypeError(kInactiveDocumentError);
return ScriptPromise(); return ScriptPromise();
} }
AddUnsupportedPlatformConsoleMessage(window_); AddUnsupportedPlatformConsoleMessage(window);
CHECK(window_->IsSecureContext()); CHECK(window->IsSecureContext());
// If the algorithm is not allowed to show a popup, reject promise with a // If the algorithm is not allowed to show a popup, reject promise with a
// SecurityError and abort these steps. // SecurityError and abort these steps.
auto* frame = window_->GetFrame(); auto* frame = window->GetFrame();
DCHECK(frame); DCHECK(frame);
if (!LocalFrame::HasTransientUserActivation(frame)) { if (!LocalFrame::HasTransientUserActivation(frame)) {
exception_state.ThrowSecurityError(kHandleGestureForPermissionRequest); exception_state.ThrowSecurityError(kHandleGestureForPermissionRequest);
return ScriptPromise(); return ScriptPromise();
} }
EnsureServiceConnection(window_); EnsureServiceConnection(window);
// In order to convert the arguments from service names and aliases to just // In order to convert the arguments from service names and aliases to just
// UUIDs, do the following substeps: // UUIDs, do the following substeps:
...@@ -347,33 +351,35 @@ void Bluetooth::RequestScanningCallback( ...@@ -347,33 +351,35 @@ void Bluetooth::RequestScanningCallback(
ScriptPromise Bluetooth::requestLEScan(ScriptState* script_state, ScriptPromise Bluetooth::requestLEScan(ScriptState* script_state,
const BluetoothLEScanOptions* options, const BluetoothLEScanOptions* options,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (window_->IsContextDestroyed()) { LocalDOMWindow* window = GetSupplementable()->DomWindow();
if (!window) {
exception_state.ThrowTypeError(kInactiveDocumentError); exception_state.ThrowTypeError(kInactiveDocumentError);
return ScriptPromise(); return ScriptPromise();
} }
// Remind developers when they are using Web Bluetooth on unsupported // Remind developers when they are using Web Bluetooth on unsupported
// platforms. // platforms.
window_->AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>( window->AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>(
mojom::ConsoleMessageSource::kJavaScript, mojom::ConsoleMessageSource::kJavaScript,
mojom::ConsoleMessageLevel::kInfo, mojom::ConsoleMessageLevel::kInfo,
"Web Bluetooth Scanning is experimental on this platform. See " "Web Bluetooth Scanning is experimental on this platform. See "
"https://github.com/WebBluetoothCG/web-bluetooth/blob/gh-pages/" "https://github.com/WebBluetoothCG/web-bluetooth/blob/gh-pages/"
"implementation-status.md")); "implementation-status.md"));
CHECK(window_->IsSecureContext()); CHECK(window->IsSecureContext());
// If the algorithm is not allowed to show a popup, reject promise with a // If the algorithm is not allowed to show a popup, reject promise with a
// SecurityError and abort these steps. // SecurityError and abort these steps.
auto* frame = window_->GetFrame(); auto* frame = window->GetFrame();
// If !window_->IsContextDestroyed() then GetFrame() should be valid. // If Navigator::DomWindow() returned a non-null |window|, GetFrame() should
// be valid.
DCHECK(frame); DCHECK(frame);
if (!LocalFrame::HasTransientUserActivation(frame)) { if (!LocalFrame::HasTransientUserActivation(frame)) {
exception_state.ThrowSecurityError(kHandleGestureForPermissionRequest); exception_state.ThrowSecurityError(kHandleGestureForPermissionRequest);
return ScriptPromise(); return ScriptPromise();
} }
EnsureServiceConnection(window_); EnsureServiceConnection(window);
auto scan_options = mojom::blink::WebBluetoothRequestLEScanOptions::New(); auto scan_options = mojom::blink::WebBluetoothRequestLEScanOptions::New();
ConvertRequestLEScanOptions(options, scan_options, exception_state); ConvertRequestLEScanOptions(options, scan_options, exception_state);
...@@ -390,7 +396,7 @@ ScriptPromise Bluetooth::requestLEScan(ScriptState* script_state, ...@@ -390,7 +396,7 @@ ScriptPromise Bluetooth::requestLEScan(ScriptState* script_state,
// See https://bit.ly/2S0zRAS for task types. // See https://bit.ly/2S0zRAS for task types.
mojo::ReceiverId id = mojo::ReceiverId id =
client_receivers_.Add(client.InitWithNewEndpointAndPassReceiver(), client_receivers_.Add(client.InitWithNewEndpointAndPassReceiver(),
window_->GetTaskRunner(TaskType::kMiscPlatformAPI)); window->GetTaskRunner(TaskType::kMiscPlatformAPI));
auto scan_options_copy = scan_options->Clone(); auto scan_options_copy = scan_options->Clone();
service_->RequestScanningStart( service_->RequestScanningStart(
...@@ -406,7 +412,7 @@ void Bluetooth::AdvertisingEvent( ...@@ -406,7 +412,7 @@ void Bluetooth::AdvertisingEvent(
auto* event = MakeGarbageCollected<BluetoothAdvertisingEvent>( auto* event = MakeGarbageCollected<BluetoothAdvertisingEvent>(
event_type_names::kAdvertisementreceived, event_type_names::kAdvertisementreceived,
GetBluetoothDeviceRepresentingDevice(std::move(advertising_event->device), GetBluetoothDeviceRepresentingDevice(std::move(advertising_event->device),
window_), GetSupplementable()->DomWindow()),
std::move(advertising_event)); std::move(advertising_event));
DispatchEvent(*event); DispatchEvent(*event);
} }
...@@ -428,23 +434,38 @@ const WTF::AtomicString& Bluetooth::InterfaceName() const { ...@@ -428,23 +434,38 @@ const WTF::AtomicString& Bluetooth::InterfaceName() const {
} }
ExecutionContext* Bluetooth::GetExecutionContext() const { ExecutionContext* Bluetooth::GetExecutionContext() const {
return window_; return GetSupplementable()->DomWindow();
} }
void Bluetooth::Trace(Visitor* visitor) const { void Bluetooth::Trace(Visitor* visitor) const {
visitor->Trace(device_instance_map_); visitor->Trace(device_instance_map_);
visitor->Trace(window_);
visitor->Trace(client_receivers_); visitor->Trace(client_receivers_);
visitor->Trace(service_); visitor->Trace(service_);
EventTargetWithInlineData::Trace(visitor); EventTargetWithInlineData::Trace(visitor);
Supplement<Navigator>::Trace(visitor);
PageVisibilityObserver::Trace(visitor); PageVisibilityObserver::Trace(visitor);
} }
Bluetooth::Bluetooth(LocalDOMWindow* dom_window) // static
: PageVisibilityObserver(dom_window->GetFrame()->GetPage()), const char Bluetooth::kSupplementName[] = "Bluetooth";
window_(dom_window),
client_receivers_(this, dom_window), Bluetooth* Bluetooth::bluetooth(Navigator& navigator) {
service_(dom_window) {} if (!navigator.DomWindow())
return nullptr;
Bluetooth* supplement = Supplement<Navigator>::From<Bluetooth>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<Bluetooth>(navigator);
ProvideTo(navigator, supplement);
}
return supplement;
}
Bluetooth::Bluetooth(Navigator& navigator)
: Supplement<Navigator>(navigator),
PageVisibilityObserver(navigator.DomWindow()->GetFrame()->GetPage()),
client_receivers_(this, navigator.DomWindow()),
service_(navigator.DomWindow()) {}
Bluetooth::~Bluetooth() { Bluetooth::~Bluetooth() {
DCHECK(client_receivers_.empty()); DCHECK(client_receivers_.empty());
......
...@@ -15,25 +15,33 @@ ...@@ -15,25 +15,33 @@
#include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver_set.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver_set.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink { namespace blink {
class BluetoothLEScanOptions; class BluetoothLEScanOptions;
class ExceptionState; class ExceptionState;
class RequestDeviceOptions; class RequestDeviceOptions;
class Navigator;
class ScriptPromise; class ScriptPromise;
class ScriptState; class ScriptState;
class Bluetooth final : public EventTargetWithInlineData, class Bluetooth final : public EventTargetWithInlineData,
public Supplement<Navigator>,
public PageVisibilityObserver, public PageVisibilityObserver,
public mojom::blink::WebBluetoothAdvertisementClient { public mojom::blink::WebBluetoothAdvertisementClient {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
public: public:
explicit Bluetooth(LocalDOMWindow*); static const char kSupplementName[];
// IDL exposed as navigator.bluetooth
static Bluetooth* bluetooth(Navigator&);
explicit Bluetooth(Navigator&);
~Bluetooth() override; ~Bluetooth() override;
// IDL exposed interface: // IDL exposed bluetooth interface:
ScriptPromise getAvailability(ScriptState*, ExceptionState&); ScriptPromise getAvailability(ScriptState*, ExceptionState&);
ScriptPromise getDevices(ScriptState*, ExceptionState&); ScriptPromise getDevices(ScriptState*, ExceptionState&);
ScriptPromise requestDevice(ScriptState*, ScriptPromise requestDevice(ScriptState*,
...@@ -92,8 +100,6 @@ class Bluetooth final : public EventTargetWithInlineData, ...@@ -92,8 +100,6 @@ class Bluetooth final : public EventTargetWithInlineData,
// Bluetooth device inside a single global object. // Bluetooth device inside a single global object.
HeapHashMap<String, Member<BluetoothDevice>> device_instance_map_; HeapHashMap<String, Member<BluetoothDevice>> device_instance_map_;
Member<LocalDOMWindow> window_;
HeapMojoAssociatedReceiverSet<mojom::blink::WebBluetoothAdvertisementClient, HeapMojoAssociatedReceiverSet<mojom::blink::WebBluetoothAdvertisementClient,
Bluetooth> Bluetooth>
client_receivers_; client_receivers_;
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/bluetooth/navigator_bluetooth.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/modules/bluetooth/bluetooth.h"
namespace blink {
NavigatorBluetooth& NavigatorBluetooth::From(Navigator& navigator) {
NavigatorBluetooth* supplement =
Supplement<Navigator>::From<NavigatorBluetooth>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<NavigatorBluetooth>(navigator);
ProvideTo(navigator, supplement);
}
return *supplement;
}
Bluetooth* NavigatorBluetooth::bluetooth(Navigator& navigator) {
return NavigatorBluetooth::From(navigator).bluetooth();
}
Bluetooth* NavigatorBluetooth::bluetooth() {
if (bluetooth_)
return bluetooth_.Get();
if (!GetSupplementable()->DomWindow())
return nullptr;
bluetooth_ =
MakeGarbageCollected<Bluetooth>(GetSupplementable()->DomWindow());
return bluetooth_.Get();
}
void NavigatorBluetooth::Trace(Visitor* visitor) const {
visitor->Trace(bluetooth_);
Supplement<Navigator>::Trace(visitor);
}
NavigatorBluetooth::NavigatorBluetooth(Navigator& navigator)
: Supplement<Navigator>(navigator) {}
// static
const char NavigatorBluetooth::kSupplementName[] = "NavigatorBluetooth";
} // namespace blink
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_NAVIGATOR_BLUETOOTH_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_NAVIGATOR_BLUETOOTH_H_
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink {
class Bluetooth;
class Navigator;
class NavigatorBluetooth final : public GarbageCollected<NavigatorBluetooth>,
public Supplement<Navigator> {
public:
static const char kSupplementName[];
// Gets, or creates, NavigatorBluetooth supplement on Navigator.
// See platform/Supplementable.h
static NavigatorBluetooth& From(Navigator&);
static Bluetooth* bluetooth(Navigator&);
// IDL exposed interface:
Bluetooth* bluetooth();
explicit NavigatorBluetooth(Navigator&);
void Trace(Visitor*) const override;
private:
Member<Bluetooth> bluetooth_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_NAVIGATOR_BLUETOOTH_H_
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
// https://webbluetoothcg.github.io/web-bluetooth/#navigator-extensions // https://webbluetoothcg.github.io/web-bluetooth/#navigator-extensions
[ [
ImplementedAs=NavigatorBluetooth, ImplementedAs=Bluetooth,
RuntimeEnabled=WebBluetooth, RuntimeEnabled=WebBluetooth,
SecureContext SecureContext
] partial interface Navigator { ] partial interface Navigator {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment