summaryrefslogtreecommitdiffstats
path: root/src/webenginewidgets/api/qwebenginepage.h
diff options
context:
space:
mode:
authorJocelyn Turcotte <jocelyn.turcotte@digia.com>2014-01-15 18:22:24 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-01-22 08:45:09 +0100
commit6d760436bc0d7081d5bdb50e4141171b6ba940ee (patch)
tree9f17b8122a09b89a818373d9a269072d984ca0a6 /src/webenginewidgets/api/qwebenginepage.h
parente6b846f3800eed35d31a45122f7f8c3215dc38ef (diff)
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 <pierre.rossi@gmail.com>
Diffstat (limited to 'src/webenginewidgets/api/qwebenginepage.h')
-rw-r--r--src/webenginewidgets/api/qwebenginepage.h45
1 files changed, 25 insertions, 20 deletions
diff --git a/src/webenginewidgets/api/qwebenginepage.h b/src/webenginewidgets/api/qwebenginepage.h
index 4d6a72fc4..c33119571 100644
--- a/src/webenginewidgets/api/qwebenginepage.h
+++ b/src/webenginewidgets/api/qwebenginepage.h
@@ -44,6 +44,7 @@ class QWebEngineElementCollection;
class QWebEngineHistory;
class QWebEngineHistoryItem;
+class QWebEnginePage;
class QWebEnginePagePrivate;
class QWebEnginePluginFactory;
class QWebEngineSecurityOrigin;
@@ -52,19 +53,33 @@ class QWebEngineHitTestResultPrivate;
namespace QtWebEnginePrivate {
-struct FunctorBase {
- virtual ~FunctorBase() {}
- virtual void operator()(const QVariant &) = 0;
+template <typename T>
+class QWebEngineCallbackPrivateBase : public QSharedData {
+public:
+ virtual ~QWebEngineCallbackPrivateBase() {}
+ virtual void operator()(T) = 0;
+};
+
+template <typename T, typename F>
+class QWebEngineCallbackPrivate : public QWebEngineCallbackPrivateBase<T> {
+public:
+ QWebEngineCallbackPrivate(F callable) : m_callable(callable) {}
+ virtual void operator()(T value) Q_DECL_OVERRIDE { m_callable(value); }
+private:
+ F m_callable;
};
-template <typename F>
-struct FunctorCallback : public FunctorBase {
- FunctorCallback(F callback) : m_callback(callback) {}
- virtual void operator()(const QVariant &value) Q_DECL_OVERRIDE { m_callback(value); }
+} // namespace QtWebEnginePrivate
+
+template <typename T>
+class QWebEngineCallback {
+public:
+ template <typename F>
+ QWebEngineCallback(F f) : d(new QtWebEnginePrivate::QWebEngineCallbackPrivate<T, F>(f)) { }
private:
- F m_callback;
+ QExplicitlySharedDataPointer<QtWebEnginePrivate::QWebEngineCallbackPrivateBase<T> > d;
+ friend class QWebEnginePage;
};
-}
class QWEBENGINEWIDGETS_EXPORT QWebEngineHitTestResult {
public:
@@ -484,9 +499,7 @@ 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());
+ void runJavaScript(const QString& scriptSource, const QWebEngineCallback<const QVariant &> &resultCallback, const QString &xPath = QString());
public Q_SLOTS:
// Ex-QWebFrame slot
@@ -564,7 +577,6 @@ 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;
@@ -573,13 +585,6 @@ 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