Home | History | Annotate | Download | only in streams
      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 {
     27     DEFINE_WRAPPERTYPEINFO();
     28 public:
     29     enum State {
     30         Readable,
     31         Waiting,
     32         Closed,
     33         Errored,
     34     };
     35 
     36     // FIXME: Define Strategy here.
     37     // FIXME: Add |strategy| constructor parameter.
     38     // After ReadableStream construction, |didSourceStart| must be called when
     39     // |source| initialization succeeds and |error| must be called when
     40     // |source| initialization fails.
     41     ReadableStream(ExecutionContext*, UnderlyingSource* /* source */);
     42     virtual ~ReadableStream();
     43 
     44     bool isStarted() const { return m_isStarted; }
     45     bool isDraining() const { return m_isDraining; }
     46     bool isPulling() const { return m_isPulling; }
     47     State state() const { return m_state; }
     48     String stateString() const;
     49 
     50     virtual ScriptValue read(ScriptState*, ExceptionState&) = 0;
     51     ScriptPromise wait(ScriptState*);
     52     ScriptPromise cancel(ScriptState*, ScriptValue reason);
     53     ScriptPromise closed(ScriptState*);
     54 
     55     void close();
     56     void error(PassRefPtrWillBeRawPtr<DOMException>);
     57 
     58     void didSourceStart();
     59 
     60     virtual void trace(Visitor*);
     61 
     62 protected:
     63     bool enqueuePreliminaryCheck(size_t chunkSize);
     64     bool enqueuePostAction(size_t totalQueueSize);
     65     void readPreliminaryCheck(ExceptionState&);
     66     void readPostAction();
     67 
     68 private:
     69     typedef ScriptPromiseProperty<Member<ReadableStream>, V8UndefinedType, RefPtrWillBeMember<DOMException> > WaitPromise;
     70     typedef ScriptPromiseProperty<Member<ReadableStream>, V8UndefinedType, RefPtrWillBeMember<DOMException> > ClosedPromise;
     71 
     72     virtual bool isQueueEmpty() const = 0;
     73     virtual void clearQueue() = 0;
     74 
     75     void callOrSchedulePull();
     76 
     77     Member<UnderlyingSource> m_source;
     78     bool m_isStarted;
     79     bool m_isDraining;
     80     bool m_isPulling;
     81     bool m_isSchedulingPull;
     82     State m_state;
     83 
     84     Member<WaitPromise> m_wait;
     85     Member<ClosedPromise> m_closed;
     86     RefPtrWillBeMember<DOMException> m_exception;
     87 };
     88 
     89 } // namespace blink
     90 
     91 #endif // ReadableStream_h
     92