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,
const gfx::Size& max_size);
~ConstrainedDialogWebView() override;
void SetAutoResizeSize(const gfx::Size& auto_resize_size);
// ConstrainedWebDialogDelegate:
const ui::WebDialogDelegate* GetWebDialogDelegate() const override;
ui::WebDialogDelegate* GetWebDialogDelegate() override;
......@@ -95,27 +93,13 @@ class ConstrainedDialogWebView : public views::WebView,
gfx::Size CalculatePreferredSize() const override;
gfx::Size GetMinimumSize() 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;
private:
void EnableAutoResize();
InitiatorWebContentsObserver initiator_observer_;
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);
};
......@@ -157,7 +141,11 @@ class WebDialogWebContentsDelegateViews
if (!initiator_observer_->web_contents())
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 =
constrained_window::GetTopLevelWebContents(
......@@ -234,22 +222,16 @@ ConstrainedDialogWebView::ConstrainedDialogWebView(
impl_(new ConstrainedWebDialogDelegateViews(browser_context,
delegate,
&initiator_observer_,
this)),
min_size_(RestrictToPlatformMinimumSize(min_size)),
max_size_(max_size) {
this)) {
SetWebContents(GetWebContents());
AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
if (!max_size_.IsEmpty())
EnableAutoResize();
if (!max_size.IsEmpty()) {
EnableSizingFromWebContents(RestrictToPlatformMinimumSize(min_size),
max_size);
}
}
ConstrainedDialogWebView::~ConstrainedDialogWebView() {}
void ConstrainedDialogWebView::SetAutoResizeSize(
const gfx::Size& default_size) {
auto_resize_size_ = default_size;
PreferredSizeChanged();
}
const ui::WebDialogDelegate* ConstrainedDialogWebView::GetWebDialogDelegate()
const {
return impl_->GetWebDialogDelegate();
......@@ -342,37 +324,23 @@ gfx::Size ConstrainedDialogWebView::CalculatePreferredSize() const {
return gfx::Size();
// If auto-resizing is enabled and the dialog has been auto-resized,
// |auto_resize_size_| will hold the appropriate current size. In this
// case, GetDialogSize() should leave its argument untouched. In all
// other cases, GetDialogSize() will overwrite the passed-in size.
gfx::Size size = auto_resize_size_;
// View::GetPreferredSize() won't try to calculate the size again, since a
// preferred size has been set explicitly from the renderer.
gfx::Size size = WebView::CalculatePreferredSize();
GetWebDialogDelegate()->GetDialogSize(&size);
return size;
}
gfx::Size ConstrainedDialogWebView::GetMinimumSize() const {
return min_size_;
return min_size();
}
gfx::Size ConstrainedDialogWebView::GetMaximumSize() const {
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();
return !max_size().IsEmpty() ? max_size() : WebView::GetMaximumSize();
}
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 =
constrained_window::GetTopLevelWebContents(
initiator_observer_.web_contents());
......@@ -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
ConstrainedWebDialogDelegate* ShowConstrainedWebDialog(
......
......@@ -36,6 +36,11 @@ ExtensionViewViews::ExtensionViewViews(extensions::ExtensionHost* host,
browser_(browser),
container_(nullptr) {
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() {
......@@ -80,18 +85,12 @@ void ExtensionViewViews::ResizeDueToAutoResize(
return;
}
if (new_size != GetPreferredSize())
SetPreferredSize(new_size);
WebView::ResizeDueToAutoResize(web_contents, new_size);
}
void ExtensionViewViews::RenderViewCreated(
content::RenderViewHost* render_view_host) {
extensions::ViewType host_type = host_->extension_host_type();
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));
}
WebView::RenderViewCreated(render_view_host);
}
void ExtensionViewViews::HandleKeyboardEvent(
......
......@@ -91,6 +91,8 @@ void WebView::SetWebContents(content::WebContents* replacement) {
}
AttachWebContents();
NotifyAccessibilityWebContentsChanged();
MaybeEnableAutoResize();
}
void WebView::SetEmbedFullscreenWidgetMode(bool enable) {
......@@ -109,6 +111,14 @@ void WebView::SetFastResize(bool 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) {
holder_->set_resize_background_color(resize_background_color);
}
......@@ -271,6 +281,10 @@ bool WebView::EmbedsFullscreenWidget() const {
////////////////////////////////////////////////////////////////////////////////
// WebView, content::WebContentsObserver implementation:
void WebView::RenderViewCreated(content::RenderViewHost* render_view_host) {
MaybeEnableAutoResize();
}
void WebView::RenderViewReady() {
UpdateCrashedOverlayView();
NotifyAccessibilityWebContentsChanged();
......@@ -283,6 +297,8 @@ void WebView::RenderViewDeleted(content::RenderViewHost* render_view_host) {
void WebView::RenderViewHostChanged(content::RenderViewHost* old_host,
content::RenderViewHost* new_host) {
MaybeEnableAutoResize();
if (HasFocus())
OnFocus();
NotifyAccessibilityWebContentsChanged();
......@@ -326,6 +342,14 @@ void WebView::RenderProcessGone(base::TerminationStatus status) {
NotifyAccessibilityWebContentsChanged();
}
void WebView::ResizeDueToAutoResize(content::WebContents* source,
const gfx::Size& new_size) {
if (source != web_contents())
return;
SetPreferredSize(new_size);
}
////////////////////////////////////////////////////////////////////////////////
// WebView, private:
......@@ -411,4 +435,15 @@ std::unique_ptr<content::WebContents> WebView::CreateWebContents(
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
......@@ -76,6 +76,11 @@ class WEBVIEW_EXPORT WebView : public View,
// resizing performance during interactive resizes and animations.
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
// by default.
void SetResizeBackgroundColor(SkColor resize_background_color);
......@@ -94,6 +99,10 @@ class WEBVIEW_EXPORT WebView : public View,
// Overridden from View:
const char* GetClassName() const override;
// Overridden from content::WebContentsDelegate:
void ResizeDueToAutoResize(content::WebContents* source,
const gfx::Size& new_size) override;
NativeViewHost* holder() { return holder_; }
using WebContentsCreator =
base::RepeatingCallback<std::unique_ptr<content::WebContents>(
......@@ -123,6 +132,9 @@ class WEBVIEW_EXPORT WebView : public View,
virtual void OnLetterboxingChanged() {}
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:
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
void ViewHierarchyChanged(
......@@ -138,6 +150,7 @@ class WEBVIEW_EXPORT WebView : public View,
bool EmbedsFullscreenWidget() const override;
// Overridden from content::WebContentsObserver:
void RenderViewCreated(content::RenderViewHost* render_view_host) override;
void RenderViewReady() override;
void RenderViewDeleted(content::RenderViewHost* render_view_host) override;
void RenderViewHostChanged(content::RenderViewHost* old_host,
......@@ -167,6 +180,11 @@ class WEBVIEW_EXPORT WebView : public View,
void UpdateCrashedOverlayView();
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
// in a unit test or not).
std::unique_ptr<content::WebContents> CreateWebContents(
......@@ -188,6 +206,11 @@ class WEBVIEW_EXPORT WebView : public View,
bool allow_accelerators_;
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);
};
......
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