| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "extensions/browser/api/web_contents_capture_client.h" | 5 #include "extensions/browser/api/web_contents_capture_client.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/public/browser/render_widget_host.h" | 9 #include "content/public/browser/render_widget_host.h" |
| 10 #include "content/public/browser/render_widget_host_view.h" | 10 #include "content/public/browser/render_widget_host_view.h" |
| 11 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 12 #include "extensions/browser/extension_function.h" | 12 #include "extensions/browser/extension_function.h" |
| 13 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "ui/display/display.h" | |
| 16 #include "ui/display/screen.h" | |
| 17 #include "ui/gfx/codec/jpeg_codec.h" | 15 #include "ui/gfx/codec/jpeg_codec.h" |
| 18 #include "ui/gfx/codec/png_codec.h" | 16 #include "ui/gfx/codec/png_codec.h" |
| 19 #include "ui/gfx/geometry/size_conversions.h" | |
| 20 | 17 |
| 21 using content::RenderWidgetHost; | 18 using content::RenderWidgetHost; |
| 22 using content::RenderWidgetHostView; | 19 using content::RenderWidgetHostView; |
| 23 using content::WebContents; | 20 using content::WebContents; |
| 24 | 21 |
| 25 namespace extensions { | 22 namespace extensions { |
| 26 | 23 |
| 27 using api::extension_types::ImageDetails; | 24 using api::extension_types::ImageDetails; |
| 28 | 25 |
| 29 bool WebContentsCaptureClient::CaptureAsync( | 26 bool WebContentsCaptureClient::CaptureAsync( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 46 | 43 |
| 47 if (image_details) { | 44 if (image_details) { |
| 48 if (image_details->format != api::extension_types::IMAGE_FORMAT_NONE) | 45 if (image_details->format != api::extension_types::IMAGE_FORMAT_NONE) |
| 49 image_format_ = image_details->format; | 46 image_format_ = image_details->format; |
| 50 if (image_details->quality.get()) | 47 if (image_details->quality.get()) |
| 51 image_quality_ = *image_details->quality; | 48 image_quality_ = *image_details->quality; |
| 52 } | 49 } |
| 53 | 50 |
| 54 // TODO(miu): Account for fullscreen render widget? http://crbug.com/419878 | 51 // TODO(miu): Account for fullscreen render widget? http://crbug.com/419878 |
| 55 RenderWidgetHostView* const view = web_contents->GetRenderWidgetHostView(); | 52 RenderWidgetHostView* const view = web_contents->GetRenderWidgetHostView(); |
| 56 RenderWidgetHost* const host = view ? view->GetRenderWidgetHost() : nullptr; | 53 if (!view) { |
| 57 if (!view || !host) { | |
| 58 OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE); | 54 OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE); |
| 59 return false; | 55 return false; |
| 60 } | 56 } |
| 61 | 57 view->CopyFromSurface(gfx::Rect(), // Copy entire surface area. |
| 62 // By default, the requested bitmap size is the view size in screen | 58 gfx::Size(), // Result contains device-level detail. |
| 63 // coordinates. However, if there's more pixel detail available on the | 59 callback, kN32_SkColorType); |
| 64 // current system, increase the requested bitmap size to capture it all. | |
| 65 const gfx::Size view_size = view->GetViewBounds().size(); | |
| 66 gfx::Size bitmap_size = view_size; | |
| 67 const gfx::NativeView native_view = view->GetNativeView(); | |
| 68 display::Screen* const screen = display::Screen::GetScreen(); | |
| 69 const float scale = | |
| 70 screen->GetDisplayNearestWindow(native_view).device_scale_factor(); | |
| 71 if (scale > 1.0f) | |
| 72 bitmap_size = gfx::ScaleToCeiledSize(view_size, scale); | |
| 73 | |
| 74 host->CopyFromBackingStore(gfx::Rect(view_size), bitmap_size, callback, | |
| 75 kN32_SkColorType); | |
| 76 return true; | 60 return true; |
| 77 } | 61 } |
| 78 | 62 |
| 79 void WebContentsCaptureClient::CopyFromBackingStoreComplete( | 63 void WebContentsCaptureClient::CopyFromSurfaceComplete( |
| 80 const SkBitmap& bitmap, | 64 const SkBitmap& bitmap, |
| 81 content::ReadbackResponse response) { | 65 content::ReadbackResponse response) { |
| 82 if (response == content::READBACK_SUCCESS) { | 66 if (response == content::READBACK_SUCCESS) { |
| 83 OnCaptureSuccess(bitmap); | 67 OnCaptureSuccess(bitmap); |
| 84 return; | 68 return; |
| 85 } | 69 } |
| 86 // TODO(wjmaclean): Improve error reporting. Why aren't we passing more | 70 // TODO(wjmaclean): Improve error reporting. Why aren't we passing more |
| 87 // information here? | 71 // information here? |
| 88 std::string reason; | 72 std::string reason; |
| 89 switch (response) { | 73 switch (response) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 data.size()); | 119 data.size()); |
| 136 | 120 |
| 137 base::Base64Encode(stream_as_string, base64_result); | 121 base::Base64Encode(stream_as_string, base64_result); |
| 138 base64_result->insert( | 122 base64_result->insert( |
| 139 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); | 123 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); |
| 140 | 124 |
| 141 return true; | 125 return true; |
| 142 } | 126 } |
| 143 | 127 |
| 144 } // namespace extensions | 128 } // namespace extensions |
| OLD | NEW |