Chromium Code Reviews| Index: third_party/WebKit/Source/core/streams/ReadableStreamController.h |
| diff --git a/third_party/WebKit/Source/core/streams/ReadableStreamController.h b/third_party/WebKit/Source/core/streams/ReadableStreamController.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..324b5c8dbcb0921ed64c6cdce7fefd9a8c6f372f |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/streams/ReadableStreamController.h |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2016 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 ReadableStreamController_h |
| +#define ReadableStreamController_h |
| + |
| +#include "bindings/core/v8/ScopedPersistent.h" |
| +#include "bindings/core/v8/ScriptValue.h" |
| +#include "bindings/core/v8/ToV8.h" |
| +#include "bindings/core/v8/V8BindingMacros.h" |
| +#include "core/CoreExport.h" |
| +#include "platform/heap/Handle.h" |
| +#include <v8.h> |
| + |
| +namespace blink { |
| + |
| +class CORE_EXPORT ReadableStreamController final : public GarbageCollectedFinalized<ReadableStreamController> { |
|
haraken
2016/01/15 03:06:33
I think we should move this file to bindings/core/
|
| +public: |
| + DEFINE_INLINE_TRACE() {} |
| + |
| + explicit ReadableStreamController(ScriptValue stream) |
| + : m_scriptState(stream.scriptState()) |
| + , m_stream(stream.isolate(), stream.v8Value()) |
| + { |
| + m_stream.setWeak(&m_stream, ReadableStreamController::streamWeakCallback); |
| + } |
| + |
| + // Users of the ReadableStreamController can call this to note that the stream has been canceled and thus they |
| + // don't anticipate using the ReadableStreamController anymore. (close/desiredSize/enqueue/error will become no-ops |
| + // afterward.) |
| + void noteHasBeenCanceled() |
| + { |
| + m_stream.clear(); |
| + } |
| + |
| + bool isActive() const |
| + { |
| + return !m_stream.isEmpty(); |
| + } |
| + |
| + void close() |
| + { |
| + if (m_stream.isEmpty()) |
| + return; |
| + |
| + ScriptState::Scope scope(m_scriptState); |
| + v8::Isolate* isolate = m_scriptState->isolate(); |
| + |
| + v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate) }; |
| + v8CallExtraOrCrash(m_scriptState, "CloseReadableStream", args); |
| + |
| + m_stream.clear(); |
| + } |
| + |
| + double desiredSize() const |
| + { |
| + if (m_stream.isEmpty()) |
| + return 0; |
| + |
| + ScriptState::Scope scope(m_scriptState); |
| + v8::Isolate* isolate = m_scriptState->isolate(); |
| + |
| + v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate) }; |
| + v8::Local<v8::Value> result = v8CallExtraOrCrash(m_scriptState, "GetReadableStreamDesiredSize", args); |
| + |
| + return result.As<v8::Number>()->Value(); |
| + } |
| + |
| + template <typename ChunkType> |
| + void enqueue(ChunkType chunk) const |
| + { |
| + if (m_stream.isEmpty()) |
| + return; |
| + |
| + ScriptState::Scope scope(m_scriptState); |
| + v8::Local<v8::Context> context = m_scriptState->context(); |
| + v8::Local<v8::Object> global = context->Global(); |
| + v8::Isolate* isolate = m_scriptState->isolate(); |
| + |
| + v8::Local<v8::Value> jsChunk = toV8(chunk, global, isolate); |
| + v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate), jsChunk }; |
| + v8CallExtraOrCrash(m_scriptState, "EnqueueInReadableStream", args); |
| + } |
| + |
| + template <typename ErrorType> |
| + void error(ErrorType e) |
| + { |
| + if (m_stream.isEmpty()) |
| + return; |
| + |
| + ScriptState::Scope scope(m_scriptState); |
| + v8::Local<v8::Context> context = m_scriptState->context(); |
| + v8::Local<v8::Object> global = context->Global(); |
| + v8::Isolate* isolate = m_scriptState->isolate(); |
| + |
| + v8::Local<v8::Value> jsError = toV8(e, global, isolate); |
| + v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate), jsError }; |
| + v8CallExtraOrCrash(m_scriptState, "ErrorReadableStream", args); |
| + |
| + m_stream.clear(); |
| + } |
| + |
| +private: |
| + static void streamWeakCallback(const v8::WeakCallbackInfo<ScopedPersistent<v8::Value>>& weakInfo) |
| + { |
| + weakInfo.GetParameter()->clear(); |
| + } |
| + |
| + ScopedPersistent<v8::Value> m_stream; |
| + ScriptState* m_scriptState; |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // ReadableStreamController_h |