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