| 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 "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| 12 #include "core/CoreExport.h" | 13 #include "core/CoreExport.h" |
| 14 #include "core/workers/WorkerGlobalScope.h" |
| 13 #include "platform/heap/Handle.h" | 15 #include "platform/heap/Handle.h" |
| 14 #include "wtf/RefPtr.h" | 16 #include "wtf/RefPtr.h" |
| 15 #include <v8.h> | 17 #include <v8.h> |
| 16 | 18 |
| 17 namespace blink { | 19 namespace blink { |
| 18 | 20 |
| 19 // TODO(tyoshino): Rename this to ReadableStreamDefaultControllerWrapper. | 21 // TODO(tyoshino): Rename this to ReadableStreamDefaultControllerWrapper. |
| 20 class CORE_EXPORT ReadableStreamController final : public GarbageCollectedFinali
zed<ReadableStreamController> { | 22 class CORE_EXPORT ReadableStreamController final : public GarbageCollectedFinali
zed<ReadableStreamController> { |
| 21 public: | 23 public: |
| 22 DEFINE_INLINE_TRACE() {} | 24 DEFINE_INLINE_TRACE() {} |
| (...skipping 13 matching lines...) Expand all Loading... |
| 36 m_jsController.clear(); | 38 m_jsController.clear(); |
| 37 } | 39 } |
| 38 | 40 |
| 39 bool isActive() const | 41 bool isActive() const |
| 40 { | 42 { |
| 41 return !m_jsController.isEmpty(); | 43 return !m_jsController.isEmpty(); |
| 42 } | 44 } |
| 43 | 45 |
| 44 void close() | 46 void close() |
| 45 { | 47 { |
| 48 if (isTerminating(m_scriptState.get())) { |
| 49 m_jsController.clear(); |
| 50 return; |
| 51 } |
| 46 ScriptState* scriptState = m_scriptState.get(); | 52 ScriptState* scriptState = m_scriptState.get(); |
| 47 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated | 53 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated |
| 48 v8::Isolate* isolate = scriptState->isolate(); | 54 v8::Isolate* isolate = scriptState->isolate(); |
| 49 | 55 |
| 50 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); | 56 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); |
| 51 if (controller.IsEmpty()) | 57 if (controller.IsEmpty()) |
| 52 return; | 58 return; |
| 53 | 59 |
| 54 v8::Local<v8::Value> args[] = { controller }; | 60 v8::Local<v8::Value> args[] = { controller }; |
| 55 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerClose", args); | 61 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerClose", args); |
| 56 m_jsController.clear(); | 62 m_jsController.clear(); |
| 63 if (isTerminating(m_scriptState.get())) |
| 64 return; |
| 57 result.ToLocalChecked(); | 65 result.ToLocalChecked(); |
| 58 } | 66 } |
| 59 | 67 |
| 60 double desiredSize() const | 68 double desiredSize() const |
| 61 { | 69 { |
| 70 if (isTerminating(m_scriptState.get())) |
| 71 return 0; |
| 62 ScriptState* scriptState = m_scriptState.get(); | 72 ScriptState* scriptState = m_scriptState.get(); |
| 63 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated | 73 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated |
| 64 v8::Isolate* isolate = scriptState->isolate(); | 74 v8::Isolate* isolate = scriptState->isolate(); |
| 65 | 75 |
| 66 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); | 76 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); |
| 67 if (controller.IsEmpty()) | 77 if (controller.IsEmpty()) |
| 68 return 0; | 78 return 0; |
| 69 | 79 |
| 70 v8::Local<v8::Value> args[] = { controller }; | 80 v8::Local<v8::Value> args[] = { controller }; |
| 71 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerGetDesiredSize", args); | 81 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerGetDesiredSize", args); |
| 82 if (isTerminating(m_scriptState.get())) |
| 83 return 0; |
| 84 |
| 72 return result.ToLocalChecked().As<v8::Number>()->Value(); | 85 return result.ToLocalChecked().As<v8::Number>()->Value(); |
| 73 } | 86 } |
| 74 | 87 |
| 75 template <typename ChunkType> | 88 template <typename ChunkType> |
| 76 void enqueue(ChunkType chunk) const | 89 void enqueue(ChunkType chunk) const |
| 77 { | 90 { |
| 91 if (isTerminating(m_scriptState.get())) |
| 92 return; |
| 78 ScriptState* scriptState = m_scriptState.get(); | 93 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 | 94 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(); | 95 v8::Isolate* isolate = scriptState->isolate(); |
| 81 | 96 |
| 82 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); | 97 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); |
| 83 if (controller.IsEmpty()) | 98 if (controller.IsEmpty()) |
| 84 return; | 99 return; |
| 85 | 100 |
| 86 v8::Local<v8::Value> jsChunk = toV8(chunk, scriptState); | 101 v8::Local<v8::Value> jsChunk = toV8(chunk, scriptState); |
| 87 v8::Local<v8::Value> args[] = { controller, jsChunk }; | 102 v8::Local<v8::Value> args[] = { controller, jsChunk }; |
| 88 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerEnqueue", args); | 103 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerEnqueue", args); |
| 104 if (isTerminating(m_scriptState.get())) |
| 105 return; |
| 89 result.ToLocalChecked(); | 106 result.ToLocalChecked(); |
| 90 } | 107 } |
| 91 | 108 |
| 92 template <typename ErrorType> | 109 template <typename ErrorType> |
| 93 void error(ErrorType error) | 110 void error(ErrorType error) |
| 94 { | 111 { |
| 112 if (isTerminating(m_scriptState.get())) { |
| 113 m_jsController.clear(); |
| 114 return; |
| 115 } |
| 95 ScriptState* scriptState = m_scriptState.get(); | 116 ScriptState* scriptState = m_scriptState.get(); |
| 96 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated | 117 ScriptState::Scope scope(scriptState); // will assert context is valid;
do not call this method when the context is invalidated |
| 97 v8::Isolate* isolate = scriptState->isolate(); | 118 v8::Isolate* isolate = scriptState->isolate(); |
| 98 | 119 |
| 99 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); | 120 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); |
| 100 if (controller.IsEmpty()) | 121 if (controller.IsEmpty()) |
| 101 return; | 122 return; |
| 102 | 123 |
| 103 v8::Local<v8::Value> jsError = toV8(error, scriptState); | 124 v8::Local<v8::Value> jsError = toV8(error, scriptState); |
| 104 v8::Local<v8::Value> args[] = { controller, jsError }; | 125 v8::Local<v8::Value> args[] = { controller, jsError }; |
| 105 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerError", args); | 126 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerError", args); |
| 106 m_jsController.clear(); | 127 m_jsController.clear(); |
| 128 if (isTerminating(m_scriptState.get())) |
| 129 return; |
| 107 result.ToLocalChecked(); | 130 result.ToLocalChecked(); |
| 108 } | 131 } |
| 109 | 132 |
| 110 private: | 133 private: |
| 134 static bool isTerminating(ScriptState* scriptState) |
| 135 { |
| 136 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 137 if (!executionContext) |
| 138 return true; |
| 139 if (!executionContext->isWorkerGlobalScope()) |
| 140 return false; |
| 141 return toWorkerGlobalScope(executionContext)->scriptController()->isExec
utionTerminating(); |
| 142 } |
| 143 |
| 111 RefPtr<ScriptState> m_scriptState; | 144 RefPtr<ScriptState> m_scriptState; |
| 112 ScopedPersistent<v8::Value> m_jsController; | 145 ScopedPersistent<v8::Value> m_jsController; |
| 113 }; | 146 }; |
| 114 | 147 |
| 115 } // namespace blink | 148 } // namespace blink |
| 116 | 149 |
| 117 #endif // ReadableStreamController_h | 150 #endif // ReadableStreamController_h |
| OLD | NEW |