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