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