Chromium Code Reviews| 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/message_loop/message_loop_proxy.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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 devtools::Page::startScreencast::kName, | 119 devtools::Page::startScreencast::kName, |
| 116 base::Bind( | 120 base::Bind( |
| 117 &RendererOverridesHandler::PageStartScreencast, | 121 &RendererOverridesHandler::PageStartScreencast, |
| 118 base::Unretained(this))); | 122 base::Unretained(this))); |
| 119 RegisterCommandHandler( | 123 RegisterCommandHandler( |
| 120 devtools::Page::stopScreencast::kName, | 124 devtools::Page::stopScreencast::kName, |
| 121 base::Bind( | 125 base::Bind( |
| 122 &RendererOverridesHandler::PageStopScreencast, | 126 &RendererOverridesHandler::PageStopScreencast, |
| 123 base::Unretained(this))); | 127 base::Unretained(this))); |
| 124 RegisterCommandHandler( | 128 RegisterCommandHandler( |
| 129 devtools::Quota::queryUsageAndQuota::kName, | |
| 130 base::Bind( | |
| 131 &RendererOverridesHandler::QuotaQueryUsageAndQuota, | |
| 132 base::Unretained(this))); | |
| 133 RegisterCommandHandler( | |
| 125 devtools::Input::dispatchMouseEvent::kName, | 134 devtools::Input::dispatchMouseEvent::kName, |
| 126 base::Bind( | 135 base::Bind( |
| 127 &RendererOverridesHandler::InputDispatchMouseEvent, | 136 &RendererOverridesHandler::InputDispatchMouseEvent, |
| 128 base::Unretained(this))); | 137 base::Unretained(this))); |
| 129 RegisterCommandHandler( | 138 RegisterCommandHandler( |
| 130 devtools::Input::dispatchGestureEvent::kName, | 139 devtools::Input::dispatchGestureEvent::kName, |
| 131 base::Bind( | 140 base::Bind( |
| 132 &RendererOverridesHandler::InputDispatchGestureEvent, | 141 &RendererOverridesHandler::InputDispatchGestureEvent, |
| 133 base::Unretained(this))); | 142 base::Unretained(this))); |
| 134 } | 143 } |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 514 response->Set(devtools::Page::kParamViewport, viewport); | 523 response->Set(devtools::Page::kParamViewport, viewport); |
| 515 } | 524 } |
| 516 | 525 |
| 517 if (command) { | 526 if (command) { |
| 518 SendAsyncResponse(command->SuccessResponse(response)); | 527 SendAsyncResponse(command->SuccessResponse(response)); |
| 519 } else { | 528 } else { |
| 520 SendNotification(devtools::Page::screencastFrame::kName, response); | 529 SendNotification(devtools::Page::screencastFrame::kName, response); |
| 521 } | 530 } |
| 522 } | 531 } |
| 523 | 532 |
| 533 namespace { | |
|
pfeldman
2013/09/16 14:15:36
We already have anonymous namespace defined above.
SeRya
2013/09/17 12:44:55
Moved there.
| |
| 534 | |
| 535 typedef base::Callback<void (DevToolsProtocol::Response*)> RespondCallback; | |
| 536 | |
| 537 class QuotaTask { | |
| 538 public: | |
| 539 virtual ~QuotaTask() {} | |
| 540 | |
| 541 static void Run(scoped_ptr<QuotaTask> task) { | |
| 542 // Will be deleted when done. | |
| 543 BrowserThread::PostTask( | |
| 544 BrowserThread::IO, FROM_HERE, | |
| 545 base::Bind(&QuotaTask::Start, base::Unretained(task.release()))); | |
| 546 } | |
| 547 | |
| 548 protected: | |
| 549 QuotaTask(scoped_refptr<DevToolsProtocol::Command> command, | |
| 550 const RespondCallback& callback, | |
|
pfeldman
2013/09/16 14:15:36
poor indentation.
SeRya
2013/09/17 12:44:55
Done.
| |
| 551 RenderViewHost* render_vew_host, | |
| 552 const std::string& security_origin) | |
| 553 : command_(command), | |
| 554 callback_(callback), | |
| 555 origin_(security_origin), | |
| 556 quota_manager_(render_vew_host->GetProcess()-> | |
| 557 GetStoragePartition()->GetQuotaManager()), | |
| 558 response_loop_proxy_(base::MessageLoopProxy::current()) { | |
| 559 | |
| 560 } | |
| 561 | |
| 562 // Called on IO thread. | |
| 563 virtual void Start() = 0; | |
| 564 | |
| 565 void Done(scoped_ptr<base::DictionaryValue> responseData) { | |
| 566 base::MessageLoopProxy* loop = response_loop_proxy_.get(); | |
|
pfeldman
2013/09/16 14:15:36
You should not define methods inline.
SeRya
2013/09/17 12:44:55
Done.
| |
| 567 loop->PostTask(FROM_HERE, base::Bind( | |
| 568 &QuotaTask::RespondOnCallerThreadAndDelete, | |
| 569 this, base::Passed(&responseData))); | |
| 570 } | |
| 571 | |
| 572 scoped_refptr<quota::QuotaManager> GetQuotaManager() { | |
| 573 return quota_manager_.get(); | |
| 574 } | |
| 575 | |
| 576 std::string host() const { return net::GetHostOrSpecFromURL(origin_); } | |
| 577 const GURL& origin() const { return origin_; } | |
| 578 | |
| 579 private: | |
| 580 scoped_refptr<DevToolsProtocol::Command> command_; | |
| 581 RespondCallback callback_; | |
| 582 | |
| 583 GURL origin_; | |
| 584 scoped_refptr<quota::QuotaManager> quota_manager_; | |
| 585 | |
| 586 scoped_refptr<base::MessageLoopProxy> response_loop_proxy_; | |
| 587 | |
| 588 void RespondOnCallerThread(scoped_ptr<base::DictionaryValue> response) { | |
| 589 callback_.Run(command_->SuccessResponse(response.release()).get()); | |
| 590 } | |
| 591 | |
| 592 static void RespondOnCallerThreadAndDelete(QuotaTask* task, | |
| 593 scoped_ptr<base::DictionaryValue> responseData) { | |
| 594 task->RespondOnCallerThread(responseData.Pass()); | |
| 595 delete task; | |
| 596 } | |
| 597 }; | |
| 598 | |
| 599 class GetUsageAndQuotaTask : public QuotaTask { | |
|
pfeldman
2013/09/16 14:15:36
There is only one task. Why extracting superclass?
SeRya
2013/09/17 12:44:55
Initially I planned to implement more methods base
pfeldman
2013/09/17 15:19:45
I think it is now clear you should not split these
| |
| 600 public: | |
| 601 GetUsageAndQuotaTask(scoped_refptr<DevToolsProtocol::Command> command, | |
| 602 const RespondCallback& callback, | |
|
pfeldman
2013/09/16 14:15:36
indent
SeRya
2013/09/17 12:44:55
Done.
| |
| 603 RenderViewHost* render_vew_host, | |
| 604 const std::string& security_origin) | |
| 605 : QuotaTask(command, callback, render_vew_host, security_origin), | |
| 606 quota_(new base::DictionaryValue), | |
|
pfeldman
2013/09/16 14:15:36
indent
SeRya
2013/09/17 12:44:55
Done.
| |
| 607 usage_(new base::DictionaryValue) { | |
| 608 steps_.push_back(base::Bind("a::QuotaManager::GetAvailableSpace, | |
|
pfeldman
2013/09/16 14:15:36
You should use barrier.
SeRya
2013/09/17 12:44:55
I had a look in base/barrier_closure.h and don't s
pfeldman
2013/09/17 12:46:14
I am not sure I follow.
| |
| 609 GetQuotaManager(), | |
| 610 MakeQuotaCallback("availableSpace"))); | |
| 611 | |
| 612 steps_.push_back(base::Bind( | |
| 613 "a::QuotaManager::GetUsageAndQuotaForWebApps, | |
| 614 GetQuotaManager(), | |
| 615 origin(), | |
| 616 quota::kStorageTypeTemporary, | |
| 617 base::Bind(&GetUsageAndQuotaTask::DidGetUsageAndQuotaForWebApps, | |
| 618 base::Unretained(this), "temporaryHostQuota"))); | |
| 619 | |
| 620 steps_.push_back(base::Bind("a::QuotaManager::GetPersistentHostQuota, | |
| 621 GetQuotaManager(), | |
| 622 host(), | |
| 623 MakeQuotaCallback("persistentHostQuota"))); | |
| 624 | |
| 625 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
| 626 base::Unretained(this), | |
| 627 quota::kStorageTypeTemporary, | |
| 628 quota::QuotaClient::kFileSystem, | |
| 629 "temporaryFileSystemUsage")); | |
|
pfeldman
2013/09/16 14:15:36
You should have a dictionary per storage type and
SeRya
2013/09/17 12:44:55
I employed different approach: introduced a method
| |
| 630 | |
| 631 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
| 632 base::Unretained(this), | |
| 633 quota::kStorageTypePersistent, | |
| 634 quota::QuotaClient::kFileSystem, | |
| 635 "persistentFileSystemUsage")); | |
| 636 | |
| 637 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
| 638 base::Unretained(this), | |
| 639 quota::kStorageTypeSyncable, | |
| 640 quota::QuotaClient::kFileSystem, | |
| 641 "syncableFileSystemUsage")); | |
| 642 | |
| 643 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
| 644 base::Unretained(this), | |
| 645 quota::kStorageTypeTemporary, | |
| 646 quota::QuotaClient::kDatabase, | |
| 647 "databaseUsage")); | |
| 648 | |
| 649 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
| 650 base::Unretained(this), | |
| 651 quota::kStorageTypeTemporary, | |
| 652 quota::QuotaClient::kAppcache, | |
| 653 "appcacheUsage")); | |
| 654 | |
| 655 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
| 656 base::Unretained(this), | |
| 657 quota::kStorageTypeTemporary, | |
| 658 quota::QuotaClient::kIndexedDatabase, | |
| 659 "indexedDatabaseUsage")); | |
| 660 | |
| 661 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::Finish, | |
| 662 base::Unretained(this))); | |
| 663 } | |
| 664 | |
| 665 protected: | |
| 666 virtual void Start() OVERRIDE { | |
| 667 currentStep_ = 0; | |
| 668 readyForNextStep_ = true; | |
| 669 stepDone_ = true; | |
| 670 DoStep(); | |
| 671 } | |
| 672 | |
| 673 private: | |
| 674 scoped_ptr<base::DictionaryValue> quota_; | |
| 675 scoped_ptr<base::DictionaryValue> usage_; | |
| 676 std::vector<base::Callback<void (void)> > steps_; | |
| 677 bool readyForNextStep_; | |
| 678 bool stepDone_; | |
| 679 int currentStep_; | |
| 680 | |
| 681 void GetHostUsage(quota::StorageType type, | |
| 682 quota::QuotaClient::ID client_id, | |
| 683 const char* name) { | |
| 684 quota::UsageCallback callback = | |
| 685 base::Bind(&GetUsageAndQuotaTask::DidGetUsage, | |
| 686 base::Unretained(this), name); | |
| 687 GetQuotaManager()->GetHostUsage(host(), type, client_id, callback); | |
| 688 } | |
| 689 | |
| 690 void Finish() { | |
| 691 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); | |
| 692 result->Set("quota", quota_.release()); | |
| 693 result->Set("usage", usage_.release()); | |
| 694 Done(result.Pass()); | |
| 695 } | |
| 696 | |
| 697 void DoStep() { | |
| 698 DCHECK(readyForNextStep_); | |
|
pfeldman
2013/09/16 14:15:36
Please add DCHECK that it is currently on IO
SeRya
2013/09/17 12:44:55
Done.
| |
| 699 DCHECK(stepDone_); | |
| 700 readyForNextStep_ = false; | |
| 701 stepDone_ = false; | |
| 702 int startStep = currentStep_; | |
| 703 | |
| 704 steps_[currentStep_].Run(); | |
| 705 | |
| 706 readyForNextStep_ = true; | |
| 707 if (stepDone_) { | |
| 708 DCHECK(currentStep_ == startStep + 1); | |
| 709 DoStep(); | |
| 710 } | |
| 711 } | |
| 712 | |
| 713 void StepDone() { | |
| 714 DCHECK(!stepDone_); | |
| 715 currentStep_++; | |
| 716 stepDone_ = true; | |
| 717 if (readyForNextStep_) | |
| 718 DoStep(); | |
| 719 } | |
| 720 | |
| 721 void DidGetQuota(const char* name, | |
| 722 quota::QuotaStatusCode status, | |
| 723 int64 space) { | |
| 724 if (status == quota::kQuotaStatusOk) | |
| 725 quota_->SetDouble(name, space); | |
| 726 StepDone(); | |
| 727 } | |
| 728 | |
| 729 quota::QuotaCallback MakeQuotaCallback(const char* name) { | |
| 730 return base::Bind(&GetUsageAndQuotaTask::DidGetQuota, | |
| 731 base::Unretained(this), name); | |
| 732 } | |
| 733 | |
| 734 void DidGetUsage(const char* name, int64 usage_value) { | |
| 735 usage_->SetDouble(name, usage_value); | |
| 736 StepDone(); | |
| 737 } | |
| 738 | |
| 739 void DidGetUsageAndQuotaForWebApps(const char* name, | |
| 740 quota::QuotaStatusCode status, int64 usage, int64 quota) { | |
| 741 if (status == quota::kQuotaStatusOk) | |
| 742 quota_->SetDouble(name, quota); | |
| 743 StepDone(); | |
| 744 } | |
| 745 }; | |
| 746 | |
| 747 } // namespace | |
| 748 | |
| 749 scoped_refptr<DevToolsProtocol::Response> | |
| 750 RendererOverridesHandler::QuotaQueryUsageAndQuota( | |
| 751 scoped_refptr<DevToolsProtocol::Command> command) { | |
| 752 base::DictionaryValue* params = command->params(); | |
| 753 std::string security_origin; | |
| 754 if (!params || !params->GetString( | |
| 755 devtools::Quota::queryUsageAndQuota::kParamSecurityOrigin, | |
| 756 &security_origin)) { | |
| 757 return command->InvalidParamResponse( | |
| 758 devtools::Quota::queryUsageAndQuota::kParamSecurityOrigin); | |
| 759 } | |
| 760 | |
| 761 RespondCallback callback = base::Bind( | |
| 762 &RendererOverridesHandler::AsyncTaskDone, weak_factory_.GetWeakPtr()); | |
| 763 QuotaTask::Run(scoped_ptr<QuotaTask>(new GetUsageAndQuotaTask(command, | |
| 764 callback, agent_->GetRenderViewHost(), security_origin))); | |
| 765 return command->AsyncResponsePromise(); | |
| 766 } | |
| 767 | |
| 768 void RendererOverridesHandler::AsyncTaskDone( | |
| 769 DevToolsProtocol::Response* response) { | |
| 770 SendRawMessage(response->Serialize()); | |
| 771 } | |
| 524 | 772 |
| 525 // Input agent handlers ------------------------------------------------------ | 773 // Input agent handlers ------------------------------------------------------ |
| 526 | 774 |
| 527 scoped_refptr<DevToolsProtocol::Response> | 775 scoped_refptr<DevToolsProtocol::Response> |
| 528 RendererOverridesHandler::InputDispatchMouseEvent( | 776 RendererOverridesHandler::InputDispatchMouseEvent( |
| 529 scoped_refptr<DevToolsProtocol::Command> command) { | 777 scoped_refptr<DevToolsProtocol::Command> command) { |
| 530 base::DictionaryValue* params = command->params(); | 778 base::DictionaryValue* params = command->params(); |
| 531 if (!params) | 779 if (!params) |
| 532 return NULL; | 780 return NULL; |
| 533 | 781 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 660 return NULL; | 908 return NULL; |
| 661 } | 909 } |
| 662 event.data.pinchUpdate.scale = static_cast<float>(scale); | 910 event.data.pinchUpdate.scale = static_cast<float>(scale); |
| 663 } | 911 } |
| 664 | 912 |
| 665 host->ForwardGestureEvent(event); | 913 host->ForwardGestureEvent(event); |
| 666 return command->SuccessResponse(NULL); | 914 return command->SuccessResponse(NULL); |
| 667 } | 915 } |
| 668 | 916 |
| 669 } // namespace content | 917 } // namespace content |
| OLD | NEW |