| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/fileapi/upload_file_system_file_element_reader.h" | 5 #include "content/browser/fileapi/upload_file_system_file_element_reader.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/numerics/safe_conversions.h" | 11 #include "base/numerics/safe_conversions.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "storage/browser/fileapi/file_stream_reader.h" | 13 #include "storage/browser/fileapi/file_stream_reader.h" |
| 14 #include "storage/browser/fileapi/file_system_context.h" | 14 #include "storage/browser/fileapi/file_system_context.h" |
| 15 #include "storage/browser/fileapi/file_system_url.h" | 15 #include "storage/browser/fileapi/file_system_url.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 UploadFileSystemFileElementReader::UploadFileSystemFileElementReader( | 19 UploadFileSystemFileElementReader::UploadFileSystemFileElementReader( |
| 20 storage::FileSystemContext* file_system_context, | 20 storage::FileSystemContext* file_system_context, |
| 21 const GURL& url, | 21 const GURL& url, |
| 22 uint64 range_offset, | 22 uint64_t range_offset, |
| 23 uint64 range_length, | 23 uint64_t range_length, |
| 24 const base::Time& expected_modification_time) | 24 const base::Time& expected_modification_time) |
| 25 : file_system_context_(file_system_context), | 25 : file_system_context_(file_system_context), |
| 26 url_(url), | 26 url_(url), |
| 27 range_offset_(range_offset), | 27 range_offset_(range_offset), |
| 28 range_length_(range_length), | 28 range_length_(range_length), |
| 29 expected_modification_time_(expected_modification_time), | 29 expected_modification_time_(expected_modification_time), |
| 30 stream_length_(0), | 30 stream_length_(0), |
| 31 position_(0), | 31 position_(0), |
| 32 weak_ptr_factory_(this) { | 32 weak_ptr_factory_(this) {} |
| 33 } | |
| 34 | 33 |
| 35 UploadFileSystemFileElementReader::~UploadFileSystemFileElementReader() { | 34 UploadFileSystemFileElementReader::~UploadFileSystemFileElementReader() { |
| 36 } | 35 } |
| 37 | 36 |
| 38 int UploadFileSystemFileElementReader::Init( | 37 int UploadFileSystemFileElementReader::Init( |
| 39 const net::CompletionCallback& callback) { | 38 const net::CompletionCallback& callback) { |
| 40 // Reset states. | 39 // Reset states. |
| 41 weak_ptr_factory_.InvalidateWeakPtrs(); | 40 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 42 stream_length_ = 0; | 41 stream_length_ = 0; |
| 43 position_ = 0; | 42 position_ = 0; |
| 44 | 43 |
| 45 // Initialize the stream reader and the length. | 44 // Initialize the stream reader and the length. |
| 46 stream_reader_ = file_system_context_->CreateFileStreamReader( | 45 stream_reader_ = file_system_context_->CreateFileStreamReader( |
| 47 file_system_context_->CrackURL(url_), | 46 file_system_context_->CrackURL(url_), range_offset_, |
| 48 range_offset_, | 47 range_length_ == std::numeric_limits<uint64_t>::max() |
| 49 range_length_ == std::numeric_limits<uint64>::max() | |
| 50 ? storage::kMaximumLength | 48 ? storage::kMaximumLength |
| 51 : base::checked_cast<int64>(range_length_), | 49 : base::checked_cast<int64_t>(range_length_), |
| 52 expected_modification_time_); | 50 expected_modification_time_); |
| 53 DCHECK(stream_reader_); | 51 DCHECK(stream_reader_); |
| 54 | 52 |
| 55 const int64 result = stream_reader_->GetLength( | 53 const int64_t result = stream_reader_->GetLength( |
| 56 base::Bind(&UploadFileSystemFileElementReader::OnGetLength, | 54 base::Bind(&UploadFileSystemFileElementReader::OnGetLength, |
| 57 weak_ptr_factory_.GetWeakPtr(), | 55 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 58 callback)); | |
| 59 if (result >= 0) { | 56 if (result >= 0) { |
| 60 stream_length_ = result; | 57 stream_length_ = result; |
| 61 return net::OK; | 58 return net::OK; |
| 62 } | 59 } |
| 63 | 60 |
| 64 // The error code can be casted to int. | 61 // The error code can be casted to int. |
| 65 return static_cast<int>(result); | 62 return static_cast<int>(result); |
| 66 } | 63 } |
| 67 | 64 |
| 68 uint64 UploadFileSystemFileElementReader::GetContentLength() const { | 65 uint64_t UploadFileSystemFileElementReader::GetContentLength() const { |
| 69 return std::min(stream_length_, range_length_); | 66 return std::min(stream_length_, range_length_); |
| 70 } | 67 } |
| 71 | 68 |
| 72 uint64 UploadFileSystemFileElementReader::BytesRemaining() const { | 69 uint64_t UploadFileSystemFileElementReader::BytesRemaining() const { |
| 73 return GetContentLength() - position_; | 70 return GetContentLength() - position_; |
| 74 } | 71 } |
| 75 | 72 |
| 76 int UploadFileSystemFileElementReader::Read( | 73 int UploadFileSystemFileElementReader::Read( |
| 77 net::IOBuffer* buf, | 74 net::IOBuffer* buf, |
| 78 int buf_length, | 75 int buf_length, |
| 79 const net::CompletionCallback& callback) { | 76 const net::CompletionCallback& callback) { |
| 80 DCHECK_LT(0, buf_length); | 77 DCHECK_LT(0, buf_length); |
| 81 DCHECK(stream_reader_); | 78 DCHECK(stream_reader_); |
| 82 | 79 |
| 83 const uint64 num_bytes_to_read = | 80 const uint64_t num_bytes_to_read = |
| 84 std::min(BytesRemaining(), static_cast<uint64>(buf_length)); | 81 std::min(BytesRemaining(), static_cast<uint64_t>(buf_length)); |
| 85 | 82 |
| 86 if (num_bytes_to_read == 0) | 83 if (num_bytes_to_read == 0) |
| 87 return 0; | 84 return 0; |
| 88 | 85 |
| 89 const int result = stream_reader_->Read( | 86 const int result = stream_reader_->Read( |
| 90 buf, num_bytes_to_read, | 87 buf, num_bytes_to_read, |
| 91 base::Bind(&UploadFileSystemFileElementReader::OnRead, | 88 base::Bind(&UploadFileSystemFileElementReader::OnRead, |
| 92 weak_ptr_factory_.GetWeakPtr(), | 89 weak_ptr_factory_.GetWeakPtr(), |
| 93 callback)); | 90 callback)); |
| 94 if (result >= 0) | 91 if (result >= 0) |
| 95 OnRead(net::CompletionCallback(), result); | 92 OnRead(net::CompletionCallback(), result); |
| 96 return result; | 93 return result; |
| 97 } | 94 } |
| 98 | 95 |
| 99 void UploadFileSystemFileElementReader::OnGetLength( | 96 void UploadFileSystemFileElementReader::OnGetLength( |
| 100 const net::CompletionCallback& callback, | 97 const net::CompletionCallback& callback, |
| 101 int64 result) { | 98 int64_t result) { |
| 102 if (result >= 0) { | 99 if (result >= 0) { |
| 103 stream_length_ = result; | 100 stream_length_ = result; |
| 104 callback.Run(net::OK); | 101 callback.Run(net::OK); |
| 105 return; | 102 return; |
| 106 } | 103 } |
| 107 callback.Run(result); | 104 callback.Run(result); |
| 108 } | 105 } |
| 109 | 106 |
| 110 void UploadFileSystemFileElementReader::OnRead( | 107 void UploadFileSystemFileElementReader::OnRead( |
| 111 const net::CompletionCallback& callback, | 108 const net::CompletionCallback& callback, |
| 112 int result) { | 109 int result) { |
| 113 if (result > 0) { | 110 if (result > 0) { |
| 114 position_ += result; | 111 position_ += result; |
| 115 DCHECK_LE(position_, GetContentLength()); | 112 DCHECK_LE(position_, GetContentLength()); |
| 116 } | 113 } |
| 117 if (!callback.is_null()) | 114 if (!callback.is_null()) |
| 118 callback.Run(result); | 115 callback.Run(result); |
| 119 } | 116 } |
| 120 | 117 |
| 121 } // namespace content | 118 } // namespace content |
| OLD | NEW |