Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: content/browser/quota_dispatcher_host.cc

Issue 185793004: [Quota] Pass user gesture state with quota request message. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 : RequestDispatcher(dispatcher_host, params.request_id),
105 StorageType type, 106 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), 107 current_usage_(0),
112 current_quota_(0), 108 current_quota_(0),
113 requested_quota_(0), 109 requested_quota_(0),
114 render_view_id_(render_view_id),
115 weak_factory_(this) { 110 weak_factory_(this) {
116 // Convert the requested size from uint64 to int64 since the quota backend 111 // Convert the requested size from uint64 to int64 since the quota backend
117 // requires int64 values. 112 // requires int64 values.
118 // TODO(nhiroki): The backend should accept uint64 values. 113 // TODO(nhiroki): The backend should accept uint64 values.
119 requested_quota_ = base::saturated_cast<int64>(requested_quota); 114 requested_quota_ = base::saturated_cast<int64>(params_.requested_size);
120 } 115 }
121 virtual ~RequestQuotaDispatcher() {} 116 virtual ~RequestQuotaDispatcher() {}
122 117
123 void Start() { 118 void Start() {
124 DCHECK(dispatcher_host()); 119 DCHECK(dispatcher_host());
125 120
126 DCHECK(type_ == quota::kStorageTypeTemporary || 121 DCHECK(params_.storage_type == quota::kStorageTypeTemporary ||
127 type_ == quota::kStorageTypePersistent || 122 params_.storage_type == quota::kStorageTypePersistent ||
128 type_ == quota::kStorageTypeSyncable); 123 params_.storage_type == quota::kStorageTypeSyncable);
129 if (type_ == quota::kStorageTypePersistent) { 124 if (params_.storage_type == quota::kStorageTypePersistent) {
130 quota_manager()->GetUsageAndQuotaForWebApps( 125 quota_manager()->GetUsageAndQuotaForWebApps(
131 origin_, type_, 126 params_.origin_url, params_.storage_type,
132 base::Bind(&self_type::DidGetPersistentUsageAndQuota, 127 base::Bind(&self_type::DidGetPersistentUsageAndQuota,
133 weak_factory_.GetWeakPtr())); 128 weak_factory_.GetWeakPtr()));
134 } else { 129 } else {
135 quota_manager()->GetUsageAndQuotaForWebApps( 130 quota_manager()->GetUsageAndQuotaForWebApps(
136 origin_, type_, 131 params_.origin_url, params_.storage_type,
137 base::Bind(&self_type::DidGetTemporaryUsageAndQuota, 132 base::Bind(&self_type::DidGetTemporaryUsageAndQuota,
138 weak_factory_.GetWeakPtr())); 133 weak_factory_.GetWeakPtr()));
139 } 134 }
140 } 135 }
141 136
142 private: 137 private:
143 void DidGetPersistentUsageAndQuota(QuotaStatusCode status, 138 void DidGetPersistentUsageAndQuota(QuotaStatusCode status,
144 int64 usage, 139 int64 usage,
145 int64 quota) { 140 int64 quota) {
146 if (!dispatcher_host()) 141 if (!dispatcher_host())
147 return; 142 return;
148 if (status != quota::kQuotaStatusOk) { 143 if (status != quota::kQuotaStatusOk) {
149 DidFinish(status, 0, 0); 144 DidFinish(status, 0, 0);
150 return; 145 return;
151 } 146 }
152 147
153 if (quota_manager()->IsStorageUnlimited(origin_, type_) || 148 if (quota_manager()->IsStorageUnlimited(params_.origin_url,
149 params_.storage_type) ||
154 requested_quota_ <= quota) { 150 requested_quota_ <= quota) {
155 // Seems like we can just let it go. 151 // Seems like we can just let it go.
156 DidFinish(quota::kQuotaStatusOk, usage, requested_quota_); 152 DidFinish(quota::kQuotaStatusOk, usage, params_.requested_size);
157 return; 153 return;
158 } 154 }
159 current_usage_ = usage; 155 current_usage_ = usage;
160 current_quota_ = quota; 156 current_quota_ = quota;
161 157
162 // Otherwise we need to consult with the permission context and 158 // Otherwise we need to consult with the permission context and
163 // possibly show an infobar. 159 // possibly show a prompt.
164 DCHECK(permission_context()); 160 DCHECK(permission_context());
165 permission_context()->RequestQuotaPermission( 161 permission_context()->RequestQuotaPermission(params_, render_process_id(),
166 origin_, type_, requested_quota_, render_process_id(), render_view_id_,
167 base::Bind(&self_type::DidGetPermissionResponse, 162 base::Bind(&self_type::DidGetPermissionResponse,
168 weak_factory_.GetWeakPtr())); 163 weak_factory_.GetWeakPtr()));
169 } 164 }
170 165
171 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status, 166 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status,
172 int64 usage, 167 int64 usage,
173 int64 quota) { 168 int64 quota) {
174 DidFinish(status, usage, std::min(requested_quota_, quota)); 169 DidFinish(status, usage, std::min(requested_quota_, quota));
175 } 170 }
176 171
177 void DidGetPermissionResponse( 172 void DidGetPermissionResponse(
178 QuotaPermissionContext::QuotaPermissionResponse response) { 173 QuotaPermissionContext::QuotaPermissionResponse response) {
179 if (!dispatcher_host()) 174 if (!dispatcher_host())
180 return; 175 return;
181 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) { 176 if (response != QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW) {
182 // User didn't allow the new quota. Just returning the current quota. 177 // User didn't allow the new quota. Just returning the current quota.
183 DidFinish(quota::kQuotaStatusOk, current_usage_, current_quota_); 178 DidFinish(quota::kQuotaStatusOk, current_usage_, current_quota_);
184 return; 179 return;
185 } 180 }
186 // Now we're allowed to set the new quota. 181 // Now we're allowed to set the new quota.
187 quota_manager()->SetPersistentHostQuota( 182 quota_manager()->SetPersistentHostQuota(
188 net::GetHostOrSpecFromURL(origin_), requested_quota_, 183 net::GetHostOrSpecFromURL(params_.origin_url), params_.requested_size,
189 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr())); 184 base::Bind(&self_type::DidSetHostQuota, weak_factory_.GetWeakPtr()));
190 } 185 }
191 186
192 void DidSetHostQuota(QuotaStatusCode status, int64 new_quota) { 187 void DidSetHostQuota(QuotaStatusCode status, int64 new_quota) {
193 DidFinish(status, current_usage_, new_quota); 188 DidFinish(status, current_usage_, new_quota);
194 } 189 }
195 190
196 void DidFinish(QuotaStatusCode status, 191 void DidFinish(QuotaStatusCode status,
197 int64 usage, 192 int64 usage,
198 int64 granted_quota) { 193 int64 granted_quota) {
199 if (!dispatcher_host()) 194 if (!dispatcher_host())
200 return; 195 return;
201 DCHECK(dispatcher_host()); 196 DCHECK(dispatcher_host());
202 if (status != quota::kQuotaStatusOk) { 197 if (status != quota::kQuotaStatusOk) {
203 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status)); 198 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status));
204 } else { 199 } else {
205 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota( 200 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota(
206 request_id(), usage, granted_quota)); 201 request_id(), usage, granted_quota));
207 } 202 }
208 Completed(); 203 Completed();
209 } 204 }
210 205
211 const GURL origin_; 206 RequestStorageQuotaParams params_;
212 const StorageType type_;
213 int64 current_usage_; 207 int64 current_usage_;
214 int64 current_quota_; 208 int64 current_quota_;
215 int64 requested_quota_; 209 int64 requested_quota_;
216 const int render_view_id_;
217 base::WeakPtrFactory<self_type> weak_factory_; 210 base::WeakPtrFactory<self_type> weak_factory_;
218 }; 211 };
219 212
220 QuotaDispatcherHost::QuotaDispatcherHost( 213 QuotaDispatcherHost::QuotaDispatcherHost(
221 int process_id, 214 int process_id,
222 QuotaManager* quota_manager, 215 QuotaManager* quota_manager,
223 QuotaPermissionContext* permission_context) 216 QuotaPermissionContext* permission_context)
224 : BrowserMessageFilter(QuotaMsgStart), 217 : BrowserMessageFilter(QuotaMsgStart),
225 process_id_(process_id), 218 process_id_(process_id),
226 quota_manager_(quota_manager), 219 quota_manager_(quota_manager),
(...skipping 20 matching lines...) Expand all
247 void QuotaDispatcherHost::OnQueryStorageUsageAndQuota( 240 void QuotaDispatcherHost::OnQueryStorageUsageAndQuota(
248 int request_id, 241 int request_id,
249 const GURL& origin, 242 const GURL& origin,
250 StorageType type) { 243 StorageType type) {
251 QueryUsageAndQuotaDispatcher* dispatcher = new QueryUsageAndQuotaDispatcher( 244 QueryUsageAndQuotaDispatcher* dispatcher = new QueryUsageAndQuotaDispatcher(
252 weak_factory_.GetWeakPtr(), request_id); 245 weak_factory_.GetWeakPtr(), request_id);
253 dispatcher->QueryStorageUsageAndQuota(origin, type); 246 dispatcher->QueryStorageUsageAndQuota(origin, type);
254 } 247 }
255 248
256 void QuotaDispatcherHost::OnRequestStorageQuota( 249 void QuotaDispatcherHost::OnRequestStorageQuota(
257 int render_view_id, 250 const RequestStorageQuotaParams& params) {
258 int request_id, 251 if (params.storage_type != quota::kStorageTypeTemporary &&
259 const GURL& origin, 252 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. 253 // Unsupported storage types.
265 Send(new QuotaMsg_DidFail(request_id, quota::kQuotaErrorNotSupported)); 254 Send(new QuotaMsg_DidFail(params.request_id,
255 quota::kQuotaErrorNotSupported));
266 return; 256 return;
267 } 257 }
268 258
269 RequestQuotaDispatcher* dispatcher = new RequestQuotaDispatcher( 259 RequestQuotaDispatcher* dispatcher =
270 weak_factory_.GetWeakPtr(), request_id, origin, type, 260 new RequestQuotaDispatcher(weak_factory_.GetWeakPtr(),
271 requested_size, render_view_id); 261 params);
272 dispatcher->Start(); 262 dispatcher->Start();
273 } 263 }
274 264
275 } // namespace content 265 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698