| 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" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 : public GarbageCollectedFinalized<ReadableStreamController> { | 21 : public GarbageCollectedFinalized<ReadableStreamController> { |
| 22 public: | 22 public: |
| 23 DEFINE_INLINE_TRACE() {} | 23 DEFINE_INLINE_TRACE() {} |
| 24 | 24 |
| 25 explicit ReadableStreamController(ScriptValue controller) | 25 explicit ReadableStreamController(ScriptValue controller) |
| 26 : m_scriptState(controller.getScriptState()), | 26 : m_scriptState(controller.getScriptState()), |
| 27 m_jsController(controller.isolate(), controller.v8Value()) { | 27 m_jsController(controller.isolate(), controller.v8Value()) { |
| 28 m_jsController.setPhantom(); | 28 m_jsController.setPhantom(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Users of the ReadableStreamController can call this to note that the stream
has been canceled and thus they | 31 // Users of the ReadableStreamController can call this to note that the stream |
| 32 // don't anticipate using the ReadableStreamController anymore. (close/desired
Size/enqueue/error will become no-ops | 32 // has been canceled and thus they don't anticipate using the |
| 33 // afterward.) | 33 // ReadableStreamController anymore. (close/desiredSize/enqueue/error will |
| 34 // become no-ops afterward.) |
| 34 void noteHasBeenCanceled() { m_jsController.clear(); } | 35 void noteHasBeenCanceled() { m_jsController.clear(); } |
| 35 | 36 |
| 36 bool isActive() const { return !m_jsController.isEmpty(); } | 37 bool isActive() const { return !m_jsController.isEmpty(); } |
| 37 | 38 |
| 38 void close() { | 39 void close() { |
| 39 ScriptState* scriptState = m_scriptState.get(); | 40 ScriptState* scriptState = m_scriptState.get(); |
| 40 ScriptState::Scope scope( | 41 // This will assert that the context is valid; do not call this method when |
| 41 scriptState); // will assert context is valid; do not call this method
when the context is invalidated | 42 // the context is invalidated. |
| 43 ScriptState::Scope scope(scriptState); |
| 42 v8::Isolate* isolate = scriptState->isolate(); | 44 v8::Isolate* isolate = scriptState->isolate(); |
| 43 | 45 |
| 44 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); | 46 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); |
| 45 if (controller.IsEmpty()) | 47 if (controller.IsEmpty()) |
| 46 return; | 48 return; |
| 47 | 49 |
| 48 v8::Local<v8::Value> args[] = {controller}; | 50 v8::Local<v8::Value> args[] = {controller}; |
| 49 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra( | 51 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra( |
| 50 scriptState, "ReadableStreamDefaultControllerClose", args); | 52 scriptState, "ReadableStreamDefaultControllerClose", args); |
| 51 m_jsController.clear(); | 53 m_jsController.clear(); |
| 52 result.ToLocalChecked(); | 54 result.ToLocalChecked(); |
| 53 } | 55 } |
| 54 | 56 |
| 55 double desiredSize() const { | 57 double desiredSize() const { |
| 56 ScriptState* scriptState = m_scriptState.get(); | 58 ScriptState* scriptState = m_scriptState.get(); |
| 57 ScriptState::Scope scope( | 59 // This will assert that the context is valid; do not call this method when |
| 58 scriptState); // will assert context is valid; do not call this method
when the context is invalidated | 60 // the context is invalidated. |
| 61 ScriptState::Scope scope(scriptState); |
| 59 v8::Isolate* isolate = scriptState->isolate(); | 62 v8::Isolate* isolate = scriptState->isolate(); |
| 60 | 63 |
| 61 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); | 64 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); |
| 62 if (controller.IsEmpty()) | 65 if (controller.IsEmpty()) |
| 63 return 0; | 66 return 0; |
| 64 | 67 |
| 65 v8::Local<v8::Value> args[] = {controller}; | 68 v8::Local<v8::Value> args[] = {controller}; |
| 66 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra( | 69 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra( |
| 67 scriptState, "ReadableStreamDefaultControllerGetDesiredSize", args); | 70 scriptState, "ReadableStreamDefaultControllerGetDesiredSize", args); |
| 68 | 71 |
| 69 return result.ToLocalChecked().As<v8::Number>()->Value(); | 72 return result.ToLocalChecked().As<v8::Number>()->Value(); |
| 70 } | 73 } |
| 71 | 74 |
| 72 template <typename ChunkType> | 75 template <typename ChunkType> |
| 73 void enqueue(ChunkType chunk) const { | 76 void enqueue(ChunkType chunk) const { |
| 74 ScriptState* scriptState = m_scriptState.get(); | 77 ScriptState* scriptState = m_scriptState.get(); |
| 75 ScriptState::Scope scope( | 78 // This will assert that the context is valid; do not call this method when |
| 76 scriptState); // will assert context is valid; do not call this method
when the context is invalidated | 79 // the context is invalidated. |
| 80 ScriptState::Scope scope(scriptState); |
| 77 v8::Isolate* isolate = scriptState->isolate(); | 81 v8::Isolate* isolate = scriptState->isolate(); |
| 78 | 82 |
| 79 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); | 83 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); |
| 80 if (controller.IsEmpty()) | 84 if (controller.IsEmpty()) |
| 81 return; | 85 return; |
| 82 | 86 |
| 83 v8::Local<v8::Value> jsChunk = toV8(chunk, scriptState); | 87 v8::Local<v8::Value> jsChunk = toV8(chunk, scriptState); |
| 84 v8::Local<v8::Value> args[] = {controller, jsChunk}; | 88 v8::Local<v8::Value> args[] = {controller, jsChunk}; |
| 85 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra( | 89 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra( |
| 86 scriptState, "ReadableStreamDefaultControllerEnqueue", args); | 90 scriptState, "ReadableStreamDefaultControllerEnqueue", args); |
| 87 result.ToLocalChecked(); | 91 result.ToLocalChecked(); |
| 88 } | 92 } |
| 89 | 93 |
| 90 template <typename ErrorType> | 94 template <typename ErrorType> |
| 91 void error(ErrorType error) { | 95 void error(ErrorType error) { |
| 92 ScriptState* scriptState = m_scriptState.get(); | 96 ScriptState* scriptState = m_scriptState.get(); |
| 93 ScriptState::Scope scope( | 97 // This will assert that the context is valid; do not call this method when |
| 94 scriptState); // will assert context is valid; do not call this method
when the context is invalidated | 98 // the context is invalidated. |
| 99 ScriptState::Scope scope(scriptState); |
| 95 v8::Isolate* isolate = scriptState->isolate(); | 100 v8::Isolate* isolate = scriptState->isolate(); |
| 96 | 101 |
| 97 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); | 102 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); |
| 98 if (controller.IsEmpty()) | 103 if (controller.IsEmpty()) |
| 99 return; | 104 return; |
| 100 | 105 |
| 101 v8::Local<v8::Value> jsError = toV8(error, scriptState); | 106 v8::Local<v8::Value> jsError = toV8(error, scriptState); |
| 102 v8::Local<v8::Value> args[] = {controller, jsError}; | 107 v8::Local<v8::Value> args[] = {controller, jsError}; |
| 103 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra( | 108 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra( |
| 104 scriptState, "ReadableStreamDefaultControllerError", args); | 109 scriptState, "ReadableStreamDefaultControllerError", args); |
| 105 m_jsController.clear(); | 110 m_jsController.clear(); |
| 106 result.ToLocalChecked(); | 111 result.ToLocalChecked(); |
| 107 } | 112 } |
| 108 | 113 |
| 109 private: | 114 private: |
| 110 RefPtr<ScriptState> m_scriptState; | 115 RefPtr<ScriptState> m_scriptState; |
| 111 ScopedPersistent<v8::Value> m_jsController; | 116 ScopedPersistent<v8::Value> m_jsController; |
| 112 }; | 117 }; |
| 113 | 118 |
| 114 } // namespace blink | 119 } // namespace blink |
| 115 | 120 |
| 116 #endif // ReadableStreamController_h | 121 #endif // ReadableStreamController_h |
| OLD | NEW |