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/barrier_closure.h" |
9 #include "base/base64.h" | 10 #include "base/base64.h" |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.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/dip_util.h" | 19 #include "content/browser/renderer_host/dip_util.h" |
19 #include "content/browser/renderer_host/render_view_host_delegate.h" | 20 #include "content/browser/renderer_host/render_view_host_delegate.h" |
20 #include "content/browser/renderer_host/render_view_host_impl.h" | 21 #include "content/browser/renderer_host/render_view_host_impl.h" |
21 #include "content/common/view_messages.h" | 22 #include "content/common/view_messages.h" |
22 #include "content/port/browser/render_widget_host_view_port.h" | 23 #include "content/port/browser/render_widget_host_view_port.h" |
23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/devtools_agent_host.h" | 25 #include "content/public/browser/devtools_agent_host.h" |
25 #include "content/public/browser/javascript_dialog_manager.h" | 26 #include "content/public/browser/javascript_dialog_manager.h" |
26 #include "content/public/browser/navigation_controller.h" | 27 #include "content/public/browser/navigation_controller.h" |
27 #include "content/public/browser/navigation_entry.h" | 28 #include "content/public/browser/navigation_entry.h" |
28 #include "content/public/browser/render_process_host.h" | 29 #include "content/public/browser/render_process_host.h" |
29 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
30 #include "content/public/browser/render_widget_host_view.h" | 31 #include "content/public/browser/render_widget_host_view.h" |
| 32 #include "content/public/browser/storage_partition.h" |
31 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
32 #include "content/public/browser/web_contents_delegate.h" | 34 #include "content/public/browser/web_contents_delegate.h" |
33 #include "content/public/common/page_transition_types.h" | 35 #include "content/public/common/page_transition_types.h" |
34 #include "content/public/common/referrer.h" | 36 #include "content/public/common/referrer.h" |
35 #include "ipc/ipc_sender.h" | 37 #include "ipc/ipc_sender.h" |
| 38 #include "net/base/net_util.h" |
36 #include "third_party/WebKit/public/web/WebInputEvent.h" | 39 #include "third_party/WebKit/public/web/WebInputEvent.h" |
37 #include "ui/gfx/codec/jpeg_codec.h" | 40 #include "ui/gfx/codec/jpeg_codec.h" |
38 #include "ui/gfx/codec/png_codec.h" | 41 #include "ui/gfx/codec/png_codec.h" |
39 #include "ui/gfx/size_conversions.h" | 42 #include "ui/gfx/size_conversions.h" |
40 #include "ui/snapshot/snapshot.h" | 43 #include "ui/snapshot/snapshot.h" |
41 #include "url/gurl.h" | 44 #include "url/gurl.h" |
| 45 #include "webkit/browser/quota/quota_manager.h" |
42 | 46 |
43 using WebKit::WebGestureEvent; | 47 using WebKit::WebGestureEvent; |
44 using WebKit::WebInputEvent; | 48 using WebKit::WebInputEvent; |
45 using WebKit::WebMouseEvent; | 49 using WebKit::WebMouseEvent; |
46 | 50 |
47 namespace content { | 51 namespace content { |
48 | 52 |
49 namespace { | 53 namespace { |
50 | 54 |
51 static const char kPng[] = "png"; | 55 static const char kPng[] = "png"; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 devtools::Page::startScreencast::kName, | 124 devtools::Page::startScreencast::kName, |
121 base::Bind( | 125 base::Bind( |
122 &RendererOverridesHandler::PageStartScreencast, | 126 &RendererOverridesHandler::PageStartScreencast, |
123 base::Unretained(this))); | 127 base::Unretained(this))); |
124 RegisterCommandHandler( | 128 RegisterCommandHandler( |
125 devtools::Page::stopScreencast::kName, | 129 devtools::Page::stopScreencast::kName, |
126 base::Bind( | 130 base::Bind( |
127 &RendererOverridesHandler::PageStopScreencast, | 131 &RendererOverridesHandler::PageStopScreencast, |
128 base::Unretained(this))); | 132 base::Unretained(this))); |
129 RegisterCommandHandler( | 133 RegisterCommandHandler( |
| 134 devtools::Page::queryUsageAndQuota::kName, |
| 135 base::Bind( |
| 136 &RendererOverridesHandler::PageQueryUsageAndQuota, |
| 137 base::Unretained(this))); |
| 138 RegisterCommandHandler( |
130 devtools::Input::dispatchMouseEvent::kName, | 139 devtools::Input::dispatchMouseEvent::kName, |
131 base::Bind( | 140 base::Bind( |
132 &RendererOverridesHandler::InputDispatchMouseEvent, | 141 &RendererOverridesHandler::InputDispatchMouseEvent, |
133 base::Unretained(this))); | 142 base::Unretained(this))); |
134 RegisterCommandHandler( | 143 RegisterCommandHandler( |
135 devtools::Input::dispatchGestureEvent::kName, | 144 devtools::Input::dispatchGestureEvent::kName, |
136 base::Bind( | 145 base::Bind( |
137 &RendererOverridesHandler::InputDispatchGestureEvent, | 146 &RendererOverridesHandler::InputDispatchGestureEvent, |
138 base::Unretained(this))); | 147 base::Unretained(this))); |
139 } | 148 } |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 response->Set(devtools::Page::kParamViewport, viewport); | 561 response->Set(devtools::Page::kParamViewport, viewport); |
553 } | 562 } |
554 | 563 |
555 if (command) { | 564 if (command) { |
556 SendAsyncResponse(command->SuccessResponse(response)); | 565 SendAsyncResponse(command->SuccessResponse(response)); |
557 } else { | 566 } else { |
558 SendNotification(devtools::Page::screencastFrame::kName, response); | 567 SendNotification(devtools::Page::screencastFrame::kName, response); |
559 } | 568 } |
560 } | 569 } |
561 | 570 |
| 571 // Quota and Usage ------------------------------------------ |
| 572 |
| 573 namespace { |
| 574 |
| 575 typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)> |
| 576 ResponseCallback; |
| 577 |
| 578 void QueryUsageAndQuotaCompletedOnIOThread( |
| 579 scoped_ptr<base::DictionaryValue> quota, |
| 580 scoped_ptr<base::DictionaryValue> usage, |
| 581 ResponseCallback callback) { |
| 582 |
| 583 scoped_ptr<base::DictionaryValue> response_data(new base::DictionaryValue); |
| 584 response_data->Set(devtools::Page::queryUsageAndQuota::kResponseQuota, |
| 585 quota.release()); |
| 586 response_data->Set(devtools::Page::queryUsageAndQuota::kResponseUsage, |
| 587 usage.release()); |
| 588 |
| 589 BrowserThread::PostTask( |
| 590 BrowserThread::UI, FROM_HERE, |
| 591 base::Bind(callback, base::Passed(&response_data))); |
| 592 } |
| 593 |
| 594 void DidGetHostUsage( |
| 595 base::ListValue* list, |
| 596 const std::string& client_id, |
| 597 const base::Closure& barrier, |
| 598 int64 value) { |
| 599 base::DictionaryValue* usage_item = new base::DictionaryValue; |
| 600 usage_item->SetString(devtools::Page::UsageItem::kItemID, client_id); |
| 601 usage_item->SetDouble(devtools::Page::UsageItem::kItemValue, value); |
| 602 list->Append(usage_item); |
| 603 barrier.Run(); |
| 604 } |
| 605 |
| 606 void DidGetQuotaValue( |
| 607 base::DictionaryValue* dictionary, |
| 608 const std::string& item_name, |
| 609 const base::Closure& barrier, |
| 610 quota::QuotaStatusCode status, |
| 611 int64 value) { |
| 612 if (status == quota::kQuotaStatusOk) |
| 613 dictionary->SetDouble(item_name, value); |
| 614 barrier.Run(); |
| 615 } |
| 616 |
| 617 void DidGetUsageAndQuotaForWebApps( |
| 618 base::DictionaryValue* quota, |
| 619 const std::string& item_name, |
| 620 const base::Closure& barrier, |
| 621 quota::QuotaStatusCode status, |
| 622 int64 used_bytes, |
| 623 int64 quota_in_bytes) { |
| 624 if (status == quota::kQuotaStatusOk) |
| 625 quota->SetDouble(item_name, quota_in_bytes); |
| 626 barrier.Run(); |
| 627 } |
| 628 |
| 629 std::string GetStorageTypeName(quota::StorageType type) { |
| 630 switch (type) { |
| 631 case quota::kStorageTypeTemporary: |
| 632 return devtools::Page::Usage::kItemTemporary; |
| 633 case quota::kStorageTypePersistent: |
| 634 return devtools::Page::Usage::kItemPersistent; |
| 635 case quota::kStorageTypeSyncable: |
| 636 return devtools::Page::Usage::kItemSyncable; |
| 637 default: |
| 638 NOTREACHED(); |
| 639 return ""; |
| 640 } |
| 641 } |
| 642 |
| 643 std::string GetQuotaClientName(quota::QuotaClient::ID id) { |
| 644 switch (id) { |
| 645 case quota::QuotaClient::kFileSystem: |
| 646 return devtools::Page::UsageItem::ID::kFilesystem; |
| 647 case quota::QuotaClient::kDatabase: |
| 648 return devtools::Page::UsageItem::ID::kDatabase; |
| 649 case quota::QuotaClient::kAppcache: |
| 650 return devtools::Page::UsageItem::ID::kAppcache; |
| 651 case quota::QuotaClient::kIndexedDatabase: |
| 652 return devtools::Page::UsageItem::ID::kIndexedDatabase; |
| 653 default: |
| 654 NOTREACHED(); |
| 655 return ""; |
| 656 } |
| 657 } |
| 658 |
| 659 void QueryUsageAndQuotaOnIOThread( |
| 660 scoped_refptr<quota::QuotaManager> quota_manager, |
| 661 const GURL& security_origin, |
| 662 const ResponseCallback& callback) { |
| 663 scoped_ptr<base::DictionaryValue> quota(new base::DictionaryValue); |
| 664 scoped_ptr<base::DictionaryValue> usage(new base::DictionaryValue); |
| 665 |
| 666 static quota::QuotaClient::ID kQuotaClients[] = { |
| 667 quota::QuotaClient::kFileSystem, |
| 668 quota::QuotaClient::kDatabase, |
| 669 quota::QuotaClient::kAppcache, |
| 670 quota::QuotaClient::kIndexedDatabase |
| 671 }; |
| 672 |
| 673 static const size_t kStorageTypeCount = quota::kStorageTypeUnknown; |
| 674 |
| 675 base::ListValue* storage_type_lists[kStorageTypeCount]; |
| 676 |
| 677 for (size_t i = 0; i != kStorageTypeCount; i++) { |
| 678 const quota::StorageType type = static_cast<quota::StorageType>(i); |
| 679 storage_type_lists[i] = new base::ListValue; |
| 680 usage->Set(GetStorageTypeName(type), storage_type_lists[i]); |
| 681 } |
| 682 |
| 683 COMPILE_ASSERT(kStorageTypeCount == arraysize(storage_type_lists), |
| 684 inconsistent_storage_type_list); |
| 685 |
| 686 int kExpectedResults = 2 + arraysize(kQuotaClients) * kStorageTypeCount; |
| 687 |
| 688 base::DictionaryValue* quota_raw_ptr = quota.get(); |
| 689 |
| 690 // Takes owneship on usage and quota. |
| 691 base::Closure barrier = BarrierClosure( |
| 692 kExpectedResults, |
| 693 base::Bind(&QueryUsageAndQuotaCompletedOnIOThread, |
| 694 base::Passed("a), |
| 695 base::Passed(&usage), |
| 696 callback)); |
| 697 std::string host = net::GetHostOrSpecFromURL(security_origin); |
| 698 |
| 699 quota_manager->GetUsageAndQuotaForWebApps( |
| 700 security_origin, |
| 701 quota::kStorageTypeTemporary, |
| 702 base::Bind(&DidGetUsageAndQuotaForWebApps, quota_raw_ptr, |
| 703 std::string(devtools::Page::Quota::kItemTemporary), barrier)); |
| 704 |
| 705 quota_manager->GetPersistentHostQuota( |
| 706 host, |
| 707 base::Bind(&DidGetQuotaValue, quota_raw_ptr, |
| 708 std::string(devtools::Page::Quota::kItemPersistent), |
| 709 barrier)); |
| 710 |
| 711 for (size_t i = 0; i != arraysize(kQuotaClients); i++) { |
| 712 for (size_t j = 0; j != kStorageTypeCount; j++) { |
| 713 const quota::StorageType type = static_cast<quota::StorageType>(j); |
| 714 if (!quota_manager->IsTrackingHostUsage(type, |
| 715 kQuotaClients[i])) { |
| 716 barrier.Run(); |
| 717 continue; |
| 718 } |
| 719 quota_manager->GetHostUsage(host, type, |
| 720 kQuotaClients[i], |
| 721 base::Bind(&DidGetHostUsage, storage_type_lists[j], |
| 722 GetQuotaClientName(kQuotaClients[i]), |
| 723 barrier)); |
| 724 } |
| 725 } |
| 726 } |
| 727 |
| 728 } // namespace |
| 729 |
| 730 scoped_refptr<DevToolsProtocol::Response> |
| 731 RendererOverridesHandler::PageQueryUsageAndQuota( |
| 732 scoped_refptr<DevToolsProtocol::Command> command) { |
| 733 base::DictionaryValue* params = command->params(); |
| 734 std::string security_origin; |
| 735 if (!params || !params->GetString( |
| 736 devtools::Page::queryUsageAndQuota::kParamSecurityOrigin, |
| 737 &security_origin)) { |
| 738 return command->InvalidParamResponse( |
| 739 devtools::Page::queryUsageAndQuota::kParamSecurityOrigin); |
| 740 } |
| 741 |
| 742 ResponseCallback callback = base::Bind( |
| 743 &RendererOverridesHandler::PageQueryUsageAndQuotaCompleted, |
| 744 weak_factory_.GetWeakPtr(), |
| 745 command); |
| 746 |
| 747 scoped_refptr<quota::QuotaManager> quota_manager = |
| 748 agent_->GetRenderViewHost()->GetProcess()-> |
| 749 GetStoragePartition()->GetQuotaManager(); |
| 750 |
| 751 BrowserThread::PostTask( |
| 752 BrowserThread::IO, FROM_HERE, |
| 753 base::Bind( |
| 754 &QueryUsageAndQuotaOnIOThread, |
| 755 quota_manager, |
| 756 GURL(security_origin), |
| 757 callback)); |
| 758 |
| 759 return command->AsyncResponsePromise(); |
| 760 } |
| 761 |
| 762 void RendererOverridesHandler::PageQueryUsageAndQuotaCompleted( |
| 763 scoped_refptr<DevToolsProtocol::Command> command, |
| 764 scoped_ptr<base::DictionaryValue> response_data) { |
| 765 SendAsyncResponse(command->SuccessResponse(response_data.release())); |
| 766 } |
| 767 |
562 void RendererOverridesHandler::NotifyScreencastVisibility(bool visible) { | 768 void RendererOverridesHandler::NotifyScreencastVisibility(bool visible) { |
563 base::DictionaryValue* params = new base::DictionaryValue(); | 769 base::DictionaryValue* params = new base::DictionaryValue(); |
564 params->SetBoolean( | 770 params->SetBoolean( |
565 devtools::Page::screencastVisibilityChanged::kParamVisible, visible); | 771 devtools::Page::screencastVisibilityChanged::kParamVisible, visible); |
566 SendNotification( | 772 SendNotification( |
567 devtools::Page::screencastVisibilityChanged::kName, params); | 773 devtools::Page::screencastVisibilityChanged::kName, params); |
568 } | 774 } |
569 | 775 |
570 // Input agent handlers ------------------------------------------------------ | 776 // Input agent handlers ------------------------------------------------------ |
571 | 777 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 return NULL; | 911 return NULL; |
706 } | 912 } |
707 event.data.pinchUpdate.scale = static_cast<float>(scale); | 913 event.data.pinchUpdate.scale = static_cast<float>(scale); |
708 } | 914 } |
709 | 915 |
710 host->ForwardGestureEvent(event); | 916 host->ForwardGestureEvent(event); |
711 return command->SuccessResponse(NULL); | 917 return command->SuccessResponse(NULL); |
712 } | 918 } |
713 | 919 |
714 } // namespace content | 920 } // namespace content |
OLD | NEW |