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 30 matching lines...) Expand all Loading... |
41 | 41 |
42 namespace { | 42 namespace { |
43 | 43 |
44 static const char kPng[] = "png"; | 44 static const char kPng[] = "png"; |
45 static const char kJpeg[] = "jpeg"; | 45 static const char kJpeg[] = "jpeg"; |
46 static int kDefaultScreenshotQuality = 80; | 46 static int kDefaultScreenshotQuality = 80; |
47 static int kFrameRateThresholdMs = 100; | 47 static int kFrameRateThresholdMs = 100; |
48 static int kCaptureRetryLimit = 2; | 48 static int kCaptureRetryLimit = 2; |
49 | 49 |
50 void QueryUsageAndQuotaCompletedOnIOThread( | 50 void QueryUsageAndQuotaCompletedOnIOThread( |
51 const UsageAndQuotaQuery::Callback& callback, | 51 const UsageAndQuotaQuery::Callback& callback, |
52 scoped_ptr<QueryUsageAndQuotaResponse> response) { | 52 scoped_refptr<QueryUsageAndQuotaResponse> response) { |
53 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 53 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
54 base::Bind(callback, base::Passed(&response))); | 54 base::Bind(callback, response)); |
55 } | 55 } |
56 | 56 |
57 void QueryUsageAndQuotaOnIOThread( | 57 void QueryUsageAndQuotaOnIOThread( |
58 scoped_refptr<storage::QuotaManager> quota_manager, | 58 scoped_refptr<storage::QuotaManager> quota_manager, |
59 const GURL& security_origin, | 59 const GURL& security_origin, |
60 const UsageAndQuotaQuery::Callback& callback) { | 60 const UsageAndQuotaQuery::Callback& callback) { |
61 new UsageAndQuotaQuery( | 61 new UsageAndQuotaQuery( |
62 quota_manager, | 62 quota_manager, |
63 security_origin, | 63 security_origin, |
64 base::Bind(&QueryUsageAndQuotaCompletedOnIOThread, | 64 base::Bind(&QueryUsageAndQuotaCompletedOnIOThread, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 | 116 |
117 void PageHandler::OnVisibilityChanged(bool visible) { | 117 void PageHandler::OnVisibilityChanged(bool visible) { |
118 if (!screencast_enabled_) | 118 if (!screencast_enabled_) |
119 return; | 119 return; |
120 NotifyScreencastVisibility(visible); | 120 NotifyScreencastVisibility(visible); |
121 } | 121 } |
122 | 122 |
123 void PageHandler::DidAttachInterstitialPage() { | 123 void PageHandler::DidAttachInterstitialPage() { |
124 if (!enabled_) | 124 if (!enabled_) |
125 return; | 125 return; |
126 InterstitialShownParams params; | 126 client_->InterstitialShown(InterstitialShownParams::Create()); |
127 client_->InterstitialShown(params); | |
128 } | 127 } |
129 | 128 |
130 void PageHandler::DidDetachInterstitialPage() { | 129 void PageHandler::DidDetachInterstitialPage() { |
131 if (!enabled_) | 130 if (!enabled_) |
132 return; | 131 return; |
133 InterstitialHiddenParams params; | 132 client_->InterstitialHidden(InterstitialHiddenParams::Create()); |
134 client_->InterstitialHidden(params); | |
135 } | 133 } |
136 | 134 |
137 Response PageHandler::Enable() { | 135 Response PageHandler::Enable() { |
138 enabled_ = true; | 136 enabled_ = true; |
139 return Response::FallThrough(); | 137 return Response::FallThrough(); |
140 } | 138 } |
141 | 139 |
142 Response PageHandler::Disable() { | 140 Response PageHandler::Disable() { |
143 enabled_ = false; | 141 enabled_ = false; |
144 touch_emulation_enabled_ = false; | 142 touch_emulation_enabled_ = false; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 | 175 |
178 WebContents* web_contents = WebContents::FromRenderViewHost(host_); | 176 WebContents* web_contents = WebContents::FromRenderViewHost(host_); |
179 if (!web_contents) | 177 if (!web_contents) |
180 return Response::InternalError("No WebContents to navigate"); | 178 return Response::InternalError("No WebContents to navigate"); |
181 | 179 |
182 web_contents->GetController() | 180 web_contents->GetController() |
183 .LoadURL(gurl, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); | 181 .LoadURL(gurl, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); |
184 return Response::FallThrough(); | 182 return Response::FallThrough(); |
185 } | 183 } |
186 | 184 |
187 Response PageHandler::GetNavigationHistory( | 185 Response PageHandler::GetNavigationHistory(int* current_index, |
188 int* current_index, | 186 NavigationEntries* entries) { |
189 std::vector<NavigationEntry>* entries) { | |
190 if (!host_) | 187 if (!host_) |
191 return Response::InternalError("Could not connect to view"); | 188 return Response::InternalError("Could not connect to view"); |
192 | 189 |
193 WebContents* web_contents = WebContents::FromRenderViewHost(host_); | 190 WebContents* web_contents = WebContents::FromRenderViewHost(host_); |
194 if (!web_contents) | 191 if (!web_contents) |
195 return Response::InternalError("No WebContents to navigate"); | 192 return Response::InternalError("No WebContents to navigate"); |
196 | 193 |
197 NavigationController& controller = web_contents->GetController(); | 194 NavigationController& controller = web_contents->GetController(); |
198 *current_index = controller.GetCurrentEntryIndex(); | 195 *current_index = controller.GetCurrentEntryIndex(); |
199 for (int i = 0; i != controller.GetEntryCount(); ++i) { | 196 for (int i = 0; i != controller.GetEntryCount(); ++i) { |
200 NavigationEntry entry; | 197 entries->push_back(NavigationEntry::Create() |
201 entry.set_id(controller.GetEntryAtIndex(i)->GetUniqueID()); | 198 ->set_id(controller.GetEntryAtIndex(i)->GetUniqueID()) |
202 entry.set_url(controller.GetEntryAtIndex(i)->GetURL().spec()); | 199 ->set_url(controller.GetEntryAtIndex(i)->GetURL().spec()) |
203 entry.set_title( | 200 ->set_title( |
204 base::UTF16ToUTF8(controller.GetEntryAtIndex(i)->GetTitle())); | 201 base::UTF16ToUTF8(controller.GetEntryAtIndex(i)->GetTitle()))); |
205 entries->push_back(entry); | |
206 } | 202 } |
207 return Response::OK(); | 203 return Response::OK(); |
208 } | 204 } |
209 | 205 |
210 Response PageHandler::NavigateToHistoryEntry(int entry_id) { | 206 Response PageHandler::NavigateToHistoryEntry(int entry_id) { |
211 if (!host_) | 207 if (!host_) |
212 return Response::InternalError("Could not connect to view"); | 208 return Response::InternalError("Could not connect to view"); |
213 | 209 |
214 WebContents* web_contents = WebContents::FromRenderViewHost(host_); | 210 WebContents* web_contents = WebContents::FromRenderViewHost(host_); |
215 if (!web_contents) | 211 if (!web_contents) |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 host_->SetTouchEventEmulationEnabled(enabled); | 404 host_->SetTouchEventEmulationEnabled(enabled); |
409 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | 405 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( |
410 WebContents::FromRenderViewHost(host_)); | 406 WebContents::FromRenderViewHost(host_)); |
411 if (web_contents) | 407 if (web_contents) |
412 web_contents->SetForceDisableOverscrollContent(enabled); | 408 web_contents->SetForceDisableOverscrollContent(enabled); |
413 } | 409 } |
414 | 410 |
415 void PageHandler::NotifyScreencastVisibility(bool visible) { | 411 void PageHandler::NotifyScreencastVisibility(bool visible) { |
416 if (visible) | 412 if (visible) |
417 capture_retry_count_ = kCaptureRetryLimit; | 413 capture_retry_count_ = kCaptureRetryLimit; |
418 ScreencastVisibilityChangedParams params; | 414 client_->ScreencastVisibilityChanged( |
419 params.set_visible(visible); | 415 ScreencastVisibilityChangedParams::Create()->set_visible(visible)); |
420 client_->ScreencastVisibilityChanged(params); | |
421 } | 416 } |
422 | 417 |
423 void PageHandler::InnerSwapCompositorFrame() { | 418 void PageHandler::InnerSwapCompositorFrame() { |
424 if ((base::TimeTicks::Now() - last_frame_time_).InMilliseconds() < | 419 if ((base::TimeTicks::Now() - last_frame_time_).InMilliseconds() < |
425 kFrameRateThresholdMs) { | 420 kFrameRateThresholdMs) { |
426 return; | 421 return; |
427 } | 422 } |
428 | 423 |
429 if (!host_ || !host_->GetView()) | 424 if (!host_ || !host_->GetView()) |
430 return; | 425 return; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 } | 511 } |
517 | 512 |
518 if (!encoded) | 513 if (!encoded) |
519 return; | 514 return; |
520 | 515 |
521 std::string base_64_data; | 516 std::string base_64_data; |
522 base::Base64Encode( | 517 base::Base64Encode( |
523 base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()), | 518 base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()), |
524 &base_64_data); | 519 &base_64_data); |
525 | 520 |
526 ScreencastFrameMetadata param_metadata; | |
527 // Consider metadata empty in case it has no device scale factor. | 521 // Consider metadata empty in case it has no device scale factor. |
528 if (metadata.device_scale_factor != 0 && host_) { | 522 if (metadata.device_scale_factor == 0 || !host_) |
529 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>( | 523 return; |
530 host_->GetView()); | |
531 if (!view) | |
532 return; | |
533 | 524 |
534 gfx::SizeF viewport_size_dip = gfx::ScaleSize( | 525 RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>( |
535 metadata.scrollable_viewport_size, metadata.page_scale_factor); | 526 host_->GetView()); |
536 gfx::SizeF screen_size_dip = gfx::ScaleSize( | 527 if (!view) |
537 view->GetPhysicalBackingSize(), 1 / metadata.device_scale_factor); | 528 return; |
538 | 529 |
539 param_metadata.set_device_scale_factor(metadata.device_scale_factor); | 530 gfx::SizeF viewport_size_dip = gfx::ScaleSize( |
540 param_metadata.set_page_scale_factor(metadata.page_scale_factor); | 531 metadata.scrollable_viewport_size, metadata.page_scale_factor); |
541 param_metadata.set_page_scale_factor_min(metadata.min_page_scale_factor); | 532 gfx::SizeF screen_size_dip = gfx::ScaleSize( |
542 param_metadata.set_page_scale_factor_max(metadata.max_page_scale_factor); | 533 view->GetPhysicalBackingSize(), 1 / metadata.device_scale_factor); |
543 param_metadata.set_offset_top( | 534 scoped_refptr<ScreencastFrameMetadata> param_metadata = |
544 metadata.location_bar_content_translation.y()); | 535 ScreencastFrameMetadata::Create() |
545 param_metadata.set_offset_bottom(screen_size_dip.height() - | 536 ->set_device_scale_factor(metadata.device_scale_factor) |
546 metadata.location_bar_content_translation.y() - | 537 ->set_page_scale_factor(metadata.page_scale_factor) |
547 viewport_size_dip.height()); | 538 ->set_page_scale_factor_min(metadata.min_page_scale_factor) |
548 param_metadata.set_device_width(screen_size_dip.width()); | 539 ->set_page_scale_factor_max(metadata.max_page_scale_factor) |
549 param_metadata.set_device_height(screen_size_dip.height()); | 540 ->set_offset_top(metadata.location_bar_content_translation.y()) |
550 param_metadata.set_scroll_offset_x(metadata.root_scroll_offset.x()); | 541 ->set_offset_bottom(screen_size_dip.height() - |
551 param_metadata.set_scroll_offset_y(metadata.root_scroll_offset.y()); | 542 metadata.location_bar_content_translation.y() - |
552 | 543 viewport_size_dip.height()) |
553 devtools::dom::Rect viewport; | 544 ->set_device_width(screen_size_dip.width()) |
554 viewport.set_x(metadata.root_scroll_offset.x()); | 545 ->set_device_height(screen_size_dip.height()) |
555 viewport.set_y(metadata.root_scroll_offset.y()); | 546 ->set_scroll_offset_x(metadata.root_scroll_offset.x()) |
556 viewport.set_width(metadata.scrollable_viewport_size.width()); | 547 ->set_scroll_offset_y(metadata.root_scroll_offset.y()) |
557 viewport.set_height(metadata.scrollable_viewport_size.height()); | 548 ->set_viewport(dom::Rect::Create() |
558 param_metadata.set_viewport(viewport); | 549 ->set_x(metadata.root_scroll_offset.x()) |
559 } | 550 ->set_y(metadata.root_scroll_offset.y()) |
560 | 551 ->set_width(metadata.scrollable_viewport_size.width()) |
561 ScreencastFrameParams params; | 552 ->set_height(metadata.scrollable_viewport_size.height())); |
562 params.set_data(base_64_data); | 553 client_->ScreencastFrame(ScreencastFrameParams::Create() |
563 params.set_metadata(param_metadata); | 554 ->set_data(base_64_data) |
564 client_->ScreencastFrame(params); | 555 ->set_metadata(param_metadata)); |
565 } | 556 } |
566 | 557 |
567 void PageHandler::ScreenshotCaptured( | 558 void PageHandler::ScreenshotCaptured( |
568 scoped_refptr<DevToolsProtocol::Command> command, | 559 scoped_refptr<DevToolsProtocol::Command> command, |
569 const unsigned char* png_data, | 560 const unsigned char* png_data, |
570 size_t png_size) { | 561 size_t png_size) { |
571 if (!png_data || !png_size) { | 562 if (!png_data || !png_size) { |
572 client_->SendInternalErrorResponse(command, | 563 client_->SendInternalErrorResponse(command, |
573 "Unable to capture screenshot"); | 564 "Unable to capture screenshot"); |
574 return; | 565 return; |
575 } | 566 } |
576 | 567 |
577 std::string base_64_data; | 568 std::string base_64_data; |
578 base::Base64Encode( | 569 base::Base64Encode( |
579 base::StringPiece(reinterpret_cast<const char*>(png_data), png_size), | 570 base::StringPiece(reinterpret_cast<const char*>(png_data), png_size), |
580 &base_64_data); | 571 &base_64_data); |
581 | 572 |
582 CaptureScreenshotResponse response; | 573 client_->SendCaptureScreenshotResponse(command, |
583 response.set_data(base_64_data); | 574 CaptureScreenshotResponse::Create()->set_data(base_64_data)); |
584 client_->SendCaptureScreenshotResponse(command, response); | |
585 } | 575 } |
586 | 576 |
587 void PageHandler::OnColorPicked(int r, int g, int b, int a) { | 577 void PageHandler::OnColorPicked(int r, int g, int b, int a) { |
588 dom::RGBA color; | 578 scoped_refptr<dom::RGBA> color = |
589 color.set_r(r); | 579 dom::RGBA::Create()->set_r(r)->set_g(g)->set_b(b)->set_a(a); |
590 color.set_g(g); | 580 client_->ColorPicked(ColorPickedParams::Create()->set_color(color)); |
591 color.set_b(b); | |
592 color.set_a(a); | |
593 ColorPickedParams params; | |
594 params.set_color(color); | |
595 client_->ColorPicked(params); | |
596 } | 581 } |
597 | 582 |
598 void PageHandler::QueryUsageAndQuotaCompleted( | 583 void PageHandler::QueryUsageAndQuotaCompleted( |
599 scoped_refptr<DevToolsProtocol::Command> command, | 584 scoped_refptr<DevToolsProtocol::Command> command, |
600 scoped_ptr<QueryUsageAndQuotaResponse> response_data) { | 585 scoped_refptr<QueryUsageAndQuotaResponse> response_data) { |
601 client_->SendQueryUsageAndQuotaResponse(command, *response_data); | 586 client_->SendQueryUsageAndQuotaResponse(command, response_data); |
602 } | 587 } |
603 | 588 |
604 } // namespace page | 589 } // namespace page |
605 } // namespace devtools | 590 } // namespace devtools |
606 } // namespace content | 591 } // namespace content |
OLD | NEW |