| 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 <stdint.h> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 9 #include "base/numerics/safe_conversions.h" | 11 #include "base/numerics/safe_conversions.h" |
| 10 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
| 11 #include "content/common/quota_messages.h" | 13 #include "content/common/quota_messages.h" |
| 12 #include "content/public/browser/quota_permission_context.h" | 14 #include "content/public/browser/quota_permission_context.h" |
| 13 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
| 14 #include "storage/browser/quota/quota_manager.h" | 16 #include "storage/browser/quota/quota_manager.h" |
| 15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 16 | 18 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 "QuotaDispatcherHost::QueryUsageAndQuotaDispatcher" | 79 "QuotaDispatcherHost::QueryUsageAndQuotaDispatcher" |
| 78 "::QueryStorageUsageAndQuota"); | 80 "::QueryStorageUsageAndQuota"); |
| 79 | 81 |
| 80 quota_manager()->GetUsageAndQuotaForWebApps( | 82 quota_manager()->GetUsageAndQuotaForWebApps( |
| 81 origin, type, | 83 origin, type, |
| 82 base::Bind(&QueryUsageAndQuotaDispatcher::DidQueryStorageUsageAndQuota, | 84 base::Bind(&QueryUsageAndQuotaDispatcher::DidQueryStorageUsageAndQuota, |
| 83 weak_factory_.GetWeakPtr())); | 85 weak_factory_.GetWeakPtr())); |
| 84 } | 86 } |
| 85 | 87 |
| 86 private: | 88 private: |
| 87 void DidQueryStorageUsageAndQuota( | 89 void DidQueryStorageUsageAndQuota(QuotaStatusCode status, |
| 88 QuotaStatusCode status, int64 usage, int64 quota) { | 90 int64_t usage, |
| 91 int64_t quota) { |
| 89 if (!dispatcher_host()) | 92 if (!dispatcher_host()) |
| 90 return; | 93 return; |
| 91 // crbug.com/349708 | 94 // crbug.com/349708 |
| 92 TRACE_EVENT0("io", "QuotaDispatcherHost::RequestQuotaDispatcher" | 95 TRACE_EVENT0("io", "QuotaDispatcherHost::RequestQuotaDispatcher" |
| 93 "::DidQueryStorageUsageAndQuota"); | 96 "::DidQueryStorageUsageAndQuota"); |
| 94 | 97 |
| 95 if (status != storage::kQuotaStatusOk) { | 98 if (status != storage::kQuotaStatusOk) { |
| 96 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); | 99 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); |
| 97 } else { | 100 } else { |
| 98 dispatcher_host()->Send(new QuotaMsg_DidQueryStorageUsageAndQuota( | 101 dispatcher_host()->Send(new QuotaMsg_DidQueryStorageUsageAndQuota( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 110 typedef RequestQuotaDispatcher self_type; | 113 typedef RequestQuotaDispatcher self_type; |
| 111 | 114 |
| 112 RequestQuotaDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, | 115 RequestQuotaDispatcher(base::WeakPtr<QuotaDispatcherHost> dispatcher_host, |
| 113 const StorageQuotaParams& params) | 116 const StorageQuotaParams& params) |
| 114 : RequestDispatcher(dispatcher_host, params.request_id), | 117 : RequestDispatcher(dispatcher_host, params.request_id), |
| 115 params_(params), | 118 params_(params), |
| 116 current_usage_(0), | 119 current_usage_(0), |
| 117 current_quota_(0), | 120 current_quota_(0), |
| 118 requested_quota_(0), | 121 requested_quota_(0), |
| 119 weak_factory_(this) { | 122 weak_factory_(this) { |
| 120 // Convert the requested size from uint64 to int64 since the quota backend | 123 // Convert the requested size from uint64_t to int64_t since the quota |
| 121 // requires int64 values. | 124 // backend |
| 122 // TODO(nhiroki): The backend should accept uint64 values. | 125 // requires int64_t values. |
| 123 requested_quota_ = base::saturated_cast<int64>(params_.requested_size); | 126 // TODO(nhiroki): The backend should accept uint64_t values. |
| 127 requested_quota_ = base::saturated_cast<int64_t>(params_.requested_size); |
| 124 } | 128 } |
| 125 ~RequestQuotaDispatcher() override {} | 129 ~RequestQuotaDispatcher() override {} |
| 126 | 130 |
| 127 void Start() { | 131 void Start() { |
| 128 DCHECK(dispatcher_host()); | 132 DCHECK(dispatcher_host()); |
| 129 // crbug.com/349708 | 133 // crbug.com/349708 |
| 130 TRACE_EVENT0("io", "QuotaDispatcherHost::RequestQuotaDispatcher::Start"); | 134 TRACE_EVENT0("io", "QuotaDispatcherHost::RequestQuotaDispatcher::Start"); |
| 131 | 135 |
| 132 DCHECK(params_.storage_type == storage::kStorageTypeTemporary || | 136 DCHECK(params_.storage_type == storage::kStorageTypeTemporary || |
| 133 params_.storage_type == storage::kStorageTypePersistent); | 137 params_.storage_type == storage::kStorageTypePersistent); |
| 134 if (params_.storage_type == storage::kStorageTypePersistent) { | 138 if (params_.storage_type == storage::kStorageTypePersistent) { |
| 135 quota_manager()->GetUsageAndQuotaForWebApps( | 139 quota_manager()->GetUsageAndQuotaForWebApps( |
| 136 params_.origin_url, params_.storage_type, | 140 params_.origin_url, params_.storage_type, |
| 137 base::Bind(&self_type::DidGetPersistentUsageAndQuota, | 141 base::Bind(&self_type::DidGetPersistentUsageAndQuota, |
| 138 weak_factory_.GetWeakPtr())); | 142 weak_factory_.GetWeakPtr())); |
| 139 } else { | 143 } else { |
| 140 quota_manager()->GetUsageAndQuotaForWebApps( | 144 quota_manager()->GetUsageAndQuotaForWebApps( |
| 141 params_.origin_url, params_.storage_type, | 145 params_.origin_url, params_.storage_type, |
| 142 base::Bind(&self_type::DidGetTemporaryUsageAndQuota, | 146 base::Bind(&self_type::DidGetTemporaryUsageAndQuota, |
| 143 weak_factory_.GetWeakPtr())); | 147 weak_factory_.GetWeakPtr())); |
| 144 } | 148 } |
| 145 } | 149 } |
| 146 | 150 |
| 147 private: | 151 private: |
| 148 void DidGetPersistentUsageAndQuota(QuotaStatusCode status, | 152 void DidGetPersistentUsageAndQuota(QuotaStatusCode status, |
| 149 int64 usage, | 153 int64_t usage, |
| 150 int64 quota) { | 154 int64_t quota) { |
| 151 if (!dispatcher_host()) | 155 if (!dispatcher_host()) |
| 152 return; | 156 return; |
| 153 if (status != storage::kQuotaStatusOk) { | 157 if (status != storage::kQuotaStatusOk) { |
| 154 DidFinish(status, 0, 0); | 158 DidFinish(status, 0, 0); |
| 155 return; | 159 return; |
| 156 } | 160 } |
| 157 | 161 |
| 158 if (quota_manager()->IsStorageUnlimited(params_.origin_url, | 162 if (quota_manager()->IsStorageUnlimited(params_.origin_url, |
| 159 params_.storage_type) || | 163 params_.storage_type) || |
| 160 requested_quota_ <= quota) { | 164 requested_quota_ <= quota) { |
| 161 // Seems like we can just let it go. | 165 // Seems like we can just let it go. |
| 162 DidFinish(storage::kQuotaStatusOk, usage, params_.requested_size); | 166 DidFinish(storage::kQuotaStatusOk, usage, params_.requested_size); |
| 163 return; | 167 return; |
| 164 } | 168 } |
| 165 current_usage_ = usage; | 169 current_usage_ = usage; |
| 166 current_quota_ = quota; | 170 current_quota_ = quota; |
| 167 | 171 |
| 168 // Otherwise we need to consult with the permission context and | 172 // Otherwise we need to consult with the permission context and |
| 169 // possibly show a prompt. | 173 // possibly show a prompt. |
| 170 DCHECK(permission_context()); | 174 DCHECK(permission_context()); |
| 171 permission_context()->RequestQuotaPermission(params_, render_process_id(), | 175 permission_context()->RequestQuotaPermission(params_, render_process_id(), |
| 172 base::Bind(&self_type::DidGetPermissionResponse, | 176 base::Bind(&self_type::DidGetPermissionResponse, |
| 173 weak_factory_.GetWeakPtr())); | 177 weak_factory_.GetWeakPtr())); |
| 174 } | 178 } |
| 175 | 179 |
| 176 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status, | 180 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status, |
| 177 int64 usage, | 181 int64_t usage, |
| 178 int64 quota) { | 182 int64_t quota) { |
| 179 DidFinish(status, usage, std::min(requested_quota_, quota)); | 183 DidFinish(status, usage, std::min(requested_quota_, quota)); |
| 180 } | 184 } |
| 181 | 185 |
| 182 void DidGetPermissionResponse( | 186 void DidGetPermissionResponse( |
| 183 QuotaPermissionContext::QuotaPermissionResponse response) { | 187 QuotaPermissionContext::QuotaPermissionResponse response) { |
| 184 if (!dispatcher_host()) | 188 if (!dispatcher_host()) |
| 185 return; | 189 return; |
| 186 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) { | 190 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) { |
| 187 // User didn't allow the new quota. Just returning the current quota. | 191 // User didn't allow the new quota. Just returning the current quota. |
| 188 DidFinish(storage::kQuotaStatusOk, current_usage_, current_quota_); | 192 DidFinish(storage::kQuotaStatusOk, current_usage_, current_quota_); |
| 189 return; | 193 return; |
| 190 } | 194 } |
| 191 // Now we're allowed to set the new quota. | 195 // Now we're allowed to set the new quota. |
| 192 quota_manager()->SetPersistentHostQuota( | 196 quota_manager()->SetPersistentHostQuota( |
| 193 net::GetHostOrSpecFromURL(params_.origin_url), params_.requested_size, | 197 net::GetHostOrSpecFromURL(params_.origin_url), params_.requested_size, |
| 194 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr())); | 198 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr())); |
| 195 } | 199 } |
| 196 | 200 |
| 197 void DidSetHostQuota(QuotaStatusCode status, int64 new_quota) { | 201 void DidSetHostQuota(QuotaStatusCode status, int64_t new_quota) { |
| 198 DidFinish(status, current_usage_, new_quota); | 202 DidFinish(status, current_usage_, new_quota); |
| 199 } | 203 } |
| 200 | 204 |
| 201 void DidFinish(QuotaStatusCode status, | 205 void DidFinish(QuotaStatusCode status, int64_t usage, int64_t granted_quota) { |
| 202 int64 usage, | |
| 203 int64 granted_quota) { | |
| 204 if (!dispatcher_host()) | 206 if (!dispatcher_host()) |
| 205 return; | 207 return; |
| 206 DCHECK(dispatcher_host()); | 208 DCHECK(dispatcher_host()); |
| 207 if (status != storage::kQuotaStatusOk) { | 209 if (status != storage::kQuotaStatusOk) { |
| 208 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); | 210 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); |
| 209 } else { | 211 } else { |
| 210 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota( | 212 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota( |
| 211 request_id(), usage, granted_quota)); | 213 request_id(), usage, granted_quota)); |
| 212 } | 214 } |
| 213 Completed(); | 215 Completed(); |
| 214 } | 216 } |
| 215 | 217 |
| 216 StorageQuotaParams params_; | 218 StorageQuotaParams params_; |
| 217 int64 current_usage_; | 219 int64_t current_usage_; |
| 218 int64 current_quota_; | 220 int64_t current_quota_; |
| 219 int64 requested_quota_; | 221 int64_t requested_quota_; |
| 220 base::WeakPtrFactory<self_type> weak_factory_; | 222 base::WeakPtrFactory<self_type> weak_factory_; |
| 221 }; | 223 }; |
| 222 | 224 |
| 223 QuotaDispatcherHost::QuotaDispatcherHost( | 225 QuotaDispatcherHost::QuotaDispatcherHost( |
| 224 int process_id, | 226 int process_id, |
| 225 QuotaManager* quota_manager, | 227 QuotaManager* quota_manager, |
| 226 QuotaPermissionContext* permission_context) | 228 QuotaPermissionContext* permission_context) |
| 227 : BrowserMessageFilter(QuotaMsgStart), | 229 : BrowserMessageFilter(QuotaMsgStart), |
| 228 process_id_(process_id), | 230 process_id_(process_id), |
| 229 quota_manager_(quota_manager), | 231 quota_manager_(quota_manager), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 return; | 266 return; |
| 265 } | 267 } |
| 266 | 268 |
| 267 RequestQuotaDispatcher* dispatcher = | 269 RequestQuotaDispatcher* dispatcher = |
| 268 new RequestQuotaDispatcher(weak_factory_.GetWeakPtr(), | 270 new RequestQuotaDispatcher(weak_factory_.GetWeakPtr(), |
| 269 params); | 271 params); |
| 270 dispatcher->Start(); | 272 dispatcher->Start(); |
| 271 } | 273 } |
| 272 | 274 |
| 273 } // namespace content | 275 } // namespace content |
| OLD | NEW |