OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/sandbox_file_stream_writer.h" | 5 #include "storage/browser/fileapi/sandbox_file_stream_writer.h" |
6 | 6 |
| 7 #include <limits> |
| 8 |
7 #include "base/files/file_util_proxy.h" | 9 #include "base/files/file_util_proxy.h" |
8 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
9 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
10 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
11 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
12 #include "storage/browser/fileapi/file_observers.h" | 14 #include "storage/browser/fileapi/file_observers.h" |
13 #include "storage/browser/fileapi/file_stream_reader.h" | 15 #include "storage/browser/fileapi/file_stream_reader.h" |
14 #include "storage/browser/fileapi/file_system_context.h" | 16 #include "storage/browser/fileapi/file_system_context.h" |
15 #include "storage/browser/fileapi/file_system_operation_runner.h" | 17 #include "storage/browser/fileapi/file_system_operation_runner.h" |
16 #include "storage/browser/quota/quota_manager_proxy.h" | 18 #include "storage/browser/quota/quota_manager_proxy.h" |
17 #include "storage/common/fileapi/file_system_util.h" | 19 #include "storage/common/fileapi/file_system_util.h" |
18 | 20 |
19 namespace storage { | 21 namespace storage { |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 // Adjust the |quota| value in overwriting case (i.e. |file_size| > 0 and | 25 // Adjust the |quota| value in overwriting case (i.e. |file_size| > 0 and |
24 // |file_offset| < |file_size|) to make the remaining quota calculation easier. | 26 // |file_offset| < |file_size|) to make the remaining quota calculation easier. |
25 // Specifically this widens the quota for overlapping range (so that we can | 27 // Specifically this widens the quota for overlapping range (so that we can |
26 // simply compare written bytes against the adjusted quota). | 28 // simply compare written bytes against the adjusted quota). |
27 int64 AdjustQuotaForOverlap(int64 quota, | 29 int64_t AdjustQuotaForOverlap(int64_t quota, |
28 int64 file_offset, | 30 int64_t file_offset, |
29 int64 file_size) { | 31 int64_t file_size) { |
30 DCHECK_LE(file_offset, file_size); | 32 DCHECK_LE(file_offset, file_size); |
31 if (quota < 0) | 33 if (quota < 0) |
32 quota = 0; | 34 quota = 0; |
33 int64 overlap = file_size - file_offset; | 35 int64_t overlap = file_size - file_offset; |
34 if (kint64max - overlap > quota) | 36 if (std::numeric_limits<int64_t>::max() - overlap > quota) |
35 quota += overlap; | 37 quota += overlap; |
36 return quota; | 38 return quota; |
37 } | 39 } |
38 | 40 |
39 } // namespace | 41 } // namespace |
40 | 42 |
41 SandboxFileStreamWriter::SandboxFileStreamWriter( | 43 SandboxFileStreamWriter::SandboxFileStreamWriter( |
42 FileSystemContext* file_system_context, | 44 FileSystemContext* file_system_context, |
43 const FileSystemURL& url, | 45 const FileSystemURL& url, |
44 int64 initial_offset, | 46 int64_t initial_offset, |
45 const UpdateObserverList& observers) | 47 const UpdateObserverList& observers) |
46 : file_system_context_(file_system_context), | 48 : file_system_context_(file_system_context), |
47 url_(url), | 49 url_(url), |
48 initial_offset_(initial_offset), | 50 initial_offset_(initial_offset), |
49 observers_(observers), | 51 observers_(observers), |
50 file_size_(0), | 52 file_size_(0), |
51 total_bytes_written_(0), | 53 total_bytes_written_(0), |
52 allowed_bytes_to_write_(0), | 54 allowed_bytes_to_write_(0), |
53 has_pending_operation_(false), | 55 has_pending_operation_(false), |
54 default_quota_(kint64max), | 56 default_quota_(std::numeric_limits<int64_t>::max()), |
55 weak_factory_(this) { | 57 weak_factory_(this) { |
56 DCHECK(url_.is_valid()); | 58 DCHECK(url_.is_valid()); |
57 } | 59 } |
58 | 60 |
59 SandboxFileStreamWriter::~SandboxFileStreamWriter() {} | 61 SandboxFileStreamWriter::~SandboxFileStreamWriter() {} |
60 | 62 |
61 int SandboxFileStreamWriter::Write( | 63 int SandboxFileStreamWriter::Write( |
62 net::IOBuffer* buf, int buf_len, | 64 net::IOBuffer* buf, int buf_len, |
63 const net::CompletionCallback& callback) { | 65 const net::CompletionCallback& callback) { |
64 has_pending_operation_ = true; | 66 has_pending_operation_ = true; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 quota_manager_proxy->quota_manager()->GetUsageAndQuota( | 161 quota_manager_proxy->quota_manager()->GetUsageAndQuota( |
160 url_.origin(), | 162 url_.origin(), |
161 FileSystemTypeToQuotaStorageType(url_.type()), | 163 FileSystemTypeToQuotaStorageType(url_.type()), |
162 base::Bind(&SandboxFileStreamWriter::DidGetUsageAndQuota, | 164 base::Bind(&SandboxFileStreamWriter::DidGetUsageAndQuota, |
163 weak_factory_.GetWeakPtr(), callback)); | 165 weak_factory_.GetWeakPtr(), callback)); |
164 } | 166 } |
165 | 167 |
166 void SandboxFileStreamWriter::DidGetUsageAndQuota( | 168 void SandboxFileStreamWriter::DidGetUsageAndQuota( |
167 const net::CompletionCallback& callback, | 169 const net::CompletionCallback& callback, |
168 storage::QuotaStatusCode status, | 170 storage::QuotaStatusCode status, |
169 int64 usage, | 171 int64_t usage, |
170 int64 quota) { | 172 int64_t quota) { |
171 if (CancelIfRequested()) | 173 if (CancelIfRequested()) |
172 return; | 174 return; |
173 if (status != storage::kQuotaStatusOk) { | 175 if (status != storage::kQuotaStatusOk) { |
174 LOG(WARNING) << "Got unexpected quota error : " << status; | 176 LOG(WARNING) << "Got unexpected quota error : " << status; |
175 | 177 |
176 // crbug.com/349708 | 178 // crbug.com/349708 |
177 TRACE_EVENT0("io", "SandboxFileStreamWriter::DidGetUsageAndQuota FAILED"); | 179 TRACE_EVENT0("io", "SandboxFileStreamWriter::DidGetUsageAndQuota FAILED"); |
178 | 180 |
179 callback.Run(net::ERR_FAILED); | 181 callback.Run(net::ERR_FAILED); |
180 return; | 182 return; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 DCHECK(cancel_callback_.is_null()); | 250 DCHECK(cancel_callback_.is_null()); |
249 | 251 |
250 // Write() is not called yet, so there's nothing to flush. | 252 // Write() is not called yet, so there's nothing to flush. |
251 if (!local_file_writer_) | 253 if (!local_file_writer_) |
252 return net::OK; | 254 return net::OK; |
253 | 255 |
254 return local_file_writer_->Flush(callback); | 256 return local_file_writer_->Flush(callback); |
255 } | 257 } |
256 | 258 |
257 } // namespace storage | 259 } // namespace storage |
OLD | NEW |