| 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> | |
| 10 | 9 |
| 11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 12 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 13 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 15 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
| 16 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 17 #include "webkit/browser/webkit_storage_browser_export.h" | 16 #include "webkit/browser/webkit_storage_browser_export.h" |
| 18 #include "webkit/common/fileapi/file_system_types.h" | 17 #include "webkit/common/fileapi/file_system_types.h" |
| 19 | 18 |
| 20 namespace fileapi { | 19 namespace fileapi { |
| 21 | 20 |
| 22 class QuotaReservation; | 21 class QuotaReservation; |
| 23 class QuotaReservationBuffer; | 22 class QuotaReservationBuffer; |
| 24 class OpenFileHandle; | 23 class OpenFileHandle; |
| 25 class OpenFileHandleContext; | 24 class OpenFileHandleContext; |
| 26 | 25 |
| 27 class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservationManager { | 26 class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservationManager { |
| 28 public: | 27 public: |
| 29 // Callback for ReserveQuota. When this callback returns false, ReserveQuota | 28 typedef base::Callback<void(base::PlatformFileError error)> StatusCallback; |
| 30 // operation should be reverted. | |
| 31 typedef base::Callback<bool(base::PlatformFileError error)> | 29 typedef base::Callback<bool(base::PlatformFileError error)> |
| 32 ReserveQuotaCallback; | 30 ReserveQuotaCallback; |
| 33 | 31 |
| 34 // An abstraction of backing quota system. | 32 // An abstraction of backing quota system. |
| 35 class WEBKIT_STORAGE_BROWSER_EXPORT QuotaBackend { | 33 class QuotaBackend { |
| 36 public: | 34 public: |
| 37 QuotaBackend() {} | |
| 38 virtual ~QuotaBackend() {} | |
| 39 | |
| 40 // Reserves or reclaims |delta| of quota for |origin| and |type| pair. | 35 // 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 | 36 // Reserved quota should be counted as usage, but it should be on-memory |
| 42 // and be cleared by a browser restart. | 37 // and be cleared by a browser restart. |
| 43 // Invokes |callback| upon completion with an error code. | 38 // Invokes |callback| upon completion with an error code. |
| 44 // |callback| should return false if it can't accept the reservation, in | 39 // |callback| should return false if it can't accept the reservation, in |
| 45 // that case, the backend should roll back the reservation. | 40 // that case, the backend should roll back the reservation. |
| 46 virtual void ReserveQuota(const GURL& origin, | 41 virtual void ReserveQuota(const GURL& origin, |
| 47 FileSystemType type, | 42 FileSystemType type, |
| 48 int64 delta, | 43 int64 delta, |
| 49 const ReserveQuotaCallback& callback) = 0; | 44 const ReserveQuotaCallback& callback) = 0; |
| 50 | 45 |
| 51 // Reclaims |size| of quota for |origin| and |type|. | 46 // Reclaims |size| of quota for |origin| and |type|. |
| 52 virtual void ReleaseReservedQuota(const GURL& origin, | 47 virtual void ReleaseReservedQuota(const GURL& origin, |
| 53 FileSystemType type, | 48 FileSystemType type, |
| 54 int64 size) = 0; | 49 int64 size) = 0; |
| 55 | 50 |
| 56 // Updates disk usage of |origin| and |type|. | 51 // Updates disk usage of |origin| and |type|. |
| 57 // Invokes |callback| upon completion with an error code. | 52 // Invokes |callback| upon completion with an error code. |
| 58 virtual void CommitQuotaUsage(const GURL& origin, | 53 virtual void CommitQuotaUsage(const GURL& origin, |
| 59 FileSystemType type, | 54 FileSystemType type, |
| 60 int64 delta) = 0; | 55 int64 delta, |
| 56 const StatusCallback& callback) = 0; |
| 61 | 57 |
| 62 virtual void IncrementDirtyCount(const GURL& origin, | 58 virtual void IncreaseDirtyCount(const GURL& origin, |
| 63 FileSystemType type) = 0; | 59 FileSystemType type) = 0; |
| 64 virtual void DecrementDirtyCount(const GURL& origin, | 60 virtual void DecreaseDirtyCount(const GURL& origin, |
| 65 FileSystemType type) = 0; | 61 FileSystemType type) = 0; |
| 66 | 62 |
| 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(scoped_ptr<QuotaBackend> backend); | 71 explicit QuotaReservationManager(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; | |
| 87 | 86 |
| 88 void ReserveQuota(const GURL& origin, | 87 void ReserveQuota(const GURL& origin, |
| 89 FileSystemType type, | 88 FileSystemType type, |
| 90 int64 delta, | 89 int64 delta, |
| 91 const ReserveQuotaCallback& callback); | 90 const ReserveQuotaCallback& callback); |
| 92 | 91 |
| 93 void ReleaseReservedQuota(const GURL& origin, | 92 void ReleaseReservedQuota(const GURL& origin, |
| 94 FileSystemType type, | 93 FileSystemType type, |
| 95 int64 size); | 94 int64 size); |
| 96 | 95 |
| 97 void CommitQuotaUsage(const GURL& origin, | 96 void CommitQuotaUsage(const GURL& origin, |
| 98 FileSystemType type, | 97 FileSystemType type, |
| 99 int64 delta); | 98 int64 delta, |
| 99 const StatusCallback& callback); |
| 100 | 100 |
| 101 void IncrementDirtyCount(const GURL& origin, FileSystemType type); | 101 void IncreaseDirtyCount(const GURL& origin, FileSystemType type); |
| 102 void DecrementDirtyCount(const GURL& origin, FileSystemType type); | 102 void DecreaseDirtyCount(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 scoped_ptr<QuotaBackend> backend_; | 109 |
| 110 // Not owned. |backend_| should outlive QuotaReservationManager |
| 111 QuotaBackend* backend_; |
| 110 | 112 |
| 111 // Not owned. The destructor of ReservationBuffer should erase itself from | 113 // Not owned. The destructor of ReservationBuffer should erase itself from |
| 112 // |reservation_buffers_| by calling ReleaseReservationBuffer. | 114 // |reservation_buffers_| by calling ReleaseReservationBuffer. |
| 113 ReservationBufferByOriginAndType reservation_buffers_; | 115 ReservationBufferByOriginAndType reservation_buffers_; |
| 114 | 116 |
| 115 base::SequenceChecker sequence_checker_; | 117 base::SequenceChecker sequence_checker_; |
| 116 base::WeakPtrFactory<QuotaReservationManager> weak_ptr_factory_; | 118 base::WeakPtrFactory<QuotaReservationManager> weak_ptr_factory_; |
| 117 | 119 |
| 118 DISALLOW_COPY_AND_ASSIGN(QuotaReservationManager); | 120 DISALLOW_COPY_AND_ASSIGN(QuotaReservationManager); |
| 119 }; | 121 }; |
| 120 | 122 |
| 121 } // namespace fileapi | 123 } // namespace fileapi |
| 122 | 124 |
| 123 #endif // WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_ | 125 #endif // WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_MANAGER_H_ |
| OLD | NEW |