Commit 3b4c6874 authored by Pavel Feldman's avatar Pavel Feldman Committed by Commit Bot

DevTools: introduce clip into Page.captureScreenshot.

Change-Id: I4f926faa964b9eded1ec4ea9a19058c30de05de0
Reviewed-on: https://chromium-review.googlesource.com/564303
Commit-Queue: Eric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485931}
parent f8059bbc
...@@ -226,17 +226,28 @@ Response EmulationHandler::SetDeviceMetricsOverride( ...@@ -226,17 +226,28 @@ Response EmulationHandler::SetDeviceMetricsOverride(
device_emulation_enabled_ = true; device_emulation_enabled_ = true;
device_emulation_params_ = params; device_emulation_params_ = params;
// if (width > 0 && height > 0) if (width > 0 && height > 0) {
// widget_host->GetView()->SetSize(gfx::Size(width, height)); original_view_size_ = widget_host->GetView()->GetViewBounds().size();
widget_host->GetView()->SetSize(gfx::Size(width, height));
} else {
original_view_size_ = gfx::Size();
}
UpdateDeviceEmulationState(); UpdateDeviceEmulationState();
return Response::OK(); return Response::OK();
} }
Response EmulationHandler::ClearDeviceMetricsOverride() { Response EmulationHandler::ClearDeviceMetricsOverride() {
RenderWidgetHostImpl* widget_host =
host_ ? host_->GetRenderWidgetHost() : nullptr;
if (!widget_host)
return Response::Error("Target does not support metrics override");
if (!device_emulation_enabled_) if (!device_emulation_enabled_)
return Response::OK(); return Response::OK();
device_emulation_enabled_ = false; device_emulation_enabled_ = false;
if (original_view_size_.width())
widget_host->GetView()->SetSize(original_view_size_);
original_view_size_ = gfx::Size();
UpdateDeviceEmulationState(); UpdateDeviceEmulationState();
return Response::OK(); return Response::OK();
} }
......
...@@ -68,6 +68,7 @@ class EmulationHandler : public DevToolsDomainHandler, ...@@ -68,6 +68,7 @@ class EmulationHandler : public DevToolsDomainHandler,
std::string touch_emulation_configuration_; std::string touch_emulation_configuration_;
bool device_emulation_enabled_; bool device_emulation_enabled_;
gfx::Size original_view_size_;
blink::WebDeviceEmulationParams device_emulation_params_; blink::WebDeviceEmulationParams device_emulation_params_;
RenderFrameHostImpl* host_; RenderFrameHostImpl* host_;
......
...@@ -355,6 +355,7 @@ Response PageHandler::NavigateToHistoryEntry(int entry_id) { ...@@ -355,6 +355,7 @@ Response PageHandler::NavigateToHistoryEntry(int entry_id) {
void PageHandler::CaptureScreenshot( void PageHandler::CaptureScreenshot(
Maybe<std::string> format, Maybe<std::string> format,
Maybe<int> quality, Maybe<int> quality,
Maybe<Page::Viewport> clip,
Maybe<bool> from_surface, Maybe<bool> from_surface,
std::unique_ptr<CaptureScreenshotCallback> callback) { std::unique_ptr<CaptureScreenshotCallback> callback) {
if (!host_ || !host_->GetRenderWidgetHost()) { if (!host_ || !host_->GetRenderWidgetHost()) {
...@@ -381,9 +382,25 @@ void PageHandler::CaptureScreenshot( ...@@ -381,9 +382,25 @@ void PageHandler::CaptureScreenshot(
modified_params.scale = dpfactor; modified_params.scale = dpfactor;
modified_params.view_size.width = emulated_view_size.width(); modified_params.view_size.width = emulated_view_size.width();
modified_params.view_size.height = emulated_view_size.height(); modified_params.view_size.height = emulated_view_size.height();
if (clip.isJust()) {
// TODO(pfeldman): Modifying here to save on the extra
// RenderWidgetScreenMetricsEmulator / DevToolsEmulator delegate back
// and forth.
modified_params.viewport_offset.x = clip.fromJust()->GetX() * dpfactor;
modified_params.viewport_offset.y = clip.fromJust()->GetY() * dpfactor;
modified_params.viewport_scale = clip.fromJust()->GetScale();
}
emulation_handler_->SetDeviceEmulationParams(modified_params); emulation_handler_->SetDeviceEmulationParams(modified_params);
widget_host->GetView()->SetSize(
gfx::ScaleToFlooredSize(emulated_view_size, dpfactor)); if (clip.isJust()) {
widget_host->GetView()->SetSize(gfx::ScaleToCeiledSize(
gfx::Size(clip.fromJust()->GetWidth(), clip.fromJust()->GetHeight()),
dpfactor * clip.fromJust()->GetScale()));
} else {
widget_host->GetView()->SetSize(
gfx::ScaleToFlooredSize(emulated_view_size, dpfactor));
}
} }
std::string screenshot_format = format.fromMaybe(kPng); std::string screenshot_format = format.fromMaybe(kPng);
......
...@@ -82,6 +82,7 @@ class PageHandler : public DevToolsDomainHandler, ...@@ -82,6 +82,7 @@ class PageHandler : public DevToolsDomainHandler,
void CaptureScreenshot( void CaptureScreenshot(
Maybe<std::string> format, Maybe<std::string> format,
Maybe<int> quality, Maybe<int> quality,
Maybe<Page::Viewport> clip,
Maybe<bool> from_surface, Maybe<bool> from_surface,
std::unique_ptr<CaptureScreenshotCallback> callback) override; std::unique_ptr<CaptureScreenshotCallback> callback) override;
void PrintToPDF(Maybe<bool> landscape, void PrintToPDF(Maybe<bool> landscape,
......
...@@ -155,6 +155,8 @@ IPC_STRUCT_TRAITS_BEGIN(blink::WebDeviceEmulationParams) ...@@ -155,6 +155,8 @@ IPC_STRUCT_TRAITS_BEGIN(blink::WebDeviceEmulationParams)
IPC_STRUCT_TRAITS_MEMBER(fit_to_view) IPC_STRUCT_TRAITS_MEMBER(fit_to_view)
IPC_STRUCT_TRAITS_MEMBER(offset) IPC_STRUCT_TRAITS_MEMBER(offset)
IPC_STRUCT_TRAITS_MEMBER(scale) IPC_STRUCT_TRAITS_MEMBER(scale)
IPC_STRUCT_TRAITS_MEMBER(viewport_offset)
IPC_STRUCT_TRAITS_MEMBER(viewport_scale)
IPC_STRUCT_TRAITS_MEMBER(screen_orientation_angle) IPC_STRUCT_TRAITS_MEMBER(screen_orientation_angle)
IPC_STRUCT_TRAITS_MEMBER(screen_orientation_type) IPC_STRUCT_TRAITS_MEMBER(screen_orientation_type)
IPC_STRUCT_TRAITS_END() IPC_STRUCT_TRAITS_END()
......
...@@ -2691,6 +2691,7 @@ crbug.com/698520 external/wpt/preload/fetch-destination.https.html [ Failure Pas ...@@ -2691,6 +2691,7 @@ crbug.com/698520 external/wpt/preload/fetch-destination.https.html [ Failure Pas
# Sheriff failures 2017-03-10 # Sheriff failures 2017-03-10
crbug.com/402805 inspector-protocol/input/emulateTouchFromMouseEvent.js [ Timeout ] crbug.com/402805 inspector-protocol/input/emulateTouchFromMouseEvent.js [ Timeout ]
crbug.com/741210 [ Mac ] inspector-protocol/emulation/device-emulation-restore.js [ Failure ]
crbug.com/700374 [ Win ] virtual/mojo-loading/http/tests/inspector/workers-on-navigation.html [ Failure Pass ] crbug.com/700374 [ Win ] virtual/mojo-loading/http/tests/inspector/workers-on-navigation.html [ Failure Pass ]
crbug.com/700374 [ Win ] http/tests/inspector/workers-on-navigation.html [ Failure Pass ] crbug.com/700374 [ Win ] http/tests/inspector/workers-on-navigation.html [ Failure Pass ]
......
...@@ -217,7 +217,8 @@ void DevToolsEmulator::EnableDeviceEmulation( ...@@ -217,7 +217,8 @@ void DevToolsEmulator::EnableDeviceEmulation(
emulation_params_.screen_position == params.screen_position && emulation_params_.screen_position == params.screen_position &&
emulation_params_.device_scale_factor == params.device_scale_factor && emulation_params_.device_scale_factor == params.device_scale_factor &&
emulation_params_.offset == params.offset && emulation_params_.offset == params.offset &&
emulation_params_.scale == params.scale) { emulation_params_.scale == params.scale &&
emulation_params_.viewport_offset == params.viewport_offset) {
return; return;
} }
if (emulation_params_.device_scale_factor != params.device_scale_factor || if (emulation_params_.device_scale_factor != params.device_scale_factor ||
...@@ -238,7 +239,11 @@ void DevToolsEmulator::EnableDeviceEmulation( ...@@ -238,7 +239,11 @@ void DevToolsEmulator::EnableDeviceEmulation(
DisableMobileEmulation(); DisableMobileEmulation();
web_view_->SetCompositorDeviceScaleFactorOverride(params.device_scale_factor); web_view_->SetCompositorDeviceScaleFactorOverride(params.device_scale_factor);
UpdateRootLayerTransform(); if (params.viewport_offset.x >= 0)
ForceViewport(params.viewport_offset, params.viewport_scale);
else
ResetViewport();
// TODO(dgozman): mainFrameImpl() is null when it's remote. Figure out how // TODO(dgozman): mainFrameImpl() is null when it's remote. Figure out how
// we end up with enabling emulation in this case. // we end up with enabling emulation in this case.
if (web_view_->MainFrameImpl()) { if (web_view_->MainFrameImpl()) {
...@@ -385,8 +390,10 @@ void DevToolsEmulator::ForceViewport(const WebFloatPoint& position, ...@@ -385,8 +390,10 @@ void DevToolsEmulator::ForceViewport(const WebFloatPoint& position,
} }
void DevToolsEmulator::ResetViewport() { void DevToolsEmulator::ResetViewport() {
if (!viewport_override_) if (!viewport_override_) {
UpdateRootLayerTransform();
return; return;
}
bool original_masking = viewport_override_->original_visual_viewport_masking; bool original_masking = viewport_override_->original_visual_viewport_masking;
viewport_override_ = WTF::nullopt; viewport_override_ = WTF::nullopt;
......
...@@ -213,6 +213,19 @@ ...@@ -213,6 +213,19 @@
{ "name": "clientHeight", "type": "number", "description": "Height (CSS pixels), excludes scrollbar if present." }, { "name": "clientHeight", "type": "number", "description": "Height (CSS pixels), excludes scrollbar if present." },
{ "name": "scale", "type": "number", "description": "Scale relative to the ideal viewport (size at width=device-width)." } { "name": "scale", "type": "number", "description": "Scale relative to the ideal viewport (size at width=device-width)." }
] ]
},
{
"id": "Viewport",
"type": "object",
"description": "Viewport for capturing screenshot.",
"experimental": true,
"properties": [
{ "name": "x", "type": "number", "description": "X offset in CSS pixels." },
{ "name": "y", "type": "number", "description": "Y offset in CSS pixels" },
{ "name": "width", "type": "number", "description": "Rectangle width in CSS pixels" },
{ "name": "height", "type": "number", "description": "Rectangle height in CSS pixels" },
{ "name": "scale", "type": "number", "description": "Page scale factor." }
]
} }
], ],
"commands": [ "commands": [
...@@ -453,6 +466,7 @@ ...@@ -453,6 +466,7 @@
"parameters": [ "parameters": [
{ "name": "format", "type": "string", "optional": true, "enum": ["jpeg", "png"], "description": "Image compression format (defaults to png)." }, { "name": "format", "type": "string", "optional": true, "enum": ["jpeg", "png"], "description": "Image compression format (defaults to png)." },
{ "name": "quality", "type": "integer", "optional": true, "description": "Compression quality from range [0..100] (jpeg only)." }, { "name": "quality", "type": "integer", "optional": true, "description": "Compression quality from range [0..100] (jpeg only)." },
{ "name": "clip", "$ref": "Viewport", "optional": true, "description": "Capture the screenshot of a given region only.", "experimental": true },
{ "name": "fromSurface", "type": "boolean", "optional": true, "description": "Capture the screenshot from the surface, rather than the view. Defaults to true.", "experimental": true } { "name": "fromSurface", "type": "boolean", "optional": true, "description": "Capture the screenshot from the surface, rather than the view. Defaults to true.", "experimental": true }
], ],
"returns": [ "returns": [
......
...@@ -46,7 +46,7 @@ SDK.ScreenCaptureModel = class extends SDK.SDKModel { ...@@ -46,7 +46,7 @@ SDK.ScreenCaptureModel = class extends SDK.SDKModel {
* @return {!Promise<?string>} * @return {!Promise<?string>}
*/ */
captureScreenshot(format, quality) { captureScreenshot(format, quality) {
return this._agent.captureScreenshot(format, quality, true); return this._agent.captureScreenshot(format, quality, undefined, true);
} }
/** /**
......
...@@ -44,6 +44,13 @@ struct WebDeviceEmulationParams { ...@@ -44,6 +44,13 @@ struct WebDeviceEmulationParams {
// Scale of emulated view inside available space, not in fit to view mode. // Scale of emulated view inside available space, not in fit to view mode.
float scale; float scale;
// Forced viewport offset for screenshots during emulation, (-1, -1) for
// disabled.
WebFloatPoint viewport_offset;
// Viewport scale for screenshots during emulation, 0 for current.
float viewport_scale;
// Optional screen orientation type, with WebScreenOrientationUndefined // Optional screen orientation type, with WebScreenOrientationUndefined
// value meaning no emulation necessary. // value meaning no emulation necessary.
WebScreenOrientationType screen_orientation_type; WebScreenOrientationType screen_orientation_type;
...@@ -56,6 +63,8 @@ struct WebDeviceEmulationParams { ...@@ -56,6 +63,8 @@ struct WebDeviceEmulationParams {
device_scale_factor(0), device_scale_factor(0),
fit_to_view(false), fit_to_view(false),
scale(1), scale(1),
viewport_offset(-1, -1),
viewport_scale(0),
screen_orientation_type(kWebScreenOrientationUndefined), screen_orientation_type(kWebScreenOrientationUndefined),
screen_orientation_angle(0) {} screen_orientation_angle(0) {}
}; };
...@@ -68,7 +77,9 @@ inline bool operator==(const WebDeviceEmulationParams& a, ...@@ -68,7 +77,9 @@ inline bool operator==(const WebDeviceEmulationParams& a,
a.view_size == b.view_size && a.fit_to_view == b.fit_to_view && a.view_size == b.view_size && a.fit_to_view == b.fit_to_view &&
a.offset == b.offset && a.scale == b.scale && a.offset == b.offset && a.scale == b.scale &&
a.screen_orientation_type == b.screen_orientation_type && a.screen_orientation_type == b.screen_orientation_type &&
a.screen_orientation_angle == b.screen_orientation_angle; a.screen_orientation_angle == b.screen_orientation_angle &&
a.viewport_offset == b.viewport_offset &&
a.viewport_scale == b.viewport_scale;
} }
inline bool operator!=(const WebDeviceEmulationParams& a, inline bool operator!=(const WebDeviceEmulationParams& a,
......
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