From 1f17c9b3307b2ec409b3db22e8bd45fc623ae0cd Mon Sep 17 00:00:00 2001 From: Jocelyn Turcotte Date: Mon, 10 Feb 2014 12:14:11 +0100 Subject: Refactor the way callbacks are stored With the upcoming addition of a new type of callback result, this patch allows storing multiple callback types in the same QHash instead or requiring a different hash table just to please the type system. This does so by managing the ref-counted callback pointers directly instead of relying on a templated QExplicitlySharedDataPointer that requires a different type for each different callback pointer type. The ref-counting, construction and destruction is managed through a run-time type enum. Change-Id: I90ab2e1efc0c9703fc5b6ef57b38204ac8eea828 Reviewed-by: Simon Hausmann --- src/webenginewidgets/api/qwebenginepage_p.h | 42 ++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'src/webenginewidgets/api/qwebenginepage_p.h') diff --git a/src/webenginewidgets/api/qwebenginepage_p.h b/src/webenginewidgets/api/qwebenginepage_p.h index 86605c78d..9a4dc57fd 100644 --- a/src/webenginewidgets/api/qwebenginepage_p.h +++ b/src/webenginewidgets/api/qwebenginepage_p.h @@ -58,6 +58,43 @@ class QWebEngineHistory; class QWebEnginePage; class QWebEngineView; +class CallbackDirectory { +public: + typedef QtWebEnginePrivate::QWebEngineCallbackPrivateBase VariantCallback; + typedef QtWebEnginePrivate::QWebEngineCallbackPrivateBase StringCallback; + + ~CallbackDirectory(); + void registerCallback(quint64 requestId, const QExplicitlySharedDataPointer &callback); + void registerCallback(quint64 requestId, const QExplicitlySharedDataPointer &callback); + void invoke(quint64 requestId, const QVariant &result); + void invoke(quint64 requestId, const QString &result); + +private: + struct CallbackSharedDataPointer { + enum { + None, + Variant, + String + } type; + union { + VariantCallback *variantCallback; + StringCallback *stringCallback; + }; + CallbackSharedDataPointer() : type(None) { } + CallbackSharedDataPointer(VariantCallback *callback) : type(Variant), variantCallback(callback) { callback->ref.ref(); } + CallbackSharedDataPointer(StringCallback *callback) : type(String), stringCallback(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 m_callbackMap; +}; + class QWebEnginePagePrivate : public QObjectPrivate, public WebContentsAdapterClient { public: @@ -104,10 +141,7 @@ public: WebEngineContextMenuData m_menuData; QPointer m_rwhvDelegate; - typedef QtWebEnginePrivate::QWebEngineCallbackPrivateBase VariantCallback; - typedef QtWebEnginePrivate::QWebEngineCallbackPrivateBase StringCallback; - mutable QHash > m_variantCallbacks; - mutable QHash > m_stringCallbacks; + mutable CallbackDirectory m_callbacks; }; QT_END_NAMESPACE -- cgit v1.2.3