electron/shell/browser/web_view_guest_delegate.cc

124 lines
4.5 KiB
C++
Raw Normal View History

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/web_view_guest_delegate.h"
2018-09-15 00:16:22 +00:00
#include <memory>
#include "atom/browser/api/atom_api_web_contents.h"
2015-08-05 11:50:23 +00:00
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "content/browser/web_contents/web_contents_impl.h" // nogncheck
#include "content/public/browser/navigation_handle.h"
2015-08-05 11:50:23 +00:00
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
2016-03-08 14:28:53 +00:00
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
namespace atom {
WebViewGuestDelegate::WebViewGuestDelegate(content::WebContents* embedder,
api::WebContents* api_web_contents)
: embedder_web_contents_(embedder), api_web_contents_(api_web_contents) {}
2015-09-07 23:54:07 +00:00
WebViewGuestDelegate::~WebViewGuestDelegate() {
ResetZoomController();
}
void WebViewGuestDelegate::AttachToIframe(
content::WebContents* embedder_web_contents,
int embedder_frame_id) {
embedder_web_contents_ = embedder_web_contents;
int embedder_process_id =
embedder_web_contents_->GetMainFrame()->GetProcess()->GetID();
auto* embedder_frame =
content::RenderFrameHost::FromID(embedder_process_id, embedder_frame_id);
DCHECK_EQ(embedder_web_contents_,
content::WebContents::FromRenderFrameHost(embedder_frame));
content::WebContents* guest_web_contents = api_web_contents_->web_contents();
// 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.
embedder_web_contents_->AttachInnerWebContents(
base::WrapUnique<content::WebContents>(guest_web_contents),
embedder_frame);
ResetZoomController();
embedder_zoom_controller_ =
WebContentsZoomController::FromWebContents(embedder_web_contents_);
embedder_zoom_controller_->AddObserver(this);
auto* zoom_controller = api_web_contents_->GetZoomController();
zoom_controller->SetEmbedderZoomController(embedder_zoom_controller_);
api_web_contents_->Emit("did-attach");
}
void WebViewGuestDelegate::DidDetach() {
ResetZoomController();
}
content::WebContents* WebViewGuestDelegate::GetOwnerWebContents() const {
return embedder_web_contents_;
}
void WebViewGuestDelegate::OnZoomLevelChanged(
content::WebContents* web_contents,
double level,
bool is_temporary) {
if (web_contents == GetOwnerWebContents()) {
if (is_temporary) {
api_web_contents_->GetZoomController()->SetTemporaryZoomLevel(level);
} else {
api_web_contents_->GetZoomController()->SetZoomLevel(level);
}
// Change the default zoom factor to match the embedders' new zoom level.
double zoom_factor = content::ZoomLevelToZoomFactor(level);
api_web_contents_->GetZoomController()->SetDefaultZoomFactor(zoom_factor);
}
}
void WebViewGuestDelegate::OnZoomControllerWebContentsDestroyed() {
ResetZoomController();
}
void WebViewGuestDelegate::ResetZoomController() {
if (embedder_zoom_controller_) {
embedder_zoom_controller_->RemoveObserver(this);
embedder_zoom_controller_ = nullptr;
}
}
2017-01-25 01:44:58 +00:00
content::RenderWidgetHost* WebViewGuestDelegate::GetOwnerRenderWidgetHost() {
return embedder_web_contents_->GetRenderViewHost()->GetWidget();
}
content::SiteInstance* WebViewGuestDelegate::GetOwnerSiteInstance() {
return embedder_web_contents_->GetSiteInstance();
}
content::WebContents* WebViewGuestDelegate::CreateNewGuestWindow(
const content::WebContents::CreateParams& create_params) {
// Code below mirrors what content::WebContentsImpl::CreateNewWindow
// does for non-guest sources
content::WebContents::CreateParams guest_params(create_params);
guest_params.initial_size =
embedder_web_contents_->GetContainerBounds().size();
guest_params.context = embedder_web_contents_->GetNativeView();
2018-09-15 00:16:22 +00:00
std::unique_ptr<content::WebContents> guest_contents =
content::WebContents::Create(guest_params);
content::RenderWidgetHost* render_widget_host =
guest_contents->GetRenderViewHost()->GetWidget();
auto* guest_contents_impl =
2018-09-15 00:16:22 +00:00
static_cast<content::WebContentsImpl*>(guest_contents.release());
guest_contents_impl->GetView()->CreateViewForWidget(render_widget_host,
false);
2018-09-15 00:16:22 +00:00
return guest_contents_impl;
}
} // namespace atom