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_buffer.h"
8 #include "storage/browser/fileapi/quota/open_file_handle.h"
9 #include "storage/browser/fileapi/quota/open_file_handle_context.h"
10 #include "storage/browser/fileapi/quota/quota_reservation.h"
14 QuotaReservationBuffer::QuotaReservationBuffer(
15 base::WeakPtr
<QuotaReservationManager
> reservation_manager
,
18 : reservation_manager_(reservation_manager
),
22 DCHECK(origin
.is_valid());
23 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
24 reservation_manager_
->IncrementDirtyCount(origin
, type
);
27 scoped_refptr
<QuotaReservation
> QuotaReservationBuffer::CreateReservation() {
28 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
29 return make_scoped_refptr(new QuotaReservation(this));
32 scoped_ptr
<OpenFileHandle
> QuotaReservationBuffer::GetOpenFileHandle(
33 QuotaReservation
* reservation
,
34 const base::FilePath
& platform_path
) {
35 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
36 OpenFileHandleContext
** open_file
= &open_files_
[platform_path
];
38 *open_file
= new OpenFileHandleContext(platform_path
, this);
39 return make_scoped_ptr(new OpenFileHandle(reservation
, *open_file
));
42 void QuotaReservationBuffer::CommitFileGrowth(int64 reserved_quota_consumption
,
44 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
45 if (!reservation_manager_
)
47 reservation_manager_
->CommitQuotaUsage(origin_
, type_
, usage_delta
);
49 if (reserved_quota_consumption
> 0) {
50 if (reserved_quota_consumption
> reserved_quota_
) {
51 LOG(ERROR
) << "Detected over consumption of the storage quota beyond its"
53 reserved_quota_consumption
= reserved_quota_
;
56 reserved_quota_
-= reserved_quota_consumption
;
57 reservation_manager_
->ReleaseReservedQuota(
58 origin_
, type_
, reserved_quota_consumption
);
62 void QuotaReservationBuffer::DetachOpenFileHandleContext(
63 OpenFileHandleContext
* open_file
) {
64 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
65 DCHECK_EQ(open_file
, open_files_
[open_file
->platform_path()]);
66 open_files_
.erase(open_file
->platform_path());
69 void QuotaReservationBuffer::PutReservationToBuffer(int64 reservation
) {
70 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
71 DCHECK_LE(0, reservation
);
72 reserved_quota_
+= reservation
;
75 QuotaReservationBuffer::~QuotaReservationBuffer() {
76 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
77 if (!reservation_manager_
)
80 DCHECK_LE(0, reserved_quota_
);
81 if (reserved_quota_
&& reservation_manager_
) {
82 reservation_manager_
->ReserveQuota(
83 origin_
, type_
, -reserved_quota_
,
84 base::Bind(&QuotaReservationBuffer::DecrementDirtyCount
,
85 reservation_manager_
, origin_
, type_
));
87 reservation_manager_
->ReleaseReservationBuffer(this);
91 bool QuotaReservationBuffer::DecrementDirtyCount(
92 base::WeakPtr
<QuotaReservationManager
> reservation_manager
,
95 base::File::Error error
,
97 DCHECK(origin
.is_valid());
98 if (error
== base::File::FILE_OK
&& reservation_manager
) {
99 reservation_manager
->DecrementDirtyCount(origin
, type
);
105 } // namespace storage