fix: crash when creating interface for speculative frame (#33919)

This commit is contained in:
Samuel Maddock 2022-05-19 14:34:58 -04:00 committed by GitHub
parent 17c8ec765b
commit 5ff94e7f2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 21 deletions

View file

@ -1505,7 +1505,7 @@ void WebContents::HandleNewRenderFrame(
auto* web_frame = WebFrameMain::FromRenderFrameHost(render_frame_host);
if (web_frame)
web_frame->Connect();
web_frame->MaybeSetupMojoConnection();
}
void WebContents::OnBackgroundColorChanged() {

View file

@ -96,13 +96,15 @@ void WebFrameMain::Destroyed() {
void WebFrameMain::MarkRenderFrameDisposed() {
render_frame_ = nullptr;
render_frame_disposed_ = true;
TeardownMojoConnection();
}
void WebFrameMain::UpdateRenderFrameHost(content::RenderFrameHost* rfh) {
// Should only be called when swapping frames.
render_frame_disposed_ = false;
render_frame_ = rfh;
renderer_api_.reset();
TeardownMojoConnection();
MaybeSetupMojoConnection();
}
bool WebFrameMain::CheckRenderFrame() const {
@ -181,20 +183,30 @@ void WebFrameMain::Send(v8::Isolate* isolate,
}
const mojo::Remote<mojom::ElectronRenderer>& WebFrameMain::GetRendererApi() {
if (!renderer_api_) {
pending_receiver_ = renderer_api_.BindNewPipeAndPassReceiver();
if (render_frame_->IsRenderFrameCreated()) {
render_frame_->GetRemoteInterfaces()->GetInterface(
std::move(pending_receiver_));
}
renderer_api_.set_disconnect_handler(base::BindOnce(
&WebFrameMain::OnRendererConnectionError, weak_factory_.GetWeakPtr()));
}
MaybeSetupMojoConnection();
return renderer_api_;
}
void WebFrameMain::OnRendererConnectionError() {
void WebFrameMain::MaybeSetupMojoConnection() {
if (!renderer_api_) {
pending_receiver_ = renderer_api_.BindNewPipeAndPassReceiver();
renderer_api_.set_disconnect_handler(base::BindOnce(
&WebFrameMain::OnRendererConnectionError, weak_factory_.GetWeakPtr()));
}
// Wait for RenderFrame to be created in renderer before accessing remote.
if (pending_receiver_ && render_frame_->IsRenderFrameCreated()) {
render_frame_->GetRemoteInterfaces()->GetInterface(
std::move(pending_receiver_));
}
}
void WebFrameMain::TeardownMojoConnection() {
renderer_api_.reset();
pending_receiver_.reset();
}
void WebFrameMain::OnRendererConnectionError() {
TeardownMojoConnection();
}
void WebFrameMain::PostMessage(v8::Isolate* isolate,
@ -314,13 +326,6 @@ std::vector<content::RenderFrameHost*> WebFrameMain::FramesInSubtree() const {
return frame_hosts;
}
void WebFrameMain::Connect() {
if (pending_receiver_) {
render_frame_->GetRemoteInterfaces()->GetInterface(
std::move(pending_receiver_));
}
}
void WebFrameMain::DOMContentLoaded() {
Emit("dom-ready");
}

View file

@ -82,6 +82,9 @@ class WebFrameMain : public gin::Wrappable<WebFrameMain>,
void UpdateRenderFrameHost(content::RenderFrameHost* rfh);
const mojo::Remote<mojom::ElectronRenderer>& GetRendererApi();
void MaybeSetupMojoConnection();
void TeardownMojoConnection();
void OnRendererConnectionError();
// WebFrameMain can outlive its RenderFrameHost pointer so we need to check
// whether its been disposed of prior to accessing it.
@ -112,8 +115,6 @@ class WebFrameMain : public gin::Wrappable<WebFrameMain>,
std::vector<content::RenderFrameHost*> Frames() const;
std::vector<content::RenderFrameHost*> FramesInSubtree() const;
void OnRendererConnectionError();
void Connect();
void DOMContentLoaded();
mojo::Remote<mojom::ElectronRenderer> renderer_api_;