summaryrefslogtreecommitdiffstats
path: root/src/webenginewidgets/api
diff options
context:
space:
mode:
authorAndras Becsi <andras.becsi@theqtcompany.com>2015-05-11 14:32:01 +0200
committerLiang Qi <liang.qi@theqtcompany.com>2015-06-08 10:56:08 +0000
commit8c3178b64d992435cb90a426ff075fee6778e04a (patch)
treee53836e4824073851f94d4c6fcfac99526f0a100 /src/webenginewidgets/api
parentbfef77e3ca0d82253fb70ff178ff5a826c671d55 (diff)
Move QWebEngineCallback to core API and templatize CallbackDirectory
This undocumented helper template will also be utilized by the core API since Chromium's content API heavily relies on callbacks. To make the internal CallbackDirectory easily extendable with new types, templatize its callback registration and dispatching functions to be able to replace its type switches and the internal union. Change-Id: I3f636fef48973ac95253f1c1bd396550286e571e Reviewed-by: Pierre Rossi <pierre.rossi@theqtcompany.com>
Diffstat (limited to 'src/webenginewidgets/api')
-rw-r--r--src/webenginewidgets/api/qwebenginepage.cpp112
-rw-r--r--src/webenginewidgets/api/qwebenginepage.h32
-rw-r--r--src/webenginewidgets/api/qwebenginepage_p.h47
3 files changed, 8 insertions, 183 deletions
diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp
index bc0a97f97..bf5868925 100644
--- a/src/webenginewidgets/api/qwebenginepage.cpp
+++ b/src/webenginewidgets/api/qwebenginepage.cpp
@@ -79,106 +79,6 @@ static QWebEnginePage::WebWindowType toWindowType(WebContentsAdapterClient::Wind
}
}
-CallbackDirectory::~CallbackDirectory()
-{
- // "Cancel" pending callbacks by calling them with an invalid value.
- // This guarantees that each callback is called exactly once.
- Q_FOREACH (const CallbackSharedDataPointer &sharedPtr, m_callbackMap) {
- switch (sharedPtr.type) {
- case CallbackSharedDataPointer::Variant:
- (*sharedPtr.variantCallback)(QVariant());
- break;
- case CallbackSharedDataPointer::String:
- (*sharedPtr.stringCallback)(QString());
- break;
- case CallbackSharedDataPointer::Bool:
- (*sharedPtr.boolCallback)(false);
- break;
- default:
- Q_UNREACHABLE();
- }
- }
-}
-
-void CallbackDirectory::registerCallback(quint64 requestId, const QExplicitlySharedDataPointer<VariantCallback> &callback)
-{
- m_callbackMap.insert(requestId, CallbackSharedDataPointer(callback.data()));
-}
-
-void CallbackDirectory::registerCallback(quint64 requestId, const QExplicitlySharedDataPointer<StringCallback> &callback)
-{
- m_callbackMap.insert(requestId, CallbackSharedDataPointer(callback.data()));
-}
-
-void CallbackDirectory::registerCallback(quint64 requestId, const QExplicitlySharedDataPointer<BoolCallback> &callback)
-{
- m_callbackMap.insert(requestId, CallbackSharedDataPointer(callback.data()));
-}
-
-void CallbackDirectory::invoke(quint64 requestId, const QVariant &result)
-{
- CallbackSharedDataPointer sharedPtr = m_callbackMap.take(requestId);
- if (sharedPtr) {
- Q_ASSERT(sharedPtr.type == CallbackSharedDataPointer::Variant);
- (*sharedPtr.variantCallback)(result);
- }
-}
-
-void CallbackDirectory::invoke(quint64 requestId, const QString &result)
-{
- CallbackSharedDataPointer sharedPtr = m_callbackMap.take(requestId);
- if (sharedPtr) {
- Q_ASSERT(sharedPtr.type == CallbackSharedDataPointer::String);
- (*sharedPtr.stringCallback)(result);
- }
-}
-
-void CallbackDirectory::invoke(quint64 requestId, bool result)
-{
- CallbackSharedDataPointer sharedPtr = m_callbackMap.take(requestId);
- if (sharedPtr) {
- Q_ASSERT(sharedPtr.type == CallbackSharedDataPointer::Bool);
- (*sharedPtr.boolCallback)(result);
- }
-}
-
-void CallbackDirectory::CallbackSharedDataPointer::doRef()
-{
- switch (type) {
- case None:
- break;
- case Variant:
- variantCallback->ref.ref();
- break;
- case String:
- stringCallback->ref.ref();
- break;
- case Bool:
- boolCallback->ref.ref();
- break;
- }
-}
-
-void CallbackDirectory::CallbackSharedDataPointer::doDeref()
-{
- switch (type) {
- case None:
- break;
- case Variant:
- if (!variantCallback->ref.deref())
- delete variantCallback;
- break;
- case String:
- if (!stringCallback->ref.deref())
- delete stringCallback;
- break;
- case Bool:
- if (!boolCallback->ref.deref())
- delete boolCallback;
- break;
- }
-}
-
QWebEnginePagePrivate::QWebEnginePagePrivate(QWebEngineProfile *_profile)
: adapter(new WebContentsAdapter)
, history(new QWebEngineHistory(new QWebEngineHistoryPrivate(this)))
@@ -814,12 +714,10 @@ void QWebEnginePage::findText(const QString &subString, FindFlags options, const
Q_D(QWebEnginePage);
if (subString.isEmpty()) {
d->adapter->stopFinding();
- if (resultCallback.d)
- (*resultCallback.d)(false);
+ d->m_callbacks.invokeEmpty(resultCallback);
} else {
quint64 requestId = d->adapter->findText(subString, options & FindCaseSensitively, options & FindBackward);
- if (resultCallback.d)
- d->m_callbacks.registerCallback(requestId, resultCallback.d);
+ d->m_callbacks.registerCallback(requestId, resultCallback);
}
}
@@ -1087,14 +985,14 @@ void QWebEnginePage::toHtml(const QWebEngineCallback<const QString &> &resultCal
{
Q_D(const QWebEnginePage);
quint64 requestId = d->adapter->fetchDocumentMarkup();
- d->m_callbacks.registerCallback(requestId, resultCallback.d);
+ d->m_callbacks.registerCallback(requestId, resultCallback);
}
void QWebEnginePage::toPlainText(const QWebEngineCallback<const QString &> &resultCallback) const
{
Q_D(const QWebEnginePage);
quint64 requestId = d->adapter->fetchDocumentInnerText();
- d->m_callbacks.registerCallback(requestId, resultCallback.d);
+ d->m_callbacks.registerCallback(requestId, resultCallback);
}
void QWebEnginePage::setHtml(const QString &html, const QUrl &baseUrl)
@@ -1161,7 +1059,7 @@ void QWebEnginePage::runJavaScript(const QString& scriptSource, const QWebEngine
{
Q_D(QWebEnginePage);
quint64 requestId = d->adapter->runJavaScriptCallbackResult(scriptSource);
- d->m_callbacks.registerCallback(requestId, resultCallback.d);
+ d->m_callbacks.registerCallback(requestId, resultCallback);
}
/*!
diff --git a/src/webenginewidgets/api/qwebenginepage.h b/src/webenginewidgets/api/qwebenginepage.h
index 4281a70a0..16b33a196 100644
--- a/src/webenginewidgets/api/qwebenginepage.h
+++ b/src/webenginewidgets/api/qwebenginepage.h
@@ -39,6 +39,7 @@
#include <QtWebEngineWidgets/qtwebenginewidgetsglobal.h>
#include <QtWebEngineWidgets/qwebenginecertificateerror.h>
+#include <QtWebEngineCore/qwebenginecallback.h>
#include <QtCore/qobject.h>
#include <QtCore/qurl.h>
@@ -56,37 +57,6 @@ class QWebEngineProfile;
class QWebEngineScriptCollection;
class QWebEngineSettings;
-namespace QtWebEnginePrivate {
-
-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;
-};
-
-} // namespace QtWebEnginePrivate
-
-template <typename T>
-class QWebEngineCallback {
-public:
- template <typename F>
- QWebEngineCallback(F f) : d(new QtWebEnginePrivate::QWebEngineCallbackPrivate<T, F>(f)) { }
- QWebEngineCallback() { }
-private:
- QExplicitlySharedDataPointer<QtWebEnginePrivate::QWebEngineCallbackPrivateBase<T> > d;
- friend class QWebEnginePage;
-};
-
class QWEBENGINEWIDGETS_EXPORT QWebEnginePage : public QObject {
Q_OBJECT
Q_PROPERTY(QString selectedText READ selectedText)
diff --git a/src/webenginewidgets/api/qwebenginepage_p.h b/src/webenginewidgets/api/qwebenginepage_p.h
index 62ca16415..a5ede1a8e 100644
--- a/src/webenginewidgets/api/qwebenginepage_p.h
+++ b/src/webenginewidgets/api/qwebenginepage_p.h
@@ -39,10 +39,10 @@
#include "qwebenginepage.h"
+#include "qwebenginecallback_p.h"
#include "qwebenginescriptcollection.h"
#include "web_contents_adapter_client.h"
#include <QtCore/qcompilerdetection.h>
-#include <QSharedData>
namespace QtWebEngineCore {
class RenderWidgetHostViewQtDelegate;
@@ -56,49 +56,6 @@ class QWebEngineProfile;
class QWebEngineSettings;
class QWebEngineView;
-class CallbackDirectory {
-public:
- typedef QtWebEnginePrivate::QWebEngineCallbackPrivateBase<const QVariant&> VariantCallback;
- typedef QtWebEnginePrivate::QWebEngineCallbackPrivateBase<const QString&> StringCallback;
- typedef QtWebEnginePrivate::QWebEngineCallbackPrivateBase<bool> BoolCallback;
-
- ~CallbackDirectory();
- void registerCallback(quint64 requestId, const QExplicitlySharedDataPointer<VariantCallback> &callback);
- void registerCallback(quint64 requestId, const QExplicitlySharedDataPointer<StringCallback> &callback);
- void registerCallback(quint64 requestId, const QExplicitlySharedDataPointer<BoolCallback> &callback);
- void invoke(quint64 requestId, const QVariant &result);
- void invoke(quint64 requestId, const QString &result);
- void invoke(quint64 requestId, bool result);
-
-private:
- struct CallbackSharedDataPointer {
- enum {
- None,
- Variant,
- String,
- Bool
- } type;
- union {
- VariantCallback *variantCallback;
- StringCallback *stringCallback;
- BoolCallback *boolCallback;
- };
- CallbackSharedDataPointer() : type(None) { }
- CallbackSharedDataPointer(VariantCallback *callback) : type(Variant), variantCallback(callback) { callback->ref.ref(); }
- CallbackSharedDataPointer(StringCallback *callback) : type(String), stringCallback(callback) { callback->ref.ref(); }
- CallbackSharedDataPointer(BoolCallback *callback) : type(Bool), boolCallback(callback) { callback->ref.ref(); }
- CallbackSharedDataPointer(const CallbackSharedDataPointer &other) : type(other.type), variantCallback(other.variantCallback) { doRef(); }
- ~CallbackSharedDataPointer() { doDeref(); }
- operator bool () const { return type != None; }
-
- private:
- void doRef();
- void doDeref();
- };
-
- QHash<quint64, CallbackSharedDataPointer> m_callbackMap;
-};
-
class QWebEnginePagePrivate : public QtWebEngineCore::WebContentsAdapterClient
{
public:
@@ -171,7 +128,7 @@ public:
bool isLoading;
QWebEngineScriptCollection scriptCollection;
- mutable CallbackDirectory m_callbacks;
+ mutable QtWebEngineCore::CallbackDirectory m_callbacks;
mutable QAction *actions[QWebEnginePage::WebActionCount];
};