Commit 632ef553 authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Make Serial a Supplement<NavigatorBase>

NavigatorBase is a newly-added base class for Navigator and
WorkerNavigator. Making Serial a Supplement of NavigatorBase
allows a single class to handle all of the work of exposing the
navigator.serial getter for both windows and workers, and
greatly reduces boilerplate.

Bug: 1147612
Change-Id: If8bb50bdb9729b106be20f0ee228233560f09511
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2537920
Commit-Queue: Nate Chapin <japhet@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Auto-Submit: Nate Chapin <japhet@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827501}
parent 29b8f25d
...@@ -10,8 +10,6 @@ assert(!is_android) ...@@ -10,8 +10,6 @@ assert(!is_android)
blink_modules_sources("serial") { blink_modules_sources("serial") {
sources = [ sources = [
"navigator_serial.cc",
"navigator_serial.h",
"serial.cc", "serial.cc",
"serial.h", "serial.h",
"serial_connection_event.cc", "serial_connection_event.cc",
...@@ -22,7 +20,5 @@ blink_modules_sources("serial") { ...@@ -22,7 +20,5 @@ blink_modules_sources("serial") {
"serial_port_underlying_sink.h", "serial_port_underlying_sink.h",
"serial_port_underlying_source.cc", "serial_port_underlying_source.cc",
"serial_port_underlying_source.h", "serial_port_underlying_source.h",
"worker_navigator_serial.cc",
"worker_navigator_serial.h",
] ]
} }
// Copyright 2018 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/serial/navigator_serial.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/modules/serial/serial.h"
namespace blink {
const char NavigatorSerial::kSupplementName[] = "NavigatorSerial";
NavigatorSerial& NavigatorSerial::From(Navigator& navigator) {
NavigatorSerial* supplement =
Supplement<Navigator>::From<NavigatorSerial>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<NavigatorSerial>(navigator);
ProvideTo(navigator, supplement);
}
return *supplement;
}
Serial* NavigatorSerial::serial(Navigator& navigator) {
return NavigatorSerial::From(navigator).serial();
}
void NavigatorSerial::Trace(Visitor* visitor) const {
visitor->Trace(serial_);
Supplement<Navigator>::Trace(visitor);
}
NavigatorSerial::NavigatorSerial(Navigator& navigator)
: Supplement<Navigator>(navigator) {
if (navigator.DomWindow()) {
serial_ = MakeGarbageCollected<Serial>(*navigator.DomWindow());
}
}
} // namespace blink
// Copyright 2018 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_SERIAL_NAVIGATOR_SERIAL_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_NAVIGATOR_SERIAL_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 Serial;
class NavigatorSerial final : public GarbageCollected<NavigatorSerial>,
public Supplement<Navigator> {
public:
static const char kSupplementName[];
static NavigatorSerial& From(Navigator&);
static Serial* serial(Navigator&);
Serial* serial() { return serial_; }
explicit NavigatorSerial(Navigator&);
void Trace(Visitor*) const override;
private:
Member<Serial> serial_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_NAVIGATOR_SERIAL_H_
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
// https://wicg.github.io/serial // https://wicg.github.io/serial
[ [
ImplementedAs=NavigatorSerial, ImplementedAs=Serial,
RuntimeEnabled=Serial, RuntimeEnabled=Serial,
SecureContext SecureContext
] partial interface Navigator { ] partial interface Navigator {
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "third_party/blink/renderer/core/dom/document.h" #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/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/execution_context/navigator_base.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/modules/event_target_modules_names.h" #include "third_party/blink/renderer/modules/event_target_modules_names.h"
#include "third_party/blink/renderer/modules/serial/serial_connection_event.h" #include "third_party/blink/renderer/modules/serial/serial_connection_event.h"
...@@ -41,10 +42,30 @@ String TokenToString(const base::UnguessableToken& token) { ...@@ -41,10 +42,30 @@ String TokenToString(const base::UnguessableToken& token) {
} // namespace } // namespace
Serial::Serial(ExecutionContext& execution_context) const char Serial::kSupplementName[] = "Serial";
: ExecutionContextLifecycleObserver(&execution_context),
service_(&execution_context), Serial* Serial::serial(NavigatorBase& navigator) {
receiver_(this, &execution_context) {} ExecutionContext* context = navigator.GetExecutionContext();
if (!context ||
(!context->IsWindow() && !context->IsDedicatedWorkerGlobalScope())) {
// TODO(https://crbug.com/839117): Remove this check once the Exposed
// attribute is fixed to only expose this property in dedicated workers.
return nullptr;
}
Serial* serial = Supplement<NavigatorBase>::From<Serial>(navigator);
if (!serial) {
serial = MakeGarbageCollected<Serial>(navigator);
ProvideTo(navigator, serial);
}
return serial;
}
Serial::Serial(NavigatorBase& navigator)
: Supplement<NavigatorBase>(navigator),
ExecutionContextLifecycleObserver(navigator.GetExecutionContext()),
service_(navigator.GetExecutionContext()),
receiver_(this, navigator.GetExecutionContext()) {}
ExecutionContext* Serial::GetExecutionContext() const { ExecutionContext* Serial::GetExecutionContext() const {
return ExecutionContextLifecycleObserver::GetExecutionContext(); return ExecutionContextLifecycleObserver::GetExecutionContext();
...@@ -173,6 +194,7 @@ void Serial::Trace(Visitor* visitor) const { ...@@ -173,6 +194,7 @@ void Serial::Trace(Visitor* visitor) const {
visitor->Trace(request_port_promises_); visitor->Trace(request_port_promises_);
visitor->Trace(port_cache_); visitor->Trace(port_cache_);
EventTargetWithInlineData::Trace(visitor); EventTargetWithInlineData::Trace(visitor);
Supplement<NavigatorBase>::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor); ExecutionContextLifecycleObserver::Trace(visitor);
} }
......
...@@ -15,23 +15,31 @@ ...@@ -15,23 +15,31 @@
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.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"
#include "third_party/blink/renderer/platform/wtf/functional.h" #include "third_party/blink/renderer/platform/wtf/functional.h"
namespace blink { namespace blink {
class ExecutionContext; class ExecutionContext;
class NavigatorBase;
class ScriptPromiseResolver; class ScriptPromiseResolver;
class ScriptState; class ScriptState;
class SerialPort; class SerialPort;
class SerialPortRequestOptions; class SerialPortRequestOptions;
class Serial final : public EventTargetWithInlineData, class Serial final : public EventTargetWithInlineData,
public Supplement<NavigatorBase>,
public ExecutionContextLifecycleObserver, public ExecutionContextLifecycleObserver,
public mojom::blink::SerialServiceClient { public mojom::blink::SerialServiceClient {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
public: public:
explicit Serial(ExecutionContext&); static const char kSupplementName[];
// Web-exposed navigator.serial
static Serial* serial(NavigatorBase&);
explicit Serial(NavigatorBase&);
// EventTarget // EventTarget
ExecutionContext* GetExecutionContext() const override; ExecutionContext* GetExecutionContext() const override;
......
// Copyright 2018 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/serial/worker_navigator_serial.h"
#include "third_party/blink/renderer/modules/serial/serial.h"
namespace blink {
const char WorkerNavigatorSerial::kSupplementName[] = "WorkerNavigatorSerial";
WorkerNavigatorSerial& WorkerNavigatorSerial::From(WorkerNavigator& navigator) {
WorkerNavigatorSerial* supplement =
Supplement<WorkerNavigator>::From<WorkerNavigatorSerial>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<WorkerNavigatorSerial>(navigator);
ProvideTo(navigator, supplement);
}
return *supplement;
}
Serial* WorkerNavigatorSerial::serial(ScriptState* script_state,
WorkerNavigator& navigator) {
return WorkerNavigatorSerial::From(navigator).serial(script_state);
}
Serial* WorkerNavigatorSerial::serial(ScriptState* script_state) {
if (!serial_) {
auto* execution_context = ExecutionContext::From(script_state);
DCHECK(execution_context);
// TODO(https://crbug.com/839117): Remove this check once the Exposed
// attribute is fixed to only expose this property in dedicated workers.
if (execution_context->IsDedicatedWorkerGlobalScope())
serial_ = MakeGarbageCollected<Serial>(*execution_context);
}
return serial_;
}
void WorkerNavigatorSerial::Trace(Visitor* visitor) const {
visitor->Trace(serial_);
Supplement<WorkerNavigator>::Trace(visitor);
}
WorkerNavigatorSerial::WorkerNavigatorSerial(WorkerNavigator& navigator)
: Supplement<WorkerNavigator>(navigator) {}
} // namespace blink
// Copyright 2018 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_SERIAL_WORKER_NAVIGATOR_SERIAL_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_WORKER_NAVIGATOR_SERIAL_H_
#include "third_party/blink/renderer/core/workers/worker_navigator.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink {
class ScriptState;
class Serial;
class WorkerNavigatorSerial final
: public GarbageCollected<WorkerNavigatorSerial>,
public Supplement<WorkerNavigator> {
public:
static const char kSupplementName[];
static WorkerNavigatorSerial& From(WorkerNavigator&);
static Serial* serial(ScriptState*, WorkerNavigator&);
Serial* serial(ScriptState*);
explicit WorkerNavigatorSerial(WorkerNavigator&);
void Trace(Visitor*) const override;
private:
Member<Serial> serial_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_WORKER_NAVIGATOR_SERIAL_H_
...@@ -9,9 +9,9 @@ ...@@ -9,9 +9,9 @@
// interface declaration once it's supported. // interface declaration once it's supported.
// //
// Exposed=DedicatedWorker, // Exposed=DedicatedWorker,
ImplementedAs=WorkerNavigatorSerial, ImplementedAs=Serial,
RuntimeEnabled=Serial, RuntimeEnabled=Serial,
SecureContext SecureContext
] partial interface WorkerNavigator { ] partial interface WorkerNavigator {
[CallWith=ScriptState, SameObject] readonly attribute Serial serial; [SameObject] readonly attribute Serial serial;
}; };
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