Chromium Code Reviews| 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 "chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.h" | 5 #include "chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/numerics/safe_conversions.h" | |
| 9 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 10 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h" | 13 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h" |
| 11 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper_map_servic e.h" | 14 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper_map_servic e.h" |
| 12 #include "chrome/browser/media_galleries/linux/snapshot_file_details.h" | 15 #include "chrome/browser/media_galleries/linux/snapshot_file_details.h" |
| 13 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 14 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 15 | 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 // File path separator constant. | 21 // File path separator constant. |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 | 147 |
| 145 // Copies the contents of |device_file_path| to |snapshot_file_path|. | 148 // Copies the contents of |device_file_path| to |snapshot_file_path|. |
| 146 // | 149 // |
| 147 // Called on the UI thread to dispatch the request to the | 150 // Called on the UI thread to dispatch the request to the |
| 148 // MediaTransferProtocolManager. | 151 // MediaTransferProtocolManager. |
| 149 // | 152 // |
| 150 // |storage_name| specifies the name of the storage device. | 153 // |storage_name| specifies the name of the storage device. |
| 151 // |request| is a struct containing details about the byte read request. | 154 // |request| is a struct containing details about the byte read request. |
| 152 void ReadBytesOnUIThread( | 155 void ReadBytesOnUIThread( |
| 153 const std::string& storage_name, | 156 const std::string& storage_name, |
| 154 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { | 157 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, |
| 158 const base::File::Info& file_info) { | |
| 155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 159 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 156 MTPDeviceTaskHelper* task_helper = | 160 MTPDeviceTaskHelper* task_helper = |
| 157 GetDeviceTaskHelperForStorage(storage_name); | 161 GetDeviceTaskHelperForStorage(storage_name); |
| 158 if (!task_helper) | 162 if (!task_helper) |
| 159 return; | 163 return; |
| 160 task_helper->ReadBytes(request); | 164 task_helper->ReadBytes(request.device_file_relative_path, request.buf, |
| 165 request.offset, request.buf_len, | |
| 166 base::Bind(request.success_callback, file_info), | |
| 167 request.error_callback); | |
| 161 } | 168 } |
| 162 | 169 |
| 163 // Closes the device storage specified by the |storage_name| and destroys the | 170 // Closes the device storage specified by the |storage_name| and destroys the |
| 164 // MTPDeviceTaskHelper object associated with the device storage. | 171 // MTPDeviceTaskHelper object associated with the device storage. |
| 165 // | 172 // |
| 166 // Called on the UI thread to dispatch the request to the | 173 // Called on the UI thread to dispatch the request to the |
| 167 // MediaTransferProtocolManager. | 174 // MediaTransferProtocolManager. |
| 168 void CloseStorageAndDestroyTaskHelperOnUIThread( | 175 void CloseStorageAndDestroyTaskHelperOnUIThread( |
| 169 const std::string& storage_name) { | 176 const std::string& storage_name) { |
| 170 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 177 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 455 void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadBytes( | 462 void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadBytes( |
| 456 const ReadBytesRequest& request, | 463 const ReadBytesRequest& request, |
| 457 const base::File::Info& file_info) { | 464 const base::File::Info& file_info) { |
| 458 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 465 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 459 DCHECK(request.buf); | 466 DCHECK(request.buf); |
| 460 DCHECK(request.buf_len >= 0); | 467 DCHECK(request.buf_len >= 0); |
| 461 DCHECK(task_in_progress_); | 468 DCHECK(task_in_progress_); |
| 462 base::File::Error error = base::File::FILE_OK; | 469 base::File::Error error = base::File::FILE_OK; |
| 463 if (file_info.is_directory) | 470 if (file_info.is_directory) |
| 464 error = base::File::FILE_ERROR_NOT_A_FILE; | 471 error = base::File::FILE_ERROR_NOT_A_FILE; |
| 465 else if (file_info.size < 0 || file_info.size > kuint32max) | 472 else if (file_info.size < 0 || file_info.size > kuint32max || |
| 473 request.offset >= file_info.size) { | |
|
vandebo (ex-Chrome)
2014/02/27 17:20:50
Should this be > instead of >=. It's possible tha
tommycli
2014/02/27 18:57:33
I think the byte indices run from 0 to size-1, so
| |
| 466 error = base::File::FILE_ERROR_FAILED; | 474 error = base::File::FILE_ERROR_FAILED; |
| 475 } | |
| 467 | 476 |
| 468 if (error != base::File::FILE_OK) | 477 if (error != base::File::FILE_OK) |
| 469 return HandleDeviceFileError(request.error_callback, error); | 478 return HandleDeviceFileError(request.error_callback, error); |
| 470 | 479 |
| 480 int bytes_to_read = std::min( | |
|
vandebo (ex-Chrome)
2014/02/27 17:20:50
You might be able to push the file info validation
tommycli
2014/02/27 18:57:33
Done.
| |
| 481 request.buf_len, | |
| 482 base::checked_cast<int>(file_info.size - request.offset)); | |
| 483 | |
| 471 ReadBytesRequest new_request( | 484 ReadBytesRequest new_request( |
| 472 request.device_file_relative_path, | 485 request.device_file_relative_path, |
| 473 request.buf, | 486 request.buf, |
| 474 request.offset, | 487 request.offset, |
| 475 request.buf_len, | 488 bytes_to_read, |
| 476 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes, | 489 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes, |
| 477 weak_ptr_factory_.GetWeakPtr(), request.success_callback), | 490 weak_ptr_factory_.GetWeakPtr(), request.success_callback), |
| 478 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, | 491 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, |
| 479 weak_ptr_factory_.GetWeakPtr(), request.error_callback)); | 492 weak_ptr_factory_.GetWeakPtr(), request.error_callback)); |
| 480 | 493 |
| 481 content::BrowserThread::PostTask( | 494 content::BrowserThread::PostTask( |
| 482 content::BrowserThread::UI, | 495 content::BrowserThread::UI, |
| 483 FROM_HERE, | 496 FROM_HERE, |
| 484 base::Bind(&ReadBytesOnUIThread, storage_name_, new_request)); | 497 base::Bind(&ReadBytesOnUIThread, storage_name_, new_request, file_info)); |
| 485 } | 498 } |
| 486 | 499 |
| 487 void MTPDeviceDelegateImplLinux::OnDidReadDirectory( | 500 void MTPDeviceDelegateImplLinux::OnDidReadDirectory( |
| 488 const ReadDirectorySuccessCallback& success_callback, | 501 const ReadDirectorySuccessCallback& success_callback, |
| 489 const fileapi::AsyncFileUtil::EntryList& file_list) { | 502 const fileapi::AsyncFileUtil::EntryList& file_list) { |
| 490 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 503 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 491 success_callback.Run(file_list, false /*no more entries*/); | 504 success_callback.Run(file_list, false /*no more entries*/); |
| 492 task_in_progress_ = false; | 505 task_in_progress_ = false; |
| 493 ProcessNextPendingRequest(); | 506 ProcessNextPendingRequest(); |
| 494 } | 507 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 512 DCHECK(current_snapshot_request_info_.get()); | 525 DCHECK(current_snapshot_request_info_.get()); |
| 513 DCHECK(task_in_progress_); | 526 DCHECK(task_in_progress_); |
| 514 current_snapshot_request_info_->error_callback.Run(error); | 527 current_snapshot_request_info_->error_callback.Run(error); |
| 515 task_in_progress_ = false; | 528 task_in_progress_ = false; |
| 516 current_snapshot_request_info_.reset(); | 529 current_snapshot_request_info_.reset(); |
| 517 ProcessNextPendingRequest(); | 530 ProcessNextPendingRequest(); |
| 518 } | 531 } |
| 519 | 532 |
| 520 void MTPDeviceDelegateImplLinux::OnDidReadBytes( | 533 void MTPDeviceDelegateImplLinux::OnDidReadBytes( |
| 521 const ReadBytesSuccessCallback& success_callback, | 534 const ReadBytesSuccessCallback& success_callback, |
| 522 int bytes_read) { | 535 const base::File::Info& file_info, int bytes_read) { |
| 523 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 536 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 524 DCHECK(task_in_progress_); | 537 DCHECK(task_in_progress_); |
| 525 success_callback.Run(bytes_read); | 538 success_callback.Run(file_info, bytes_read); |
| 526 task_in_progress_ = false; | 539 task_in_progress_ = false; |
| 527 ProcessNextPendingRequest(); | 540 ProcessNextPendingRequest(); |
| 528 } | 541 } |
| 529 | 542 |
| 530 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( | 543 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( |
| 531 const ErrorCallback& error_callback, | 544 const ErrorCallback& error_callback, |
| 532 base::File::Error error) { | 545 base::File::Error error) { |
| 533 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 546 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 534 DCHECK(task_in_progress_); | 547 DCHECK(task_in_progress_); |
| 535 error_callback.Run(error); | 548 error_callback.Run(error); |
| 536 task_in_progress_ = false; | 549 task_in_progress_ = false; |
| 537 ProcessNextPendingRequest(); | 550 ProcessNextPendingRequest(); |
| 538 } | 551 } |
| 539 | 552 |
| 540 void CreateMTPDeviceAsyncDelegate( | 553 void CreateMTPDeviceAsyncDelegate( |
| 541 const std::string& device_location, | 554 const std::string& device_location, |
| 542 const CreateMTPDeviceAsyncDelegateCallback& callback) { | 555 const CreateMTPDeviceAsyncDelegateCallback& callback) { |
| 543 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 556 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 544 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); | 557 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); |
| 545 } | 558 } |
| OLD | NEW |