refactor: do not pass WebContents to NativeWindow

This commit is contained in:
Cheng Zhao 2018-05-08 12:51:27 +09:00
parent 640877ebf8
commit 2b24b26e59
8 changed files with 36 additions and 41 deletions

View file

@ -4,6 +4,7 @@
#include "atom/browser/api/atom_api_browser_window.h"
#include "atom/browser/api/atom_api_web_contents_view.h"
#include "atom/browser/browser.h"
#include "atom/browser/unresponsive_suppressor.h"
#include "atom/browser/web_contents_preferences.h"
@ -67,6 +68,11 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
api_web_contents_->AddObserver(this);
Observe(api_web_contents_->web_contents());
// Create a WebContentsView for the WebContents.
auto* web_contents_view = static_cast<WebContentsView*>(
WebContentsView::New(isolate, web_contents));
web_contents_view_.Reset(isolate, web_contents_view->GetWrapper());
// Keep a copy of the options for later use.
mate::Dictionary(isolate, web_contents->GetWrapper())
.Set("browserWindowOptions", options);
@ -77,7 +83,7 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
// Associate with BrowserWindow.
web_contents->SetOwnerWindow(window());
window()->SetContentView(web_contents->managed_web_contents());
window()->SetContentView(web_contents_view->view());
auto* host = web_contents->web_contents()->GetRenderViewHost();
if (host)

View file

@ -109,6 +109,8 @@ class BrowserWindow : public TopLevelWindow,
v8::Global<v8::Value> web_contents_;
api::WebContents* api_web_contents_;
v8::Global<v8::Value> web_contents_view_;
base::WeakPtrFactory<BrowserWindow> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(BrowserWindow);

View file

@ -48,6 +48,7 @@ gfx::Size GetExpandedWindowSize(const NativeWindow* window, gfx::Size size) {
NativeWindow::NativeWindow(const mate::Dictionary& options,
NativeWindow* parent)
: widget_(new views::Widget),
content_view_(nullptr),
has_frame_(true),
transparent_(false),
enable_larger_than_screen_(false),

View file

@ -20,10 +20,6 @@
class SkRegion;
namespace brightray {
class InspectableWebContents;
}
namespace content {
struct NativeWebKeyboardEvent;
}
@ -60,8 +56,7 @@ class NativeWindow : public base::SupportsUserData,
void InitFromOptions(const mate::Dictionary& options);
virtual void SetContentView(
brightray::InspectableWebContents* web_contents) = 0;
virtual void SetContentView(views::View* view) = 0;
virtual void Close() = 0;
virtual void CloseImmediately() = 0;
@ -264,6 +259,7 @@ class NativeWindow : public base::SupportsUserData,
}
views::Widget* widget() const { return widget_.get(); }
views::View* content_view() const { return content_view_; }
bool has_frame() const { return has_frame_; }
void set_has_frame(bool has_frame) { has_frame_ = has_frame; }
@ -282,6 +278,7 @@ class NativeWindow : public base::SupportsUserData,
views::Widget* GetWidget() override;
const views::Widget* GetWidget() const override;
void set_content_view(views::View* view) { content_view_ = view; }
void set_browser_view(NativeBrowserView* browser_view) {
browser_view_ = browser_view;
}
@ -289,6 +286,9 @@ class NativeWindow : public base::SupportsUserData,
private:
std::unique_ptr<views::Widget> widget_;
// The content view, weak ref.
views::View* content_view_;
// Whether window has standard frame.
bool has_frame_;

View file

@ -21,10 +21,6 @@
@class CustomWindowButtonView;
@class FullSizeContentView;
namespace views {
class NativeViewHost;
}
namespace atom {
class RootViewMac;
@ -35,7 +31,7 @@ class NativeWindowMac : public NativeWindow {
~NativeWindowMac() override;
// NativeWindow:
void SetContentView(brightray::InspectableWebContents* web_contents) override;
void SetContentView(views::View* view) override;
void Close() override;
void CloseImmediately() override;
void Focus(bool focus) override;
@ -144,7 +140,6 @@ class NativeWindowMac : public NativeWindow {
};
TitleBarStyle title_bar_style() const { return title_bar_style_; }
views::View* content_view() { return content_view_; }
AtomPreviewItem* preview_item() const { return preview_item_.get(); }
AtomTouchBar* touch_bar() const { return touch_bar_.get(); }
bool zoom_to_page_width() const { return zoom_to_page_width_; }
@ -178,9 +173,6 @@ class NativeWindowMac : public NativeWindow {
// The view that fills the client area.
std::unique_ptr<RootViewMac> root_view_;
// The content view, managed by widget_.
views::NativeViewHost* content_view_;
bool is_kiosk_;
bool was_fullscreen_;
bool zoom_to_page_width_;

View file

@ -257,7 +257,6 @@ NativeWindowMac::NativeWindowMac(const mate::Dictionary& options,
NativeWindow* parent)
: NativeWindow(options, parent),
root_view_(new RootViewMac(this)),
content_view_(nullptr),
is_kiosk_(false),
was_fullscreen_(false),
zoom_to_page_width_(false),
@ -512,15 +511,13 @@ NativeWindowMac::~NativeWindowMac() {
[NSEvent removeMonitor:wheel_event_monitor_];
}
void NativeWindowMac::SetContentView(
brightray::InspectableWebContents* web_contents) {
void NativeWindowMac::SetContentView(views::View* view) {
views::View* root_view = GetContentsView();
if (content_view_)
root_view->RemoveChildView(content_view_);
if (content_view())
root_view->RemoveChildView(content_view());
content_view_ = new views::NativeViewHost();
root_view->AddChildView(content_view_);
content_view_->Attach(web_contents->GetView()->GetNativeView());
set_content_view(view);
root_view->AddChildView(content_view());
if (buttons_view_) {
// Ensure the buttons view are always floated on the top.

View file

@ -102,7 +102,6 @@ NativeWindowViews::NativeWindowViews(const mate::Dictionary& options,
NativeWindow* parent)
: NativeWindow(options, parent),
root_view_(new RootView(this)),
content_view_(nullptr),
focused_view_(nullptr),
#if defined(OS_WIN)
checked_for_a11y_support_(false),
@ -304,19 +303,18 @@ NativeWindowViews::~NativeWindowViews() {
#endif
}
void NativeWindowViews::SetContentView(
brightray::InspectableWebContents* web_contents) {
if (content_view_) {
root_view_->RemoveChildView(content_view_);
void NativeWindowViews::SetContentView(views::View* view) {
if (content_view()) {
root_view_->RemoveChildView(content_view());
if (browser_view()) {
content_view_->RemoveChildView(
content_view()->RemoveChildView(
browser_view()->GetInspectableWebContentsView()->GetView());
set_browser_view(nullptr);
}
}
content_view_ = web_contents->GetView()->GetView();
focused_view_ = web_contents->GetView()->GetWebView();
root_view_->AddChildView(content_view_);
set_content_view(view);
focused_view_ = view;
root_view_->AddChildView(content_view());
root_view_->Layout();
}
@ -560,7 +558,7 @@ gfx::Rect NativeWindowViews::GetBounds() {
}
gfx::Rect NativeWindowViews::GetContentBounds() {
return content_view_ ? content_view_->GetBoundsInScreen() : gfx::Rect();
return content_view() ? content_view()->GetBoundsInScreen() : gfx::Rect();
}
gfx::Size NativeWindowViews::GetContentSize() {
@ -569,7 +567,7 @@ gfx::Size NativeWindowViews::GetContentSize() {
return NativeWindow::GetContentSize();
#endif
return content_view_ ? content_view_->size() : gfx::Size();
return content_view() ? content_view()->size() : gfx::Size();
}
void NativeWindowViews::SetContentSizeConstraints(
@ -924,11 +922,11 @@ void NativeWindowViews::SetMenu(AtomMenuModel* menu_model) {
}
void NativeWindowViews::SetBrowserView(NativeBrowserView* view) {
if (!content_view_)
if (!content_view())
return;
if (browser_view()) {
content_view_->RemoveChildView(
content_view()->RemoveChildView(
browser_view()->GetInspectableWebContentsView()->GetView());
set_browser_view(nullptr);
}
@ -940,7 +938,8 @@ void NativeWindowViews::SetBrowserView(NativeBrowserView* view) {
// Add as child of the main web view to avoid (0, 0) origin from overlapping
// with menu bar.
set_browser_view(view);
content_view_->AddChildView(view->GetInspectableWebContentsView()->GetView());
content_view()->AddChildView(
view->GetInspectableWebContentsView()->GetView());
}
void NativeWindowViews::SetParentWindow(NativeWindow* parent) {

View file

@ -44,7 +44,7 @@ class NativeWindowViews : public NativeWindow,
~NativeWindowViews() override;
// NativeWindow:
void SetContentView(brightray::InspectableWebContents* web_contents) override;
void SetContentView(views::View* view) override;
void Close() override;
void CloseImmediately() override;
void Focus(bool focus) override;
@ -135,7 +135,6 @@ class NativeWindowViews : public NativeWindow,
void SetIcon(const gfx::ImageSkia& icon);
#endif
views::View* content_view() const { return content_view_; }
SkRegion* draggable_region() const { return draggable_region_.get(); }
#if defined(OS_WIN)
@ -197,7 +196,6 @@ class NativeWindowViews : public NativeWindow,
std::unique_ptr<RootView> root_view_;
views::View* content_view_; // Weak ref.
views::View* focused_view_; // The view should be focused by default.
// The "resizable" flag on Linux is implemented by setting size constraints,