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

View file

@ -109,6 +109,8 @@ class BrowserWindow : public TopLevelWindow,
v8::Global<v8::Value> web_contents_; v8::Global<v8::Value> web_contents_;
api::WebContents* api_web_contents_; api::WebContents* api_web_contents_;
v8::Global<v8::Value> web_contents_view_;
base::WeakPtrFactory<BrowserWindow> weak_factory_; base::WeakPtrFactory<BrowserWindow> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(BrowserWindow); 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::NativeWindow(const mate::Dictionary& options,
NativeWindow* parent) NativeWindow* parent)
: widget_(new views::Widget), : widget_(new views::Widget),
content_view_(nullptr),
has_frame_(true), has_frame_(true),
transparent_(false), transparent_(false),
enable_larger_than_screen_(false), enable_larger_than_screen_(false),

View file

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

View file

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

View file

@ -257,7 +257,6 @@ NativeWindowMac::NativeWindowMac(const mate::Dictionary& options,
NativeWindow* parent) NativeWindow* parent)
: NativeWindow(options, parent), : NativeWindow(options, parent),
root_view_(new RootViewMac(this)), root_view_(new RootViewMac(this)),
content_view_(nullptr),
is_kiosk_(false), is_kiosk_(false),
was_fullscreen_(false), was_fullscreen_(false),
zoom_to_page_width_(false), zoom_to_page_width_(false),
@ -512,15 +511,13 @@ NativeWindowMac::~NativeWindowMac() {
[NSEvent removeMonitor:wheel_event_monitor_]; [NSEvent removeMonitor:wheel_event_monitor_];
} }
void NativeWindowMac::SetContentView( void NativeWindowMac::SetContentView(views::View* view) {
brightray::InspectableWebContents* web_contents) {
views::View* root_view = GetContentsView(); views::View* root_view = GetContentsView();
if (content_view_) if (content_view())
root_view->RemoveChildView(content_view_); root_view->RemoveChildView(content_view());
content_view_ = new views::NativeViewHost(); set_content_view(view);
root_view->AddChildView(content_view_); root_view->AddChildView(content_view());
content_view_->Attach(web_contents->GetView()->GetNativeView());
if (buttons_view_) { if (buttons_view_) {
// Ensure the buttons view are always floated on the top. // 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* parent)
: NativeWindow(options, parent), : NativeWindow(options, parent),
root_view_(new RootView(this)), root_view_(new RootView(this)),
content_view_(nullptr),
focused_view_(nullptr), focused_view_(nullptr),
#if defined(OS_WIN) #if defined(OS_WIN)
checked_for_a11y_support_(false), checked_for_a11y_support_(false),
@ -304,19 +303,18 @@ NativeWindowViews::~NativeWindowViews() {
#endif #endif
} }
void NativeWindowViews::SetContentView( void NativeWindowViews::SetContentView(views::View* view) {
brightray::InspectableWebContents* web_contents) { if (content_view()) {
if (content_view_) { root_view_->RemoveChildView(content_view());
root_view_->RemoveChildView(content_view_);
if (browser_view()) { if (browser_view()) {
content_view_->RemoveChildView( content_view()->RemoveChildView(
browser_view()->GetInspectableWebContentsView()->GetView()); browser_view()->GetInspectableWebContentsView()->GetView());
set_browser_view(nullptr); set_browser_view(nullptr);
} }
} }
content_view_ = web_contents->GetView()->GetView(); set_content_view(view);
focused_view_ = web_contents->GetView()->GetWebView(); focused_view_ = view;
root_view_->AddChildView(content_view_); root_view_->AddChildView(content_view());
root_view_->Layout(); root_view_->Layout();
} }
@ -560,7 +558,7 @@ gfx::Rect NativeWindowViews::GetBounds() {
} }
gfx::Rect NativeWindowViews::GetContentBounds() { gfx::Rect NativeWindowViews::GetContentBounds() {
return content_view_ ? content_view_->GetBoundsInScreen() : gfx::Rect(); return content_view() ? content_view()->GetBoundsInScreen() : gfx::Rect();
} }
gfx::Size NativeWindowViews::GetContentSize() { gfx::Size NativeWindowViews::GetContentSize() {
@ -569,7 +567,7 @@ gfx::Size NativeWindowViews::GetContentSize() {
return NativeWindow::GetContentSize(); return NativeWindow::GetContentSize();
#endif #endif
return content_view_ ? content_view_->size() : gfx::Size(); return content_view() ? content_view()->size() : gfx::Size();
} }
void NativeWindowViews::SetContentSizeConstraints( void NativeWindowViews::SetContentSizeConstraints(
@ -924,11 +922,11 @@ void NativeWindowViews::SetMenu(AtomMenuModel* menu_model) {
} }
void NativeWindowViews::SetBrowserView(NativeBrowserView* view) { void NativeWindowViews::SetBrowserView(NativeBrowserView* view) {
if (!content_view_) if (!content_view())
return; return;
if (browser_view()) { if (browser_view()) {
content_view_->RemoveChildView( content_view()->RemoveChildView(
browser_view()->GetInspectableWebContentsView()->GetView()); browser_view()->GetInspectableWebContentsView()->GetView());
set_browser_view(nullptr); 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 // Add as child of the main web view to avoid (0, 0) origin from overlapping
// with menu bar. // with menu bar.
set_browser_view(view); set_browser_view(view);
content_view_->AddChildView(view->GetInspectableWebContentsView()->GetView()); content_view()->AddChildView(
view->GetInspectableWebContentsView()->GetView());
} }
void NativeWindowViews::SetParentWindow(NativeWindow* parent) { void NativeWindowViews::SetParentWindow(NativeWindow* parent) {

View file

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