Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: content/browser/devtools/renderer_overrides_handler.cc

Issue 23240002: Backend for DevTools quota managements. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
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::Page::queryUsageAndQuota::kName,
130 base::Bind(
131 &RendererOverridesHandler::PageQueryUsageAndQuota,
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 383 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 response->Set(devtools::Page::kParamViewport, viewport); 527 response->Set(devtools::Page::kParamViewport, viewport);
519 } 528 }
520 529
521 if (command) { 530 if (command) {
522 SendAsyncResponse(command->SuccessResponse(response)); 531 SendAsyncResponse(command->SuccessResponse(response));
523 } else { 532 } else {
524 SendNotification(devtools::Page::screencastFrame::kName, response); 533 SendNotification(devtools::Page::screencastFrame::kName, response);
525 } 534 }
526 } 535 }
527 536
537 // Quota and Usage ------------------------------------------
538
539 namespace {
540
541 typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)>
542 ResponseCallback;
543
544 void QueryUsageAndQuotaCompletedOnIOThread(
545 scoped_ptr<base::DictionaryValue> quota,
546 scoped_ptr<base::ListValue> temporaryStorageUsage,
547 scoped_ptr<base::ListValue> persistentStorageUsage,
548 scoped_ptr<base::ListValue> syncableStorageUsage,
549 ResponseCallback callback) {
550
551 scoped_ptr<base::DictionaryValue> response_data(new base::DictionaryValue);
552 response_data->Set(devtools::Page::queryUsageAndQuota::kResponseQuota,
553 quota.release());
554
555 scoped_ptr<base::DictionaryValue> usage(new base::DictionaryValue);
556 usage->Set(devtools::Page::Usage::kItemTemporary,
557 temporaryStorageUsage.release());
558 usage->Set(devtools::Page::Usage::kItemPersistent,
559 persistentStorageUsage.release());
560 usage->Set(devtools::Page::Usage::kItemSyncable,
561 syncableStorageUsage.release());
562 response_data->Set(devtools::Page::queryUsageAndQuota::kResponseUsage,
563 usage.release());
564
565 BrowserThread::PostTask(
566 BrowserThread::UI, FROM_HERE,
567 base::Bind(callback, base::Passed(&response_data)));
568 }
569
570 void DidGetHostUsage(
571 base::ListValue* list,
572 const char* client_id,
573 const base::Closure& barrier,
574 int64 value) {
575 base::DictionaryValue* usage_item = new base::DictionaryValue;
576 usage_item->SetString(devtools::Page::UsageItem::kItemID, client_id);
pfeldman 2013/09/20 13:26:25 You could attach usage lists lazily and get rid of
SeRya 2013/09/20 19:39:40 Now I create in a loop.
577 usage_item->SetDouble(devtools::Page::UsageItem::kItemValue, value);
578 list->Append(usage_item);
579 barrier.Run();
580 }
581
582 void DidGetQuotaValue(
583 base::DictionaryValue* dictionary,
584 const char* item_name,
585 const base::Closure& barrier,
586 quota::QuotaStatusCode status,
587 int64 value) {
588 if (status == quota::kQuotaStatusOk)
589 dictionary->SetDouble(item_name, value);
590 barrier.Run();
591 }
592
593 void DidGetUsageAndQuotaForWebApps(
594 base::DictionaryValue* quota,
595 const char* item_name,
596 const base::Closure& barrier,
597 quota::QuotaStatusCode status,
598 int64 used_bytes,
599 int64 quota_in_bytes) {
600 if (status == quota::kQuotaStatusOk)
601 quota->SetDouble(item_name, quota_in_bytes);
602 barrier.Run();
603 }
604
605 void QueryUsageAndQuotaOnIOThread(
606 scoped_refptr<quota::QuotaManager> quota_manager,
607 const GURL& security_origin,
608 const ResponseCallback& callback) {
609 base::ListValue* temporaryStorageUsage = new base::ListValue;
pfeldman 2013/09/20 13:26:25 You should create them as scoped_ptr<>s
SeRya 2013/09/20 19:39:40 Done.
610 base::ListValue* persistentStorageUsage = new base::ListValue;
611 base::ListValue* syncableStorageUsage = new base::ListValue;
612 base::DictionaryValue* quota = new base::DictionaryValue;
613
614 static const int kExpectedResults = 8;
615
616 // Takes owneship on usage and quota.
617 base::Closure barrier = BarrierClosure(
618 kExpectedResults,
619 base::Bind(&QueryUsageAndQuotaCompletedOnIOThread,
620 base::Passed(scoped_ptr<base::DictionaryValue>(quota)),
621 base::Passed(scoped_ptr<base::ListValue>(
622 temporaryStorageUsage)),
623 base::Passed(scoped_ptr<base::ListValue>(
624 persistentStorageUsage)),
625 base::Passed(scoped_ptr<base::ListValue>(
626 syncableStorageUsage)),
627 callback));
628 std::string host = net::GetHostOrSpecFromURL(security_origin);
629
630 quota_manager->GetUsageAndQuotaForWebApps(
631 security_origin,
632 quota::kStorageTypeTemporary,
633 base::Bind(&DidGetUsageAndQuotaForWebApps, quota,
634 devtools::Page::Quota::kItemTemporary, barrier));
635
636 quota_manager->GetPersistentHostQuota(
637 host,
638 base::Bind(&DidGetQuotaValue, quota,
639 devtools::Page::Quota::kItemPersistent,
640 barrier));
641
642 quota_manager->GetHostUsage(host, quota::kStorageTypeTemporary,
643 quota::QuotaClient::kFileSystem,
644 base::Bind(&DidGetHostUsage, temporaryStorageUsage,
645 devtools::Page::UsageItem::ID::kFilesystem,
646 barrier));
647
648 quota_manager->GetHostUsage(host, quota::kStorageTypePersistent,
649 quota::QuotaClient::kFileSystem,
650 base::Bind(&DidGetHostUsage, persistentStorageUsage,
651 devtools::Page::UsageItem::ID::kFilesystem,
652 barrier));
653
654 quota_manager->GetHostUsage(host, quota::kStorageTypeSyncable,
655 quota::QuotaClient::kFileSystem,
656 base::Bind(&DidGetHostUsage, syncableStorageUsage,
657 devtools::Page::UsageItem::ID::kFilesystem,
658 barrier));
659
660 quota_manager->GetHostUsage(host, quota::kStorageTypeTemporary,
661 quota::QuotaClient::kDatabase,
662 base::Bind(&DidGetHostUsage, temporaryStorageUsage,
663 devtools::Page::UsageItem::ID::kDatabase,
664 barrier));
665
666 quota_manager->GetHostUsage(host, quota::kStorageTypeTemporary,
667 quota::QuotaClient::kAppcache,
668 base::Bind(&DidGetHostUsage, temporaryStorageUsage,
669 devtools::Page::UsageItem::ID::kAppcache,
670 barrier));
671
672 quota_manager->GetHostUsage(host, quota::kStorageTypeTemporary,
673 quota::QuotaClient::kIndexedDatabase,
674 base::Bind(&DidGetHostUsage, temporaryStorageUsage,
675 devtools::Page::UsageItem::ID::kIndexedDatabase,
676 barrier));
677 }
678
679 } // namespace
680
681 scoped_refptr<DevToolsProtocol::Response>
682 RendererOverridesHandler::PageQueryUsageAndQuota(
683 scoped_refptr<DevToolsProtocol::Command> command) {
684 base::DictionaryValue* params = command->params();
685 std::string security_origin;
686 if (!params || !params->GetString(
687 devtools::Page::queryUsageAndQuota::kParamSecurityOrigin,
688 &security_origin)) {
689 return command->InvalidParamResponse(
690 devtools::Page::queryUsageAndQuota::kParamSecurityOrigin);
691 }
692
693 ResponseCallback callback = base::Bind(
694 &RendererOverridesHandler::PageQueryUsageAndQuotaCompleted,
695 weak_factory_.GetWeakPtr(),
696 command);
697
698 scoped_refptr<quota::QuotaManager> quota_manager =
699 agent_->GetRenderViewHost()->GetProcess()->
700 GetStoragePartition()->GetQuotaManager();
701
702 BrowserThread::PostTask(
703 BrowserThread::IO, FROM_HERE,
704 base::Bind(
705 &QueryUsageAndQuotaOnIOThread,
706 quota_manager,
707 GURL(security_origin),
708 callback));
709
710 return command->AsyncResponsePromise();
711 }
712
713 void RendererOverridesHandler::PageQueryUsageAndQuotaCompleted(
714 scoped_refptr<DevToolsProtocol::Command> command,
715 scoped_ptr<base::DictionaryValue> response_data) {
716 SendAsyncResponse(command->SuccessResponse(response_data.release()));
717 }
528 718
529 // Input agent handlers ------------------------------------------------------ 719 // Input agent handlers ------------------------------------------------------
530 720
531 scoped_refptr<DevToolsProtocol::Response> 721 scoped_refptr<DevToolsProtocol::Response>
532 RendererOverridesHandler::InputDispatchMouseEvent( 722 RendererOverridesHandler::InputDispatchMouseEvent(
533 scoped_refptr<DevToolsProtocol::Command> command) { 723 scoped_refptr<DevToolsProtocol::Command> command) {
534 base::DictionaryValue* params = command->params(); 724 base::DictionaryValue* params = command->params();
535 if (!params) 725 if (!params)
536 return NULL; 726 return NULL;
537 727
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 return NULL; 854 return NULL;
665 } 855 }
666 event.data.pinchUpdate.scale = static_cast<float>(scale); 856 event.data.pinchUpdate.scale = static_cast<float>(scale);
667 } 857 }
668 858
669 host->ForwardGestureEvent(event); 859 host->ForwardGestureEvent(event);
670 return command->SuccessResponse(NULL); 860 return command->SuccessResponse(NULL);
671 } 861 }
672 862
673 } // namespace content 863 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698