diff options
author | Michal Klocek <michal.klocek@qt.io> | 2020-10-15 12:14:36 +0200 |
---|---|---|
committer | Michal Klocek <michal.klocek@qt.io> | 2020-12-11 20:51:38 +0100 |
commit | c175a7fbea6b707de9544ff02eb7351600a3a71b (patch) | |
tree | 3fad2a3e26794196a8f1b5bb2838c184a4a47cca /src/core/renderer_host | |
parent | 62db6a3b4ac0adfd7fe33d214b79ef9327ad9524 (diff) |
Cache mojo interface bindings for WebChannelIPCTransport
Change-Id: Ica6b794e16aecc60dc2c39d31750acc6b25410df
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/core/renderer_host')
-rw-r--r-- | src/core/renderer_host/web_channel_ipc_transport_host.cpp | 34 | ||||
-rw-r--r-- | src/core/renderer_host/web_channel_ipc_transport_host.h | 8 |
2 files changed, 33 insertions, 9 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 9ed3749ad..087e0e83b 100644 --- a/src/core/renderer_host/web_channel_ipc_transport_host.cpp +++ b/src/core/renderer_host/web_channel_ipc_transport_host.cpp @@ -96,10 +96,9 @@ void WebChannelIPCTransportHost::sendMessage(const QJsonObject &message) int size = 0; const char *rawData = doc.rawData(&size); content::RenderFrameHost *frame = web_contents()->GetMainFrame(); - mojo::AssociatedRemote<qtwebchannel::mojom::WebChannelTransportRender> webChannelTransport; - frame->GetRemoteAssociatedInterfaces()->GetInterface(&webChannelTransport); qCDebug(log).nospace() << "sending webchannel message to " << frame << ": " << doc; - webChannelTransport->DispatchWebChannelMessage(std::vector<uint8_t>(rawData, rawData + size), m_worldId); + GetWebChannelIPCTransportRemote(frame)->DispatchWebChannelMessage( + std::vector<uint8_t>(rawData, rawData + size), m_worldId); } void WebChannelIPCTransportHost::setWorldId(uint32_t worldId) @@ -116,9 +115,7 @@ void WebChannelIPCTransportHost::setWorldId(content::RenderFrameHost *frame, uin if (!frame->IsRenderFrameLive()) return; qCDebug(log).nospace() << "sending setWorldId(" << worldId << ") message to " << frame; - mojo::AssociatedRemote<qtwebchannel::mojom::WebChannelTransportRender> webChannelTransport; - frame->GetRemoteAssociatedInterfaces()->GetInterface(&webChannelTransport); - webChannelTransport->SetWorldId(worldId); + GetWebChannelIPCTransportRemote(frame)->SetWorldId(worldId); } void WebChannelIPCTransportHost::resetWorldId() @@ -126,9 +123,7 @@ void WebChannelIPCTransportHost::resetWorldId() for (content::RenderFrameHost *frame : web_contents()->GetAllFrames()) { if (!frame->IsRenderFrameLive()) return; - mojo::AssociatedRemote<qtwebchannel::mojom::WebChannelTransportRender> webChannelTransport; - frame->GetRemoteAssociatedInterfaces()->GetInterface(&webChannelTransport); - webChannelTransport->ResetWorldId(); + GetWebChannelIPCTransportRemote(frame)->ResetWorldId(); } } @@ -156,4 +151,25 @@ void WebChannelIPCTransportHost::RenderFrameCreated(content::RenderFrameHost *fr setWorldId(frame, m_worldId); } +void WebChannelIPCTransportHost::RenderFrameDeleted(content::RenderFrameHost *rfh) +{ + m_renderFrames.erase(rfh); +} + +const mojo::AssociatedRemote<qtwebchannel::mojom::WebChannelTransportRender> & +WebChannelIPCTransportHost::GetWebChannelIPCTransportRemote(content::RenderFrameHost *rfh) +{ + auto it = m_renderFrames.find(rfh); + if (it == m_renderFrames.end()) { + mojo::AssociatedRemote<qtwebchannel::mojom::WebChannelTransportRender> remote; + rfh->GetRemoteAssociatedInterfaces()->GetInterface(remote.BindNewEndpointAndPassReceiver()); + it = m_renderFrames.insert(std::make_pair(rfh, std::move(remote))).first; + } else if (it->second.is_bound() && !it->second.is_connected()) { + it->second.reset(); + rfh->GetRemoteAssociatedInterfaces()->GetInterface(&it->second); + } + + return it->second; +} + } // 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 a1575355a..d06483ee6 100644 --- a/src/core/renderer_host/web_channel_ipc_transport_host.h +++ b/src/core/renderer_host/web_channel_ipc_transport_host.h @@ -47,6 +47,7 @@ #include "qtwebengine/browser/qtwebchannel.mojom.h" #include <QWebChannelAbstractTransport> +#include <map> QT_FORWARD_DECLARE_CLASS(QString) @@ -72,8 +73,12 @@ private: void resetWorldId(); void onWebChannelMessage(const std::vector<char> &message); + const mojo::AssociatedRemote<qtwebchannel::mojom::WebChannelTransportRender> & + GetWebChannelIPCTransportRemote(content::RenderFrameHost *rfh); + // WebContentsObserver void RenderFrameCreated(content::RenderFrameHost *frame) override; + void RenderFrameDeleted(content::RenderFrameHost *render_frame_host) override; // qtwebchannel::mojom::WebChannelTransportHost void DispatchWebChannelMessage(const std::vector<uint8_t> &binaryJson) override; @@ -82,6 +87,9 @@ private: // WebChannelIPCTransports/RenderFrames in the observed WebContents. uint32_t m_worldId; content::WebContentsFrameReceiverSet<qtwebchannel::mojom::WebChannelTransportHost> m_receiver; + std::map<content::RenderFrameHost *, + mojo::AssociatedRemote<qtwebchannel::mojom::WebChannelTransportRender>> + m_renderFrames; }; } // namespace |