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/V8ScriptRunner.h" |
| 12 #include "core/CoreExport.h" |
| 13 #include "platform/heap/Handle.h" |
| 14 #include "wtf/RefPtr.h" |
| 15 #include <v8.h> |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 class CORE_EXPORT ReadableStreamController final : public GarbageCollectedFinali
zed<ReadableStreamController> { |
| 20 public: |
| 21 DEFINE_INLINE_TRACE() {} |
| 22 |
| 23 explicit ReadableStreamController(ScriptValue stream) |
| 24 : m_scriptState(stream.scriptState()) |
| 25 , m_stream(stream.isolate(), stream.v8Value()) |
| 26 { |
| 27 m_stream.setWeak(&m_stream, ReadableStreamController::streamWeakCallback
); |
| 28 } |
| 29 |
| 30 // Users of the ReadableStreamController can call this to note that the stre
am has been canceled and thus they |
| 31 // don't anticipate using the ReadableStreamController anymore. (close/desir
edSize/enqueue/error will become no-ops |
| 32 // afterward.) |
| 33 void noteHasBeenCanceled() |
| 34 { |
| 35 m_stream.clear(); |
| 36 } |
| 37 |
| 38 bool isActive() const |
| 39 { |
| 40 return !m_stream.isEmpty(); |
| 41 } |
| 42 |
| 43 void close() |
| 44 { |
| 45 ScriptState* scriptState = m_scriptState.get(); |
| 46 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated |
| 47 v8::Isolate* isolate = scriptState->isolate(); |
| 48 |
| 49 v8::Local<v8::Value> stream = m_stream.newLocal(isolate); |
| 50 if (stream.IsEmpty()) |
| 51 return; |
| 52 |
| 53 v8::Local<v8::Value> args[] = { stream }; |
| 54 V8ScriptRunner::callExtraOrCrash(scriptState, "CloseReadableStream", arg
s); |
| 55 |
| 56 m_stream.clear(); |
| 57 } |
| 58 |
| 59 double desiredSize() const |
| 60 { |
| 61 ScriptState* scriptState = m_scriptState.get(); |
| 62 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated |
| 63 v8::Isolate* isolate = scriptState->isolate(); |
| 64 |
| 65 v8::Local<v8::Value> stream = m_stream.newLocal(isolate); |
| 66 if (stream.IsEmpty()) |
| 67 return 0; |
| 68 |
| 69 v8::Local<v8::Value> args[] = { stream }; |
| 70 v8::Local<v8::Value> result = V8ScriptRunner::callExtraOrCrash(scriptSta
te, "GetReadableStreamDesiredSize", args); |
| 71 |
| 72 return result.As<v8::Number>()->Value(); |
| 73 } |
| 74 |
| 75 template <typename ChunkType> |
| 76 void enqueue(ChunkType chunk) const |
| 77 { |
| 78 ScriptState* scriptState = m_scriptState.get(); |
| 79 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated |
| 80 v8::Isolate* isolate = scriptState->isolate(); |
| 81 |
| 82 v8::Local<v8::Value> stream = m_stream.newLocal(isolate); |
| 83 if (stream.IsEmpty()) |
| 84 return; |
| 85 |
| 86 v8::Local<v8::Value> jsChunk = toV8(chunk, scriptState); |
| 87 v8::Local<v8::Value> args[] = { stream, jsChunk }; |
| 88 V8ScriptRunner::callExtraOrCrash(scriptState, "EnqueueInReadableStream",
args); |
| 89 } |
| 90 |
| 91 template <typename ErrorType> |
| 92 void error(ErrorType error) |
| 93 { |
| 94 ScriptState* scriptState = m_scriptState.get(); |
| 95 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated |
| 96 v8::Isolate* isolate = scriptState->isolate(); |
| 97 |
| 98 v8::Local<v8::Value> stream = m_stream.newLocal(isolate); |
| 99 if (stream.IsEmpty()) |
| 100 return; |
| 101 |
| 102 v8::Local<v8::Value> jsError = toV8(error, scriptState); |
| 103 v8::Local<v8::Value> args[] = { stream, jsError }; |
| 104 V8ScriptRunner::callExtraOrCrash(scriptState, "ErrorReadableStream", arg
s); |
| 105 |
| 106 m_stream.clear(); |
| 107 } |
| 108 |
| 109 private: |
| 110 static void streamWeakCallback(const v8::WeakCallbackInfo<ScopedPersistent<v
8::Value>>& weakInfo) |
| 111 { |
| 112 weakInfo.GetParameter()->clear(); |
| 113 } |
| 114 |
| 115 RefPtr<ScriptState> m_scriptState; |
| 116 ScopedPersistent<v8::Value> m_stream; |
| 117 }; |
| 118 |
| 119 } // namespace blink |
| 120 |
| 121 #endif // ReadableStreamController_h |
OLD | NEW |