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_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_
      6 #define WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_
      7 
      8 #include <map>
      9 #include <utility>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback_forward.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/platform_file.h"
     16 #include "url/gurl.h"
     17 #include "webkit/browser/webkit_storage_browser_export.h"
     18 #include "webkit/common/fileapi/file_system_types.h"
     19 
     20 namespace fileapi {
     21 
     22 class QuotaReservation;
     23 class QuotaReservationBuffer;
     24 class OpenFileHandle;
     25 class OpenFileHandleContext;
     26 
     27 class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservationManager {
     28  public:
     29   // Callback for ReserveQuota. When this callback returns false, ReserveQuota
     30   // operation should be reverted.
     31   typedef base::Callback<bool(base::PlatformFileError error)>
     32       ReserveQuotaCallback;
     33 
     34   // An abstraction of backing quota system.
     35   class WEBKIT_STORAGE_BROWSER_EXPORT QuotaBackend {
     36    public:
     37     QuotaBackend() {}
     38     virtual ~QuotaBackend() {}
     39 
     40     // Reserves or reclaims |delta| of quota for |origin| and |type| pair.
     41     // Reserved quota should be counted as usage, but it should be on-memory
     42     // and be cleared by a browser restart.
     43     // Invokes |callback| upon completion with an error code.
     44     // |callback| should return false if it can't accept the reservation, in
     45     // that case, the backend should roll back the reservation.
     46     virtual void ReserveQuota(const GURL& origin,
     47                               FileSystemType type,
     48                               int64 delta,
     49                               const ReserveQuotaCallback& callback) = 0;
     50 
     51     // Reclaims |size| of quota for |origin| and |type|.
     52     virtual void ReleaseReservedQuota(const GURL& origin,
     53                                       FileSystemType type,
     54                                       int64 size) = 0;
     55 
     56     // Updates disk usage of |origin| and |type|.
     57     // Invokes |callback| upon completion with an error code.
     58     virtual void CommitQuotaUsage(const GURL& origin,
     59                                   FileSystemType type,
     60                                   int64 delta) = 0;
     61 
     62     virtual void IncrementDirtyCount(const GURL& origin,
     63                                     FileSystemType type) = 0;
     64     virtual void DecrementDirtyCount(const GURL& origin,
     65                                     FileSystemType type) = 0;
     66 
     67    private:
     68     DISALLOW_COPY_AND_ASSIGN(QuotaBackend);
     69   };
     70 
     71   explicit QuotaReservationManager(scoped_ptr<QuotaBackend> backend);
     72   ~QuotaReservationManager();
     73 
     74   // The entry point of the quota reservation.  Creates new reservation object
     75   // for |origin| and |type|.
     76   scoped_refptr<QuotaReservation> CreateReservation(
     77       const GURL& origin,
     78       FileSystemType type);
     79 
     80  private:
     81   typedef std::map<std::pair<GURL, FileSystemType>, QuotaReservationBuffer*>
     82       ReservationBufferByOriginAndType;
     83 
     84   friend class QuotaReservation;
     85   friend class QuotaReservationBuffer;
     86   friend class QuotaReservationManagerTest;
     87 
     88   void ReserveQuota(const GURL& origin,
     89                     FileSystemType type,
     90                     int64 delta,
     91                     const ReserveQuotaCallback& callback);
     92 
     93   void ReleaseReservedQuota(const GURL& origin,
     94                             FileSystemType type,
     95                             int64 size);
     96 
     97   void CommitQuotaUsage(const GURL& origin,
     98                         FileSystemType type,
     99                         int64 delta);
    100 
    101   void IncrementDirtyCount(const GURL& origin, FileSystemType type);
    102   void DecrementDirtyCount(const GURL& origin, FileSystemType type);
    103 
    104   scoped_refptr<QuotaReservationBuffer> GetReservationBuffer(
    105       const GURL& origin,
    106       FileSystemType type);
    107   void ReleaseReservationBuffer(QuotaReservationBuffer* reservation_pool);
    108 
    109   scoped_ptr<QuotaBackend> backend_;
    110 
    111   // Not owned.  The destructor of ReservationBuffer should erase itself from
    112   // |reservation_buffers_| by calling ReleaseReservationBuffer.
    113   ReservationBufferByOriginAndType reservation_buffers_;
    114 
    115   base::SequenceChecker sequence_checker_;
    116   base::WeakPtrFactory<QuotaReservationManager> weak_ptr_factory_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(QuotaReservationManager);
    119 };
    120 
    121 }  // namespace fileapi
    122 
    123 #endif  // WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_
    124