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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef ReadableStreamController_h
6 #define ReadableStreamController_h
7
8 #include "bindings/core/v8/ScopedPersistent.h"
9 #include "bindings/core/v8/ScriptValue.h"
10 #include "bindings/core/v8/ToV8.h"
11 #include "bindings/core/v8/V8BindingMacros.h"
12 #include "core/CoreExport.h"
13 #include "platform/heap/Handle.h"
14 #include <v8.h>
15
16 namespace blink {
17
18 class CORE_EXPORT ReadableStreamController final : public GarbageCollectedFinali zed<ReadableStreamController> {
19 public:
20 DEFINE_INLINE_TRACE() {}
21
22 explicit ReadableStreamController(ScriptValue stream)
23 : m_scriptState(stream.scriptState())
24 , m_stream(stream.isolate(), stream.v8Value())
25 {
26 m_stream.setWeak(&m_stream, ReadableStreamController::streamWeakCallback );
27 }
28
29 // Users of the ReadableStreamController can call this to note that the stre am has been canceled and thus they
30 // don't anticipate using the ReadableStreamController anymore. (close/desir edSize/enqueue/error will become no-ops
31 // afterward.)
32 void noteHasBeenCanceled()
33 {
34 m_stream.clear();
35 }
36
37 bool isActive() const
38 {
39 return !m_stream.isEmpty();
40 }
41
42 void close()
43 {
44 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
45 return;
46
47 ScriptState::Scope scope(m_scriptState);
48 v8::Isolate* isolate = m_scriptState->isolate();
49
50 v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate) };
51 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
52
53 m_stream.clear();
54 }
55
56 double desiredSize() const
57 {
58 if (m_stream.isEmpty())
59 return 0;
60
61 ScriptState::Scope scope(m_scriptState);
62 v8::Isolate* isolate = m_scriptState->isolate();
63
64 v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate) };
65 v8::Local<v8::Value> result = v8CallExtraOrCrash(m_scriptState, "GetRead ableStreamDesiredSize", args);
66
67 return result.As<v8::Number>()->Value();
68 }
69
70 template <typename ChunkType>
71 void enqueue(ChunkType chunk) const
72 {
73 if (m_stream.isEmpty())
74 return;
75
76 ScriptState::Scope scope(m_scriptState);
77
78 v8::Local<v8::Value> jsChunk = toV8(chunk, m_scriptState);
79 v8::Local<v8::Value> args[] = { m_stream.newLocal(m_scriptState->isolate ()), jsChunk };
80 v8CallExtraOrCrash(m_scriptState, "EnqueueInReadableStream", args);
81 }
82
83 template <typename ErrorType>
84 void error(ErrorType e)
85 {
86 if (m_stream.isEmpty())
87 return;
88
89 ScriptState::Scope scope(m_scriptState);
90
91 v8::Local<v8::Value> jsError = toV8(e, m_scriptState);
92 v8::Local<v8::Value> args[] = { m_stream.newLocal(m_scriptState->isolate ()), jsError };
93 v8CallExtraOrCrash(m_scriptState, "ErrorReadableStream", args);
94
95 m_stream.clear();
96 }
97
98 private:
99 static void streamWeakCallback(const v8::WeakCallbackInfo<ScopedPersistent<v 8::Value>>& weakInfo)
100 {
101 weakInfo.GetParameter()->clear();
102 }
103
104 ScopedPersistent<v8::Value> m_stream;
105 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
106 };
107
108 } // namespace blink
109
110 #endif // ReadableStreamController_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698