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 V8ScriptRunner::callExtraOrCrash(scriptState, "ReadableStreamDefaultCont
rollerClose", args); | 61 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerClose", args); |
56 | |
57 m_jsController.clear(); | 62 m_jsController.clear(); |
| 63 if (isTerminating(m_scriptState.get())) |
| 64 return; |
| 65 v8CallOrCrash(result); |
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::Local<v8::Value> result = V8ScriptRunner::callExtraOrCrash(scriptSta
te, "ReadableStreamDefaultControllerGetDesiredSize", args); | 81 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerGetDesiredSize", args); |
| 82 if (isTerminating(m_scriptState.get())) |
| 83 return 0; |
72 | 84 |
73 return result.As<v8::Number>()->Value(); | 85 return v8CallOrCrash(result).As<v8::Number>()->Value(); |
74 } | 86 } |
75 | 87 |
76 template <typename ChunkType> | 88 template <typename ChunkType> |
77 void enqueue(ChunkType chunk) const | 89 void enqueue(ChunkType chunk) const |
78 { | 90 { |
| 91 if (isTerminating(m_scriptState.get())) |
| 92 return; |
79 ScriptState* scriptState = m_scriptState.get(); | 93 ScriptState* scriptState = m_scriptState.get(); |
80 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 |
81 v8::Isolate* isolate = scriptState->isolate(); | 95 v8::Isolate* isolate = scriptState->isolate(); |
82 | 96 |
83 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); | 97 v8::Local<v8::Value> controller = m_jsController.newLocal(isolate); |
84 if (controller.IsEmpty()) | 98 if (controller.IsEmpty()) |
85 return; | 99 return; |
86 | 100 |
87 v8::Local<v8::Value> jsChunk = toV8(chunk, scriptState); | 101 v8::Local<v8::Value> jsChunk = toV8(chunk, scriptState); |
88 v8::Local<v8::Value> args[] = { controller, jsChunk }; | 102 v8::Local<v8::Value> args[] = { controller, jsChunk }; |
89 V8ScriptRunner::callExtraOrCrash(scriptState, "ReadableStreamDefaultCont
rollerEnqueue", args); | 103 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerEnqueue", args); |
| 104 if (isTerminating(m_scriptState.get())) |
| 105 return; |
| 106 v8CallOrCrash(result); |
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 V8ScriptRunner::callExtraOrCrash(scriptState, "ReadableStreamDefaultCont
rollerError", args); | 126 v8::MaybeLocal<v8::Value> result = V8ScriptRunner::callExtra(scriptState
, "ReadableStreamDefaultControllerError", args); |
106 | |
107 m_jsController.clear(); | 127 m_jsController.clear(); |
| 128 if (isTerminating(m_scriptState.get())) |
| 129 return; |
| 130 v8CallOrCrash(result); |
108 } | 131 } |
109 | 132 |
110 private: | 133 private: |
111 static void controllerWeakCallback(const v8::WeakCallbackInfo<ScopedPersiste
nt<v8::Value>>& weakInfo) | 134 static void controllerWeakCallback(const v8::WeakCallbackInfo<ScopedPersiste
nt<v8::Value>>& weakInfo) |
112 { | 135 { |
113 weakInfo.GetParameter()->clear(); | 136 weakInfo.GetParameter()->clear(); |
114 } | 137 } |
115 | 138 |
| 139 static bool isTerminating(ScriptState* scriptState) |
| 140 { |
| 141 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 142 if (!executionContext) |
| 143 return true; |
| 144 if (!executionContext->isWorkerGlobalScope()) |
| 145 return false; |
| 146 return toWorkerGlobalScope(executionContext)->scriptController()->isExec
utionTerminating(); |
| 147 } |
| 148 |
116 RefPtr<ScriptState> m_scriptState; | 149 RefPtr<ScriptState> m_scriptState; |
117 ScopedPersistent<v8::Value> m_jsController; | 150 ScopedPersistent<v8::Value> m_jsController; |
118 }; | 151 }; |
119 | 152 |
120 } // namespace blink | 153 } // namespace blink |
121 | 154 |
122 #endif // ReadableStreamController_h | 155 #endif // ReadableStreamController_h |
OLD | NEW |