| 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 } |
| 50 |
| 56 bool ReadableStream::enqueuePreliminaryCheck(size_t chunkSize) | 51 bool ReadableStream::enqueuePreliminaryCheck(size_t chunkSize) |
| 57 { | 52 { |
| 58 if (m_state == Errored || m_state == Closed || m_isDraining) | 53 if (m_state == Errored || m_state == Closed || m_isDraining) |
| 59 return false; | 54 return false; |
| 60 | 55 |
| 61 // FIXME: Query strategy. | 56 // FIXME: Query strategy. |
| 62 return true; | 57 return true; |
| 63 } | 58 } |
| 64 | 59 |
| 65 bool ReadableStream::enqueuePostAction(size_t totalQueueSize) | 60 bool ReadableStream::enqueuePostAction(size_t totalQueueSize) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 81 { | 76 { |
| 82 if (m_state == Waiting) { | 77 if (m_state == Waiting) { |
| 83 m_wait->resolve(V8UndefinedType()); | 78 m_wait->resolve(V8UndefinedType()); |
| 84 m_closed->resolve(V8UndefinedType()); | 79 m_closed->resolve(V8UndefinedType()); |
| 85 m_state = Closed; | 80 m_state = Closed; |
| 86 } else if (m_state == Readable) { | 81 } else if (m_state == Readable) { |
| 87 m_isDraining = true; | 82 m_isDraining = true; |
| 88 } | 83 } |
| 89 } | 84 } |
| 90 | 85 |
| 91 void ReadableStream::readPreliminaryCheck(ExceptionState* exceptionState) | 86 void ReadableStream::readPreliminaryCheck(ExceptionState& exceptionState) |
| 92 { | 87 { |
| 93 if (m_state == Waiting) { | 88 if (m_state == Waiting) { |
| 94 exceptionState->throwTypeError("read is called while state is waiting"); | 89 exceptionState.throwTypeError("read is called while state is waiting"); |
| 95 return; | 90 return; |
| 96 } | 91 } |
| 97 if (m_state == Closed) { | 92 if (m_state == Closed) { |
| 98 exceptionState->throwTypeError("read is called while state is closed"); | 93 exceptionState.throwTypeError("read is called while state is closed"); |
| 99 return; | 94 return; |
| 100 } | 95 } |
| 101 if (m_state == Errored) { | 96 if (m_state == Errored) { |
| 102 exceptionState->throwDOMException(m_exception->code(), m_exception->mess
age()); | 97 exceptionState.throwDOMException(m_exception->code(), m_exception->messa
ge()); |
| 103 return; | 98 return; |
| 104 } | 99 } |
| 105 } | 100 } |
| 106 | 101 |
| 107 void ReadableStream::readPostAction() | 102 void ReadableStream::readPostAction() |
| 108 { | 103 { |
| 109 ASSERT(m_state == Readable); | 104 ASSERT(m_state == Readable); |
| 110 if (isQueueEmpty()) { | 105 if (isQueueEmpty()) { |
| 111 if (m_isDraining) { | 106 if (m_isDraining) { |
| 112 m_state = Closed; | 107 m_state = Closed; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 | 162 |
| 168 if (m_state == Waiting || m_state == Readable) { | 163 if (m_state == Waiting || m_state == Readable) { |
| 169 m_state = Errored; | 164 m_state = Errored; |
| 170 m_exception = exception; | 165 m_exception = exception; |
| 171 if (m_wait->state() == m_wait->Pending) | 166 if (m_wait->state() == m_wait->Pending) |
| 172 m_wait->reject(m_exception); | 167 m_wait->reject(m_exception); |
| 173 m_closed->reject(m_exception); | 168 m_closed->reject(m_exception); |
| 174 } | 169 } |
| 175 } | 170 } |
| 176 | 171 |
| 177 void ReadableStream::onStarted() | 172 void ReadableStream::didSourceStart() |
| 178 { | 173 { |
| 179 m_isStarted = true; | 174 m_isStarted = true; |
| 180 if (m_isSchedulingPull) | 175 if (m_isSchedulingPull) |
| 181 m_source->pullSource(); | 176 m_source->pullSource(); |
| 182 } | 177 } |
| 183 | 178 |
| 184 void ReadableStream::callOrSchedulePull() | 179 void ReadableStream::callOrSchedulePull() |
| 185 { | 180 { |
| 186 if (m_isPulling) | 181 if (m_isPulling) |
| 187 return; | 182 return; |
| 188 m_isPulling = true; | 183 m_isPulling = true; |
| 189 if (m_isStarted) | 184 if (m_isStarted) |
| 190 m_source->pullSource(); | 185 m_source->pullSource(); |
| 191 else | 186 else |
| 192 m_isSchedulingPull = true; | 187 m_isSchedulingPull = true; |
| 193 } | 188 } |
| 194 | 189 |
| 195 void ReadableStream::trace(Visitor* visitor) | 190 void ReadableStream::trace(Visitor* visitor) |
| 196 { | 191 { |
| 197 visitor->trace(m_source); | 192 visitor->trace(m_source); |
| 198 visitor->trace(m_wait); | 193 visitor->trace(m_wait); |
| 199 visitor->trace(m_closed); | 194 visitor->trace(m_closed); |
| 200 visitor->trace(m_exception); | 195 visitor->trace(m_exception); |
| 201 } | 196 } |
| 202 | 197 |
| 203 } // namespace blink | 198 } // namespace blink |
| 204 | 199 |
| OLD | NEW |