| 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 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 | 288 |
| 286 void MTPDeviceDelegateImplLinux::ReadBytes( | 289 void MTPDeviceDelegateImplLinux::ReadBytes( |
| 287 const base::FilePath& device_file_path, | 290 const base::FilePath& device_file_path, |
| 288 net::IOBuffer* buf, int64 offset, int buf_len, | 291 net::IOBuffer* buf, int64 offset, int buf_len, |
| 289 const ReadBytesSuccessCallback& success_callback, | 292 const ReadBytesSuccessCallback& success_callback, |
| 290 const ErrorCallback& error_callback) { | 293 const ErrorCallback& error_callback) { |
| 291 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 294 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 292 DCHECK(!device_file_path.empty()); | 295 DCHECK(!device_file_path.empty()); |
| 293 std::string device_file_relative_path = | 296 std::string device_file_relative_path = |
| 294 GetDeviceRelativePath(device_path_, device_file_path); | 297 GetDeviceRelativePath(device_path_, device_file_path); |
| 295 ReadBytesRequest request(device_file_relative_path, buf, offset, buf_len, | 298 |
| 296 success_callback, error_callback); | 299 ReadBytesRequest request( |
| 300 device_file_relative_path, buf, offset, buf_len, |
| 301 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes, |
| 302 weak_ptr_factory_.GetWeakPtr(), success_callback), |
| 303 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, |
| 304 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 305 |
| 297 base::Closure call_closure = | 306 base::Closure call_closure = |
| 298 base::Bind( | 307 base::Bind(base::Bind(&ReadBytesOnUIThread, storage_name_, request)); |
| 299 &GetFileInfoOnUIThread, | 308 |
| 300 storage_name_, | |
| 301 device_file_relative_path, | |
| 302 base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadBytes, | |
| 303 weak_ptr_factory_.GetWeakPtr(), request), | |
| 304 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, | |
| 305 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 306 EnsureInitAndRunTask(PendingTaskInfo(FROM_HERE, call_closure)); | 309 EnsureInitAndRunTask(PendingTaskInfo(FROM_HERE, call_closure)); |
| 307 } | 310 } |
| 308 | 311 |
| 309 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { | 312 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { |
| 310 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 313 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 311 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object. | 314 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object. |
| 312 content::BrowserThread::PostTask( | 315 content::BrowserThread::PostTask( |
| 313 content::BrowserThread::UI, | 316 content::BrowserThread::UI, |
| 314 FROM_HERE, | 317 FROM_HERE, |
| 315 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread, storage_name_)); | 318 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread, storage_name_)); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 | 448 |
| 446 current_snapshot_request_info_.reset(snapshot_request_info.release()); | 449 current_snapshot_request_info_.reset(snapshot_request_info.release()); |
| 447 if (file_info.size == 0) { | 450 if (file_info.size == 0) { |
| 448 // Empty snapshot file. | 451 // Empty snapshot file. |
| 449 return OnDidWriteDataIntoSnapshotFile( | 452 return OnDidWriteDataIntoSnapshotFile( |
| 450 snapshot_file_info, current_snapshot_request_info_->snapshot_file_path); | 453 snapshot_file_info, current_snapshot_request_info_->snapshot_file_path); |
| 451 } | 454 } |
| 452 WriteDataIntoSnapshotFile(snapshot_file_info); | 455 WriteDataIntoSnapshotFile(snapshot_file_info); |
| 453 } | 456 } |
| 454 | 457 |
| 455 void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadBytes( | |
| 456 const ReadBytesRequest& request, | |
| 457 const base::File::Info& file_info) { | |
| 458 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 459 DCHECK(request.buf); | |
| 460 DCHECK(request.buf_len >= 0); | |
| 461 DCHECK(task_in_progress_); | |
| 462 base::File::Error error = base::File::FILE_OK; | |
| 463 if (file_info.is_directory) | |
| 464 error = base::File::FILE_ERROR_NOT_A_FILE; | |
| 465 else if (file_info.size < 0 || file_info.size > kuint32max) | |
| 466 error = base::File::FILE_ERROR_FAILED; | |
| 467 | |
| 468 if (error != base::File::FILE_OK) | |
| 469 return HandleDeviceFileError(request.error_callback, error); | |
| 470 | |
| 471 ReadBytesRequest new_request( | |
| 472 request.device_file_relative_path, | |
| 473 request.buf, | |
| 474 request.offset, | |
| 475 request.buf_len, | |
| 476 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes, | |
| 477 weak_ptr_factory_.GetWeakPtr(), request.success_callback), | |
| 478 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, | |
| 479 weak_ptr_factory_.GetWeakPtr(), request.error_callback)); | |
| 480 | |
| 481 content::BrowserThread::PostTask( | |
| 482 content::BrowserThread::UI, | |
| 483 FROM_HERE, | |
| 484 base::Bind(&ReadBytesOnUIThread, storage_name_, new_request)); | |
| 485 } | |
| 486 | |
| 487 void MTPDeviceDelegateImplLinux::OnDidReadDirectory( | 458 void MTPDeviceDelegateImplLinux::OnDidReadDirectory( |
| 488 const ReadDirectorySuccessCallback& success_callback, | 459 const ReadDirectorySuccessCallback& success_callback, |
| 489 const fileapi::AsyncFileUtil::EntryList& file_list) { | 460 const fileapi::AsyncFileUtil::EntryList& file_list) { |
| 490 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 461 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 491 success_callback.Run(file_list, false /*no more entries*/); | 462 success_callback.Run(file_list, false /*no more entries*/); |
| 492 task_in_progress_ = false; | 463 task_in_progress_ = false; |
| 493 ProcessNextPendingRequest(); | 464 ProcessNextPendingRequest(); |
| 494 } | 465 } |
| 495 | 466 |
| 496 void MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile( | 467 void MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 512 DCHECK(current_snapshot_request_info_.get()); | 483 DCHECK(current_snapshot_request_info_.get()); |
| 513 DCHECK(task_in_progress_); | 484 DCHECK(task_in_progress_); |
| 514 current_snapshot_request_info_->error_callback.Run(error); | 485 current_snapshot_request_info_->error_callback.Run(error); |
| 515 task_in_progress_ = false; | 486 task_in_progress_ = false; |
| 516 current_snapshot_request_info_.reset(); | 487 current_snapshot_request_info_.reset(); |
| 517 ProcessNextPendingRequest(); | 488 ProcessNextPendingRequest(); |
| 518 } | 489 } |
| 519 | 490 |
| 520 void MTPDeviceDelegateImplLinux::OnDidReadBytes( | 491 void MTPDeviceDelegateImplLinux::OnDidReadBytes( |
| 521 const ReadBytesSuccessCallback& success_callback, | 492 const ReadBytesSuccessCallback& success_callback, |
| 522 int bytes_read) { | 493 const base::File::Info& file_info, int bytes_read) { |
| 523 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 494 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 524 DCHECK(task_in_progress_); | 495 DCHECK(task_in_progress_); |
| 525 success_callback.Run(bytes_read); | 496 success_callback.Run(file_info, bytes_read); |
| 526 task_in_progress_ = false; | 497 task_in_progress_ = false; |
| 527 ProcessNextPendingRequest(); | 498 ProcessNextPendingRequest(); |
| 528 } | 499 } |
| 529 | 500 |
| 530 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( | 501 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( |
| 531 const ErrorCallback& error_callback, | 502 const ErrorCallback& error_callback, |
| 532 base::File::Error error) { | 503 base::File::Error error) { |
| 533 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 504 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 534 DCHECK(task_in_progress_); | 505 DCHECK(task_in_progress_); |
| 535 error_callback.Run(error); | 506 error_callback.Run(error); |
| 536 task_in_progress_ = false; | 507 task_in_progress_ = false; |
| 537 ProcessNextPendingRequest(); | 508 ProcessNextPendingRequest(); |
| 538 } | 509 } |
| 539 | 510 |
| 540 void CreateMTPDeviceAsyncDelegate( | 511 void CreateMTPDeviceAsyncDelegate( |
| 541 const std::string& device_location, | 512 const std::string& device_location, |
| 542 const CreateMTPDeviceAsyncDelegateCallback& callback) { | 513 const CreateMTPDeviceAsyncDelegateCallback& callback) { |
| 543 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 514 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 544 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); | 515 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); |
| 545 } | 516 } |
| OLD | NEW |