1 // Copyright (c) 2012 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/renderer_host/quota_dispatcher_host.h"
8 #include "base/memory/weak_ptr.h"
9 #include "content/common/quota_messages.h"
10 #include "content/public/browser/quota_permission_context.h"
11 #include "googleurl/src/gurl.h"
12 #include "net/base/net_util.h"
13 #include "webkit/browser/quota/quota_manager.h"
15 using quota::QuotaClient
;
16 using quota::QuotaManager
;
17 using quota::QuotaStatusCode
;
18 using quota::StorageType
;
22 // Created one per request to carry the request's request_id around.
23 // Dispatches requests from renderer/worker to the QuotaManager and
24 // sends back the response to the renderer/worker.
25 class QuotaDispatcherHost::RequestDispatcher
{
27 RequestDispatcher(QuotaDispatcherHost
* dispatcher_host
,
29 : dispatcher_host_(dispatcher_host
),
30 request_id_(request_id
) {
31 dispatcher_host_
->outstanding_requests_
.AddWithID(this, request_id_
);
33 virtual ~RequestDispatcher() {}
36 // Subclass must call this when it's done with the request.
38 dispatcher_host_
->outstanding_requests_
.Remove(request_id_
);
41 QuotaDispatcherHost
* dispatcher_host() const { return dispatcher_host_
; }
42 quota::QuotaManager
* quota_manager() const {
43 return dispatcher_host_
->quota_manager_
;
45 QuotaPermissionContext
* permission_context() const {
46 return dispatcher_host_
->permission_context_
.get();
48 int render_process_id() const { return dispatcher_host_
->process_id_
; }
49 int request_id() const { return request_id_
; }
52 QuotaDispatcherHost
* dispatcher_host_
;
56 class QuotaDispatcherHost::QueryUsageAndQuotaDispatcher
57 : public RequestDispatcher
{
59 QueryUsageAndQuotaDispatcher(
60 QuotaDispatcherHost
* dispatcher_host
,
62 : RequestDispatcher(dispatcher_host
, request_id
),
63 weak_factory_(this) {}
64 virtual ~QueryUsageAndQuotaDispatcher() {}
66 void QueryStorageUsageAndQuota(const GURL
& origin
, StorageType type
) {
67 quota_manager()->GetUsageAndQuotaForWebApps(
69 base::Bind(&QueryUsageAndQuotaDispatcher::DidQueryStorageUsageAndQuota
,
70 weak_factory_
.GetWeakPtr()));
74 void DidQueryStorageUsageAndQuota(
75 QuotaStatusCode status
, int64 usage
, int64 quota
) {
76 DCHECK(dispatcher_host());
77 if (status
!= quota::kQuotaStatusOk
) {
78 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status
));
80 dispatcher_host()->Send(new QuotaMsg_DidQueryStorageUsageAndQuota(
81 request_id(), usage
, quota
));
86 base::WeakPtrFactory
<QueryUsageAndQuotaDispatcher
> weak_factory_
;
89 class QuotaDispatcherHost::RequestQuotaDispatcher
90 : public RequestDispatcher
{
92 typedef RequestQuotaDispatcher self_type
;
94 RequestQuotaDispatcher(QuotaDispatcherHost
* dispatcher_host
,
98 int64 requested_quota
,
100 : RequestDispatcher(dispatcher_host
, request_id
),
102 host_(net::GetHostOrSpecFromURL(origin
)),
105 requested_quota_(requested_quota
),
106 render_view_id_(render_view_id
),
107 weak_factory_(this) {}
108 virtual ~RequestQuotaDispatcher() {}
111 DCHECK(type_
== quota::kStorageTypeTemporary
||
112 type_
== quota::kStorageTypePersistent
||
113 type_
== quota::kStorageTypeSyncable
);
114 if (type_
== quota::kStorageTypePersistent
) {
115 quota_manager()->GetPersistentHostQuota(
117 base::Bind(&self_type::DidGetHostQuota
,
118 weak_factory_
.GetWeakPtr(), host_
, type_
));
120 quota_manager()->GetUsageAndQuotaForWebApps(
122 base::Bind(&self_type::DidGetTemporaryUsageAndQuota
,
123 weak_factory_
.GetWeakPtr()));
128 void DidGetHostQuota(const std::string
& host
,
130 QuotaStatusCode status
,
132 DCHECK_EQ(type_
, type
);
133 DCHECK_EQ(host_
, host
);
134 if (status
!= quota::kQuotaStatusOk
) {
135 DidFinish(status
, 0);
138 if (requested_quota_
< 0) {
139 DidFinish(quota::kQuotaErrorInvalidModification
, 0);
142 if (requested_quota_
<= quota
) {
143 // Seems like we can just let it go.
144 DidFinish(quota::kQuotaStatusOk
, requested_quota_
);
147 current_quota_
= quota
;
148 // Otherwise we need to consult with the permission context and
149 // possibly show an infobar.
150 DCHECK(permission_context());
151 permission_context()->RequestQuotaPermission(
152 origin_
, type_
, requested_quota_
, render_process_id(), render_view_id_
,
153 base::Bind(&self_type::DidGetPermissionResponse
,
154 weak_factory_
.GetWeakPtr()));
157 void DidGetTemporaryUsageAndQuota(QuotaStatusCode status
,
160 DidFinish(status
, std::min(requested_quota_
, quota
));
163 void DidGetPermissionResponse(
164 QuotaPermissionContext::QuotaPermissionResponse response
) {
165 if (response
!= QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW
) {
166 // User didn't allow the new quota. Just returning the current quota.
167 DidFinish(quota::kQuotaStatusOk
, current_quota_
);
170 // Now we're allowed to set the new quota.
171 quota_manager()->SetPersistentHostQuota(
172 host_
, requested_quota_
,
173 base::Bind(&self_type::DidSetHostQuota
, weak_factory_
.GetWeakPtr()));
176 void DidSetHostQuota(QuotaStatusCode status
, int64 new_quota
) {
177 DidFinish(status
, new_quota
);
180 void DidFinish(QuotaStatusCode status
, int64 granted_quota
) {
181 DCHECK(dispatcher_host());
182 if (status
!= quota::kQuotaStatusOk
) {
183 dispatcher_host()->Send(new QuotaMsg_DidFail(request_id(), status
));
185 dispatcher_host()->Send(new QuotaMsg_DidGrantStorageQuota(
186 request_id(), granted_quota
));
192 const std::string host_
;
193 const StorageType type_
;
194 int64 current_quota_
;
195 const int64 requested_quota_
;
196 const int render_view_id_
;
197 base::WeakPtrFactory
<self_type
> weak_factory_
;
200 QuotaDispatcherHost::QuotaDispatcherHost(
202 QuotaManager
* quota_manager
,
203 QuotaPermissionContext
* permission_context
)
204 : process_id_(process_id
),
205 quota_manager_(quota_manager
),
206 permission_context_(permission_context
) {
209 bool QuotaDispatcherHost::OnMessageReceived(
210 const IPC::Message
& message
, bool* message_was_ok
) {
211 *message_was_ok
= true;
213 IPC_BEGIN_MESSAGE_MAP_EX(QuotaDispatcherHost
, message
, *message_was_ok
)
214 IPC_MESSAGE_HANDLER(QuotaHostMsg_QueryStorageUsageAndQuota
,
215 OnQueryStorageUsageAndQuota
)
216 IPC_MESSAGE_HANDLER(QuotaHostMsg_RequestStorageQuota
,
217 OnRequestStorageQuota
)
218 IPC_MESSAGE_UNHANDLED(handled
= false)
219 IPC_END_MESSAGE_MAP_EX()
223 QuotaDispatcherHost::~QuotaDispatcherHost() {}
225 void QuotaDispatcherHost::OnQueryStorageUsageAndQuota(
229 QueryUsageAndQuotaDispatcher
* dispatcher
= new QueryUsageAndQuotaDispatcher(
231 dispatcher
->QueryStorageUsageAndQuota(origin
, type
);
234 void QuotaDispatcherHost::OnRequestStorageQuota(
239 int64 requested_size
) {
240 if (quota_manager_
->IsStorageUnlimited(origin
, type
)) {
241 // If the origin is marked 'unlimited' we always just return ok.
242 Send(new QuotaMsg_DidGrantStorageQuota(request_id
, requested_size
));
246 if (type
!= quota::kStorageTypeTemporary
&&
247 type
!= quota::kStorageTypePersistent
) {
248 // Unsupported storage types.
249 Send(new QuotaMsg_DidFail(request_id
, quota::kQuotaErrorNotSupported
));
253 RequestQuotaDispatcher
* dispatcher
= new RequestQuotaDispatcher(
254 this, request_id
, origin
, type
, requested_size
, render_view_id
);
258 } // namespace content