Chromium Code Reviews| 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" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/threading/worker_pool.h" | 16 #include "base/threading/worker_pool.h" |
| 17 #include "content/browser/devtools/page_navigation_throttle.h" | |
| 17 #include "content/browser/devtools/protocol/color_picker.h" | 18 #include "content/browser/devtools/protocol/color_picker.h" |
| 18 #include "content/browser/renderer_host/render_widget_host_impl.h" | 19 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 19 #include "content/browser/renderer_host/render_widget_host_view_base.h" | 20 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
| 20 #include "content/browser/web_contents/web_contents_impl.h" | 21 #include "content/browser/web_contents/web_contents_impl.h" |
| 21 #include "content/common/view_messages.h" | 22 #include "content/common/view_messages.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/javascript_dialog_manager.h" | 24 #include "content/public/browser/javascript_dialog_manager.h" |
| 24 #include "content/public/browser/navigation_controller.h" | 25 #include "content/public/browser/navigation_controller.h" |
| 25 #include "content/public/browser/navigation_entry.h" | 26 #include "content/public/browser/navigation_entry.h" |
| 27 #include "content/public/browser/navigation_handle.h" | |
| 26 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 27 #include "content/public/browser/notification_types.h" | 29 #include "content/public/browser/notification_types.h" |
| 28 #include "content/public/browser/storage_partition.h" | 30 #include "content/public/browser/storage_partition.h" |
| 29 #include "content/public/browser/web_contents_delegate.h" | 31 #include "content/public/browser/web_contents_delegate.h" |
| 30 #include "content/public/common/referrer.h" | 32 #include "content/public/common/referrer.h" |
| 31 #include "third_party/WebKit/public/platform/WebScreenInfo.h" | 33 #include "third_party/WebKit/public/platform/WebScreenInfo.h" |
| 32 #include "third_party/skia/include/core/SkBitmap.h" | 34 #include "third_party/skia/include/core/SkBitmap.h" |
| 33 #include "ui/base/page_transition_types.h" | 35 #include "ui/base/page_transition_types.h" |
| 34 #include "ui/gfx/codec/jpeg_codec.h" | 36 #include "ui/gfx/codec/jpeg_codec.h" |
| 35 #include "ui/gfx/codec/png_codec.h" | 37 #include "ui/gfx/codec/png_codec.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 screencast_enabled_(false), | 97 screencast_enabled_(false), |
| 96 screencast_quality_(kDefaultScreenshotQuality), | 98 screencast_quality_(kDefaultScreenshotQuality), |
| 97 screencast_max_width_(-1), | 99 screencast_max_width_(-1), |
| 98 screencast_max_height_(-1), | 100 screencast_max_height_(-1), |
| 99 capture_every_nth_frame_(1), | 101 capture_every_nth_frame_(1), |
| 100 capture_retry_count_(0), | 102 capture_retry_count_(0), |
| 101 has_compositor_frame_metadata_(false), | 103 has_compositor_frame_metadata_(false), |
| 102 session_id_(0), | 104 session_id_(0), |
| 103 frame_counter_(0), | 105 frame_counter_(0), |
| 104 frames_in_flight_(0), | 106 frames_in_flight_(0), |
| 105 color_picker_(new ColorPicker(base::Bind( | 107 color_picker_(new ColorPicker( |
| 106 &PageHandler::OnColorPicked, base::Unretained(this)))), | 108 base::Bind(&PageHandler::OnColorPicked, base::Unretained(this)))), |
| 109 navigation_throttle_enabled_(false), | |
| 110 next_navigation_id_(0), | |
| 107 host_(nullptr), | 111 host_(nullptr), |
| 108 weak_factory_(this) { | 112 weak_factory_(this) {} |
| 109 } | |
| 110 | 113 |
| 111 PageHandler::~PageHandler() { | 114 PageHandler::~PageHandler() { |
| 112 } | 115 } |
| 113 | 116 |
| 114 void PageHandler::SetRenderFrameHost(RenderFrameHostImpl* host) { | 117 void PageHandler::SetRenderFrameHost(RenderFrameHostImpl* host) { |
| 115 if (host_ == host) | 118 if (host_ == host) |
| 116 return; | 119 return; |
| 117 | 120 |
| 118 RenderWidgetHostImpl* widget_host = | 121 RenderWidgetHostImpl* widget_host = |
| 119 host_ ? host_->GetRenderWidgetHost() : nullptr; | 122 host_ ? host_->GetRenderWidgetHost() : nullptr; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 } | 197 } |
| 195 | 198 |
| 196 Response PageHandler::Enable() { | 199 Response PageHandler::Enable() { |
| 197 enabled_ = true; | 200 enabled_ = true; |
| 198 return Response::FallThrough(); | 201 return Response::FallThrough(); |
| 199 } | 202 } |
| 200 | 203 |
| 201 Response PageHandler::Disable() { | 204 Response PageHandler::Disable() { |
| 202 enabled_ = false; | 205 enabled_ = false; |
| 203 screencast_enabled_ = false; | 206 screencast_enabled_ = false; |
| 207 SetControlNavigations(false); | |
| 204 color_picker_->SetEnabled(false); | 208 color_picker_->SetEnabled(false); |
| 205 return Response::FallThrough(); | 209 return Response::FallThrough(); |
| 206 } | 210 } |
| 207 | 211 |
| 208 Response PageHandler::Reload(const bool* bypassCache, | 212 Response PageHandler::Reload(const bool* bypassCache, |
| 209 const std::string* script_to_evaluate_on_load, | 213 const std::string* script_to_evaluate_on_load, |
| 210 const std::string* script_preprocessor) { | 214 const std::string* script_preprocessor) { |
| 211 WebContentsImpl* web_contents = GetWebContents(); | 215 WebContentsImpl* web_contents = GetWebContents(); |
| 212 if (!web_contents) | 216 if (!web_contents) |
| 213 return Response::InternalError("Could not connect to view"); | 217 return Response::InternalError("Could not connect to view"); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 366 } | 370 } |
| 367 | 371 |
| 368 Response PageHandler::RequestAppBanner() { | 372 Response PageHandler::RequestAppBanner() { |
| 369 WebContentsImpl* web_contents = GetWebContents(); | 373 WebContentsImpl* web_contents = GetWebContents(); |
| 370 if (!web_contents) | 374 if (!web_contents) |
| 371 return Response::InternalError("Could not connect to view"); | 375 return Response::InternalError("Could not connect to view"); |
| 372 web_contents->GetDelegate()->RequestAppBannerFromDevTools(web_contents); | 376 web_contents->GetDelegate()->RequestAppBannerFromDevTools(web_contents); |
| 373 return Response::OK(); | 377 return Response::OK(); |
| 374 } | 378 } |
| 375 | 379 |
| 380 Response PageHandler::SetControlNavigations(bool enabled) { | |
| 381 navigation_throttle_enabled_ = enabled; | |
| 382 // We don't own the page PageNavigationThrottles so we can't delete them, but | |
| 383 // we can turn them into NOPs. | |
| 384 for (auto& pair : navigation_throttles_) { | |
| 385 pair.second->AlwaysProceed(); | |
| 386 } | |
| 387 navigation_throttles_.clear(); | |
| 388 return Response::OK(); | |
| 389 } | |
| 390 | |
| 391 Response PageHandler::ProcessNavigation(const std::string& response, | |
| 392 int navigation_id) { | |
| 393 auto it = navigation_throttles_.find(navigation_id); | |
| 394 if (it == navigation_throttles_.end()) | |
| 395 return Response::InvalidParams("Unknown navigation id"); | |
| 396 | |
| 397 if (response == kNavigationThrottleResponseProceed) { | |
| 398 it->second->Resume(); | |
| 399 return Response::OK(); | |
| 400 } else if (response == kNavigationThrottleResponseCancel) { | |
| 401 it->second->CancelDeferredNavigation(content::NavigationThrottle::CANCEL); | |
| 402 return Response::OK(); | |
| 403 } else if (response == kNavigationThrottleResponseCancelAndIgnore) { | |
| 404 it->second->CancelDeferredNavigation( | |
| 405 content::NavigationThrottle::CANCEL_AND_IGNORE); | |
| 406 return Response::OK(); | |
| 407 } | |
| 408 | |
| 409 return Response::InvalidParams("Unrecognized response"); | |
| 410 } | |
| 411 | |
| 412 std::unique_ptr<PageNavigationThrottle> PageHandler::GetThrottleForNavigation( | |
|
nasko
2016/07/13 22:37:34
nit: This should be named Create, as it doesn't Ge
alex clarke (OOO till 29th)
2016/07/14 16:06:04
Done.
| |
| 413 NavigationHandle* navigation_handle) { | |
| 414 if (!navigation_throttle_enabled_) | |
| 415 return nullptr; | |
| 416 | |
| 417 std::unique_ptr<PageNavigationThrottle> throttle(new PageNavigationThrottle( | |
| 418 weak_factory_.GetWeakPtr(), next_navigation_id_, navigation_handle)); | |
| 419 navigation_throttles_[next_navigation_id_++] = throttle.get(); | |
| 420 return throttle; | |
| 421 } | |
| 422 | |
| 423 void PageHandler::OnPageNavigationThrottleDisposed(int navigation_id) { | |
| 424 DCHECK(navigation_throttles_.find(navigation_id) != | |
| 425 navigation_throttles_.end()); | |
| 426 navigation_throttles_.erase(navigation_id); | |
| 427 } | |
| 428 | |
| 429 void PageHandler::NavigationRequested(const PageNavigationThrottle* throttle) { | |
| 430 NavigationHandle* navigation_handle = throttle->navigation_handle(); | |
| 431 client_->NavigationRequested( | |
| 432 NavigationRequestedParams::Create() | |
| 433 ->set_is_in_main_frame(navigation_handle->IsInMainFrame()) | |
| 434 ->set_is_parent_main_frame(navigation_handle->IsParentMainFrame()) | |
|
nasko
2016/07/13 22:37:34
Is this really needed? I'd rather not expose it if
alex clarke (OOO till 29th)
2016/07/14 16:06:04
Done.
| |
| 435 ->set_is_redirect(navigation_handle->WasServerRedirect()) | |
| 436 ->set_navigation_id(throttle->navigation_id()) | |
| 437 ->set_url(navigation_handle->GetURL().spec()) | |
| 438 ->set_renderer_initiated(navigation_handle->IsRendererInitiated())); | |
| 439 } | |
| 440 | |
| 376 WebContentsImpl* PageHandler::GetWebContents() { | 441 WebContentsImpl* PageHandler::GetWebContents() { |
| 377 return host_ ? | 442 return host_ ? |
| 378 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(host_)) : | 443 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(host_)) : |
| 379 nullptr; | 444 nullptr; |
| 380 } | 445 } |
| 381 | 446 |
| 382 void PageHandler::NotifyScreencastVisibility(bool visible) { | 447 void PageHandler::NotifyScreencastVisibility(bool visible) { |
| 383 if (visible) | 448 if (visible) |
| 384 capture_retry_count_ = kCaptureRetryLimit; | 449 capture_retry_count_ = kCaptureRetryLimit; |
| 385 client_->ScreencastVisibilityChanged( | 450 client_->ScreencastVisibilityChanged( |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 516 | 581 |
| 517 void PageHandler::OnColorPicked(int r, int g, int b, int a) { | 582 void PageHandler::OnColorPicked(int r, int g, int b, int a) { |
| 518 scoped_refptr<dom::RGBA> color = | 583 scoped_refptr<dom::RGBA> color = |
| 519 dom::RGBA::Create()->set_r(r)->set_g(g)->set_b(b)->set_a(a); | 584 dom::RGBA::Create()->set_r(r)->set_g(g)->set_b(b)->set_a(a); |
| 520 client_->ColorPicked(ColorPickedParams::Create()->set_color(color)); | 585 client_->ColorPicked(ColorPickedParams::Create()->set_color(color)); |
| 521 } | 586 } |
| 522 | 587 |
| 523 } // namespace page | 588 } // namespace page |
| 524 } // namespace devtools | 589 } // namespace devtools |
| 525 } // namespace content | 590 } // namespace content |
| OLD | NEW |