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_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_
6 #define STORAGE_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"
16 #include "storage/browser/storage_browser_export.h"
17 #include "storage/common/fileapi/file_system_types.h"
21 class QuotaReservationManagerTest
;
26 class QuotaReservation
;
27 class QuotaReservationBuffer
;
29 class OpenFileHandleContext
;
31 class STORAGE_EXPORT QuotaReservationManager
{
33 // Callback for ReserveQuota. When this callback returns false, ReserveQuota
34 // operation should be reverted.
35 typedef base::Callback
<bool(base::File::Error error
, int64 delta
)>
38 // An abstraction of backing quota system.
39 class STORAGE_EXPORT QuotaBackend
{
42 virtual ~QuotaBackend() {}
44 // Reserves or reclaims |delta| of quota for |origin| and |type| pair.
45 // Reserved quota should be counted as usage, but it should be on-memory
46 // and be cleared by a browser restart.
47 // Invokes |callback| upon completion with an error code.
48 // |callback| should return false if it can't accept the reservation, in
49 // that case, the backend should roll back the reservation.
50 virtual void ReserveQuota(const GURL
& origin
,
53 const ReserveQuotaCallback
& callback
) = 0;
55 // Reclaims |size| of quota for |origin| and |type|.
56 virtual void ReleaseReservedQuota(const GURL
& origin
,
60 // Updates disk usage of |origin| and |type|.
61 // Invokes |callback| upon completion with an error code.
62 virtual void CommitQuotaUsage(const GURL
& origin
,
66 virtual void IncrementDirtyCount(const GURL
& origin
,
67 FileSystemType type
) = 0;
68 virtual void DecrementDirtyCount(const GURL
& origin
,
69 FileSystemType type
) = 0;
72 DISALLOW_COPY_AND_ASSIGN(QuotaBackend
);
75 explicit QuotaReservationManager(scoped_ptr
<QuotaBackend
> backend
);
76 ~QuotaReservationManager();
78 // The entry point of the quota reservation. Creates new reservation object
79 // for |origin| and |type|.
80 scoped_refptr
<QuotaReservation
> CreateReservation(
85 typedef std::map
<std::pair
<GURL
, FileSystemType
>, QuotaReservationBuffer
*>
86 ReservationBufferByOriginAndType
;
88 friend class QuotaReservation
;
89 friend class QuotaReservationBuffer
;
90 friend class content::QuotaReservationManagerTest
;
92 void ReserveQuota(const GURL
& origin
,
95 const ReserveQuotaCallback
& callback
);
97 void ReleaseReservedQuota(const GURL
& origin
,
101 void CommitQuotaUsage(const GURL
& origin
,
105 void IncrementDirtyCount(const GURL
& origin
, FileSystemType type
);
106 void DecrementDirtyCount(const GURL
& origin
, FileSystemType type
);
108 scoped_refptr
<QuotaReservationBuffer
> GetReservationBuffer(
110 FileSystemType type
);
111 void ReleaseReservationBuffer(QuotaReservationBuffer
* reservation_pool
);
113 scoped_ptr
<QuotaBackend
> backend_
;
115 // Not owned. The destructor of ReservationBuffer should erase itself from
116 // |reservation_buffers_| by calling ReleaseReservationBuffer.
117 ReservationBufferByOriginAndType reservation_buffers_
;
119 base::SequenceChecker sequence_checker_
;
120 base::WeakPtrFactory
<QuotaReservationManager
> weak_ptr_factory_
;
122 DISALLOW_COPY_AND_ASSIGN(QuotaReservationManager
);
125 } // namespace storage
127 #endif // STORAGE_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_