| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "modules/fetch/ReadableStreamDataConsumerHandle.h" | 5 #include "modules/fetch/ReadableStreamDataConsumerHandle.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/ScopedPersistent.h" | 8 #include "bindings/core/v8/ScopedPersistent.h" |
| 9 #include "bindings/core/v8/ScriptFunction.h" | 9 #include "bindings/core/v8/ScriptFunction.h" |
| 10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
| 11 #include "bindings/core/v8/ScriptValue.h" | 11 #include "bindings/core/v8/ScriptValue.h" |
| 12 #include "bindings/core/v8/V8BindingMacros.h" | 12 #include "bindings/core/v8/V8BindingMacros.h" |
| 13 #include "bindings/core/v8/V8IteratorResultValue.h" | 13 #include "bindings/core/v8/V8IteratorResultValue.h" |
| 14 #include "bindings/core/v8/V8Uint8Array.h" | 14 #include "bindings/core/v8/V8Uint8Array.h" |
| 15 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
| 15 #include "core/dom/DOMTypedArray.h" | 16 #include "core/dom/DOMTypedArray.h" |
| 16 #include "core/streams/ReadableStreamOperations.h" | 17 #include "core/streams/ReadableStreamOperations.h" |
| 18 #include "core/workers/WorkerGlobalScope.h" |
| 17 #include "public/platform/Platform.h" | 19 #include "public/platform/Platform.h" |
| 18 #include "public/platform/WebTaskRunner.h" | 20 #include "public/platform/WebTaskRunner.h" |
| 19 #include "public/platform/WebThread.h" | 21 #include "public/platform/WebThread.h" |
| 20 #include "public/platform/WebTraceLocation.h" | 22 #include "public/platform/WebTraceLocation.h" |
| 21 #include "wtf/Assertions.h" | 23 #include "wtf/Assertions.h" |
| 22 #include "wtf/Functional.h" | 24 #include "wtf/Functional.h" |
| 23 #include "wtf/RefCounted.h" | 25 #include "wtf/RefCounted.h" |
| 24 #include <algorithm> | 26 #include <algorithm> |
| 25 #include <string.h> | 27 #include <string.h> |
| 26 #include <v8.h> | 28 #include <v8.h> |
| 27 | 29 |
| 28 namespace blink { | 30 namespace blink { |
| 29 | 31 |
| 32 namespace { |
| 33 |
| 34 bool isTerminating(ScriptState* scriptState) |
| 35 { |
| 36 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 37 if (!executionContext) |
| 38 return true; |
| 39 if (!executionContext->isWorkerGlobalScope()) |
| 40 return false; |
| 41 return toWorkerGlobalScope(executionContext)->scriptController()->isExecutio
nTerminating(); |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 30 using Result = WebDataConsumerHandle::Result; | 46 using Result = WebDataConsumerHandle::Result; |
| 31 using Flags = WebDataConsumerHandle::Flags; | 47 using Flags = WebDataConsumerHandle::Flags; |
| 32 | 48 |
| 33 // This context is not yet thread-safe. | 49 // This context is not yet thread-safe. |
| 34 class ReadableStreamDataConsumerHandle::ReadingContext final : public RefCounted
<ReadingContext> { | 50 class ReadableStreamDataConsumerHandle::ReadingContext final : public RefCounted
<ReadingContext> { |
| 35 WTF_MAKE_NONCOPYABLE(ReadingContext); | 51 WTF_MAKE_NONCOPYABLE(ReadingContext); |
| 36 public: | 52 public: |
| 37 class OnFulfilled final : public ScriptFunction { | 53 class OnFulfilled final : public ScriptFunction { |
| 38 public: | 54 public: |
| 39 static v8::Local<v8::Function> createFunction(ScriptState* scriptState,
PassRefPtr<ReadingContext> context) | 55 static v8::Local<v8::Function> createFunction(ScriptState* scriptState,
PassRefPtr<ReadingContext> context) |
| 40 { | 56 { |
| 41 return (new OnFulfilled(scriptState, context))->bindToV8Function(); | 57 return (new OnFulfilled(scriptState, context))->bindToV8Function(); |
| 42 } | 58 } |
| 43 | 59 |
| 44 ScriptValue call(ScriptValue v) override | 60 ScriptValue call(ScriptValue v) override |
| 45 { | 61 { |
| 46 RefPtr<ReadingContext> readingContext(m_readingContext); | 62 RefPtr<ReadingContext> readingContext(m_readingContext); |
| 47 if (!readingContext) | 63 if (!readingContext) |
| 48 return v; | 64 return v; |
| 49 bool done; | 65 bool done; |
| 50 v8::Local<v8::Value> item = v.v8Value(); | 66 v8::Local<v8::Value> item = v.v8Value(); |
| 51 ASSERT(item->IsObject()); | 67 ASSERT(item->IsObject()); |
| 68 if (isTerminating(v.getScriptState())) |
| 69 return ScriptValue(); |
| 52 v8::MaybeLocal<v8::Value> maybeValue = v8UnpackIteratorResult(v.getS
criptState(), item.As<v8::Object>(), &done); | 70 v8::MaybeLocal<v8::Value> maybeValue = v8UnpackIteratorResult(v.getS
criptState(), item.As<v8::Object>(), &done); |
| 71 if (isTerminating(v.getScriptState())) |
| 72 return ScriptValue(); |
| 53 v8::Local<v8::Value> value = maybeValue.ToLocalChecked(); | 73 v8::Local<v8::Value> value = maybeValue.ToLocalChecked(); |
| 54 if (done) { | 74 if (done) { |
| 55 readingContext->onReadDone(); | 75 readingContext->onReadDone(); |
| 56 return v; | 76 return v; |
| 57 } | 77 } |
| 58 if (!value->IsUint8Array()) { | 78 if (!value->IsUint8Array()) { |
| 59 readingContext->onRejected(); | 79 readingContext->onRejected(); |
| 60 return ScriptValue(); | 80 return ScriptValue(); |
| 61 } | 81 } |
| 62 readingContext->onRead(V8Uint8Array::toImpl(value.As<v8::Object>()))
; | 82 readingContext->onRead(V8Uint8Array::toImpl(value.As<v8::Object>()))
; |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 { | 292 { |
| 273 } | 293 } |
| 274 ReadableStreamDataConsumerHandle::~ReadableStreamDataConsumerHandle() = default; | 294 ReadableStreamDataConsumerHandle::~ReadableStreamDataConsumerHandle() = default; |
| 275 | 295 |
| 276 std::unique_ptr<FetchDataConsumerHandle::Reader> ReadableStreamDataConsumerHandl
e::obtainFetchDataReader(Client* client) | 296 std::unique_ptr<FetchDataConsumerHandle::Reader> ReadableStreamDataConsumerHandl
e::obtainFetchDataReader(Client* client) |
| 277 { | 297 { |
| 278 return WTF::wrapUnique(new ReadingContext::ReaderImpl(m_readingContext, clie
nt)); | 298 return WTF::wrapUnique(new ReadingContext::ReaderImpl(m_readingContext, clie
nt)); |
| 279 } | 299 } |
| 280 | 300 |
| 281 } // namespace blink | 301 } // namespace blink |
| OLD | NEW |