OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/common/quota_dispatcher.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "content/common/child_thread.h" | |
9 #include "content/common/quota_messages.h" | |
10 #include "googleurl/src/gurl.h" | |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageQuotaCallba
cks.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageQuotaType.h
" | |
13 | |
14 using quota::QuotaStatusCode; | |
15 using quota::StorageType; | |
16 | |
17 using WebKit::WebStorageQuotaCallbacks; | |
18 using WebKit::WebStorageQuotaError; | |
19 using WebKit::WebStorageQuotaType; | |
20 | |
21 namespace content { | |
22 namespace { | |
23 | |
24 // QuotaDispatcher::Callback implementation for WebStorageQuotaCallbacks. | |
25 class WebStorageQuotaDispatcherCallback : public QuotaDispatcher::Callback { | |
26 public: | |
27 WebStorageQuotaDispatcherCallback(WebKit::WebStorageQuotaCallbacks* callback) | |
28 : callbacks_(callback) { | |
29 DCHECK(callbacks_); | |
30 } | |
31 virtual ~WebStorageQuotaDispatcherCallback() {} | |
32 virtual void DidQueryStorageUsageAndQuota(int64 usage, int64 quota) OVERRIDE { | |
33 callbacks_->didQueryStorageUsageAndQuota(usage, quota); | |
34 } | |
35 virtual void DidGrantStorageQuota(int64 granted_quota) OVERRIDE { | |
36 callbacks_->didGrantStorageQuota(granted_quota); | |
37 } | |
38 virtual void DidFail(quota::QuotaStatusCode error) OVERRIDE { | |
39 callbacks_->didFail(static_cast<WebStorageQuotaError>(error)); | |
40 } | |
41 | |
42 private: | |
43 // Not owned (self-destructed). | |
44 WebKit::WebStorageQuotaCallbacks* callbacks_; | |
45 }; | |
46 | |
47 } // namespace | |
48 | |
49 QuotaDispatcher::QuotaDispatcher() { | |
50 } | |
51 | |
52 QuotaDispatcher::~QuotaDispatcher() { | |
53 IDMap<Callback, IDMapOwnPointer>::iterator iter(&pending_quota_callbacks_); | |
54 while (!iter.IsAtEnd()) { | |
55 iter.GetCurrentValue()->DidFail(quota::kQuotaErrorAbort); | |
56 iter.Advance(); | |
57 } | |
58 } | |
59 | |
60 bool QuotaDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
61 bool handled = true; | |
62 IPC_BEGIN_MESSAGE_MAP(QuotaDispatcher, msg) | |
63 IPC_MESSAGE_HANDLER(QuotaMsg_DidGrantStorageQuota, | |
64 DidGrantStorageQuota) | |
65 IPC_MESSAGE_HANDLER(QuotaMsg_DidQueryStorageUsageAndQuota, | |
66 DidQueryStorageUsageAndQuota); | |
67 IPC_MESSAGE_HANDLER(QuotaMsg_DidFail, DidFail); | |
68 IPC_MESSAGE_UNHANDLED(handled = false) | |
69 IPC_END_MESSAGE_MAP() | |
70 return handled; | |
71 } | |
72 | |
73 void QuotaDispatcher::QueryStorageUsageAndQuota( | |
74 const GURL& origin_url, | |
75 StorageType type, | |
76 Callback* callback) { | |
77 DCHECK(callback); | |
78 int request_id = pending_quota_callbacks_.Add(callback); | |
79 ChildThread::current()->Send(new QuotaHostMsg_QueryStorageUsageAndQuota( | |
80 request_id, origin_url, type)); | |
81 } | |
82 | |
83 void QuotaDispatcher::RequestStorageQuota( | |
84 int render_view_id, | |
85 const GURL& origin_url, | |
86 StorageType type, | |
87 int64 requested_size, | |
88 Callback* callback) { | |
89 DCHECK(callback); | |
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)); | |
93 } | |
94 | |
95 // static | |
96 QuotaDispatcher::Callback* | |
97 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper( | |
98 WebKit::WebStorageQuotaCallbacks* callbacks) { | |
99 return new WebStorageQuotaDispatcherCallback(callbacks); | |
100 } | |
101 | |
102 void QuotaDispatcher::DidGrantStorageQuota( | |
103 int request_id, | |
104 int64 granted_quota) { | |
105 Callback* callback = pending_quota_callbacks_.Lookup(request_id); | |
106 DCHECK(callback); | |
107 callback->DidGrantStorageQuota(granted_quota); | |
108 pending_quota_callbacks_.Remove(request_id); | |
109 } | |
110 | |
111 void QuotaDispatcher::DidQueryStorageUsageAndQuota( | |
112 int request_id, | |
113 int64 current_usage, | |
114 int64 current_quota) { | |
115 Callback* callback = pending_quota_callbacks_.Lookup(request_id); | |
116 DCHECK(callback); | |
117 callback->DidQueryStorageUsageAndQuota(current_usage, current_quota); | |
118 pending_quota_callbacks_.Remove(request_id); | |
119 } | |
120 | |
121 void QuotaDispatcher::DidFail( | |
122 int request_id, | |
123 QuotaStatusCode error) { | |
124 Callback* callback = pending_quota_callbacks_.Lookup(request_id); | |
125 DCHECK(callback); | |
126 callback->DidFail(error); | |
127 pending_quota_callbacks_.Remove(request_id); | |
128 } | |
129 | |
130 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypeTemporary) == \ | |
131 int(quota::kStorageTypeTemporary), mismatching_enums); | |
132 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypePersistent) == \ | |
133 int(quota::kStorageTypePersistent), mismatching_enums); | |
134 | |
135 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorNotSupported) == \ | |
136 int(quota::kQuotaErrorNotSupported), mismatching_enums); | |
137 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorAbort) == \ | |
138 int(quota::kQuotaErrorAbort), mismatching_enums); | |
139 | |
140 } // namespace content | |
OLD | NEW |