Commit 062cc441 authored by Yutaka Hirano's avatar Yutaka Hirano Committed by Commit Bot

Introduce UnderlyingSinkBase

As a preliminary step to have a platform provided underlying sink, this
CL defines UnderlyingSinkBase. UnderlyingSinkBase is much simpler than
UnderlyingSource because we don't need extra logic that was needed for
lifetime issues.

Bug: None
Change-Id: Ibcb5ccd364627df553cdd2be1c9dc1c1fa45a685
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1564278
Commit-Queue: Yutaka Hirano <yhirano@chromium.org>
Reviewed-by: default avatarAdam Rice <ricea@chromium.org>
Cr-Commit-Position: refs/heads/master@{#650476}
parent ef5d1016
......@@ -335,6 +335,7 @@ core_idl_files =
"streams/writable_stream_default_controller.idl",
"streams/writable_stream_default_writer.idl",
"streams/underlying_source_base.idl",
"streams/underlying_sink_base.idl",
"svg/svg_a_element.idl",
"svg/svg_angle.idl",
"svg/svg_animate_element.idl",
......
......@@ -37,12 +37,15 @@ blink_core_sources("streams") {
"transform_stream_transformer.h",
"transform_stream_wrapper.cc",
"transform_stream_wrapper.h",
"underlying_sink_base.h",
"underlying_source_base.cc",
"underlying_source_base.h",
"writable_stream.cc",
"writable_stream.h",
"writable_stream_default_controller.cc",
"writable_stream_default_controller.h",
"writable_stream_default_controller_interface.cc",
"writable_stream_default_controller_interface.h",
"writable_stream_default_writer.cc",
"writable_stream_default_writer.h",
"writable_stream_native.cc",
......
// 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_CORE_STREAMS_UNDERLYING_SINK_BASE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_UNDERLYING_SINK_BASE_H_
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/streams/writable_stream_default_controller_interface.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/visitor.h"
namespace blink {
class ScriptPromise;
class ScriptValue;
class ScriptState;
class CORE_EXPORT UnderlyingSinkBase : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
public:
~UnderlyingSinkBase() override = default;
// We define non-virtual |start| and |write| which take ScriptValue for
// |controller| and are called from IDL. Also we define virtual |start| and
// |write| which take WritableStreamDefaultControllerInterface. This is needed
// because we have two streams implementations.
virtual ScriptPromise start(ScriptState*,
WritableStreamDefaultControllerInterface*) = 0;
virtual ScriptPromise write(ScriptState*,
ScriptValue chunk,
WritableStreamDefaultControllerInterface*) = 0;
virtual ScriptPromise close(ScriptState*) = 0;
virtual ScriptPromise abort(ScriptState*, ScriptValue reason) = 0;
ScriptPromise start(ScriptState* script_state, ScriptValue controller) {
controller_ = WritableStreamDefaultControllerInterface::Create(controller);
return start(script_state, controller_);
}
ScriptPromise write(ScriptState* script_state,
ScriptValue chunk,
ScriptValue controller) {
DCHECK(controller_);
return write(script_state, chunk, controller_);
}
void Trace(Visitor* visitor) override {
visitor->Trace(controller_);
ScriptWrappable::Trace(visitor);
}
private:
Member<WritableStreamDefaultControllerInterface> controller_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_UNDERLYING_SINK_BASE_H_
// 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.
// This is not a spec interface. Instead, it is used to generate bindings
// so that Blink code which wishes to create a stream can derive from
// UnderlyingSinkBase, and a JavaScript object can then be generated
// automatically for use in initializing a WritableStream.
[
NoInterfaceObject
]
interface UnderlyingSinkBase {
[CallWith=ScriptState] Promise<void> start(any controller);
[CallWith=ScriptState] Promise<void> write(any chunk, any controller);
[CallWith=ScriptState] Promise<void> close();
[CallWith=ScriptState] Promise<void> abort(any reason);
};
......@@ -86,6 +86,12 @@ class WritableStreamDefaultController final : public ScriptWrappable {
v8::Local<v8::Value> chunk,
double chunk_size);
// https://streams.spec.whatwg.org/#writable-stream-default-controller-error
// TODO(yhirano): Make this private once we ship StreamsNative.
static void Error(ScriptState*,
WritableStreamDefaultController*,
v8::Local<v8::Value> error);
// Exposed to WritableStreamNative. Not part of the standard.
bool Started() const { return started_; }
......@@ -115,11 +121,6 @@ class WritableStreamDefaultController final : public ScriptWrappable {
// https://streams.spec.whatwg.org/#writable-stream-default-controller-get-backpressure
static bool GetBackpressure(const WritableStreamDefaultController*);
// https://streams.spec.whatwg.org/#writable-stream-default-controller-error
static void Error(ScriptState*,
WritableStreamDefaultController*,
v8::Local<v8::Value> error);
// Most member variables correspond 1:1 with the internal slots in the
// standard. See
// https://streams.spec.whatwg.org/#ws-default-controller-internal-slots.
......
// 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/core/streams/writable_stream_default_controller_interface.h"
#include "base/optional.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_script_runner.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_writable_stream_default_controller.h"
#include "third_party/blink/renderer/core/streams/writable_stream_default_controller.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_v8_reference.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
namespace blink {
namespace {
class WritableStreamDefaultControllerWrapper final
: public WritableStreamDefaultControllerInterface {
public:
explicit WritableStreamDefaultControllerWrapper(ScriptValue controller)
: controller_(controller.GetIsolate(), controller.V8Value()) {}
void Error(ScriptState* script_state,
v8::Local<v8::Value> js_error) override {
v8::Isolate* isolate = script_state->GetIsolate();
v8::Local<v8::Value> controller = controller_.NewLocal(isolate);
v8::Local<v8::Value> args[] = {controller, js_error};
v8::MaybeLocal<v8::Value> result = V8ScriptRunner::CallExtra(
script_state, "WritableStreamDefaultControllerError", args);
result.ToLocalChecked();
}
void Trace(blink::Visitor* visitor) override {
visitor->Trace(controller_);
WritableStreamDefaultControllerInterface::Trace(visitor);
}
private:
TraceWrapperV8Reference<v8::Value> controller_;
};
class WritableStreamDefaultControllerNative final
: public WritableStreamDefaultControllerInterface {
public:
explicit WritableStreamDefaultControllerNative(ScriptValue controller) {
DCHECK(RuntimeEnabledFeatures::StreamsNativeEnabled());
DCHECK(controller.IsObject());
controller_ = V8WritableStreamDefaultController::ToImpl(
controller.V8Value().As<v8::Object>());
DCHECK(controller_);
}
void Error(ScriptState* script_state, v8::Local<v8::Value> error) override {
WritableStreamDefaultController::Error(script_state, controller_, error);
}
void Trace(Visitor* visitor) override {
visitor->Trace(controller_);
WritableStreamDefaultControllerInterface::Trace(visitor);
}
private:
Member<WritableStreamDefaultController> controller_;
};
} // namespace
WritableStreamDefaultControllerInterface::
WritableStreamDefaultControllerInterface() = default;
WritableStreamDefaultControllerInterface::
~WritableStreamDefaultControllerInterface() = default;
WritableStreamDefaultControllerInterface*
WritableStreamDefaultControllerInterface::Create(ScriptValue controller) {
if (RuntimeEnabledFeatures::StreamsNativeEnabled()) {
return MakeGarbageCollected<WritableStreamDefaultControllerNative>(
controller);
}
return MakeGarbageCollected<WritableStreamDefaultControllerWrapper>(
controller);
}
} // 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_CORE_STREAMS_WRITABLE_STREAM_DEFAULT_CONTROLLER_INTERFACE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_WRITABLE_STREAM_DEFAULT_CONTROLLER_INTERFACE_H_
#include "third_party/blink/renderer/bindings/core/v8/to_v8_for_core.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "v8/include/v8.h"
namespace blink {
class Visitor;
class CORE_EXPORT WritableStreamDefaultControllerInterface
: public GarbageCollectedFinalized<
WritableStreamDefaultControllerInterface> {
public:
WritableStreamDefaultControllerInterface();
virtual ~WritableStreamDefaultControllerInterface();
static WritableStreamDefaultControllerInterface* Create(
ScriptValue controller);
// Unlike the corresponding method in
// ReadableStreamDefaultControllerInterface, a caller needs to enter a
// ScriptState::Scope.
virtual void Error(ScriptState* script_state, v8::Local<v8::Value>) = 0;
// Helper method
template <typename ErrorType>
void Error(ScriptState* script_state, ErrorType error) {
Error(script_state, ToV8(error, script_state).ToV8Value());
}
virtual void Trace(Visitor*) {}
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_WRITABLE_STREAM_DEFAULT_CONTROLLER_INTERFACE_H_
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