1/*
2 * Copyright (C) 2016 Canon Inc.
3 * Copyright (C) 2017 Apple Inc. All rights reserved.
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#pragma once
31
32#if ENABLE(STREAMS_API)
33
34#include "JSDOMConvertBufferSource.h"
35#include "JSReadableStreamDefaultController.h"
36#include <JavaScriptCore/JSCJSValue.h>
37#include <JavaScriptCore/JSCJSValueInlines.h>
38#include <JavaScriptCore/TypedArrays.h>
39
40namespace WebCore {
41
42class ReadableStreamSource;
43
44class ReadableStreamDefaultController {
45public:
46 explicit ReadableStreamDefaultController(JSReadableStreamDefaultController* controller) : m_jsController(controller) { }
47
48 static JSC::JSValue invoke(JSC::ExecState&, JSC::JSObject&, const char*, JSC::JSValue);
49
50 bool enqueue(RefPtr<JSC::ArrayBuffer>&&);
51
52 void error(const Exception&);
53
54 void close() { invoke(*globalObject().globalExec(), jsController(), "close", JSC::jsUndefined()); }
55
56private:
57 void error(JSC::ExecState& state, JSC::JSValue value) { invoke(state, jsController(), "error", value); }
58 void enqueue(JSC::ExecState& state, JSC::JSValue value) { invoke(state, jsController(), "enqueue", value); }
59 JSReadableStreamDefaultController& jsController() const;
60
61 JSDOMGlobalObject& globalObject() const;
62 JSC::ExecState& globalExec() const;
63
64 // The owner of ReadableStreamDefaultController is responsible to keep uncollected the JSReadableStreamDefaultController.
65 JSReadableStreamDefaultController* m_jsController { nullptr };
66};
67
68inline JSReadableStreamDefaultController& ReadableStreamDefaultController::jsController() const
69{
70 ASSERT(m_jsController);
71 return *m_jsController;
72}
73
74inline JSDOMGlobalObject& ReadableStreamDefaultController::globalObject() const
75{
76 ASSERT(m_jsController);
77 ASSERT(m_jsController->globalObject());
78 return *static_cast<JSDOMGlobalObject*>(m_jsController->globalObject());
79}
80
81inline JSC::ExecState& ReadableStreamDefaultController::globalExec() const
82{
83 ASSERT(globalObject().globalExec());
84 return *globalObject().globalExec();
85}
86
87inline bool ReadableStreamDefaultController::enqueue(RefPtr<JSC::ArrayBuffer>&& buffer)
88{
89 auto& globalObject = this->globalObject();
90 JSC::VM& vm = globalObject.vm();
91 JSC::JSLockHolder locker(vm);
92 auto scope = DECLARE_THROW_SCOPE(vm);
93 JSC::ExecState& state = globalExec();
94
95 if (!buffer) {
96 error(state, createOutOfMemoryError(&state));
97 return false;
98 }
99 auto length = buffer->byteLength();
100 auto chunk = JSC::Uint8Array::create(WTFMove(buffer), 0, length);
101 enqueue(state, toJS(&state, &globalObject, chunk.ptr()));
102 scope.assertNoException();
103 return true;
104}
105
106inline void ReadableStreamDefaultController::error(const Exception& exception)
107{
108 JSC::ExecState& state = globalExec();
109 JSC::JSLockHolder locker(&state);
110 error(state, createDOMException(&state, exception.code(), exception.message()));
111}
112
113} // namespace WebCore
114
115#endif // ENABLE(STREAMS_API)
116