OLD | NEW |
---|---|
(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> { | |
haraken
2016/01/15 03:06:33
I think we should move this file to bindings/core/
| |
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()) | |
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); | |
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 v8::Local<v8::Context> context = m_scriptState->context(); | |
78 v8::Local<v8::Object> global = context->Global(); | |
79 v8::Isolate* isolate = m_scriptState->isolate(); | |
80 | |
81 v8::Local<v8::Value> jsChunk = toV8(chunk, global, isolate); | |
82 v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate), jsChunk }; | |
83 v8CallExtraOrCrash(m_scriptState, "EnqueueInReadableStream", args); | |
84 } | |
85 | |
86 template <typename ErrorType> | |
87 void error(ErrorType e) | |
88 { | |
89 if (m_stream.isEmpty()) | |
90 return; | |
91 | |
92 ScriptState::Scope scope(m_scriptState); | |
93 v8::Local<v8::Context> context = m_scriptState->context(); | |
94 v8::Local<v8::Object> global = context->Global(); | |
95 v8::Isolate* isolate = m_scriptState->isolate(); | |
96 | |
97 v8::Local<v8::Value> jsError = toV8(e, global, isolate); | |
98 v8::Local<v8::Value> args[] = { m_stream.newLocal(isolate), jsError }; | |
99 v8CallExtraOrCrash(m_scriptState, "ErrorReadableStream", args); | |
100 | |
101 m_stream.clear(); | |
102 } | |
103 | |
104 private: | |
105 static void streamWeakCallback(const v8::WeakCallbackInfo<ScopedPersistent<v 8::Value>>& weakInfo) | |
106 { | |
107 weakInfo.GetParameter()->clear(); | |
108 } | |
109 | |
110 ScopedPersistent<v8::Value> m_stream; | |
111 ScriptState* m_scriptState; | |
112 }; | |
113 | |
114 } // namespace blink | |
115 | |
116 #endif // ReadableStreamController_h | |
OLD | NEW |