| 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 "chrome/browser/chromeos/drive/drive_file_stream_reader.h" | 5 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 8 #include <cstring> | 10 #include <cstring> |
| 9 | 11 |
| 10 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 12 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 13 #include "components/drive/drive.pb.h" | 15 #include "components/drive/drive.pb.h" |
| 14 #include "components/drive/file_system_interface.h" | 16 #include "components/drive/file_system_interface.h" |
| 15 #include "components/drive/local_file_reader.h" | 17 #include "components/drive/local_file_reader.h" |
| 16 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 30 } | 32 } |
| 31 | 33 |
| 32 // Computes the concrete |start| offset and the |length| of |range| in a file | 34 // Computes the concrete |start| offset and the |length| of |range| in a file |
| 33 // of |total| size. | 35 // of |total| size. |
| 34 // | 36 // |
| 35 // This is a thin wrapper of HttpByteRange::ComputeBounds, extended to allow | 37 // This is a thin wrapper of HttpByteRange::ComputeBounds, extended to allow |
| 36 // an empty range at the end of the file, like "Range: bytes 0-" on a zero byte | 38 // an empty range at the end of the file, like "Range: bytes 0-" on a zero byte |
| 37 // file. This is for convenience in unifying implementation with the seek | 39 // file. This is for convenience in unifying implementation with the seek |
| 38 // operation of stream reader. HTTP doesn't allow such ranges but we want to | 40 // operation of stream reader. HTTP doesn't allow such ranges but we want to |
| 39 // treat such seeking as valid. | 41 // treat such seeking as valid. |
| 40 bool ComputeConcretePosition(net::HttpByteRange range, int64 total, | 42 bool ComputeConcretePosition(net::HttpByteRange range, |
| 41 int64* start, int64* length) { | 43 int64_t total, |
| 44 int64_t* start, |
| 45 int64_t* length) { |
| 42 // The special case when empty range in the end of the file is selected. | 46 // The special case when empty range in the end of the file is selected. |
| 43 if (range.HasFirstBytePosition() && range.first_byte_position() == total) { | 47 if (range.HasFirstBytePosition() && range.first_byte_position() == total) { |
| 44 *start = range.first_byte_position(); | 48 *start = range.first_byte_position(); |
| 45 *length = 0; | 49 *length = 0; |
| 46 return true; | 50 return true; |
| 47 } | 51 } |
| 48 | 52 |
| 49 // Otherwise forward to HttpByteRange::ComputeBounds. | 53 // Otherwise forward to HttpByteRange::ComputeBounds. |
| 50 if (!range.ComputeBounds(total)) | 54 if (!range.ComputeBounds(total)) |
| 51 return false; | 55 return false; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 | 89 |
| 86 // Consume the copied data. | 90 // Consume the copied data. |
| 87 pending_data->erase(pending_data->begin(), pending_data->begin() + index); | 91 pending_data->erase(pending_data->begin(), pending_data->begin() + index); |
| 88 | 92 |
| 89 return offset; | 93 return offset; |
| 90 } | 94 } |
| 91 | 95 |
| 92 } // namespace | 96 } // namespace |
| 93 | 97 |
| 94 LocalReaderProxy::LocalReaderProxy( | 98 LocalReaderProxy::LocalReaderProxy( |
| 95 scoped_ptr<util::LocalFileReader> file_reader, int64 length) | 99 scoped_ptr<util::LocalFileReader> file_reader, |
| 100 int64_t length) |
| 96 : file_reader_(file_reader.Pass()), | 101 : file_reader_(file_reader.Pass()), |
| 97 remaining_length_(length), | 102 remaining_length_(length), |
| 98 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) { |
| 99 DCHECK(file_reader_); | 104 DCHECK(file_reader_); |
| 100 } | 105 } |
| 101 | 106 |
| 102 LocalReaderProxy::~LocalReaderProxy() { | 107 LocalReaderProxy::~LocalReaderProxy() { |
| 103 } | 108 } |
| 104 | 109 |
| 105 int LocalReaderProxy::Read(net::IOBuffer* buffer, int buffer_length, | 110 int LocalReaderProxy::Read(net::IOBuffer* buffer, int buffer_length, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // |read_result| bytes data is read. | 146 // |read_result| bytes data is read. |
| 142 DCHECK_LE(read_result, remaining_length_); | 147 DCHECK_LE(read_result, remaining_length_); |
| 143 remaining_length_ -= read_result; | 148 remaining_length_ -= read_result; |
| 144 } else { | 149 } else { |
| 145 // An error occurs. Close the |file_reader_|. | 150 // An error occurs. Close the |file_reader_|. |
| 146 file_reader_.reset(); | 151 file_reader_.reset(); |
| 147 } | 152 } |
| 148 callback.Run(read_result); | 153 callback.Run(read_result); |
| 149 } | 154 } |
| 150 | 155 |
| 151 NetworkReaderProxy::NetworkReaderProxy( | 156 NetworkReaderProxy::NetworkReaderProxy(int64_t offset, |
| 152 int64 offset, | 157 int64_t content_length, |
| 153 int64 content_length, | 158 int64_t full_content_length, |
| 154 int64 full_content_length, | 159 const base::Closure& job_canceller) |
| 155 const base::Closure& job_canceller) | |
| 156 : remaining_offset_(offset), | 160 : remaining_offset_(offset), |
| 157 remaining_content_length_(content_length), | 161 remaining_content_length_(content_length), |
| 158 is_full_download_(offset + content_length == full_content_length), | 162 is_full_download_(offset + content_length == full_content_length), |
| 159 error_code_(net::OK), | 163 error_code_(net::OK), |
| 160 buffer_length_(0), | 164 buffer_length_(0), |
| 161 job_canceller_(job_canceller) { | 165 job_canceller_(job_canceller) {} |
| 162 } | |
| 163 | 166 |
| 164 NetworkReaderProxy::~NetworkReaderProxy() { | 167 NetworkReaderProxy::~NetworkReaderProxy() { |
| 165 if (!job_canceller_.is_null()) { | 168 if (!job_canceller_.is_null()) { |
| 166 job_canceller_.Run(); | 169 job_canceller_.Run(); |
| 167 } | 170 } |
| 168 } | 171 } |
| 169 | 172 |
| 170 int NetworkReaderProxy::Read(net::IOBuffer* buffer, int buffer_length, | 173 int NetworkReaderProxy::Read(net::IOBuffer* buffer, int buffer_length, |
| 171 const net::CompletionCallback& callback) { | 174 const net::CompletionCallback& callback) { |
| 172 DCHECK(thread_checker_.CalledOnValidThread()); | 175 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 if (is_full_download_ && remaining_content_length_ == 0) | 215 if (is_full_download_ && remaining_content_length_ == 0) |
| 213 job_canceller_.Reset(); | 216 job_canceller_.Reset(); |
| 214 | 217 |
| 215 return result; | 218 return result; |
| 216 } | 219 } |
| 217 | 220 |
| 218 void NetworkReaderProxy::OnGetContent(scoped_ptr<std::string> data) { | 221 void NetworkReaderProxy::OnGetContent(scoped_ptr<std::string> data) { |
| 219 DCHECK(thread_checker_.CalledOnValidThread()); | 222 DCHECK(thread_checker_.CalledOnValidThread()); |
| 220 DCHECK(data && !data->empty()); | 223 DCHECK(data && !data->empty()); |
| 221 | 224 |
| 222 if (remaining_offset_ >= static_cast<int64>(data->length())) { | 225 if (remaining_offset_ >= static_cast<int64_t>(data->length())) { |
| 223 // Skip unneeded leading data. | 226 // Skip unneeded leading data. |
| 224 remaining_offset_ -= data->length(); | 227 remaining_offset_ -= data->length(); |
| 225 return; | 228 return; |
| 226 } | 229 } |
| 227 | 230 |
| 228 if (remaining_offset_ > 0) { | 231 if (remaining_offset_ > 0) { |
| 229 // Erase unnecessary leading bytes. | 232 // Erase unnecessary leading bytes. |
| 230 data->erase(0, static_cast<size_t>(remaining_offset_)); | 233 data->erase(0, static_cast<size_t>(remaining_offset_)); |
| 231 remaining_offset_ = 0; | 234 remaining_offset_ = 0; |
| 232 } | 235 } |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 DCHECK(thread_checker_.CalledOnValidThread()); | 392 DCHECK(thread_checker_.CalledOnValidThread()); |
| 390 // StoreCancelDownloadClosure() should be called before this function. | 393 // StoreCancelDownloadClosure() should be called before this function. |
| 391 DCHECK(!cancel_download_closure_.is_null()); | 394 DCHECK(!cancel_download_closure_.is_null()); |
| 392 | 395 |
| 393 if (error != FILE_ERROR_OK) { | 396 if (error != FILE_ERROR_OK) { |
| 394 callback.Run(FileErrorToNetError(error), scoped_ptr<ResourceEntry>()); | 397 callback.Run(FileErrorToNetError(error), scoped_ptr<ResourceEntry>()); |
| 395 return; | 398 return; |
| 396 } | 399 } |
| 397 DCHECK(entry); | 400 DCHECK(entry); |
| 398 | 401 |
| 399 int64 range_start = 0, range_length = 0; | 402 int64_t range_start = 0, range_length = 0; |
| 400 if (!ComputeConcretePosition(byte_range, entry->file_info().size(), | 403 if (!ComputeConcretePosition(byte_range, entry->file_info().size(), |
| 401 &range_start, &range_length)) { | 404 &range_start, &range_length)) { |
| 402 // If |byte_range| is invalid (e.g. out of bounds), return with an error. | 405 // If |byte_range| is invalid (e.g. out of bounds), return with an error. |
| 403 // At the same time, we cancel the in-flight downloading operation if | 406 // At the same time, we cancel the in-flight downloading operation if |
| 404 // needed and and invalidate weak pointers so that we won't | 407 // needed and and invalidate weak pointers so that we won't |
| 405 // receive unwanted callbacks. | 408 // receive unwanted callbacks. |
| 406 cancel_download_closure_.Run(); | 409 cancel_download_closure_.Run(); |
| 407 weak_ptr_factory_.InvalidateWeakPtrs(); | 410 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 408 callback.Run( | 411 callback.Run( |
| 409 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, scoped_ptr<ResourceEntry>()); | 412 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, scoped_ptr<ResourceEntry>()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 430 base::Bind( | 433 base::Bind( |
| 431 &DriveFileStreamReader::InitializeAfterLocalFileOpen, | 434 &DriveFileStreamReader::InitializeAfterLocalFileOpen, |
| 432 weak_ptr_factory_.GetWeakPtr(), | 435 weak_ptr_factory_.GetWeakPtr(), |
| 433 range_length, | 436 range_length, |
| 434 callback, | 437 callback, |
| 435 base::Passed(&entry), | 438 base::Passed(&entry), |
| 436 base::Passed(&file_reader))); | 439 base::Passed(&file_reader))); |
| 437 } | 440 } |
| 438 | 441 |
| 439 void DriveFileStreamReader::InitializeAfterLocalFileOpen( | 442 void DriveFileStreamReader::InitializeAfterLocalFileOpen( |
| 440 int64 length, | 443 int64_t length, |
| 441 const InitializeCompletionCallback& callback, | 444 const InitializeCompletionCallback& callback, |
| 442 scoped_ptr<ResourceEntry> entry, | 445 scoped_ptr<ResourceEntry> entry, |
| 443 scoped_ptr<util::LocalFileReader> file_reader, | 446 scoped_ptr<util::LocalFileReader> file_reader, |
| 444 int open_result) { | 447 int open_result) { |
| 445 DCHECK(thread_checker_.CalledOnValidThread()); | 448 DCHECK(thread_checker_.CalledOnValidThread()); |
| 446 | 449 |
| 447 if (open_result != net::OK) { | 450 if (open_result != net::OK) { |
| 448 callback.Run(net::ERR_FAILED, scoped_ptr<ResourceEntry>()); | 451 callback.Run(net::ERR_FAILED, scoped_ptr<ResourceEntry>()); |
| 449 return; | 452 return; |
| 450 } | 453 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 480 // Note: due to the same reason, LocalReaderProxy::OnCompleted may | 483 // Note: due to the same reason, LocalReaderProxy::OnCompleted may |
| 481 // or may not be called. This is timing issue, and it is difficult to avoid | 484 // or may not be called. This is timing issue, and it is difficult to avoid |
| 482 // unfortunately. | 485 // unfortunately. |
| 483 if (error != FILE_ERROR_OK) { | 486 if (error != FILE_ERROR_OK) { |
| 484 callback.Run(FileErrorToNetError(error), scoped_ptr<ResourceEntry>()); | 487 callback.Run(FileErrorToNetError(error), scoped_ptr<ResourceEntry>()); |
| 485 } | 488 } |
| 486 } | 489 } |
| 487 } | 490 } |
| 488 | 491 |
| 489 } // namespace drive | 492 } // namespace drive |
| OLD | NEW |