OLD | NEW |
(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 |
OLD | NEW |