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 STORAGE_BROWSER_QUOTA_QUOTA_CALLBACKS_H_
6 #define STORAGE_BROWSER_QUOTA_QUOTA_CALLBACKS_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "storage/common/quota/quota_status_code.h"
15 #include "storage/common/quota/quota_types.h"
22 typedef std::vector
<UsageInfo
> UsageInfoEntries
;
24 // Common callback types that are used throughout in the quota module.
25 typedef base::Callback
<void(int64 usage
,
26 int64 unlimited_usage
)> GlobalUsageCallback
;
27 typedef base::Callback
<void(QuotaStatusCode status
, int64 quota
)> QuotaCallback
;
28 typedef base::Callback
<void(int64 usage
)> UsageCallback
;
29 typedef base::Callback
<void(QuotaStatusCode
, int64
)> AvailableSpaceCallback
;
30 typedef base::Callback
<void(QuotaStatusCode
)> StatusCallback
;
31 typedef base::Callback
<void(const std::set
<GURL
>& origins
,
32 StorageType type
)> GetOriginsCallback
;
33 typedef base::Callback
<void(const UsageInfoEntries
&)> GetUsageInfoCallback
;
34 typedef base::Callback
<void(const GURL
&)> GetOriginCallback
;
36 // Simple template wrapper for a callback queue.
37 template <typename CallbackType
, typename
... Args
>
40 // Returns true if the given |callback| is the first one added to the queue.
41 bool Add(const CallbackType
& callback
) {
42 callbacks_
.push_back(callback
);
43 return (callbacks_
.size() == 1);
46 bool HasCallbacks() const {
47 return !callbacks_
.empty();
50 // Runs the callbacks added to the queue and clears the queue.
52 typename
base::internal::CallbackParamTraits
<Args
>::ForwardType
... args
) {
53 std::vector
<CallbackType
> callbacks
;
54 callbacks
.swap(callbacks_
);
55 for (const auto& callback
: callbacks
)
56 callback
.Run(base::internal::CallbackForward(args
)...);
59 void Swap(CallbackQueue
<CallbackType
, Args
...>* other
) {
60 callbacks_
.swap(other
->callbacks_
);
64 std::vector
<CallbackType
> callbacks_
;
67 template <typename CallbackType
, typename Key
, typename
... Args
>
68 class CallbackQueueMap
{
70 typedef CallbackQueue
<CallbackType
, Args
...> CallbackQueueType
;
71 typedef std::map
<Key
, CallbackQueueType
> CallbackMap
;
72 typedef typename
CallbackMap::iterator iterator
;
74 bool Add(const Key
& key
, const CallbackType
& callback
) {
75 return callback_map_
[key
].Add(callback
);
78 bool HasCallbacks(const Key
& key
) const {
79 return (callback_map_
.find(key
) != callback_map_
.end());
82 bool HasAnyCallbacks() const {
83 return !callback_map_
.empty();
86 iterator
Begin() { return callback_map_
.begin(); }
87 iterator
End() { return callback_map_
.end(); }
89 void Clear() { callback_map_
.clear(); }
91 // Runs the callbacks added for the given |key| and clears the key
95 typename
base::internal::CallbackParamTraits
<Args
>::ForwardType
... args
) {
96 if (!this->HasCallbacks(key
))
98 CallbackQueueType queue
;
99 queue
.Swap(&callback_map_
[key
]);
100 callback_map_
.erase(key
);
101 queue
.Run(base::internal::CallbackForward(args
)...);
105 CallbackMap callback_map_
;
108 } // namespace storage
110 #endif // STORAGE_QUOTA_QUOTA_TYPES_H_