Commit 9909e7bc authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Merge NavigatorHID and HID

HID becomes a Supplement<Navigator> instead of an
ExecutionContextClient. This means it retains a references
to the Navigator, which is itself an ExecutionContextClient,
so it still has access to the ExecutionContext.

Change-Id: Ie1d789fb6588c816f39c5b0645d1973aee43caef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2495503
Commit-Queue: Nate Chapin <japhet@chromium.org>
Auto-Submit: Nate Chapin <japhet@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820450}
parent 27a5509e
......@@ -14,8 +14,6 @@ blink_modules_sources("hid") {
"hid_device.h",
"hid_input_report_event.cc",
"hid_input_report_event.h",
"navigator_hid.cc",
"navigator_hid.h",
]
}
......
......@@ -15,7 +15,9 @@
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.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/navigator.h"
#include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/modules/hid/hid_connection_event.h"
#include "third_party/blink/renderer/modules/hid/hid_device.h"
......@@ -82,12 +84,27 @@ mojom::blink::HidDeviceFilterPtr ConvertDeviceFilter(
} // namespace
HID::HID(ExecutionContext& context)
: ExecutionContextClient(&context),
service_(&context),
feature_handle_for_scheduler_(context.GetScheduler()->RegisterFeature(
SchedulingPolicy::Feature::kWebHID,
{SchedulingPolicy::RecordMetricsForBackForwardCache()})) {}
const char HID::kSupplementName[] = "HID";
HID* HID::hid(Navigator& navigator) {
if (!navigator.DomWindow())
return nullptr;
HID* hid = Supplement<Navigator>::From<HID>(navigator);
if (!hid) {
hid = MakeGarbageCollected<HID>(navigator);
ProvideTo(navigator, hid);
}
return hid;
}
HID::HID(Navigator& navigator)
: Supplement<Navigator>(navigator),
service_(navigator.DomWindow()),
feature_handle_for_scheduler_(
navigator.DomWindow()->GetScheduler()->RegisterFeature(
SchedulingPolicy::Feature::kWebHID,
{SchedulingPolicy::RecordMetricsForBackForwardCache()})) {}
HID::~HID() {
DCHECK(get_devices_promises_.IsEmpty());
......@@ -95,7 +112,7 @@ HID::~HID() {
}
ExecutionContext* HID::GetExecutionContext() const {
return ExecutionContextClient::GetExecutionContext();
return GetSupplementable()->DomWindow();
}
const AtomicString& HID::InterfaceName() const {
......@@ -164,8 +181,7 @@ ScriptPromise HID::getDevices(ScriptState* script_state,
ScriptPromise HID::requestDevice(ScriptState* script_state,
const HIDDeviceRequestOptions* options,
ExceptionState& exception_state) {
auto* frame = GetFrame();
if (!frame || !frame->GetDocument()) {
if (!GetExecutionContext()) {
exception_state.ThrowDOMException(DOMExceptionCode::kNotSupportedError,
kContextGone);
return ScriptPromise();
......@@ -178,7 +194,8 @@ ScriptPromise HID::requestDevice(ScriptState* script_state,
return ScriptPromise();
}
if (!LocalFrame::HasTransientUserActivation(frame)) {
if (!LocalFrame::HasTransientUserActivation(
GetSupplementable()->DomWindow()->GetFrame())) {
exception_state.ThrowSecurityError(
"Must be handling a user gesture to show a permission request.");
return ScriptPromise();
......@@ -289,7 +306,7 @@ void HID::Trace(Visitor* visitor) const {
visitor->Trace(request_device_promises_);
visitor->Trace(device_cache_);
EventTargetWithInlineData::Trace(visitor);
ExecutionContextClient::Trace(visitor);
Supplement<Navigator>::Trace(visitor);
}
} // namespace blink
......@@ -10,28 +10,34 @@
#include "third_party/blink/public/mojom/hid/hid.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/dom/events/event_target.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/handle.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/scheduler/public/frame_or_worker_scheduler.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink {
class ExecutionContext;
class HIDDevice;
class HIDDeviceRequestOptions;
class Navigator;
class ScriptPromiseResolver;
class ScriptState;
class HID : public EventTargetWithInlineData,
public ExecutionContextClient,
public Supplement<Navigator>,
public device::mojom::blink::HidManagerClient {
DEFINE_WRAPPERTYPEINFO();
public:
explicit HID(ExecutionContext& context);
static const char kSupplementName[];
// Web-exposed getter for navigator.hid
static HID* hid(Navigator&);
explicit HID(Navigator&);
~HID() override;
// EventTarget:
......@@ -43,7 +49,7 @@ class HID : public EventTargetWithInlineData,
void DeviceRemoved(
device::mojom::blink::HidDeviceInfoPtr device_info) override;
// Web-exposed interfaces:
// Web-exposed interfaces on hid object:
DEFINE_ATTRIBUTE_EVENT_LISTENER(connect, kConnect)
DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect, kDisconnect)
ScriptPromise getDevices(ScriptState*, ExceptionState&);
......
// Copyright 2019 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/hid/navigator_hid.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/navigator.h"
#include "third_party/blink/renderer/modules/hid/hid.h"
namespace blink {
NavigatorHID& NavigatorHID::From(Navigator& navigator) {
NavigatorHID* supplement =
Supplement<Navigator>::From<NavigatorHID>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<NavigatorHID>(navigator);
ProvideTo(navigator, supplement);
}
return *supplement;
}
HID* NavigatorHID::hid(Navigator& navigator) {
return NavigatorHID::From(navigator).hid();
}
HID* NavigatorHID::hid() {
return hid_;
}
void NavigatorHID::Trace(Visitor* visitor) const {
visitor->Trace(hid_);
Supplement<Navigator>::Trace(visitor);
}
NavigatorHID::NavigatorHID(Navigator& navigator) {
if (navigator.GetFrame()) {
DCHECK(navigator.GetFrame()->DomWindow());
hid_ = MakeGarbageCollected<HID>(*navigator.GetFrame()->DomWindow());
}
}
const char NavigatorHID::kSupplementName[] = "NavigatorHID";
} // namespace blink
// Copyright 2019 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_HID_NAVIGATOR_HID_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_HID_NAVIGATOR_HID_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 Navigator;
class HID;
class NavigatorHID final : public GarbageCollected<NavigatorHID>,
public Supplement<Navigator> {
public:
static const char kSupplementName[];
// Gets, or creates, NavigatorHID supplement on Navigator.
// See platform/Supplementable.h
static NavigatorHID& From(Navigator&);
static HID* hid(Navigator&);
HID* hid();
void Trace(Visitor*) const override;
explicit NavigatorHID(Navigator&);
private:
Member<HID> hid_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_HID_NAVIGATOR_HID_H_
......@@ -7,7 +7,7 @@
// https://wicg.github.io/webhid/index.html#enumeration
[
ImplementedAs=NavigatorHID,
ImplementedAs=HID,
RuntimeEnabled=WebHID,
SecureContext
] 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