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
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
259 if (m_state->m_promise.isUndefined() || m_state->m_promise["constructor"]["name"].as<std::string>() != "Promise") {
260 qFatal("This function did not return a promise");
261 }
262 addFinallyFunction([](){});
263 }
264
268 for (const auto &p : promises)
270
271 auto arr = emscripten::val::array(all);
273 m_state->m_promise = emscripten::val::global("Promise").call<emscripten::val>("all", arr);
274 addFinallyFunction([](){});
275 }
276
280
281 void suspendExclusive();
282
283 emscripten::val getPromise() const;
284
285 public:
286 class State {
287 private:
288 friend class Promise;
289
290 State(const State&) = delete;
291 State(State&&) = delete;
292 State& operator=(const State&) = delete;
293 State& operator=(State&&) = delete;
294
295 public:
298 static size_t numInstances() { return s_numInstances; }
299
300 private:
303 static size_t s_numInstances;
304 };
305
306 private:
308
309 public:
310 // Deprecated: To be backwards compatible
312
313 template<typename... Args>
317 Args... args)
318 {
321 if (promiseObject.isUndefined() || promiseObject["constructor"]["name"].as<std::string>() != "Promise") {
322 qFatal("This function did not return a promise");
323 }
324
326 }
327
328 template<typename... Args>
329 static void make(
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
347 };
348
349 template<class F>
350 decltype(auto) bindForever(F wrappedCallback)
351 {
352 return wrappedCallback;
353 }
354
355 class Q_CORE_EXPORT BlobIODevice: public QIODevice
356 {
357 public:
360 bool isSequential() const override;
361 qint64 size() const override;
362 bool seek(qint64 pos) override;
363
364 protected:
366 qint64 writeData(const char *, qint64) override;
367
368 private:
369 Blob m_blob;
370 };
371
373 {
374 public:
375 Uint8ArrayIODevice(Uint8Array array);
376 bool open(QIODevice::OpenMode mode) override;
377 bool isSequential() const override;
378 qint64 size() const override;
379 bool seek(qint64 pos) override;
380
381 protected:
382 qint64 readData(char *data, qint64 maxSize) override;
383 qint64 writeData(const char *data, qint64 size) override;
384
385 private:
386 Uint8Array m_array;
387 };
388
390 {
391 public:
394 void close() override;
395 bool isSequential() const override;
396 qint64 size() const override;
397 bool seek(qint64 pos) override;
398
399 protected:
401 qint64 writeData(const char *data, qint64 size) override;
402
403 private:
405 qint64 m_size = 0;
406 };
407
428
430 {
431 static emscripten::val savedWindow = emscripten::val::global("window");
432 return savedWindow;
433 }
434
435 bool Q_CORE_EXPORT haveAsyncify();
436 bool Q_CORE_EXPORT haveJspi();
438}
439
440QT_END_NAMESPACE
441
442#endif
qint64 size() const override
For open random-access devices, this function returns the size of the device.
Definition qstdweb.cpp:734
bool seek(qint64 pos) override
For random-access devices, this function sets the current position to pos, returning true on success,...
Definition qstdweb.cpp:739
Uint8ArrayIODevice(Uint8Array array)
Definition qstdweb.cpp:718
bool isSequential() const override
Returns true if this device is sequential; otherwise returns false.
Definition qstdweb.cpp:729
qint64 writeData(const char *data, qint64 size) override
Writes up to maxSize bytes from data to the device.
Definition qstdweb.cpp:756
bool open(QIODevice::OpenMode mode) override
Definition qstdweb.cpp:724
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:746
bool canBlockCallingThread()
Definition qstdweb.cpp:666
emscripten::val window()
Definition qstdweb_p.h:429
double uint53_t
Definition qstdweb.cpp:46
bool haveAsyncify()
Definition qstdweb.cpp:660
bool haveJspi()
Definition qstdweb.cpp:654
const char makeContextfulPromiseFunctionName[]
static void usePotentialyUnusedSymbols()
Definition qstdweb.cpp:31
decltype(auto) bindForever(F wrappedCallback)
Definition qstdweb_p.h:350
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