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 CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_ 7 8 #include <map> 9 10 #include "base/basictypes.h" 11 #include "base/callback.h" 12 #include "content/common/content_export.h" 13 #include "ppapi/c/pp_stdint.h" // For int64_t on Windows. 14 #include "ppapi/shared_impl/file_growth.h" 15 #include "storage/browser/fileapi/file_system_context.h" 16 #include "url/gurl.h" 17 18 namespace storage { 19 class FileSystemURL; 20 class OpenFileHandle; 21 class QuotaReservation; 22 } 23 24 namespace content { 25 26 struct QuotaReservationDeleter; 27 28 // This class holds a QuotaReservation and manages OpenFileHandles for checking 29 // quota. It should be created, used, and destroyed on a FileSystemContext's 30 // default_file_task_runner() instance. This is a RefCountedThreadSafe object 31 // because it needs to be passed to the file thread and kept alive during 32 // potentially long-running quota operations. 33 class CONTENT_EXPORT QuotaReservation 34 : public base::RefCountedThreadSafe<QuotaReservation, 35 QuotaReservationDeleter> { 36 public: 37 // Static method to facilitate construction on the file task runner. 38 static scoped_refptr<QuotaReservation> Create( 39 scoped_refptr<storage::FileSystemContext> file_system_context, 40 const GURL& origin_url, 41 storage::FileSystemType file_system_type); 42 43 // Opens a file with the given id and path and returns its current size. 44 int64_t OpenFile(int32_t id, const storage::FileSystemURL& url); 45 // Closes the file opened by OpenFile with the given id. 46 void CloseFile(int32_t id, const ppapi::FileGrowth& file_growth); 47 // Refreshes the quota reservation to a new amount. A map that associates file 48 // ids with maximum written offsets is provided as input. The callback will 49 // receive a similar map with the updated file sizes. 50 typedef base::Callback<void(int64_t, const ppapi::FileSizeMap&)> 51 ReserveQuotaCallback; 52 void ReserveQuota(int64_t amount, 53 const ppapi::FileGrowthMap& file_growth, 54 const ReserveQuotaCallback& callback); 55 56 // Notifies underlying QuotaReservation that the associated client crashed, 57 // and that the reserved quota is no longer traceable. 58 void OnClientCrash(); 59 60 private: 61 friend class base::RefCountedThreadSafe<QuotaReservation, 62 QuotaReservationDeleter>; 63 friend class base::DeleteHelper<QuotaReservation>; 64 friend struct QuotaReservationDeleter; 65 friend class QuotaReservationTest; 66 67 QuotaReservation( 68 scoped_refptr<storage::FileSystemContext> file_system_context, 69 const GURL& origin_url, 70 storage::FileSystemType file_system_type); 71 72 // For unit testing only. A QuotaReservation intended for unit testing will 73 // have file_system_context_ == NULL. 74 QuotaReservation(scoped_refptr<storage::QuotaReservation> quota_reservation, 75 const GURL& origin_url, 76 storage::FileSystemType file_system_type); 77 78 ~QuotaReservation(); 79 80 void GotReservedQuota(const ReserveQuotaCallback& callback, 81 base::File::Error error); 82 83 void DeleteOnCorrectThread() const; 84 85 scoped_refptr<storage::FileSystemContext> file_system_context_; 86 scoped_refptr<storage::QuotaReservation> quota_reservation_; 87 typedef std::map<int32_t, storage::OpenFileHandle*> FileMap; 88 FileMap files_; 89 90 DISALLOW_COPY_AND_ASSIGN(QuotaReservation); 91 }; 92 93 struct QuotaReservationDeleter { DestructQuotaReservationDeleter94 static void Destruct(const QuotaReservation* quota_reservation) { 95 quota_reservation->DeleteOnCorrectThread(); 96 } 97 }; 98 99 } // namespace content 100 101 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_ 102