Chromium Code Reviews

Unified Diff: Source/core/streams/ReadableStreamController.h

Issue 1167343002: Add methods for creating V8 extras-based ReadableStreams from C++ (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: That todo is not necessary according to yhirano@ Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
« no previous file with comments | « Source/core/streams/ReadableStream2.js ('k') | Source/core/streams/UnderlyingSourceBase.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/streams/ReadableStreamController.h
diff --git a/Source/core/streams/ReadableStreamController.h b/Source/core/streams/ReadableStreamController.h
new file mode 100644
index 0000000000000000000000000000000000000000..792241f923dea69a816fcc9c9535b9dfa16c26d2
--- /dev/null
+++ b/Source/core/streams/ReadableStreamController.h
@@ -0,0 +1,134 @@
+// 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.
+
+#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 "core/CoreExport.h"
+#include "platform/heap/Handle.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()
+ {
+ if (m_stream.isEmpty())
+ return;
+
+ ScriptState::Scope scope(m_scriptState);
+
+ // TODO: should we be storing these somewhere so that we don't need to get the v8::Local<v8::Function> every
+ // time?
+ auto func = m_scriptState->getFromExtrasExports("CloseReadableStream").v8Value().As<v8::Function>();
+
+ auto isolate = m_scriptState->isolate();
+ auto context = m_scriptState->context();
+ auto undefined = v8::Undefined(isolate);
+ v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate) };
+ auto result = func->Call(context, undefined, arraysize(args), args);
+
+ ASSERT_UNUSED(result, !result.IsEmpty());
+ m_stream.clear();
+ }
+
+ double desiredSize() const
+ {
+ if (m_stream.isEmpty())
+ return 0;
+
+ ScriptState::Scope scope(m_scriptState);
+
+ auto func = m_scriptState->getFromExtrasExports("GetReadableStreamDesiredSize").v8Value().As<v8::Function>();
+
+ auto isolate = m_scriptState->isolate();
+ auto context = m_scriptState->context();
+ auto undefined = v8::Undefined(isolate);
+ v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate) };
+ auto result = func->Call(context, undefined, arraysize(args), args).ToLocalChecked();
+
+ return result.As<v8::Number>()->Value();
+ }
+
+ template<typename ChunkType>
+ void enqueue(ChunkType chunk) const
+ {
+ if (m_stream.isEmpty())
+ return;
+
+ ScriptState::Scope scope(m_scriptState);
+
+ auto func = m_scriptState->getFromExtrasExports("EnqueueInReadableStream").v8Value().As<v8::Function>();
+
+ auto isolate = m_scriptState->isolate();
+ auto context = m_scriptState->context();
+ auto undefined = v8::Undefined(isolate);
+ auto chunkForV8 = toV8(chunk, context->Global(), isolate);
+ v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate), chunkForV8 };
+
+ auto result = func->Call(context, undefined, arraysize(args), args);
+
+ ASSERT_UNUSED(result, !result.IsEmpty());
+ }
+
+ template<typename ErrorType>
+ void error(ErrorType e)
+ {
+ if (m_stream.isEmpty())
+ return;
+
+ ScriptState::Scope scope(m_scriptState);
+
+ auto func = m_scriptState->getFromExtrasExports("ErrorReadableStream").v8Value().As<v8::Function>();
+
+ auto isolate = m_scriptState->isolate();
+ auto context = m_scriptState->context();
+ auto undefined = v8::Undefined(isolate);
+ auto errorForV8 = toV8(e, context->Global(), isolate);
+ v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate), errorForV8 };
+ auto result = func->Call(context, undefined, arraysize(args), args);
+
+ ASSERT_UNUSED(result, !result.IsEmpty());
+ 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
« no previous file with comments | « Source/core/streams/ReadableStream2.js ('k') | Source/core/streams/UnderlyingSourceBase.h » ('j') | no next file with comments »

Powered by Google App Engine