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/capture_web_contents_function.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 "ui/gfx/codec/jpeg_codec.h" | 14 #include "ui/gfx/codec/jpeg_codec.h" |
15 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
16 #include "ui/gfx/display.h" | 16 #include "ui/gfx/display.h" |
17 #include "ui/gfx/geometry/size_conversions.h" | 17 #include "ui/gfx/geometry/size_conversions.h" |
18 #include "ui/gfx/screen.h" | 18 #include "ui/gfx/screen.h" |
19 | 19 |
20 using content::RenderWidgetHost; | 20 using content::RenderWidgetHost; |
21 using content::RenderWidgetHostView; | 21 using content::RenderWidgetHostView; |
22 using content::WebContents; | 22 using content::WebContents; |
23 | 23 |
24 namespace extensions { | 24 namespace extensions { |
25 | 25 |
26 using api::extension_types::ImageDetails; | 26 using api::extension_types::ImageDetails; |
27 | 27 |
28 bool CaptureWebContentsFunction::HasPermission() { | 28 bool WebContentsCaptureClient::CaptureAsync( |
29 return true; | 29 WebContents* web_contents, |
30 } | 30 const ImageDetails* image_details, |
31 | 31 const content::ReadbackRequestCallback callback) { |
32 bool CaptureWebContentsFunction::RunAsync() { | 32 if (!web_contents) |
33 EXTENSION_FUNCTION_VALIDATE(args_); | 33 return false; |
34 | |
35 context_id_ = extension_misc::kCurrentWindowId; | |
36 args_->GetInteger(0, &context_id_); | |
37 | |
38 scoped_ptr<ImageDetails> image_details; | |
39 if (args_->GetSize() > 1) { | |
40 base::Value* spec = NULL; | |
41 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec); | |
42 image_details = ImageDetails::FromValue(*spec); | |
43 } | |
44 | 34 |
45 if (!IsScreenshotEnabled()) | 35 if (!IsScreenshotEnabled()) |
46 return false; | 36 return false; |
47 | 37 |
48 WebContents* contents = GetWebContentsForID(context_id_); | |
49 if (!contents) | |
50 return false; | |
51 | |
52 // The default format and quality setting used when encoding jpegs. | 38 // The default format and quality setting used when encoding jpegs. |
53 const api::extension_types::ImageFormat kDefaultFormat = | 39 const api::extension_types::ImageFormat kDefaultFormat = |
54 api::extension_types::IMAGE_FORMAT_JPEG; | 40 api::extension_types::IMAGE_FORMAT_JPEG; |
55 const int kDefaultQuality = 90; | 41 const int kDefaultQuality = 90; |
56 | 42 |
57 image_format_ = kDefaultFormat; | 43 image_format_ = kDefaultFormat; |
58 image_quality_ = kDefaultQuality; | 44 image_quality_ = kDefaultQuality; |
59 | 45 |
60 if (image_details) { | 46 if (image_details) { |
61 if (image_details->format != api::extension_types::IMAGE_FORMAT_NONE) | 47 if (image_details->format != api::extension_types::IMAGE_FORMAT_NONE) |
62 image_format_ = image_details->format; | 48 image_format_ = image_details->format; |
63 if (image_details->quality.get()) | 49 if (image_details->quality.get()) |
64 image_quality_ = *image_details->quality; | 50 image_quality_ = *image_details->quality; |
65 } | 51 } |
66 | 52 |
67 // TODO(miu): Account for fullscreen render widget? http://crbug.com/419878 | 53 // TODO(miu): Account for fullscreen render widget? http://crbug.com/419878 |
68 RenderWidgetHostView* const view = contents->GetRenderWidgetHostView(); | 54 RenderWidgetHostView* const view = web_contents->GetRenderWidgetHostView(); |
69 RenderWidgetHost* const host = view ? view->GetRenderWidgetHost() : nullptr; | 55 RenderWidgetHost* const host = view ? view->GetRenderWidgetHost() : nullptr; |
70 if (!view || !host) { | 56 if (!view || !host) { |
71 OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE); | 57 OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE); |
72 return false; | 58 return false; |
73 } | 59 } |
74 | 60 |
75 // By default, the requested bitmap size is the view size in screen | 61 // By default, the requested bitmap size is the view size in screen |
76 // coordinates. However, if there's more pixel detail available on the | 62 // coordinates. However, if there's more pixel detail available on the |
77 // current system, increase the requested bitmap size to capture it all. | 63 // current system, increase the requested bitmap size to capture it all. |
78 const gfx::Size view_size = view->GetViewBounds().size(); | 64 const gfx::Size view_size = view->GetViewBounds().size(); |
79 gfx::Size bitmap_size = view_size; | 65 gfx::Size bitmap_size = view_size; |
80 const gfx::NativeView native_view = view->GetNativeView(); | 66 const gfx::NativeView native_view = view->GetNativeView(); |
81 gfx::Screen* const screen = gfx::Screen::GetScreenFor(native_view); | 67 gfx::Screen* const screen = gfx::Screen::GetScreenFor(native_view); |
82 const float scale = | 68 const float scale = |
83 screen->GetDisplayNearestWindow(native_view).device_scale_factor(); | 69 screen->GetDisplayNearestWindow(native_view).device_scale_factor(); |
84 if (scale > 1.0f) | 70 if (scale > 1.0f) |
85 bitmap_size = gfx::ScaleToCeiledSize(view_size, scale); | 71 bitmap_size = gfx::ScaleToCeiledSize(view_size, scale); |
86 | 72 |
87 host->CopyFromBackingStore( | 73 host->CopyFromBackingStore(gfx::Rect(view_size), bitmap_size, callback, |
88 gfx::Rect(view_size), | 74 kN32_SkColorType); |
89 bitmap_size, | |
90 base::Bind(&CaptureWebContentsFunction::CopyFromBackingStoreComplete, | |
91 this), | |
92 kN32_SkColorType); | |
93 return true; | 75 return true; |
94 } | 76 } |
95 | 77 |
96 void CaptureWebContentsFunction::CopyFromBackingStoreComplete( | 78 void WebContentsCaptureClient::CopyFromBackingStoreComplete( |
97 const SkBitmap& bitmap, | 79 const SkBitmap& bitmap, |
98 content::ReadbackResponse response) { | 80 content::ReadbackResponse response) { |
99 if (response == content::READBACK_SUCCESS) { | 81 if (response == content::READBACK_SUCCESS) { |
100 OnCaptureSuccess(bitmap); | 82 OnCaptureSuccess(bitmap); |
101 return; | 83 return; |
102 } | 84 } |
85 // TODO(wjmaclean): Improve error reporting. Why aren't we passing more | |
Fady Samuel
2016/01/25 16:20:39
Remove this now?
| |
86 // information here? | |
87 std::string reason; | |
88 switch (response) { | |
89 case content::READBACK_FAILED: | |
90 reason = "READBACK_FAILED"; | |
91 break; | |
92 case content::READBACK_SURFACE_UNAVAILABLE: | |
93 reason = "READBACK_SURFACE_UNAVAILABLE"; | |
94 break; | |
95 case content::READBACK_BITMAP_ALLOCATION_FAILURE: | |
96 reason = "READBACK_BITMAP_ALLOCATION_FAILURE"; | |
97 break; | |
98 default: | |
99 reason = "<unknown>"; | |
100 } | |
103 OnCaptureFailure(FAILURE_REASON_UNKNOWN); | 101 OnCaptureFailure(FAILURE_REASON_UNKNOWN); |
104 } | 102 } |
105 | 103 |
106 void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) { | 104 // TODO(wjmaclean) can this be static? |
105 bool WebContentsCaptureClient::EncodeBitmap(const SkBitmap& bitmap, | |
106 std::string* base64_result) { | |
107 DCHECK(base64_result); | |
107 std::vector<unsigned char> data; | 108 std::vector<unsigned char> data; |
108 SkAutoLockPixels screen_capture_lock(bitmap); | 109 SkAutoLockPixels screen_capture_lock(bitmap); |
109 bool encoded = false; | 110 bool encoded = false; |
110 std::string mime_type; | 111 std::string mime_type; |
111 switch (image_format_) { | 112 switch (image_format_) { |
112 case api::extension_types::IMAGE_FORMAT_JPEG: | 113 case api::extension_types::IMAGE_FORMAT_JPEG: |
113 encoded = gfx::JPEGCodec::Encode( | 114 encoded = gfx::JPEGCodec::Encode( |
114 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 115 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
115 gfx::JPEGCodec::FORMAT_SkBitmap, | 116 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), |
116 bitmap.width(), | 117 static_cast<int>(bitmap.rowBytes()), image_quality_, &data); |
117 bitmap.height(), | |
118 static_cast<int>(bitmap.rowBytes()), | |
119 image_quality_, | |
120 &data); | |
121 mime_type = kMimeTypeJpeg; | 118 mime_type = kMimeTypeJpeg; |
122 break; | 119 break; |
123 case api::extension_types::IMAGE_FORMAT_PNG: | 120 case api::extension_types::IMAGE_FORMAT_PNG: |
124 encoded = | 121 encoded = |
125 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, | 122 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, |
126 true, // Discard transparency. | 123 true, // Discard transparency. |
127 &data); | 124 &data); |
128 mime_type = kMimeTypePng; | 125 mime_type = kMimeTypePng; |
129 break; | 126 break; |
130 default: | 127 default: |
131 NOTREACHED() << "Invalid image format."; | 128 NOTREACHED() << "Invalid image format."; |
132 } | 129 } |
133 | 130 |
134 if (!encoded) { | 131 if (!encoded) |
135 OnCaptureFailure(FAILURE_REASON_ENCODING_FAILED); | 132 return false; |
136 return; | |
137 } | |
138 | 133 |
139 std::string base64_result; | |
140 base::StringPiece stream_as_string(reinterpret_cast<const char*>(data.data()), | 134 base::StringPiece stream_as_string(reinterpret_cast<const char*>(data.data()), |
141 data.size()); | 135 data.size()); |
142 | 136 |
143 base::Base64Encode(stream_as_string, &base64_result); | 137 base::Base64Encode(stream_as_string, base64_result); |
144 base64_result.insert( | 138 base64_result->insert( |
145 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); | 139 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); |
146 SetResult(new base::StringValue(base64_result)); | 140 |
147 SendResponse(true); | 141 return true; |
148 } | 142 } |
149 | 143 |
150 } // namespace extensions | 144 } // namespace extensions |
OLD | NEW |