2015-06-25 00:28:13 -06:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 14:46:59 -06:00
|
|
|
#include "shell/browser/web_view_guest_delegate.h"
|
2015-06-25 00:28:13 -06:00
|
|
|
|
2018-09-14 18:16:22 -06:00
|
|
|
#include <memory>
|
|
|
|
|
2019-03-04 22:08:55 -07:00
|
|
|
#include "content/browser/web_contents/web_contents_impl.h" // nogncheck
|
2016-07-13 18:19:28 -06:00
|
|
|
#include "content/public/browser/navigation_handle.h"
|
2015-08-05 05:50:23 -06:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2018-08-16 16:57:40 -06:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2015-06-25 00:28:13 -06:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
2016-03-08 07:28:53 -07:00
|
|
|
#include "content/public/browser/render_widget_host.h"
|
2015-06-25 00:28:13 -06:00
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
2020-02-04 13:19:40 -07:00
|
|
|
#include "shell/browser/api/electron_api_web_contents.h"
|
2019-10-18 13:57:34 -06:00
|
|
|
#include "third_party/blink/public/common/page/page_zoom.h"
|
2015-06-25 00:28:13 -06:00
|
|
|
|
2019-06-19 15:23:04 -06:00
|
|
|
namespace electron {
|
2015-06-25 00:28:13 -06:00
|
|
|
|
2018-08-16 16:57:40 -06:00
|
|
|
WebViewGuestDelegate::WebViewGuestDelegate(content::WebContents* embedder,
|
|
|
|
api::WebContents* api_web_contents)
|
|
|
|
: embedder_web_contents_(embedder), api_web_contents_(api_web_contents) {}
|
2015-09-07 17:54:07 -06:00
|
|
|
|
2018-08-16 16:57:40 -06:00
|
|
|
WebViewGuestDelegate::~WebViewGuestDelegate() {
|
2017-11-30 08:09:05 -07:00
|
|
|
ResetZoomController();
|
2015-06-25 00:28:13 -06:00
|
|
|
}
|
|
|
|
|
2018-08-16 16:57:40 -06:00
|
|
|
void WebViewGuestDelegate::AttachToIframe(
|
|
|
|
content::WebContents* embedder_web_contents,
|
|
|
|
int embedder_frame_id) {
|
|
|
|
embedder_web_contents_ = embedder_web_contents;
|
2015-06-25 00:28:13 -06:00
|
|
|
|
2018-08-16 16:57:40 -06:00
|
|
|
int embedder_process_id =
|
2022-06-27 14:50:08 -06:00
|
|
|
embedder_web_contents_->GetPrimaryMainFrame()->GetProcess()->GetID();
|
2018-08-16 16:57:40 -06:00
|
|
|
auto* embedder_frame =
|
|
|
|
content::RenderFrameHost::FromID(embedder_process_id, embedder_frame_id);
|
|
|
|
DCHECK_EQ(embedder_web_contents_,
|
|
|
|
content::WebContents::FromRenderFrameHost(embedder_frame));
|
2015-06-25 00:28:13 -06:00
|
|
|
|
2019-01-10 09:24:43 -07:00
|
|
|
content::WebContents* guest_web_contents = api_web_contents_->web_contents();
|
2020-08-12 12:33:58 -06:00
|
|
|
|
2018-08-16 16:57:40 -06:00
|
|
|
// Attach this inner WebContents |guest_web_contents| to the outer
|
|
|
|
// WebContents |embedder_web_contents|. The outer WebContents's
|
|
|
|
// frame |embedder_frame| hosts the inner WebContents.
|
2019-03-06 18:25:13 -07:00
|
|
|
embedder_web_contents_->AttachInnerWebContents(
|
2019-01-10 09:24:43 -07:00
|
|
|
base::WrapUnique<content::WebContents>(guest_web_contents),
|
2022-07-20 05:03:34 -06:00
|
|
|
embedder_frame,
|
|
|
|
/*remote_frame=*/mojo::NullAssociatedRemote(),
|
|
|
|
/*remote_frame_host_receiver=*/mojo::NullAssociatedReceiver(),
|
|
|
|
/*is_full_page=*/false);
|
2017-11-30 08:09:05 -07:00
|
|
|
|
|
|
|
ResetZoomController();
|
|
|
|
|
2017-01-30 04:18:40 -07:00
|
|
|
embedder_zoom_controller_ =
|
|
|
|
WebContentsZoomController::FromWebContents(embedder_web_contents_);
|
|
|
|
embedder_zoom_controller_->AddObserver(this);
|
2018-08-16 16:57:40 -06:00
|
|
|
auto* zoom_controller = api_web_contents_->GetZoomController();
|
2017-01-31 02:15:45 -07:00
|
|
|
zoom_controller->SetEmbedderZoomController(embedder_zoom_controller_);
|
2015-06-25 00:28:13 -06:00
|
|
|
|
2018-08-16 16:57:40 -06:00
|
|
|
api_web_contents_->Emit("did-attach");
|
2015-06-25 00:28:13 -06:00
|
|
|
}
|
|
|
|
|
2020-07-28 14:00:44 -06:00
|
|
|
void WebViewGuestDelegate::WillDestroy() {
|
|
|
|
ResetZoomController();
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:22:09 -06:00
|
|
|
content::WebContents* WebViewGuestDelegate::GetOwnerWebContents() {
|
2018-08-16 16:57:40 -06:00
|
|
|
return embedder_web_contents_;
|
2015-06-25 00:28:13 -06:00
|
|
|
}
|
2017-01-30 04:18:40 -07:00
|
|
|
|
2023-08-14 19:33:30 -06:00
|
|
|
void WebViewGuestDelegate::OnZoomChanged(
|
|
|
|
const WebContentsZoomController::ZoomChangedEventData& data) {
|
|
|
|
if (data.web_contents == GetOwnerWebContents()) {
|
2023-11-03 08:36:25 -06:00
|
|
|
auto* zoom_controller = api_web_contents_->GetZoomController();
|
2023-08-14 19:33:30 -06:00
|
|
|
if (data.temporary) {
|
2023-11-03 08:36:25 -06:00
|
|
|
zoom_controller->SetTemporaryZoomLevel(data.new_zoom_level);
|
2017-01-30 10:06:50 -07:00
|
|
|
} else {
|
2024-07-02 01:51:33 -06:00
|
|
|
if (blink::ZoomValuesEqual(data.new_zoom_level,
|
|
|
|
zoom_controller->GetZoomLevel()))
|
2023-11-03 08:36:25 -06:00
|
|
|
return;
|
|
|
|
zoom_controller->SetZoomLevel(data.new_zoom_level);
|
2017-01-30 10:06:50 -07:00
|
|
|
}
|
2017-01-31 02:15:45 -07:00
|
|
|
// Change the default zoom factor to match the embedders' new zoom level.
|
2024-07-02 01:51:33 -06:00
|
|
|
double zoom_factor = blink::ZoomLevelToZoomFactor(data.new_zoom_level);
|
2023-11-03 08:36:25 -06:00
|
|
|
zoom_controller->SetDefaultZoomFactor(zoom_factor);
|
2017-01-30 04:18:40 -07:00
|
|
|
}
|
|
|
|
}
|
2015-06-25 00:28:13 -06:00
|
|
|
|
2023-08-14 19:33:30 -06:00
|
|
|
void WebViewGuestDelegate::OnZoomControllerDestroyed(
|
|
|
|
WebContentsZoomController* zoom_controller) {
|
2018-08-16 16:57:40 -06:00
|
|
|
ResetZoomController();
|
2015-06-25 00:28:13 -06:00
|
|
|
}
|
|
|
|
|
2017-11-30 08:09:05 -07:00
|
|
|
void WebViewGuestDelegate::ResetZoomController() {
|
|
|
|
if (embedder_zoom_controller_) {
|
|
|
|
embedder_zoom_controller_->RemoveObserver(this);
|
|
|
|
embedder_zoom_controller_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 01:46:37 -06:00
|
|
|
std::unique_ptr<content::WebContents>
|
|
|
|
WebViewGuestDelegate::CreateNewGuestWindow(
|
2017-05-23 14:26:19 -06:00
|
|
|
const content::WebContents::CreateParams& create_params) {
|
2017-05-23 17:06:16 -06:00
|
|
|
// Code below mirrors what content::WebContentsImpl::CreateNewWindow
|
|
|
|
// does for non-guest sources
|
2017-05-23 14:26:19 -06:00
|
|
|
content::WebContents::CreateParams guest_params(create_params);
|
|
|
|
guest_params.context = embedder_web_contents_->GetNativeView();
|
2018-09-14 18:16:22 -06:00
|
|
|
std::unique_ptr<content::WebContents> guest_contents =
|
|
|
|
content::WebContents::Create(guest_params);
|
2021-06-29 19:10:18 -06:00
|
|
|
if (!create_params.opener_suppressed) {
|
|
|
|
auto* guest_contents_impl =
|
|
|
|
static_cast<content::WebContentsImpl*>(guest_contents.release());
|
|
|
|
auto* new_guest_view = guest_contents_impl->GetView();
|
|
|
|
content::RenderWidgetHostView* widget_view =
|
|
|
|
new_guest_view->CreateViewForWidget(
|
|
|
|
guest_contents_impl->GetRenderViewHost()->GetWidget());
|
|
|
|
if (!create_params.initially_hidden)
|
|
|
|
widget_view->Show();
|
2022-09-07 01:46:37 -06:00
|
|
|
return base::WrapUnique(
|
|
|
|
static_cast<content::WebContentsImpl*>(guest_contents_impl));
|
2021-06-29 19:10:18 -06:00
|
|
|
}
|
2022-09-07 01:46:37 -06:00
|
|
|
return guest_contents;
|
2017-05-23 14:26:19 -06:00
|
|
|
}
|
|
|
|
|
2023-05-23 13:58:58 -06:00
|
|
|
base::WeakPtr<content::BrowserPluginGuestDelegate>
|
|
|
|
WebViewGuestDelegate::GetGuestDelegateWeakPtr() {
|
|
|
|
return weak_ptr_factory_.GetWeakPtr();
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:23:04 -06:00
|
|
|
} // namespace electron
|