Home | History | Annotate | Download | only in quota
      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_BROWSER_QUOTA_QUOTA_CALLBACKS_H_
      6 #define WEBKIT_BROWSER_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/common/quota/quota_status_code.h"
     17 #include "webkit/common/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<UsageCallback, Tuple1<int64> >
     74     UsageCallbackQueue;
     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;
     82 
     83 template <typename CallbackType, typename Key, typename Args>
     84 class CallbackQueueMap {
     85  public:
     86   typedef CallbackQueue<CallbackType, Args> CallbackQueueType;
     87   typedef std::map<Key, CallbackQueueType> CallbackMap;
     88   typedef typename CallbackMap::iterator iterator;
     89 
     90   bool Add(const Key& key, const CallbackType& callback) {
     91     return callback_map_[key].Add(callback);
     92   }
     93 
     94   bool HasCallbacks(const Key& key) const {
     95     return (callback_map_.find(key) != callback_map_.end());
     96   }
     97 
     98   bool HasAnyCallbacks() const {
     99     return !callback_map_.empty();
    100   }
    101 
    102   iterator Begin() { return callback_map_.begin(); }
    103   iterator End() { return callback_map_.end(); }
    104 
    105   void Clear() { callback_map_.clear(); }
    106 
    107   // Runs the callbacks added for the given |key| and clears the key
    108   // from the map.
    109   void Run(const Key& key, const Args& args) {
    110     if (!this->HasCallbacks(key))
    111       return;
    112     CallbackQueueType& queue = callback_map_[key];
    113     queue.Run(args);
    114     callback_map_.erase(key);
    115   }
    116 
    117  private:
    118   CallbackMap callback_map_;
    119 };
    120 
    121 typedef CallbackQueueMap<UsageCallback, std::string, Tuple1<int64> >
    122     HostUsageCallbackMap;
    123 typedef CallbackQueueMap<QuotaCallback, std::string,
    124                          Tuple2<QuotaStatusCode, int64> >
    125     HostQuotaCallbackMap;
    126 
    127 }  // namespace quota
    128 
    129 #endif  // WEBKIT_QUOTA_QUOTA_TYPES_H_
    130