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 #include "content/browser/renderer_host/pepper/quota_reservation.h"
8 #include "base/callback.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "webkit/browser/fileapi/quota/open_file_handle.h"
11 #include "webkit/browser/fileapi/quota/quota_reservation.h"
12 #include "webkit/common/fileapi/file_system_util.h"
17 scoped_refptr
<QuotaReservation
> QuotaReservation::Create(
18 scoped_refptr
<fileapi::FileSystemContext
> file_system_context
,
19 const GURL
& origin_url
,
20 fileapi::FileSystemType type
) {
21 return scoped_refptr
<QuotaReservation
>(new QuotaReservation(
22 file_system_context
, origin_url
, type
));
25 QuotaReservation::QuotaReservation(
26 scoped_refptr
<fileapi::FileSystemContext
> file_system_context
,
27 const GURL
& origin_url
,
28 fileapi::FileSystemType file_system_type
)
29 : file_system_context_(file_system_context
) {
31 file_system_context
->CreateQuotaReservationOnFileTaskRunner(
36 // For unit testing only.
37 QuotaReservation::QuotaReservation(
38 scoped_refptr
<fileapi::QuotaReservation
> quota_reservation
,
39 const GURL
& /* origin_url */,
40 fileapi::FileSystemType
/* file_system_type */)
41 : quota_reservation_(quota_reservation
) {
44 QuotaReservation::~QuotaReservation() {
45 // We should have no open files at this point.
46 DCHECK(files_
.size() == 0);
47 for (FileMap::iterator it
= files_
.begin(); it
!= files_
.end(); ++ it
)
51 int64_t QuotaReservation::OpenFile(int32_t id
,
52 const base::FilePath
& file_path
) {
53 scoped_ptr
<fileapi::OpenFileHandle
> file_handle
=
54 quota_reservation_
->GetOpenFileHandle(file_path
);
55 std::pair
<FileMap::iterator
, bool> insert_result
=
56 files_
.insert(std::make_pair(id
, file_handle
.get()));
57 if (insert_result
.second
) {
58 int64_t max_written_offset
= file_handle
->base_file_size();
59 ignore_result(file_handle
.release());
60 return max_written_offset
;
66 void QuotaReservation::CloseFile(int32_t id
,
67 int64_t max_written_offset
) {
68 FileMap::iterator it
= files_
.find(id
);
69 if (it
!= files_
.end()) {
70 it
->second
->UpdateMaxWrittenOffset(max_written_offset
);
77 void QuotaReservation::ReserveQuota(
79 const OffsetMap
& max_written_offsets
,
80 const ReserveQuotaCallback
& callback
) {
81 for (FileMap::iterator it
= files_
.begin(); it
!= files_
.end(); ++ it
) {
82 OffsetMap::const_iterator offset_it
= max_written_offsets
.find(it
->first
);
83 if (offset_it
!= max_written_offsets
.end())
84 it
->second
->UpdateMaxWrittenOffset(offset_it
->second
);
89 quota_reservation_
->RefreshReservation(
91 base::Bind(&QuotaReservation::GotReservedQuota
,
96 void QuotaReservation::GotReservedQuota(
97 const ReserveQuotaCallback
& callback
,
98 base::PlatformFileError error
) {
99 OffsetMap max_written_offsets
;
100 for (FileMap::iterator it
= files_
.begin(); it
!= files_
.end(); ++ it
) {
101 max_written_offsets
.insert(
102 std::make_pair(it
->first
, it
->second
->base_file_size()));
105 if (file_system_context_
) {
106 BrowserThread::PostTask(
110 quota_reservation_
->remaining_quota(),
111 max_written_offsets
));
113 // Unit testing code path.
114 callback
.Run(quota_reservation_
->remaining_quota(), max_written_offsets
);
118 void QuotaReservation::DeleteOnCorrectThread() const {
119 if (file_system_context_
) {
120 file_system_context_
->default_file_task_runner()->DeleteSoon(
124 // Unit testing code path.
129 } // namespace content