Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qstdweb_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QSTDWEB_P_H
6#define QSTDWEB_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qglobal_p.h>
20#include <QtCore/qglobal.h>
21#include "QtCore/qhash.h"
22#include "QtCore/qiodevice.h"
23#include "QtCore/private/qwasmsuspendresumecontrol_p.h"
24
25#include <emscripten/val.h>
26
27#include <cstdint>
28#include <functional>
29#include <initializer_list>
30#include <memory>
31#include <string>
32#include <utility>
33
34#if QT_CONFIG(thread)
35#include <emscripten/proxying.h>
36#include <emscripten/threading.h>
37#endif // #if QT_CONFIG(thread)
38
39#if QT_CONFIG(wasm_jspi)
40# define QT_WASM_EMSCRIPTEN_ASYNC ,emscripten::async()
41#else
42# define QT_WASM_EMSCRIPTEN_ASYNC
43#endif
44
45QT_BEGIN_NAMESPACE
46
47class QMimeData;
48
49namespace qstdweb {
50 extern const char makeContextfulPromiseFunctionName[];
51
52 // DOM API in C++, implemented using emscripten val.h and bind.h.
53 // This is private API and can be extended and changed as needed.
54 // The API mirrors that of the native API, with some extensions
55 // to ease usage from C++ code.
56
57 class ArrayBuffer;
58 class Blob;
59 class File;
60 class FileList;
61 class FileReader;
62 class Uint8Array;
63 class EventCallback;
64
65 class Q_CORE_EXPORT ArrayBuffer {
66 public:
67 explicit ArrayBuffer(uint32_t size);
68 explicit ArrayBuffer(const emscripten::val &arrayBuffer);
69 uint32_t byteLength() const;
71 emscripten::val val() const;
72
73 private:
74 friend class Uint8Array;
76 };
77
78 class Q_CORE_EXPORT Blob {
79 public:
80 explicit Blob(const emscripten::val &blob);
82 uint32_t size() const;
83 static Blob copyFrom(const char *buffer, uint32_t size, std::string mimeType);
84 static Blob copyFrom(const char *buffer, uint32_t size);
87 emscripten::val val() const;
88 std::string type() const;
89
90 private:
91 friend class FileReader;
93 };
94
95 class Q_CORE_EXPORT File {
96 public:
97 File() = default;
98 explicit File(const emscripten::val &file);
99 ~File();
100
101 File(const File &other);
105
107 std::string name() const;
108 uint64_t size() const;
109 std::string type() const;
111 std::function<void()> completed) const;
112 void stream(char *buffer, std::function<void()> completed) const;
113 emscripten::val val() const;
115 const QString &fileUrlPath() const { return m_urlPath; }
116 emscripten::val file() const { return m_file; }
117
118 private:
121 };
122
139
141
142 class Q_CORE_EXPORT FileList {
143 public:
144 FileList() = default;
145 explicit FileList(const emscripten::val &fileList);
146
147 int length() const;
148 File item(int index) const;
149 File operator[](int index) const;
150 emscripten::val val() const;
151
152 private:
154 };
155
156 class Q_CORE_EXPORT FileReader {
157 public:
158 ArrayBuffer result() const;
159 void readAsArrayBuffer(const Blob &blob) const;
160
161 void onLoad(const std::function<void(emscripten::val)> &onLoad);
162 void onError(const std::function<void(emscripten::val)> &onError);
163 void onAbort(const std::function<void(emscripten::val)> &onAbort);
164 emscripten::val val() const;
165
166 private:
167 emscripten::val m_fileReader = emscripten::val::global("FileReader").new_();
171 };
172
173 class Q_CORE_EXPORT Uint8Array {
174 public:
175 explicit Uint8Array(const emscripten::val &uint8Array);
176 explicit Uint8Array(const ArrayBuffer &buffer);
177 explicit Uint8Array(uint32_t size);
179 Uint8Array(const char *buffer, uint32_t size);
180
181 ArrayBuffer buffer() const;
182 uint32_t length() const;
183 void set(const Uint8Array &source);
185
186 void copyTo(char *destination) const;
188
189 static void copy(char *destination, const Uint8Array &source);
190 static Uint8Array copyFrom(const char *buffer, uint32_t size);
191 static Uint8Array copyFrom(const QByteArray &buffer);
192 emscripten::val val() const;
193
194 private:
195 static emscripten::val constructor_();
197 };
198
199 class Q_CORE_EXPORT FileSystemWritableFileStream {
200 public:
203 emscripten::val val() const;
204
205 private:
207 };
208
209 class Q_CORE_EXPORT FileSystemFileHandle {
210 public:
213
214 std::string name() const;
215 std::string kind() const;
216
217 emscripten::val val() const;
218
219 private:
221 };
222
223 // EventCallback here for source compatibility; prefer using QWasmEventHandler directly
224 class Q_CORE_EXPORT EventCallback : public QWasmEventHandler
225 {
226 public:
227 EventCallback() = default;
228 EventCallback(EventCallback const&) = delete;
231 const std::function<void(emscripten::val)> &fn);
232 };
233
235 {
238 std::function<void()> finallyFunc = []() {};
239 };
240
241 // Note: it is ok for the Promise object to go out of scope,
242 // the resources will be cleaned up in the finally handler.
243 class Q_CORE_EXPORT Promise {
244 public:
245 template<typename... Args>
250 if (m_state->m_promise.isUndefined() || m_state->m_promise["constructor"]["name"].as<std::string>() != "Promise") {
251 qFatal("This function did not return a promise");
252 }
253 addFinallyFunction([](){});
254 }
255
256 template<typename... Args>
261 if (m_state->m_promise.isUndefined() || m_state->m_promise["constructor"]["name"].as<std::string>() != "Promise") {
262 qFatal("This function did not return a promise");
263 }
264 addFinallyFunction([](){});
265 }
266
270 if (m_state->m_promise.isUndefined() || m_state->m_promise["constructor"]["name"].as<std::string>() != "Promise") {
271 qFatal("This function did not return a promise");
272 }
273 addFinallyFunction([](){});
274 }
275
279 for (const auto &p : promises)
281
282 auto arr = emscripten::val::array(all);
284 m_state->m_promise = emscripten::val::global("Promise").call<emscripten::val>("all", arr);
285 addFinallyFunction([](){});
286 }
287
291
292 void suspendExclusive();
293
299
300 emscripten::val getPromise() const;
301
302 public:
303 class State {
304 private:
305 friend class Promise;
306
307 State(const State&) = delete;
308 State(State&&) = delete;
309 State& operator=(const State&) = delete;
310 State& operator=(State&&) = delete;
311
312 public:
315 static size_t numInstances() { return s_numInstances; }
316
317 private:
320 static size_t s_numInstances;
321 };
322
323 private:
325
326 public:
327 // Deprecated: To be backwards compatible
329
330 template<typename... Args>
334 Args... args)
335 {
338 if (promiseObject.isUndefined() || promiseObject["constructor"]["name"].as<std::string>() != "Promise") {
339 qFatal("This function did not return a promise");
340 }
341
343 }
344
345 template<typename... Args>
346 static void make(
351 Args... args)
352 {
355 if (promiseObject.isUndefined() || promiseObject["constructor"]["name"].as<std::string>() != "Promise") {
356 qFatal("This function did not return a promise");
357 }
358
360 }
361
364 };
365
366 template<class F>
367 decltype(auto) bindForever(F wrappedCallback)
368 {
369 return wrappedCallback;
370 }
371
372 class Q_CORE_EXPORT BlobIODevice: public QIODevice
373 {
374 public:
377 bool isSequential() const override;
378 qint64 size() const override;
379 bool seek(qint64 pos) override;
380
381 protected:
383 qint64 writeData(const char *, qint64) override;
384
385 private:
386 Blob m_blob;
387 };
388
390 {
391 public:
392 Uint8ArrayIODevice(Uint8Array array);
393 bool open(QIODevice::OpenMode mode) override;
394 bool isSequential() const override;
395 qint64 size() const override;
396 bool seek(qint64 pos) override;
397
398 protected:
399 qint64 readData(char *data, qint64 maxSize) override;
400 qint64 writeData(const char *data, qint64 size) override;
401
402 private:
403 Uint8Array m_array;
404 };
405
407 {
408 public:
411 void close() override;
412 bool isSequential() const override;
413 qint64 size() const override;
414 bool seek(qint64 pos) override;
415
416 protected:
418 qint64 writeData(const char *data, qint64 size) override;
419
420 private:
422 qint64 m_size = 0;
423 };
424
445
447 {
448 static emscripten::val savedWindow = emscripten::val::global("window");
449 return savedWindow;
450 }
451
452 bool Q_CORE_EXPORT haveAsyncify();
453 bool Q_CORE_EXPORT haveJspi();
455}
456
457QT_END_NAMESPACE
458
459#endif
qint64 size() const override
For open random-access devices, this function returns the size of the device.
Definition qstdweb.cpp:741
bool seek(qint64 pos) override
For random-access devices, this function sets the current position to pos, returning true on success,...
Definition qstdweb.cpp:746
Uint8ArrayIODevice(Uint8Array array)
Definition qstdweb.cpp:725
bool isSequential() const override
Returns true if this device is sequential; otherwise returns false.
Definition qstdweb.cpp:736
qint64 writeData(const char *data, qint64 size) override
Writes up to maxSize bytes from data to the device.
Definition qstdweb.cpp:763
bool open(QIODevice::OpenMode mode) override
Definition qstdweb.cpp:731
qint64 readData(char *data, qint64 maxSize) override
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an...
Definition qstdweb.cpp:753
bool canBlockCallingThread()
Definition qstdweb.cpp:673
emscripten::val window()
Definition qstdweb_p.h:446
double uint53_t
Definition qstdweb.cpp:46
bool haveAsyncify()
Definition qstdweb.cpp:667
bool haveJspi()
Definition qstdweb.cpp:661
const char makeContextfulPromiseFunctionName[]
static void usePotentialyUnusedSymbols()
Definition qstdweb.cpp:31
decltype(auto) bindForever(F wrappedCallback)
Definition qstdweb_p.h:367
std::function< void(emscripten::val)> thenFunc
Definition qstdweb_p.h:236
std::function< void()> finallyFunc
Definition qstdweb_p.h:238
std::function< void(emscripten::val)> catchFunc
Definition qstdweb_p.h:237