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