Updating trunk VERSION from 769.0 to 770.0
[chromium-blink-merge.git] / webkit / quota / quota_types.h
blobac49bd8ef185cef39e91c4e355072286f40a57b2
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 #ifndef WEBKIT_QUOTA_QUOTA_TYPES_H_
6 #define WEBKIT_QUOTA_QUOTA_TYPES_H_
7 #pragma once
9 #include <deque>
10 #include <map>
11 #include <string>
13 #include "base/basictypes.h"
14 #include "base/callback_old.h"
15 #include "base/stl_util-inl.h"
17 namespace quota {
19 enum StorageType {
20 kStorageTypeTemporary,
21 kStorageTypePersistent,
22 kStorageTypeUnknown,
25 // TODO(tzik): Add assertions to
26 // content/browser/renderer_host/quota_dispatcher_host.cc
27 // ref) third_party/WebKit/Source/WebCore/dom/ExceptionCode.h,
28 enum QuotaStatusCode {
29 kQuotaStatusOk = 0,
30 kQuotaErrorNotSupported = 9, // NOT_SUPPORTED_ERR
31 kQuotaErrorInvalidModification = 13, // INVALID_MODIFICATION_ERR
32 kQuotaErrorInvalidAccess = 15, // INVALID_ACCESS_ERR
33 kQuotaErrorAbort = 20, // ABORT_ERR
34 kQuotaStatusUnknown = -1,
37 // Common callback types that are used throughout in the quota module.
38 typedef Callback1<int64>::Type UsageCallback;
39 typedef Callback2<QuotaStatusCode,
40 int64>::Type QuotaCallback;
41 typedef Callback2<const std::string& /* host */,
42 int64>::Type HostUsageCallback;
43 typedef Callback3<QuotaStatusCode,
44 const std::string& /* host */,
45 int64>::Type HostQuotaCallback;
46 typedef Callback2<QuotaStatusCode,
47 int64>::Type AvailableSpaceCallback;
49 // Simple template wrapper for a callback queue.
50 template <typename CallbackType>
51 class CallbackQueueBase {
52 public:
53 typedef typename std::deque<CallbackType> Queue;
54 typedef typename Queue::iterator iterator;
56 virtual ~CallbackQueueBase() {
57 STLDeleteContainerPointers(callbacks_.begin(), callbacks_.end());
60 // Returns true if the given |callback| is the first one added to the queue.
61 bool Add(CallbackType callback) {
62 callbacks_.push_back(callback);
63 return (callbacks_.size() == 1);
66 bool HasCallbacks() const {
67 return !callbacks_.empty();
70 protected:
71 std::deque<CallbackType> callbacks_;
74 template <typename CallbackType1, typename A1>
75 class CallbackQueue1 : public CallbackQueueBase<CallbackType1> {
76 public:
77 typedef typename CallbackQueueBase<CallbackType1>::Queue Queue;
78 // Runs the callbacks added to the queue and clears the queue.
79 void Run(A1 arg) {
80 // Note: template-derived class needs 'this->' to access its base class.
81 for (typename Queue::iterator iter = this->callbacks_.begin();
82 iter != this->callbacks_.end(); ++iter) {
83 (*iter)->Run(arg);
84 delete *iter;
86 this->callbacks_.clear();
90 template <typename CallbackType2, typename A1, typename A2>
91 class CallbackQueue2 : public CallbackQueueBase<CallbackType2> {
92 public:
93 typedef typename CallbackQueueBase<CallbackType2>::Queue Queue;
94 // Runs the callbacks added to the queue and clears the queue.
95 void Run(A1 arg1, A2 arg2) {
96 for (typename Queue::iterator iter = this->callbacks_.begin();
97 iter != this->callbacks_.end(); ++iter) {
98 (*iter)->Run(arg1, arg2);
99 delete *iter;
101 this->callbacks_.clear();
105 template <typename CallbackType3, typename A1, typename A2, typename A3>
106 class CallbackQueue3 : public CallbackQueueBase<CallbackType3> {
107 public:
108 typedef typename CallbackQueueBase<CallbackType3>::Queue Queue;
109 // Runs the callbacks added to the queue and clears the queue.
110 void Run(A1 arg1, A2 arg2, A3 arg3) {
111 for (typename Queue::iterator iter = this->callbacks_.begin();
112 iter != this->callbacks_.end(); ++iter) {
113 (*iter)->Run(arg1, arg2, arg3);
114 delete *iter;
116 this->callbacks_.clear();
120 typedef CallbackQueue1<UsageCallback*, int64> UsageCallbackQueue;
121 typedef CallbackQueue2<QuotaCallback*,
122 QuotaStatusCode, int64> QuotaCallbackQueue;
124 template <typename CallbackType, typename CallbackQueueType, typename KEY>
125 class CallbackQueueMapBase {
126 public:
127 typedef std::map<KEY, CallbackQueueType> CallbackMap;
128 typedef typename CallbackMap::iterator iterator;
130 bool Add(const KEY& key, CallbackType callback) {
131 return callback_map_[key].Add(callback);
134 bool HasCallbacks(const KEY& key) const {
135 return (callback_map_.find(key) != callback_map_.end());
138 iterator Begin() { return callback_map_.begin(); }
139 iterator End() { return callback_map_.end(); }
141 void Clear() { callback_map_.clear(); }
143 protected:
144 CallbackMap callback_map_;
147 template <typename CallbackType1, typename KEY, typename ARG>
148 class CallbackQueueMap1
149 : public CallbackQueueMapBase<CallbackType1,
150 CallbackQueue1<CallbackType1, ARG>,
151 KEY> {
152 public:
153 typedef typename CallbackQueueMapBase<
154 CallbackType1,
155 CallbackQueue1<CallbackType1, ARG>,
156 KEY>::iterator iterator;
157 typedef CallbackQueue1<CallbackType1, ARG> Queue;
159 // Runs the callbacks added for the given |key| and clears the key
160 // from the map.
161 void Run(const KEY& key, ARG arg) {
162 if (!this->HasCallbacks(key))
163 return;
164 Queue& queue = this->callback_map_[key];
165 queue.Run(arg);
166 this->callback_map_.erase(key);
170 template <typename CallbackType2, typename KEY, typename ARG1, typename ARG2>
171 class CallbackQueueMap2
172 : public CallbackQueueMapBase<CallbackType2,
173 CallbackQueue2<CallbackType2, ARG1, ARG2>,
174 KEY> {
175 public:
176 typedef typename CallbackQueueMapBase<
177 CallbackType2,
178 CallbackQueue2<CallbackType2, ARG1, ARG2>,
179 KEY>::iterator iterator;
180 typedef CallbackQueue2<CallbackType2, ARG1, ARG2> Queue;
182 // Runs the callbacks added for the given |key| and clears the key
183 // from the map.
184 void Run(const KEY& key, ARG1 arg1, ARG2 arg2) {
185 if (!this->HasCallbacks(key))
186 return;
187 Queue& queue = this->callback_map_[key];
188 queue.Run(arg1, arg2);
189 this->callback_map_.erase(key);
193 template <typename CallbackType3, typename KEY,
194 typename ARG1, typename ARG2, typename ARG3>
195 class CallbackQueueMap3
196 : public CallbackQueueMapBase<CallbackType3,
197 CallbackQueue3<CallbackType3,
198 ARG1, ARG2, ARG3>,
199 KEY> {
200 public:
201 typedef typename CallbackQueueMapBase<
202 CallbackType3,
203 CallbackQueue3<CallbackType3, ARG1, ARG2, ARG3>,
204 KEY>::iterator iterator;
205 typedef CallbackQueue3<CallbackType3, ARG1, ARG2, ARG3> Queue;
207 // Runs the callbacks added for the given |key| and clears the key
208 // from the map.
209 void Run(const KEY& key, ARG1 arg1, ARG2 arg2, ARG3 arg3) {
210 if (!this->HasCallbacks(key))
211 return;
212 Queue& queue = this->callback_map_[key];
213 queue.Run(arg1, arg2, arg3);
214 this->callback_map_.erase(key);
218 typedef CallbackQueueMap2<HostUsageCallback*, std::string,
219 const std::string&, int64> HostUsageCallbackMap;
220 typedef CallbackQueueMap3<HostQuotaCallback*, std::string,
221 QuotaStatusCode,
222 const std::string&, int64> HostQuotaCallbackMap;
224 } // namespace quota
226 #endif // WEBKIT_QUOTA_QUOTA_TYPES_H_