fix: use OOPIF for webview tag (#13869)

* fix: use OOIF for webview tag

* fix: do not call GetNativeView for webview

* fix: OOIPF webview's WebContents is managed by embedder frame

* fix: guest view can not be focused

* fix: clear zoom controller when guest is destroyed

* fix: implement the webview resize event

The webview is no longer a browser plugin with the resize event, use
ResizeObserver instead.

* test: disable failed tests due to OOPIF webview

* fix: embedder can be destroyed earlier than guest

This happens when embedder is manually destroyed.

* fix: don't double attach

* fix: recreate iframe when webview is reattached

* fix: resize event may happen very early

* test: some tests are working after OOPIF webview

* chore: remove unused browser plugin webview code

* fix: get embedder via closure

When the "destroyed" event is emitted, the entry in guestInstances would be
cleared.

* chore: rename browserPluginNode to internalElement

* test: make the visibilityState test more robust

* chore: guestinstance can not work with OOPIF webview

* fix: element could be detached before got response from browser
This commit is contained in:
Cheng Zhao 2018-08-16 15:57:40 -07:00 committed by Charles Kerr
parent 48407c5b93
commit dd5b8769be
28 changed files with 268 additions and 1008 deletions

View file

@ -120,28 +120,6 @@ struct PrintSettings {
namespace mate {
template <>
struct Converter<atom::SetSizeParams> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
atom::SetSizeParams* out) {
mate::Dictionary params;
if (!ConvertFromV8(isolate, val, &params))
return false;
bool autosize;
if (params.Get("enableAutoSize", &autosize))
out->enable_auto_size.reset(new bool(autosize));
gfx::Size size;
if (params.Get("min", &size))
out->min_size.reset(new gfx::Size(size));
if (params.Get("max", &size))
out->max_size.reset(new gfx::Size(size));
if (params.Get("normal", &size))
out->normal_size.reset(new gfx::Size(size));
return true;
}
};
template <>
struct Converter<PrintSettings> {
static bool FromV8(v8::Isolate* isolate,
@ -396,7 +374,8 @@ WebContents::WebContents(v8::Isolate* isolate,
GURL("chrome-guest://fake-host"));
content::WebContents::CreateParams params(session->browser_context(),
site_instance);
guest_delegate_.reset(new WebViewGuestDelegate);
guest_delegate_.reset(
new WebViewGuestDelegate(embedder_->web_contents(), this));
params.guest_delegate = guest_delegate_.get();
#if defined(ENABLE_OSR)
@ -448,7 +427,7 @@ void WebContents::InitWithSessionAndOptions(v8::Isolate* isolate,
mate::Handle<api::Session> session,
const mate::Dictionary& options) {
Observe(web_contents);
InitWithWebContents(web_contents, session->browser_context());
InitWithWebContents(web_contents, session->browser_context(), IsGuest());
managed_web_contents()->GetView()->SetDelegate(this);
@ -481,8 +460,6 @@ void WebContents::InitWithSessionAndOptions(v8::Isolate* isolate,
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
if (IsGuest()) {
guest_delegate_->Initialize(this);
NativeWindow* owner_window = nullptr;
if (embedder_) {
// New WebContents's owner_window is the embedder's owner_window.
@ -504,27 +481,18 @@ WebContents::~WebContents() {
if (managed_web_contents()) {
managed_web_contents()->GetView()->SetDelegate(nullptr);
// For webview we need to tell content module to do some cleanup work before
// destroying it.
if (type_ == WEB_VIEW)
guest_delegate_->Destroy();
RenderViewDeleted(web_contents()->GetRenderViewHost());
if (type_ == WEB_VIEW) {
DestroyWebContents(false /* async */);
if (type_ == BROWSER_WINDOW && owner_window()) {
for (ExtendedWebContentsObserver& observer : observers_)
observer.OnCloseContents();
} else {
if (type_ == BROWSER_WINDOW && owner_window()) {
for (ExtendedWebContentsObserver& observer : observers_)
observer.OnCloseContents();
} else {
DestroyWebContents(true /* async */);
}
// The WebContentsDestroyed will not be called automatically because we
// destroy the webContents in the next tick. So we have to manually
// call it here to make sure "destroyed" event is emitted.
WebContentsDestroyed();
DestroyWebContents(!IsGuest() /* async */);
}
// The WebContentsDestroyed will not be called automatically because we
// destroy the webContents in the next tick. So we have to manually
// call it here to make sure "destroyed" event is emitted.
WebContentsDestroyed();
}
}
@ -784,13 +752,6 @@ content::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager(
return dialog_manager_.get();
}
void WebContents::ResizeDueToAutoResize(content::WebContents* web_contents,
const gfx::Size& new_size) {
if (IsGuest()) {
guest_delegate_->ResizeDueToAutoResize(new_size);
}
}
void WebContents::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
// Do nothing, we override this method just to avoid compilation error since
// there are two virtual functions named BeforeUnloadFired.
@ -935,6 +896,8 @@ void WebContents::DidFinishNavigation(
Emit("did-navigate", url, http_response_code, http_status_text);
}
}
if (IsGuest())
Emit("load-commit", url, is_main_frame);
} else {
auto url = navigation_handle->GetURL();
int code = navigation_handle->GetNetErrorCode();
@ -1075,7 +1038,8 @@ bool WebContents::OnMessageReceived(const IPC::Message& message,
// 1. call webContents.destroy();
// 2. garbage collection;
// 3. user closes the window of webContents;
// For webview only #1 will happen, for BrowserWindow both #1 and #3 may
// 4. the embedder detaches the frame.
// For webview only #4 will happen, for BrowserWindow both #1 and #3 may
// happen. The #2 should never happen for webContents, because webview is
// managed by GuestViewManager, and BrowserWindow's webContents is managed
// by api::BrowserWindow.
@ -1083,6 +1047,7 @@ bool WebContents::OnMessageReceived(const IPC::Message& message,
// sure "destroyed" event is emitted. For #3, the content::WebContents will
// be destroyed on close, and WebContentsDestroyed would be called for it, so
// we need to make sure the api::WebContents is also deleted.
// For #4, the WebContents will be destroyed by embedder.
void WebContents::WebContentsDestroyed() {
// Cleanup relationships with other parts.
RemoveFromWeakMap();
@ -1093,6 +1058,13 @@ void WebContents::WebContentsDestroyed() {
Emit("destroyed");
// For guest view based on OOPIF, the WebContents is released by the embedder
// frame, and we need to clear the reference to the memory.
if (IsGuest() && managed_web_contents()) {
managed_web_contents()->ReleaseWebContents();
ResetManagedWebContents(false);
}
// Destroy the native class in next tick.
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, GetDestroyClosure());
}
@ -1129,10 +1101,6 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
return;
}
if (guest_delegate_ && !guest_delegate_->IsAttached()) {
return;
}
content::NavigationController::LoadURLParams params(url);
if (!options.Get("httpReferrer", &params.referrer)) {
@ -1747,15 +1715,20 @@ void WebContents::OnCursorChange(const content::WebCursor& cursor) {
}
}
void WebContents::SetSize(const SetSizeParams& params) {
if (guest_delegate_)
guest_delegate_->SetSize(params);
void WebContents::SetSize(v8::Local<v8::Value>) {
// TODO(zcbenz): Remove this method in 4.0.
}
bool WebContents::IsGuest() const {
return type_ == WEB_VIEW;
}
void WebContents::AttachToIframe(content::WebContents* embedder_web_contents,
int embedder_frame_id) {
if (guest_delegate_)
guest_delegate_->AttachToIframe(embedder_web_contents, embedder_frame_id);
}
bool WebContents::IsOffScreen() const {
#if defined(ENABLE_OSR)
return type_ == OFF_SCREEN;
@ -2045,6 +2018,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("startDrag", &WebContents::StartDrag)
.SetMethod("setSize", &WebContents::SetSize)
.SetMethod("isGuest", &WebContents::IsGuest)
.SetMethod("attachToIframe", &WebContents::AttachToIframe)
.SetMethod("isOffscreen", &WebContents::IsOffScreen)
.SetMethod("startPainting", &WebContents::StartPainting)
.SetMethod("stopPainting", &WebContents::StopPainting)

View file

@ -42,7 +42,6 @@ class ResourceRequestBody;
namespace atom {
struct SetSizeParams;
class AtomBrowserContext;
class AtomJavaScriptDialogManager;
class WebContentsZoomController;
@ -197,8 +196,10 @@ class WebContents : public mate::TrackableObject<WebContents>,
void CapturePage(mate::Arguments* args);
// Methods for creating <webview>.
void SetSize(const SetSizeParams& params);
void SetSize(v8::Local<v8::Value>);
bool IsGuest() const;
void AttachToIframe(content::WebContents* embedder_web_contents,
int embedder_frame_id);
// Methods for offscreen rendering
bool IsOffScreen() const;
@ -340,8 +341,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
const content::BluetoothChooser::EventHandler& handler) override;
content::JavaScriptDialogManager* GetJavaScriptDialogManager(
content::WebContents* source) override;
void ResizeDueToAutoResize(content::WebContents* web_contents,
const gfx::Size& new_size) override;
// content::WebContentsObserver:
void BeforeUnloadFired(const base::TimeTicks& proceed_time) override;

View file

@ -10,6 +10,7 @@
#include "atom/common/options_switches.h"
#include "content/public/browser/browser_context.h"
#include "native_mate/dictionary.h"
// Must be the last in the includes list.
// See https://github.com/electron/electron/issues/10363
#include "atom/common/node_includes.h"

View file

@ -152,7 +152,8 @@ CommonWebContentsDelegate::~CommonWebContentsDelegate() {}
void CommonWebContentsDelegate::InitWithWebContents(
content::WebContents* web_contents,
AtomBrowserContext* browser_context) {
AtomBrowserContext* browser_context,
bool is_guest) {
browser_context_ = browser_context;
web_contents->SetDelegate(this);
@ -165,7 +166,8 @@ void CommonWebContentsDelegate::InitWithWebContents(
!web_preferences || web_preferences->IsEnabled(options::kOffscreen);
// Create InspectableWebContents.
web_contents_.reset(brightray::InspectableWebContents::Create(web_contents));
web_contents_.reset(
brightray::InspectableWebContents::Create(web_contents, is_guest));
web_contents_->SetDelegate(this);
}

View file

@ -42,7 +42,8 @@ class CommonWebContentsDelegate
// Creates a InspectableWebContents object and takes onwership of
// |web_contents|.
void InitWithWebContents(content::WebContents* web_contents,
AtomBrowserContext* browser_context);
AtomBrowserContext* browser_context,
bool is_guest);
// Set the window as owner window.
void SetOwnerWindow(NativeWindow* owner_window);

View file

@ -228,6 +228,9 @@ void WebContentsZoomController::DidFinishNavigation(
}
void WebContentsZoomController::WebContentsDestroyed() {
for (Observer& observer : observers_)
observer.OnZoomControllerWebContentsDestroyed();
observers_.Clear();
embedder_zoom_controller_ = nullptr;
}

View file

@ -24,6 +24,7 @@ class WebContentsZoomController
virtual void OnZoomLevelChanged(content::WebContents* web_contents,
double level,
bool is_temporary) {}
virtual void OnZoomControllerWebContentsDestroyed() {}
protected:
virtual ~Observer() {}

View file

@ -7,144 +7,60 @@
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/guest_host.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
namespace atom {
namespace {
WebViewGuestDelegate::WebViewGuestDelegate(content::WebContents* embedder,
api::WebContents* api_web_contents)
: embedder_web_contents_(embedder), api_web_contents_(api_web_contents) {}
const int kDefaultWidth = 300;
const int kDefaultHeight = 300;
} // namespace
SetSizeParams::SetSizeParams() = default;
SetSizeParams::~SetSizeParams() = default;
WebViewGuestDelegate::WebViewGuestDelegate() {}
WebViewGuestDelegate::~WebViewGuestDelegate() {}
void WebViewGuestDelegate::Initialize(api::WebContents* api_web_contents) {
api_web_contents_ = api_web_contents;
Observe(api_web_contents->GetWebContents());
}
void WebViewGuestDelegate::Destroy() {
// Give the content module an opportunity to perform some cleanup.
ResetZoomController();
guest_host_->WillDestroy();
guest_host_ = nullptr;
}
void WebViewGuestDelegate::SetSize(const SetSizeParams& params) {
bool enable_auto_size =
params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_;
gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_;
gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_;
if (params.normal_size)
normal_size_ = *params.normal_size;
min_auto_size_ = min_size;
min_auto_size_.SetToMin(max_size);
max_auto_size_ = max_size;
max_auto_size_.SetToMax(min_size);
enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty();
auto* rvh = web_contents()->GetRenderViewHost();
if (enable_auto_size) {
// Autosize is being enabled.
rvh->EnableAutoResize(min_auto_size_, max_auto_size_);
normal_size_.SetSize(0, 0);
} else {
// Autosize is being disabled.
// Use default width/height if missing from partially defined normal size.
if (normal_size_.width() && !normal_size_.height())
normal_size_.set_height(GetDefaultSize().height());
if (!normal_size_.width() && normal_size_.height())
normal_size_.set_width(GetDefaultSize().width());
gfx::Size new_size;
if (!normal_size_.IsEmpty()) {
new_size = normal_size_;
} else if (!guest_size_.IsEmpty()) {
new_size = guest_size_;
} else {
new_size = GetDefaultSize();
}
bool changed_due_to_auto_resize = false;
if (auto_size_enabled_) {
// Autosize was previously enabled.
rvh->DisableAutoResize(new_size);
changed_due_to_auto_resize = true;
} else {
// Autosize was already disabled.
guest_host_->SizeContents(new_size);
}
UpdateGuestSize(new_size, changed_due_to_auto_resize);
}
auto_size_enabled_ = enable_auto_size;
}
void WebViewGuestDelegate::ResizeDueToAutoResize(const gfx::Size& new_size) {
UpdateGuestSize(new_size, auto_size_enabled_);
}
void WebViewGuestDelegate::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage()) {
auto is_main_frame = navigation_handle->IsInMainFrame();
auto url = navigation_handle->GetURL();
api_web_contents_->Emit("load-commit", url, is_main_frame);
}
}
void WebViewGuestDelegate::DidDetach() {
attached_ = false;
WebViewGuestDelegate::~WebViewGuestDelegate() {
ResetZoomController();
}
void WebViewGuestDelegate::DidAttach(int guest_proxy_routing_id) {
attached_ = true;
api_web_contents_->Emit("did-attach");
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));
// 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.
api_web_contents_->web_contents()->AttachToOuterWebContentsFrame(
embedder_web_contents_, embedder_frame);
ResetZoomController();
embedder_zoom_controller_ =
WebContentsZoomController::FromWebContents(embedder_web_contents_);
auto* zoom_controller = api_web_contents_->GetZoomController();
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::SetGuestHost(content::GuestHost* guest_host) {
guest_host_ = guest_host;
}
void WebViewGuestDelegate::WillAttach(
content::WebContents* embedder_web_contents,
int element_instance_id,
bool is_full_page_plugin,
const base::Closure& completion_callback) {
embedder_web_contents_ = embedder_web_contents;
is_full_page_plugin_ = is_full_page_plugin;
completion_callback.Run();
}
void WebViewGuestDelegate::OnZoomLevelChanged(
content::WebContents* web_contents,
double level,
@ -161,23 +77,8 @@ void WebViewGuestDelegate::OnZoomLevelChanged(
}
}
void WebViewGuestDelegate::UpdateGuestSize(const gfx::Size& new_size,
bool due_to_auto_resize) {
if (due_to_auto_resize)
api_web_contents_->Emit("size-changed", guest_size_.width(),
guest_size_.height(), new_size.width(),
new_size.height());
guest_size_ = new_size;
}
gfx::Size WebViewGuestDelegate::GetDefaultSize() const {
if (is_full_page_plugin_) {
// Full page plugins default to the size of the owner's viewport.
return embedder_web_contents_->GetRenderWidgetHostView()
->GetVisibleViewportSize();
} else {
return gfx::Size(kDefaultWidth, kDefaultHeight);
}
void WebViewGuestDelegate::OnZoomControllerWebContentsDestroyed() {
ResetZoomController();
}
void WebViewGuestDelegate::ResetZoomController() {
@ -187,10 +88,6 @@ void WebViewGuestDelegate::ResetZoomController() {
}
}
bool WebViewGuestDelegate::CanBeEmbeddedInsideCrossProcessFrames() {
return true;
}
content::RenderWidgetHost* WebViewGuestDelegate::GetOwnerRenderWidgetHost() {
return embedder_web_contents_->GetRenderViewHost()->GetWidget();
}

View file

@ -7,7 +7,6 @@
#include "atom/browser/web_contents_zoom_controller.h"
#include "content/public/browser/browser_plugin_guest_delegate.h"
#include "content/public/browser/web_contents_observer.h"
namespace atom {
@ -15,80 +14,33 @@ namespace api {
class WebContents;
}
// A struct of parameters for SetSize(). The parameters are all declared as
// scoped pointers since they are all optional. Null pointers indicate that the
// parameter has not been provided, and the last used value should be used. Note
// that when |enable_auto_size| is true, providing |normal_size| is not
// meaningful. This is because the normal size of the guestview is overridden
// whenever autosizing occurs.
struct SetSizeParams {
SetSizeParams();
~SetSizeParams();
std::unique_ptr<bool> enable_auto_size;
std::unique_ptr<gfx::Size> min_size;
std::unique_ptr<gfx::Size> max_size;
std::unique_ptr<gfx::Size> normal_size;
};
class WebViewGuestDelegate : public content::BrowserPluginGuestDelegate,
public content::WebContentsObserver,
public WebContentsZoomController::Observer {
public:
WebViewGuestDelegate();
WebViewGuestDelegate(content::WebContents* embedder,
api::WebContents* api_web_contents);
~WebViewGuestDelegate() override;
void Initialize(api::WebContents* api_web_contents);
// Called when the WebContents is going to be destroyed.
void Destroy();
// Used to toggle autosize mode for this GuestView, and set both the automatic
// and normal sizes.
void SetSize(const SetSizeParams& params);
// Invoked when the contents auto-resized and the container should match it.
void ResizeDueToAutoResize(const gfx::Size& new_size);
// Return true if attached.
bool IsAttached() const { return attached_; }
// Attach to the iframe.
void AttachToIframe(content::WebContents* embedder_web_contents,
int embedder_frame_id);
protected:
// content::WebContentsObserver:
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
// content::BrowserPluginGuestDelegate:
void DidAttach(int guest_proxy_routing_id) final;
void DidDetach() final;
content::WebContents* GetOwnerWebContents() const final;
void SetGuestHost(content::GuestHost* guest_host) final;
void WillAttach(content::WebContents* embedder_web_contents,
int element_instance_id,
bool is_full_page_plugin,
const base::Closure& completion_callback) final;
bool CanBeEmbeddedInsideCrossProcessFrames() override;
content::RenderWidgetHost* GetOwnerRenderWidgetHost() override;
content::SiteInstance* GetOwnerSiteInstance() override;
content::RenderWidgetHost* GetOwnerRenderWidgetHost() final;
content::SiteInstance* GetOwnerSiteInstance() final;
content::WebContents* CreateNewGuestWindow(
const content::WebContents::CreateParams& create_params) override;
const content::WebContents::CreateParams& create_params) final;
// WebContentsZoomController::Observer:
void OnZoomLevelChanged(content::WebContents* web_contents,
double level,
bool is_temporary) override;
void OnZoomControllerWebContentsDestroyed() override;
private:
// This method is invoked when the contents auto-resized to give the container
// an opportunity to match it if it wishes.
//
// This gives the derived class an opportunity to inform its container element
// or perform other actions.
void UpdateGuestSize(const gfx::Size& new_size, bool due_to_auto_resize);
// Returns the default size of the guestview.
gfx::Size GetDefaultSize() const;
void ResetZoomController();
// The WebContents that attaches this guest view.
@ -98,34 +50,6 @@ class WebViewGuestDelegate : public content::BrowserPluginGuestDelegate,
// to subscribe for zoom changes.
WebContentsZoomController* embedder_zoom_controller_ = nullptr;
// The size of the container element.
gfx::Size element_size_;
// The size of the guest content. Note: In autosize mode, the container
// element may not match the size of the guest.
gfx::Size guest_size_;
// A pointer to the guest_host.
content::GuestHost* guest_host_ = nullptr;
// Indicates whether autosize mode is enabled or not.
bool auto_size_enabled_ = false;
// The maximum size constraints of the container element in autosize mode.
gfx::Size max_auto_size_;
// The minimum size constraints of the container element in autosize mode.
gfx::Size min_auto_size_;
// The size that will be used when autosize mode is disabled.
gfx::Size normal_size_;
// Whether the guest view is inside a plugin document.
bool is_full_page_plugin_ = false;
// Whether attached.
bool attached_ = false;
api::WebContents* api_web_contents_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(WebViewGuestDelegate);