OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/quota_dispatcher_host.h" | 5 #include "content/browser/quota_dispatcher_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
10 #include "content/common/quota_messages.h" | 10 #include "content/common/quota_messages.h" |
11 #include "content/public/browser/quota_permission_context.h" | 11 #include "content/public/browser/quota_permission_context.h" |
12 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
14 #include "webkit/browser/quota/quota_manager.h" | 14 #include "webkit/browser/quota/quota_manager.h" |
15 | 15 |
16 using quota::QuotaClient; | 16 using storage::QuotaClient; |
17 using quota::QuotaManager; | 17 using storage::QuotaManager; |
18 using quota::QuotaStatusCode; | 18 using storage::QuotaStatusCode; |
19 using quota::StorageType; | 19 using storage::StorageType; |
20 | 20 |
21 namespace content { | 21 namespace content { |
22 | 22 |
23 // Created one per request to carry the request's request_id around. | 23 // Created one per request to carry the request's request_id around. |
24 // Dispatches requests from renderer/worker to the QuotaManager and | 24 // Dispatches requests from renderer/worker to the QuotaManager and |
25 // sends back the response to the renderer/worker. | 25 // sends back the response to the renderer/worker. |
26 class QuotaDispatcherHost::RequestDispatcher { | 26 class QuotaDispatcherHost::RequestDispatcher { |
27 public: | 27 public: |
28 RequestDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, | 28 RequestDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, |
29 int request_id) | 29 int request_id) |
30 : dispatcher_host_(dispatcher_host), | 30 : dispatcher_host_(dispatcher_host), |
31 render_process_id_(dispatcher_host->process_id_), | 31 render_process_id_(dispatcher_host->process_id_), |
32 request_id_(request_id) { | 32 request_id_(request_id) { |
33 dispatcher_host_->outstanding_requests_.AddWithID(this, request_id_); | 33 dispatcher_host_->outstanding_requests_.AddWithID(this, request_id_); |
34 } | 34 } |
35 virtual ~RequestDispatcher() {} | 35 virtual ~RequestDispatcher() {} |
36 | 36 |
37 protected: | 37 protected: |
38 // Subclass must call this when it's done with the request. | 38 // Subclass must call this when it's done with the request. |
39 void Completed() { | 39 void Completed() { |
40 if (dispatcher_host_) | 40 if (dispatcher_host_) |
41 dispatcher_host_->outstanding_requests_.Remove(request_id_); | 41 dispatcher_host_->outstanding_requests_.Remove(request_id_); |
42 } | 42 } |
43 | 43 |
44 QuotaDispatcherHost* dispatcher_host() const { | 44 QuotaDispatcherHost* dispatcher_host() const { |
45 return dispatcher_host_.get(); | 45 return dispatcher_host_.get(); |
46 } | 46 } |
47 quota::QuotaManager* quota_manager() const { | 47 storage::QuotaManager* quota_manager() const { |
48 return dispatcher_host_ ? dispatcher_host_->quota_manager_ : NULL; | 48 return dispatcher_host_ ? dispatcher_host_->quota_manager_ : NULL; |
49 } | 49 } |
50 QuotaPermissionContext* permission_context() const { | 50 QuotaPermissionContext* permission_context() const { |
51 return dispatcher_host_ ? | 51 return dispatcher_host_ ? |
52 dispatcher_host_->permission_context_.get() : NULL; | 52 dispatcher_host_->permission_context_.get() : NULL; |
53 } | 53 } |
54 int render_process_id() const { return render_process_id_; } | 54 int render_process_id() const { return render_process_id_; } |
55 int request_id() const { return request_id_; } | 55 int request_id() const { return request_id_; } |
56 | 56 |
57 private: | 57 private: |
(...skipping 17 matching lines...) Expand all Loading... |
75 origin, type, | 75 origin, type, |
76 base::Bind(&QueryUsageAndQuotaDispatcher::DidQueryStorageUsageAndQuota, | 76 base::Bind(&QueryUsageAndQuotaDispatcher::DidQueryStorageUsageAndQuota, |
77 weak_factory_.GetWeakPtr())); | 77 weak_factory_.GetWeakPtr())); |
78 } | 78 } |
79 | 79 |
80 private: | 80 private: |
81 void DidQueryStorageUsageAndQuota( | 81 void DidQueryStorageUsageAndQuota( |
82 QuotaStatusCode status, int64 usage, int64 quota) { | 82 QuotaStatusCode status, int64 usage, int64 quota) { |
83 if (!dispatcher_host()) | 83 if (!dispatcher_host()) |
84 return; | 84 return; |
85 if (status != quota::kQuotaStatusOk) { | 85 if (status != storage::kQuotaStatusOk) { |
86 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); | 86 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); |
87 } else { | 87 } else { |
88 dispatcher_host()->Send(new QuotaMsg_DidQueryStorageUsageAndQuota( | 88 dispatcher_host()->Send(new QuotaMsg_DidQueryStorageUsageAndQuota( |
89 request_id(), usage, quota)); | 89 request_id(), usage, quota)); |
90 } | 90 } |
91 Completed(); | 91 Completed(); |
92 } | 92 } |
93 | 93 |
94 base::WeakPtrFactory<QueryUsageAndQuotaDispatcher> weak_factory_; | 94 base::WeakPtrFactory<QueryUsageAndQuotaDispatcher> weak_factory_; |
95 }; | 95 }; |
(...skipping 14 matching lines...) Expand all Loading... |
110 // Convert the requested size from uint64 to int64 since the quota backend | 110 // Convert the requested size from uint64 to int64 since the quota backend |
111 // requires int64 values. | 111 // requires int64 values. |
112 // TODO(nhiroki): The backend should accept uint64 values. | 112 // TODO(nhiroki): The backend should accept uint64 values. |
113 requested_quota_ = base::saturated_cast<int64>(params_.requested_size); | 113 requested_quota_ = base::saturated_cast<int64>(params_.requested_size); |
114 } | 114 } |
115 virtual ~RequestQuotaDispatcher() {} | 115 virtual ~RequestQuotaDispatcher() {} |
116 | 116 |
117 void Start() { | 117 void Start() { |
118 DCHECK(dispatcher_host()); | 118 DCHECK(dispatcher_host()); |
119 | 119 |
120 DCHECK(params_.storage_type == quota::kStorageTypeTemporary || | 120 DCHECK(params_.storage_type == storage::kStorageTypeTemporary || |
121 params_.storage_type == quota::kStorageTypePersistent || | 121 params_.storage_type == storage::kStorageTypePersistent || |
122 params_.storage_type == quota::kStorageTypeSyncable); | 122 params_.storage_type == storage::kStorageTypeSyncable); |
123 if (params_.storage_type == quota::kStorageTypePersistent) { | 123 if (params_.storage_type == storage::kStorageTypePersistent) { |
124 quota_manager()->GetUsageAndQuotaForWebApps( | 124 quota_manager()->GetUsageAndQuotaForWebApps( |
125 params_.origin_url, params_.storage_type, | 125 params_.origin_url, params_.storage_type, |
126 base::Bind(&self_type::DidGetPersistentUsageAndQuota, | 126 base::Bind(&self_type::DidGetPersistentUsageAndQuota, |
127 weak_factory_.GetWeakPtr())); | 127 weak_factory_.GetWeakPtr())); |
128 } else { | 128 } else { |
129 quota_manager()->GetUsageAndQuotaForWebApps( | 129 quota_manager()->GetUsageAndQuotaForWebApps( |
130 params_.origin_url, params_.storage_type, | 130 params_.origin_url, params_.storage_type, |
131 base::Bind(&self_type::DidGetTemporaryUsageAndQuota, | 131 base::Bind(&self_type::DidGetTemporaryUsageAndQuota, |
132 weak_factory_.GetWeakPtr())); | 132 weak_factory_.GetWeakPtr())); |
133 } | 133 } |
134 } | 134 } |
135 | 135 |
136 private: | 136 private: |
137 void DidGetPersistentUsageAndQuota(QuotaStatusCode status, | 137 void DidGetPersistentUsageAndQuota(QuotaStatusCode status, |
138 int64 usage, | 138 int64 usage, |
139 int64 quota) { | 139 int64 quota) { |
140 if (!dispatcher_host()) | 140 if (!dispatcher_host()) |
141 return; | 141 return; |
142 if (status != quota::kQuotaStatusOk) { | 142 if (status != storage::kQuotaStatusOk) { |
143 DidFinish(status, 0, 0); | 143 DidFinish(status, 0, 0); |
144 return; | 144 return; |
145 } | 145 } |
146 | 146 |
147 if (quota_manager()->IsStorageUnlimited(params_.origin_url, | 147 if (quota_manager()->IsStorageUnlimited(params_.origin_url, |
148 params_.storage_type) || | 148 params_.storage_type) || |
149 requested_quota_ <= quota) { | 149 requested_quota_ <= quota) { |
150 // Seems like we can just let it go. | 150 // Seems like we can just let it go. |
151 DidFinish(quota::kQuotaStatusOk, usage, params_.requested_size); | 151 DidFinish(storage::kQuotaStatusOk, usage, params_.requested_size); |
152 return; | 152 return; |
153 } | 153 } |
154 current_usage_ = usage; | 154 current_usage_ = usage; |
155 current_quota_ = quota; | 155 current_quota_ = quota; |
156 | 156 |
157 // Otherwise we need to consult with the permission context and | 157 // Otherwise we need to consult with the permission context and |
158 // possibly show a prompt. | 158 // possibly show a prompt. |
159 DCHECK(permission_context()); | 159 DCHECK(permission_context()); |
160 permission_context()->RequestQuotaPermission(params_, render_process_id(), | 160 permission_context()->RequestQuotaPermission(params_, render_process_id(), |
161 base::Bind(&self_type::DidGetPermissionResponse, | 161 base::Bind(&self_type::DidGetPermissionResponse, |
162 weak_factory_.GetWeakPtr())); | 162 weak_factory_.GetWeakPtr())); |
163 } | 163 } |
164 | 164 |
165 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status, | 165 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status, |
166 int64 usage, | 166 int64 usage, |
167 int64 quota) { | 167 int64 quota) { |
168 DidFinish(status, usage, std::min(requested_quota_, quota)); | 168 DidFinish(status, usage, std::min(requested_quota_, quota)); |
169 } | 169 } |
170 | 170 |
171 void DidGetPermissionResponse( | 171 void DidGetPermissionResponse( |
172 QuotaPermissionContext::QuotaPermissionResponse response) { | 172 QuotaPermissionContext::QuotaPermissionResponse response) { |
173 if (!dispatcher_host()) | 173 if (!dispatcher_host()) |
174 return; | 174 return; |
175 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) { | 175 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) { |
176 // User didn't allow the new quota. Just returning the current quota. | 176 // User didn't allow the new quota. Just returning the current quota. |
177 DidFinish(quota::kQuotaStatusOk, current_usage_, current_quota_); | 177 DidFinish(storage::kQuotaStatusOk, current_usage_, current_quota_); |
178 return; | 178 return; |
179 } | 179 } |
180 // Now we're allowed to set the new quota. | 180 // Now we're allowed to set the new quota. |
181 quota_manager()->SetPersistentHostQuota( | 181 quota_manager()->SetPersistentHostQuota( |
182 net::GetHostOrSpecFromURL(params_.origin_url), params_.requested_size, | 182 net::GetHostOrSpecFromURL(params_.origin_url), params_.requested_size, |
183 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr())); | 183 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr())); |
184 } | 184 } |
185 | 185 |
186 void DidSetHostQuota(QuotaStatusCode status, int64 new_quota) { | 186 void DidSetHostQuota(QuotaStatusCode status, int64 new_quota) { |
187 DidFinish(status, current_usage_, new_quota); | 187 DidFinish(status, current_usage_, new_quota); |
188 } | 188 } |
189 | 189 |
190 void DidFinish(QuotaStatusCode status, | 190 void DidFinish(QuotaStatusCode status, |
191 int64 usage, | 191 int64 usage, |
192 int64 granted_quota) { | 192 int64 granted_quota) { |
193 if (!dispatcher_host()) | 193 if (!dispatcher_host()) |
194 return; | 194 return; |
195 DCHECK(dispatcher_host()); | 195 DCHECK(dispatcher_host()); |
196 if (status != quota::kQuotaStatusOk) { | 196 if (status != storage::kQuotaStatusOk) { |
197 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); | 197 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); |
198 } else { | 198 } else { |
199 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota( | 199 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota( |
200 request_id(), usage, granted_quota)); | 200 request_id(), usage, granted_quota)); |
201 } | 201 } |
202 Completed(); | 202 Completed(); |
203 } | 203 } |
204 | 204 |
205 StorageQuotaParams params_; | 205 StorageQuotaParams params_; |
206 int64 current_usage_; | 206 int64 current_usage_; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 int request_id, | 238 int request_id, |
239 const GURL& origin, | 239 const GURL& origin, |
240 StorageType type) { | 240 StorageType type) { |
241 QueryUsageAndQuotaDispatcher* dispatcher = new QueryUsageAndQuotaDispatcher( | 241 QueryUsageAndQuotaDispatcher* dispatcher = new QueryUsageAndQuotaDispatcher( |
242 weak_factory_.GetWeakPtr(), request_id); | 242 weak_factory_.GetWeakPtr(), request_id); |
243 dispatcher->QueryStorageUsageAndQuota(origin, type); | 243 dispatcher->QueryStorageUsageAndQuota(origin, type); |
244 } | 244 } |
245 | 245 |
246 void QuotaDispatcherHost::OnRequestStorageQuota( | 246 void QuotaDispatcherHost::OnRequestStorageQuota( |
247 const StorageQuotaParams& params) { | 247 const StorageQuotaParams& params) { |
248 if (params.storage_type != quota::kStorageTypeTemporary && | 248 if (params.storage_type != storage::kStorageTypeTemporary && |
249 params.storage_type != quota::kStorageTypePersistent) { | 249 params.storage_type != storage::kStorageTypePersistent) { |
250 // Unsupported storage types. | 250 // Unsupported storage types. |
251 Send(new QuotaMsg_DidFail(params.request_id, | 251 Send(new QuotaMsg_DidFail(params.request_id, |
252 quota::kQuotaErrorNotSupported)); | 252 storage::kQuotaErrorNotSupported)); |
253 return; | 253 return; |
254 } | 254 } |
255 | 255 |
256 RequestQuotaDispatcher* dispatcher = | 256 RequestQuotaDispatcher* dispatcher = |
257 new RequestQuotaDispatcher(weak_factory_.GetWeakPtr(), | 257 new RequestQuotaDispatcher(weak_factory_.GetWeakPtr(), |
258 params); | 258 params); |
259 dispatcher->Start(); | 259 dispatcher->Start(); |
260 } | 260 } |
261 | 261 |
262 } // namespace content | 262 } // namespace content |
OLD | NEW |