diff options
author | Pierre Rossi <pierre.rossi@digia.com> | 2013-09-09 17:49:18 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-11-04 12:15:19 +0100 |
commit | a99fdc02797ece15253ff863102d3257945abfab (patch) | |
tree | cead6337f019ab87afff0c0f0865a18d8a075556 /lib/widgets | |
parent | 1b3be641e660f9f87ec290e9adbb42558860ea37 (diff) |
Implement QWebEnginePage::runJavaScript
We can't have QWebFrame::evaluateJavascript in its old form for
several reasons, the first of which being that we don't have a
QWebEngineFrame class anymore. This is worked around by adding
an optional QString parameter with the frame's XPath.
Another issue is that the WebKit1 API was synchronous, and this would
not play nicely with the very asynchronous nature of chromium we're
now sitting on top of. In order to make this obvious when porting, we
rename it to runJavaScript which doesn't return any result.
This also introduces a template member function overload that will
accept function pointers, functors and lambdas,much like Qt5's new
signal/slot syntax, in order to get the result of the javascript
evaluation back.
Change-Id: I64e15a6f5a168936c52a4da2cef6285dfd16e0d5
Reviewed-by: Zeno Albisser <zeno.albisser@digia.com>
Reviewed-by: Andras Becsi <andras.becsi@digia.com>
Diffstat (limited to 'lib/widgets')
-rw-r--r-- | lib/widgets/Api/qwebenginepage.cpp | 22 | ||||
-rw-r--r-- | lib/widgets/Api/qwebenginepage.h | 34 |
2 files changed, 53 insertions, 3 deletions
diff --git a/lib/widgets/Api/qwebenginepage.cpp b/lib/widgets/Api/qwebenginepage.cpp index 223c67844..e37c6126d 100644 --- a/lib/widgets/Api/qwebenginepage.cpp +++ b/lib/widgets/Api/qwebenginepage.cpp @@ -406,6 +406,28 @@ void QWebEnginePage::setZoomFactor(qreal factor) d->adapter->setZoomFactor(factor); } +void QWebEnginePage::runJavaScript(const QString &scriptSource, const QString &xPath) +{ + Q_D(QWebEnginePage); + 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) +{ + Q_D(QWebEnginePage); + d->adapter->runJavaScript(source, xPath, new JSCallbackFunctor(functor)); +} + QWebEnginePage *QWebEnginePage::createWindow(WebWindowType type) { Q_D(QWebEnginePage); diff --git a/lib/widgets/Api/qwebenginepage.h b/lib/widgets/Api/qwebenginepage.h index e426f8da6..8bb4176a6 100644 --- a/lib/widgets/Api/qwebenginepage.h +++ b/lib/widgets/Api/qwebenginepage.h @@ -50,6 +50,22 @@ class QWebEngineSecurityOrigin; class QtViewportAttributesPrivate; class QWebEngineHitTestResultPrivate; +namespace QtWebEnginePrivate { + +struct FunctorBase { + virtual ~FunctorBase() {} + virtual void operator()(const QVariant &) = 0; +}; + +template <typename F> +struct FunctorCallback : public FunctorBase { + FunctorCallback(F callback) : m_callback(callback) {} + virtual void operator()(const QVariant &value) { m_callback(value); } +private: + F m_callback; +}; +} + class QWEBENGINEWIDGETS_EXPORT QWebEngineHitTestResult { public: QWebEngineHitTestResult(); @@ -474,11 +490,15 @@ public: QWebEngineSecurityOrigin securityOrigin() const; + void runJavaScript(const QString& scriptSource, const QString &xPath = QString()); + + template <typename F> + void runJavaScript(const QString& scriptSource, F func, const QString &xPath = QString()); + public Q_SLOTS: - // Ex-QWebFrame slots - QVariant evaluateJavaScript(const QString& scriptSource) { Q_UNUSED(scriptSource); Q_UNREACHABLE(); return QVariant(); }; + // Ex-QWebFrame slot #ifndef QT_NO_PRINTER - void print(QPrinter *printer) const { Q_UNUSED(printer); Q_UNREACHABLE(); }; + void print(QPrinter *printer) const { Q_UNUSED(printer); Q_UNREACHABLE(); } #endif @@ -551,6 +571,7 @@ private: #ifndef QT_NO_ACTION Q_PRIVATE_SLOT(d_func(), void _q_webActionTriggered(bool checked)) #endif + void runJavaScriptHelper(const QString &source, QtWebEnginePrivate::FunctorBase *, const QString &xPath); friend class QWebEngineView; friend class QWebEngineViewPrivate; @@ -559,6 +580,13 @@ private: Q_DECLARE_OPERATORS_FOR_FLAGS(QWebEnginePage::FindFlags); Q_DECLARE_OPERATORS_FOR_FLAGS(QWebEnginePage::RenderLayers); + +template <typename F> +inline void QWebEnginePage::runJavaScript(const QString &scriptSource, F func, const QString &xPath) +{ + runJavaScriptHelper(scriptSource, new QtWebEnginePrivate::FunctorCallback<F>(func), xPath); +} + QT_END_NAMESPACE #endif // QWEBENGINEPAGE_H |