summaryrefslogtreecommitdiffstats
path: root/src/core/renderer_host
diff options
context:
space:
mode:
authorMichal Klocek <michal.klocek@qt.io>2018-06-26 13:40:24 +0200
committerMichal Klocek <michal.klocek@qt.io>2018-07-19 07:40:53 +0000
commitdf3681dc6c401f3cebb6e767ef8b8ca4e1a8260b (patch)
treeac6f08476923c5426853b50bf981e6e019535965 /src/core/renderer_host
parentd82ef0ce217e967ac91f067377ef82803e0eeb5d (diff)
Migrate WebChannel IPC to mojo
Mojoifies: * WebChannelIPCTransport_SetWorldId * WebChannelIPCTransport_Message * WebChannelIPCTransportHost_SendMessage This change is just the simple rewrite of IPC in web channel transport, however ultimate goal here is to use mojo directly in javascript land. Change-Id: Ifcf84659b1d48d99cc4e87849b8a258303e8fedc Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Diffstat (limited to 'src/core/renderer_host')
-rw-r--r--src/core/renderer_host/web_channel_ipc_transport_host.cpp64
-rw-r--r--src/core/renderer_host/web_channel_ipc_transport_host.h25
2 files changed, 57 insertions, 32 deletions
diff --git a/src/core/renderer_host/web_channel_ipc_transport_host.cpp b/src/core/renderer_host/web_channel_ipc_transport_host.cpp
index d99dfde97..e1929e4cd 100644
--- a/src/core/renderer_host/web_channel_ipc_transport_host.cpp
+++ b/src/core/renderer_host/web_channel_ipc_transport_host.cpp
@@ -42,7 +42,10 @@
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
-
+#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
+#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
+#include "services/service_manager/public/cpp/interface_provider.h"
+#include "qtwebengine/browser/qtwebchannel.mojom.h"
#include "common/qt_messages.h"
#include <QJsonDocument>
@@ -72,13 +75,21 @@ inline QDebug operator<<(QDebug stream, const base::Optional<T> &opt)
WebChannelIPCTransportHost::WebChannelIPCTransportHost(content::WebContents *contents, uint worldId, QObject *parent)
: QWebChannelAbstractTransport(parent)
, content::WebContentsObserver(contents)
+ , m_worldId(worldId)
+ , m_binding(contents, this)
{
- setWorldId(worldId);
+ for (content::RenderFrameHost *frame : contents->GetAllFrames())
+ setWorldId(frame, worldId);
}
WebChannelIPCTransportHost::~WebChannelIPCTransportHost()
{
- setWorldId(base::nullopt);
+ resetWorldId();
+}
+
+uint WebChannelIPCTransportHost::worldId() const
+{
+ return m_worldId;
}
void WebChannelIPCTransportHost::sendMessage(const QJsonObject &message)
@@ -87,11 +98,13 @@ void WebChannelIPCTransportHost::sendMessage(const QJsonObject &message)
int size = 0;
const char *rawData = doc.rawData(&size);
content::RenderFrameHost *frame = web_contents()->GetMainFrame();
+ qtwebchannel::mojom::WebChannelTransportRenderAssociatedPtr webChannelTransport;
+ frame->GetRemoteAssociatedInterfaces()->GetInterface(&webChannelTransport);
qCDebug(log).nospace() << "sending webchannel message to " << frame << ": " << doc;
- frame->Send(new WebChannelIPCTransport_Message(frame->GetRoutingID(), std::vector<char>(rawData, rawData + size), *m_worldId));
+ webChannelTransport->DispatchWebChannelMessage(std::vector<uint8_t>(rawData, rawData + size), m_worldId);
}
-void WebChannelIPCTransportHost::setWorldId(base::Optional<uint> worldId)
+void WebChannelIPCTransportHost::setWorldId(uint32_t worldId)
{
if (m_worldId == worldId)
return;
@@ -100,23 +113,41 @@ void WebChannelIPCTransportHost::setWorldId(base::Optional<uint> worldId)
m_worldId = worldId;
}
-void WebChannelIPCTransportHost::setWorldId(content::RenderFrameHost *frame, base::Optional<uint> worldId)
+void WebChannelIPCTransportHost::setWorldId(content::RenderFrameHost *frame, uint32_t worldId)
{
if (!frame->IsRenderFrameLive())
return;
qCDebug(log).nospace() << "sending setWorldId(" << worldId << ") message to " << frame;
- frame->Send(new WebChannelIPCTransport_SetWorldId(frame->GetRoutingID(), worldId));
+ qtwebchannel::mojom::WebChannelTransportRenderAssociatedPtr webChannelTransport;
+ frame->GetRemoteAssociatedInterfaces()->GetInterface(&webChannelTransport);
+ webChannelTransport->SetWorldId(worldId);
}
-void WebChannelIPCTransportHost::onWebChannelMessage(const std::vector<char> &message)
+void WebChannelIPCTransportHost::resetWorldId()
+{
+ for (content::RenderFrameHost *frame : web_contents()->GetAllFrames()) {
+ if (!frame->IsRenderFrameLive())
+ return;
+ qtwebchannel::mojom::WebChannelTransportRenderAssociatedPtr webChannelTransport;
+ frame->GetRemoteAssociatedInterfaces()->GetInterface(&webChannelTransport);
+ webChannelTransport->ResetWorldId();
+ }
+}
+
+void WebChannelIPCTransportHost::DispatchWebChannelMessage(const std::vector<uint8_t> &binaryJson)
{
content::RenderFrameHost *frame = web_contents()->GetMainFrame();
+ if (m_binding.GetCurrentTargetFrame() != frame) {
+ return;
+ }
+
QJsonDocument doc;
// QJsonDocument::fromRawData does not check the length before it starts
// parsing the QJsonPrivate::Header and QJsonPrivate::Base structures.
- if (message.size() >= sizeof(QJsonPrivate::Header) + sizeof(QJsonPrivate::Base))
- doc = QJsonDocument::fromRawData(message.data(), message.size());
+ if (binaryJson.size() >= sizeof(QJsonPrivate::Header) + sizeof(QJsonPrivate::Base))
+ doc = QJsonDocument::fromRawData(reinterpret_cast<const char *>(binaryJson.data()),
+ binaryJson.size());
if (!doc.isObject()) {
qCCritical(log).nospace() << "received invalid webchannel message from " << frame;
@@ -132,17 +163,4 @@ void WebChannelIPCTransportHost::RenderFrameCreated(content::RenderFrameHost *fr
setWorldId(frame, m_worldId);
}
-bool WebChannelIPCTransportHost::OnMessageReceived(const IPC::Message& message, content::RenderFrameHost *receiver)
-{
- if (receiver != web_contents()->GetMainFrame())
- return false;
-
- bool handled = true;
- IPC_BEGIN_MESSAGE_MAP(WebChannelIPCTransportHost, message)
- IPC_MESSAGE_HANDLER(WebChannelIPCTransportHost_SendMessage, onWebChannelMessage)
- IPC_MESSAGE_UNHANDLED(handled = false)
- IPC_END_MESSAGE_MAP()
- return handled;
-}
-
} // namespace QtWebEngineCore
diff --git a/src/core/renderer_host/web_channel_ipc_transport_host.h b/src/core/renderer_host/web_channel_ipc_transport_host.h
index 3a814a794..94891f25f 100644
--- a/src/core/renderer_host/web_channel_ipc_transport_host.h
+++ b/src/core/renderer_host/web_channel_ipc_transport_host.h
@@ -43,6 +43,9 @@
#include "qtwebenginecoreglobal.h"
#include "content/public/browser/web_contents_observer.h"
+#include "services/service_manager/public/cpp/binder_registry.h"
+#include "content/public/browser/web_contents_binding_set.h"
+#include "qtwebengine/browser/qtwebchannel.mojom.h"
#include <QWebChannelAbstractTransport>
@@ -51,29 +54,33 @@ QT_FORWARD_DECLARE_CLASS(QString)
namespace QtWebEngineCore {
class WebChannelIPCTransportHost : public QWebChannelAbstractTransport
- , private content::WebContentsObserver {
+ , private content::WebContentsObserver
+ , qtwebchannel::mojom::WebChannelTransportHost {
public:
- WebChannelIPCTransportHost(content::WebContents *webContents, uint worldId = 0, QObject *parent = nullptr);
- virtual ~WebChannelIPCTransportHost();
+ WebChannelIPCTransportHost(content::WebContents *webContents, uint32_t worldId = 0, QObject *parent = nullptr);
+ ~WebChannelIPCTransportHost() override;
- void setWorldId(uint worldId) { setWorldId(base::Optional<uint>(worldId)); }
- uint worldId() const { return *m_worldId; }
+ void setWorldId(uint32_t worldId);
+ uint32_t worldId() const;
// QWebChannelAbstractTransport
void sendMessage(const QJsonObject &message) override;
private:
- void setWorldId(base::Optional<uint> worldId);
- void setWorldId(content::RenderFrameHost *frame, base::Optional<uint> worldId);
+ void setWorldId(content::RenderFrameHost *frame, uint32_t worldId);
+ void resetWorldId();
void onWebChannelMessage(const std::vector<char> &message);
// WebContentsObserver
void RenderFrameCreated(content::RenderFrameHost *frame) override;
- bool OnMessageReceived(const IPC::Message& message, content::RenderFrameHost *receiver) override;
+
+ // qtwebchannel::mojom::WebChannelTransportHost
+ void DispatchWebChannelMessage(const std::vector<uint8_t> &binaryJson) override;
// Empty only during construction/destruction. Synchronized to all the
// WebChannelIPCTransports/RenderFrames in the observed WebContents.
- base::Optional<uint> m_worldId;
+ uint32_t m_worldId;
+ content::WebContentsFrameBindingSet<qtwebchannel::mojom::WebChannelTransportHost> m_binding;
};
} // namespace