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" |
19 #include "content/public/browser/devtools_agent_host.h" | 21 #include "content/public/browser/devtools_agent_host.h" |
20 #include "content/public/browser/javascript_dialog_manager.h" | 22 #include "content/public/browser/javascript_dialog_manager.h" |
21 #include "content/public/browser/navigation_controller.h" | 23 #include "content/public/browser/navigation_controller.h" |
22 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/render_view_host.h" | 25 #include "content/public/browser/render_view_host.h" |
24 #include "content/public/browser/render_widget_host_view.h" | 26 #include "content/public/browser/render_widget_host_view.h" |
25 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
26 #include "content/public/browser/web_contents_delegate.h" | 28 #include "content/public/browser/web_contents_delegate.h" |
27 #include "content/public/common/page_transition_types.h" | 29 #include "content/public/common/page_transition_types.h" |
28 #include "content/public/common/referrer.h" | 30 #include "content/public/common/referrer.h" |
29 #include "ui/snapshot/snapshot.h" | |
30 #include "url/gurl.h" | 31 #include "url/gurl.h" |
31 | 32 |
| 33 using base::TimeTicks; |
| 34 |
32 namespace content { | 35 namespace content { |
33 | 36 |
34 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) | 37 RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent) |
35 : agent_(agent) { | 38 : agent_(agent) { |
36 RegisterCommandHandler( | 39 RegisterCommandHandler( |
37 devtools::DOM::setFileInputFiles::kName, | 40 devtools::DOM::setFileInputFiles::kName, |
38 base::Bind( | 41 base::Bind( |
39 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, | 42 &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles, |
40 base::Unretained(this))); | 43 base::Unretained(this))); |
41 RegisterCommandHandler( | 44 RegisterCommandHandler( |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 .LoadURL(gurl, Referrer(), PAGE_TRANSITION_TYPED, std::string()); | 135 .LoadURL(gurl, Referrer(), PAGE_TRANSITION_TYPED, std::string()); |
133 return command->SuccessResponse(new base::DictionaryValue()); | 136 return command->SuccessResponse(new base::DictionaryValue()); |
134 } | 137 } |
135 } | 138 } |
136 return command->InternalErrorResponse("No WebContents to navigate"); | 139 return command->InternalErrorResponse("No WebContents to navigate"); |
137 } | 140 } |
138 | 141 |
139 scoped_ptr<DevToolsProtocol::Response> | 142 scoped_ptr<DevToolsProtocol::Response> |
140 RendererOverridesHandler::PageCaptureScreenshot( | 143 RendererOverridesHandler::PageCaptureScreenshot( |
141 DevToolsProtocol::Command* command) { | 144 DevToolsProtocol::Command* command) { |
| 145 std::string format; |
| 146 int quality = 100; |
| 147 double scale = 0; |
| 148 |
| 149 base::DictionaryValue* params = command->params(); |
| 150 if (params) { |
| 151 params->GetString(devtools::Page::captureScreenshot::kParamFormat, |
| 152 &format); |
| 153 params->GetInteger(devtools::Page::captureScreenshot::kParamQuality, |
| 154 &quality); |
| 155 params->GetDouble(devtools::Page::captureScreenshot::kParamScale, |
| 156 &scale); |
| 157 } |
| 158 |
| 159 if (format.empty()) |
| 160 format = "png"; |
| 161 if (quality < 0 || quality > 100) |
| 162 quality = 100; |
| 163 if (scale <= 0 || scale > 1) |
| 164 scale = 1; |
| 165 |
142 std::string base_64_data; | 166 std::string base_64_data; |
143 if (!CaptureScreenshot(&base_64_data)) | 167 if (!CaptureScreenshot(format, quality, scale, &base_64_data)) |
144 return command->InternalErrorResponse("Unable to capture a screenshot"); | 168 return command->InternalErrorResponse( |
| 169 "Unable to capture a screenshot or " |
| 170 "image format is not supported by the target"); |
145 | 171 |
146 base::DictionaryValue* response = new base::DictionaryValue(); | 172 base::DictionaryValue* response = new base::DictionaryValue(); |
147 response->SetString( | 173 response->SetString( |
148 devtools::Page::captureScreenshot::kResponseData, base_64_data); | 174 devtools::Page::captureScreenshot::kResponseData, base_64_data); |
149 return command->SuccessResponse(response); | 175 return command->SuccessResponse(response); |
150 } | 176 } |
151 | 177 |
152 bool RendererOverridesHandler::CaptureScreenshot(std::string* base_64_data) { | 178 bool RendererOverridesHandler::CaptureScreenshot(const std::string& format, |
| 179 int quality, |
| 180 double scale, |
| 181 std::string* base_64_data) { |
153 RenderViewHost* host = agent_->GetRenderViewHost(); | 182 RenderViewHost* host = agent_->GetRenderViewHost(); |
154 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); | 183 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); |
155 gfx::Rect snapshot_bounds(view_bounds.size()); | 184 gfx::Rect snapshot_bounds(view_bounds.size()); |
156 gfx::Size snapshot_size = snapshot_bounds.size(); | 185 gfx::Size snapshot_size = snapshot_bounds.size(); |
157 std::vector<unsigned char> png; | 186 RenderViewHostDelegate* delegate = host->GetDelegate(); |
158 if (!ui::GrabViewSnapshot(host->GetView()->GetNativeView(), | 187 RenderViewHostDelegateView* view = delegate->GetDelegateView(); |
159 &png, | 188 |
160 snapshot_bounds)) | 189 if (!view) |
161 return false; | 190 return false; |
162 | 191 |
163 return base::Base64Encode(base::StringPiece( | 192 std::vector<uint8> data; |
164 reinterpret_cast<char*>(&*png.begin()), | 193 if (!view->GrabSnapshot(format, quality, scale, &data)) |
165 png.size()), | 194 return false; |
| 195 |
| 196 bool res = base::Base64Encode(base::StringPiece( |
| 197 reinterpret_cast<char*>(&*data.begin()), |
| 198 data.size()), |
166 base_64_data); | 199 base_64_data); |
| 200 return res; |
167 } | 201 } |
168 | 202 |
169 } // namespace content | 203 } // namespace content |
OLD | NEW |