[Streams API] ReadableStream constructor start function should be able to error the...
[WebKit-https.git] / Source / WebCore / Modules / streams / ReadableStreamReader.cpp
1 /*
2  * Copyright (C) 2015 Canon Inc.
3  * Copyright (C) 2015 Igalia S.L.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted, provided that the following conditions
7  * are required to be met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Canon Inc. nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY CANON INC. AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL CANON INC. AND ITS CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "ReadableStreamReader.h"
32
33 #if ENABLE(STREAMS_API)
34
35 #include "NotImplemented.h"
36 #include <wtf/RefCountedLeakCounter.h>
37
38 namespace WebCore {
39
40 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, readableStreamReaderCounter, ("ReadableStreamReader"));
41
42 ReadableStreamReader::ReadableStreamReader(ReadableStream& stream)
43     : ActiveDOMObject(stream.scriptExecutionContext())
44     , m_stream(&stream)
45 {
46 #ifndef NDEBUG
47     readableStreamReaderCounter.increment();
48 #endif
49     suspendIfNeeded();
50     initialize();
51 }
52
53 ReadableStreamReader::~ReadableStreamReader()
54 {
55 #ifndef NDEBUG
56     readableStreamReaderCounter.decrement();
57 #endif
58     if (m_stream) {
59         m_stream->releaseButKeepLocked();
60         m_stream = nullptr;
61     }
62 }
63
64 void ReadableStreamReader::initialize()
65 {
66     ASSERT_WITH_MESSAGE(!m_stream->isLocked(), "A ReadableStream cannot be locked by two readers at the same time.");
67     m_stream->lock(*this);
68     if (m_stream->internalState() == ReadableStream::State::Closed) {
69         changeStateToClosed();
70         return;
71     }
72     if (m_stream->internalState() == ReadableStream::State::Errored) {
73         changeStateToErrored();
74         return;
75     }
76 }
77
78 void ReadableStreamReader::releaseStream()
79 {
80     ASSERT(m_stream);
81     m_stream->release();
82     m_stream = nullptr;
83 }
84
85 void ReadableStreamReader::closed(ClosedSuccessCallback successCallback, ClosedErrorCallback errorCallback)
86 {
87     if (m_state == State::Closed) {
88         successCallback();
89         return;
90     }
91     if (m_state == State::Errored) {
92         errorCallback();
93         return;
94     }
95     m_closedSuccessCallback = WTF::move(successCallback);
96     m_closedErrorCallback = WTF::move(errorCallback);
97 }
98
99 void ReadableStreamReader::changeStateToClosed()
100 {
101     ASSERT(m_state == State::Readable);
102     m_state = State::Closed;
103
104     if (m_closedSuccessCallback) {
105         ClosedSuccessCallback closedSuccessCallback = WTF::move(m_closedSuccessCallback);
106         closedSuccessCallback();
107     }
108     ASSERT(!m_closedSuccessCallback);
109     m_closedErrorCallback = nullptr;
110
111     // FIXME: Implement read promise fulfilling.
112
113     releaseStream();
114 }
115
116 void ReadableStreamReader::changeStateToErrored()
117 {
118     ASSERT(m_state == State::Readable);
119     m_state = State::Errored;
120
121     if (m_closedErrorCallback) {
122         ClosedErrorCallback closedErrorCallback = WTF::move(m_closedErrorCallback);
123         closedErrorCallback();
124     }
125     ASSERT(!m_closedErrorCallback);
126     m_closedSuccessCallback = nullptr;
127
128     // FIXME: Implement read promise rejection.
129
130     releaseStream();
131 }
132
133 const char* ReadableStreamReader::activeDOMObjectName() const
134 {
135     return "ReadableStreamReader";
136 }
137
138 bool ReadableStreamReader::canSuspendForPageCache() const
139 {
140     // FIXME: We should try and do better here.
141     return false;
142 }
143
144 }
145
146 #endif