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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_
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"
21 class QuotaReservation
;
26 struct QuotaReservationDeleter
;
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
> {
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
);
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
&)>
52 void ReserveQuota(int64_t amount
,
53 const ppapi::FileGrowthMap
& file_growth
,
54 const ReserveQuotaCallback
& callback
);
56 // Notifies underlying QuotaReservation that the associated client crashed,
57 // and that the reserved quota is no longer traceable.
61 friend class base::RefCountedThreadSafe
<QuotaReservation
,
62 QuotaReservationDeleter
>;
63 friend class base::DeleteHelper
<QuotaReservation
>;
64 friend struct QuotaReservationDeleter
;
65 friend class QuotaReservationTest
;
68 scoped_refptr
<storage::FileSystemContext
> file_system_context
,
69 const GURL
& origin_url
,
70 storage::FileSystemType file_system_type
);
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
);
80 void GotReservedQuota(const ReserveQuotaCallback
& callback
,
81 base::File::Error error
);
83 void DeleteOnCorrectThread() const;
85 scoped_refptr
<storage::FileSystemContext
> file_system_context_
;
86 scoped_refptr
<storage::QuotaReservation
> quota_reservation_
;
87 typedef std::map
<int32_t, storage::OpenFileHandle
*> FileMap
;
90 DISALLOW_COPY_AND_ASSIGN(QuotaReservation
);
93 struct QuotaReservationDeleter
{
94 static void Destruct(const QuotaReservation
* quota_reservation
) {
95 quota_reservation
->DeleteOnCorrectThread();
99 } // namespace content
101 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_