Commit d6417bc1 authored by Christopher Lam's avatar Christopher Lam Committed by Commit Bot

[Webview] Refactor AutoResize handling into Webview.

This CL moves WebContents AutoResizing behavior into Webview rather than
handling it in subclasses of Webview. This will enable the addition of
other AutoResizing Webviews without the need for a custom subclass.

Bug: 849551
Change-Id: I2a31a57180ff43b6cda9827ba9d19dcddcd1db24
Reviewed-on: https://chromium-review.googlesource.com/1127216
Commit-Queue: calamity <calamity@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576404}
parent 96e63637
...@@ -65,8 +65,6 @@ class ConstrainedDialogWebView : public views::WebView, ...@@ -65,8 +65,6 @@ class ConstrainedDialogWebView : public views::WebView,
const gfx::Size& max_size); const gfx::Size& max_size);
~ConstrainedDialogWebView() override; ~ConstrainedDialogWebView() override;
void SetAutoResizeSize(const gfx::Size& auto_resize_size);
// ConstrainedWebDialogDelegate: // ConstrainedWebDialogDelegate:
const ui::WebDialogDelegate* GetWebDialogDelegate() const override; const ui::WebDialogDelegate* GetWebDialogDelegate() const override;
ui::WebDialogDelegate* GetWebDialogDelegate() override; ui::WebDialogDelegate* GetWebDialogDelegate() override;
...@@ -95,27 +93,13 @@ class ConstrainedDialogWebView : public views::WebView, ...@@ -95,27 +93,13 @@ class ConstrainedDialogWebView : public views::WebView,
gfx::Size CalculatePreferredSize() const override; gfx::Size CalculatePreferredSize() const override;
gfx::Size GetMinimumSize() const override; gfx::Size GetMinimumSize() const override;
gfx::Size GetMaximumSize() const override; gfx::Size GetMaximumSize() const override;
void RenderViewCreated(content::RenderViewHost* render_view_host) override;
void RenderViewHostChanged(content::RenderViewHost* old_host,
content::RenderViewHost* new_host) override;
void DocumentOnLoadCompletedInMainFrame() override; void DocumentOnLoadCompletedInMainFrame() override;
private: private:
void EnableAutoResize();
InitiatorWebContentsObserver initiator_observer_; InitiatorWebContentsObserver initiator_observer_;
std::unique_ptr<ConstrainedWebDialogDelegateViews> impl_; std::unique_ptr<ConstrainedWebDialogDelegateViews> impl_;
// Minimum and maximum sizes to determine dialog bounds for auto-resizing.
const gfx::Size min_size_;
const gfx::Size max_size_;
// The self-reported desired size of the WebContents. Empty if auto resize is
// not enabled. This will be passed to and possibly adjusted by the
// WebDialogDelegate in order to calculate the preferred size of the dialog.
gfx::Size auto_resize_size_;
DISALLOW_COPY_AND_ASSIGN(ConstrainedDialogWebView); DISALLOW_COPY_AND_ASSIGN(ConstrainedDialogWebView);
}; };
...@@ -157,7 +141,11 @@ class WebDialogWebContentsDelegateViews ...@@ -157,7 +141,11 @@ class WebDialogWebContentsDelegateViews
if (!initiator_observer_->web_contents()) if (!initiator_observer_->web_contents())
return; return;
web_view_->SetAutoResizeSize(new_size); // views::WebView is only a delegate for a WebContents it creates itself via
// views::WebView::GetWebContents(). ConstrainedDialogWebView's constructor
// sets its own WebContents (via an override of WebView::GetWebContents()).
// So forward this notification to views::WebView.
web_view_->ResizeDueToAutoResize(source, new_size);
content::WebContents* top_level_web_contents = content::WebContents* top_level_web_contents =
constrained_window::GetTopLevelWebContents( constrained_window::GetTopLevelWebContents(
...@@ -234,22 +222,16 @@ ConstrainedDialogWebView::ConstrainedDialogWebView( ...@@ -234,22 +222,16 @@ ConstrainedDialogWebView::ConstrainedDialogWebView(
impl_(new ConstrainedWebDialogDelegateViews(browser_context, impl_(new ConstrainedWebDialogDelegateViews(browser_context,
delegate, delegate,
&initiator_observer_, &initiator_observer_,
this)), this)) {
min_size_(RestrictToPlatformMinimumSize(min_size)),
max_size_(max_size) {
SetWebContents(GetWebContents()); SetWebContents(GetWebContents());
AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
if (!max_size_.IsEmpty()) if (!max_size.IsEmpty()) {
EnableAutoResize(); EnableSizingFromWebContents(RestrictToPlatformMinimumSize(min_size),
max_size);
}
} }
ConstrainedDialogWebView::~ConstrainedDialogWebView() {} ConstrainedDialogWebView::~ConstrainedDialogWebView() {}
void ConstrainedDialogWebView::SetAutoResizeSize(
const gfx::Size& default_size) {
auto_resize_size_ = default_size;
PreferredSizeChanged();
}
const ui::WebDialogDelegate* ConstrainedDialogWebView::GetWebDialogDelegate() const ui::WebDialogDelegate* ConstrainedDialogWebView::GetWebDialogDelegate()
const { const {
return impl_->GetWebDialogDelegate(); return impl_->GetWebDialogDelegate();
...@@ -342,37 +324,23 @@ gfx::Size ConstrainedDialogWebView::CalculatePreferredSize() const { ...@@ -342,37 +324,23 @@ gfx::Size ConstrainedDialogWebView::CalculatePreferredSize() const {
return gfx::Size(); return gfx::Size();
// If auto-resizing is enabled and the dialog has been auto-resized, // If auto-resizing is enabled and the dialog has been auto-resized,
// |auto_resize_size_| will hold the appropriate current size. In this // View::GetPreferredSize() won't try to calculate the size again, since a
// case, GetDialogSize() should leave its argument untouched. In all // preferred size has been set explicitly from the renderer.
// other cases, GetDialogSize() will overwrite the passed-in size. gfx::Size size = WebView::CalculatePreferredSize();
gfx::Size size = auto_resize_size_;
GetWebDialogDelegate()->GetDialogSize(&size); GetWebDialogDelegate()->GetDialogSize(&size);
return size; return size;
} }
gfx::Size ConstrainedDialogWebView::GetMinimumSize() const { gfx::Size ConstrainedDialogWebView::GetMinimumSize() const {
return min_size_; return min_size();
} }
gfx::Size ConstrainedDialogWebView::GetMaximumSize() const { gfx::Size ConstrainedDialogWebView::GetMaximumSize() const {
return !max_size_.IsEmpty() ? max_size_ : WebView::GetMaximumSize(); return !max_size().IsEmpty() ? max_size() : WebView::GetMaximumSize();
}
void ConstrainedDialogWebView::RenderViewCreated(
content::RenderViewHost* render_view_host) {
if (!max_size_.IsEmpty())
EnableAutoResize();
}
void ConstrainedDialogWebView::RenderViewHostChanged(
content::RenderViewHost* old_host,
content::RenderViewHost* new_host) {
if (!max_size_.IsEmpty())
EnableAutoResize();
} }
void ConstrainedDialogWebView::DocumentOnLoadCompletedInMainFrame() { void ConstrainedDialogWebView::DocumentOnLoadCompletedInMainFrame() {
if (!max_size_.IsEmpty() && initiator_observer_.web_contents()) { if (!max_size().IsEmpty() && initiator_observer_.web_contents()) {
content::WebContents* top_level_web_contents = content::WebContents* top_level_web_contents =
constrained_window::GetTopLevelWebContents( constrained_window::GetTopLevelWebContents(
initiator_observer_.web_contents()); initiator_observer_.web_contents());
...@@ -383,12 +351,6 @@ void ConstrainedDialogWebView::DocumentOnLoadCompletedInMainFrame() { ...@@ -383,12 +351,6 @@ void ConstrainedDialogWebView::DocumentOnLoadCompletedInMainFrame() {
} }
} }
void ConstrainedDialogWebView::EnableAutoResize() {
content::RenderWidgetHostView* render_widget_host_view =
GetWebContents()->GetRenderWidgetHostView();
render_widget_host_view->EnableAutoResize(min_size_, max_size_);
}
} // namespace } // namespace
ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( ConstrainedWebDialogDelegate* ShowConstrainedWebDialog(
......
...@@ -36,6 +36,11 @@ ExtensionViewViews::ExtensionViewViews(extensions::ExtensionHost* host, ...@@ -36,6 +36,11 @@ ExtensionViewViews::ExtensionViewViews(extensions::ExtensionHost* host,
browser_(browser), browser_(browser),
container_(nullptr) { container_(nullptr) {
SetWebContents(host_->web_contents()); SetWebContents(host_->web_contents());
if (host->extension_host_type() == extensions::VIEW_TYPE_EXTENSION_POPUP) {
EnableSizingFromWebContents(
gfx::Size(ExtensionPopup::kMinWidth, ExtensionPopup::kMinHeight),
gfx::Size(ExtensionPopup::kMaxWidth, ExtensionPopup::kMaxHeight));
}
} }
ExtensionViewViews::~ExtensionViewViews() { ExtensionViewViews::~ExtensionViewViews() {
...@@ -80,18 +85,12 @@ void ExtensionViewViews::ResizeDueToAutoResize( ...@@ -80,18 +85,12 @@ void ExtensionViewViews::ResizeDueToAutoResize(
return; return;
} }
if (new_size != GetPreferredSize()) WebView::ResizeDueToAutoResize(web_contents, new_size);
SetPreferredSize(new_size);
} }
void ExtensionViewViews::RenderViewCreated( void ExtensionViewViews::RenderViewCreated(
content::RenderViewHost* render_view_host) { content::RenderViewHost* render_view_host) {
extensions::ViewType host_type = host_->extension_host_type(); WebView::RenderViewCreated(render_view_host);
if (host_type == extensions::VIEW_TYPE_EXTENSION_POPUP) {
host_->host_contents()->GetRenderWidgetHostView()->EnableAutoResize(
gfx::Size(ExtensionPopup::kMinWidth, ExtensionPopup::kMinHeight),
gfx::Size(ExtensionPopup::kMaxWidth, ExtensionPopup::kMaxHeight));
}
} }
void ExtensionViewViews::HandleKeyboardEvent( void ExtensionViewViews::HandleKeyboardEvent(
......
...@@ -91,6 +91,8 @@ void WebView::SetWebContents(content::WebContents* replacement) { ...@@ -91,6 +91,8 @@ void WebView::SetWebContents(content::WebContents* replacement) {
} }
AttachWebContents(); AttachWebContents();
NotifyAccessibilityWebContentsChanged(); NotifyAccessibilityWebContentsChanged();
MaybeEnableAutoResize();
} }
void WebView::SetEmbedFullscreenWidgetMode(bool enable) { void WebView::SetEmbedFullscreenWidgetMode(bool enable) {
...@@ -109,6 +111,14 @@ void WebView::SetFastResize(bool fast_resize) { ...@@ -109,6 +111,14 @@ void WebView::SetFastResize(bool fast_resize) {
holder_->set_fast_resize(fast_resize); holder_->set_fast_resize(fast_resize);
} }
void WebView::EnableSizingFromWebContents(const gfx::Size& min_size,
const gfx::Size& max_size) {
DCHECK(!max_size.IsEmpty());
min_size_ = min_size;
max_size_ = max_size;
MaybeEnableAutoResize();
}
void WebView::SetResizeBackgroundColor(SkColor resize_background_color) { void WebView::SetResizeBackgroundColor(SkColor resize_background_color) {
holder_->set_resize_background_color(resize_background_color); holder_->set_resize_background_color(resize_background_color);
} }
...@@ -271,6 +281,10 @@ bool WebView::EmbedsFullscreenWidget() const { ...@@ -271,6 +281,10 @@ bool WebView::EmbedsFullscreenWidget() const {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// WebView, content::WebContentsObserver implementation: // WebView, content::WebContentsObserver implementation:
void WebView::RenderViewCreated(content::RenderViewHost* render_view_host) {
MaybeEnableAutoResize();
}
void WebView::RenderViewReady() { void WebView::RenderViewReady() {
UpdateCrashedOverlayView(); UpdateCrashedOverlayView();
NotifyAccessibilityWebContentsChanged(); NotifyAccessibilityWebContentsChanged();
...@@ -283,6 +297,8 @@ void WebView::RenderViewDeleted(content::RenderViewHost* render_view_host) { ...@@ -283,6 +297,8 @@ void WebView::RenderViewDeleted(content::RenderViewHost* render_view_host) {
void WebView::RenderViewHostChanged(content::RenderViewHost* old_host, void WebView::RenderViewHostChanged(content::RenderViewHost* old_host,
content::RenderViewHost* new_host) { content::RenderViewHost* new_host) {
MaybeEnableAutoResize();
if (HasFocus()) if (HasFocus())
OnFocus(); OnFocus();
NotifyAccessibilityWebContentsChanged(); NotifyAccessibilityWebContentsChanged();
...@@ -326,6 +342,14 @@ void WebView::RenderProcessGone(base::TerminationStatus status) { ...@@ -326,6 +342,14 @@ void WebView::RenderProcessGone(base::TerminationStatus status) {
NotifyAccessibilityWebContentsChanged(); NotifyAccessibilityWebContentsChanged();
} }
void WebView::ResizeDueToAutoResize(content::WebContents* source,
const gfx::Size& new_size) {
if (source != web_contents())
return;
SetPreferredSize(new_size);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// WebView, private: // WebView, private:
...@@ -411,4 +435,15 @@ std::unique_ptr<content::WebContents> WebView::CreateWebContents( ...@@ -411,4 +435,15 @@ std::unique_ptr<content::WebContents> WebView::CreateWebContents(
return contents; return contents;
} }
void WebView::MaybeEnableAutoResize() {
if (max_size_.IsEmpty() || !web_contents() ||
!web_contents()->GetRenderWidgetHostView()) {
return;
}
content::RenderWidgetHostView* render_widget_host_view =
web_contents()->GetRenderWidgetHostView();
render_widget_host_view->EnableAutoResize(min_size_, max_size_);
}
} // namespace views } // namespace views
...@@ -76,6 +76,11 @@ class WEBVIEW_EXPORT WebView : public View, ...@@ -76,6 +76,11 @@ class WEBVIEW_EXPORT WebView : public View,
// resizing performance during interactive resizes and animations. // resizing performance during interactive resizes and animations.
void SetFastResize(bool fast_resize); void SetFastResize(bool fast_resize);
// If enabled, this will make the WebView's preferred size dependent on the
// WebContents' size.
void EnableSizingFromWebContents(const gfx::Size& min_size,
const gfx::Size& max_size);
// Set the background color to use while resizing with a clip. This is white // Set the background color to use while resizing with a clip. This is white
// by default. // by default.
void SetResizeBackgroundColor(SkColor resize_background_color); void SetResizeBackgroundColor(SkColor resize_background_color);
...@@ -94,6 +99,10 @@ class WEBVIEW_EXPORT WebView : public View, ...@@ -94,6 +99,10 @@ class WEBVIEW_EXPORT WebView : public View,
// Overridden from View: // Overridden from View:
const char* GetClassName() const override; const char* GetClassName() const override;
// Overridden from content::WebContentsDelegate:
void ResizeDueToAutoResize(content::WebContents* source,
const gfx::Size& new_size) override;
NativeViewHost* holder() { return holder_; } NativeViewHost* holder() { return holder_; }
using WebContentsCreator = using WebContentsCreator =
base::RepeatingCallback<std::unique_ptr<content::WebContents>( base::RepeatingCallback<std::unique_ptr<content::WebContents>(
...@@ -123,6 +132,9 @@ class WEBVIEW_EXPORT WebView : public View, ...@@ -123,6 +132,9 @@ class WEBVIEW_EXPORT WebView : public View,
virtual void OnLetterboxingChanged() {} virtual void OnLetterboxingChanged() {}
bool is_letterboxing() const { return is_letterboxing_; } bool is_letterboxing() const { return is_letterboxing_; }
const gfx::Size& min_size() const { return min_size_; }
const gfx::Size& max_size() const { return max_size_; }
// Overridden from View: // Overridden from View:
void OnBoundsChanged(const gfx::Rect& previous_bounds) override; void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
void ViewHierarchyChanged( void ViewHierarchyChanged(
...@@ -138,6 +150,7 @@ class WEBVIEW_EXPORT WebView : public View, ...@@ -138,6 +150,7 @@ class WEBVIEW_EXPORT WebView : public View,
bool EmbedsFullscreenWidget() const override; bool EmbedsFullscreenWidget() const override;
// Overridden from content::WebContentsObserver: // Overridden from content::WebContentsObserver:
void RenderViewCreated(content::RenderViewHost* render_view_host) override;
void RenderViewReady() override; void RenderViewReady() override;
void RenderViewDeleted(content::RenderViewHost* render_view_host) override; void RenderViewDeleted(content::RenderViewHost* render_view_host) override;
void RenderViewHostChanged(content::RenderViewHost* old_host, void RenderViewHostChanged(content::RenderViewHost* old_host,
...@@ -167,6 +180,11 @@ class WEBVIEW_EXPORT WebView : public View, ...@@ -167,6 +180,11 @@ class WEBVIEW_EXPORT WebView : public View,
void UpdateCrashedOverlayView(); void UpdateCrashedOverlayView();
void NotifyAccessibilityWebContentsChanged(); void NotifyAccessibilityWebContentsChanged();
// Registers for ResizeDueToAutoResize() notifications from the
// RenderWidgetHostView whenever it is created or changes, if
// EnableSizingFromWebContents() has been called.
void MaybeEnableAutoResize();
// Create a regular or test web contents (based on whether we're running // Create a regular or test web contents (based on whether we're running
// in a unit test or not). // in a unit test or not).
std::unique_ptr<content::WebContents> CreateWebContents( std::unique_ptr<content::WebContents> CreateWebContents(
...@@ -188,6 +206,11 @@ class WEBVIEW_EXPORT WebView : public View, ...@@ -188,6 +206,11 @@ class WEBVIEW_EXPORT WebView : public View,
bool allow_accelerators_; bool allow_accelerators_;
View* crashed_overlay_view_ = nullptr; View* crashed_overlay_view_ = nullptr;
// Minimum and maximum sizes to determine WebView bounds for auto-resizing.
// Empty if auto resize is not enabled.
gfx::Size min_size_;
gfx::Size max_size_;
DISALLOW_COPY_AND_ASSIGN(WebView); DISALLOW_COPY_AND_ASSIGN(WebView);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment