Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(925)

Side by Side Diff: content/browser/devtools/renderer_overrides_handler.cc

Issue 311313003: DevTools: Fix for Page.captureScreenshot (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added test Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/barrier_closure.h" 10 #include "base/barrier_closure.h"
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 if (format->empty()) 256 if (format->empty())
257 *format = kPng; 257 *format = kPng;
258 if (*quality < 0 || *quality > 100) 258 if (*quality < 0 || *quality > 100)
259 *quality = kDefaultScreenshotQuality; 259 *quality = kDefaultScreenshotQuality;
260 if (*scale <= 0) 260 if (*scale <= 0)
261 *scale = 0.1; 261 *scale = 0.1;
262 if (*scale > 5) 262 if (*scale > 5)
263 *scale = 5; 263 *scale = 5;
264 } 264 }
265 265
266 base::DictionaryValue* RendererOverridesHandler::CreateScreenshotResponse(
267 const std::vector<unsigned char>& png_data) {
268 std::string base_64_data;
269 base::Base64Encode(
270 base::StringPiece(reinterpret_cast<const char*>(&png_data[0]),
271 png_data.size()),
272 &base_64_data);
273
274 base::DictionaryValue* response = new base::DictionaryValue();
275 response->SetString(
276 devtools::Page::captureScreenshot::kResponseData, base_64_data);
277 return response;
278 }
279
280 // DOM agent handlers -------------------------------------------------------- 266 // DOM agent handlers --------------------------------------------------------
281 267
282 scoped_refptr<DevToolsProtocol::Response> 268 scoped_refptr<DevToolsProtocol::Response>
283 RendererOverridesHandler::GrantPermissionsForSetFileInputFiles( 269 RendererOverridesHandler::GrantPermissionsForSetFileInputFiles(
284 scoped_refptr<DevToolsProtocol::Command> command) { 270 scoped_refptr<DevToolsProtocol::Command> command) {
285 base::DictionaryValue* params = command->params(); 271 base::DictionaryValue* params = command->params();
286 base::ListValue* file_list = NULL; 272 base::ListValue* file_list = NULL;
287 const char* param = 273 const char* param =
288 devtools::DOM::setFileInputFiles::kParamFiles; 274 devtools::DOM::setFileInputFiles::kParamFiles;
289 if (!params || !params->GetList(param, &file_list)) 275 if (!params || !params->GetList(param, &file_list))
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 } 449 }
464 return command->InvalidParamResponse(param); 450 return command->InvalidParamResponse(param);
465 } 451 }
466 } 452 }
467 return command->InternalErrorResponse("No WebContents to navigate"); 453 return command->InternalErrorResponse("No WebContents to navigate");
468 } 454 }
469 455
470 scoped_refptr<DevToolsProtocol::Response> 456 scoped_refptr<DevToolsProtocol::Response>
471 RendererOverridesHandler::PageCaptureScreenshot( 457 RendererOverridesHandler::PageCaptureScreenshot(
472 scoped_refptr<DevToolsProtocol::Command> command) { 458 scoped_refptr<DevToolsProtocol::Command> command) {
473 RenderViewHost* host = agent_->GetRenderViewHost(); 459 RenderViewHostImpl* host = static_cast<RenderViewHostImpl*>(
460 agent_->GetRenderViewHost());
474 if (!host->GetView()) 461 if (!host->GetView())
475 return command->InternalErrorResponse("Unable to access the view"); 462 return command->InternalErrorResponse("Unable to access the view");
476 463
477 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); 464 host->GetSnapshotFromBrowser(
478 gfx::Rect snapshot_bounds(view_bounds.size());
479 gfx::Size snapshot_size = snapshot_bounds.size();
480
481 std::vector<unsigned char> png_data;
482 if (ui::GrabViewSnapshot(host->GetView()->GetNativeView(),
483 &png_data,
484 snapshot_bounds)) {
485 if (png_data.size())
486 return command->SuccessResponse(CreateScreenshotResponse(png_data));
487 else
488 return command->InternalErrorResponse("Unable to capture screenshot");
489 }
490
491 ui::GrabViewSnapshotAsync(
492 host->GetView()->GetNativeView(),
493 snapshot_bounds,
494 base::ThreadTaskRunnerHandle::Get(),
495 base::Bind(&RendererOverridesHandler::ScreenshotCaptured, 465 base::Bind(&RendererOverridesHandler::ScreenshotCaptured,
496 weak_factory_.GetWeakPtr(), command)); 466 weak_factory_.GetWeakPtr(), command));
497 return command->AsyncResponsePromise(); 467 return command->AsyncResponsePromise();
498 } 468 }
499 469
500 void RendererOverridesHandler::ScreenshotCaptured( 470 void RendererOverridesHandler::ScreenshotCaptured(
501 scoped_refptr<DevToolsProtocol::Command> command, 471 scoped_refptr<DevToolsProtocol::Command> command,
502 scoped_refptr<base::RefCountedBytes> png_data) { 472 const unsigned char* png_data,
503 if (png_data) { 473 size_t png_size) {
504 SendAsyncResponse( 474 if (!png_data || !png_size) {
505 command->SuccessResponse(CreateScreenshotResponse(png_data->data())));
506 } else {
507 SendAsyncResponse( 475 SendAsyncResponse(
508 command->InternalErrorResponse("Unable to capture screenshot")); 476 command->InternalErrorResponse("Unable to capture screenshot"));
477 return;
509 } 478 }
479
480 std::string base_64_data;
481 base::Base64Encode(
482 base::StringPiece(reinterpret_cast<const char*>(png_data), png_size),
483 &base_64_data);
484
485 base::DictionaryValue* response = new base::DictionaryValue();
486 response->SetString(devtools::Page::screencastFrame::kParamData,
487 base_64_data);
488
489 SendAsyncResponse(command->SuccessResponse(response));
510 } 490 }
511 491
512 scoped_refptr<DevToolsProtocol::Response> 492 scoped_refptr<DevToolsProtocol::Response>
513 RendererOverridesHandler::PageCanScreencast( 493 RendererOverridesHandler::PageCanScreencast(
514 scoped_refptr<DevToolsProtocol::Command> command) { 494 scoped_refptr<DevToolsProtocol::Command> command) {
515 base::DictionaryValue* result = new base::DictionaryValue(); 495 base::DictionaryValue* result = new base::DictionaryValue();
516 #if defined(OS_ANDROID) 496 #if defined(OS_ANDROID)
517 result->SetBoolean(devtools::kResult, true); 497 result->SetBoolean(devtools::kResult, true);
518 #else 498 #else
519 result->SetBoolean(devtools::kResult, false); 499 result->SetBoolean(devtools::kResult, false);
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
998 return NULL; 978 return NULL;
999 } 979 }
1000 event.data.pinchUpdate.scale = static_cast<float>(scale); 980 event.data.pinchUpdate.scale = static_cast<float>(scale);
1001 } 981 }
1002 982
1003 host->ForwardGestureEvent(event); 983 host->ForwardGestureEvent(event);
1004 return command->SuccessResponse(NULL); 984 return command->SuccessResponse(NULL);
1005 } 985 }
1006 986
1007 } // namespace content 987 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698