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