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

Side by Side 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. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/streams/ReadableStream2.js ('k') | Source/core/streams/UnderlyingSourceBase.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 "core/CoreExport.h"
12 #include "platform/heap/Handle.h"
13 #include <v8.h>
14
15 namespace blink {
16
17 class CORE_EXPORT ReadableStreamController final : public GarbageCollectedFinali zed<ReadableStreamController> {
18 public:
19 DEFINE_INLINE_TRACE() { }
20
21 explicit ReadableStreamController(ScriptValue stream)
22 : m_scriptState(stream.scriptState())
23 , m_stream(stream.isolate(), stream.v8Value())
24 {
25 m_stream.setWeak(&m_stream, ReadableStreamController::streamWeakCallback );
26 }
27
28 // Users of the ReadableStreamController can call this to note that the stre am has been canceled and thus they
29 // don't anticipate using the ReadableStreamController anymore. (close/desir edSize/enqueue/error will become no-ops
30 // afterward.)
31 void noteHasBeenCanceled()
32 {
33 m_stream.clear();
34 }
35
36 bool isActive() const
37 {
38 return !m_stream.isEmpty();
39 }
40
41 void close()
42 {
43 if (m_stream.isEmpty())
44 return;
45
46 ScriptState::Scope scope(m_scriptState);
47
48 // TODO: should we be storing these somewhere so that we don't need to g et the v8::Local<v8::Function> every
49 // time?
50 auto func = m_scriptState->getFromExtrasExports("CloseReadableStream").v 8Value().As<v8::Function>();
51
52 auto isolate = m_scriptState->isolate();
53 auto context = m_scriptState->context();
54 auto undefined = v8::Undefined(isolate);
55 v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate) };
56 auto result = func->Call(context, undefined, arraysize(args), args);
57
58 ASSERT_UNUSED(result, !result.IsEmpty());
59 m_stream.clear();
60 }
61
62 double desiredSize() const
63 {
64 if (m_stream.isEmpty())
65 return 0;
66
67 ScriptState::Scope scope(m_scriptState);
68
69 auto func = m_scriptState->getFromExtrasExports("GetReadableStreamDesire dSize").v8Value().As<v8::Function>();
70
71 auto isolate = m_scriptState->isolate();
72 auto context = m_scriptState->context();
73 auto undefined = v8::Undefined(isolate);
74 v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate) };
75 auto result = func->Call(context, undefined, arraysize(args), args).ToLo calChecked();
76
77 return result.As<v8::Number>()->Value();
78 }
79
80 template<typename ChunkType>
81 void enqueue(ChunkType chunk) const
82 {
83 if (m_stream.isEmpty())
84 return;
85
86 ScriptState::Scope scope(m_scriptState);
87
88 auto func = m_scriptState->getFromExtrasExports("EnqueueInReadableStream ").v8Value().As<v8::Function>();
89
90 auto isolate = m_scriptState->isolate();
91 auto context = m_scriptState->context();
92 auto undefined = v8::Undefined(isolate);
93 auto chunkForV8 = toV8(chunk, context->Global(), isolate);
94 v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate), chunkForV8 } ;
95
96 auto result = func->Call(context, undefined, arraysize(args), args);
97
98 ASSERT_UNUSED(result, !result.IsEmpty());
99 }
100
101 template<typename ErrorType>
102 void error(ErrorType e)
103 {
104 if (m_stream.isEmpty())
105 return;
106
107 ScriptState::Scope scope(m_scriptState);
108
109 auto func = m_scriptState->getFromExtrasExports("ErrorReadableStream").v 8Value().As<v8::Function>();
110
111 auto isolate = m_scriptState->isolate();
112 auto context = m_scriptState->context();
113 auto undefined = v8::Undefined(isolate);
114 auto errorForV8 = toV8(e, context->Global(), isolate);
115 v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate), errorForV8 } ;
116 auto result = func->Call(context, undefined, arraysize(args), args);
117
118 ASSERT_UNUSED(result, !result.IsEmpty());
119 m_stream.clear();
120 }
121
122 private:
123 static void streamWeakCallback(const v8::WeakCallbackInfo<ScopedPersistent<v 8::Value>>& weakInfo)
124 {
125 weakInfo.GetParameter()->clear();
126 }
127
128 ScopedPersistent<v8::Value> m_stream;
129 ScriptState* m_scriptState;
130 };
131
132 } // namespace blink
133
134 #endif // ReadableStreamController_h
OLDNEW
« 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
This is Rietveld 408576698