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