| Index: Source/core/streams/ScriptReadableStreamController.cpp
|
| diff --git a/Source/core/streams/ScriptReadableStreamController.cpp b/Source/core/streams/ScriptReadableStreamController.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..fb1b853e25870e45546690be4d1badc773b507fd
|
| --- /dev/null
|
| +++ b/Source/core/streams/ScriptReadableStreamController.cpp
|
| @@ -0,0 +1,95 @@
|
| +// Copyright 2015 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 "config.h"
|
| +#include "core/streams/ScriptReadableStreamController.h"
|
| +
|
| +#include "bindings/core/v8/ToV8.h"
|
| +
|
| +namespace blink {
|
| +
|
| +// TODO probably need the asserts from the spec here
|
| +
|
| +// NOTE: the normal graph is ScriptReadableStreamController -> ScriptReadableStream -> V8 ReadableStream object ->
|
| +// V8 underlying source object -> ScriptUnderlyingSource -> ScriptReadableStream -> ScriptReadableStreamController ....
|
| +// However once you call close() or error() methods of ScriptReadableStreamController, the
|
| +// ScriptReadableStreamController -> ScriptReadableStream link is broken by m_stream.clear().
|
| +
|
| +ScriptReadableStreamController::ScriptReadableStreamController(ScriptValue stream) : m_stream(stream)
|
| +{
|
| +}
|
| +
|
| +void ScriptReadableStreamController::close()
|
| +{
|
| + if (m_stream.isEmpty())
|
| + return;
|
| +
|
| + ScriptState::Scope scope(m_stream.scriptState());
|
| +
|
| + auto func = m_stream.scriptState()->getFromExtrasExports("CloseReadableStream").v8Value().As<v8::Function>();
|
| +
|
| + auto undefined = v8::Undefined(m_stream.isolate());
|
| + v8::Local<v8::Value> args[] = { m_stream.v8Value() };
|
| + auto result = func->Call(undefined, 1, args);
|
| +
|
| + // TODO crash if result is empty (i.e. an exception)
|
| +
|
| + m_stream.clear();
|
| +}
|
| +
|
| +double ScriptReadableStreamController::desiredSize() const
|
| +{
|
| + if (m_stream.isEmpty())
|
| + return 0;
|
| +
|
| + ScriptState::Scope scope(m_stream.scriptState());
|
| +
|
| + auto func = m_stream.scriptState()->getFromExtrasExports("GetReadableStreamDesiredSize").v8Value().As<v8::Function>();
|
| +
|
| + auto undefined = v8::Undefined(m_stream.isolate());
|
| + v8::Local<v8::Value> args[] = { m_stream.v8Value() };
|
| + auto result = func->Call(undefined, 1, args);
|
| +
|
| + return result.As<v8::Number>()->Value();
|
| +}
|
| +
|
| +template<typename ChunkType>
|
| +void ScriptReadableStreamController::enqueue(ChunkType chunk) const
|
| +{
|
| + if (m_stream.isEmpty())
|
| + return;
|
| +
|
| + ScriptState::Scope scope(m_stream.scriptState());
|
| +
|
| + auto func = m_stream.scriptState()->getFromExtrasExports("EnqueueInReadableStream").v8Value().As<v8::Function>();
|
| +
|
| + auto chunkForV8 = toV8(chunk, m_stream.context()->Global(), m_stream.isolate());
|
| + auto undefined = v8::Undefined(m_stream.isolate());
|
| + v8::Local<v8::Value> args[] = { m_stream.v8Value(), chunkForV8 };
|
| + auto result = func->Call(undefined, 2, args);
|
| +
|
| + // TODO crash if result is empty (i.e. an exception)
|
| +}
|
| +
|
| +template<typename ErrorType>
|
| +void ScriptReadableStreamController::error(ErrorType e)
|
| +{
|
| + if (m_stream.isEmpty())
|
| + return;
|
| +
|
| + ScriptState::Scope scope(m_stream.scriptState());
|
| +
|
| + auto func = m_stream.scriptState()->getFromExtrasExports("ErrorReadableStream").v8Value().As<v8::Function>();
|
| +
|
| + auto errorForV8 = toV8(e, m_stream.context()->Global(), m_stream.isolate());
|
| + auto undefined = v8::Undefined(m_stream.isolate());
|
| + v8::Local<v8::Value> args[] = { m_stream.v8Value(), errorForV8 };
|
| + auto result = func->Call(undefined, 2, args);
|
| +
|
| + // TODO crash if result is empty (i.e. an exception)
|
| +
|
| + m_stream.clear();
|
| +}
|
| +
|
| +} // namespace blink
|
|
|