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