diff options
author | Jocelyn Turcotte <jocelyn.turcotte@digia.com> | 2014-01-15 18:22:24 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-01-22 08:45:09 +0100 |
commit | 6d760436bc0d7081d5bdb50e4141171b6ba940ee (patch) | |
tree | 9f17b8122a09b89a818373d9a269072d984ca0a6 /src/core/web_contents_adapter.cpp | |
parent | e6b846f3800eed35d31a45122f7f8c3215dc38ef (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/core/web_contents_adapter.cpp')
-rw-r--r-- | src/core/web_contents_adapter.cpp | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index 108506b06..56b56ce33 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -143,10 +143,9 @@ static QVariant fromJSValue(const base::Value *result) return ret; } -static void callbackOnEvaluateJS(JSCallbackBase *callback, const base::Value *result) +static void callbackOnEvaluateJS(WebContentsAdapterClient *adapterClient, quint64 requestId, const base::Value *result) { - callback->call(fromJSValue(result)); - delete callback; + adapterClient->didRunJavaScript(fromJSValue(result), requestId); } static QStringList listRecursively(const QDir& dir) { @@ -169,11 +168,13 @@ public: scoped_ptr<content::WebContents> webContents; scoped_ptr<WebContentsDelegateQt> webContentsDelegate; WebContentsAdapterClient *adapterClient; + quint64 lastRequestId; }; WebContentsAdapterPrivate::WebContentsAdapterPrivate(WebContentsAdapterClient::RenderingMode renderingMode) // This has to be the first thing we create, and the last we destroy. : engineContext(WebEngineContext::currentOrCreate(renderingMode)) + , lastRequestId(0) { } @@ -372,17 +373,22 @@ void WebContentsAdapter::enableInspector(bool enable) ContentBrowserClientQt::Get()->enableInspector(enable); } -void WebContentsAdapter::runJavaScript(const QString &javaScript, const QString &xPath, JSCallbackBase *func) +void WebContentsAdapter::runJavaScript(const QString &javaScript, const QString &xPath) { Q_D(WebContentsAdapter); content::RenderViewHost *rvh = d->webContents->GetRenderViewHost(); Q_ASSERT(rvh); - if (!func) - rvh->ExecuteJavascriptInWebFrame(toString16(xPath), toString16(javaScript)); - else { - content::RenderViewHost::JavascriptResultCallback callback = base::Bind(&callbackOnEvaluateJS, func); - rvh->ExecuteJavascriptInWebFrameCallbackResult(toString16(xPath), toString16(javaScript), callback); - } + rvh->ExecuteJavascriptInWebFrame(toString16(xPath), toString16(javaScript)); +} + +quint64 WebContentsAdapter::runJavaScriptCallbackResult(const QString &javaScript, const QString &xPath) +{ + Q_D(WebContentsAdapter); + content::RenderViewHost *rvh = d->webContents->GetRenderViewHost(); + Q_ASSERT(rvh); + content::RenderViewHost::JavascriptResultCallback callback = base::Bind(&callbackOnEvaluateJS, d->adapterClient, ++d->lastRequestId); + rvh->ExecuteJavascriptInWebFrameCallbackResult(toString16(xPath), toString16(javaScript), callback); + return d->lastRequestId; } void WebContentsAdapter::dpiScaleChanged() |