Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1450)

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: Remove C++ queuing strategies Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..0720edf37dbf6cce92329a4ba751be1072c06958
--- /dev/null
+++ b/Source/core/streams/ReadableStreamController.h
@@ -0,0 +1,100 @@
+// 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/ScriptValue.h"
+#include "bindings/core/v8/ToV8.h"
+#include "core/CoreExport.h"
+#include <v8.h>
+
+namespace blink {
+
+class CORE_EXPORT ReadableStreamController {
+public:
+ explicit ReadableStreamController(ScriptValue stream) : m_stream(stream)
+ {
+ }
+
+ void 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(m_stream.context(), undefined, arraysize(args), args);
+
+ ASSERT(!result.IsEmpty());
+ m_stream.clear();
+ }
+
+ double 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(m_stream.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_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(m_stream.context(), undefined, arraysize(args), args);
+
+ ASSERT(!result.IsEmpty());
+ }
+
+ template<typename ErrorType>
+ void 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(m_stream.context(), undefined, arraysize(args), args);
+
+ ASSERT(!result.IsEmpty());
+ m_stream.clear();
+ }
+
+private:
+ ScriptValue m_stream;
+};
+
+} // namespace blink
+
+#endif // ReadableStreamController_h

Powered by Google App Engine
This is Rietveld 408576698