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.
16 #include "webkit/browser/fileapi/file_system_context.h"
24 class QuotaReservation
;
29 struct QuotaReservationDeleter
;
31 // This class holds a QuotaReservation and manages OpenFileHandles for checking
32 // quota. It should be created, used, and destroyed on a FileSystemContext's
33 // default_file_task_runner() instance. This is a RefCountedThreadSafe object
34 // because it needs to be passed to the file thread and kept alive during
35 // potentially long-running quota operations.
36 class CONTENT_EXPORT QuotaReservation
37 : public base::RefCountedThreadSafe
<QuotaReservation
,
38 QuotaReservationDeleter
> {
40 // Static method to facilitate construction on the file task runner.
41 static scoped_refptr
<QuotaReservation
> Create(
42 scoped_refptr
<fileapi::FileSystemContext
> file_system_context
,
43 const GURL
& origin_url
,
44 fileapi::FileSystemType file_system_type
);
46 // Opens a file with the given id and path and returns its current size.
47 int64_t OpenFile(int32_t id
, const base::FilePath
& file_path
);
48 // Closes the file opened by OpenFile with the given id.
49 void CloseFile(int32_t id
, int64_t max_written_offset
);
50 // Refreshes the quota reservation to a new amount. A map that associates file
51 // ids with maximum written offsets is provided as input. The callback will
52 // receive a similar map with the updated file sizes.
53 typedef std::map
<int32_t, int64_t> OffsetMap
;
54 typedef base::Callback
<void(int64_t, const OffsetMap
&)> ReserveQuotaCallback
;
55 void ReserveQuota(int64_t amount
,
56 const OffsetMap
& max_written_offsets
,
57 const ReserveQuotaCallback
& callback
);
59 // Notifies underlying QuotaReservation that the associated client crashed,
60 // and that the reserved quota is no longer traceable.
64 friend class base::RefCountedThreadSafe
<QuotaReservation
,
65 QuotaReservationDeleter
>;
66 friend class base::DeleteHelper
<QuotaReservation
>;
67 friend struct QuotaReservationDeleter
;
68 friend class QuotaReservationTest
;
71 scoped_refptr
<fileapi::FileSystemContext
> file_system_context
,
72 const GURL
& origin_url
,
73 fileapi::FileSystemType file_system_type
);
75 // For unit testing only. A QuotaReservation intended for unit testing will
76 // have file_system_context_ == NULL.
78 scoped_refptr
<fileapi::QuotaReservation
> quota_reservation
,
79 const GURL
& origin_url
,
80 fileapi::FileSystemType file_system_type
);
84 void GotReservedQuota(const ReserveQuotaCallback
& callback
,
85 base::PlatformFileError error
);
87 void DeleteOnCorrectThread() const;
89 scoped_refptr
<fileapi::FileSystemContext
> file_system_context_
;
90 scoped_refptr
<fileapi::QuotaReservation
> quota_reservation_
;
91 typedef std::map
<int32_t, fileapi::OpenFileHandle
*> FileMap
;
94 DISALLOW_COPY_AND_ASSIGN(QuotaReservation
);
97 struct QuotaReservationDeleter
{
98 static void Destruct(const QuotaReservation
* quota_reservation
) {
99 quota_reservation
->DeleteOnCorrectThread();
103 } // namespace content
105 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_