IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / quota_reservation.cc
blob018fdfd2efc5730118eaa8e9547537ba115fdf24
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"
7 #include "base/bind.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"
14 namespace content {
16 // static
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) {
30 quota_reservation_ =
31 file_system_context->CreateQuotaReservationOnFileTaskRunner(
32 origin_url,
33 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) {
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)
48 delete it->second;
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;
62 NOTREACHED();
63 return 0;
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);
71 files_.erase(it);
72 } else {
73 NOTREACHED();
77 void QuotaReservation::ReserveQuota(
78 int64_t amount,
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);
85 else
86 NOTREACHED();
89 quota_reservation_->RefreshReservation(
90 amount,
91 base::Bind(&QuotaReservation::GotReservedQuota,
92 this,
93 callback));
96 void QuotaReservation::OnClientCrash() {
97 quota_reservation_->OnClientCrash();
100 void QuotaReservation::GotReservedQuota(
101 const ReserveQuotaCallback& callback,
102 base::PlatformFileError error) {
103 OffsetMap max_written_offsets;
104 for (FileMap::iterator it = files_.begin(); it != files_.end(); ++ it) {
105 max_written_offsets.insert(
106 std::make_pair(it->first, it->second->base_file_size()));
109 if (file_system_context_) {
110 BrowserThread::PostTask(
111 BrowserThread::IO,
112 FROM_HERE,
113 base::Bind(callback,
114 quota_reservation_->remaining_quota(),
115 max_written_offsets));
116 } else {
117 // Unit testing code path.
118 callback.Run(quota_reservation_->remaining_quota(), max_written_offsets);
122 void QuotaReservation::DeleteOnCorrectThread() const {
123 if (file_system_context_ &&
124 !file_system_context_->
125 default_file_task_runner()->RunsTasksOnCurrentThread()) {
126 file_system_context_->default_file_task_runner()->DeleteSoon(
127 FROM_HERE,
128 this);
129 } else {
130 // We're on the right thread to delete, or unit test.
131 delete this;
135 } // namespace content