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
3 // found in the LICENSE file.
5 #include "content/browser/quota_dispatcher_host.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "content/common/quota_messages.h"
11 #include "content/public/browser/quota_permission_context.h"
12 #include "net/base/net_util.h"
13 #include "storage/browser/quota/quota_manager.h"
16 using storage::QuotaClient
;
17 using storage::QuotaManager
;
18 using storage::QuotaStatusCode
;
19 using storage::StorageType
;
23 // Created one per request to carry the request's request_id around.
24 // Dispatches requests from renderer/worker to the QuotaManager and
25 // sends back the response to the renderer/worker.
26 class QuotaDispatcherHost::RequestDispatcher
{
28 RequestDispatcher(base::WeakPtr
<QuotaDispatcherHost
> dispatcher_host
,
30 : dispatcher_host_(dispatcher_host
),
31 render_process_id_(dispatcher_host
->process_id_
),
32 request_id_(request_id
) {
33 dispatcher_host_
->outstanding_requests_
.AddWithID(this, request_id_
);
35 virtual ~RequestDispatcher() {}
38 // Subclass must call this when it's done with the request.
41 dispatcher_host_
->outstanding_requests_
.Remove(request_id_
);
44 QuotaDispatcherHost
* dispatcher_host() const {
45 return dispatcher_host_
.get();
47 storage::QuotaManager
* quota_manager() const {
48 return dispatcher_host_
? dispatcher_host_
->quota_manager_
: NULL
;
50 QuotaPermissionContext
* permission_context() const {
51 return dispatcher_host_
?
52 dispatcher_host_
->permission_context_
.get() : NULL
;
54 int render_process_id() const { return render_process_id_
; }
55 int request_id() const { return request_id_
; }
58 base::WeakPtr
<QuotaDispatcherHost
> dispatcher_host_
;
59 int render_process_id_
;
63 class QuotaDispatcherHost::QueryUsageAndQuotaDispatcher
64 : public RequestDispatcher
{
66 QueryUsageAndQuotaDispatcher(
67 base::WeakPtr
<QuotaDispatcherHost
> dispatcher_host
,
69 : RequestDispatcher(dispatcher_host
, request_id
),
70 weak_factory_(this) {}
71 ~QueryUsageAndQuotaDispatcher() override
{}
73 void QueryStorageUsageAndQuota(const GURL
& origin
, StorageType type
) {
74 quota_manager()->GetUsageAndQuotaForWebApps(
76 base::Bind(&QueryUsageAndQuotaDispatcher::DidQueryStorageUsageAndQuota
,
77 weak_factory_
.GetWeakPtr()));
81 void DidQueryStorageUsageAndQuota(
82 QuotaStatusCode status
, int64 usage
, int64 quota
) {
83 if (!dispatcher_host())
85 if (status
!= storage::kQuotaStatusOk
) {
86 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status
));
88 dispatcher_host()->Send(new QuotaMsg_DidQueryStorageUsageAndQuota(
89 request_id(), usage
, quota
));
94 base::WeakPtrFactory
<QueryUsageAndQuotaDispatcher
> weak_factory_
;
97 class QuotaDispatcherHost::RequestQuotaDispatcher
98 : public RequestDispatcher
{
100 typedef RequestQuotaDispatcher self_type
;
102 RequestQuotaDispatcher(base::WeakPtr
<QuotaDispatcherHost
> dispatcher_host
,
103 const StorageQuotaParams
& params
)
104 : RequestDispatcher(dispatcher_host
, params
.request_id
),
109 weak_factory_(this) {
110 // Convert the requested size from uint64 to int64 since the quota backend
111 // requires int64 values.
112 // TODO(nhiroki): The backend should accept uint64 values.
113 requested_quota_
= base::saturated_cast
<int64
>(params_
.requested_size
);
115 ~RequestQuotaDispatcher() override
{}
118 DCHECK(dispatcher_host());
120 DCHECK(params_
.storage_type
== storage::kStorageTypeTemporary
||
121 params_
.storage_type
== storage::kStorageTypePersistent
);
122 if (params_
.storage_type
== storage::kStorageTypePersistent
) {
123 quota_manager()->GetUsageAndQuotaForWebApps(
124 params_
.origin_url
, params_
.storage_type
,
125 base::Bind(&self_type::DidGetPersistentUsageAndQuota
,
126 weak_factory_
.GetWeakPtr()));
128 quota_manager()->GetUsageAndQuotaForWebApps(
129 params_
.origin_url
, params_
.storage_type
,
130 base::Bind(&self_type::DidGetTemporaryUsageAndQuota
,
131 weak_factory_
.GetWeakPtr()));
136 void DidGetPersistentUsageAndQuota(QuotaStatusCode status
,
139 if (!dispatcher_host())
141 if (status
!= storage::kQuotaStatusOk
) {
142 DidFinish(status
, 0, 0);
146 if (quota_manager()->IsStorageUnlimited(params_
.origin_url
,
147 params_
.storage_type
) ||
148 requested_quota_
<= quota
) {
149 // Seems like we can just let it go.
150 DidFinish(storage::kQuotaStatusOk
, usage
, params_
.requested_size
);
153 current_usage_
= usage
;
154 current_quota_
= quota
;
156 // Otherwise we need to consult with the permission context and
157 // possibly show a prompt.
158 DCHECK(permission_context());
159 permission_context()->RequestQuotaPermission(params_
, render_process_id(),
160 base::Bind(&self_type::DidGetPermissionResponse
,
161 weak_factory_
.GetWeakPtr()));
164 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status
,
167 DidFinish(status
, usage
, std::min(requested_quota_
, quota
));
170 void DidGetPermissionResponse(
171 QuotaPermissionContext::QuotaPermissionResponse response
) {
172 if (!dispatcher_host())
174 if (response
!= QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW
) {
175 // User didn't allow the new quota. Just returning the current quota.
176 DidFinish(storage::kQuotaStatusOk
, current_usage_
, current_quota_
);
179 // Now we're allowed to set the new quota.
180 quota_manager()->SetPersistentHostQuota(
181 net::GetHostOrSpecFromURL(params_
.origin_url
), params_
.requested_size
,
182 base::Bind(&self_type::DidSetHostQuota
, weak_factory_
.GetWeakPtr()));
185 void DidSetHostQuota(QuotaStatusCode status
, int64 new_quota
) {
186 DidFinish(status
, current_usage_
, new_quota
);
189 void DidFinish(QuotaStatusCode status
,
191 int64 granted_quota
) {
192 if (!dispatcher_host())
194 DCHECK(dispatcher_host());
195 if (status
!= storage::kQuotaStatusOk
) {
196 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status
));
198 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota(
199 request_id(), usage
, granted_quota
));
204 StorageQuotaParams params_
;
205 int64 current_usage_
;
206 int64 current_quota_
;
207 int64 requested_quota_
;
208 base::WeakPtrFactory
<self_type
> weak_factory_
;
211 QuotaDispatcherHost::QuotaDispatcherHost(
213 QuotaManager
* quota_manager
,
214 QuotaPermissionContext
* permission_context
)
215 : BrowserMessageFilter(QuotaMsgStart
),
216 process_id_(process_id
),
217 quota_manager_(quota_manager
),
218 permission_context_(permission_context
),
219 weak_factory_(this) {
222 bool QuotaDispatcherHost::OnMessageReceived(const IPC::Message
& message
) {
224 IPC_BEGIN_MESSAGE_MAP(QuotaDispatcherHost
, message
)
225 IPC_MESSAGE_HANDLER(QuotaHostMsg_QueryStorageUsageAndQuota
,
226 OnQueryStorageUsageAndQuota
)
227 IPC_MESSAGE_HANDLER(QuotaHostMsg_RequestStorageQuota
,
228 OnRequestStorageQuota
)
229 IPC_MESSAGE_UNHANDLED(handled
= false)
230 IPC_END_MESSAGE_MAP()
234 QuotaDispatcherHost::~QuotaDispatcherHost() {}
236 void QuotaDispatcherHost::OnQueryStorageUsageAndQuota(
240 QueryUsageAndQuotaDispatcher
* dispatcher
= new QueryUsageAndQuotaDispatcher(
241 weak_factory_
.GetWeakPtr(), request_id
);
242 dispatcher
->QueryStorageUsageAndQuota(origin
, type
);
245 void QuotaDispatcherHost::OnRequestStorageQuota(
246 const StorageQuotaParams
& params
) {
247 if (params
.storage_type
!= storage::kStorageTypeTemporary
&&
248 params
.storage_type
!= storage::kStorageTypePersistent
) {
249 // Unsupported storage types.
250 Send(new QuotaMsg_DidFail(params
.request_id
,
251 storage::kQuotaErrorNotSupported
));
255 RequestQuotaDispatcher
* dispatcher
=
256 new RequestQuotaDispatcher(weak_factory_
.GetWeakPtr(),
261 } // namespace content