Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: webkit/quota/quota_types.h

Issue 8070001: Use base::Callback in Quota related code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/quota/quota_temporary_storage_evictor_unittest.cc ('k') | webkit/quota/usage_tracker.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef WEBKIT_QUOTA_QUOTA_TYPES_H_ 5 #ifndef WEBKIT_QUOTA_QUOTA_TYPES_H_
6 #define WEBKIT_QUOTA_QUOTA_TYPES_H_ 6 #define WEBKIT_QUOTA_QUOTA_TYPES_H_
7 #pragma once 7 #pragma once
8 8
9 #include <deque> 9 #include <deque>
10 #include <map> 10 #include <map>
11 #include <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 #include <vector>
13 14
14 #include "base/basictypes.h" 15 #include "base/basictypes.h"
15 #include "base/callback_old.h" 16 #include "base/callback.h"
16 #include "base/stl_util.h"
17 17
18 class GURL; 18 class GURL;
19 19
20 namespace quota { 20 namespace quota {
21 21
22 enum StorageType { 22 enum StorageType {
23 kStorageTypeTemporary, 23 kStorageTypeTemporary,
24 kStorageTypePersistent, 24 kStorageTypePersistent,
25 kStorageTypeUnknown, 25 kStorageTypeUnknown,
26 }; 26 };
27 27
28 // The numbers should match with the error code defined in 28 // The numbers should match with the error code defined in
29 // third_party/WebKit/Source/WebCore/dom/ExceptionCode.h. 29 // third_party/WebKit/Source/WebCore/dom/ExceptionCode.h.
30 enum QuotaStatusCode { 30 enum QuotaStatusCode {
31 kQuotaStatusOk = 0, 31 kQuotaStatusOk = 0,
32 kQuotaErrorNotSupported = 9, // NOT_SUPPORTED_ERR 32 kQuotaErrorNotSupported = 9, // NOT_SUPPORTED_ERR
33 kQuotaErrorInvalidModification = 13, // INVALID_MODIFICATION_ERR 33 kQuotaErrorInvalidModification = 13, // INVALID_MODIFICATION_ERR
34 kQuotaErrorInvalidAccess = 15, // INVALID_ACCESS_ERR 34 kQuotaErrorInvalidAccess = 15, // INVALID_ACCESS_ERR
35 kQuotaErrorAbort = 20, // ABORT_ERR 35 kQuotaErrorAbort = 20, // ABORT_ERR
36 kQuotaStatusUnknown = -1, 36 kQuotaStatusUnknown = -1,
37 }; 37 };
38 38
39 struct UsageInfo; 39 struct UsageInfo;
40 typedef std::vector<UsageInfo> UsageInfoEntries; 40 typedef std::vector<UsageInfo> UsageInfoEntries;
41 41
42 // Common callback types that are used throughout in the quota module. 42 // Common callback types that are used throughout in the quota module.
43 typedef Callback2<StorageType, int64>::Type UsageCallback; 43 typedef base::Callback<void(StorageType, int64)> UsageCallback;
44 typedef Callback3<StorageType, int64, int64>::Type GlobalUsageCallback; 44 typedef base::Callback<void(StorageType, int64, int64)> GlobalUsageCallback;
45 typedef Callback3<QuotaStatusCode, 45 typedef base::Callback<void(QuotaStatusCode, StorageType, int64)>
46 StorageType, 46 QuotaCallback;
47 int64>::Type QuotaCallback; 47 typedef base::Callback<void(const std::string&, StorageType, int64)>
48 typedef Callback3<const std::string& /* host */, 48 HostUsageCallback;
49 StorageType, 49 typedef base::Callback<void(QuotaStatusCode,
50 int64>::Type HostUsageCallback; 50 const std::string&,
51 typedef Callback4<QuotaStatusCode, 51 StorageType,
52 const std::string& /* host */, 52 int64)> HostQuotaCallback;
53 StorageType, 53 typedef base::Callback<void(QuotaStatusCode, int64)> AvailableSpaceCallback;
54 int64>::Type HostQuotaCallback; 54 typedef base::Callback<void(QuotaStatusCode)> StatusCallback;
55 typedef Callback2<QuotaStatusCode, 55 typedef base::Callback<void(const std::set<GURL>&, StorageType)>
56 int64>::Type AvailableSpaceCallback; 56 GetOriginsCallback;
57 typedef Callback1<QuotaStatusCode>::Type StatusCallback; 57 typedef base::Callback<void(const UsageInfoEntries&)> GetUsageInfoCallback;
58 typedef Callback2<const std::set<GURL>&, StorageType>::Type GetOriginsCallback;
59 typedef Callback1<const UsageInfoEntries&>::Type GetUsageInfoCallback;
60 58
61 // Simple template wrapper for a callback queue. 59 // Simple template wrapper for a callback queue.
62 template <typename CallbackType> 60 template <typename CallbackType>
63 class CallbackQueueBase { 61 class CallbackQueueBase {
64 public: 62 public:
65 typedef typename std::deque<CallbackType> Queue; 63 typedef typename std::deque<CallbackType> Queue;
66 typedef typename Queue::iterator iterator; 64 typedef typename Queue::iterator iterator;
67 65
68 virtual ~CallbackQueueBase() { 66 virtual ~CallbackQueueBase() {}
69 STLDeleteContainerPointers(callbacks_.begin(), callbacks_.end());
70 }
71 67
72 // Returns true if the given |callback| is the first one added to the queue. 68 // Returns true if the given |callback| is the first one added to the queue.
73 bool Add(CallbackType callback) { 69 bool Add(const CallbackType& callback) {
74 callbacks_.push_back(callback); 70 callbacks_.push_back(callback);
75 return (callbacks_.size() == 1); 71 return (callbacks_.size() == 1);
76 } 72 }
77 73
78 bool HasCallbacks() const { 74 bool HasCallbacks() const {
79 return !callbacks_.empty(); 75 return !callbacks_.empty();
80 } 76 }
81 77
82 protected: 78 protected:
83 std::deque<CallbackType> callbacks_; 79 std::deque<CallbackType> callbacks_;
84 }; 80 };
85 81
86 template <typename CallbackType1, typename A1> 82 template <typename CallbackType1, typename A1>
87 class CallbackQueue1 : public CallbackQueueBase<CallbackType1> { 83 class CallbackQueue1 : public CallbackQueueBase<CallbackType1> {
88 public: 84 public:
89 typedef typename CallbackQueueBase<CallbackType1>::Queue Queue; 85 typedef typename CallbackQueueBase<CallbackType1>::Queue Queue;
90 // Runs the callbacks added to the queue and clears the queue. 86 // Runs the callbacks added to the queue and clears the queue.
91 void Run(A1 arg) { 87 void Run(A1 arg) {
92 // Note: template-derived class needs 'this->' to access its base class. 88 // Note: template-derived class needs 'this->' to access its base class.
93 for (typename Queue::iterator iter = this->callbacks_.begin(); 89 for (typename Queue::iterator iter = this->callbacks_.begin();
94 iter != this->callbacks_.end(); ++iter) { 90 iter != this->callbacks_.end(); ++iter) {
95 (*iter)->Run(arg); 91 iter->Run(arg);
96 delete *iter;
97 } 92 }
98 this->callbacks_.clear(); 93 this->callbacks_.clear();
99 } 94 }
100 }; 95 };
101 96
102 template <typename CallbackType2, typename A1, typename A2> 97 template <typename CallbackType2, typename A1, typename A2>
103 class CallbackQueue2 : public CallbackQueueBase<CallbackType2> { 98 class CallbackQueue2 : public CallbackQueueBase<CallbackType2> {
104 public: 99 public:
105 typedef typename CallbackQueueBase<CallbackType2>::Queue Queue; 100 typedef typename CallbackQueueBase<CallbackType2>::Queue Queue;
106 // Runs the callbacks added to the queue and clears the queue. 101 // Runs the callbacks added to the queue and clears the queue.
107 void Run(A1 arg1, A2 arg2) { 102 void Run(A1 arg1, A2 arg2) {
108 for (typename Queue::iterator iter = this->callbacks_.begin(); 103 for (typename Queue::iterator iter = this->callbacks_.begin();
109 iter != this->callbacks_.end(); ++iter) { 104 iter != this->callbacks_.end(); ++iter) {
110 (*iter)->Run(arg1, arg2); 105 iter->Run(arg1, arg2);
111 delete *iter;
112 } 106 }
113 this->callbacks_.clear(); 107 this->callbacks_.clear();
114 } 108 }
115 }; 109 };
116 110
117 template <typename CallbackType3, typename A1, typename A2, typename A3> 111 template <typename CallbackType3, typename A1, typename A2, typename A3>
118 class CallbackQueue3 : public CallbackQueueBase<CallbackType3> { 112 class CallbackQueue3 : public CallbackQueueBase<CallbackType3> {
119 public: 113 public:
120 typedef typename CallbackQueueBase<CallbackType3>::Queue Queue; 114 typedef typename CallbackQueueBase<CallbackType3>::Queue Queue;
121 // Runs the callbacks added to the queue and clears the queue. 115 // Runs the callbacks added to the queue and clears the queue.
122 void Run(A1 arg1, A2 arg2, A3 arg3) { 116 void Run(A1 arg1, A2 arg2, A3 arg3) {
123 for (typename Queue::iterator iter = this->callbacks_.begin(); 117 for (typename Queue::iterator iter = this->callbacks_.begin();
124 iter != this->callbacks_.end(); ++iter) { 118 iter != this->callbacks_.end(); ++iter) {
125 (*iter)->Run(arg1, arg2, arg3); 119 iter->Run(arg1, arg2, arg3);
126 delete *iter;
127 } 120 }
128 this->callbacks_.clear(); 121 this->callbacks_.clear();
129 } 122 }
130 }; 123 };
131 124
132 template <typename CallbackType4, 125 template <typename CallbackType4,
133 typename A1, typename A2, typename A3, typename A4> 126 typename A1, typename A2, typename A3, typename A4>
134 class CallbackQueue4 : public CallbackQueueBase<CallbackType4> { 127 class CallbackQueue4 : public CallbackQueueBase<CallbackType4> {
135 public: 128 public:
136 typedef typename CallbackQueueBase<CallbackType4>::Queue Queue; 129 typedef typename CallbackQueueBase<CallbackType4>::Queue Queue;
137 // Runs the callbacks added to the queue and clears the queue. 130 // Runs the callbacks added to the queue and clears the queue.
138 void Run(A1 arg1, A2 arg2, A3 arg3, A4 arg4) { 131 void Run(A1 arg1, A2 arg2, A3 arg3, A4 arg4) {
139 for (typename Queue::iterator iter = this->callbacks_.begin(); 132 for (typename Queue::iterator iter = this->callbacks_.begin();
140 iter != this->callbacks_.end(); ++iter) { 133 iter != this->callbacks_.end(); ++iter) {
141 (*iter)->Run(arg1, arg2, arg3, arg4); 134 iter->Run(arg1, arg2, arg3, arg4);
142 delete *iter;
143 } 135 }
144 this->callbacks_.clear(); 136 this->callbacks_.clear();
145 } 137 }
146 }; 138 };
147 139
148 typedef CallbackQueue2<UsageCallback*, 140 typedef CallbackQueue2<UsageCallback,
149 StorageType, int64> UsageCallbackQueue; 141 StorageType, int64> UsageCallbackQueue;
150 typedef CallbackQueue3<GlobalUsageCallback*, 142 typedef CallbackQueue3<GlobalUsageCallback,
151 StorageType, int64, int64> GlobalUsageCallbackQueue; 143 StorageType, int64, int64> GlobalUsageCallbackQueue;
152 typedef CallbackQueue3<QuotaCallback*, 144 typedef CallbackQueue3<QuotaCallback,
153 QuotaStatusCode, 145 QuotaStatusCode,
154 StorageType, int64> QuotaCallbackQueue; 146 StorageType, int64> QuotaCallbackQueue;
155 147
156 template <typename CallbackType, typename CallbackQueueType, typename KEY> 148 template <typename CallbackType, typename CallbackQueueType, typename KEY>
157 class CallbackQueueMapBase { 149 class CallbackQueueMapBase {
158 public: 150 public:
159 typedef std::map<KEY, CallbackQueueType> CallbackMap; 151 typedef std::map<KEY, CallbackQueueType> CallbackMap;
160 typedef typename CallbackMap::iterator iterator; 152 typedef typename CallbackMap::iterator iterator;
161 153
162 bool Add(const KEY& key, CallbackType callback) { 154 bool Add(const KEY& key, const CallbackType& callback) {
163 return callback_map_[key].Add(callback); 155 return callback_map_[key].Add(callback);
164 } 156 }
165 157
166 bool HasCallbacks(const KEY& key) const { 158 bool HasCallbacks(const KEY& key) const {
167 return (callback_map_.find(key) != callback_map_.end()); 159 return (callback_map_.find(key) != callback_map_.end());
168 } 160 }
169 161
170 bool HasAnyCallbacks() const { 162 bool HasAnyCallbacks() const {
171 return !callback_map_.empty(); 163 return !callback_map_.empty();
172 } 164 }
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 // from the map. 261 // from the map.
270 void Run(const KEY& key, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4) { 262 void Run(const KEY& key, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4) {
271 if (!this->HasCallbacks(key)) 263 if (!this->HasCallbacks(key))
272 return; 264 return;
273 Queue& queue = this->callback_map_[key]; 265 Queue& queue = this->callback_map_[key];
274 queue.Run(arg1, arg2, arg3, arg4); 266 queue.Run(arg1, arg2, arg3, arg4);
275 this->callback_map_.erase(key); 267 this->callback_map_.erase(key);
276 } 268 }
277 }; 269 };
278 270
279 typedef CallbackQueueMap1<UsageCallback*, GURL, int64> OriginUsageCallbackMap; 271 typedef CallbackQueueMap1<UsageCallback, GURL, int64> OriginUsageCallbackMap;
280 typedef CallbackQueueMap3<HostUsageCallback*, std::string, 272 typedef CallbackQueueMap3<HostUsageCallback, std::string,
281 const std::string&, 273 const std::string&,
282 StorageType, int64> HostUsageCallbackMap; 274 StorageType, int64> HostUsageCallbackMap;
283 typedef CallbackQueueMap4<HostQuotaCallback*, std::string, 275 typedef CallbackQueueMap4<HostQuotaCallback, std::string,
284 QuotaStatusCode, 276 QuotaStatusCode,
285 const std::string&, 277 const std::string&,
286 StorageType, int64> HostQuotaCallbackMap; 278 StorageType, int64> HostQuotaCallbackMap;
287 279
288 } // namespace quota 280 } // namespace quota
289 281
290 #endif // WEBKIT_QUOTA_QUOTA_TYPES_H_ 282 #endif // WEBKIT_QUOTA_QUOTA_TYPES_H_
OLDNEW
« no previous file with comments | « webkit/quota/quota_temporary_storage_evictor_unittest.cc ('k') | webkit/quota/usage_tracker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698