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
3 // found in the LICENSE file.
5 #include "content/browser/devtools/protocol/usage_and_quota_query.h"
7 #include "net/base/net_util.h"
15 class UsageQuery : public base::RefCounted<UsageQuery> {
17 using Callback = base::Callback<void(const std::vector<UsageItem>&)>;
19 UsageQuery(scoped_refptr<storage::QuotaManager> quota_manager,
20 const std::string& host,
21 storage::StorageType storage_type,
22 const Callback& callback)
23 : quota_manager_(quota_manager),
25 storage_type_(storage_type),
28 GetForClient(storage::QuotaClient::kFileSystem,
29 usage_item::kIdFilesystem);
30 GetForClient(storage::QuotaClient::kDatabase,
31 usage_item::kIdDatabase);
32 GetForClient(storage::QuotaClient::kAppcache,
33 usage_item::kIdAppcache);
34 GetForClient(storage::QuotaClient::kIndexedDatabase,
35 usage_item::kIdIndexeddatabase);
40 friend class base::RefCounted<UsageQuery>;
43 callback_.Run(usage_list_);
46 void GetForClient(storage::QuotaClient::ID client_id,
47 const std::string& client_name) {
48 if (!quota_manager_->IsTrackingHostUsage(storage_type_, client_id))
50 quota_manager_->GetHostUsage(
51 host_, storage_type_, client_id,
52 base::Bind(&UsageQuery::DidGetForClient, this, client_name));
55 void DidGetForClient(const std::string& client_name, int64 value) {
57 usage_item.set_id(client_name);
58 usage_item.set_value(value);
59 usage_list_.push_back(usage_item);
62 scoped_refptr<storage::QuotaManager> quota_manager_;
64 storage::StorageType storage_type_;
65 std::vector<UsageItem> usage_list_;
71 UsageAndQuotaQuery::UsageAndQuotaQuery(
72 scoped_refptr<storage::QuotaManager> quota_manager,
73 const GURL& security_origin,
74 const Callback& callback)
75 : quota_manager_(quota_manager),
76 security_origin_(security_origin),
79 quota_manager->GetUsageAndQuotaForWebApps(
81 storage::kStorageTypeTemporary,
82 base::Bind(&UsageAndQuotaQuery::DidGetTemporaryQuota, this));
83 quota_manager->GetPersistentHostQuota(
84 net::GetHostOrSpecFromURL(security_origin),
85 base::Bind(&UsageAndQuotaQuery::DidGetPersistentQuota, this));
86 GetHostUsage(storage::kStorageTypeTemporary,
87 base::Bind(&Usage::set_temporary, base::Unretained(&usage_)));
88 GetHostUsage(storage::kStorageTypePersistent,
89 base::Bind(&Usage::set_persistent, base::Unretained(&usage_)));
90 GetHostUsage(storage::kStorageTypeSyncable,
91 base::Bind(&Usage::set_syncable, base::Unretained(&usage_)));
95 UsageAndQuotaQuery::~UsageAndQuotaQuery() {
96 scoped_ptr<QueryUsageAndQuotaResponse> response(
97 new QueryUsageAndQuotaResponse);
98 response->set_quota(quota_);
99 response->set_usage(usage_);
100 callback_.Run(response.Pass());
103 void UsageAndQuotaQuery::DidGetTemporaryQuota(storage::QuotaStatusCode status,
105 int64 quota_in_bytes) {
106 if (status == storage::kQuotaStatusOk)
107 quota_.set_temporary(quota_in_bytes);
110 void UsageAndQuotaQuery::DidGetPersistentQuota(storage::QuotaStatusCode status,
112 if (status == storage::kQuotaStatusOk)
113 quota_.set_persistent(value);
116 void UsageAndQuotaQuery::GetHostUsage(
117 storage::StorageType storage_type,
118 const UsageItemsCallback& items_callback) {
119 // |base::Bind| is used instead of passing |items_callback| directly
120 // so that |this| is retained.
121 new UsageQuery(quota_manager_,
122 net::GetHostOrSpecFromURL(security_origin_),
124 base::Bind(&UsageAndQuotaQuery::DidGetHostUsage,
125 this, items_callback));
128 void UsageAndQuotaQuery::DidGetHostUsage(
129 const UsageItemsCallback& items_callback,
130 const std::vector<UsageItem>& usage_list) {
131 items_callback.Run(usage_list);
135 } // namespace devtools
136 } // namespace content