From 6d760436bc0d7081d5bdb50e4141171b6ba940ee Mon Sep 17 00:00:00 2001 From: Jocelyn Turcotte Date: Wed, 15 Jan 2014 18:22:24 +0100 Subject: Refactor the callback mechanism used by runJavaScript This prepares the way for other API made async like toHtml and toPlainText. Use a callback class with an implicit templated constructor to carry the functor across the API boundary and avoid the intermediate helper method as the ABI that we have to maintain. Also pass the callback result through WebContentsAdapterClient using a bookkeeping ID instead of transferring the callback to WebContentsAdapter. This will allow other calls, which might not already allow passing a callback functor, to use a consisten way of carrying back the result to the top API layer. Change-Id: Ia923767b9c1021a108c26da17d4c41878ef7cb95 Reviewed-by: Pierre Rossi --- src/webenginewidgets/api/qwebenginepage.cpp | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src/webenginewidgets/api/qwebenginepage.cpp') diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp index 54abb4384..8192f38ed 100644 --- a/src/webenginewidgets/api/qwebenginepage.cpp +++ b/src/webenginewidgets/api/qwebenginepage.cpp @@ -146,6 +146,11 @@ void QWebEnginePagePrivate::close() Q_EMIT q->windowCloseRequested(); } +void QWebEnginePagePrivate::didRunJavaScript(const QVariant& result, quint64 requestId) +{ + (*m_variantCallbacks.take(requestId))(result); +} + void QWebEnginePagePrivate::updateAction(QWebEnginePage::WebAction action) const { #ifdef QT_NO_ACTION @@ -490,20 +495,11 @@ void QWebEnginePage::runJavaScript(const QString &scriptSource, const QString &x d->adapter->runJavaScript(scriptSource, xPath); } -namespace { -struct JSCallbackFunctor : public JSCallbackBase { - JSCallbackFunctor(QtWebEnginePrivate::FunctorBase *functor) : m_func(functor) { } - ~JSCallbackFunctor() { delete m_func; } - void call(const QVariant &value) { (*m_func)(value); } -private: - QtWebEnginePrivate::FunctorBase *m_func; -}; -} - -void QWebEnginePage::runJavaScriptHelper(const QString &source, QtWebEnginePrivate::FunctorBase *functor, const QString &xPath) +void QWebEnginePage::runJavaScript(const QString& scriptSource, const QWebEngineCallback &resultCallback, const QString &xPath) { Q_D(QWebEnginePage); - d->adapter->runJavaScript(source, xPath, new JSCallbackFunctor(functor)); + quint64 requestId = d->adapter->runJavaScriptCallbackResult(scriptSource, xPath); + d->m_variantCallbacks.insert(requestId, resultCallback.d); } QWebEnginePage *QWebEnginePage::createWindow(WebWindowType type) -- cgit v1.2.3