From df3681dc6c401f3cebb6e767ef8b8ca4e1a8260b Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Tue, 26 Jun 2018 13:40:24 +0200 Subject: 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 --- .../web_channel_ipc_transport_host.cpp | 64 ++++++++++++++-------- .../renderer_host/web_channel_ipc_transport_host.h | 25 ++++++--- 2 files changed, 57 insertions(+), 32 deletions(-) (limited to 'src/core/renderer_host') 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 @@ -72,13 +75,21 @@ inline QDebug operator<<(QDebug stream, const base::Optional &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(rawData, rawData + size), *m_worldId)); + webChannelTransport->DispatchWebChannelMessage(std::vector(rawData, rawData + size), m_worldId); } -void WebChannelIPCTransportHost::setWorldId(base::Optional worldId) +void WebChannelIPCTransportHost::setWorldId(uint32_t worldId) { if (m_worldId == worldId) return; @@ -100,23 +113,41 @@ void WebChannelIPCTransportHost::setWorldId(base::Optional worldId) m_worldId = worldId; } -void WebChannelIPCTransportHost::setWorldId(content::RenderFrameHost *frame, base::Optional 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 &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 &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(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 @@ -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(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 worldId); - void setWorldId(content::RenderFrameHost *frame, base::Optional worldId); + void setWorldId(content::RenderFrameHost *frame, uint32_t worldId); + void resetWorldId(); void onWebChannelMessage(const std::vector &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 &binaryJson) override; // Empty only during construction/destruction. Synchronized to all the // WebChannelIPCTransports/RenderFrames in the observed WebContents. - base::Optional m_worldId; + uint32_t m_worldId; + content::WebContentsFrameBindingSet m_binding; }; } // namespace -- cgit v1.2.3