Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / content / browser / devtools / protocol / usage_and_quota_query.cc
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.
4
5 #include "content/browser/devtools/protocol/usage_and_quota_query.h"
6
7 #include "net/base/net_util.h"
8
9 namespace content {
10 namespace devtools {
11 namespace page {
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(
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),
77       callback_(callback) {
78   AddRef();
79   quota_manager->GetUsageAndQuotaForWebApps(
80       security_origin,
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_)));
92   Release();
93 }
94
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());
101 }
102
103 void UsageAndQuotaQuery::DidGetTemporaryQuota(storage::QuotaStatusCode status,
104                                               int64 used_bytes,
105                                               int64 quota_in_bytes) {
106   if (status == storage::kQuotaStatusOk)
107     quota_.set_temporary(quota_in_bytes);
108 }
109
110 void UsageAndQuotaQuery::DidGetPersistentQuota(storage::QuotaStatusCode status,
111                                                int64 value) {
112   if (status == storage::kQuotaStatusOk)
113     quota_.set_persistent(value);
114 }
115
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_),
123                  storage_type,
124                  base::Bind(&UsageAndQuotaQuery::DidGetHostUsage,
125                             this, items_callback));
126 }
127
128 void UsageAndQuotaQuery::DidGetHostUsage(
129     const UsageItemsCallback& items_callback,
130     const std::vector<UsageItem>& usage_list) {
131   items_callback.Run(usage_list);
132 }
133
134 }  // namespace page
135 }  // namespace devtools
136 }  // namespace content