summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasmclipboard.cpp
diff options
context:
space:
mode:
authorMorten Sørvig <morten.sorvig@qt.io>2025-02-28 20:09:00 +0100
committerMorten Sørvig <morten.sorvig@qt.io>2025-04-25 18:14:23 +0200
commitf4287c4531d4bede3347df37168c3e19f3d25e89 (patch)
treef12f7b49d479a525be0e4bd04e3d668f68efb021 /src/plugins/platforms/wasm/qwasmclipboard.cpp
parentd4efce2119ed1967d7b2ba8e5ae5d8571ea68906 (diff)
wasm: fix clipboard event handler leaks
Use QWasmEventHandler instead of calling addEventListener() directly (using QWasmEventHandler also allows supporting JSPI). The QWasmEventHandler destructor calls removeEventListener(), which should make sure everything gets cleaned up. Keep the Chrome-specific global (document) event handler code path, but register once at startup instead of once per window. Change-Id: If4314df738afc0dcfdb0f6f1ab9e1f176e1812ac Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'src/plugins/platforms/wasm/qwasmclipboard.cpp')
-rw-r--r--src/plugins/platforms/wasm/qwasmclipboard.cpp41
1 files changed, 15 insertions, 26 deletions
diff --git a/src/plugins/platforms/wasm/qwasmclipboard.cpp b/src/plugins/platforms/wasm/qwasmclipboard.cpp
index b96262319d8..7851162494d 100644
--- a/src/plugins/platforms/wasm/qwasmclipboard.cpp
+++ b/src/plugins/platforms/wasm/qwasmclipboard.cpp
@@ -46,7 +46,7 @@ static void commonCopyEvent(val event)
event.call<void>("preventDefault");
}
-static void qClipboardCutTo(val event)
+void QWasmClipboard::cut(val event)
{
QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext();
if (wasmInput && wasmInput->usingTextInput())
@@ -61,7 +61,7 @@ static void qClipboardCutTo(val event)
commonCopyEvent(event);
}
-static void qClipboardCopyTo(val event)
+void QWasmClipboard::copy(val event)
{
QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext();
if (wasmInput && wasmInput->usingTextInput())
@@ -75,7 +75,7 @@ static void qClipboardCopyTo(val event)
commonCopyEvent(event);
}
-static void qClipboardPasteTo(val event)
+void QWasmClipboard::paste(val event)
{
QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext();
if (wasmInput && wasmInput->usingTextInput())
@@ -86,12 +86,6 @@ static void qClipboardPasteTo(val event)
QWasmIntegration::get()->getWasmClipboard()->sendClipboardData(event);
}
-EMSCRIPTEN_BINDINGS(qtClipboardModule) {
- function("qtClipboardCutTo", &qClipboardCutTo);
- function("qtClipboardCopyTo", &qClipboardCopyTo);
- function("qtClipboardPasteTo", &qClipboardPasteTo);
-}
-
QWasmClipboard::QWasmClipboard()
{
val clipboard = val::global("navigator")["clipboard"];
@@ -101,6 +95,13 @@ QWasmClipboard::QWasmClipboard()
if (m_hasClipboardApi && hasPermissionsApi)
initClipboardPermissions();
+
+ if (!shouldInstallWindowEventHandlers()) {
+ val document = val::global("document");
+ m_documentCut = QWasmEventHandler(document, "cut", QWasmClipboard::cut);
+ m_documentCopy = QWasmEventHandler(document, "copy", QWasmClipboard::copy);
+ m_documentPaste = QWasmEventHandler(document, "paste", QWasmClipboard::paste);
+ }
}
QWasmClipboard::~QWasmClipboard()
@@ -167,27 +168,15 @@ void QWasmClipboard::initClipboardPermissions()
})());
}
-void QWasmClipboard::installEventHandlers(const emscripten::val &target)
+bool QWasmClipboard::hasClipboardApi()
{
- emscripten::val cContext = val::undefined();
- emscripten::val isChromium = val::global("window")["chrome"];
- if (!isChromium.isUndefined()) {
- cContext = val::global("document");
- } else {
- cContext = target;
- }
- // Fallback path for browsers which do not support direct clipboard access
- cContext.call<void>("addEventListener", val("cut"),
- val::module_property("qtClipboardCutTo"), true);
- cContext.call<void>("addEventListener", val("copy"),
- val::module_property("qtClipboardCopyTo"), true);
- cContext.call<void>("addEventListener", val("paste"),
- val::module_property("qtClipboardPasteTo"), true);
+ return m_hasClipboardApi;
}
-bool QWasmClipboard::hasClipboardApi()
+bool QWasmClipboard::shouldInstallWindowEventHandlers()
{
- return m_hasClipboardApi;
+ // Chrome uses global handlers
+ return val::global("window")["chrome"].isUndefined() == false;
}
void QWasmClipboard::writeToClipboardApi()