| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/streams/ReadableStream.h" | 6 #include "core/streams/ReadableStream.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ScriptFunction.h" | 9 #include "bindings/core/v8/ScriptFunction.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "bindings/core/v8/V8Binding.h" | 11 #include "bindings/core/v8/V8Binding.h" |
| 12 #include "core/dom/DOMException.h" | 12 #include "core/dom/DOMException.h" |
| 13 #include "core/dom/ExceptionCode.h" | 13 #include "core/dom/ExceptionCode.h" |
| 14 #include "core/dom/ExecutionContext.h" | 14 #include "core/dom/ExecutionContext.h" |
| 15 #include "core/streams/UnderlyingSource.h" | 15 #include "core/streams/UnderlyingSource.h" |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 | 18 |
| 19 class ReadableStream::OnStarted : public ScriptFunction { | 19 ReadableStream::ReadableStream(ExecutionContext* executionContext, UnderlyingSou
rce* source) |
| 20 public: | 20 : m_source(source) |
| 21 OnStarted(v8::Isolate* isolate, ReadableStream* stream) | |
| 22 : ScriptFunction(isolate) | |
| 23 , m_stream(stream) { } | |
| 24 virtual ScriptValue call(ScriptValue value) OVERRIDE | |
| 25 { | |
| 26 m_stream->onStarted(); | |
| 27 return value; | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 Persistent<ReadableStream> m_stream; | |
| 32 }; | |
| 33 | |
| 34 ReadableStream::ReadableStream(ScriptState* scriptState, UnderlyingSource* sourc
e, ExceptionState* exceptionState) | |
| 35 : ContextLifecycleObserver(scriptState->executionContext()) | |
| 36 , m_source(source) | |
| 37 , m_isStarted(false) | 21 , m_isStarted(false) |
| 38 , m_isDraining(false) | 22 , m_isDraining(false) |
| 39 , m_isPulling(false) | 23 , m_isPulling(false) |
| 40 , m_isSchedulingPull(false) | 24 , m_isSchedulingPull(false) |
| 41 , m_state(Waiting) | 25 , m_state(Waiting) |
| 42 , m_wait(new WaitPromise(scriptState->executionContext(), this, WaitPromise:
:Ready)) | 26 , m_wait(new WaitPromise(executionContext, this, WaitPromise::Ready)) |
| 43 , m_closed(new ClosedPromise(scriptState->executionContext(), this, ClosedPr
omise::Closed)) | 27 , m_closed(new ClosedPromise(executionContext, this, ClosedPromise::Closed)) |
| 44 { | 28 { |
| 45 ScriptWrappable::init(this); | 29 ScriptWrappable::init(this); |
| 46 | |
| 47 ScriptPromise promise = source->startSource(exceptionState); | |
| 48 // The underlying source calls |this->error| on failure. | |
| 49 promise.then(adoptPtr(new OnStarted(scriptState->isolate(), this))); | |
| 50 } | 30 } |
| 51 | 31 |
| 52 ReadableStream::~ReadableStream() | 32 ReadableStream::~ReadableStream() |
| 53 { | 33 { |
| 54 } | 34 } |
| 55 | 35 |
| 36 String ReadableStream::stateString() const |
| 37 { |
| 38 switch (m_state) { |
| 39 case Readable: |
| 40 return "readable"; |
| 41 case Waiting: |
| 42 return "waiting"; |
| 43 case Closed: |
| 44 return "closed"; |
| 45 case Errored: |
| 46 return "errored"; |
| 47 } |
| 48 ASSERT(false); |
| 49 return String(); |
| 50 } |
| 51 |
| 56 bool ReadableStream::enqueuePreliminaryCheck(size_t chunkSize) | 52 bool ReadableStream::enqueuePreliminaryCheck(size_t chunkSize) |
| 57 { | 53 { |
| 58 if (m_state == Errored || m_state == Closed || m_isDraining) | 54 if (m_state == Errored || m_state == Closed || m_isDraining) |
| 59 return false; | 55 return false; |
| 60 | 56 |
| 61 // FIXME: Query strategy. | 57 // FIXME: Query strategy. |
| 62 return true; | 58 return true; |
| 63 } | 59 } |
| 64 | 60 |
| 65 bool ReadableStream::enqueuePostAction(size_t totalQueueSize) | 61 bool ReadableStream::enqueuePostAction(size_t totalQueueSize) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 81 { | 77 { |
| 82 if (m_state == Waiting) { | 78 if (m_state == Waiting) { |
| 83 m_wait->resolve(V8UndefinedType()); | 79 m_wait->resolve(V8UndefinedType()); |
| 84 m_closed->resolve(V8UndefinedType()); | 80 m_closed->resolve(V8UndefinedType()); |
| 85 m_state = Closed; | 81 m_state = Closed; |
| 86 } else if (m_state == Readable) { | 82 } else if (m_state == Readable) { |
| 87 m_isDraining = true; | 83 m_isDraining = true; |
| 88 } | 84 } |
| 89 } | 85 } |
| 90 | 86 |
| 91 void ReadableStream::readPreliminaryCheck(ExceptionState* exceptionState) | 87 void ReadableStream::readPreliminaryCheck(ExceptionState& exceptionState) |
| 92 { | 88 { |
| 93 if (m_state == Waiting) { | 89 if (m_state == Waiting) { |
| 94 exceptionState->throwTypeError("read is called while state is waiting"); | 90 exceptionState.throwTypeError("read is called while state is waiting"); |
| 95 return; | 91 return; |
| 96 } | 92 } |
| 97 if (m_state == Closed) { | 93 if (m_state == Closed) { |
| 98 exceptionState->throwTypeError("read is called while state is closed"); | 94 exceptionState.throwTypeError("read is called while state is closed"); |
| 99 return; | 95 return; |
| 100 } | 96 } |
| 101 if (m_state == Errored) { | 97 if (m_state == Errored) { |
| 102 exceptionState->throwDOMException(m_exception->code(), m_exception->mess
age()); | 98 exceptionState.throwDOMException(m_exception->code(), m_exception->messa
ge()); |
| 103 return; | 99 return; |
| 104 } | 100 } |
| 105 } | 101 } |
| 106 | 102 |
| 107 void ReadableStream::readPostAction() | 103 void ReadableStream::readPostAction() |
| 108 { | 104 { |
| 109 ASSERT(m_state == Readable); | 105 ASSERT(m_state == Readable); |
| 110 if (isQueueEmpty()) { | 106 if (isQueueEmpty()) { |
| 111 if (m_isDraining) { | 107 if (m_isDraining) { |
| 112 m_state = Closed; | 108 m_state = Closed; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 | 163 |
| 168 if (m_state == Waiting || m_state == Readable) { | 164 if (m_state == Waiting || m_state == Readable) { |
| 169 m_state = Errored; | 165 m_state = Errored; |
| 170 m_exception = exception; | 166 m_exception = exception; |
| 171 if (m_wait->state() == m_wait->Pending) | 167 if (m_wait->state() == m_wait->Pending) |
| 172 m_wait->reject(m_exception); | 168 m_wait->reject(m_exception); |
| 173 m_closed->reject(m_exception); | 169 m_closed->reject(m_exception); |
| 174 } | 170 } |
| 175 } | 171 } |
| 176 | 172 |
| 177 void ReadableStream::onStarted() | 173 void ReadableStream::didSourceStart() |
| 178 { | 174 { |
| 179 m_isStarted = true; | 175 m_isStarted = true; |
| 180 if (m_isSchedulingPull) | 176 if (m_isSchedulingPull) |
| 181 m_source->pullSource(); | 177 m_source->pullSource(); |
| 182 } | 178 } |
| 183 | 179 |
| 184 void ReadableStream::callOrSchedulePull() | 180 void ReadableStream::callOrSchedulePull() |
| 185 { | 181 { |
| 186 if (m_isPulling) | 182 if (m_isPulling) |
| 187 return; | 183 return; |
| 188 m_isPulling = true; | 184 m_isPulling = true; |
| 189 if (m_isStarted) | 185 if (m_isStarted) |
| 190 m_source->pullSource(); | 186 m_source->pullSource(); |
| 191 else | 187 else |
| 192 m_isSchedulingPull = true; | 188 m_isSchedulingPull = true; |
| 193 } | 189 } |
| 194 | 190 |
| 195 void ReadableStream::trace(Visitor* visitor) | 191 void ReadableStream::trace(Visitor* visitor) |
| 196 { | 192 { |
| 197 visitor->trace(m_source); | 193 visitor->trace(m_source); |
| 198 visitor->trace(m_wait); | 194 visitor->trace(m_wait); |
| 199 visitor->trace(m_closed); | 195 visitor->trace(m_closed); |
| 200 visitor->trace(m_exception); | 196 visitor->trace(m_exception); |
| 201 } | 197 } |
| 202 | 198 |
| 203 } // namespace blink | 199 } // namespace blink |
| 204 | 200 |
| OLD | NEW |