When emulation is enabled, set the new render view size to be the current emulated view size.
The size was set to the window size, which caused the screenshot in the trace to be incorrect.
Steps to reproduce the bug:
1. run chrome with --remote-debugging-port=9222 --headless --window-size=400,400
2. send devtools commands:
1) Emulation.setDeviceMetricsOverride({width: 800, height: 600, deviceScaleFactor: 0, mobile: false});
2) Tracing.start({categories: '-*,disabled-by-default-devtools.screenshot'})
3) Page.navigate({url: "http://www.example.com"}); // here the screenshot is correct.
4) Page.navigate({url: "http://www.google.com"}); // here the screenshot is incorrect. The screenshot is a stretched version of the upper left 400x400 instead of the 800x600 one.
Bug:
Change-Id: Ib1908c949e8123cbfc6b76d7c5344023f9c88559
Reviewed-on: https://chromium-review.googlesource.com/846611
Commit-Queue: Jianzhou Feng <[email protected]>
Reviewed-by: Dmitry Gozman <[email protected]>
Cr-Commit-Position: refs/heads/master@{#530034}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index 9a152f6..3484edd 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -5457,9 +5457,9 @@
RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
}
-void WebContentsImpl::UpdateRenderViewSizeForRenderManager() {
+void WebContentsImpl::UpdateRenderViewSizeForRenderManager(bool is_main_frame) {
// TODO(brettw) this is a hack. See WebContentsView::SizeContents.
- gfx::Size size = GetSizeForNewRenderView();
+ gfx::Size size = GetSizeForNewRenderView(is_main_frame);
// 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
// here during container initialization and normal window size will be set
// later. In case of tab duplication this resizing to 0x0 prevents setting
@@ -5535,7 +5535,7 @@
// Now that the RenderView has been created, we need to tell it its size.
if (rwh_view)
- rwh_view->SetSize(GetSizeForNewRenderView());
+ rwh_view->SetSize(GetSizeForNewRenderView(true));
}
bool WebContentsImpl::CreateRenderViewForRenderManager(
@@ -5756,9 +5756,11 @@
browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
}
-gfx::Size WebContentsImpl::GetSizeForNewRenderView() {
+gfx::Size WebContentsImpl::GetSizeForNewRenderView(bool is_main_frame) {
gfx::Size size;
- if (delegate_)
+ if (is_main_frame)
+ size = device_emulation_size_;
+ if (size.IsEmpty() && delegate_)
size = delegate_->GetSizeForNewRenderView(this);
if (size.IsEmpty())
size = GetContainerBounds().size();
@@ -5894,6 +5896,34 @@
view_->SetOverscrollControllerEnabled(CanOverscrollContent());
}
+bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
+ device_emulation_size_ = new_size;
+ RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
+
+ const gfx::Size current_size = rwhv->GetViewBounds().size();
+ if (view_size_before_emulation_.IsEmpty())
+ view_size_before_emulation_ = current_size;
+
+ if (current_size != new_size)
+ rwhv->SetSize(new_size);
+
+ return current_size != new_size;
+}
+
+void WebContentsImpl::ClearDeviceEmulationSize() {
+ RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
+ // WebContentsView could get resized during emulation, which also resizes
+ // RWHV. If it happens, assume user would like to keep using the size after
+ // emulation.
+ // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
+ // WebContentsView size changes).
+ if (!view_size_before_emulation_.IsEmpty() &&
+ rwhv->GetViewBounds().size() == device_emulation_size_)
+ rwhv->SetSize(view_size_before_emulation_);
+ device_emulation_size_ = gfx::Size();
+ view_size_before_emulation_ = gfx::Size();
+}
+
void WebContentsImpl::MediaStartedPlaying(
const WebContentsObserver::MediaPlayerInfo& media_info,
const WebContentsObserver::MediaPlayerId& id) {