Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / streams / ReadableStreamImpl.h
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.
4
5 #ifndef ReadableStreamImpl_h
6 #define ReadableStreamImpl_h
7
8 #include "bindings/core/v8/ExceptionState.h"
9 #include "bindings/core/v8/ScriptState.h"
10 #include "bindings/core/v8/ScriptValue.h"
11 #include "bindings/core/v8/V8Binding.h"
12 #include "core/dom/DOMArrayBuffer.h"
13 #include "core/streams/ReadableStream.h"
14 #include "wtf/Deque.h"
15 #include "wtf/RefPtr.h"
16 #include "wtf/text/WTFString.h"
17
18 namespace blink {
19
20 // We define the default ChunkTypeTraits for frequently used types.
21 template<typename ChunkType>
22 class ReadableStreamChunkTypeTraits { };
23
24 template<>
25 class ReadableStreamChunkTypeTraits<String> {
26 public:
27     typedef String HoldType;
28     typedef const String& PassType;
29
30     static size_t size(const String& value) { return value.length(); }
31     static ScriptValue toScriptValue(ScriptState* scriptState, const HoldType& value)
32     {
33         return ScriptValue(scriptState, v8String(scriptState->isolate(), value));
34     }
35 };
36
37 template<>
38 class ReadableStreamChunkTypeTraits<DOMArrayBuffer> {
39 public:
40     typedef RefPtr<DOMArrayBuffer> HoldType;
41     typedef PassRefPtr<DOMArrayBuffer> PassType;
42
43     static size_t size(const PassType& value) { return value->byteLength(); }
44     static size_t size(const HoldType& value) { return value->byteLength(); }
45     static ScriptValue toScriptValue(ScriptState* scriptState, const HoldType& value)
46     {
47         return ScriptValue(scriptState, toV8NoInline(value.get(), scriptState->context()->Global(), scriptState->isolate()));
48     }
49 };
50
51 // ReadableStreamImpl<ChunkTypeTraits> is a ReadableStream subtype. It has a
52 // queue whose type depends on ChunkTypeTraits and it implements queue-related
53 // ReadableStream pure virtual methods.
54 template <typename ChunkTypeTraits>
55 class ReadableStreamImpl : public ReadableStream {
56 public:
57     ReadableStreamImpl(ExecutionContext* executionContext, UnderlyingSource* source)
58         : ReadableStream(executionContext, source)
59         , m_totalQueueSize(0) { }
60     virtual ~ReadableStreamImpl() { }
61
62     // ReadableStream methods
63     virtual ScriptValue read(ScriptState*, ExceptionState&) override;
64
65     bool enqueue(typename ChunkTypeTraits::PassType);
66
67     virtual void trace(Visitor* visitor) override
68     {
69         ReadableStream::trace(visitor);
70     }
71
72 private:
73     // ReadableStream methods
74     virtual bool isQueueEmpty() const override { return m_queue.isEmpty(); }
75     virtual void clearQueue() override
76     {
77         m_queue.clear();
78         m_totalQueueSize = 0;
79     }
80
81     Deque<typename ChunkTypeTraits::HoldType> m_queue;
82     size_t m_totalQueueSize;
83 };
84
85 template <typename ChunkTypeTraits>
86 bool ReadableStreamImpl<ChunkTypeTraits>::enqueue(typename ChunkTypeTraits::PassType chunk)
87 {
88     size_t size = ChunkTypeTraits::size(chunk);
89     if (!enqueuePreliminaryCheck(size))
90         return false;
91     m_queue.append(chunk);
92     m_totalQueueSize += size;
93     return enqueuePostAction(m_totalQueueSize);
94 }
95
96 template <typename ChunkTypeTraits>
97 ScriptValue ReadableStreamImpl<ChunkTypeTraits>::read(ScriptState* scriptState, ExceptionState& exceptionState)
98 {
99     readPreliminaryCheck(exceptionState);
100     if (exceptionState.hadException())
101         return ScriptValue();
102     ASSERT(state() == Readable);
103     ASSERT(!m_queue.isEmpty());
104     typename ChunkTypeTraits::HoldType chunk = m_queue.takeFirst();
105     m_totalQueueSize -= ChunkTypeTraits::size(chunk);
106     readPostAction();
107     return ChunkTypeTraits::toScriptValue(scriptState, chunk);
108 }
109
110 } // namespace blink
111
112 #endif // ReadableStreamImpl_h