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_H_ 6 #define WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_H_ 7 8 #include "base/basictypes.h" 9 #include "base/files/file_path.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/weak_ptr.h" 12 #include "webkit/browser/fileapi/quota/quota_reservation_manager.h" 13 #include "webkit/browser/webkit_storage_browser_export.h" 14 #include "webkit/common/fileapi/file_system_types.h" 15 16 class GURL; 17 18 namespace fileapi { 19 20 class QuotaReservationBuffer; 21 class OpenFileHandle; 22 23 // Represents a unit of quota reservation. 24 class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservation 25 : public base::RefCounted<QuotaReservation> { 26 public: 27 typedef base::Callback<void(base::PlatformFileError error)> StatusCallback; 28 29 // Reclaims unused quota and reserves another |size| of quota. So that the 30 // resulting new |remaining_quota| will be same as |size|. 31 // Invokes |callback| upon completion. 32 void RefreshReservation(int64 size, const StatusCallback& callback); 33 34 // Associates |platform_path| to the QuotaReservation instance. 35 // Returns an OpenFileHandle instance that represents a quota managed file. 36 scoped_ptr<OpenFileHandle> GetOpenFileHandle( 37 const base::FilePath& platform_path); 38 39 // Should be called when the associated client is crashed. 40 // This implies the client can no longer report its consumption of the 41 // reserved quota. 42 // QuotaReservation puts all remaining quota to the QuotaReservationBuffer, so 43 // that the remaining quota will be reclaimed after all open files associated 44 // to the origin and type. 45 void OnClientCrash(); 46 47 // Consumes |size| of reserved quota for a associated file. 48 // Consumed quota is sent to associated QuotaReservationBuffer for staging. 49 void ConsumeReservation(int64 size); 50 51 // Returns amount of unused reserved quota. 52 int64 remaining_quota() const { return remaining_quota_; } 53 54 QuotaReservationManager* reservation_manager(); 55 const GURL& origin() const; 56 FileSystemType type() const; 57 58 private: 59 friend class QuotaReservationBuffer; 60 61 // Use QuotaReservationManager as the entry point. 62 explicit QuotaReservation(QuotaReservationBuffer* reservation_buffer); 63 64 friend class base::RefCounted<QuotaReservation>; 65 virtual ~QuotaReservation(); 66 67 static bool AdaptDidUpdateReservedQuota( 68 const base::WeakPtr<QuotaReservation>& reservation, 69 int64 new_reserved_size, 70 const StatusCallback& callback, 71 base::PlatformFileError error); 72 void DidUpdateReservedQuota(int64 new_reserved_size, 73 const StatusCallback& callback, 74 base::PlatformFileError error); 75 76 bool running_refresh_request_; 77 int64 remaining_quota_; 78 79 scoped_refptr<QuotaReservationBuffer> reservation_buffer_; 80 81 base::SequenceChecker sequence_checker_; 82 base::WeakPtrFactory<QuotaReservation> weak_ptr_factory_; 83 84 DISALLOW_COPY_AND_ASSIGN(QuotaReservation); 85 }; 86 87 } // namespace fileapi 88 89 #endif // WEBKIT_BROWSER_FILEAPI_QUOTA_QUOTA_RESERVATION_H_ 90