Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / streams / ReadableStream.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 ReadableStream_h
6 #define ReadableStream_h
7
8 #include "bindings/core/v8/ScriptPromise.h"
9 #include "bindings/core/v8/ScriptPromiseProperty.h"
10 #include "bindings/core/v8/ScriptState.h"
11 #include "bindings/core/v8/ScriptValue.h"
12 #include "bindings/core/v8/ScriptWrappable.h"
13 #include "bindings/core/v8/V8Binding.h"
14 #include "core/dom/ContextLifecycleObserver.h"
15 #include "platform/heap/Handle.h"
16 #include "wtf/Forward.h"
17 #include "wtf/PassRefPtr.h"
18 #include "wtf/RefPtr.h"
19
20 namespace blink {
21
22 class DOMException;
23 class ExceptionState;
24 class UnderlyingSource;
25
26 class ReadableStream : public GarbageCollectedFinalized<ReadableStream>, public ScriptWrappable, public ContextLifecycleObserver {
27 public:
28     enum State {
29         Readable,
30         Waiting,
31         Closed,
32         Errored,
33     };
34
35     // FIXME: Define Strategy here.
36     // FIXME: Add |strategy| constructor parameter.
37     ReadableStream(ScriptState*, UnderlyingSource*, ExceptionState*);
38     virtual ~ReadableStream();
39
40     bool isStarted() const { return m_isStarted; }
41     bool isDraining() const { return m_isDraining; }
42     bool isPulling() const { return m_isPulling; }
43     State state() const { return m_state; }
44
45     virtual ScriptValue read(ScriptState*, ExceptionState*) = 0;
46     ScriptPromise wait(ScriptState*);
47     ScriptPromise cancel(ScriptState*, ScriptValue reason);
48     ScriptPromise closed(ScriptState*);
49
50     void close();
51     void error(PassRefPtrWillBeRawPtr<DOMException>);
52
53     virtual void trace(Visitor*);
54
55 protected:
56     bool enqueuePreliminaryCheck(size_t chunkSize);
57     bool enqueuePostAction(size_t totalQueueSize);
58     void readPreliminaryCheck(ExceptionState*);
59     void readPostAction();
60
61 private:
62     class OnStarted;
63     typedef ScriptPromiseProperty<Member<ReadableStream>, V8UndefinedType, RefPtrWillBeMember<DOMException> > WaitPromise;
64     typedef ScriptPromiseProperty<Member<ReadableStream>, V8UndefinedType, RefPtrWillBeMember<DOMException> > ClosedPromise;
65
66     virtual bool isQueueEmpty() const = 0;
67     virtual void clearQueue() = 0;
68
69     void onStarted(void);
70     void callOrSchedulePull();
71
72     Member<UnderlyingSource> m_source;
73     bool m_isStarted;
74     bool m_isDraining;
75     bool m_isPulling;
76     bool m_isSchedulingPull;
77     State m_state;
78
79     Member<WaitPromise> m_wait;
80     Member<ClosedPromise> m_closed;
81     RefPtrWillBeMember<DOMException> m_exception;
82 };
83
84 } // namespace blink
85
86 #endif // ReadableStream_h
87