OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/protocol/usage_and_quota_query.h" | 5 #include "content/browser/devtools/protocol/usage_and_quota_query.h" |
6 | 6 |
7 #include "net/base/net_util.h" | 7 #include "net/base/net_util.h" |
8 | 8 |
9 namespace content { | 9 namespace content { |
10 namespace devtools { | 10 namespace devtools { |
11 namespace page { | 11 namespace page { |
12 | 12 |
13 namespace { | |
14 | |
15 class UsageQuery : public base::RefCounted<UsageQuery> { | |
16 public: | |
17 using Callback = base::Callback<void(const std::vector<UsageItem>&)>; | |
18 | |
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), | |
24 host_(host), | |
25 storage_type_(storage_type), | |
26 callback_(callback) { | |
27 AddRef(); | |
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); | |
36 Release(); | |
37 } | |
38 | |
39 private: | |
40 friend class base::RefCounted<UsageQuery>; | |
41 | |
42 ~UsageQuery() { | |
43 callback_.Run(usage_list_); | |
44 } | |
45 | |
46 void GetForClient(storage::QuotaClient::ID client_id, | |
47 const std::string& client_name) { | |
48 if (!quota_manager_->IsTrackingHostUsage(storage_type_, client_id)) | |
49 return; | |
50 quota_manager_->GetHostUsage( | |
51 host_, storage_type_, client_id, | |
52 base::Bind(&UsageQuery::DidGetForClient, this, client_name)); | |
53 } | |
54 | |
55 void DidGetForClient(const std::string& client_name, int64 value) { | |
56 UsageItem usage_item; | |
57 usage_item.set_id(client_name); | |
58 usage_item.set_value(value); | |
59 usage_list_.push_back(usage_item); | |
60 } | |
61 | |
62 scoped_refptr<storage::QuotaManager> quota_manager_; | |
63 std::string host_; | |
64 storage::StorageType storage_type_; | |
65 std::vector<UsageItem> usage_list_; | |
66 Callback callback_; | |
67 }; | |
68 | |
69 } // namespace | |
70 | |
71 UsageAndQuotaQuery::UsageAndQuotaQuery( | 13 UsageAndQuotaQuery::UsageAndQuotaQuery( |
72 scoped_refptr<storage::QuotaManager> quota_manager, | 14 scoped_refptr<storage::QuotaManager> quota_manager, |
73 const GURL& security_origin, | 15 const GURL& security_origin, |
74 const Callback& callback) | 16 const Callback& callback) |
75 : quota_manager_(quota_manager), | 17 : quota_manager_(quota_manager), |
76 security_origin_(security_origin), | 18 security_origin_(security_origin), |
77 callback_(callback) { | 19 callback_(callback) { |
78 AddRef(); | 20 AddRef(); |
79 quota_manager->GetUsageAndQuotaForWebApps( | 21 quota_manager->GetUsageAndQuotaForWebApps( |
80 security_origin, | 22 security_origin, |
81 storage::kStorageTypeTemporary, | 23 storage::kStorageTypeTemporary, |
82 base::Bind(&UsageAndQuotaQuery::DidGetTemporaryQuota, this)); | 24 base::Bind(&UsageAndQuotaQuery::DidGetTemporaryQuota, this)); |
83 quota_manager->GetPersistentHostQuota( | 25 quota_manager->GetPersistentHostQuota( |
84 net::GetHostOrSpecFromURL(security_origin), | 26 net::GetHostOrSpecFromURL(security_origin), |
85 base::Bind(&UsageAndQuotaQuery::DidGetPersistentQuota, this)); | 27 base::Bind(&UsageAndQuotaQuery::DidGetPersistentQuota, this)); |
86 GetHostUsage(storage::kStorageTypeTemporary, | 28 GetHostUsage(&temporary_usage_, storage::kStorageTypeTemporary); |
87 base::Bind(&Usage::set_temporary, base::Unretained(&usage_))); | 29 GetHostUsage(&persistent_usage_, storage::kStorageTypePersistent); |
88 GetHostUsage(storage::kStorageTypePersistent, | 30 GetHostUsage(&syncable_usage_, storage::kStorageTypeSyncable); |
89 base::Bind(&Usage::set_persistent, base::Unretained(&usage_))); | |
90 GetHostUsage(storage::kStorageTypeSyncable, | |
91 base::Bind(&Usage::set_syncable, base::Unretained(&usage_))); | |
92 Release(); | 31 Release(); |
93 } | 32 } |
94 | 33 |
95 UsageAndQuotaQuery::~UsageAndQuotaQuery() { | 34 UsageAndQuotaQuery::~UsageAndQuotaQuery() { |
96 scoped_ptr<QueryUsageAndQuotaResponse> response( | 35 callback_.Run(QueryUsageAndQuotaResponse::Create() |
97 new QueryUsageAndQuotaResponse); | 36 .set_quota(Quota::Create().set_temporary(temporary_quota_) |
98 response->set_quota(quota_); | 37 .set_persistent(persistent_quota_) |
99 response->set_usage(usage_); | 38 .Pass()) |
100 callback_.Run(response.Pass()); | 39 .set_usage(Usage::Create().set_temporary(temporary_usage_.Pass()) |
| 40 .set_persistent(persistent_usage_.Pass()) |
| 41 .set_syncable(syncable_usage_.Pass()) |
| 42 .Pass()) |
| 43 .Pass()); |
101 } | 44 } |
102 | 45 |
103 void UsageAndQuotaQuery::DidGetTemporaryQuota(storage::QuotaStatusCode status, | 46 void UsageAndQuotaQuery::DidGetTemporaryQuota(storage::QuotaStatusCode status, |
104 int64 used_bytes, | 47 int64 used_bytes, |
105 int64 quota_in_bytes) { | 48 int64 quota_in_bytes) { |
106 if (status == storage::kQuotaStatusOk) | 49 if (status == storage::kQuotaStatusOk) |
107 quota_.set_temporary(quota_in_bytes); | 50 temporary_quota_ = quota_in_bytes; |
108 } | 51 } |
109 | 52 |
110 void UsageAndQuotaQuery::DidGetPersistentQuota(storage::QuotaStatusCode status, | 53 void UsageAndQuotaQuery::DidGetPersistentQuota(storage::QuotaStatusCode status, |
111 int64 value) { | 54 int64 value) { |
112 if (status == storage::kQuotaStatusOk) | 55 if (status == storage::kQuotaStatusOk) |
113 quota_.set_persistent(value); | 56 persistent_quota_ = value; |
114 } | 57 } |
115 | 58 |
116 void UsageAndQuotaQuery::GetHostUsage( | 59 void UsageAndQuotaQuery::GetHostUsage( |
117 storage::StorageType storage_type, | 60 ListBuilder<UsageItem>* list, |
118 const UsageItemsCallback& items_callback) { | 61 storage::StorageType storage_type) { |
119 // |base::Bind| is used instead of passing |items_callback| directly | 62 GetUsageForClient(list, storage_type, storage::QuotaClient::kFileSystem, |
120 // so that |this| is retained. | 63 usage_item::kIdFilesystem); |
121 new UsageQuery(quota_manager_, | 64 GetUsageForClient(list, storage_type, storage::QuotaClient::kDatabase, |
122 net::GetHostOrSpecFromURL(security_origin_), | 65 usage_item::kIdDatabase); |
123 storage_type, | 66 GetUsageForClient(list, storage_type, storage::QuotaClient::kAppcache, |
124 base::Bind(&UsageAndQuotaQuery::DidGetHostUsage, | 67 usage_item::kIdAppcache); |
125 this, items_callback)); | 68 GetUsageForClient(list, storage_type, storage::QuotaClient::kIndexedDatabase, |
| 69 usage_item::kIdIndexeddatabase); |
126 } | 70 } |
127 | 71 |
128 void UsageAndQuotaQuery::DidGetHostUsage( | 72 void UsageAndQuotaQuery::GetUsageForClient(ListBuilder<UsageItem>* list, |
129 const UsageItemsCallback& items_callback, | 73 storage::StorageType storage_type, |
130 const std::vector<UsageItem>& usage_list) { | 74 storage::QuotaClient::ID client_id, |
131 items_callback.Run(usage_list); | 75 const std::string& client_name) { |
| 76 if (!quota_manager_->IsTrackingHostUsage(storage_type, client_id)) |
| 77 return; |
| 78 quota_manager_->GetHostUsage( |
| 79 net::GetHostOrSpecFromURL(security_origin_), |
| 80 storage_type, |
| 81 client_id, |
| 82 base::Bind(&UsageAndQuotaQuery::DidGetUsageForClient, |
| 83 this, list, client_name)); |
132 } | 84 } |
133 | 85 |
| 86 void UsageAndQuotaQuery::DidGetUsageForClient(ListBuilder<UsageItem>* list, |
| 87 const std::string& client_name, |
| 88 int64 value) { |
| 89 list->push_back( |
| 90 UsageItem::Create().set_id(client_name).set_value(value).Pass()); |
| 91 } |
| 92 |
| 93 |
134 } // namespace page | 94 } // namespace page |
135 } // namespace devtools | 95 } // namespace devtools |
136 } // namespace content | 96 } // namespace content |
OLD | NEW |