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"
8 #include "storage/browser/fileapi/quota/open_file_handle.h"
9 #include "storage/browser/fileapi/quota/quota_reservation_buffer.h"
13 void QuotaReservation::RefreshReservation(
15 const StatusCallback
& callback
) {
16 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
17 DCHECK(!running_refresh_request_
);
18 DCHECK(!client_crashed_
);
19 if (!reservation_manager())
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_
)
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_
);
51 void QuotaReservation::ConsumeReservation(int64 size
) {
52 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
54 DCHECK_LE(size
, remaining_quota_
);
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),
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_
);
94 bool QuotaReservation::AdaptDidUpdateReservedQuota(
95 const base::WeakPtr
<QuotaReservation
>& reservation
,
97 const StatusCallback
& callback
,
98 base::File::Error error
,
103 return reservation
->DidUpdateReservedQuota(
104 previous_size
, callback
, error
, delta
);
107 bool QuotaReservation::DidUpdateReservedQuota(
109 const StatusCallback
& callback
,
110 base::File::Error error
,
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
);
121 if (error
== base::File::FILE_OK
)
122 remaining_quota_
= previous_size
+ delta
;
127 } // namespace storage