electron/shell/browser/electron_web_contents_utility_handler_impl.cc
Samuel Attard e07c2b84d7
fix: ensure ElectronBrowser mojo service is only bound to appropriate render frames (#33323)
* fix: ensure ElectronBrowser mojo service is only bound to authorized render frames

Notes: no-notes

* refactor: extract electron API IPC to its own mojo interface

* fix: just check main frame not primary main frame
2022-03-18 19:50:05 -07:00

83 lines
2.8 KiB
C++

// Copyright (c) 2022 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/electron_web_contents_utility_handler_impl.h"
#include <utility>
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
namespace electron {
ElectronWebContentsUtilityHandlerImpl::ElectronWebContentsUtilityHandlerImpl(
content::RenderFrameHost* frame_host,
mojo::PendingAssociatedReceiver<mojom::ElectronWebContentsUtility> receiver)
: render_process_id_(frame_host->GetProcess()->GetID()),
render_frame_id_(frame_host->GetRoutingID()) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(frame_host);
DCHECK(web_contents);
content::WebContentsObserver::Observe(web_contents);
receiver_.Bind(std::move(receiver));
receiver_.set_disconnect_handler(base::BindOnce(
&ElectronWebContentsUtilityHandlerImpl::OnConnectionError, GetWeakPtr()));
}
ElectronWebContentsUtilityHandlerImpl::
~ElectronWebContentsUtilityHandlerImpl() = default;
void ElectronWebContentsUtilityHandlerImpl::WebContentsDestroyed() {
delete this;
}
void ElectronWebContentsUtilityHandlerImpl::OnConnectionError() {
delete this;
}
void ElectronWebContentsUtilityHandlerImpl::OnFirstNonEmptyLayout() {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->OnFirstNonEmptyLayout(GetRenderFrameHost());
}
}
void ElectronWebContentsUtilityHandlerImpl::UpdateDraggableRegions(
std::vector<mojom::DraggableRegionPtr> regions) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->UpdateDraggableRegions(std::move(regions));
}
}
void ElectronWebContentsUtilityHandlerImpl::SetTemporaryZoomLevel(
double level) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->SetTemporaryZoomLevel(level);
}
}
void ElectronWebContentsUtilityHandlerImpl::DoGetZoomLevel(
DoGetZoomLevelCallback callback) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents) {
api_web_contents->DoGetZoomLevel(std::move(callback));
}
}
content::RenderFrameHost*
ElectronWebContentsUtilityHandlerImpl::GetRenderFrameHost() {
return content::RenderFrameHost::FromID(render_process_id_, render_frame_id_);
}
// static
void ElectronWebContentsUtilityHandlerImpl::Create(
content::RenderFrameHost* frame_host,
mojo::PendingAssociatedReceiver<mojom::ElectronWebContentsUtility>
receiver) {
new ElectronWebContentsUtilityHandlerImpl(frame_host, std::move(receiver));
}
} // namespace electron