1 // Copyright (c) 2011 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/child/quota_dispatcher.h"
7 #include "base/basictypes.h"
8 #include "content/child/child_thread.h"
9 #include "content/common/quota_messages.h"
10 #include "googleurl/src/gurl.h"
11 #include "third_party/WebKit/public/web/WebStorageQuotaCallbacks.h"
12 #include "third_party/WebKit/public/web/WebStorageQuotaType.h"
14 using quota::QuotaStatusCode
;
15 using quota::StorageType
;
17 using WebKit::WebStorageQuotaCallbacks
;
18 using WebKit::WebStorageQuotaError
;
19 using WebKit::WebStorageQuotaType
;
24 // QuotaDispatcher::Callback implementation for WebStorageQuotaCallbacks.
25 class WebStorageQuotaDispatcherCallback
: public QuotaDispatcher::Callback
{
27 WebStorageQuotaDispatcherCallback(WebKit::WebStorageQuotaCallbacks
* callback
)
28 : callbacks_(callback
) {
31 virtual ~WebStorageQuotaDispatcherCallback() {}
32 virtual void DidQueryStorageUsageAndQuota(int64 usage
, int64 quota
) OVERRIDE
{
33 callbacks_
->didQueryStorageUsageAndQuota(usage
, quota
);
35 virtual void DidGrantStorageQuota(int64 granted_quota
) OVERRIDE
{
36 callbacks_
->didGrantStorageQuota(granted_quota
);
38 virtual void DidFail(quota::QuotaStatusCode error
) OVERRIDE
{
39 callbacks_
->didFail(static_cast<WebStorageQuotaError
>(error
));
43 // Not owned (self-destructed).
44 WebKit::WebStorageQuotaCallbacks
* callbacks_
;
49 QuotaDispatcher::QuotaDispatcher() {
52 QuotaDispatcher::~QuotaDispatcher() {
53 IDMap
<Callback
, IDMapOwnPointer
>::iterator
iter(&pending_quota_callbacks_
);
54 while (!iter
.IsAtEnd()) {
55 iter
.GetCurrentValue()->DidFail(quota::kQuotaErrorAbort
);
60 bool QuotaDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
62 IPC_BEGIN_MESSAGE_MAP(QuotaDispatcher
, msg
)
63 IPC_MESSAGE_HANDLER(QuotaMsg_DidGrantStorageQuota
,
65 IPC_MESSAGE_HANDLER(QuotaMsg_DidQueryStorageUsageAndQuota
,
66 DidQueryStorageUsageAndQuota
);
67 IPC_MESSAGE_HANDLER(QuotaMsg_DidFail
, DidFail
);
68 IPC_MESSAGE_UNHANDLED(handled
= false)
73 void QuotaDispatcher::QueryStorageUsageAndQuota(
74 const GURL
& origin_url
,
78 int request_id
= pending_quota_callbacks_
.Add(callback
);
79 ChildThread::current()->Send(new QuotaHostMsg_QueryStorageUsageAndQuota(
80 request_id
, origin_url
, type
));
83 void QuotaDispatcher::RequestStorageQuota(
85 const GURL
& origin_url
,
90 int request_id
= pending_quota_callbacks_
.Add(callback
);
91 ChildThread::current()->Send(new QuotaHostMsg_RequestStorageQuota(
92 render_view_id
, request_id
, origin_url
, type
, requested_size
));
96 QuotaDispatcher::Callback
*
97 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(
98 WebKit::WebStorageQuotaCallbacks
* callbacks
) {
99 return new WebStorageQuotaDispatcherCallback(callbacks
);
102 void QuotaDispatcher::DidGrantStorageQuota(
104 int64 granted_quota
) {
105 Callback
* callback
= pending_quota_callbacks_
.Lookup(request_id
);
107 callback
->DidGrantStorageQuota(granted_quota
);
108 pending_quota_callbacks_
.Remove(request_id
);
111 void QuotaDispatcher::DidQueryStorageUsageAndQuota(
114 int64 current_quota
) {
115 Callback
* callback
= pending_quota_callbacks_
.Lookup(request_id
);
117 callback
->DidQueryStorageUsageAndQuota(current_usage
, current_quota
);
118 pending_quota_callbacks_
.Remove(request_id
);
121 void QuotaDispatcher::DidFail(
123 QuotaStatusCode error
) {
124 Callback
* callback
= pending_quota_callbacks_
.Lookup(request_id
);
126 callback
->DidFail(error
);
127 pending_quota_callbacks_
.Remove(request_id
);
130 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypeTemporary
) == \
131 int(quota::kStorageTypeTemporary
), mismatching_enums
);
132 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypePersistent
) == \
133 int(quota::kStorageTypePersistent
), mismatching_enums
);
135 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorNotSupported
) == \
136 int(quota::kQuotaErrorNotSupported
), mismatching_enums
);
137 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorAbort
) == \
138 int(quota::kQuotaErrorAbort
), mismatching_enums
);
140 } // namespace content