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

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

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

Powered by Google App Engine
This is Rietveld 408576698