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>(new QuotaReservation(
23 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(
37 // For unit testing only.
38 QuotaReservation::QuotaReservation(
39 scoped_refptr<fileapi::QuotaReservation> quota_reservation,
40 const GURL& /* origin_url */,
41 fileapi::FileSystemType /* file_system_type */)
42 : quota_reservation_(quota_reservation) {
45 QuotaReservation::~QuotaReservation() {
46 // We should have no open files at this point.
47 DCHECK(files_.size() == 0);
48 for (FileMap::iterator it = files_.begin(); it != files_.end(); ++ it)
52 int64_t QuotaReservation::OpenFile(int32_t id,
53 const fileapi::FileSystemURL& url) {
54 base::FilePath platform_file_path;
55 if (file_system_context_) {
56 base::File::Error error =
57 file_system_context_->operation_runner()->SyncGetPlatformPath(
58 url, &platform_file_path);
59 if (error != base::File::FILE_OK) {
65 platform_file_path = url.path();
68 scoped_ptr<fileapi::OpenFileHandle> file_handle =
69 quota_reservation_->GetOpenFileHandle(platform_file_path);
70 std::pair<FileMap::iterator, bool> insert_result =
71 files_.insert(std::make_pair(id, file_handle.get()));
72 if (insert_result.second) {
73 int64_t max_written_offset = file_handle->GetMaxWrittenOffset();
74 ignore_result(file_handle.release());
75 return max_written_offset;
81 void QuotaReservation::CloseFile(int32_t id,
82 const ppapi::FileGrowth& file_growth) {
83 FileMap::iterator it = files_.find(id);
84 if (it != files_.end()) {
85 it->second->UpdateMaxWrittenOffset(file_growth.max_written_offset);
86 it->second->AddAppendModeWriteAmount(file_growth.append_mode_write_amount);
94 void QuotaReservation::ReserveQuota(
96 const ppapi::FileGrowthMap& file_growths,
97 const ReserveQuotaCallback& callback) {
98 for (FileMap::iterator it = files_.begin(); it != files_.end(); ++it) {
99 ppapi::FileGrowthMap::const_iterator growth_it =
100 file_growths.find(it->first);
101 if (growth_it != file_growths.end()) {
102 it->second->UpdateMaxWrittenOffset(growth_it->second.max_written_offset);
103 it->second->AddAppendModeWriteAmount(
104 growth_it->second.append_mode_write_amount);
110 quota_reservation_->RefreshReservation(
112 base::Bind(&QuotaReservation::GotReservedQuota,
117 void QuotaReservation::OnClientCrash() {
118 quota_reservation_->OnClientCrash();
121 void QuotaReservation::GotReservedQuota(
122 const ReserveQuotaCallback& callback,
123 base::File::Error error) {
124 ppapi::FileSizeMap file_sizes;
125 for (FileMap::iterator it = files_.begin(); it != files_.end(); ++ it)
126 file_sizes[it->first] = it->second->GetMaxWrittenOffset();
128 if (file_system_context_) {
129 BrowserThread::PostTask(
133 quota_reservation_->remaining_quota(),
136 // Unit testing code path.
137 callback.Run(quota_reservation_->remaining_quota(), file_sizes);
141 void QuotaReservation::DeleteOnCorrectThread() const {
142 if (file_system_context_ &&
143 !file_system_context_->
144 default_file_task_runner()->RunsTasksOnCurrentThread()) {
145 file_system_context_->default_file_task_runner()->DeleteSoon(
149 // We're on the right thread to delete, or unit test.
154 } // namespace content