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