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..2b39de77cf061436b381eba32b7db0d612299440 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/streams/ReadableStreamController.h |
| @@ -0,0 +1,121 @@ |
| +// 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 "wtf/RefPtr.h" |
| +#include <v8.h> |
| + |
| +namespace blink { |
| + |
| +class CORE_EXPORT ReadableStreamController final : public GarbageCollectedFinalized<ReadableStreamController> { |
| +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() |
| + { |
|
haraken
2016/02/04 01:27:50
You need to add:
if (!m_scriptState->contextIsV
yhirano
2016/02/05 02:15:42
Sorry for my ignorance, is it needed even when we'
haraken
2016/02/05 02:32:27
Ah, then not needed. Then shall we add ASSERT(scri
|
| + ScriptState* scriptState = m_scriptState.get(); |
| + ScriptState::Scope scope(scriptState); |
| + v8::Isolate* isolate = scriptState->isolate(); |
| + |
| + v8::Local<v8::Value> stream = m_stream.newLocal(isolate); |
| + if (stream.IsEmpty()) |
| + return; |
| + |
| + v8::Local<v8::Value> args[] = { stream }; |
| + v8CallExtraOrCrash(scriptState, "CloseReadableStream", args); |
|
haraken
2016/02/04 01:27:50
Don't you need to add MicroTaskSuppression?
|
| + |
| + m_stream.clear(); |
| + } |
| + |
| + double desiredSize() const |
| + { |
| + ScriptState* scriptState = m_scriptState.get(); |
| + ScriptState::Scope scope(scriptState); |
| + v8::Isolate* isolate = scriptState->isolate(); |
| + |
| + v8::Local<v8::Value> stream = m_stream.newLocal(isolate); |
| + if (stream.IsEmpty()) |
| + return 0; |
| + |
| + v8::Local<v8::Value> args[] = { stream }; |
| + v8::Local<v8::Value> result = v8CallExtraOrCrash(scriptState, "GetReadableStreamDesiredSize", args); |
| + |
| + return result.As<v8::Number>()->Value(); |
| + } |
| + |
| + template <typename ChunkType> |
| + void enqueue(ChunkType chunk) const |
| + { |
| + ScriptState* scriptState = m_scriptState.get(); |
| + ScriptState::Scope scope(scriptState); |
| + v8::Isolate* isolate = scriptState->isolate(); |
| + |
| + v8::Local<v8::Value> stream = m_stream.newLocal(isolate); |
| + if (stream.IsEmpty()) |
| + return; |
| + |
| + v8::Local<v8::Value> jsChunk = toV8(chunk, scriptState); |
| + v8::Local<v8::Value> args[] = { stream, jsChunk }; |
| + v8CallExtraOrCrash(scriptState, "EnqueueInReadableStream", args); |
| + } |
| + |
| + template <typename ErrorType> |
| + void error(ErrorType e) |
|
haraken
2016/02/04 01:27:50
e => errorType
|
| + { |
| + ScriptState* scriptState = m_scriptState.get(); |
| + ScriptState::Scope scope(scriptState); |
| + v8::Isolate* isolate = scriptState->isolate(); |
| + |
| + v8::Local<v8::Value> stream = m_stream.newLocal(isolate); |
| + if (stream.IsEmpty()) |
| + return; |
| + |
| + v8::Local<v8::Value> jsError = toV8(e, scriptState); |
| + v8::Local<v8::Value> args[] = { stream, jsError }; |
| + v8CallExtraOrCrash(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; |
| + RefPtr<ScriptState> m_scriptState; |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // ReadableStreamController_h |