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_widget_host_view_port.h" |
19 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/devtools_agent_host.h" | 22 #include "content/public/browser/devtools_agent_host.h" |
21 #include "content/public/browser/javascript_dialog_manager.h" | 23 #include "content/public/browser/javascript_dialog_manager.h" |
22 #include "content/public/browser/navigation_controller.h" | 24 #include "content/public/browser/navigation_controller.h" |
23 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
24 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" |
25 #include "content/public/browser/render_widget_host_view.h" | 27 #include "content/public/browser/render_widget_host_view.h" |
26 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
27 #include "content/public/browser/web_contents_delegate.h" | 29 #include "content/public/browser/web_contents_delegate.h" |
28 #include "content/public/common/page_transition_types.h" | 30 #include "content/public/common/page_transition_types.h" |
29 #include "content/public/common/referrer.h" | 31 #include "content/public/common/referrer.h" |
| 32 #include "ui/gfx/codec/jpeg_codec.h" |
| 33 #include "ui/gfx/codec/png_codec.h" |
| 34 #include "ui/gfx/size_conversions.h" |
30 #include "ui/snapshot/snapshot.h" | 35 #include "ui/snapshot/snapshot.h" |
31 #include "url/gurl.h" | 36 #include "url/gurl.h" |
32 | 37 |
| 38 using base::TimeTicks; |
| 39 |
| 40 namespace { |
| 41 |
| 42 static const char kPng[] = "png"; |
| 43 static const char kJpeg[] = "jpeg"; |
| 44 static int kDefaultScreenshotQuality = 80; |
| 45 |
| 46 } // namespace |
| 47 |
33 namespace content { | 48 namespace content { |
34 | 49 |
35 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) | 50 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) |
36 : agent_(agent), | 51 : agent_(agent), |
37 weak_factory_(this) { | 52 weak_factory_(this) { |
38 RegisterCommandHandler( | 53 RegisterCommandHandler( |
39 devtools::DOM::setFileInputFiles::kName, | 54 devtools::DOM::setFileInputFiles::kName, |
40 base::Bind( | 55 base::Bind( |
41 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, | 56 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, |
42 base::Unretained(this))); | 57 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()); | 149 .LoadURL(gurl, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
135 return command->SuccessResponse(new base::DictionaryValue()); | 150 return command->SuccessResponse(new base::DictionaryValue()); |
136 } | 151 } |
137 } | 152 } |
138 return command->InternalErrorResponse("No WebContents to navigate"); | 153 return command->InternalErrorResponse("No WebContents to navigate"); |
139 } | 154 } |
140 | 155 |
141 scoped_refptr<DevToolsProtocol::Response> | 156 scoped_refptr<DevToolsProtocol::Response> |
142 RendererOverridesHandler::PageCaptureScreenshot( | 157 RendererOverridesHandler::PageCaptureScreenshot( |
143 scoped_refptr<DevToolsProtocol::Command> command) { | 158 scoped_refptr<DevToolsProtocol::Command> command) { |
144 // Emulate async processing. | 159 // Parse input parameters. |
145 BrowserThread::PostTask( | 160 std::string format; |
146 BrowserThread::UI, FROM_HERE, | 161 int quality = kDefaultScreenshotQuality; |
147 base::Bind(&RendererOverridesHandler::CaptureScreenshot, | 162 double scale = 1; |
148 weak_factory_.GetWeakPtr(), | 163 base::DictionaryValue* params = command->params(); |
149 command)); | 164 if (params) { |
| 165 params->GetString(devtools::Page::captureScreenshot::kParamFormat, |
| 166 &format); |
| 167 params->GetInteger(devtools::Page::captureScreenshot::kParamQuality, |
| 168 &quality); |
| 169 params->GetDouble(devtools::Page::captureScreenshot::kParamScale, |
| 170 &scale); |
| 171 } |
| 172 if (format.empty()) |
| 173 format = kPng; |
| 174 if (quality < 0 || quality > 100) |
| 175 quality = kDefaultScreenshotQuality; |
| 176 if (scale <= 0 || scale > 1) |
| 177 scale = 1; |
150 | 178 |
| 179 RenderViewHost* host = agent_->GetRenderViewHost(); |
| 180 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); |
| 181 |
| 182 // Grab screen pixels if available for current platform. |
| 183 // TODO(pfeldman): support format, scale and quality in ui::GrabViewSnapshot. |
| 184 std::vector<unsigned char> png; |
| 185 bool is_unscaled_png = scale == 1 && format == kPng; |
| 186 if (is_unscaled_png && ui::GrabViewSnapshot(host->GetView()->GetNativeView(), |
| 187 &png, view_bounds)) { |
| 188 std::string base64_data; |
| 189 bool success = base::Base64Encode( |
| 190 base::StringPiece(reinterpret_cast<char*>(&*png.begin()), png.size()), |
| 191 &base64_data); |
| 192 if (success) { |
| 193 base::DictionaryValue* result = new base::DictionaryValue(); |
| 194 result->SetString( |
| 195 devtools::Page::captureScreenshot::kResponseData, base64_data); |
| 196 return command->SuccessResponse(result); |
| 197 } |
| 198 return command->InternalErrorResponse("Unable to base64encode screenshot"); |
| 199 } |
| 200 |
| 201 // Fallback to copying from compositing surface. |
| 202 RenderWidgetHostViewPort* view_port = |
| 203 RenderWidgetHostViewPort::FromRWHV(host->GetView()); |
| 204 |
| 205 gfx::Size snapshot_size = gfx::ToFlooredSize( |
| 206 gfx::ScaleSize(view_bounds.size(), scale)); |
| 207 view_port->CopyFromCompositingSurface( |
| 208 view_bounds, snapshot_size, |
| 209 base::Bind(&RendererOverridesHandler::ScreenshotCaptured, |
| 210 weak_factory_.GetWeakPtr(), command, format, quality, scale)); |
151 return command->AsyncResponsePromise(); | 211 return command->AsyncResponsePromise(); |
152 } | 212 } |
153 | 213 |
154 void RendererOverridesHandler::CaptureScreenshot( | 214 void RendererOverridesHandler::ScreenshotCaptured( |
155 scoped_refptr<DevToolsProtocol::Command> command) { | 215 scoped_refptr<DevToolsProtocol::Command> command, |
| 216 const std::string& format, |
| 217 int quality, |
| 218 double scale, |
| 219 bool success, |
| 220 const SkBitmap& bitmap) { |
| 221 if (!success) { |
| 222 SendRawMessage( |
| 223 command->InternalErrorResponse("Unable to capture screenshot")-> |
| 224 Serialize()); |
| 225 return; |
| 226 } |
156 | 227 |
157 RenderViewHost* host = agent_->GetRenderViewHost(); | 228 std::vector<unsigned char> data; |
158 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); | 229 SkAutoLockPixels lock_image(bitmap); |
159 gfx::Rect snapshot_bounds(view_bounds.size()); | 230 bool encoded; |
160 gfx::Size snapshot_size = snapshot_bounds.size(); | 231 if (format == kPng) { |
| 232 encoded = gfx::PNGCodec::Encode( |
| 233 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 234 gfx::PNGCodec::FORMAT_SkBitmap, |
| 235 gfx::Size(bitmap.width(), bitmap.height()), |
| 236 bitmap.width() * bitmap.bytesPerPixel(), |
| 237 false, std::vector<gfx::PNGCodec::Comment>(), &data); |
| 238 } else if (format == kJpeg) { |
| 239 encoded = gfx::JPEGCodec::Encode( |
| 240 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 241 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 242 bitmap.width(), |
| 243 bitmap.height(), |
| 244 bitmap.width() * bitmap.bytesPerPixel(), |
| 245 quality, &data); |
| 246 } else { |
| 247 encoded = false; |
| 248 } |
161 | 249 |
162 std::vector<unsigned char> png; | 250 if (!encoded) { |
163 if (ui::GrabViewSnapshot(host->GetView()->GetNativeView(), | 251 SendRawMessage( |
164 &png, | 252 command->InternalErrorResponse("Unable to encode screenshot")-> |
165 snapshot_bounds)) { | 253 Serialize()); |
166 std::string base_64_data; | 254 return; |
167 bool success = base::Base64Encode( | |
168 base::StringPiece(reinterpret_cast<char*>(&*png.begin()), png.size()), | |
169 &base_64_data); | |
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 } | 255 } |
180 SendRawMessage(command-> | 256 |
181 InternalErrorResponse("Unable to capture a screenshot")->Serialize()); | 257 std::string base_64_data; |
| 258 if (!base::Base64Encode(base::StringPiece( |
| 259 reinterpret_cast<char*>(&data[0]), |
| 260 data.size()), |
| 261 &base_64_data)) { |
| 262 SendRawMessage( |
| 263 command->InternalErrorResponse("Unable to base64 encode screenshot")-> |
| 264 Serialize()); |
| 265 return; |
| 266 } |
| 267 |
| 268 base::DictionaryValue* response = new base::DictionaryValue(); |
| 269 response->SetString( |
| 270 devtools::Page::captureScreenshot::kResponseData, base_64_data); |
| 271 SendRawMessage(command->SuccessResponse(response)->Serialize()); |
182 } | 272 } |
183 | 273 |
184 } // namespace content | 274 } // namespace content |
OLD | NEW |