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 "content/common/quota_messages.h" | 9 #include "content/common/quota_messages.h" |
10 #include "content/public/browser/quota_permission_context.h" | 10 #include "content/public/browser/quota_permission_context.h" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 typedef RequestQuotaDispatcher self_type; | 99 typedef RequestQuotaDispatcher self_type; |
100 | 100 |
101 RequestQuotaDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, | 101 RequestQuotaDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, |
102 int request_id, | 102 int request_id, |
103 const GURL& origin, | 103 const GURL& origin, |
104 StorageType type, | 104 StorageType type, |
105 int64 requested_quota, | 105 int64 requested_quota, |
106 int render_view_id) | 106 int render_view_id) |
107 : RequestDispatcher(dispatcher_host, request_id), | 107 : RequestDispatcher(dispatcher_host, request_id), |
108 origin_(origin), | 108 origin_(origin), |
109 host_(net::GetHostOrSpecFromURL(origin)), | |
110 type_(type), | 109 type_(type), |
| 110 current_usage_(0), |
111 current_quota_(0), | 111 current_quota_(0), |
112 requested_quota_(requested_quota), | 112 requested_quota_(requested_quota), |
113 render_view_id_(render_view_id), | 113 render_view_id_(render_view_id), |
114 weak_factory_(this) {} | 114 weak_factory_(this) {} |
115 virtual ~RequestQuotaDispatcher() {} | 115 virtual ~RequestQuotaDispatcher() {} |
116 | 116 |
117 void Start() { | 117 void Start() { |
118 DCHECK(dispatcher_host()); | 118 DCHECK(dispatcher_host()); |
| 119 if (requested_quota_ < 0) { |
| 120 DidFinish(quota::kQuotaErrorInvalidModification, 0, 0); |
| 121 return; |
| 122 } |
| 123 |
119 DCHECK(type_ == quota::kStorageTypeTemporary || | 124 DCHECK(type_ == quota::kStorageTypeTemporary || |
120 type_ == quota::kStorageTypePersistent || | 125 type_ == quota::kStorageTypePersistent || |
121 type_ == quota::kStorageTypeSyncable); | 126 type_ == quota::kStorageTypeSyncable); |
122 if (type_ == quota::kStorageTypePersistent) { | 127 if (type_ == quota::kStorageTypePersistent) { |
123 quota_manager()->GetPersistentHostQuota( | 128 quota_manager()->GetUsageAndQuotaForWebApps( |
124 host_, | 129 origin_, type_, |
125 base::Bind(&self_type::DidGetHostQuota, | 130 base::Bind(&self_type::DidGetPersistentUsageAndQuota, |
126 weak_factory_.GetWeakPtr(), host_, type_)); | 131 weak_factory_.GetWeakPtr())); |
127 } else { | 132 } else { |
128 quota_manager()->GetUsageAndQuotaForWebApps( | 133 quota_manager()->GetUsageAndQuotaForWebApps( |
129 origin_, type_, | 134 origin_, type_, |
130 base::Bind(&self_type::DidGetTemporaryUsageAndQuota, | 135 base::Bind(&self_type::DidGetTemporaryUsageAndQuota, |
131 weak_factory_.GetWeakPtr())); | 136 weak_factory_.GetWeakPtr())); |
132 } | 137 } |
133 } | 138 } |
134 | 139 |
135 private: | 140 private: |
136 void DidGetHostQuota(const std::string& host, | 141 void DidGetPersistentUsageAndQuota(QuotaStatusCode status, |
137 StorageType type, | 142 int64 usage, |
138 QuotaStatusCode status, | 143 int64 quota) { |
139 int64 quota) { | |
140 if (!dispatcher_host()) | 144 if (!dispatcher_host()) |
141 return; | 145 return; |
142 DCHECK_EQ(type_, type); | |
143 DCHECK_EQ(host_, host); | |
144 if (status != quota::kQuotaStatusOk) { | 146 if (status != quota::kQuotaStatusOk) { |
145 DidFinish(status, 0); | 147 DidFinish(status, 0, 0); |
146 return; | 148 return; |
147 } | 149 } |
148 if (requested_quota_ < 0) { | 150 |
149 DidFinish(quota::kQuotaErrorInvalidModification, 0); | 151 if (quota_manager()->IsStorageUnlimited(origin_, type_) || |
| 152 requested_quota_ <= quota) { |
| 153 // Seems like we can just let it go. |
| 154 DidFinish(quota::kQuotaStatusOk, usage, requested_quota_); |
150 return; | 155 return; |
151 } | 156 } |
152 if (requested_quota_ <= quota) { | 157 current_usage_ = usage; |
153 // Seems like we can just let it go. | |
154 DidFinish(quota::kQuotaStatusOk, requested_quota_); | |
155 return; | |
156 } | |
157 current_quota_ = quota; | 158 current_quota_ = quota; |
| 159 |
158 // Otherwise we need to consult with the permission context and | 160 // Otherwise we need to consult with the permission context and |
159 // possibly show an infobar. | 161 // possibly show an infobar. |
160 DCHECK(permission_context()); | 162 DCHECK(permission_context()); |
161 permission_context()->RequestQuotaPermission( | 163 permission_context()->RequestQuotaPermission( |
162 origin_, type_, requested_quota_, render_process_id(), render_view_id_, | 164 origin_, type_, requested_quota_, render_process_id(), render_view_id_, |
163 base::Bind(&self_type::DidGetPermissionResponse, | 165 base::Bind(&self_type::DidGetPermissionResponse, |
164 weak_factory_.GetWeakPtr())); | 166 weak_factory_.GetWeakPtr())); |
165 } | 167 } |
166 | 168 |
167 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status, | 169 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status, |
168 int64 usage_unused, | 170 int64 usage, |
169 int64 quota) { | 171 int64 quota) { |
170 DidFinish(status, std::min(requested_quota_, quota)); | 172 DidFinish(status, usage, std::min(requested_quota_, quota)); |
171 } | 173 } |
172 | 174 |
173 void DidGetPermissionResponse( | 175 void DidGetPermissionResponse( |
174 QuotaPermissionContext::QuotaPermissionResponse response) { | 176 QuotaPermissionContext::QuotaPermissionResponse response) { |
175 if (!dispatcher_host()) | 177 if (!dispatcher_host()) |
176 return; | 178 return; |
177 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) { | 179 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) { |
178 // User didn't allow the new quota. Just returning the current quota. | 180 // User didn't allow the new quota. Just returning the current quota. |
179 DidFinish(quota::kQuotaStatusOk, current_quota_); | 181 DidFinish(quota::kQuotaStatusOk, current_usage_, current_quota_); |
180 return; | 182 return; |
181 } | 183 } |
182 // Now we're allowed to set the new quota. | 184 // Now we're allowed to set the new quota. |
183 quota_manager()->SetPersistentHostQuota( | 185 quota_manager()->SetPersistentHostQuota( |
184 host_, requested_quota_, | 186 net::GetHostOrSpecFromURL(origin_), requested_quota_, |
185 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr())); | 187 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr())); |
186 } | 188 } |
187 | 189 |
188 void DidSetHostQuota(QuotaStatusCode status, int64 new_quota) { | 190 void DidSetHostQuota(QuotaStatusCode status, int64 new_quota) { |
189 DidFinish(status, new_quota); | 191 DidFinish(status, current_usage_, new_quota); |
190 } | 192 } |
191 | 193 |
192 void DidFinish(QuotaStatusCode status, int64 granted_quota) { | 194 void DidFinish(QuotaStatusCode status, |
| 195 int64 usage, |
| 196 int64 granted_quota) { |
193 if (!dispatcher_host()) | 197 if (!dispatcher_host()) |
194 return; | 198 return; |
195 DCHECK(dispatcher_host()); | 199 DCHECK(dispatcher_host()); |
196 if (status != quota::kQuotaStatusOk) { | 200 if (status != quota::kQuotaStatusOk) { |
197 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); | 201 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); |
198 } else { | 202 } else { |
199 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota( | 203 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota( |
200 request_id(), granted_quota)); | 204 request_id(), usage, granted_quota)); |
201 } | 205 } |
202 Completed(); | 206 Completed(); |
203 } | 207 } |
204 | 208 |
205 const GURL origin_; | 209 const GURL origin_; |
206 const std::string host_; | |
207 const StorageType type_; | 210 const StorageType type_; |
| 211 int64 current_usage_; |
208 int64 current_quota_; | 212 int64 current_quota_; |
209 const int64 requested_quota_; | 213 const int64 requested_quota_; |
210 const int render_view_id_; | 214 const int render_view_id_; |
211 base::WeakPtrFactory<self_type> weak_factory_; | 215 base::WeakPtrFactory<self_type> weak_factory_; |
212 }; | 216 }; |
213 | 217 |
214 QuotaDispatcherHost::QuotaDispatcherHost( | 218 QuotaDispatcherHost::QuotaDispatcherHost( |
215 int process_id, | 219 int process_id, |
216 QuotaManager* quota_manager, | 220 QuotaManager* quota_manager, |
217 QuotaPermissionContext* permission_context) | 221 QuotaPermissionContext* permission_context) |
(...skipping 27 matching lines...) Expand all Loading... |
245 weak_factory_.GetWeakPtr(), request_id); | 249 weak_factory_.GetWeakPtr(), request_id); |
246 dispatcher->QueryStorageUsageAndQuota(origin, type); | 250 dispatcher->QueryStorageUsageAndQuota(origin, type); |
247 } | 251 } |
248 | 252 |
249 void QuotaDispatcherHost::OnRequestStorageQuota( | 253 void QuotaDispatcherHost::OnRequestStorageQuota( |
250 int render_view_id, | 254 int render_view_id, |
251 int request_id, | 255 int request_id, |
252 const GURL& origin, | 256 const GURL& origin, |
253 StorageType type, | 257 StorageType type, |
254 int64 requested_size) { | 258 int64 requested_size) { |
255 if (quota_manager_->IsStorageUnlimited(origin, type)) { | |
256 // If the origin is marked 'unlimited' we always just return ok. | |
257 Send(new QuotaMsg_DidGrantStorageQuota(request_id, requested_size)); | |
258 return; | |
259 } | |
260 | |
261 if (type != quota::kStorageTypeTemporary && | 259 if (type != quota::kStorageTypeTemporary && |
262 type != quota::kStorageTypePersistent) { | 260 type != quota::kStorageTypePersistent) { |
263 // Unsupported storage types. | 261 // Unsupported storage types. |
264 Send(new QuotaMsg_DidFail(request_id, quota::kQuotaErrorNotSupported)); | 262 Send(new QuotaMsg_DidFail(request_id, quota::kQuotaErrorNotSupported)); |
265 return; | 263 return; |
266 } | 264 } |
267 | 265 |
268 RequestQuotaDispatcher* dispatcher = new RequestQuotaDispatcher( | 266 RequestQuotaDispatcher* dispatcher = new RequestQuotaDispatcher( |
269 weak_factory_.GetWeakPtr(), request_id, origin, type, | 267 weak_factory_.GetWeakPtr(), request_id, origin, type, |
270 requested_size, render_view_id); | 268 requested_size, render_view_id); |
271 dispatcher->Start(); | 269 dispatcher->Start(); |
272 } | 270 } |
273 | 271 |
274 } // namespace content | 272 } // namespace content |
OLD | NEW |