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 STORAGE_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_
6#define STORAGE_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/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"
18#include "url/gurl.h"
19
20namespace content {
21class QuotaReservationManagerTest;
22}
23
24namespace storage {
25
26class QuotaReservation;
27class QuotaReservationBuffer;
28class OpenFileHandle;
29class OpenFileHandleContext;
30
31class STORAGE_EXPORT QuotaReservationManager {
32 public:
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)>
36      ReserveQuotaCallback;
37
38  // An abstraction of backing quota system.
39  class STORAGE_EXPORT QuotaBackend {
40   public:
41    QuotaBackend() {}
42    virtual ~QuotaBackend() {}
43
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,
51                              FileSystemType type,
52                              int64 delta,
53                              const ReserveQuotaCallback& callback) = 0;
54
55    // Reclaims |size| of quota for |origin| and |type|.
56    virtual void ReleaseReservedQuota(const GURL& origin,
57                                      FileSystemType type,
58                                      int64 size) = 0;
59
60    // Updates disk usage of |origin| and |type|.
61    // Invokes |callback| upon completion with an error code.
62    virtual void CommitQuotaUsage(const GURL& origin,
63                                  FileSystemType type,
64                                  int64 delta) = 0;
65
66    virtual void IncrementDirtyCount(const GURL& origin,
67                                    FileSystemType type) = 0;
68    virtual void DecrementDirtyCount(const GURL& origin,
69                                    FileSystemType type) = 0;
70
71   private:
72    DISALLOW_COPY_AND_ASSIGN(QuotaBackend);
73  };
74
75  explicit QuotaReservationManager(scoped_ptr<QuotaBackend> backend);
76  ~QuotaReservationManager();
77
78  // The entry point of the quota reservation.  Creates new reservation object
79  // for |origin| and |type|.
80  scoped_refptr<QuotaReservation> CreateReservation(
81      const GURL& origin,
82      FileSystemType type);
83
84 private:
85  typedef std::map<std::pair<GURL, FileSystemType>, QuotaReservationBuffer*>
86      ReservationBufferByOriginAndType;
87
88  friend class QuotaReservation;
89  friend class QuotaReservationBuffer;
90  friend class content::QuotaReservationManagerTest;
91
92  void ReserveQuota(const GURL& origin,
93                    FileSystemType type,
94                    int64 delta,
95                    const ReserveQuotaCallback& callback);
96
97  void ReleaseReservedQuota(const GURL& origin,
98                            FileSystemType type,
99                            int64 size);
100
101  void CommitQuotaUsage(const GURL& origin,
102                        FileSystemType type,
103                        int64 delta);
104
105  void IncrementDirtyCount(const GURL& origin, FileSystemType type);
106  void DecrementDirtyCount(const GURL& origin, FileSystemType type);
107
108  scoped_refptr<QuotaReservationBuffer> GetReservationBuffer(
109      const GURL& origin,
110      FileSystemType type);
111  void ReleaseReservationBuffer(QuotaReservationBuffer* reservation_pool);
112
113  scoped_ptr<QuotaBackend> backend_;
114
115  // Not owned.  The destructor of ReservationBuffer should erase itself from
116  // |reservation_buffers_| by calling ReleaseReservationBuffer.
117  ReservationBufferByOriginAndType reservation_buffers_;
118
119  base::SequenceChecker sequence_checker_;
120  base::WeakPtrFactory<QuotaReservationManager> weak_ptr_factory_;
121
122  DISALLOW_COPY_AND_ASSIGN(QuotaReservationManager);
123};
124
125}  // namespace storage
126
127#endif  // STORAGE_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_
128