Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use OOPIF for webview tag (3-0-x) #14156

Merged
merged 1 commit into from Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 32 additions & 58 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -1075,14 +1038,16 @@ 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.
// For #1, the destructor will do the cleanup work and we only need to make
// 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();
Expand All @@ -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());
}
Expand Down Expand Up @@ -1140,10 +1112,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)) {
Expand Down Expand Up @@ -1754,15 +1722,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;
Expand Down Expand Up @@ -2051,6 +2024,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)
Expand Down
7 changes: 3 additions & 4 deletions atom/browser/api/atom_api_web_contents.h
Expand Up @@ -42,7 +42,6 @@ class ResourceRequestBody;

namespace atom {

struct SetSizeParams;
class AtomBrowserContext;
class AtomJavaScriptDialogManager;
class WebContentsZoomController;
Expand Down Expand Up @@ -199,8 +198,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;
Expand Down Expand Up @@ -342,8 +343,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;
Expand Down
1 change: 1 addition & 0 deletions atom/browser/api/atom_api_web_view_manager.cc
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions atom/browser/common_web_contents_delegate.cc
Expand Up @@ -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);

Expand All @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion atom/browser/common_web_contents_delegate.h
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions atom/browser/web_contents_zoom_controller.cc
Expand Up @@ -228,6 +228,9 @@ void WebContentsZoomController::DidFinishNavigation(
}

void WebContentsZoomController::WebContentsDestroyed() {
for (Observer& observer : observers_)
observer.OnZoomControllerWebContentsDestroyed();

observers_.Clear();
embedder_zoom_controller_ = nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions atom/browser/web_contents_zoom_controller.h
Expand Up @@ -24,6 +24,7 @@ class WebContentsZoomController
virtual void OnZoomLevelChanged(content::WebContents* web_contents,
double level,
bool is_temporary) {}
virtual void OnZoomControllerWebContentsDestroyed() {}

protected:
virtual ~Observer() {}
Expand Down