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. 4 5 #include "webkit/browser/fileapi/quota/open_file_handle_context.h" 6 7 #include "base/file_util.h" 8 #include "webkit/browser/fileapi/quota/quota_reservation_buffer.h" 9 10 namespace fileapi { 11 12 OpenFileHandleContext::OpenFileHandleContext( 13 const base::FilePath& platform_path, 14 QuotaReservationBuffer* reservation_buffer) 15 : initial_file_size_(0), 16 maximum_written_offset_(0), 17 platform_path_(platform_path), 18 reservation_buffer_(reservation_buffer) { 19 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); 20 21 base::GetFileSize(platform_path, &initial_file_size_); 22 maximum_written_offset_ = initial_file_size_; 23 } 24 25 void OpenFileHandleContext::UpdateMaxWrittenOffset( 26 int64 offset, 27 int64* new_file_size, 28 int64* growth) { 29 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); 30 if (offset > maximum_written_offset_) { 31 *growth = offset - maximum_written_offset_; 32 maximum_written_offset_ = offset; 33 } else { 34 *growth = 0; 35 } 36 37 *new_file_size = maximum_written_offset_; 38 } 39 40 OpenFileHandleContext::~OpenFileHandleContext() { 41 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); 42 43 // TODO(tzik): Optimize this for single operation. 44 45 int64 file_size = 0; 46 base::GetFileSize(platform_path_, &file_size); 47 int64 usage_delta = file_size - initial_file_size_; 48 49 // |quota_consumption| may be greater than the recorded file growth when a 50 // plugin crashed before reporting its consumption. 51 // In this case, the reserved quota for the plugin should be handled as 52 // consumed quota. 53 int64 quota_consumption = 54 std::max(maximum_written_offset_, file_size) - initial_file_size_; 55 56 reservation_buffer_->CommitFileGrowth(quota_consumption, usage_delta); 57 reservation_buffer_->DetachOpenFileHandleContext(this); 58 } 59 60 } // namespace fileapi 61