| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/streams/ReadableStream.h" | |
| 6 | |
| 7 #include "bindings/core/v8/ExceptionState.h" | |
| 8 #include "bindings/core/v8/ScriptFunction.h" | |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
| 10 #include "bindings/core/v8/V8Binding.h" | |
| 11 #include "core/dom/DOMException.h" | |
| 12 #include "core/streams/ReadableStreamReader.h" | |
| 13 #include "core/streams/UnderlyingSource.h" | |
| 14 | |
| 15 namespace blink { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 class ConstUndefined : public ScriptFunction { | |
| 20 public: | |
| 21 static v8::Local<v8::Function> create(ScriptState* scriptState) | |
| 22 { | |
| 23 return (new ConstUndefined(scriptState))->bindToV8Function(); | |
| 24 } | |
| 25 | |
| 26 private: | |
| 27 explicit ConstUndefined(ScriptState* scriptState) : ScriptFunction(scriptSta
te) { } | |
| 28 ScriptValue call(ScriptValue value) override | |
| 29 { | |
| 30 return ScriptValue(getScriptState(), v8::Undefined(getScriptState()->iso
late())); | |
| 31 } | |
| 32 }; | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 ReadableStream::ReadableStream(UnderlyingSource* source) | |
| 37 : m_source(source) | |
| 38 , m_isStarted(false) | |
| 39 , m_isDraining(false) | |
| 40 , m_isPulling(false) | |
| 41 , m_isDisturbed(false) | |
| 42 , m_state(Readable) | |
| 43 { | |
| 44 } | |
| 45 | |
| 46 ReadableStream::~ReadableStream() | |
| 47 { | |
| 48 } | |
| 49 | |
| 50 bool ReadableStream::enqueuePreliminaryCheck() | |
| 51 { | |
| 52 // This is a bit different from what spec says: it says we should throw | |
| 53 // an exception here. But sometimes a caller is not in any JavaScript | |
| 54 // context, and we don't want to throw an exception in such a case. | |
| 55 if (m_state == Errored || m_state == Closed || m_isDraining) | |
| 56 return false; | |
| 57 | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 bool ReadableStream::enqueuePostAction() | |
| 62 { | |
| 63 m_isPulling = false; | |
| 64 | |
| 65 bool shouldApplyBackpressure = this->shouldApplyBackpressure(); | |
| 66 // this->shouldApplyBackpressure may call this->error(). | |
| 67 if (m_state == Errored) | |
| 68 return false; | |
| 69 | |
| 70 return !shouldApplyBackpressure; | |
| 71 } | |
| 72 | |
| 73 void ReadableStream::close() | |
| 74 { | |
| 75 if (m_state != Readable) | |
| 76 return; | |
| 77 | |
| 78 if (isQueueEmpty()) | |
| 79 closeInternal(); | |
| 80 else | |
| 81 m_isDraining = true; | |
| 82 } | |
| 83 | |
| 84 void ReadableStream::readInternalPostAction() | |
| 85 { | |
| 86 ASSERT(m_state == Readable); | |
| 87 if (isQueueEmpty() && m_isDraining) | |
| 88 closeInternal(); | |
| 89 callPullIfNeeded(); | |
| 90 } | |
| 91 | |
| 92 ScriptPromise ReadableStream::cancel(ScriptState* scriptState) | |
| 93 { | |
| 94 return cancel(scriptState, ScriptValue(scriptState, v8::Undefined(scriptStat
e->isolate()))); | |
| 95 } | |
| 96 | |
| 97 ScriptPromise ReadableStream::cancel(ScriptState* scriptState, ScriptValue reaso
n) | |
| 98 { | |
| 99 if (m_reader) | |
| 100 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "this stream is locked to a ReadableStreamReader")); | |
| 101 setIsDisturbed(); | |
| 102 if (m_state == Closed) | |
| 103 return ScriptPromise::castUndefined(scriptState); | |
| 104 if (m_state == Errored) | |
| 105 return ScriptPromise::rejectWithDOMException(scriptState, m_exception); | |
| 106 | |
| 107 return cancelInternal(scriptState, reason); | |
| 108 } | |
| 109 | |
| 110 ScriptPromise ReadableStream::cancelInternal(ScriptState* scriptState, ScriptVal
ue reason) | |
| 111 { | |
| 112 setIsDisturbed(); | |
| 113 closeInternal(); | |
| 114 return m_source->cancelSource(scriptState, reason).then(ConstUndefined::crea
te(scriptState)); | |
| 115 } | |
| 116 | |
| 117 void ReadableStream::error(DOMException* exception) | |
| 118 { | |
| 119 if (m_state != ReadableStream::Readable) | |
| 120 return; | |
| 121 | |
| 122 m_exception = exception; | |
| 123 clearQueue(); | |
| 124 rejectAllPendingReads(m_exception); | |
| 125 m_state = Errored; | |
| 126 if (m_reader) | |
| 127 m_reader->error(); | |
| 128 } | |
| 129 | |
| 130 void ReadableStream::didSourceStart() | |
| 131 { | |
| 132 m_isStarted = true; | |
| 133 callPullIfNeeded(); | |
| 134 } | |
| 135 | |
| 136 ReadableStreamReader* ReadableStream::getReader(ExecutionContext* executionConte
xt, ExceptionState& exceptionState) | |
| 137 { | |
| 138 if (m_reader) { | |
| 139 exceptionState.throwTypeError("already locked to a ReadableStreamReader"
); | |
| 140 return nullptr; | |
| 141 } | |
| 142 return new ReadableStreamReader(executionContext, this); | |
| 143 } | |
| 144 | |
| 145 void ReadableStream::setReader(ReadableStreamReader* reader) | |
| 146 { | |
| 147 ASSERT((reader && !m_reader) || (!reader && m_reader)); | |
| 148 m_reader = reader; | |
| 149 } | |
| 150 | |
| 151 void ReadableStream::callPullIfNeeded() | |
| 152 { | |
| 153 if (m_isPulling || m_isDraining || !m_isStarted || m_state == Closed || m_st
ate == Errored) | |
| 154 return; | |
| 155 | |
| 156 bool shouldApplyBackpressure = this->shouldApplyBackpressure(); | |
| 157 if (m_state == Errored) { | |
| 158 // this->shouldApplyBackpressure may call this->error(). | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 // Note: We call |pull| if |hasPendingReads| returns true. This is not yet | |
| 163 // specified, but we need this for the fetch implementation. Because | |
| 164 // it is the only practical customer, it should be no problem for now. | |
| 165 // We need to propose this behavior to the spec. | |
| 166 if (!hasPendingReads() && shouldApplyBackpressure) { | |
| 167 // No pull is needed. | |
| 168 return; | |
| 169 } | |
| 170 m_isPulling = true; | |
| 171 m_source->pullSource(); | |
| 172 } | |
| 173 | |
| 174 void ReadableStream::closeInternal() | |
| 175 { | |
| 176 ASSERT(m_state == Readable); | |
| 177 m_state = Closed; | |
| 178 resolveAllPendingReadsAsDone(); | |
| 179 clearQueue(); | |
| 180 if (m_reader) | |
| 181 m_reader->close(); | |
| 182 } | |
| 183 | |
| 184 DEFINE_TRACE(ReadableStream) | |
| 185 { | |
| 186 visitor->trace(m_source); | |
| 187 visitor->trace(m_exception); | |
| 188 visitor->trace(m_reader); | |
| 189 } | |
| 190 | |
| 191 } // namespace blink | |
| OLD | NEW |