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/render_process_host.h" | 28 #include "content/public/browser/render_process_host.h" |
28 #include "content/public/browser/render_view_host.h" | 29 #include "content/public/browser/render_view_host.h" |
29 #include "content/public/browser/render_widget_host_view.h" | 30 #include "content/public/browser/render_widget_host_view.h" |
31 #include "content/public/browser/storage_partition.h" | |
30 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
31 #include "content/public/browser/web_contents_delegate.h" | 33 #include "content/public/browser/web_contents_delegate.h" |
32 #include "content/public/common/page_transition_types.h" | 34 #include "content/public/common/page_transition_types.h" |
33 #include "content/public/common/referrer.h" | 35 #include "content/public/common/referrer.h" |
34 #include "ipc/ipc_sender.h" | 36 #include "ipc/ipc_sender.h" |
37 #include "net/base/net_util.h" | |
35 #include "third_party/WebKit/public/web/WebInputEvent.h" | 38 #include "third_party/WebKit/public/web/WebInputEvent.h" |
36 #include "ui/gfx/codec/jpeg_codec.h" | 39 #include "ui/gfx/codec/jpeg_codec.h" |
37 #include "ui/gfx/codec/png_codec.h" | 40 #include "ui/gfx/codec/png_codec.h" |
38 #include "ui/gfx/size_conversions.h" | 41 #include "ui/gfx/size_conversions.h" |
39 #include "ui/snapshot/snapshot.h" | 42 #include "ui/snapshot/snapshot.h" |
40 #include "url/gurl.h" | 43 #include "url/gurl.h" |
44 #include "webkit/browser/quota/quota_manager.h" | |
41 | 45 |
42 using WebKit::WebGestureEvent; | 46 using WebKit::WebGestureEvent; |
43 using WebKit::WebInputEvent; | 47 using WebKit::WebInputEvent; |
44 using WebKit::WebMouseEvent; | 48 using WebKit::WebMouseEvent; |
45 | 49 |
46 namespace content { | 50 namespace content { |
47 | 51 |
48 namespace { | 52 namespace { |
49 | 53 |
50 static const char kPng[] = "png"; | 54 static const char kPng[] = "png"; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 devtools::Page::startScreencast::kName, | 108 devtools::Page::startScreencast::kName, |
105 base::Bind( | 109 base::Bind( |
106 &RendererOverridesHandler::PageStartScreencast, | 110 &RendererOverridesHandler::PageStartScreencast, |
107 base::Unretained(this))); | 111 base::Unretained(this))); |
108 RegisterCommandHandler( | 112 RegisterCommandHandler( |
109 devtools::Page::stopScreencast::kName, | 113 devtools::Page::stopScreencast::kName, |
110 base::Bind( | 114 base::Bind( |
111 &RendererOverridesHandler::PageStopScreencast, | 115 &RendererOverridesHandler::PageStopScreencast, |
112 base::Unretained(this))); | 116 base::Unretained(this))); |
113 RegisterCommandHandler( | 117 RegisterCommandHandler( |
118 devtools::Quota::queryUsageAndQuota::kName, | |
119 base::Bind( | |
120 &RendererOverridesHandler::QuotaQueryUsageAndQuota, | |
121 base::Unretained(this))); | |
122 RegisterCommandHandler( | |
114 devtools::Input::dispatchMouseEvent::kName, | 123 devtools::Input::dispatchMouseEvent::kName, |
115 base::Bind( | 124 base::Bind( |
116 &RendererOverridesHandler::InputDispatchMouseEvent, | 125 &RendererOverridesHandler::InputDispatchMouseEvent, |
117 base::Unretained(this))); | 126 base::Unretained(this))); |
118 RegisterCommandHandler( | 127 RegisterCommandHandler( |
119 devtools::Input::dispatchGestureEvent::kName, | 128 devtools::Input::dispatchGestureEvent::kName, |
120 base::Bind( | 129 base::Bind( |
121 &RendererOverridesHandler::InputDispatchGestureEvent, | 130 &RendererOverridesHandler::InputDispatchGestureEvent, |
122 base::Unretained(this))); | 131 base::Unretained(this))); |
123 } | 132 } |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
439 response->Set(devtools::Page::kParamViewport, viewport); | 448 response->Set(devtools::Page::kParamViewport, viewport); |
440 } | 449 } |
441 | 450 |
442 if (command) { | 451 if (command) { |
443 SendAsyncResponse(command->SuccessResponse(response)); | 452 SendAsyncResponse(command->SuccessResponse(response)); |
444 } else { | 453 } else { |
445 SendNotification(devtools::Page::screencastFrame::kName, response); | 454 SendNotification(devtools::Page::screencastFrame::kName, response); |
446 } | 455 } |
447 } | 456 } |
448 | 457 |
458 namespace { | |
459 | |
460 typedef base::Callback<void (DevToolsProtocol::Response*)> RespondCallback; | |
461 | |
462 class QuotaTask { | |
463 public: | |
464 virtual ~QuotaTask() {} | |
465 | |
466 static void Run(scoped_ptr<QuotaTask> task) { | |
467 // Will be deleted when done. | |
468 BrowserThread::PostTask( | |
469 BrowserThread::IO, FROM_HERE, | |
470 base::Bind(&QuotaTask::Start, base::Unretained(task.release()))); | |
471 } | |
472 | |
473 protected: | |
474 QuotaTask(scoped_refptr<DevToolsProtocol::Command> command, | |
475 const RespondCallback& callback, | |
476 RenderViewHost* render_vew_host, | |
477 const std::string& security_origin) | |
478 : command_(command), | |
479 callback_(callback), | |
480 host_(net::GetHostOrSpecFromURL(GURL(security_origin))), | |
481 quota_manager_(render_vew_host->GetProcess()-> | |
482 GetStoragePartition()->GetQuotaManager()), | |
483 response_loop_proxy_(base::MessageLoopProxy::current()) { | |
484 | |
485 } | |
486 | |
487 // Called on IO thread. | |
488 virtual void Start() = 0; | |
489 | |
490 void Done(scoped_ptr<base::DictionaryValue> responseData) { | |
Vladislav Kaznacheev
2013/09/05 12:46:10
Why are these methods public? They are only used b
SeRya
2013/09/05 13:10:46
It is protected, not public (see line before the c
| |
491 base::MessageLoopProxy* loop = response_loop_proxy_.get(); | |
492 loop->PostTask(FROM_HERE, base::Bind( | |
493 &QuotaTask::RespondOnCallerThreadAndDelete, | |
494 this, base::Passed(&responseData))); | |
495 } | |
496 | |
497 scoped_refptr<quota::QuotaManager> GetQuotaManager() { | |
498 return quota_manager_.get(); | |
499 } | |
500 | |
501 const std::string& host() const { return host_; } | |
502 | |
503 private: | |
504 scoped_refptr<DevToolsProtocol::Command> command_; | |
505 RespondCallback callback_; | |
506 | |
507 std::string host_; | |
508 scoped_refptr<quota::QuotaManager> quota_manager_; | |
509 | |
510 scoped_refptr<base::MessageLoopProxy> response_loop_proxy_; | |
511 | |
512 void RespondOnCallerThread(scoped_ptr<base::DictionaryValue> response) { | |
513 callback_.Run(command_->SuccessResponse(response.release()).get()); | |
514 } | |
515 | |
516 static void RespondOnCallerThreadAndDelete(QuotaTask* task, | |
517 scoped_ptr<base::DictionaryValue> responseData) { | |
518 task->RespondOnCallerThread(responseData.Pass()); | |
519 delete task; | |
520 } | |
521 }; | |
522 | |
523 class GetUsageAndQuotaTask : public QuotaTask { | |
524 public: | |
525 GetUsageAndQuotaTask(scoped_refptr<DevToolsProtocol::Command> command, | |
526 const RespondCallback& callback, | |
527 RenderViewHost* render_vew_host, | |
528 const std::string& security_origin) | |
529 : QuotaTask(command, callback, render_vew_host, security_origin), | |
530 quota_(new base::DictionaryValue), | |
531 usage_(new base::DictionaryValue) { | |
532 steps_.push_back(base::Bind("a::QuotaManager::GetAvailableSpace, | |
533 GetQuotaManager(), | |
534 MakeQuotaCallback("availableSpace"))); | |
535 | |
536 steps_.push_back(base::Bind("a::QuotaManager::GetTemporaryGlobalQuota, | |
537 GetQuotaManager(), | |
538 MakeQuotaCallback("temporaryGlobalQuota"))); | |
539 | |
540 steps_.push_back(base::Bind("a::QuotaManager::GetPersistentHostQuota, | |
541 GetQuotaManager(), | |
542 host(), | |
543 MakeQuotaCallback("persistentHostQuota"))); | |
544 | |
545 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
546 base::Unretained(this), | |
547 quota::kStorageTypeTemporary, | |
548 quota::QuotaClient::kFileSystem, | |
549 "temporaryFileSystemUsage")); | |
550 | |
551 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
552 base::Unretained(this), | |
553 quota::kStorageTypePersistent, | |
554 quota::QuotaClient::kFileSystem, | |
555 "persistentFileSystemUsage")); | |
556 | |
557 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
558 base::Unretained(this), | |
559 quota::kStorageTypeSyncable, | |
560 quota::QuotaClient::kFileSystem, | |
561 "syncableFileSystemUsage")); | |
562 | |
563 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
564 base::Unretained(this), | |
565 quota::kStorageTypeTemporary, | |
566 quota::QuotaClient::kDatabase, | |
567 "databaseUsage")); | |
568 | |
569 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
570 base::Unretained(this), | |
571 quota::kStorageTypeTemporary, | |
572 quota::QuotaClient::kAppcache, | |
573 "appcacheUsage")); | |
574 | |
575 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::GetHostUsage, | |
576 base::Unretained(this), | |
577 quota::kStorageTypeTemporary, | |
578 quota::QuotaClient::kIndexedDatabase, | |
579 "indexedDatabaseUsage")); | |
580 | |
581 steps_.push_back(base::Bind(&GetUsageAndQuotaTask::Finish, | |
582 base::Unretained(this))); | |
583 } | |
584 | |
585 protected: | |
586 virtual void Start() OVERRIDE { | |
587 currentStep_ = 0; | |
588 readyForNextStep_ = true; | |
589 stepDone_ = true; | |
590 DoStep(); | |
591 } | |
592 | |
593 private: | |
594 scoped_ptr<base::DictionaryValue> quota_; | |
595 scoped_ptr<base::DictionaryValue> usage_; | |
596 std::vector<base::Callback<void (void)> > steps_; | |
597 bool readyForNextStep_; | |
598 bool stepDone_; | |
599 int currentStep_; | |
600 | |
601 void GetHostUsage(quota::StorageType type, | |
602 quota::QuotaClient::ID client_id, | |
603 const char* name) { | |
604 quota::UsageCallback callback = | |
605 base::Bind(&GetUsageAndQuotaTask::DidGetUsage, | |
606 base::Unretained(this), name); | |
607 GetQuotaManager()->GetHostUsage(host(), type, client_id, callback); | |
608 } | |
609 | |
610 void Finish() { | |
611 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); | |
612 result->Set("quota", quota_.release()); | |
613 result->Set("usage", usage_.release()); | |
614 Done(result.Pass()); | |
615 } | |
616 | |
617 void DoStep() { | |
618 DCHECK(readyForNextStep_); | |
619 DCHECK(stepDone_); | |
620 readyForNextStep_ = false; | |
621 stepDone_ = false; | |
622 int startStep = currentStep_; | |
623 | |
624 steps_[currentStep_].Run(); | |
625 | |
626 readyForNextStep_ = true; | |
627 if (stepDone_) { | |
628 DCHECK(currentStep_ == startStep + 1); | |
629 DoStep(); | |
630 } | |
631 } | |
632 | |
633 void StepDone() { | |
634 DCHECK(!stepDone_); | |
635 currentStep_++; | |
636 stepDone_ = true; | |
637 if (readyForNextStep_) | |
638 DoStep(); | |
639 } | |
640 | |
641 void DidGetQuota(const char* name, | |
642 quota::QuotaStatusCode status, | |
643 int64 space) { | |
644 if (status == quota::kQuotaStatusOk) | |
645 quota_->SetDouble(name, space); | |
646 StepDone(); | |
647 } | |
648 | |
649 quota::QuotaCallback MakeQuotaCallback(const char* name) { | |
650 return base::Bind(&GetUsageAndQuotaTask::DidGetQuota, | |
651 base::Unretained(this), name); | |
652 } | |
653 | |
654 void DidGetUsage(const char* name, int64 usage_value) { | |
655 usage_->SetDouble(name, usage_value); | |
656 StepDone(); | |
657 } | |
658 }; | |
659 | |
660 } // namespace | |
661 | |
662 scoped_refptr<DevToolsProtocol::Response> | |
663 RendererOverridesHandler::QuotaQueryUsageAndQuota( | |
664 scoped_refptr<DevToolsProtocol::Command> command) { | |
665 base::DictionaryValue* params = command->params(); | |
666 std::string security_origin; | |
667 if (!params || !params->GetString( | |
668 devtools::Quota::queryUsageAndQuota::kParamSecurityOrigin, | |
669 &security_origin)) { | |
670 return command->InvalidParamResponse( | |
671 devtools::Quota::queryUsageAndQuota::kParamSecurityOrigin); | |
672 } | |
673 | |
674 RespondCallback callback = base::Bind( | |
675 &RendererOverridesHandler::AsyncTaskDone, weak_factory_.GetWeakPtr()); | |
676 QuotaTask::Run(scoped_ptr<QuotaTask>(new GetUsageAndQuotaTask(command, | |
677 callback, agent_->GetRenderViewHost(), security_origin))); | |
678 return command->AsyncResponsePromise(); | |
679 } | |
680 | |
681 void RendererOverridesHandler::AsyncTaskDone( | |
682 DevToolsProtocol::Response* response) { | |
683 SendRawMessage(response->Serialize()); | |
684 } | |
449 | 685 |
450 // Input agent handlers ------------------------------------------------------ | 686 // Input agent handlers ------------------------------------------------------ |
451 | 687 |
452 scoped_refptr<DevToolsProtocol::Response> | 688 scoped_refptr<DevToolsProtocol::Response> |
453 RendererOverridesHandler::InputDispatchMouseEvent( | 689 RendererOverridesHandler::InputDispatchMouseEvent( |
454 scoped_refptr<DevToolsProtocol::Command> command) { | 690 scoped_refptr<DevToolsProtocol::Command> command) { |
455 base::DictionaryValue* params = command->params(); | 691 base::DictionaryValue* params = command->params(); |
456 if (!params) | 692 if (!params) |
457 return NULL; | 693 return NULL; |
458 | 694 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
585 return NULL; | 821 return NULL; |
586 } | 822 } |
587 event.data.pinchUpdate.scale = static_cast<float>(scale); | 823 event.data.pinchUpdate.scale = static_cast<float>(scale); |
588 } | 824 } |
589 | 825 |
590 host->ForwardGestureEvent(event); | 826 host->ForwardGestureEvent(event); |
591 return command->SuccessResponse(NULL); | 827 return command->SuccessResponse(NULL); |
592 } | 828 } |
593 | 829 |
594 } // namespace content | 830 } // namespace content |
OLD | NEW |