Ignore non-active fullscreen windows for shelf state.
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / quota_reservation.cc
blobed2821209c8ed5ef8ac1c530bb7c014a1fd080a6
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::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(
107 BrowserThread::IO,
108 FROM_HERE,
109 base::Bind(callback,
110 quota_reservation_->remaining_quota(),
111 max_written_offsets));
112 } else {
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(
121 FROM_HERE,
122 this);
123 } else {
124 // Unit testing code path.
125 delete this;
129 } // namespace content