Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / webkit / browser / fileapi / quota / quota_reservation_manager.h
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/files/file.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.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 content {
21 class QuotaReservationManagerTest;
22 }
23
24 namespace fileapi {
25
26 class QuotaReservation;
27 class QuotaReservationBuffer;
28 class OpenFileHandle;
29 class OpenFileHandleContext;
30
31 class WEBKIT_STORAGE_BROWSER_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 WEBKIT_STORAGE_BROWSER_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 fileapi
126
127 #endif  // WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_