Home | History | Annotate | Download | only in quota
      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       append_mode_write_amount_(0),
     18       platform_path_(platform_path),
     19       reservation_buffer_(reservation_buffer) {
     20   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
     21 
     22   base::GetFileSize(platform_path, &initial_file_size_);
     23   maximum_written_offset_ = initial_file_size_;
     24 }
     25 
     26 int64 OpenFileHandleContext::UpdateMaxWrittenOffset(int64 offset) {
     27   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
     28   if (offset <= maximum_written_offset_)
     29     return 0;
     30 
     31   int64 growth = offset - maximum_written_offset_;
     32   maximum_written_offset_ = offset;
     33   return growth;
     34 }
     35 
     36 void OpenFileHandleContext::AddAppendModeWriteAmount(int64 amount) {
     37   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
     38   append_mode_write_amount_ += amount;
     39 }
     40 
     41 int64 OpenFileHandleContext::GetEstimatedFileSize() const {
     42   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
     43   return maximum_written_offset_ + append_mode_write_amount_;
     44 }
     45 
     46 int64 OpenFileHandleContext::GetMaxWrittenOffset() const {
     47   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
     48   return maximum_written_offset_;
     49 }
     50 
     51 OpenFileHandleContext::~OpenFileHandleContext() {
     52   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
     53 
     54   // TODO(tzik): Optimize this for single operation.
     55 
     56   int64 file_size = 0;
     57   base::GetFileSize(platform_path_, &file_size);
     58   int64 usage_delta = file_size - initial_file_size_;
     59 
     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
     63   // consumed quota.
     64   int64 reserved_quota_consumption =
     65       std::max(GetEstimatedFileSize(), file_size) - initial_file_size_;
     66 
     67   reservation_buffer_->CommitFileGrowth(
     68       reserved_quota_consumption, usage_delta);
     69   reservation_buffer_->DetachOpenFileHandleContext(this);
     70 }
     71 
     72 }  // namespace fileapi
     73