OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 | 7 |
8 #include <deque> | |
9 #include <map> | |
10 #include <set> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/basictypes.h" | |
15 #include "base/callback.h" | |
16 #include "base/tuple.h" | |
17 #include "webkit/quota/quota_status_code.h" | 8 #include "webkit/quota/quota_status_code.h" |
18 | 9 |
19 class GURL; | |
20 | |
21 namespace quota { | 10 namespace quota { |
22 | 11 |
23 enum StorageType { | 12 enum StorageType { |
24 kStorageTypeTemporary, | 13 kStorageTypeTemporary, |
25 kStorageTypePersistent, | 14 kStorageTypePersistent, |
26 kStorageTypeSyncable, | 15 kStorageTypeSyncable, |
27 kStorageTypeUnknown, | 16 kStorageTypeUnknown, |
28 }; | 17 }; |
29 | 18 |
30 enum QuotaLimitType { | 19 enum QuotaLimitType { |
31 kQuotaLimitTypeUnknown, | 20 kQuotaLimitTypeUnknown, |
32 kQuotaLimitTypeLimited, | 21 kQuotaLimitTypeLimited, |
33 kQuotaLimitTypeUnlimited, | 22 kQuotaLimitTypeUnlimited, |
34 }; | 23 }; |
35 | 24 |
36 struct UsageInfo; | |
37 typedef std::vector<UsageInfo> UsageInfoEntries; | |
38 | |
39 // Common callback types that are used throughout in the quota module. | |
40 typedef base::Callback<void(int64 usage, | |
41 int64 unlimited_usage)> GlobalUsageCallback; | |
42 typedef base::Callback<void(QuotaStatusCode status, int64 quota)> QuotaCallback; | |
43 typedef base::Callback<void(int64 usage)> UsageCallback; | |
44 typedef base::Callback<void(QuotaStatusCode, int64)> AvailableSpaceCallback; | |
45 typedef base::Callback<void(QuotaStatusCode)> StatusCallback; | |
46 typedef base::Callback<void(const std::set<GURL>& origins, | |
47 StorageType type)> GetOriginsCallback; | |
48 typedef base::Callback<void(const UsageInfoEntries&)> GetUsageInfoCallback; | |
49 | |
50 template<typename CallbackType, typename Args> | |
51 void DispatchToCallback(const CallbackType& callback, | |
52 const Args& args) { | |
53 DispatchToMethod(&callback, &CallbackType::Run, args); | |
54 } | |
55 | |
56 // Simple template wrapper for a callback queue. | |
57 template <typename CallbackType, typename Args> | |
58 class CallbackQueue { | |
59 public: | |
60 // Returns true if the given |callback| is the first one added to the queue. | |
61 bool Add(const CallbackType& callback) { | |
62 callbacks_.push_back(callback); | |
63 return (callbacks_.size() == 1); | |
64 } | |
65 | |
66 bool HasCallbacks() const { | |
67 return !callbacks_.empty(); | |
68 } | |
69 | |
70 // Runs the callbacks added to the queue and clears the queue. | |
71 void Run(const Args& args) { | |
72 typedef typename std::vector<CallbackType>::iterator iterator; | |
73 for (iterator iter = callbacks_.begin(); | |
74 iter != callbacks_.end(); ++iter) | |
75 DispatchToCallback(*iter, args); | |
76 callbacks_.clear(); | |
77 } | |
78 | |
79 private: | |
80 std::vector<CallbackType> callbacks_; | |
81 }; | |
82 | |
83 typedef CallbackQueue<GlobalUsageCallback, | |
84 Tuple2<int64, int64> > | |
85 GlobalUsageCallbackQueue; | |
86 typedef CallbackQueue<AvailableSpaceCallback, | |
87 Tuple2<QuotaStatusCode, int64> > | |
88 AvailableSpaceCallbackQueue; | |
89 typedef CallbackQueue<QuotaCallback, | |
90 Tuple2<QuotaStatusCode, int64> > | |
91 GlobalQuotaCallbackQueue; | |
92 typedef CallbackQueue<base::Closure, Tuple0> ClosureQueue; | |
93 | |
94 template <typename CallbackType, typename Key, typename Args> | |
95 class CallbackQueueMap { | |
96 public: | |
97 typedef CallbackQueue<CallbackType, Args> CallbackQueueType; | |
98 typedef std::map<Key, CallbackQueueType> CallbackMap; | |
99 typedef typename CallbackMap::iterator iterator; | |
100 | |
101 bool Add(const Key& key, const CallbackType& callback) { | |
102 return callback_map_[key].Add(callback); | |
103 } | |
104 | |
105 bool HasCallbacks(const Key& key) const { | |
106 return (callback_map_.find(key) != callback_map_.end()); | |
107 } | |
108 | |
109 bool HasAnyCallbacks() const { | |
110 return !callback_map_.empty(); | |
111 } | |
112 | |
113 iterator Begin() { return callback_map_.begin(); } | |
114 iterator End() { return callback_map_.end(); } | |
115 | |
116 void Clear() { callback_map_.clear(); } | |
117 | |
118 // Runs the callbacks added for the given |key| and clears the key | |
119 // from the map. | |
120 void Run(const Key& key, const Args& args) { | |
121 if (!this->HasCallbacks(key)) | |
122 return; | |
123 CallbackQueueType& queue = callback_map_[key]; | |
124 queue.Run(args); | |
125 callback_map_.erase(key); | |
126 } | |
127 | |
128 private: | |
129 CallbackMap callback_map_; | |
130 }; | |
131 | |
132 typedef CallbackQueueMap<UsageCallback, std::string, Tuple1<int64> > | |
133 HostUsageCallbackMap; | |
134 typedef CallbackQueueMap<QuotaCallback, std::string, | |
135 Tuple2<QuotaStatusCode, int64> > | |
136 HostQuotaCallbackMap; | |
137 | |
138 } // namespace quota | 25 } // namespace quota |
139 | 26 |
140 #endif // WEBKIT_QUOTA_QUOTA_TYPES_H_ | 27 #endif // WEBKIT_QUOTA_QUOTA_TYPES_H_ |
OLD | NEW |