OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "storage/browser/fileapi/quota/open_file_handle_context.h" | 5 #include "storage/browser/fileapi/quota/open_file_handle_context.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "storage/browser/fileapi/quota/quota_reservation_buffer.h" | 10 #include "storage/browser/fileapi/quota/quota_reservation_buffer.h" |
11 | 11 |
12 namespace storage { | 12 namespace storage { |
13 | 13 |
14 OpenFileHandleContext::OpenFileHandleContext( | 14 OpenFileHandleContext::OpenFileHandleContext( |
15 const base::FilePath& platform_path, | 15 const base::FilePath& platform_path, |
16 QuotaReservationBuffer* reservation_buffer) | 16 QuotaReservationBuffer* reservation_buffer) |
17 : initial_file_size_(0), | 17 : initial_file_size_(0), |
18 maximum_written_offset_(0), | 18 maximum_written_offset_(0), |
19 append_mode_write_amount_(0), | 19 append_mode_write_amount_(0), |
20 platform_path_(platform_path), | 20 platform_path_(platform_path), |
21 reservation_buffer_(reservation_buffer) { | 21 reservation_buffer_(reservation_buffer) { |
22 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 22 DCHECK(sequence_checker_.CalledOnValidSequence()); |
23 | 23 |
24 base::GetFileSize(platform_path, &initial_file_size_); | 24 base::GetFileSize(platform_path, &initial_file_size_); |
25 maximum_written_offset_ = initial_file_size_; | 25 maximum_written_offset_ = initial_file_size_; |
26 } | 26 } |
27 | 27 |
28 int64_t OpenFileHandleContext::UpdateMaxWrittenOffset(int64_t offset) { | 28 int64_t OpenFileHandleContext::UpdateMaxWrittenOffset(int64_t offset) { |
29 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 29 DCHECK(sequence_checker_.CalledOnValidSequence()); |
30 if (offset <= maximum_written_offset_) | 30 if (offset <= maximum_written_offset_) |
31 return 0; | 31 return 0; |
32 | 32 |
33 int64_t growth = offset - maximum_written_offset_; | 33 int64_t growth = offset - maximum_written_offset_; |
34 maximum_written_offset_ = offset; | 34 maximum_written_offset_ = offset; |
35 return growth; | 35 return growth; |
36 } | 36 } |
37 | 37 |
38 void OpenFileHandleContext::AddAppendModeWriteAmount(int64_t amount) { | 38 void OpenFileHandleContext::AddAppendModeWriteAmount(int64_t amount) { |
39 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 39 DCHECK(sequence_checker_.CalledOnValidSequence()); |
40 append_mode_write_amount_ += amount; | 40 append_mode_write_amount_ += amount; |
41 } | 41 } |
42 | 42 |
43 int64_t OpenFileHandleContext::GetEstimatedFileSize() const { | 43 int64_t OpenFileHandleContext::GetEstimatedFileSize() const { |
44 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 44 DCHECK(sequence_checker_.CalledOnValidSequence()); |
45 return maximum_written_offset_ + append_mode_write_amount_; | 45 return maximum_written_offset_ + append_mode_write_amount_; |
46 } | 46 } |
47 | 47 |
48 int64_t OpenFileHandleContext::GetMaxWrittenOffset() const { | 48 int64_t OpenFileHandleContext::GetMaxWrittenOffset() const { |
49 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 49 DCHECK(sequence_checker_.CalledOnValidSequence()); |
50 return maximum_written_offset_; | 50 return maximum_written_offset_; |
51 } | 51 } |
52 | 52 |
53 OpenFileHandleContext::~OpenFileHandleContext() { | 53 OpenFileHandleContext::~OpenFileHandleContext() { |
54 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 54 DCHECK(sequence_checker_.CalledOnValidSequence()); |
55 | 55 |
56 // TODO(tzik): Optimize this for single operation. | 56 // TODO(tzik): Optimize this for single operation. |
57 | 57 |
58 int64_t file_size = 0; | 58 int64_t file_size = 0; |
59 base::GetFileSize(platform_path_, &file_size); | 59 base::GetFileSize(platform_path_, &file_size); |
60 int64_t usage_delta = file_size - initial_file_size_; | 60 int64_t usage_delta = file_size - initial_file_size_; |
61 | 61 |
62 // |reserved_quota_consumption| may be greater than the recorded file growth | 62 // |reserved_quota_consumption| may be greater than the recorded file growth |
63 // when a plugin crashed before reporting its consumption. | 63 // when a plugin crashed before reporting its consumption. |
64 // In this case, the reserved quota for the plugin should be handled as | 64 // In this case, the reserved quota for the plugin should be handled as |
65 // consumed quota. | 65 // consumed quota. |
66 int64_t reserved_quota_consumption = | 66 int64_t reserved_quota_consumption = |
67 std::max(GetEstimatedFileSize(), file_size) - initial_file_size_; | 67 std::max(GetEstimatedFileSize(), file_size) - initial_file_size_; |
68 | 68 |
69 reservation_buffer_->CommitFileGrowth( | 69 reservation_buffer_->CommitFileGrowth( |
70 reserved_quota_consumption, usage_delta); | 70 reserved_quota_consumption, usage_delta); |
71 reservation_buffer_->DetachOpenFileHandleContext(this); | 71 reservation_buffer_->DetachOpenFileHandleContext(this); |
72 } | 72 } |
73 | 73 |
74 } // namespace storage | 74 } // namespace storage |
OLD | NEW |