| 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 #ifndef ReadableStreamImpl_h | 5 #ifndef ReadableStreamImpl_h |
| 6 #define ReadableStreamImpl_h | 6 #define ReadableStreamImpl_h |
| 7 | 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "bindings/core/v8/ScriptValue.h" | 10 #include "bindings/core/v8/ScriptValue.h" |
| 11 #include "bindings/core/v8/V8ArrayBuffer.h" | 11 #include "bindings/core/v8/V8ArrayBuffer.h" |
| 12 #include "bindings/core/v8/V8Binding.h" | 12 #include "bindings/core/v8/V8Binding.h" |
| 13 #include "core/dom/DOMArrayBuffer.h" | 13 #include "core/dom/DOMArrayBuffer.h" |
| 14 #include "core/streams/ReadableStream.h" | 14 #include "core/streams/ReadableStream.h" |
| 15 #include "wtf/Deque.h" | 15 #include "wtf/Deque.h" |
| 16 #include "wtf/RefPtr.h" | 16 #include "wtf/RefPtr.h" |
| 17 #include "wtf/text/WTFString.h" | 17 #include "wtf/text/WTFString.h" |
| 18 #include <utility> | 18 #include <utility> |
| 19 | 19 |
| 20 namespace blink { | 20 namespace blink { |
| 21 | 21 |
| 22 class ExclusiveStreamReader; | |
| 23 // We define the default ChunkTypeTraits for frequently used types. | 22 // We define the default ChunkTypeTraits for frequently used types. |
| 24 template<typename ChunkType> | 23 template<typename ChunkType> |
| 25 class ReadableStreamChunkTypeTraits { }; | 24 class ReadableStreamChunkTypeTraits { }; |
| 26 | 25 |
| 27 template<> | 26 template<> |
| 28 class ReadableStreamChunkTypeTraits<String> { | 27 class ReadableStreamChunkTypeTraits<String> { |
| 29 public: | 28 public: |
| 30 typedef String HoldType; | 29 typedef String HoldType; |
| 31 typedef const String& PassType; | 30 typedef const String& PassType; |
| 32 | 31 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 75 |
| 77 ReadableStreamImpl(ExecutionContext* executionContext, UnderlyingSource* sou
rce) | 76 ReadableStreamImpl(ExecutionContext* executionContext, UnderlyingSource* sou
rce) |
| 78 : ReadableStreamImpl(executionContext, source, new DefaultStrategy) { } | 77 : ReadableStreamImpl(executionContext, source, new DefaultStrategy) { } |
| 79 ReadableStreamImpl(ExecutionContext* executionContext, UnderlyingSource* sou
rce, Strategy* strategy) | 78 ReadableStreamImpl(ExecutionContext* executionContext, UnderlyingSource* sou
rce, Strategy* strategy) |
| 80 : ReadableStream(executionContext, source) | 79 : ReadableStream(executionContext, source) |
| 81 , m_strategy(strategy) | 80 , m_strategy(strategy) |
| 82 , m_totalQueueSize(0) { } | 81 , m_totalQueueSize(0) { } |
| 83 ~ReadableStreamImpl() override { } | 82 ~ReadableStreamImpl() override { } |
| 84 | 83 |
| 85 // ReadableStream methods | 84 // ReadableStream methods |
| 86 ScriptValue readInternal(ScriptState*, ExceptionState&) override; | 85 ScriptValue read(ScriptState*, ExceptionState&) override; |
| 87 | 86 |
| 88 bool enqueue(typename ChunkTypeTraits::PassType); | 87 bool enqueue(typename ChunkTypeTraits::PassType); |
| 89 | 88 |
| 90 // This function is intended to be used by internal code to withdraw | 89 // This function is intended to be used by internal code to withdraw |
| 91 // queued data. This pulls all data from this stream's queue, but | 90 // queued data. This pulls all data from this stream's queue, but |
| 92 // ReadableStream public APIs can work with the behavior (i.e. it behaves | 91 // ReadableStream public APIs can work with the behavior (i.e. it behaves |
| 93 // as if multiple read-one-buffer calls were made). | 92 // as if multiple read-one-buffer calls were made). |
| 94 void readInternal(Deque<std::pair<typename ChunkTypeTraits::HoldType, size_t
>>& queue); | 93 void read(Deque<std::pair<typename ChunkTypeTraits::HoldType, size_t>>& queu
e); |
| 95 | 94 |
| 96 void trace(Visitor* visitor) override | 95 void trace(Visitor* visitor) override |
| 97 { | 96 { |
| 98 visitor->trace(m_strategy); | 97 visitor->trace(m_strategy); |
| 99 ReadableStream::trace(visitor); | 98 ReadableStream::trace(visitor); |
| 100 } | 99 } |
| 101 | 100 |
| 102 private: | 101 private: |
| 103 // ReadableStream methods | 102 // ReadableStream methods |
| 104 bool isQueueEmpty() const override { return m_queue.isEmpty(); } | 103 bool isQueueEmpty() const override { return m_queue.isEmpty(); } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 122 { | 121 { |
| 123 size_t size = m_strategy->size(chunk, this); | 122 size_t size = m_strategy->size(chunk, this); |
| 124 if (!enqueuePreliminaryCheck()) | 123 if (!enqueuePreliminaryCheck()) |
| 125 return false; | 124 return false; |
| 126 m_queue.append(std::make_pair(chunk, size)); | 125 m_queue.append(std::make_pair(chunk, size)); |
| 127 m_totalQueueSize += size; | 126 m_totalQueueSize += size; |
| 128 return enqueuePostAction(); | 127 return enqueuePostAction(); |
| 129 } | 128 } |
| 130 | 129 |
| 131 template <typename ChunkTypeTraits> | 130 template <typename ChunkTypeTraits> |
| 132 ScriptValue ReadableStreamImpl<ChunkTypeTraits>::readInternal(ScriptState* scrip
tState, ExceptionState& exceptionState) | 131 ScriptValue ReadableStreamImpl<ChunkTypeTraits>::read(ScriptState* scriptState,
ExceptionState& exceptionState) |
| 133 { | 132 { |
| 134 readInternalPreliminaryCheck(exceptionState); | 133 readPreliminaryCheck(exceptionState); |
| 135 if (exceptionState.hadException()) | 134 if (exceptionState.hadException()) |
| 136 return ScriptValue(); | 135 return ScriptValue(); |
| 137 ASSERT(stateInternal() == Readable); | 136 ASSERT(state() == Readable); |
| 138 ASSERT(!m_queue.isEmpty()); | 137 ASSERT(!m_queue.isEmpty()); |
| 139 auto pair = m_queue.takeFirst(); | 138 auto pair = m_queue.takeFirst(); |
| 140 typename ChunkTypeTraits::HoldType chunk = pair.first; | 139 typename ChunkTypeTraits::HoldType chunk = pair.first; |
| 141 size_t size = pair.second; | 140 size_t size = pair.second; |
| 142 ASSERT(m_totalQueueSize >= size); | 141 ASSERT(m_totalQueueSize >= size); |
| 143 m_totalQueueSize -= size; | 142 m_totalQueueSize -= size; |
| 144 readInternalPostAction(); | 143 readPostAction(); |
| 145 return ChunkTypeTraits::toScriptValue(scriptState, chunk); | 144 return ChunkTypeTraits::toScriptValue(scriptState, chunk); |
| 146 } | 145 } |
| 147 | 146 |
| 148 template <typename ChunkTypeTraits> | 147 template <typename ChunkTypeTraits> |
| 149 void ReadableStreamImpl<ChunkTypeTraits>::readInternal(Deque<std::pair<typename
ChunkTypeTraits::HoldType, size_t>>& queue) | 148 void ReadableStreamImpl<ChunkTypeTraits>::read(Deque<std::pair<typename ChunkTyp
eTraits::HoldType, size_t>>& queue) |
| 150 { | 149 { |
| 151 // We omit the preliminary check. Check it by yourself. | 150 // We omit the preliminary check. Check it by yourself. |
| 152 ASSERT(stateInternal() == Readable); | 151 ASSERT(state() == Readable); |
| 153 ASSERT(!m_queue.isEmpty()); | 152 ASSERT(!m_queue.isEmpty()); |
| 154 ASSERT(queue.isEmpty()); | 153 ASSERT(queue.isEmpty()); |
| 155 | 154 |
| 156 queue.swap(m_queue); | 155 queue.swap(m_queue); |
| 157 m_totalQueueSize = 0; | 156 m_totalQueueSize = 0; |
| 158 readInternalPostAction(); | 157 readPostAction(); |
| 159 } | 158 } |
| 160 | 159 |
| 161 } // namespace blink | 160 } // namespace blink |
| 162 | 161 |
| 163 #endif // ReadableStreamImpl_h | 162 #endif // ReadableStreamImpl_h |
| OLD | NEW |