OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/protocol/page_handler.h" | 5 #include "content/browser/devtools/protocol/page_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" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 screencast_quality_(kDefaultScreenshotQuality), | 91 screencast_quality_(kDefaultScreenshotQuality), |
92 screencast_max_width_(-1), | 92 screencast_max_width_(-1), |
93 screencast_max_height_(-1), | 93 screencast_max_height_(-1), |
94 capture_retry_count_(0), | 94 capture_retry_count_(0), |
95 has_compositor_frame_metadata_(false), | 95 has_compositor_frame_metadata_(false), |
96 screencast_frame_sent_(0), | 96 screencast_frame_sent_(0), |
97 screencast_frame_acked_(0), | 97 screencast_frame_acked_(0), |
98 processing_screencast_frame_(false), | 98 processing_screencast_frame_(false), |
99 color_picker_(new ColorPicker(base::Bind( | 99 color_picker_(new ColorPicker(base::Bind( |
100 &PageHandler::OnColorPicked, base::Unretained(this)))), | 100 &PageHandler::OnColorPicked, base::Unretained(this)))), |
101 host_(nullptr), | 101 web_contents_(nullptr), |
102 screencast_listener_(nullptr), | 102 screencast_listener_(nullptr), |
103 weak_factory_(this) { | 103 weak_factory_(this) { |
104 } | 104 } |
105 | 105 |
106 PageHandler::~PageHandler() { | 106 PageHandler::~PageHandler() { |
107 } | 107 } |
108 | 108 |
109 void PageHandler::SetRenderViewHost(RenderViewHostImpl* host) { | 109 void PageHandler::SetWebContents(WebContentsImpl* web_contents) { |
110 if (host_ == host) | 110 if (web_contents_ == web_contents) |
111 return; | 111 return; |
112 | 112 |
113 color_picker_->SetRenderViewHost(host); | 113 web_contents_ = web_contents; |
114 host_ = host; | 114 color_picker_->SetRenderWidgetHost(GetRenderWidgetHost()); |
115 } | 115 } |
116 | 116 |
117 void PageHandler::SetClient(scoped_ptr<Client> client) { | 117 void PageHandler::SetClient(scoped_ptr<Client> client) { |
118 client_.swap(client); | 118 client_.swap(client); |
119 } | 119 } |
120 | 120 |
121 void PageHandler::Detached() { | 121 void PageHandler::Detached() { |
122 Disable(); | 122 Disable(); |
123 } | 123 } |
124 | 124 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 screencast_enabled_ = false; | 166 screencast_enabled_ = false; |
167 color_picker_->SetEnabled(false); | 167 color_picker_->SetEnabled(false); |
168 if (screencast_listener_) | 168 if (screencast_listener_) |
169 screencast_listener_->ScreencastEnabledChanged(); | 169 screencast_listener_->ScreencastEnabledChanged(); |
170 return Response::FallThrough(); | 170 return Response::FallThrough(); |
171 } | 171 } |
172 | 172 |
173 Response PageHandler::Reload(const bool* ignoreCache, | 173 Response PageHandler::Reload(const bool* ignoreCache, |
174 const std::string* script_to_evaluate_on_load, | 174 const std::string* script_to_evaluate_on_load, |
175 const std::string* script_preprocessor) { | 175 const std::string* script_preprocessor) { |
176 if (!host_) | 176 if (!web_contents_) |
177 return Response::InternalError("Could not connect to view"); | 177 return Response::InternalError("Could not connect to view"); |
178 | 178 |
179 WebContents* web_contents = WebContents::FromRenderViewHost(host_); | |
180 if (!web_contents) | |
181 return Response::InternalError("No WebContents to reload"); | |
182 | |
183 // Handle in browser only if it is crashed. | 179 // Handle in browser only if it is crashed. |
184 if (!web_contents->IsCrashed()) | 180 if (!web_contents_->IsCrashed()) |
185 return Response::FallThrough(); | 181 return Response::FallThrough(); |
186 | 182 |
187 web_contents->GetController().Reload(false); | 183 web_contents_->GetController().Reload(false); |
188 return Response::OK(); | 184 return Response::OK(); |
189 } | 185 } |
190 | 186 |
191 Response PageHandler::Navigate(const std::string& url, | 187 Response PageHandler::Navigate(const std::string& url, |
192 FrameId* frame_id) { | 188 FrameId* frame_id) { |
193 GURL gurl(url); | 189 GURL gurl(url); |
194 if (!gurl.is_valid()) | 190 if (!gurl.is_valid()) |
195 return Response::InternalError("Cannot navigate to invalid URL"); | 191 return Response::InternalError("Cannot navigate to invalid URL"); |
196 | 192 |
197 if (!host_) | 193 if (!web_contents_) |
198 return Response::InternalError("Could not connect to view"); | 194 return Response::InternalError("Could not connect to view"); |
199 | 195 |
200 WebContents* web_contents = WebContents::FromRenderViewHost(host_); | 196 web_contents_->GetController() |
201 if (!web_contents) | |
202 return Response::InternalError("No WebContents to navigate"); | |
203 | |
204 web_contents->GetController() | |
205 .LoadURL(gurl, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); | 197 .LoadURL(gurl, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); |
206 return Response::FallThrough(); | 198 return Response::FallThrough(); |
207 } | 199 } |
208 | 200 |
209 Response PageHandler::GetNavigationHistory(int* current_index, | 201 Response PageHandler::GetNavigationHistory(int* current_index, |
210 NavigationEntries* entries) { | 202 NavigationEntries* entries) { |
211 if (!host_) | 203 if (!web_contents_) |
212 return Response::InternalError("Could not connect to view"); | 204 return Response::InternalError("Could not connect to view"); |
213 | 205 |
214 WebContents* web_contents = WebContents::FromRenderViewHost(host_); | 206 NavigationController& controller = web_contents_->GetController(); |
215 if (!web_contents) | |
216 return Response::InternalError("No WebContents to navigate"); | |
217 | |
218 NavigationController& controller = web_contents->GetController(); | |
219 *current_index = controller.GetCurrentEntryIndex(); | 207 *current_index = controller.GetCurrentEntryIndex(); |
220 for (int i = 0; i != controller.GetEntryCount(); ++i) { | 208 for (int i = 0; i != controller.GetEntryCount(); ++i) { |
221 entries->push_back(NavigationEntry::Create() | 209 entries->push_back(NavigationEntry::Create() |
222 ->set_id(controller.GetEntryAtIndex(i)->GetUniqueID()) | 210 ->set_id(controller.GetEntryAtIndex(i)->GetUniqueID()) |
223 ->set_url(controller.GetEntryAtIndex(i)->GetURL().spec()) | 211 ->set_url(controller.GetEntryAtIndex(i)->GetURL().spec()) |
224 ->set_title( | 212 ->set_title( |
225 base::UTF16ToUTF8(controller.GetEntryAtIndex(i)->GetTitle()))); | 213 base::UTF16ToUTF8(controller.GetEntryAtIndex(i)->GetTitle()))); |
226 } | 214 } |
227 return Response::OK(); | 215 return Response::OK(); |
228 } | 216 } |
229 | 217 |
230 Response PageHandler::NavigateToHistoryEntry(int entry_id) { | 218 Response PageHandler::NavigateToHistoryEntry(int entry_id) { |
231 if (!host_) | 219 if (!web_contents_) |
232 return Response::InternalError("Could not connect to view"); | 220 return Response::InternalError("Could not connect to view"); |
233 | 221 |
234 WebContents* web_contents = WebContents::FromRenderViewHost(host_); | 222 NavigationController& controller = web_contents_->GetController(); |
235 if (!web_contents) | |
236 return Response::InternalError("No WebContents to navigate"); | |
237 | |
238 NavigationController& controller = web_contents->GetController(); | |
239 for (int i = 0; i != controller.GetEntryCount(); ++i) { | 223 for (int i = 0; i != controller.GetEntryCount(); ++i) { |
240 if (controller.GetEntryAtIndex(i)->GetUniqueID() == entry_id) { | 224 if (controller.GetEntryAtIndex(i)->GetUniqueID() == entry_id) { |
241 controller.GoToIndex(i); | 225 controller.GoToIndex(i); |
242 return Response::OK(); | 226 return Response::OK(); |
243 } | 227 } |
244 } | 228 } |
245 | 229 |
246 return Response::InvalidParams("No entry with passed id"); | 230 return Response::InvalidParams("No entry with passed id"); |
247 } | 231 } |
248 | 232 |
249 Response PageHandler::CaptureScreenshot(DevToolsCommandId command_id) { | 233 Response PageHandler::CaptureScreenshot(DevToolsCommandId command_id) { |
250 if (!host_ || !host_->GetView()) | 234 if (!web_contents_ || !web_contents_->GetRenderWidgetHostView()) |
251 return Response::InternalError("Could not connect to view"); | 235 return Response::InternalError("Could not connect to view"); |
252 | 236 |
253 host_->GetSnapshotFromBrowser( | 237 GetRenderWidgetHost()->GetSnapshotFromBrowser( |
254 base::Bind(&PageHandler::ScreenshotCaptured, | 238 base::Bind(&PageHandler::ScreenshotCaptured, |
255 weak_factory_.GetWeakPtr(), command_id)); | 239 weak_factory_.GetWeakPtr(), command_id)); |
256 return Response::OK(); | 240 return Response::OK(); |
257 } | 241 } |
258 | 242 |
259 Response PageHandler::CanScreencast(bool* result) { | 243 Response PageHandler::CanScreencast(bool* result) { |
260 #if defined(OS_ANDROID) | 244 #if defined(OS_ANDROID) |
261 *result = true; | 245 *result = true; |
262 #else | 246 #else |
263 *result = false; | 247 *result = false; |
264 #endif // defined(OS_ANDROID) | 248 #endif // defined(OS_ANDROID) |
265 return Response::OK(); | 249 return Response::OK(); |
266 } | 250 } |
267 | 251 |
268 Response PageHandler::StartScreencast(const std::string* format, | 252 Response PageHandler::StartScreencast(const std::string* format, |
269 const int* quality, | 253 const int* quality, |
270 const int* max_width, | 254 const int* max_width, |
271 const int* max_height) { | 255 const int* max_height) { |
272 if (!host_) | 256 if (!web_contents_) |
273 return Response::InternalError("Could not connect to view"); | 257 return Response::InternalError("Could not connect to view"); |
274 | 258 |
275 screencast_enabled_ = true; | 259 screencast_enabled_ = true; |
276 screencast_format_ = format ? *format : kPng; | 260 screencast_format_ = format ? *format : kPng; |
277 screencast_quality_ = quality ? *quality : kDefaultScreenshotQuality; | 261 screencast_quality_ = quality ? *quality : kDefaultScreenshotQuality; |
278 if (screencast_quality_ < 0 || screencast_quality_ > 100) | 262 if (screencast_quality_ < 0 || screencast_quality_ > 100) |
279 screencast_quality_ = kDefaultScreenshotQuality; | 263 screencast_quality_ = kDefaultScreenshotQuality; |
280 screencast_max_width_ = max_width ? *max_width : -1; | 264 screencast_max_width_ = max_width ? *max_width : -1; |
281 screencast_max_height_ = max_height ? *max_height : -1; | 265 screencast_max_height_ = max_height ? *max_height : -1; |
282 | 266 |
283 bool visible = !host_->is_hidden(); | 267 bool visible = !GetRenderWidgetHost()->is_hidden(); |
284 NotifyScreencastVisibility(visible); | 268 NotifyScreencastVisibility(visible); |
285 if (visible) { | 269 if (visible) { |
286 if (has_compositor_frame_metadata_) | 270 if (has_compositor_frame_metadata_) { |
287 InnerSwapCompositorFrame(); | 271 InnerSwapCompositorFrame(); |
288 else | 272 } else { |
289 host_->Send(new ViewMsg_ForceRedraw(host_->GetRoutingID(), 0)); | 273 GetRenderWidgetHost()->Send( |
| 274 new ViewMsg_ForceRedraw(GetRenderWidgetHost()->GetRoutingID(), 0)); |
| 275 } |
290 } | 276 } |
291 if (screencast_listener_) | 277 if (screencast_listener_) |
292 screencast_listener_->ScreencastEnabledChanged(); | 278 screencast_listener_->ScreencastEnabledChanged(); |
293 return Response::FallThrough(); | 279 return Response::FallThrough(); |
294 } | 280 } |
295 | 281 |
296 Response PageHandler::StopScreencast() { | 282 Response PageHandler::StopScreencast() { |
297 screencast_enabled_ = false; | 283 screencast_enabled_ = false; |
298 if (screencast_listener_) | 284 if (screencast_listener_) |
299 screencast_listener_->ScreencastEnabledChanged(); | 285 screencast_listener_->ScreencastEnabledChanged(); |
300 return Response::FallThrough(); | 286 return Response::FallThrough(); |
301 } | 287 } |
302 | 288 |
303 Response PageHandler::ScreencastFrameAck(int frame_number) { | 289 Response PageHandler::ScreencastFrameAck(int frame_number) { |
304 screencast_frame_acked_ = frame_number; | 290 screencast_frame_acked_ = frame_number; |
305 return Response::OK(); | 291 return Response::OK(); |
306 } | 292 } |
307 | 293 |
308 Response PageHandler::HandleJavaScriptDialog(bool accept, | 294 Response PageHandler::HandleJavaScriptDialog(bool accept, |
309 const std::string* prompt_text) { | 295 const std::string* prompt_text) { |
310 base::string16 prompt_override; | 296 base::string16 prompt_override; |
311 if (prompt_text) | 297 if (prompt_text) |
312 prompt_override = base::UTF8ToUTF16(*prompt_text); | 298 prompt_override = base::UTF8ToUTF16(*prompt_text); |
313 | 299 |
314 if (!host_) | 300 if (!web_contents_) |
315 return Response::InternalError("Could not connect to view"); | 301 return Response::InternalError("Could not connect to view"); |
316 | 302 |
317 WebContents* web_contents = WebContents::FromRenderViewHost(host_); | |
318 if (!web_contents) | |
319 return Response::InternalError("No JavaScript dialog to handle"); | |
320 | |
321 JavaScriptDialogManager* manager = | 303 JavaScriptDialogManager* manager = |
322 web_contents->GetDelegate()->GetJavaScriptDialogManager(web_contents); | 304 web_contents_->GetDelegate()->GetJavaScriptDialogManager(web_contents_); |
323 if (manager && manager->HandleJavaScriptDialog( | 305 if (manager && manager->HandleJavaScriptDialog( |
324 web_contents, accept, prompt_text ? &prompt_override : nullptr)) { | 306 web_contents_, accept, prompt_text ? &prompt_override : nullptr)) { |
325 return Response::OK(); | 307 return Response::OK(); |
326 } | 308 } |
327 | 309 |
328 return Response::InternalError("Could not handle JavaScript dialog"); | 310 return Response::InternalError("Could not handle JavaScript dialog"); |
329 } | 311 } |
330 | 312 |
331 Response PageHandler::QueryUsageAndQuota(DevToolsCommandId command_id, | 313 Response PageHandler::QueryUsageAndQuota(DevToolsCommandId command_id, |
332 const std::string& security_origin) { | 314 const std::string& security_origin) { |
333 return Response::OK(); | 315 return Response::OK(); |
334 } | 316 } |
335 | 317 |
336 Response PageHandler::SetColorPickerEnabled(bool enabled) { | 318 Response PageHandler::SetColorPickerEnabled(bool enabled) { |
337 if (!host_) | 319 if (!web_contents_) |
338 return Response::InternalError("Could not connect to view"); | 320 return Response::InternalError("Could not connect to view"); |
339 | 321 |
340 color_picker_->SetEnabled(enabled); | 322 color_picker_->SetEnabled(enabled); |
341 return Response::OK(); | 323 return Response::OK(); |
342 } | 324 } |
343 | 325 |
| 326 RenderWidgetHostImpl* PageHandler::GetRenderWidgetHost() { |
| 327 // TODO(dgozman): there should be a way to get main RenderWidgetHost from |
| 328 // WebContents without RenderViewHost. |
| 329 return web_contents_ ? |
| 330 static_cast<RenderViewHostImpl*>(web_contents_->GetRenderViewHost()) : |
| 331 nullptr; |
| 332 } |
| 333 |
344 void PageHandler::NotifyScreencastVisibility(bool visible) { | 334 void PageHandler::NotifyScreencastVisibility(bool visible) { |
345 if (visible) | 335 if (visible) |
346 capture_retry_count_ = kCaptureRetryLimit; | 336 capture_retry_count_ = kCaptureRetryLimit; |
347 client_->ScreencastVisibilityChanged( | 337 client_->ScreencastVisibilityChanged( |
348 ScreencastVisibilityChangedParams::Create()->set_visible(visible)); | 338 ScreencastVisibilityChangedParams::Create()->set_visible(visible)); |
349 } | 339 } |
350 | 340 |
351 void PageHandler::InnerSwapCompositorFrame() { | 341 void PageHandler::InnerSwapCompositorFrame() { |
352 if (screencast_frame_sent_ - screencast_frame_acked_ > | 342 if (screencast_frame_sent_ - screencast_frame_acked_ > |
353 kMaxScreencastFramesInFlight || processing_screencast_frame_) { | 343 kMaxScreencastFramesInFlight || processing_screencast_frame_) { |
354 return; | 344 return; |
355 } | 345 } |
356 | 346 |
357 if (!host_ || !host_->GetView()) | 347 if (!web_contents_ || !web_contents_->GetRenderWidgetHostView()) |
358 return; | 348 return; |
359 | 349 |
360 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>( | 350 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>( |
361 host_->GetView()); | 351 web_contents_->GetRenderWidgetHostView()); |
362 // TODO(vkuzkokov): do not use previous frame metadata. | 352 // TODO(vkuzkokov): do not use previous frame metadata. |
363 cc::CompositorFrameMetadata& metadata = last_compositor_frame_metadata_; | 353 cc::CompositorFrameMetadata& metadata = last_compositor_frame_metadata_; |
364 | 354 |
365 gfx::SizeF viewport_size_dip = gfx::ScaleSize( | 355 gfx::SizeF viewport_size_dip = gfx::ScaleSize( |
366 metadata.scrollable_viewport_size, metadata.page_scale_factor); | 356 metadata.scrollable_viewport_size, metadata.page_scale_factor); |
367 gfx::SizeF screen_size_dip = gfx::ScaleSize(view->GetPhysicalBackingSize(), | 357 gfx::SizeF screen_size_dip = gfx::ScaleSize(view->GetPhysicalBackingSize(), |
368 1 / metadata.device_scale_factor); | 358 1 / metadata.device_scale_factor); |
369 | 359 |
370 blink::WebScreenInfo screen_info; | 360 blink::WebScreenInfo screen_info; |
371 view->GetScreenInfo(&screen_info); | 361 view->GetScreenInfo(&screen_info); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 weak_factory_.GetWeakPtr(), metadata, base::Time::Now())); | 415 weak_factory_.GetWeakPtr(), metadata, base::Time::Now())); |
426 } | 416 } |
427 | 417 |
428 void PageHandler::ScreencastFrameEncoded( | 418 void PageHandler::ScreencastFrameEncoded( |
429 const cc::CompositorFrameMetadata& metadata, | 419 const cc::CompositorFrameMetadata& metadata, |
430 const base::Time& timestamp, | 420 const base::Time& timestamp, |
431 const std::string& data) { | 421 const std::string& data) { |
432 processing_screencast_frame_ = false; | 422 processing_screencast_frame_ = false; |
433 | 423 |
434 // Consider metadata empty in case it has no device scale factor. | 424 // Consider metadata empty in case it has no device scale factor. |
435 if (metadata.device_scale_factor == 0 || !host_ || data.empty()) | 425 if (metadata.device_scale_factor == 0 || !web_contents_ || data.empty()) |
436 return; | 426 return; |
437 | 427 |
438 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>( | 428 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>( |
439 host_->GetView()); | 429 web_contents_->GetRenderWidgetHostView()); |
440 if (!view) | 430 if (!view) |
441 return; | 431 return; |
442 | 432 |
443 gfx::SizeF screen_size_dip = gfx::ScaleSize( | 433 gfx::SizeF screen_size_dip = gfx::ScaleSize( |
444 view->GetPhysicalBackingSize(), 1 / metadata.device_scale_factor); | 434 view->GetPhysicalBackingSize(), 1 / metadata.device_scale_factor); |
445 scoped_refptr<ScreencastFrameMetadata> param_metadata = | 435 scoped_refptr<ScreencastFrameMetadata> param_metadata = |
446 ScreencastFrameMetadata::Create() | 436 ScreencastFrameMetadata::Create() |
447 ->set_page_scale_factor(metadata.page_scale_factor) | 437 ->set_page_scale_factor(metadata.page_scale_factor) |
448 ->set_offset_top(metadata.location_bar_content_translation.y()) | 438 ->set_offset_top(metadata.location_bar_content_translation.y()) |
449 ->set_device_width(screen_size_dip.width()) | 439 ->set_device_width(screen_size_dip.width()) |
(...skipping 27 matching lines...) Expand all Loading... |
477 | 467 |
478 void PageHandler::OnColorPicked(int r, int g, int b, int a) { | 468 void PageHandler::OnColorPicked(int r, int g, int b, int a) { |
479 scoped_refptr<dom::RGBA> color = | 469 scoped_refptr<dom::RGBA> color = |
480 dom::RGBA::Create()->set_r(r)->set_g(g)->set_b(b)->set_a(a); | 470 dom::RGBA::Create()->set_r(r)->set_g(g)->set_b(b)->set_a(a); |
481 client_->ColorPicked(ColorPickedParams::Create()->set_color(color)); | 471 client_->ColorPicked(ColorPickedParams::Create()->set_color(color)); |
482 } | 472 } |
483 | 473 |
484 } // namespace page | 474 } // namespace page |
485 } // namespace devtools | 475 } // namespace devtools |
486 } // namespace content | 476 } // namespace content |
OLD | NEW |