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_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_
6 #define WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/files/file.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
17 #include "webkit/browser/webkit_storage_browser_export.h"
18 #include "webkit/common/fileapi/file_system_types.h"
22 class QuotaReservation;
23 class QuotaReservationBuffer;
25 class OpenFileHandleContext;
27 class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservationManager {
29 // Callback for ReserveQuota. When this callback returns false, ReserveQuota
30 // operation should be reverted.
31 typedef base::Callback<bool(base::File::Error error, int64 delta)>
34 // An abstraction of backing quota system.
35 class WEBKIT_STORAGE_BROWSER_EXPORT QuotaBackend {
38 virtual ~QuotaBackend() {}
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,
49 const ReserveQuotaCallback& callback) = 0;
51 // Reclaims |size| of quota for |origin| and |type|.
52 virtual void ReleaseReservedQuota(const GURL& origin,
56 // Updates disk usage of |origin| and |type|.
57 // Invokes |callback| upon completion with an error code.
58 virtual void CommitQuotaUsage(const GURL& origin,
62 virtual void IncrementDirtyCount(const GURL& origin,
63 FileSystemType type) = 0;
64 virtual void DecrementDirtyCount(const GURL& origin,
65 FileSystemType type) = 0;
68 DISALLOW_COPY_AND_ASSIGN(QuotaBackend);
71 explicit QuotaReservationManager(scoped_ptr<QuotaBackend> backend);
72 ~QuotaReservationManager();
74 // The entry point of the quota reservation. Creates new reservation object
75 // for |origin| and |type|.
76 scoped_refptr<QuotaReservation> CreateReservation(
81 typedef std::map<std::pair<GURL, FileSystemType>, QuotaReservationBuffer*>
82 ReservationBufferByOriginAndType;
84 friend class QuotaReservation;
85 friend class QuotaReservationBuffer;
86 friend class QuotaReservationManagerTest;
88 void ReserveQuota(const GURL& origin,
91 const ReserveQuotaCallback& callback);
93 void ReleaseReservedQuota(const GURL& origin,
97 void CommitQuotaUsage(const GURL& origin,
101 void IncrementDirtyCount(const GURL& origin, FileSystemType type);
102 void DecrementDirtyCount(const GURL& origin, FileSystemType type);
104 scoped_refptr<QuotaReservationBuffer> GetReservationBuffer(
106 FileSystemType type);
107 void ReleaseReservationBuffer(QuotaReservationBuffer* reservation_pool);
109 scoped_ptr<QuotaBackend> backend_;
111 // Not owned. The destructor of ReservationBuffer should erase itself from
112 // |reservation_buffers_| by calling ReleaseReservationBuffer.
113 ReservationBufferByOriginAndType reservation_buffers_;
115 base::SequenceChecker sequence_checker_;
116 base::WeakPtrFactory<QuotaReservationManager> weak_ptr_factory_;
118 DISALLOW_COPY_AND_ASSIGN(QuotaReservationManager);
121 } // namespace fileapi
123 #endif // WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_