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/open_file_handle_context.h"
7 #include "base/files/file_util.h"
8 #include "storage/browser/fileapi/quota/quota_reservation_buffer.h"
12 OpenFileHandleContext::OpenFileHandleContext(
13 const base::FilePath
& platform_path
,
14 QuotaReservationBuffer
* reservation_buffer
)
15 : initial_file_size_(0),
16 maximum_written_offset_(0),
17 append_mode_write_amount_(0),
18 platform_path_(platform_path
),
19 reservation_buffer_(reservation_buffer
) {
20 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
22 base::GetFileSize(platform_path
, &initial_file_size_
);
23 maximum_written_offset_
= initial_file_size_
;
26 int64
OpenFileHandleContext::UpdateMaxWrittenOffset(int64 offset
) {
27 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
28 if (offset
<= maximum_written_offset_
)
31 int64 growth
= offset
- maximum_written_offset_
;
32 maximum_written_offset_
= offset
;
36 void OpenFileHandleContext::AddAppendModeWriteAmount(int64 amount
) {
37 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
38 append_mode_write_amount_
+= amount
;
41 int64
OpenFileHandleContext::GetEstimatedFileSize() const {
42 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
43 return maximum_written_offset_
+ append_mode_write_amount_
;
46 int64
OpenFileHandleContext::GetMaxWrittenOffset() const {
47 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
48 return maximum_written_offset_
;
51 OpenFileHandleContext::~OpenFileHandleContext() {
52 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
54 // TODO(tzik): Optimize this for single operation.
57 base::GetFileSize(platform_path_
, &file_size
);
58 int64 usage_delta
= file_size
- initial_file_size_
;
60 // |reserved_quota_consumption| may be greater than the recorded file growth
61 // when a plugin crashed before reporting its consumption.
62 // In this case, the reserved quota for the plugin should be handled as
64 int64 reserved_quota_consumption
=
65 std::max(GetEstimatedFileSize(), file_size
) - initial_file_size_
;
67 reservation_buffer_
->CommitFileGrowth(
68 reserved_quota_consumption
, usage_delta
);
69 reservation_buffer_
->DetachOpenFileHandleContext(this);
72 } // namespace storage