Skip to content

Commit

Permalink
Propagate emulated window segments to renderer
Browse files Browse the repository at this point in the history
Ensure that window_segments on WebDeviceEmulationParams are copied
over IPC and consumed in the renderer. The params are stored on
RenderWidgetScreenMetricsEmulator which uses these to call back to
set the emulated values on the RenderWidget. The original segments
are also stored so that they can be restored if the segments are no
longer emulated (either due to emulation being non longer applied
or if a future setDeviceMetricsOverride does not contain the segments).

Bug: 1099026

Change-Id: I9f32e432dc6e600ce3843e7774e7e0da53a97a5a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2262193
Commit-Queue: Daniel Libby <dlibby@microsoft.com>
Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#788997}
  • Loading branch information
dlibby- authored and Commit Bot committed Jul 16, 2020
1 parent 35ed61d commit a75943f
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 26 deletions.
46 changes: 24 additions & 22 deletions content/renderer/render_widget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -588,24 +588,6 @@ void RenderWidget::OnUpdateVisualProperties(
if (for_frame()) {
SetZoomLevel(visual_properties.zoom_level);

if (root_widget_window_segments_ !=
visual_properties.root_widget_window_segments) {
root_widget_window_segments_ =
visual_properties.root_widget_window_segments;

blink::WebVector<blink::WebRect> web_segments;
web_segments.reserve(root_widget_window_segments_.size());
for (const auto& segment : root_widget_window_segments_)
web_segments.emplace_back(segment);

GetWebWidget()->SetWindowSegments(std::move(web_segments));

// Propagate changes down to child local root RenderWidgets in other frame
// trees/processes.
for (auto& observer : render_frame_proxies_)
observer.OnRootWindowSegmentsChanged(root_widget_window_segments_);
}

bool capture_sequence_number_changed =
visual_properties.capture_sequence_number !=
last_capture_sequence_number_;
Expand Down Expand Up @@ -655,8 +637,12 @@ void RenderWidget::OnUpdateVisualProperties(
// emulation.
device_emulator_->OnSynchronizeVisualProperties(
visual_properties.screen_info, visual_properties.new_size,
visual_properties.visible_viewport_size);
visual_properties.visible_viewport_size,
visual_properties.root_widget_window_segments);
} else {
if (for_frame())
SetRootWindowSegments(visual_properties.root_widget_window_segments);

// We can ignore browser-initialized resizing during synchronous
// (renderer-controlled) mode, unless it is switching us to/from
// fullsreen mode or changing the device scale factor.
Expand Down Expand Up @@ -787,9 +773,6 @@ void RenderWidget::OnEnableDeviceEmulation(
window_screen_rect_);
}
device_emulator_->ChangeEmulationParams(params);
// TODO: crbug.com/1099026
// https://chromium-review.googlesource.com/c/chromium/src/+/2262193/1
// Update root_widget_window_segments here.
}

void RenderWidget::OnDisableDeviceEmulation() {
Expand Down Expand Up @@ -854,6 +837,25 @@ void RenderWidget::SetZoomLevel(double zoom_level) {
}
}

void RenderWidget::SetRootWindowSegments(
const std::vector<gfx::Rect>& root_window_segments) {
if (root_widget_window_segments_ != root_window_segments) {
root_widget_window_segments_ = root_window_segments;

blink::WebVector<blink::WebRect> web_segments;
web_segments.reserve(root_widget_window_segments_.size());
for (const auto& segment : root_widget_window_segments_)
web_segments.emplace_back(segment);

GetWebWidget()->SetWindowSegments(std::move(web_segments));

// Propagate changes down to child local root RenderWidgets in other frame
// trees/processes.
for (auto& observer : render_frame_proxies_)
observer.OnRootWindowSegmentsChanged(root_widget_window_segments_);
}
}

void RenderWidget::OnWasHidden() {
// A provisional frame widget will never be hidden since that would require it
// to be shown first. A frame must be attached to the frame tree before
Expand Down
2 changes: 2 additions & 0 deletions content/renderer/render_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ class CONTENT_EXPORT RenderWidget
const gfx::Size& visible_viewport_size) override;
void SetScreenRects(const gfx::Rect& widget_screen_rect,
const gfx::Rect& window_screen_rect) override;
void SetRootWindowSegments(
const std::vector<gfx::Rect>& root_window_segments) override;

// blink::WebWidgetClient
void ScheduleAnimation() override;
Expand Down
13 changes: 12 additions & 1 deletion content/renderer/render_widget_screen_metrics_emulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void RenderWidgetScreenMetricsEmulator::DisableAndApply() {
delegate_->SetScreenMetricsEmulationParameters(false, emulation_params_);
delegate_->SetScreenRects(original_view_screen_rect_,
original_window_screen_rect_);
delegate_->SetRootWindowSegments(original_root_window_segments_);
delegate_->SetScreenInfoAndSize(original_screen_info_, original_widget_size_,
original_visible_viewport_size_);
}
Expand Down Expand Up @@ -130,6 +131,14 @@ void RenderWidgetScreenMetricsEmulator::Apply() {
delegate_->SetScreenRects(gfx::Rect(widget_pos, widget_size),
gfx::Rect(window_pos, window_size));

// If there are no emulated window segments, use the original ones - when we
// switch from having them to having none, we need to fallback to the original
// value. If there never were any emulated segments this is a no-op.
bool emulated_window_segments = emulation_params_.window_segments.size();
delegate_->SetRootWindowSegments(emulated_window_segments
? emulation_params_.window_segments
: original_root_window_segments_);

blink::ScreenInfo screen_info = original_screen_info();
screen_info.device_scale_factor = device_scale_factor;
screen_info.rect = screen_rect;
Expand All @@ -143,10 +152,12 @@ void RenderWidgetScreenMetricsEmulator::Apply() {
void RenderWidgetScreenMetricsEmulator::OnSynchronizeVisualProperties(
const blink::ScreenInfo& screen_info,
const gfx::Size& widget_size,
const gfx::Size& visible_viewport_size) {
const gfx::Size& visible_viewport_size,
const std::vector<gfx::Rect>& root_window_segments) {
original_screen_info_ = screen_info;
original_widget_size_ = widget_size;
original_visible_viewport_size_ = visible_viewport_size;
original_root_window_segments_ = root_window_segments;
Apply();
}

Expand Down
10 changes: 7 additions & 3 deletions content/renderer/render_widget_screen_metrics_emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ class CONTENT_EXPORT RenderWidgetScreenMetricsEmulator {
// Sets new parameters and applies them to the RenderWidget.
void ChangeEmulationParams(const blink::WebDeviceEmulationParams& params);

void OnSynchronizeVisualProperties(const blink::ScreenInfo& screen_info,
const gfx::Size& widget_size,
const gfx::Size& visible_viewport_size);
void OnSynchronizeVisualProperties(
const blink::ScreenInfo& screen_info,
const gfx::Size& widget_size,
const gfx::Size& visible_viewport_size,
const std::vector<gfx::Rect>& root_window_segments);

void OnUpdateScreenRects(const gfx::Rect& view_screen_rect,
const gfx::Rect& window_screen_rect);

Expand All @@ -85,6 +88,7 @@ class CONTENT_EXPORT RenderWidgetScreenMetricsEmulator {
gfx::Size original_visible_viewport_size_;
gfx::Rect original_view_screen_rect_;
gfx::Rect original_window_screen_rect_;
std::vector<gfx::Rect> original_root_window_segments_;

DISALLOW_COPY_AND_ASSIGN(RenderWidgetScreenMetricsEmulator);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class CONTENT_EXPORT RenderWidgetScreenMetricsEmulatorDelegate {
virtual void SetScreenRects(const gfx::Rect& view_screen_rect,
const gfx::Rect& window_screen_rect) = 0;

virtual void SetRootWindowSegments(
const std::vector<gfx::Rect>& root_window_segments) = 0;

protected:
virtual ~RenderWidgetScreenMetricsEmulatorDelegate() {}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Tests that device emulation of window segments is propagated and powers getWindowSegments API.
No segments:
Main frame segments:
1
0 0 800 600
Iframe segments:
1
0 0 800 600
Side-by-side segments
Main frame segments:
2
0 0 390 600
410 0 390 600
Iframe segments:
2
0 0 390 600
410 0 390 600
Unspecified display feature
Main frame segments:
1
0 0 800 600
Iframe segments:
1
0 0 800 600
Stacked segments
Main frame segments:
2
0 0 800 290
0 310 800 290
Iframe segments:
2
0 0 800 290
0 310 800 290
Emulation disabled
Main frame segments:
1
0 0 800 600
Iframe segments:
1
0 0 800 600

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(async function(testRunner) {
let {page, session, dp} = await testRunner.startBlank('Tests that device emulation of window segments is propagated and powers getWindowSegments API.');

let deviceMetrics = {
width: 800,
height: 600,
deviceScaleFactor: 2,
mobile: false,
fitWindow: false,
scale: 2,
screenWidth: 1200,
screenHeight: 1000,
positionX: 110,
positionY: 120,
};

await session.protocol.Emulation.setDeviceMetricsOverride(deviceMetrics);

await session.navigate('../resources/device-emulation.html');

testRunner.log("No segments:");
testRunner.log(await session.evaluate(`dumpWindowSegments()`));

testRunner.log("Side-by-side segments");
deviceMetrics.displayFeature = {
orientation: "vertical",
offset: 390,
maskLength: 20
};
await session.protocol.Emulation.setDeviceMetricsOverride(deviceMetrics);
testRunner.log(await session.evaluate(`dumpWindowSegments()`));

testRunner.log("Unspecified display feature");
delete deviceMetrics.displayFeature;
await session.protocol.Emulation.setDeviceMetricsOverride(deviceMetrics);
testRunner.log(await session.evaluate(`dumpWindowSegments()`));

testRunner.log("Stacked segments");
deviceMetrics.displayFeature = {
orientation: "horizontal",
offset: 290,
maskLength: 20
};
await session.protocol.Emulation.setDeviceMetricsOverride(deviceMetrics);
testRunner.log(await session.evaluate(`dumpWindowSegments()`));

testRunner.log("Emulation disabled");
await dp.Emulation.clearDeviceMetricsOverride();
testRunner.log(await session.evaluate(`dumpWindowSegments()`));

testRunner.completeTest();
})
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@
return results.join("\n");
}

function dumpWindowSegments() {
let output = [];
output.push("Main frame segments:");
output = output.concat(dumpWindowSegmentsForWindow(this));
output.push("Iframe segments:");
output = output.concat(dumpWindowSegmentsForWindow(window.frames[0]));
return output.join("\n");
}

function dumpWindowSegmentsForWindow(window_object) {
let output = [];
output.push(window_object.getWindowSegments().length);
window_object.getWindowSegments().forEach((segment) => {
let segmentString = `${segment.x} ${segment.y} ${segment.width} ${segment.height}`;
output.push(segmentString);
});
return output;
}

function testJS(expr, unit)
{
if (unit === undefined)
Expand Down Expand Up @@ -209,5 +228,6 @@
</head>

<body>
<iframe src="about:blank"></iframe>
</body>
</html>

0 comments on commit a75943f

Please sign in to comment.