| 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> | 7 #include <stddef.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <cstring> | 9 #include <cstring> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 | 89 |
| 90 // Consume the copied data. | 90 // Consume the copied data. |
| 91 pending_data->erase(pending_data->begin(), pending_data->begin() + index); | 91 pending_data->erase(pending_data->begin(), pending_data->begin() + index); |
| 92 | 92 |
| 93 return offset; | 93 return offset; |
| 94 } | 94 } |
| 95 | 95 |
| 96 } // namespace | 96 } // namespace |
| 97 | 97 |
| 98 LocalReaderProxy::LocalReaderProxy( | 98 LocalReaderProxy::LocalReaderProxy( |
| 99 scoped_ptr<util::LocalFileReader> file_reader, | 99 std::unique_ptr<util::LocalFileReader> file_reader, |
| 100 int64_t length) | 100 int64_t length) |
| 101 : file_reader_(std::move(file_reader)), | 101 : file_reader_(std::move(file_reader)), |
| 102 remaining_length_(length), | 102 remaining_length_(length), |
| 103 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) { |
| 104 DCHECK(file_reader_); | 104 DCHECK(file_reader_); |
| 105 } | 105 } |
| 106 | 106 |
| 107 LocalReaderProxy::~LocalReaderProxy() { | 107 LocalReaderProxy::~LocalReaderProxy() { |
| 108 } | 108 } |
| 109 | 109 |
| 110 int LocalReaderProxy::Read(net::IOBuffer* buffer, int buffer_length, | 110 int LocalReaderProxy::Read(net::IOBuffer* buffer, int buffer_length, |
| 111 const net::CompletionCallback& callback) { | 111 const net::CompletionCallback& callback) { |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 113 DCHECK(file_reader_); | 113 DCHECK(file_reader_); |
| 114 | 114 |
| 115 if (buffer_length > remaining_length_) { | 115 if (buffer_length > remaining_length_) { |
| 116 // Here, narrowing is safe. | 116 // Here, narrowing is safe. |
| 117 buffer_length = static_cast<int>(remaining_length_); | 117 buffer_length = static_cast<int>(remaining_length_); |
| 118 } | 118 } |
| 119 | 119 |
| 120 if (!buffer_length) | 120 if (!buffer_length) |
| 121 return 0; | 121 return 0; |
| 122 | 122 |
| 123 file_reader_->Read(buffer, buffer_length, | 123 file_reader_->Read(buffer, buffer_length, |
| 124 base::Bind(&LocalReaderProxy::OnReadCompleted, | 124 base::Bind(&LocalReaderProxy::OnReadCompleted, |
| 125 weak_ptr_factory_.GetWeakPtr(), callback)); | 125 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 126 return net::ERR_IO_PENDING; | 126 return net::ERR_IO_PENDING; |
| 127 } | 127 } |
| 128 | 128 |
| 129 void LocalReaderProxy::OnGetContent(scoped_ptr<std::string> data) { | 129 void LocalReaderProxy::OnGetContent(std::unique_ptr<std::string> data) { |
| 130 // This method should never be called, because no data should be received | 130 // This method should never be called, because no data should be received |
| 131 // from the network during the reading of local-cache file. | 131 // from the network during the reading of local-cache file. |
| 132 NOTREACHED(); | 132 NOTREACHED(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void LocalReaderProxy::OnCompleted(FileError error) { | 135 void LocalReaderProxy::OnCompleted(FileError error) { |
| 136 // If this method is called, no network error should be happened. | 136 // If this method is called, no network error should be happened. |
| 137 DCHECK_EQ(FILE_ERROR_OK, error); | 137 DCHECK_EQ(FILE_ERROR_OK, error); |
| 138 } | 138 } |
| 139 | 139 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 | 211 |
| 212 // Although OnCompleted() should reset |job_canceller_| when download is done, | 212 // Although OnCompleted() should reset |job_canceller_| when download is done, |
| 213 // due to timing issues the ReaderProxy instance may be destructed before the | 213 // due to timing issues the ReaderProxy instance may be destructed before the |
| 214 // notification. To fix the case we reset here earlier. | 214 // notification. To fix the case we reset here earlier. |
| 215 if (is_full_download_ && remaining_content_length_ == 0) | 215 if (is_full_download_ && remaining_content_length_ == 0) |
| 216 job_canceller_.Reset(); | 216 job_canceller_.Reset(); |
| 217 | 217 |
| 218 return result; | 218 return result; |
| 219 } | 219 } |
| 220 | 220 |
| 221 void NetworkReaderProxy::OnGetContent(scoped_ptr<std::string> data) { | 221 void NetworkReaderProxy::OnGetContent(std::unique_ptr<std::string> data) { |
| 222 DCHECK(thread_checker_.CalledOnValidThread()); | 222 DCHECK(thread_checker_.CalledOnValidThread()); |
| 223 DCHECK(data && !data->empty()); | 223 DCHECK(data && !data->empty()); |
| 224 | 224 |
| 225 if (remaining_offset_ >= static_cast<int64_t>(data->length())) { | 225 if (remaining_offset_ >= static_cast<int64_t>(data->length())) { |
| 226 // Skip unneeded leading data. | 226 // Skip unneeded leading data. |
| 227 remaining_offset_ -= data->length(); | 227 remaining_offset_ -= data->length(); |
| 228 return; | 228 return; |
| 229 } | 229 } |
| 230 | 230 |
| 231 if (remaining_offset_ > 0) { | 231 if (remaining_offset_ > 0) { |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 const base::Closure& cancel_download_closure) { | 381 const base::Closure& cancel_download_closure) { |
| 382 DCHECK(thread_checker_.CalledOnValidThread()); | 382 DCHECK(thread_checker_.CalledOnValidThread()); |
| 383 cancel_download_closure_ = cancel_download_closure; | 383 cancel_download_closure_ = cancel_download_closure; |
| 384 } | 384 } |
| 385 | 385 |
| 386 void DriveFileStreamReader::InitializeAfterGetFileContentInitialized( | 386 void DriveFileStreamReader::InitializeAfterGetFileContentInitialized( |
| 387 const net::HttpByteRange& byte_range, | 387 const net::HttpByteRange& byte_range, |
| 388 const InitializeCompletionCallback& callback, | 388 const InitializeCompletionCallback& callback, |
| 389 FileError error, | 389 FileError error, |
| 390 const base::FilePath& local_cache_file_path, | 390 const base::FilePath& local_cache_file_path, |
| 391 scoped_ptr<ResourceEntry> entry) { | 391 std::unique_ptr<ResourceEntry> entry) { |
| 392 DCHECK(thread_checker_.CalledOnValidThread()); | 392 DCHECK(thread_checker_.CalledOnValidThread()); |
| 393 // StoreCancelDownloadClosure() should be called before this function. | 393 // StoreCancelDownloadClosure() should be called before this function. |
| 394 DCHECK(!cancel_download_closure_.is_null()); | 394 DCHECK(!cancel_download_closure_.is_null()); |
| 395 | 395 |
| 396 if (error != FILE_ERROR_OK) { | 396 if (error != FILE_ERROR_OK) { |
| 397 callback.Run(FileErrorToNetError(error), scoped_ptr<ResourceEntry>()); | 397 callback.Run(FileErrorToNetError(error), std::unique_ptr<ResourceEntry>()); |
| 398 return; | 398 return; |
| 399 } | 399 } |
| 400 DCHECK(entry); | 400 DCHECK(entry); |
| 401 | 401 |
| 402 int64_t range_start = 0, range_length = 0; | 402 int64_t range_start = 0, range_length = 0; |
| 403 if (!ComputeConcretePosition(byte_range, entry->file_info().size(), | 403 if (!ComputeConcretePosition(byte_range, entry->file_info().size(), |
| 404 &range_start, &range_length)) { | 404 &range_start, &range_length)) { |
| 405 // 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. |
| 406 // 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 |
| 407 // needed and and invalidate weak pointers so that we won't | 407 // needed and and invalidate weak pointers so that we won't |
| 408 // receive unwanted callbacks. | 408 // receive unwanted callbacks. |
| 409 cancel_download_closure_.Run(); | 409 cancel_download_closure_.Run(); |
| 410 weak_ptr_factory_.InvalidateWeakPtrs(); | 410 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 411 callback.Run( | 411 callback.Run(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, |
| 412 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, scoped_ptr<ResourceEntry>()); | 412 std::unique_ptr<ResourceEntry>()); |
| 413 return; | 413 return; |
| 414 } | 414 } |
| 415 | 415 |
| 416 if (local_cache_file_path.empty()) { | 416 if (local_cache_file_path.empty()) { |
| 417 // The file is not cached, and being downloaded. | 417 // The file is not cached, and being downloaded. |
| 418 reader_proxy_.reset( | 418 reader_proxy_.reset( |
| 419 new internal::NetworkReaderProxy( | 419 new internal::NetworkReaderProxy( |
| 420 range_start, range_length, | 420 range_start, range_length, |
| 421 entry->file_info().size(), cancel_download_closure_)); | 421 entry->file_info().size(), cancel_download_closure_)); |
| 422 callback.Run(net::OK, std::move(entry)); | 422 callback.Run(net::OK, std::move(entry)); |
| 423 return; | 423 return; |
| 424 } | 424 } |
| 425 | 425 |
| 426 // Otherwise, open the stream for file. | 426 // Otherwise, open the stream for file. |
| 427 scoped_ptr<util::LocalFileReader> file_reader( | 427 std::unique_ptr<util::LocalFileReader> file_reader( |
| 428 new util::LocalFileReader(file_task_runner_.get())); | 428 new util::LocalFileReader(file_task_runner_.get())); |
| 429 util::LocalFileReader* file_reader_ptr = file_reader.get(); | 429 util::LocalFileReader* file_reader_ptr = file_reader.get(); |
| 430 file_reader_ptr->Open( | 430 file_reader_ptr->Open( |
| 431 local_cache_file_path, | 431 local_cache_file_path, |
| 432 range_start, | 432 range_start, |
| 433 base::Bind( | 433 base::Bind( |
| 434 &DriveFileStreamReader::InitializeAfterLocalFileOpen, | 434 &DriveFileStreamReader::InitializeAfterLocalFileOpen, |
| 435 weak_ptr_factory_.GetWeakPtr(), | 435 weak_ptr_factory_.GetWeakPtr(), |
| 436 range_length, | 436 range_length, |
| 437 callback, | 437 callback, |
| 438 base::Passed(&entry), | 438 base::Passed(&entry), |
| 439 base::Passed(&file_reader))); | 439 base::Passed(&file_reader))); |
| 440 } | 440 } |
| 441 | 441 |
| 442 void DriveFileStreamReader::InitializeAfterLocalFileOpen( | 442 void DriveFileStreamReader::InitializeAfterLocalFileOpen( |
| 443 int64_t length, | 443 int64_t length, |
| 444 const InitializeCompletionCallback& callback, | 444 const InitializeCompletionCallback& callback, |
| 445 scoped_ptr<ResourceEntry> entry, | 445 std::unique_ptr<ResourceEntry> entry, |
| 446 scoped_ptr<util::LocalFileReader> file_reader, | 446 std::unique_ptr<util::LocalFileReader> file_reader, |
| 447 int open_result) { | 447 int open_result) { |
| 448 DCHECK(thread_checker_.CalledOnValidThread()); | 448 DCHECK(thread_checker_.CalledOnValidThread()); |
| 449 | 449 |
| 450 if (open_result != net::OK) { | 450 if (open_result != net::OK) { |
| 451 callback.Run(net::ERR_FAILED, scoped_ptr<ResourceEntry>()); | 451 callback.Run(net::ERR_FAILED, std::unique_ptr<ResourceEntry>()); |
| 452 return; | 452 return; |
| 453 } | 453 } |
| 454 | 454 |
| 455 reader_proxy_.reset( | 455 reader_proxy_.reset( |
| 456 new internal::LocalReaderProxy(std::move(file_reader), length)); | 456 new internal::LocalReaderProxy(std::move(file_reader), length)); |
| 457 callback.Run(net::OK, std::move(entry)); | 457 callback.Run(net::OK, std::move(entry)); |
| 458 } | 458 } |
| 459 | 459 |
| 460 void DriveFileStreamReader::OnGetContent( | 460 void DriveFileStreamReader::OnGetContent( |
| 461 google_apis::DriveApiErrorCode error_code, | 461 google_apis::DriveApiErrorCode error_code, |
| 462 scoped_ptr<std::string> data) { | 462 std::unique_ptr<std::string> data) { |
| 463 DCHECK(thread_checker_.CalledOnValidThread()); | 463 DCHECK(thread_checker_.CalledOnValidThread()); |
| 464 DCHECK(reader_proxy_); | 464 DCHECK(reader_proxy_); |
| 465 reader_proxy_->OnGetContent(std::move(data)); | 465 reader_proxy_->OnGetContent(std::move(data)); |
| 466 } | 466 } |
| 467 | 467 |
| 468 void DriveFileStreamReader::OnGetFileContentCompletion( | 468 void DriveFileStreamReader::OnGetFileContentCompletion( |
| 469 const InitializeCompletionCallback& callback, | 469 const InitializeCompletionCallback& callback, |
| 470 FileError error) { | 470 FileError error) { |
| 471 DCHECK(thread_checker_.CalledOnValidThread()); | 471 DCHECK(thread_checker_.CalledOnValidThread()); |
| 472 | 472 |
| 473 if (reader_proxy_) { | 473 if (reader_proxy_) { |
| 474 // If the proxy object available, send the error to it. | 474 // If the proxy object available, send the error to it. |
| 475 reader_proxy_->OnCompleted(error); | 475 reader_proxy_->OnCompleted(error); |
| 476 } else { | 476 } else { |
| 477 // Here the proxy object is not yet available. | 477 // Here the proxy object is not yet available. |
| 478 // There are two cases. 1) Some error happens during the initialization. | 478 // There are two cases. 1) Some error happens during the initialization. |
| 479 // 2) the cache file is found, but the proxy object is not *yet* | 479 // 2) the cache file is found, but the proxy object is not *yet* |
| 480 // initialized because the file is being opened. | 480 // initialized because the file is being opened. |
| 481 // We are interested in 1) only. The callback for 2) will be called | 481 // We are interested in 1) only. The callback for 2) will be called |
| 482 // after opening the file is completed. | 482 // after opening the file is completed. |
| 483 // Note: due to the same reason, LocalReaderProxy::OnCompleted may | 483 // Note: due to the same reason, LocalReaderProxy::OnCompleted may |
| 484 // 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 |
| 485 // unfortunately. | 485 // unfortunately. |
| 486 if (error != FILE_ERROR_OK) { | 486 if (error != FILE_ERROR_OK) { |
| 487 callback.Run(FileErrorToNetError(error), scoped_ptr<ResourceEntry>()); | 487 callback.Run(FileErrorToNetError(error), |
| 488 std::unique_ptr<ResourceEntry>()); |
| 488 } | 489 } |
| 489 } | 490 } |
| 490 } | 491 } |
| 491 | 492 |
| 492 } // namespace drive | 493 } // namespace drive |
| OLD | NEW |