Improve the logic for hiding the multi-window resize handle
[chromium-blink-merge.git] / storage / browser / quota / quota_callbacks.h
blob298f2696cd05876da583d68519a0582b2e9bb925
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_
8 #include <map>
9 #include <set>
10 #include <vector>
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"
17 class GURL;
19 namespace storage {
21 struct UsageInfo;
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;
35 // Simple template wrapper for a callback queue.
36 template <typename CallbackType, typename... Args>
37 class CallbackQueue {
38 public:
39 // Returns true if the given |callback| is the first one added to the queue.
40 bool Add(const CallbackType& callback) {
41 callbacks_.push_back(callback);
42 return (callbacks_.size() == 1);
45 bool HasCallbacks() const {
46 return !callbacks_.empty();
49 // Runs the callbacks added to the queue and clears the queue.
50 void Run(
51 typename base::internal::CallbackParamTraits<Args>::ForwardType... args) {
52 std::vector<CallbackType> callbacks;
53 callbacks.swap(callbacks_);
54 for (const auto& callback : callbacks)
55 callback.Run(base::internal::CallbackForward(args)...);
58 void Swap(CallbackQueue<CallbackType, Args...>* other) {
59 callbacks_.swap(other->callbacks_);
62 private:
63 std::vector<CallbackType> callbacks_;
66 template <typename CallbackType, typename Key, typename... Args>
67 class CallbackQueueMap {
68 public:
69 typedef CallbackQueue<CallbackType, Args...> CallbackQueueType;
70 typedef std::map<Key, CallbackQueueType> CallbackMap;
71 typedef typename CallbackMap::iterator iterator;
73 bool Add(const Key& key, const CallbackType& callback) {
74 return callback_map_[key].Add(callback);
77 bool HasCallbacks(const Key& key) const {
78 return (callback_map_.find(key) != callback_map_.end());
81 bool HasAnyCallbacks() const {
82 return !callback_map_.empty();
85 iterator Begin() { return callback_map_.begin(); }
86 iterator End() { return callback_map_.end(); }
88 void Clear() { callback_map_.clear(); }
90 // Runs the callbacks added for the given |key| and clears the key
91 // from the map.
92 void Run(
93 const Key& key,
94 typename base::internal::CallbackParamTraits<Args>::ForwardType... args) {
95 if (!this->HasCallbacks(key))
96 return;
97 CallbackQueueType queue;
98 queue.Swap(&callback_map_[key]);
99 callback_map_.erase(key);
100 queue.Run(base::internal::CallbackForward(args)...);
103 private:
104 CallbackMap callback_map_;
107 } // namespace storage
109 #endif // STORAGE_QUOTA_QUOTA_TYPES_H_