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" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "base/time/time.h" | |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "content/browser/child_process_security_policy_impl.h" | 16 #include "content/browser/child_process_security_policy_impl.h" |
16 #include "content/browser/devtools/devtools_protocol_constants.h" | 17 #include "content/browser/devtools/devtools_protocol_constants.h" |
17 #include "content/browser/devtools/devtools_tracing_handler.h" | 18 #include "content/browser/devtools/devtools_tracing_handler.h" |
18 #include "content/browser/renderer_host/render_view_host_delegate.h" | 19 #include "content/browser/renderer_host/render_view_host_delegate.h" |
20 #include "content/port/browser/render_view_host_delegate_view.h" | |
21 #include "content/port/browser/render_widget_host_view_port.h" | |
19 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/devtools_agent_host.h" | 23 #include "content/public/browser/devtools_agent_host.h" |
21 #include "content/public/browser/javascript_dialog_manager.h" | 24 #include "content/public/browser/javascript_dialog_manager.h" |
22 #include "content/public/browser/navigation_controller.h" | 25 #include "content/public/browser/navigation_controller.h" |
23 #include "content/public/browser/render_process_host.h" | 26 #include "content/public/browser/render_process_host.h" |
24 #include "content/public/browser/render_view_host.h" | 27 #include "content/public/browser/render_view_host.h" |
25 #include "content/public/browser/render_widget_host_view.h" | 28 #include "content/public/browser/render_widget_host_view.h" |
26 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
27 #include "content/public/browser/web_contents_delegate.h" | 30 #include "content/public/browser/web_contents_delegate.h" |
28 #include "content/public/common/page_transition_types.h" | 31 #include "content/public/common/page_transition_types.h" |
29 #include "content/public/common/referrer.h" | 32 #include "content/public/common/referrer.h" |
30 #include "ui/snapshot/snapshot.h" | 33 #include "ui/gfx/codec/jpeg_codec.h" |
34 #include "ui/gfx/codec/png_codec.h" | |
31 #include "url/gurl.h" | 35 #include "url/gurl.h" |
32 | 36 |
37 using base::TimeTicks; | |
38 | |
39 namespace { | |
40 | |
41 static const char kPng[] = "png"; | |
42 static const char kJpeg[] = "jpeg"; | |
43 static int kDefaultQuality = 90; | |
Sami
2013/08/08 11:35:00
nit: kDefaultScreenshotQuality?
pfeldman
2013/08/09 13:59:02
Done.
| |
44 | |
45 } // namespace | |
46 | |
33 namespace content { | 47 namespace content { |
34 | 48 |
35 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) | 49 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) |
36 : agent_(agent), | 50 : agent_(agent), |
37 weak_factory_(this) { | 51 weak_factory_(this) { |
38 RegisterCommandHandler( | 52 RegisterCommandHandler( |
39 devtools::DOM::setFileInputFiles::kName, | 53 devtools::DOM::setFileInputFiles::kName, |
40 base::Bind( | 54 base::Bind( |
41 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, | 55 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, |
42 base::Unretained(this))); | 56 base::Unretained(this))); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 .LoadURL(gurl, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | 148 .LoadURL(gurl, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
135 return command->SuccessResponse(new base::DictionaryValue()); | 149 return command->SuccessResponse(new base::DictionaryValue()); |
136 } | 150 } |
137 } | 151 } |
138 return command->InternalErrorResponse("No WebContents to navigate"); | 152 return command->InternalErrorResponse("No WebContents to navigate"); |
139 } | 153 } |
140 | 154 |
141 scoped_refptr<DevToolsProtocol::Response> | 155 scoped_refptr<DevToolsProtocol::Response> |
142 RendererOverridesHandler::PageCaptureScreenshot( | 156 RendererOverridesHandler::PageCaptureScreenshot( |
143 scoped_refptr<DevToolsProtocol::Command> command) { | 157 scoped_refptr<DevToolsProtocol::Command> command) { |
144 // Emulate async processing. | 158 RenderViewHost* host = agent_->GetRenderViewHost(); |
145 BrowserThread::PostTask( | 159 RenderWidgetHostViewPort* view_port = |
146 BrowserThread::UI, FROM_HERE, | 160 RenderWidgetHostViewPort::FromRWHV(host->GetView()); |
161 | |
162 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); | |
163 gfx::Rect snapshot_bounds(view_bounds.size()); | |
164 gfx::Size snapshot_size = snapshot_bounds.size(); | |
165 | |
166 view_port->CopyFromCompositingSurface( | |
167 snapshot_bounds, snapshot_size, | |
147 base::Bind(&RendererOverridesHandler::CaptureScreenshot, | 168 base::Bind(&RendererOverridesHandler::CaptureScreenshot, |
148 weak_factory_.GetWeakPtr(), | 169 weak_factory_.GetWeakPtr(), command)); |
149 command)); | |
150 | 170 |
151 return command->AsyncResponsePromise(); | 171 return command->AsyncResponsePromise(); |
152 } | 172 } |
153 | 173 |
154 void RendererOverridesHandler::CaptureScreenshot( | 174 void RendererOverridesHandler::CaptureScreenshot( |
155 scoped_refptr<DevToolsProtocol::Command> command) { | 175 scoped_refptr<DevToolsProtocol::Command> command, |
176 bool success, | |
177 const SkBitmap& bitmap) { | |
178 if (!success) { | |
179 SendRawMessage( | |
180 command->InternalErrorResponse("Unable to capture screenshot")-> | |
181 Serialize()); | |
182 return; | |
183 } | |
156 | 184 |
157 RenderViewHost* host = agent_->GetRenderViewHost(); | 185 std::string format; |
158 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); | 186 int quality = kDefaultQuality; |
159 gfx::Rect snapshot_bounds(view_bounds.size()); | 187 double scale = 0; |
160 gfx::Size snapshot_size = snapshot_bounds.size(); | |
161 | 188 |
162 std::vector<unsigned char> png; | 189 base::DictionaryValue* params = command->params(); |
163 if (ui::GrabViewSnapshot(host->GetView()->GetNativeView(), | 190 if (params) { |
164 &png, | 191 params->GetString(devtools::Page::captureScreenshot::kParamFormat, |
165 snapshot_bounds)) { | 192 &format); |
166 std::string base_64_data; | 193 params->GetInteger(devtools::Page::captureScreenshot::kParamQuality, |
167 bool success = base::Base64Encode( | 194 &quality); |
168 base::StringPiece(reinterpret_cast<char*>(&*png.begin()), png.size()), | 195 params->GetDouble(devtools::Page::captureScreenshot::kParamScale, |
169 &base_64_data); | 196 &scale); |
170 if (success) { | |
171 base::DictionaryValue* result = new base::DictionaryValue(); | |
172 result->SetString( | |
173 devtools::Page::captureScreenshot::kResponseData, base_64_data); | |
174 scoped_refptr<DevToolsProtocol::Response> response = | |
175 command->SuccessResponse(result); | |
176 SendRawMessage(response->Serialize()); | |
177 return; | |
178 } | |
179 } | 197 } |
180 SendRawMessage(command-> | 198 |
181 InternalErrorResponse("Unable to capture a screenshot")->Serialize()); | 199 if (format.empty()) |
200 format = kPng; | |
201 if (quality < 0 || quality > 100) | |
202 quality = kDefaultQuality; | |
203 if (scale <= 0 || scale > 1) | |
204 scale = 1; | |
205 | |
206 // TODO(pfeldman): apply scale. | |
207 | |
208 std::vector<unsigned char> data; | |
209 SkAutoLockPixels lock_image(bitmap); | |
210 | |
211 bool encoded; | |
212 if (format == kPng) { | |
213 encoded = gfx::PNGCodec::Encode( | |
214 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
Sami
2013/08/08 11:35:00
nit: should be indented by two more spaces.
pfeldman
2013/08/09 13:59:02
Done.
| |
215 gfx::PNGCodec::FORMAT_SkBitmap, | |
216 gfx::Size(bitmap.width(), bitmap.height()), | |
217 bitmap.width() * bitmap.bytesPerPixel(), | |
218 false, std::vector<gfx::PNGCodec::Comment>(), &data); | |
219 } else if (format == kJpeg) { | |
220 encoded = gfx::JPEGCodec::Encode( | |
221 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
Sami
2013/08/08 11:35:00
nit: ditto
pfeldman
2013/08/09 13:59:02
Done.
| |
222 gfx::JPEGCodec::FORMAT_SkBitmap, | |
223 bitmap.width(), | |
224 bitmap.height(), | |
225 bitmap.width() * bitmap.bytesPerPixel(), | |
226 quality, &data); | |
227 } else { | |
228 encoded = false; | |
229 } | |
230 | |
231 if (!encoded) { | |
232 SendRawMessage( | |
233 command->InternalErrorResponse("Unable to encode screenshot")-> | |
234 Serialize()); | |
235 return; | |
236 } | |
237 | |
238 std::string base_64_data; | |
Sami
2013/08/08 11:35:00
It would be great if we could free the bitmap at t
pfeldman
2013/08/09 13:59:02
It is declared in RenderWidgetHostViewPort and cou
| |
239 if (!base::Base64Encode(base::StringPiece( | |
240 reinterpret_cast<char*>(&data[0]), | |
241 data.size()), | |
242 &base_64_data)) { | |
243 SendRawMessage( | |
244 command->InternalErrorResponse("Unable to base64 encode screenshot")-> | |
245 Serialize()); | |
246 return; | |
247 } | |
248 | |
249 base::DictionaryValue* response = new base::DictionaryValue(); | |
250 response->SetString( | |
251 devtools::Page::captureScreenshot::kResponseData, base_64_data); | |
252 SendRawMessage(command->SuccessResponse(response)->Serialize()); | |
182 } | 253 } |
183 | 254 |
184 } // namespace content | 255 } // namespace content |
OLD | NEW |