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