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.
6 #include "core/streams/ReadableStream.h"
8 #include "bindings/core/v8/ExceptionState.h"
9 #include "bindings/core/v8/ScriptFunction.h"
10 #include "bindings/core/v8/ScriptPromiseResolver.h"
11 #include "bindings/core/v8/V8Binding.h"
12 #include "core/dom/DOMException.h"
13 #include "core/dom/ExceptionCode.h"
14 #include "core/dom/ExecutionContext.h"
15 #include "core/streams/UnderlyingSource.h"
19 ReadableStream::ReadableStream(ExecutionContext* executionContext, UnderlyingSource* source)
24 , m_isSchedulingPull(false)
26 , m_wait(new WaitPromise(executionContext, this, WaitPromise::Ready))
27 , m_closed(new ClosedPromise(executionContext, this, ClosedPromise::Closed))
31 ReadableStream::~ReadableStream()
35 String ReadableStream::stateString() const
51 bool ReadableStream::enqueuePreliminaryCheck(size_t chunkSize)
53 if (m_state == Errored || m_state == Closed || m_isDraining)
56 // FIXME: Query strategy.
60 bool ReadableStream::enqueuePostAction(size_t totalQueueSize)
64 // FIXME: Set needsMore correctly.
65 bool needsMore = true;
67 if (m_state == Waiting) {
69 m_wait->resolve(V8UndefinedType());
75 void ReadableStream::close()
77 if (m_state == Waiting) {
78 m_wait->resolve(V8UndefinedType());
79 m_closed->resolve(V8UndefinedType());
81 } else if (m_state == Readable) {
86 void ReadableStream::readPreliminaryCheck(ExceptionState& exceptionState)
88 if (m_state == Waiting) {
89 exceptionState.throwTypeError("read is called while state is waiting");
92 if (m_state == Closed) {
93 exceptionState.throwTypeError("read is called while state is closed");
96 if (m_state == Errored) {
97 exceptionState.throwDOMException(m_exception->code(), m_exception->message());
102 void ReadableStream::readPostAction()
104 ASSERT(m_state == Readable);
105 if (isQueueEmpty()) {
109 m_wait->resolve(V8UndefinedType());
110 m_closed->resolve(V8UndefinedType());
114 callOrSchedulePull();
119 ScriptPromise ReadableStream::wait(ScriptState* scriptState)
121 if (m_state == Waiting)
122 callOrSchedulePull();
123 return m_wait->promise(scriptState->world());
126 ScriptPromise ReadableStream::cancel(ScriptState* scriptState, ScriptValue reason)
128 if (m_state == Errored) {
129 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
130 ScriptPromise promise = resolver->promise();
131 resolver->reject(m_exception);
134 if (m_state == Closed)
135 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isolate()));
137 if (m_state == Waiting) {
138 m_wait->resolve(V8UndefinedType());
140 ASSERT(m_state == Readable);
142 m_wait->resolve(V8UndefinedType());
147 m_closed->resolve(V8UndefinedType());
148 return m_source->cancelSource(scriptState, reason);
151 ScriptPromise ReadableStream::closed(ScriptState* scriptState)
153 return m_closed->promise(scriptState->world());
156 void ReadableStream::error(PassRefPtrWillBeRawPtr<DOMException> exception)
158 if (m_state == Readable) {
163 if (m_state == Waiting || m_state == Readable) {
165 m_exception = exception;
166 if (m_wait->state() == m_wait->Pending)
167 m_wait->reject(m_exception);
168 m_closed->reject(m_exception);
172 void ReadableStream::didSourceStart()
175 if (m_isSchedulingPull)
176 m_source->pullSource();
179 void ReadableStream::callOrSchedulePull()
185 m_source->pullSource();
187 m_isSchedulingPull = true;
190 void ReadableStream::trace(Visitor* visitor)
192 visitor->trace(m_source);
193 visitor->trace(m_wait);
194 visitor->trace(m_closed);
195 visitor->trace(m_exception);