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