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

Unified Diff: third_party/WebKit/Source/bindings/core/v8/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: Rebase on smaller CLs; move ReadableStreamController Created 4 years, 11 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: third_party/WebKit/Source/bindings/core/v8/ReadableStreamController.h
diff --git a/third_party/WebKit/Source/bindings/core/v8/ReadableStreamController.h b/third_party/WebKit/Source/bindings/core/v8/ReadableStreamController.h
new file mode 100644
index 0000000000000000000000000000000000000000..c8c493f9abae49c9d91bc06d1cf217011d7faf3d
--- /dev/null
+++ b/third_party/WebKit/Source/bindings/core/v8/ReadableStreamController.h
@@ -0,0 +1,110 @@
+// 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> {
+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())
yhirano 2016/01/25 11:10:32 m_stream can become empty whenever GC runs, so I p
domenic 2016/02/02 22:59:55 Because GC happens on another thread and can inter
yhirano 2016/02/03 11:04:42 No, v8 GC doesn't run unless we use v8 functionali
+ 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);
haraken 2016/01/25 00:13:29 I haven't yet checked if we should add a MicroTask
domenic 2016/02/02 22:59:55 I am not sure what you mean exactly. This is a V8
+
+ 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::Value> jsChunk = toV8(chunk, m_scriptState);
+ v8::Local<v8::Value> args[] = { m_stream.newLocal(m_scriptState->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::Value> jsError = toV8(e, m_scriptState);
+ v8::Local<v8::Value> args[] = { m_stream.newLocal(m_scriptState->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;
haraken 2016/01/25 00:13:29 This should be RefPtr<ScriptState>. Otherwise it c
domenic 2016/02/02 22:59:55 Done. This makes the code more awkward in a number
+};
+
+} // namespace blink
+
+#endif // ReadableStreamController_h

Powered by Google App Engine
This is Rietveld 408576698