OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/renderer_overrides_handler.h" | 5 #include "content/browser/devtools/renderer_overrides_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 using WebKit::WebInputEvent; | 43 using WebKit::WebInputEvent; |
44 using WebKit::WebMouseEvent; | 44 using WebKit::WebMouseEvent; |
45 | 45 |
46 namespace content { | 46 namespace content { |
47 | 47 |
48 namespace { | 48 namespace { |
49 | 49 |
50 static const char kPng[] = "png"; | 50 static const char kPng[] = "png"; |
51 static const char kJpeg[] = "jpeg"; | 51 static const char kJpeg[] = "jpeg"; |
52 static int kDefaultScreenshotQuality = 80; | 52 static int kDefaultScreenshotQuality = 80; |
53 | 53 static int kFrameRateThresholdMs = 100; |
54 void ParseCaptureParameters(DevToolsProtocol::Command* command, | |
55 std::string* format, | |
56 int* quality, | |
57 double* scale) { | |
58 *quality = kDefaultScreenshotQuality; | |
59 *scale = 1; | |
60 base::DictionaryValue* params = command->params(); | |
61 if (params) { | |
62 params->GetString(devtools::Page::captureScreenshot::kParamFormat, | |
63 format); | |
64 params->GetInteger(devtools::Page::captureScreenshot::kParamQuality, | |
65 quality); | |
66 params->GetDouble(devtools::Page::captureScreenshot::kParamScale, | |
67 scale); | |
68 } | |
69 if (format->empty()) | |
70 *format = kPng; | |
71 if (*quality < 0 || *quality > 100) | |
72 *quality = kDefaultScreenshotQuality; | |
73 if (*scale <= 0 || *scale > 1) | |
74 *scale = 1; | |
75 } | |
76 | 54 |
77 void ParseGenericInputParams(base::DictionaryValue* params, | 55 void ParseGenericInputParams(base::DictionaryValue* params, |
78 WebInputEvent* event) { | 56 WebInputEvent* event) { |
79 int modifiers = 0; | 57 int modifiers = 0; |
80 if (params->GetInteger(devtools::Input::kParamModifiers, | 58 if (params->GetInteger(devtools::Input::kParamModifiers, |
81 &modifiers)) { | 59 &modifiers)) { |
82 if (modifiers & 1) | 60 if (modifiers & 1) |
83 event->modifiers |= WebInputEvent::AltKey; | 61 event->modifiers |= WebInputEvent::AltKey; |
84 if (modifiers & 2) | 62 if (modifiers & 2) |
85 event->modifiers |= WebInputEvent::ControlKey; | 63 event->modifiers |= WebInputEvent::ControlKey; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 ViewHostMsg_SwapCompositorFrame::Param param; | 133 ViewHostMsg_SwapCompositorFrame::Param param; |
156 if (!ViewHostMsg_SwapCompositorFrame::Read(&message, ¶m)) | 134 if (!ViewHostMsg_SwapCompositorFrame::Read(&message, ¶m)) |
157 return; | 135 return; |
158 last_compositor_frame_metadata_ = param.b.metadata; | 136 last_compositor_frame_metadata_ = param.b.metadata; |
159 | 137 |
160 if (screencast_command_) | 138 if (screencast_command_) |
161 InnerSwapCompositorFrame(); | 139 InnerSwapCompositorFrame(); |
162 } | 140 } |
163 | 141 |
164 void RendererOverridesHandler::InnerSwapCompositorFrame() { | 142 void RendererOverridesHandler::InnerSwapCompositorFrame() { |
| 143 if ((base::TimeTicks::Now() - last_frame_time_).InMilliseconds() < |
| 144 kFrameRateThresholdMs) { |
| 145 return; |
| 146 } |
| 147 |
| 148 last_frame_time_ = base::TimeTicks::Now(); |
165 std::string format; | 149 std::string format; |
166 int quality = kDefaultScreenshotQuality; | 150 int quality = kDefaultScreenshotQuality; |
167 double scale = 1; | 151 double scale = 1; |
168 ParseCaptureParameters(screencast_command_.get(), &format, &quality, &scale); | 152 ParseCaptureParameters(screencast_command_.get(), &format, &quality, &scale); |
169 | 153 |
170 RenderViewHost* host = agent_->GetRenderViewHost(); | 154 RenderViewHost* host = agent_->GetRenderViewHost(); |
171 RenderWidgetHostViewPort* view_port = | 155 RenderWidgetHostViewPort* view_port = |
172 RenderWidgetHostViewPort::FromRWHV(host->GetView()); | 156 RenderWidgetHostViewPort::FromRWHV(host->GetView()); |
173 | 157 |
174 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); | 158 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); |
175 gfx::Size snapshot_size = gfx::ToFlooredSize( | 159 gfx::Size snapshot_size = gfx::ToFlooredSize( |
176 gfx::ScaleSize(view_bounds.size(), scale)); | 160 gfx::ScaleSize(view_bounds.size(), scale)); |
177 | 161 |
178 view_port->CopyFromCompositingSurface( | 162 view_port->CopyFromCompositingSurface( |
179 view_bounds, snapshot_size, | 163 view_bounds, snapshot_size, |
180 base::Bind(&RendererOverridesHandler::ScreenshotCaptured, | 164 base::Bind(&RendererOverridesHandler::ScreenshotCaptured, |
181 weak_factory_.GetWeakPtr(), | 165 weak_factory_.GetWeakPtr(), |
182 scoped_refptr<DevToolsProtocol::Command>(), format, quality, | 166 scoped_refptr<DevToolsProtocol::Command>(), format, quality, |
183 last_compositor_frame_metadata_)); | 167 last_compositor_frame_metadata_)); |
184 } | 168 } |
185 | 169 |
| 170 void RendererOverridesHandler::ParseCaptureParameters( |
| 171 DevToolsProtocol::Command* command, |
| 172 std::string* format, |
| 173 int* quality, |
| 174 double* scale) { |
| 175 RenderViewHost* host = agent_->GetRenderViewHost(); |
| 176 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); |
| 177 |
| 178 *quality = kDefaultScreenshotQuality; |
| 179 *scale = 1; |
| 180 double max_width = -1; |
| 181 double max_height = -1; |
| 182 base::DictionaryValue* params = command->params(); |
| 183 if (params) { |
| 184 params->GetString(devtools::Page::captureScreenshot::kParamFormat, |
| 185 format); |
| 186 params->GetInteger(devtools::Page::captureScreenshot::kParamQuality, |
| 187 quality); |
| 188 params->GetDouble(devtools::Page::captureScreenshot::kParamMaxWidth, |
| 189 &max_width); |
| 190 params->GetDouble(devtools::Page::captureScreenshot::kParamMaxHeight, |
| 191 &max_height); |
| 192 } |
| 193 |
| 194 float device_sf = last_compositor_frame_metadata_.device_scale_factor; |
| 195 |
| 196 if (max_width > 0) |
| 197 *scale = std::min(*scale, max_width / view_bounds.width() / device_sf); |
| 198 if (max_height > 0) |
| 199 *scale = std::min(*scale, max_height / view_bounds.height() / device_sf); |
| 200 |
| 201 if (format->empty()) |
| 202 *format = kPng; |
| 203 if (*quality < 0 || *quality > 100) |
| 204 *quality = kDefaultScreenshotQuality; |
| 205 if (*scale <= 0) |
| 206 *scale = 0.1; |
| 207 if (*scale > 5) |
| 208 *scale = 5; |
| 209 } |
186 | 210 |
187 // DOM agent handlers -------------------------------------------------------- | 211 // DOM agent handlers -------------------------------------------------------- |
188 | 212 |
189 scoped_refptr<DevToolsProtocol::Response> | 213 scoped_refptr<DevToolsProtocol::Response> |
190 RendererOverridesHandler::GrantPermissionsForSetFileInputFiles( | 214 RendererOverridesHandler::GrantPermissionsForSetFileInputFiles( |
191 scoped_refptr<DevToolsProtocol::Command> command) { | 215 scoped_refptr<DevToolsProtocol::Command> command) { |
192 base::DictionaryValue* params = command->params(); | 216 base::DictionaryValue* params = command->params(); |
193 base::ListValue* file_list = NULL; | 217 base::ListValue* file_list = NULL; |
194 const char* param = | 218 const char* param = |
195 devtools::DOM::setFileInputFiles::kParamFiles; | 219 devtools::DOM::setFileInputFiles::kParamFiles; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 RendererOverridesHandler::PageStartScreencast( | 347 RendererOverridesHandler::PageStartScreencast( |
324 scoped_refptr<DevToolsProtocol::Command> command) { | 348 scoped_refptr<DevToolsProtocol::Command> command) { |
325 screencast_command_ = command; | 349 screencast_command_ = command; |
326 InnerSwapCompositorFrame(); | 350 InnerSwapCompositorFrame(); |
327 return command->SuccessResponse(NULL); | 351 return command->SuccessResponse(NULL); |
328 } | 352 } |
329 | 353 |
330 scoped_refptr<DevToolsProtocol::Response> | 354 scoped_refptr<DevToolsProtocol::Response> |
331 RendererOverridesHandler::PageStopScreencast( | 355 RendererOverridesHandler::PageStopScreencast( |
332 scoped_refptr<DevToolsProtocol::Command> command) { | 356 scoped_refptr<DevToolsProtocol::Command> command) { |
| 357 last_frame_time_ = base::TimeTicks(); |
333 screencast_command_ = NULL; | 358 screencast_command_ = NULL; |
334 return command->SuccessResponse(NULL); | 359 return command->SuccessResponse(NULL); |
335 } | 360 } |
336 | 361 |
337 void RendererOverridesHandler::ScreenshotCaptured( | 362 void RendererOverridesHandler::ScreenshotCaptured( |
338 scoped_refptr<DevToolsProtocol::Command> command, | 363 scoped_refptr<DevToolsProtocol::Command> command, |
339 const std::string& format, | 364 const std::string& format, |
340 int quality, | 365 int quality, |
341 const cc::CompositorFrameMetadata& metadata, | 366 const cc::CompositorFrameMetadata& metadata, |
342 bool success, | 367 bool success, |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 else if (type == "mouseReleased") | 471 else if (type == "mouseReleased") |
447 mouse_event.type = WebInputEvent::MouseUp; | 472 mouse_event.type = WebInputEvent::MouseUp; |
448 else if (type == "mouseMoved") | 473 else if (type == "mouseMoved") |
449 mouse_event.type = WebInputEvent::MouseMove; | 474 mouse_event.type = WebInputEvent::MouseMove; |
450 else | 475 else |
451 return NULL; | 476 return NULL; |
452 } else { | 477 } else { |
453 return NULL; | 478 return NULL; |
454 } | 479 } |
455 | 480 |
456 int x; | 481 if (!params->GetInteger(devtools::kParamX, &mouse_event.x) || |
457 int y; | 482 !params->GetInteger(devtools::kParamY, &mouse_event.y)) { |
458 if (!params->GetInteger(devtools::kParamX, &x) || | |
459 !params->GetInteger(devtools::kParamY, &y)) { | |
460 return NULL; | 483 return NULL; |
461 } | 484 } |
462 | 485 |
463 float device_scale_factor = ui::GetScaleFactorScale( | |
464 GetScaleFactorForView(host->GetView())); | |
465 mouse_event.x = floor(x / device_scale_factor); | |
466 mouse_event.y = floor(y / device_scale_factor); | |
467 mouse_event.windowX = mouse_event.x; | 486 mouse_event.windowX = mouse_event.x; |
468 mouse_event.windowY = mouse_event.y; | 487 mouse_event.windowY = mouse_event.y; |
469 mouse_event.globalX = mouse_event.x; | 488 mouse_event.globalX = mouse_event.x; |
470 mouse_event.globalY = mouse_event.y; | 489 mouse_event.globalY = mouse_event.y; |
471 | 490 |
472 params->GetInteger(devtools::Input::dispatchMouseEvent::kParamClickCount, | 491 params->GetInteger(devtools::Input::dispatchMouseEvent::kParamClickCount, |
473 &mouse_event.clickCount); | 492 &mouse_event.clickCount); |
474 | 493 |
475 std::string button; | 494 std::string button; |
476 if (!params->GetString(devtools::Input::dispatchMouseEvent::kParamButton, | 495 if (!params->GetString(devtools::Input::dispatchMouseEvent::kParamButton, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 else if (type == "pinchUpdate") | 546 else if (type == "pinchUpdate") |
528 event.type = WebInputEvent::GesturePinchUpdate; | 547 event.type = WebInputEvent::GesturePinchUpdate; |
529 else if (type == "pinchEnd") | 548 else if (type == "pinchEnd") |
530 event.type = WebInputEvent::GesturePinchEnd; | 549 event.type = WebInputEvent::GesturePinchEnd; |
531 else | 550 else |
532 return NULL; | 551 return NULL; |
533 } else { | 552 } else { |
534 return NULL; | 553 return NULL; |
535 } | 554 } |
536 | 555 |
537 float device_scale_factor = ui::GetScaleFactorScale( | 556 if (!params->GetInteger(devtools::kParamX, &event.x) || |
538 GetScaleFactorForView(host->GetView())); | 557 !params->GetInteger(devtools::kParamY, &event.y)) { |
539 | |
540 int x; | |
541 int y; | |
542 if (!params->GetInteger(devtools::kParamX, &x) || | |
543 !params->GetInteger(devtools::kParamY, &y)) { | |
544 return NULL; | 558 return NULL; |
545 } | 559 } |
546 event.x = floor(x / device_scale_factor); | |
547 event.y = floor(y / device_scale_factor); | |
548 event.globalX = event.x; | 560 event.globalX = event.x; |
549 event.globalY = event.y; | 561 event.globalY = event.y; |
550 | 562 |
551 if (type == "scrollUpdate") { | 563 if (type == "scrollUpdate") { |
552 int dx; | 564 int dx; |
553 int dy; | 565 int dy; |
554 if (!params->GetInteger( | 566 if (!params->GetInteger( |
555 devtools::Input::dispatchGestureEvent::kParamDeltaX, &dx) || | 567 devtools::Input::dispatchGestureEvent::kParamDeltaX, &dx) || |
556 !params->GetInteger( | 568 !params->GetInteger( |
557 devtools::Input::dispatchGestureEvent::kParamDeltaY, &dy)) { | 569 devtools::Input::dispatchGestureEvent::kParamDeltaY, &dy)) { |
558 return NULL; | 570 return NULL; |
559 } | 571 } |
560 event.data.scrollUpdate.deltaX = floor(dx / device_scale_factor); | 572 event.data.scrollUpdate.deltaX = dx; |
561 event.data.scrollUpdate.deltaY = floor(dy / device_scale_factor); | 573 event.data.scrollUpdate.deltaY = dy; |
562 } | 574 } |
563 | 575 |
564 if (type == "pinchUpdate") { | 576 if (type == "pinchUpdate") { |
565 double scale; | 577 double scale; |
566 if (!params->GetDouble( | 578 if (!params->GetDouble( |
567 devtools::Input::dispatchGestureEvent::kParamPinchScale, | 579 devtools::Input::dispatchGestureEvent::kParamPinchScale, |
568 &scale)) { | 580 &scale)) { |
569 return NULL; | 581 return NULL; |
570 } | 582 } |
571 event.data.pinchUpdate.scale = static_cast<float>(scale); | 583 event.data.pinchUpdate.scale = static_cast<float>(scale); |
572 } | 584 } |
573 | 585 |
574 host->ForwardGestureEvent(event); | 586 host->ForwardGestureEvent(event); |
575 return command->SuccessResponse(NULL); | 587 return command->SuccessResponse(NULL); |
576 } | 588 } |
577 | 589 |
578 } // namespace content | 590 } // namespace content |
OLD | NEW |