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.
5 #ifndef WEBKIT_BROWSER_QUOTA_QUOTA_CALLBACKS_H_
6 #define WEBKIT_BROWSER_QUOTA_QUOTA_CALLBACKS_H_
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/tuple.h"
16 #include "webkit/common/quota/quota_status_code.h"
17 #include "webkit/common/quota/quota_types.h"
24 typedef std::vector
<UsageInfo
> UsageInfoEntries
;
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
;
37 template<typename CallbackType
, typename Args
>
38 void DispatchToCallback(const CallbackType
& callback
,
40 DispatchToMethod(&callback
, &CallbackType::Run
, args
);
43 // Simple template wrapper for a callback queue.
44 template <typename CallbackType
, typename Args
>
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);
53 bool HasCallbacks() const {
54 return !callbacks_
.empty();
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
);
67 std::vector
<CallbackType
> callbacks_
;
70 typedef CallbackQueue
<GlobalUsageCallback
,
71 Tuple2
<int64
, int64
> >
72 GlobalUsageCallbackQueue
;
73 typedef CallbackQueue
<UsageCallback
, Tuple1
<int64
> >
75 typedef CallbackQueue
<AvailableSpaceCallback
,
76 Tuple2
<QuotaStatusCode
, int64
> >
77 AvailableSpaceCallbackQueue
;
78 typedef CallbackQueue
<QuotaCallback
,
79 Tuple2
<QuotaStatusCode
, int64
> >
80 GlobalQuotaCallbackQueue
;
81 typedef CallbackQueue
<base::Closure
, Tuple0
> ClosureQueue
;
83 template <typename CallbackType
, typename Key
, typename Args
>
84 class CallbackQueueMap
{
86 typedef CallbackQueue
<CallbackType
, Args
> CallbackQueueType
;
87 typedef std::map
<Key
, CallbackQueueType
> CallbackMap
;
88 typedef typename
CallbackMap::iterator iterator
;
90 bool Add(const Key
& key
, const CallbackType
& callback
) {
91 return callback_map_
[key
].Add(callback
);
94 bool HasCallbacks(const Key
& key
) const {
95 return (callback_map_
.find(key
) != callback_map_
.end());
98 bool HasAnyCallbacks() const {
99 return !callback_map_
.empty();
102 iterator
Begin() { return callback_map_
.begin(); }
103 iterator
End() { return callback_map_
.end(); }
105 void Clear() { callback_map_
.clear(); }
107 // Runs the callbacks added for the given |key| and clears the key
109 void Run(const Key
& key
, const Args
& args
) {
110 if (!this->HasCallbacks(key
))
112 CallbackQueueType
& queue
= callback_map_
[key
];
114 callback_map_
.erase(key
);
118 CallbackMap callback_map_
;
121 typedef CallbackQueueMap
<UsageCallback
, std::string
, Tuple1
<int64
> >
122 HostUsageCallbackMap
;
123 typedef CallbackQueueMap
<QuotaCallback
, std::string
,
124 Tuple2
<QuotaStatusCode
, int64
> >
125 HostQuotaCallbackMap
;
129 #endif // WEBKIT_QUOTA_QUOTA_TYPES_H_