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