From 204e5e2ffc5425df346d24b79a5c45fbda0e7236 Mon Sep 17 00:00:00 2001 From: Andy Dill Date: Thu, 29 Nov 2018 21:25:02 -0800 Subject: [PATCH] fix: move NativeWindow tracking to OSR WCV (#15585) * fix: move NativeWindow tracking to OSR WCV * fix oops --- atom/browser/api/atom_api_web_contents.h | 7 +-- atom/browser/common_web_contents_delegate.cc | 12 ++-- atom/browser/common_web_contents_delegate.h | 5 +- .../osr/osr_render_widget_host_view.cc | 38 +----------- .../browser/osr/osr_render_widget_host_view.h | 13 +--- atom/browser/osr/osr_web_contents_view.cc | 62 ++++++++++++------- atom/browser/osr/osr_web_contents_view.h | 15 ++++- 7 files changed, 71 insertions(+), 81 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index aa12661538738..56296988a9fee 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -51,7 +51,7 @@ class WebViewGuestDelegate; class FrameSubscriber; #if BUILDFLAG(ENABLE_OSR) -class OffScreenWebContentsView; +class OffScreenRenderWidgetHostView; #endif namespace api { @@ -439,9 +439,8 @@ class WebContents : public mate::TrackableObject, uint32_t GetNextRequestId() { return ++request_id_; } #if BUILDFLAG(ENABLE_OSR) - OffScreenWebContentsView* GetOffScreenWebContentsView() const; - OffScreenRenderWidgetHostView* GetOffScreenRenderWidgetHostView() - const override; + OffScreenWebContentsView* GetOffScreenWebContentsView() const override; + OffScreenRenderWidgetHostView* GetOffScreenRenderWidgetHostView() const; #endif // Called when we receive a CursorChange message from chromium. diff --git a/atom/browser/common_web_contents_delegate.cc b/atom/browser/common_web_contents_delegate.cc index 3408429633543..c7edcd03a0cc5 100644 --- a/atom/browser/common_web_contents_delegate.cc +++ b/atom/browser/common_web_contents_delegate.cc @@ -41,7 +41,7 @@ #include "storage/browser/fileapi/isolated_context.h" #if BUILDFLAG(ENABLE_OSR) -#include "atom/browser/osr/osr_render_widget_host_view.h" +#include "atom/browser/osr/osr_web_contents_view.h" #endif using content::BrowserThread; @@ -207,9 +207,9 @@ void CommonWebContentsDelegate::SetOwnerWindow( NativeWindowRelay::kNativeWindowRelayUserDataKey); } #if BUILDFLAG(ENABLE_OSR) - auto* osr_rwhv = GetOffScreenRenderWidgetHostView(); - if (osr_rwhv) - osr_rwhv->SetNativeWindow(owner_window); + auto* osr_wcv = GetOffScreenWebContentsView(); + if (osr_wcv) + osr_wcv->SetNativeWindow(owner_window); #endif } @@ -247,8 +247,8 @@ content::WebContents* CommonWebContentsDelegate::GetDevToolsWebContents() } #if BUILDFLAG(ENABLE_OSR) -OffScreenRenderWidgetHostView* -CommonWebContentsDelegate::GetOffScreenRenderWidgetHostView() const { +OffScreenWebContentsView* +CommonWebContentsDelegate::GetOffScreenWebContentsView() const { return nullptr; } #endif diff --git a/atom/browser/common_web_contents_delegate.h b/atom/browser/common_web_contents_delegate.h index 22643131c4bbe..1276119af9493 100644 --- a/atom/browser/common_web_contents_delegate.h +++ b/atom/browser/common_web_contents_delegate.h @@ -33,7 +33,7 @@ class NativeWindow; class WebDialogHelper; #if BUILDFLAG(ENABLE_OSR) -class OffScreenRenderWidgetHostView; +class OffScreenWebContentsView; #endif class CommonWebContentsDelegate @@ -71,8 +71,7 @@ class CommonWebContentsDelegate protected: #if BUILDFLAG(ENABLE_OSR) - virtual OffScreenRenderWidgetHostView* GetOffScreenRenderWidgetHostView() - const; + virtual OffScreenWebContentsView* GetOffScreenWebContentsView() const; #endif // content::WebContentsDelegate: diff --git a/atom/browser/osr/osr_render_widget_host_view.cc b/atom/browser/osr/osr_render_widget_host_view.cc index 4309d1248cad4..1ad460bef2c2b 100644 --- a/atom/browser/osr/osr_render_widget_host_view.cc +++ b/atom/browser/osr/osr_render_widget_host_view.cc @@ -257,15 +257,14 @@ OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView( const OnPaintCallback& callback, content::RenderWidgetHost* host, OffScreenRenderWidgetHostView* parent_host_view, - NativeWindow* native_window) + gfx::Size initial_size) : content::RenderWidgetHostViewBase(host), render_widget_host_(content::RenderWidgetHostImpl::From(host)), parent_host_view_(parent_host_view), - native_window_(native_window), transparent_(transparent), callback_(callback), frame_rate_(frame_rate), - size_(native_window ? native_window->GetSize() : gfx::Size()), + size_(initial_size), painting_(painting), is_showing_(!render_widget_host_->is_hidden()), cursor_manager_(new content::CursorManager(this)), @@ -312,18 +311,12 @@ OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView( #endif GetCompositor()->SetDelegate(this); - if (native_window_) - native_window_->AddObserver(this); - ResizeRootLayer(false); render_widget_host_->SetView(this); InstallTransparency(); } OffScreenRenderWidgetHostView::~OffScreenRenderWidgetHostView() { - if (native_window_) - native_window_->RemoveObserver(this); - #if defined(OS_MACOSX) if (is_showing_) browser_compositor_->SetRenderWidgetHostIsHidden(true); @@ -354,19 +347,6 @@ OffScreenRenderWidgetHostView::CreateBrowserAccessibilityManager( return nullptr; } -void OffScreenRenderWidgetHostView::OnWindowResize() { - // In offscreen mode call RenderWidgetHostView's SetSize explicitly - auto size = native_window_ ? native_window_->GetSize() : gfx::Size(); - SetSize(size); -} - -void OffScreenRenderWidgetHostView::OnWindowClosed() { - if (native_window_) { - native_window_->RemoveObserver(this); - native_window_ = nullptr; - } -} - void OffScreenRenderWidgetHostView::OnBeginFrameTimerTick() { const base::TimeTicks frame_time = base::TimeTicks::Now(); const base::TimeDelta vsync_period = @@ -757,7 +737,7 @@ OffScreenRenderWidgetHostView::CreateViewForWidget( return new OffScreenRenderWidgetHostView( transparent_, true, embedder_host_view->GetFrameRate(), callback_, - render_widget_host, embedder_host_view, native_window_); + render_widget_host, embedder_host_view, size()); } #if !defined(OS_MACOSX) @@ -1259,18 +1239,6 @@ void OffScreenRenderWidgetHostView::InvalidateBounds(const gfx::Rect& bounds) { } } -void OffScreenRenderWidgetHostView::SetNativeWindow(NativeWindow* window) { - if (native_window_) - native_window_->RemoveObserver(this); - - native_window_ = window; - - if (native_window_) - native_window_->AddObserver(this); - - OnWindowResize(); -} - void OffScreenRenderWidgetHostView::ResizeRootLayer(bool force) { SetupFrameRate(false); diff --git a/atom/browser/osr/osr_render_widget_host_view.h b/atom/browser/osr/osr_render_widget_host_view.h index b3a9688e46be1..c82e05934e3b9 100644 --- a/atom/browser/osr/osr_render_widget_host_view.h +++ b/atom/browser/osr/osr_render_widget_host_view.h @@ -14,8 +14,6 @@ #include #endif -#include "atom/browser/native_window.h" -#include "atom/browser/native_window_observer.h" #include "atom/browser/osr/osr_output_device.h" #include "atom/browser/osr/osr_view_proxy.h" #include "base/process/kill.h" @@ -75,7 +73,6 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase, #if !defined(OS_MACOSX) public content::DelegatedFrameHostClient, #endif - public NativeWindowObserver, public OffscreenViewProxyObserver { public: OffScreenRenderWidgetHostView(bool transparent, @@ -84,7 +81,7 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase, const OnPaintCallback& callback, content::RenderWidgetHost* render_widget_host, OffScreenRenderWidgetHostView* parent_host_view, - NativeWindow* native_window); + gfx::Size initial_size); ~OffScreenRenderWidgetHostView() override; content::BrowserAccessibilityManager* CreateBrowserAccessibilityManager( @@ -205,10 +202,6 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase, bool InstallTransparency(); - // NativeWindowObserver: - void OnWindowResize() override; - void OnWindowClosed() override; - void OnBeginFrameTimerTick(); void SendBeginFrame(base::TimeTicks frame_time, base::TimeDelta vsync_period); @@ -268,8 +261,7 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase, content::RenderWidgetHostImpl* render_widget_host() const { return render_widget_host_; } - void SetNativeWindow(NativeWindow* window); - NativeWindow* window() const { return native_window_; } + gfx::Size size() const { return size_; } void set_popup_host_view(OffScreenRenderWidgetHostView* popup_view) { @@ -306,7 +298,6 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase, std::set guest_host_views_; std::set proxy_views_; - NativeWindow* native_window_; OffScreenOutputDevice* software_output_device_ = nullptr; const bool transparent_; diff --git a/atom/browser/osr/osr_web_contents_view.cc b/atom/browser/osr/osr_web_contents_view.cc index a528f1098b6fb..5cbbae37322ff 100644 --- a/atom/browser/osr/osr_web_contents_view.cc +++ b/atom/browser/osr/osr_web_contents_view.cc @@ -15,13 +15,16 @@ namespace atom { OffScreenWebContentsView::OffScreenWebContentsView( bool transparent, const OnPaintCallback& callback) - : transparent_(transparent), callback_(callback) { + : native_window_(nullptr), transparent_(transparent), callback_(callback) { #if defined(OS_MACOSX) PlatformCreate(); #endif } OffScreenWebContentsView::~OffScreenWebContentsView() { + if (native_window_) + native_window_->RemoveObserver(this); + #if defined(OS_MACOSX) PlatformDestroy(); #endif @@ -34,35 +37,52 @@ void OffScreenWebContentsView::SetWebContents( RenderViewCreated(web_contents_->GetRenderViewHost()); } +void OffScreenWebContentsView::SetNativeWindow(NativeWindow* window) { + if (native_window_) + native_window_->RemoveObserver(this); + + native_window_ = window; + + if (native_window_) + native_window_->AddObserver(this); + + OnWindowResize(); +} + +void OffScreenWebContentsView::OnWindowResize() { + // In offscreen mode call RenderWidgetHostView's SetSize explicitly + if (GetView()) + GetView()->SetSize(GetSize()); +} + +void OffScreenWebContentsView::OnWindowClosed() { + if (native_window_) { + native_window_->RemoveObserver(this); + native_window_ = nullptr; + } +} + +gfx::Size OffScreenWebContentsView::GetSize() { + return native_window_ ? native_window_->GetSize() : gfx::Size(); +} + #if !defined(OS_MACOSX) gfx::NativeView OffScreenWebContentsView::GetNativeView() const { - if (!web_contents_) - return gfx::NativeView(); - - auto* relay = NativeWindowRelay::FromWebContents(web_contents_); - if (!relay) + if (!native_window_) return gfx::NativeView(); - return relay->GetNativeWindow()->GetNativeView(); + return native_window_->GetNativeView(); } gfx::NativeView OffScreenWebContentsView::GetContentNativeView() const { - if (!web_contents_) - return gfx::NativeView(); - - auto* relay = NativeWindowRelay::FromWebContents(web_contents_); - if (!relay) + if (!native_window_) return gfx::NativeView(); - return relay->GetNativeWindow()->GetNativeView(); + return native_window_->GetNativeView(); } gfx::NativeWindow OffScreenWebContentsView::GetTopLevelNativeWindow() const { - if (!web_contents_) - return gfx::NativeWindow(); - - auto* relay = NativeWindowRelay::FromWebContents(web_contents_); - if (!relay) + if (!native_window_) return gfx::NativeWindow(); - return relay->GetNativeWindow()->GetNativeWindow(); + return native_window_->GetNativeWindow(); } #endif @@ -104,7 +124,7 @@ OffScreenWebContentsView::CreateViewForWidget( return new OffScreenRenderWidgetHostView( transparent_, painting_, GetFrameRate(), callback_, render_widget_host, - nullptr, nullptr); + nullptr, GetSize()); } content::RenderWidgetHostViewBase* @@ -122,7 +142,7 @@ OffScreenWebContentsView::CreateViewForPopupWidget( return new OffScreenRenderWidgetHostView(transparent_, true, view->GetFrameRate(), callback_, - render_widget_host, view, nullptr); + render_widget_host, view, GetSize()); } void OffScreenWebContentsView::SetPageTitle(const base::string16& title) {} diff --git a/atom/browser/osr/osr_web_contents_view.h b/atom/browser/osr/osr_web_contents_view.h index 0f32c575a03da..febae35bad254 100644 --- a/atom/browser/osr/osr_web_contents_view.h +++ b/atom/browser/osr/osr_web_contents_view.h @@ -5,6 +5,9 @@ #ifndef ATOM_BROWSER_OSR_OSR_WEB_CONTENTS_VIEW_H_ #define ATOM_BROWSER_OSR_OSR_WEB_CONTENTS_VIEW_H_ +#include "atom/browser/native_window.h" +#include "atom/browser/native_window_observer.h" + #include "atom/browser/osr/osr_render_widget_host_view.h" #include "content/browser/renderer_host/render_view_host_delegate_view.h" #include "content/browser/web_contents/web_contents_view.h" @@ -21,12 +24,20 @@ class OffScreenView; namespace atom { class OffScreenWebContentsView : public content::WebContentsView, - public content::RenderViewHostDelegateView { + public content::RenderViewHostDelegateView, + public NativeWindowObserver { public: OffScreenWebContentsView(bool transparent, const OnPaintCallback& callback); ~OffScreenWebContentsView() override; void SetWebContents(content::WebContents*); + void SetNativeWindow(NativeWindow* window); + + // NativeWindowObserver: + void OnWindowResize() override; + void OnWindowClosed() override; + + gfx::Size GetSize(); // content::WebContentsView: gfx::NativeView GetNativeView() const override; @@ -84,6 +95,8 @@ class OffScreenWebContentsView : public content::WebContentsView, OffScreenRenderWidgetHostView* GetView() const; + NativeWindow* native_window_; + const bool transparent_; bool painting_ = true; int frame_rate_ = 60;