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/file_system_operation_runner.h"
11 #include "webkit/browser/fileapi/quota/open_file_handle.h"
12 #include "webkit/browser/fileapi/quota/quota_reservation.h"
13 #include "webkit/common/fileapi/file_system_util.h"
18 scoped_refptr
<QuotaReservation
> QuotaReservation::Create(
19 scoped_refptr
<fileapi::FileSystemContext
> file_system_context
,
20 const GURL
& origin_url
,
21 fileapi::FileSystemType type
) {
22 return scoped_refptr
<QuotaReservation
>(
23 new QuotaReservation(file_system_context
, origin_url
, type
));
26 QuotaReservation::QuotaReservation(
27 scoped_refptr
<fileapi::FileSystemContext
> file_system_context
,
28 const GURL
& origin_url
,
29 fileapi::FileSystemType file_system_type
)
30 : file_system_context_(file_system_context
) {
32 file_system_context
->CreateQuotaReservationOnFileTaskRunner(
33 origin_url
, file_system_type
);
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
) {}
43 QuotaReservation::~QuotaReservation() {
44 // We should have no open files at this point.
45 DCHECK(files_
.size() == 0);
46 for (FileMap::iterator it
= files_
.begin(); it
!= files_
.end(); ++it
)
50 int64_t QuotaReservation::OpenFile(int32_t id
,
51 const fileapi::FileSystemURL
& url
) {
52 base::FilePath platform_file_path
;
53 if (file_system_context_
) {
54 base::File::Error error
=
55 file_system_context_
->operation_runner()->SyncGetPlatformPath(
56 url
, &platform_file_path
);
57 if (error
!= base::File::FILE_OK
) {
63 platform_file_path
= url
.path();
66 scoped_ptr
<fileapi::OpenFileHandle
> file_handle
=
67 quota_reservation_
->GetOpenFileHandle(platform_file_path
);
68 std::pair
<FileMap::iterator
, bool> insert_result
=
69 files_
.insert(std::make_pair(id
, file_handle
.get()));
70 if (insert_result
.second
) {
71 int64_t max_written_offset
= file_handle
->GetMaxWrittenOffset();
72 ignore_result(file_handle
.release());
73 return max_written_offset
;
79 void QuotaReservation::CloseFile(int32_t id
,
80 const ppapi::FileGrowth
& file_growth
) {
81 FileMap::iterator it
= files_
.find(id
);
82 if (it
!= files_
.end()) {
83 it
->second
->UpdateMaxWrittenOffset(file_growth
.max_written_offset
);
84 it
->second
->AddAppendModeWriteAmount(file_growth
.append_mode_write_amount
);
92 void QuotaReservation::ReserveQuota(int64_t amount
,
93 const ppapi::FileGrowthMap
& file_growths
,
94 const ReserveQuotaCallback
& callback
) {
95 for (FileMap::iterator it
= files_
.begin(); it
!= files_
.end(); ++it
) {
96 ppapi::FileGrowthMap::const_iterator growth_it
=
97 file_growths
.find(it
->first
);
98 if (growth_it
!= file_growths
.end()) {
99 it
->second
->UpdateMaxWrittenOffset(growth_it
->second
.max_written_offset
);
100 it
->second
->AddAppendModeWriteAmount(
101 growth_it
->second
.append_mode_write_amount
);
107 quota_reservation_
->RefreshReservation(
108 amount
, base::Bind(&QuotaReservation::GotReservedQuota
, this, callback
));
111 void QuotaReservation::OnClientCrash() { quota_reservation_
->OnClientCrash(); }
113 void QuotaReservation::GotReservedQuota(const ReserveQuotaCallback
& callback
,
114 base::File::Error error
) {
115 ppapi::FileSizeMap file_sizes
;
116 for (FileMap::iterator it
= files_
.begin(); it
!= files_
.end(); ++it
)
117 file_sizes
[it
->first
] = it
->second
->GetMaxWrittenOffset();
119 if (file_system_context_
) {
120 BrowserThread::PostTask(
124 callback
, quota_reservation_
->remaining_quota(), file_sizes
));
126 // Unit testing code path.
127 callback
.Run(quota_reservation_
->remaining_quota(), file_sizes
);
131 void QuotaReservation::DeleteOnCorrectThread() const {
132 if (file_system_context_
&& !file_system_context_
->default_file_task_runner()
133 ->RunsTasksOnCurrentThread()) {
134 file_system_context_
->default_file_task_runner()->DeleteSoon(FROM_HERE
,
137 // We're on the right thread to delete, or unit test.
142 } // namespace content