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 quota::QuotaClient; |
17 using quota::QuotaManager; | 17 using quota::QuotaManager; |
18 using quota::QuotaStatusCode; | 18 using quota::QuotaStatusCode; |
19 using quota::RequestStorageQuotaParams; | |
19 using quota::StorageType; | 20 using quota::StorageType; |
20 | 21 |
21 namespace content { | 22 namespace content { |
22 | 23 |
23 // Created one per request to carry the request's request_id around. | 24 // Created one per request to carry the request's request_id around. |
24 // Dispatches requests from renderer/worker to the QuotaManager and | 25 // Dispatches requests from renderer/worker to the QuotaManager and |
25 // sends back the response to the renderer/worker. | 26 // sends back the response to the renderer/worker. |
26 class QuotaDispatcherHost::RequestDispatcher { | 27 class QuotaDispatcherHost::RequestDispatcher { |
27 public: | 28 public: |
28 RequestDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, | 29 RequestDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 | 94 |
94 base::WeakPtrFactory<QueryUsageAndQuotaDispatcher> weak_factory_; | 95 base::WeakPtrFactory<QueryUsageAndQuotaDispatcher> weak_factory_; |
95 }; | 96 }; |
96 | 97 |
97 class QuotaDispatcherHost::RequestQuotaDispatcher | 98 class QuotaDispatcherHost::RequestQuotaDispatcher |
98 : public RequestDispatcher { | 99 : public RequestDispatcher { |
99 public: | 100 public: |
100 typedef RequestQuotaDispatcher self_type; | 101 typedef RequestQuotaDispatcher self_type; |
101 | 102 |
102 RequestQuotaDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, | 103 RequestQuotaDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, |
103 int request_id, | 104 const RequestStorageQuotaParams& params, |
104 const GURL& origin, | 105 int request_id) |
nasko
2014/03/03 20:00:32
nit: request_id is already part of params, right?
Greg Billock
2014/03/03 22:41:47
I thought I remembered the compiler being fussy ab
| |
105 StorageType type, | |
106 uint64 requested_quota, | |
107 int render_view_id) | |
108 : RequestDispatcher(dispatcher_host, request_id), | 106 : RequestDispatcher(dispatcher_host, request_id), |
109 origin_(origin), | 107 params_(params), |
110 type_(type), | |
111 current_usage_(0), | 108 current_usage_(0), |
112 current_quota_(0), | 109 current_quota_(0), |
113 requested_quota_(0), | 110 requested_quota_(0), |
114 render_view_id_(render_view_id), | |
115 weak_factory_(this) { | 111 weak_factory_(this) { |
116 // Convert the requested size from uint64 to int64 since the quota backend | 112 // Convert the requested size from uint64 to int64 since the quota backend |
117 // requires int64 values. | 113 // requires int64 values. |
118 // TODO(nhiroki): The backend should accept uint64 values. | 114 // TODO(nhiroki): The backend should accept uint64 values. |
119 requested_quota_ = base::saturated_cast<int64>(requested_quota); | 115 requested_quota_ = base::saturated_cast<int64>(params_.requested_size); |
120 } | 116 } |
121 virtual ~RequestQuotaDispatcher() {} | 117 virtual ~RequestQuotaDispatcher() {} |
122 | 118 |
123 void Start() { | 119 void Start() { |
124 DCHECK(dispatcher_host()); | 120 DCHECK(dispatcher_host()); |
125 | 121 |
126 DCHECK(type_ == quota::kStorageTypeTemporary || | 122 DCHECK(params_.storage_type == quota::kStorageTypeTemporary || |
127 type_ == quota::kStorageTypePersistent || | 123 params_.storage_type == quota::kStorageTypePersistent || |
128 type_ == quota::kStorageTypeSyncable); | 124 params_.storage_type == quota::kStorageTypeSyncable); |
129 if (type_ == quota::kStorageTypePersistent) { | 125 if (params_.storage_type == quota::kStorageTypePersistent) { |
130 quota_manager()->GetUsageAndQuotaForWebApps( | 126 quota_manager()->GetUsageAndQuotaForWebApps( |
131 origin_, type_, | 127 params_.origin_url, params_.storage_type, |
132 base::Bind(&self_type::DidGetPersistentUsageAndQuota, | 128 base::Bind(&self_type::DidGetPersistentUsageAndQuota, |
133 weak_factory_.GetWeakPtr())); | 129 weak_factory_.GetWeakPtr())); |
134 } else { | 130 } else { |
135 quota_manager()->GetUsageAndQuotaForWebApps( | 131 quota_manager()->GetUsageAndQuotaForWebApps( |
136 origin_, type_, | 132 params_.origin_url, params_.storage_type, |
137 base::Bind(&self_type::DidGetTemporaryUsageAndQuota, | 133 base::Bind(&self_type::DidGetTemporaryUsageAndQuota, |
138 weak_factory_.GetWeakPtr())); | 134 weak_factory_.GetWeakPtr())); |
139 } | 135 } |
140 } | 136 } |
141 | 137 |
142 private: | 138 private: |
143 void DidGetPersistentUsageAndQuota(QuotaStatusCode status, | 139 void DidGetPersistentUsageAndQuota(QuotaStatusCode status, |
144 int64 usage, | 140 int64 usage, |
145 int64 quota) { | 141 int64 quota) { |
146 if (!dispatcher_host()) | 142 if (!dispatcher_host()) |
147 return; | 143 return; |
148 if (status != quota::kQuotaStatusOk) { | 144 if (status != quota::kQuotaStatusOk) { |
149 DidFinish(status, 0, 0); | 145 DidFinish(status, 0, 0); |
150 return; | 146 return; |
151 } | 147 } |
152 | 148 |
153 if (quota_manager()->IsStorageUnlimited(origin_, type_) || | 149 if (quota_manager()->IsStorageUnlimited(params_.origin_url, |
150 params_.storage_type) || | |
154 requested_quota_ <= quota) { | 151 requested_quota_ <= quota) { |
155 // Seems like we can just let it go. | 152 // Seems like we can just let it go. |
156 DidFinish(quota::kQuotaStatusOk, usage, requested_quota_); | 153 DidFinish(quota::kQuotaStatusOk, usage, params_.requested_size); |
157 return; | 154 return; |
158 } | 155 } |
159 current_usage_ = usage; | 156 current_usage_ = usage; |
160 current_quota_ = quota; | 157 current_quota_ = quota; |
161 | 158 |
162 // Otherwise we need to consult with the permission context and | 159 // Otherwise we need to consult with the permission context and |
163 // possibly show an infobar. | 160 // possibly show a prompt. |
164 DCHECK(permission_context()); | 161 DCHECK(permission_context()); |
165 permission_context()->RequestQuotaPermission( | 162 permission_context()->RequestQuotaPermission(params_, render_process_id(), |
166 origin_, type_, requested_quota_, render_process_id(), render_view_id_, | |
167 base::Bind(&self_type::DidGetPermissionResponse, | 163 base::Bind(&self_type::DidGetPermissionResponse, |
168 weak_factory_.GetWeakPtr())); | 164 weak_factory_.GetWeakPtr())); |
169 } | 165 } |
170 | 166 |
171 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status, | 167 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status, |
172 int64 usage, | 168 int64 usage, |
173 int64 quota) { | 169 int64 quota) { |
174 DidFinish(status, usage, std::min(requested_quota_, quota)); | 170 DidFinish(status, usage, std::min(requested_quota_, quota)); |
175 } | 171 } |
176 | 172 |
177 void DidGetPermissionResponse( | 173 void DidGetPermissionResponse( |
178 QuotaPermissionContext::QuotaPermissionResponse response) { | 174 QuotaPermissionContext::QuotaPermissionResponse response) { |
179 if (!dispatcher_host()) | 175 if (!dispatcher_host()) |
180 return; | 176 return; |
181 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) { | 177 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) { |
182 // User didn't allow the new quota. Just returning the current quota. | 178 // User didn't allow the new quota. Just returning the current quota. |
183 DidFinish(quota::kQuotaStatusOk, current_usage_, current_quota_); | 179 DidFinish(quota::kQuotaStatusOk, current_usage_, current_quota_); |
184 return; | 180 return; |
185 } | 181 } |
186 // Now we're allowed to set the new quota. | 182 // Now we're allowed to set the new quota. |
187 quota_manager()->SetPersistentHostQuota( | 183 quota_manager()->SetPersistentHostQuota( |
188 net::GetHostOrSpecFromURL(origin_), requested_quota_, | 184 net::GetHostOrSpecFromURL(params_.origin_url), params_.requested_size, |
189 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr())); | 185 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr())); |
190 } | 186 } |
191 | 187 |
192 void DidSetHostQuota(QuotaStatusCode status, int64 new_quota) { | 188 void DidSetHostQuota(QuotaStatusCode status, int64 new_quota) { |
193 DidFinish(status, current_usage_, new_quota); | 189 DidFinish(status, current_usage_, new_quota); |
194 } | 190 } |
195 | 191 |
196 void DidFinish(QuotaStatusCode status, | 192 void DidFinish(QuotaStatusCode status, |
197 int64 usage, | 193 int64 usage, |
198 int64 granted_quota) { | 194 int64 granted_quota) { |
199 if (!dispatcher_host()) | 195 if (!dispatcher_host()) |
200 return; | 196 return; |
201 DCHECK(dispatcher_host()); | 197 DCHECK(dispatcher_host()); |
202 if (status != quota::kQuotaStatusOk) { | 198 if (status != quota::kQuotaStatusOk) { |
203 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); | 199 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); |
204 } else { | 200 } else { |
205 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota( | 201 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota( |
206 request_id(), usage, granted_quota)); | 202 request_id(), usage, granted_quota)); |
207 } | 203 } |
208 Completed(); | 204 Completed(); |
209 } | 205 } |
210 | 206 |
211 const GURL origin_; | 207 RequestStorageQuotaParams params_; |
212 const StorageType type_; | |
213 int64 current_usage_; | 208 int64 current_usage_; |
214 int64 current_quota_; | 209 int64 current_quota_; |
215 int64 requested_quota_; | 210 int64 requested_quota_; |
216 const int render_view_id_; | |
217 base::WeakPtrFactory<self_type> weak_factory_; | 211 base::WeakPtrFactory<self_type> weak_factory_; |
218 }; | 212 }; |
219 | 213 |
220 QuotaDispatcherHost::QuotaDispatcherHost( | 214 QuotaDispatcherHost::QuotaDispatcherHost( |
221 int process_id, | 215 int process_id, |
222 QuotaManager* quota_manager, | 216 QuotaManager* quota_manager, |
223 QuotaPermissionContext* permission_context) | 217 QuotaPermissionContext* permission_context) |
224 : BrowserMessageFilter(QuotaMsgStart), | 218 : BrowserMessageFilter(QuotaMsgStart), |
225 process_id_(process_id), | 219 process_id_(process_id), |
226 quota_manager_(quota_manager), | 220 quota_manager_(quota_manager), |
(...skipping 20 matching lines...) Expand all Loading... | |
247 void QuotaDispatcherHost::OnQueryStorageUsageAndQuota( | 241 void QuotaDispatcherHost::OnQueryStorageUsageAndQuota( |
248 int request_id, | 242 int request_id, |
249 const GURL& origin, | 243 const GURL& origin, |
250 StorageType type) { | 244 StorageType type) { |
251 QueryUsageAndQuotaDispatcher* dispatcher = new QueryUsageAndQuotaDispatcher( | 245 QueryUsageAndQuotaDispatcher* dispatcher = new QueryUsageAndQuotaDispatcher( |
252 weak_factory_.GetWeakPtr(), request_id); | 246 weak_factory_.GetWeakPtr(), request_id); |
253 dispatcher->QueryStorageUsageAndQuota(origin, type); | 247 dispatcher->QueryStorageUsageAndQuota(origin, type); |
254 } | 248 } |
255 | 249 |
256 void QuotaDispatcherHost::OnRequestStorageQuota( | 250 void QuotaDispatcherHost::OnRequestStorageQuota( |
257 int render_view_id, | 251 const RequestStorageQuotaParams& params) { |
258 int request_id, | 252 if (params.storage_type != quota::kStorageTypeTemporary && |
259 const GURL& origin, | 253 params.storage_type != quota::kStorageTypePersistent) { |
260 StorageType type, | |
261 uint64 requested_size) { | |
262 if (type != quota::kStorageTypeTemporary && | |
263 type != quota::kStorageTypePersistent) { | |
264 // Unsupported storage types. | 254 // Unsupported storage types. |
265 Send(new QuotaMsg_DidFail(request_id, quota::kQuotaErrorNotSupported)); | 255 Send(new QuotaMsg_DidFail(params.request_id, |
256 quota::kQuotaErrorNotSupported)); | |
266 return; | 257 return; |
267 } | 258 } |
268 | 259 |
269 RequestQuotaDispatcher* dispatcher = new RequestQuotaDispatcher( | 260 RequestQuotaDispatcher* dispatcher = |
270 weak_factory_.GetWeakPtr(), request_id, origin, type, | 261 new RequestQuotaDispatcher(weak_factory_.GetWeakPtr(), |
271 requested_size, render_view_id); | 262 params, params.request_id); |
272 dispatcher->Start(); | 263 dispatcher->Start(); |
273 } | 264 } |
274 | 265 |
275 } // namespace content | 266 } // namespace content |
OLD | NEW |