Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / storage / browser / fileapi / quota / quota_reservation.cc
blobfff7f163175904d5fa0783e58d6c999293c613a3
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 "storage/browser/fileapi/quota/quota_reservation.h"
7 #include "base/bind.h"
8 #include "storage/browser/fileapi/quota/open_file_handle.h"
9 #include "storage/browser/fileapi/quota/quota_reservation_buffer.h"
11 namespace storage {
13 void QuotaReservation::RefreshReservation(
14 int64 size,
15 const StatusCallback& callback) {
16 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
17 DCHECK(!running_refresh_request_);
18 DCHECK(!client_crashed_);
19 if (!reservation_manager())
20 return;
22 running_refresh_request_ = true;
24 reservation_manager()->ReserveQuota(
25 origin(), type(), size - remaining_quota_,
26 base::Bind(&QuotaReservation::AdaptDidUpdateReservedQuota,
27 weak_ptr_factory_.GetWeakPtr(),
28 remaining_quota_, callback));
30 if (running_refresh_request_)
31 remaining_quota_ = 0;
34 scoped_ptr<OpenFileHandle> QuotaReservation::GetOpenFileHandle(
35 const base::FilePath& platform_path) {
36 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
37 DCHECK(!client_crashed_);
38 return reservation_buffer_->GetOpenFileHandle(this, platform_path);
41 void QuotaReservation::OnClientCrash() {
42 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
43 client_crashed_ = true;
45 if (remaining_quota_) {
46 reservation_buffer_->PutReservationToBuffer(remaining_quota_);
47 remaining_quota_ = 0;
51 void QuotaReservation::ConsumeReservation(int64 size) {
52 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
53 DCHECK_LT(0, size);
54 DCHECK_LE(size, remaining_quota_);
55 if (client_crashed_)
56 return;
58 remaining_quota_ -= size;
59 reservation_buffer_->PutReservationToBuffer(size);
62 QuotaReservationManager* QuotaReservation::reservation_manager() {
63 return reservation_buffer_->reservation_manager();
66 const GURL& QuotaReservation::origin() const {
67 return reservation_buffer_->origin();
70 FileSystemType QuotaReservation::type() const {
71 return reservation_buffer_->type();
74 QuotaReservation::QuotaReservation(
75 QuotaReservationBuffer* reservation_buffer)
76 : client_crashed_(false),
77 running_refresh_request_(false),
78 remaining_quota_(0),
79 reservation_buffer_(reservation_buffer),
80 weak_ptr_factory_(this) {
81 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
84 QuotaReservation::~QuotaReservation() {
85 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
87 if (remaining_quota_ && reservation_manager()) {
88 reservation_manager()->ReleaseReservedQuota(
89 origin(), type(), remaining_quota_);
93 // static
94 bool QuotaReservation::AdaptDidUpdateReservedQuota(
95 const base::WeakPtr<QuotaReservation>& reservation,
96 int64 previous_size,
97 const StatusCallback& callback,
98 base::File::Error error,
99 int64 delta) {
100 if (!reservation)
101 return false;
103 return reservation->DidUpdateReservedQuota(
104 previous_size, callback, error, delta);
107 bool QuotaReservation::DidUpdateReservedQuota(
108 int64 previous_size,
109 const StatusCallback& callback,
110 base::File::Error error,
111 int64 delta) {
112 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
113 DCHECK(running_refresh_request_);
114 running_refresh_request_ = false;
116 if (client_crashed_) {
117 callback.Run(base::File::FILE_ERROR_ABORT);
118 return false;
121 if (error == base::File::FILE_OK)
122 remaining_quota_ = previous_size + delta;
123 callback.Run(error);
124 return true;
127 } // namespace storage