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 <fcntl.h> | |
| 7 #include <algorithm> | 8 #include <algorithm> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
| 12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h" | 16 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h" |
| 16 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper_map_servic e.h" | 17 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper_map_servic e.h" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 const std::string& storage_name, | 163 const std::string& storage_name, |
| 163 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { | 164 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { |
| 164 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 165 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 165 MTPDeviceTaskHelper* task_helper = | 166 MTPDeviceTaskHelper* task_helper = |
| 166 GetDeviceTaskHelperForStorage(storage_name); | 167 GetDeviceTaskHelperForStorage(storage_name); |
| 167 if (!task_helper) | 168 if (!task_helper) |
| 168 return; | 169 return; |
| 169 task_helper->ReadBytes(request); | 170 task_helper->ReadBytes(request); |
| 170 } | 171 } |
| 171 | 172 |
| 173 void CopyFileFromLocalOnUIThread( | |
| 174 const std::string& storage_name, | |
| 175 const uint32 source_file_descriptor, | |
| 176 const uint32 parent_id, | |
| 177 const std::string& file_name, | |
| 178 const MTPDeviceTaskHelper::CopyFileFromLocalSuccessCallback& | |
| 179 success_callback, | |
| 180 const MTPDeviceTaskHelper::ErrorCallback& error_callback) { | |
| 181 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 182 MTPDeviceTaskHelper* task_helper = | |
| 183 GetDeviceTaskHelperForStorage(storage_name); | |
| 184 if (!task_helper) | |
| 185 return; | |
| 186 task_helper->CopyFileFromLocal(storage_name, source_file_descriptor, | |
| 187 parent_id, file_name, success_callback, | |
| 188 error_callback); | |
| 189 } | |
| 190 | |
| 172 // Closes the device storage specified by the |storage_name| and destroys the | 191 // Closes the device storage specified by the |storage_name| and destroys the |
| 173 // MTPDeviceTaskHelper object associated with the device storage. | 192 // MTPDeviceTaskHelper object associated with the device storage. |
| 174 // | 193 // |
| 175 // Called on the UI thread to dispatch the request to the | 194 // Called on the UI thread to dispatch the request to the |
| 176 // MediaTransferProtocolManager. | 195 // MediaTransferProtocolManager. |
| 177 void CloseStorageAndDestroyTaskHelperOnUIThread( | 196 void CloseStorageAndDestroyTaskHelperOnUIThread( |
| 178 const std::string& storage_name) { | 197 const std::string& storage_name) { |
| 179 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 198 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 180 MTPDeviceTaskHelper* task_helper = | 199 MTPDeviceTaskHelper* task_helper = |
| 181 GetDeviceTaskHelperForStorage(storage_name); | 200 GetDeviceTaskHelperForStorage(storage_name); |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 offset, | 444 offset, |
| 426 buf_len, | 445 buf_len, |
| 427 success_callback, | 446 success_callback, |
| 428 error_callback); | 447 error_callback); |
| 429 EnsureInitAndRunTask(PendingTaskInfo(device_file_path, | 448 EnsureInitAndRunTask(PendingTaskInfo(device_file_path, |
| 430 content::BrowserThread::IO, | 449 content::BrowserThread::IO, |
| 431 FROM_HERE, | 450 FROM_HERE, |
| 432 closure)); | 451 closure)); |
| 433 } | 452 } |
| 434 | 453 |
| 454 void MTPDeviceDelegateImplLinux::CopyFileFromLocal( | |
| 455 const base::FilePath& source_file_path, | |
| 456 const base::FilePath& device_file_path, | |
| 457 const CopyFileFromLocalSuccessCallback& success_callback, | |
| 458 const ErrorCallback& error_callback) { | |
| 459 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 460 DCHECK(!source_file_path.empty()); | |
| 461 DCHECK(!device_file_path.empty()); | |
| 462 base::Closure closure = | |
| 463 base::Bind(&MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal, | |
| 464 weak_ptr_factory_.GetWeakPtr(), source_file_path, | |
| 465 device_file_path, success_callback, error_callback); | |
| 466 EnsureInitAndRunTask(PendingTaskInfo(device_file_path.DirName(), | |
| 467 content::BrowserThread::IO, FROM_HERE, | |
| 468 closure)); | |
| 469 } | |
| 470 | |
| 435 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { | 471 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { |
| 436 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 472 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 437 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object. | 473 // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object. |
| 438 content::BrowserThread::PostTask( | 474 content::BrowserThread::PostTask( |
| 439 content::BrowserThread::UI, | 475 content::BrowserThread::UI, |
| 440 FROM_HERE, | 476 FROM_HERE, |
| 441 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread, storage_name_)); | 477 base::Bind(&CloseStorageAndDestroyTaskHelperOnUIThread, storage_name_)); |
| 442 delete this; | 478 delete this; |
| 443 } | 479 } |
| 444 | 480 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 573 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), | 609 EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), |
| 574 content::BrowserThread::UI, | 610 content::BrowserThread::UI, |
| 575 FROM_HERE, | 611 FROM_HERE, |
| 576 closure)); | 612 closure)); |
| 577 } else { | 613 } else { |
| 578 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); | 614 error_callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
| 579 } | 615 } |
| 580 PendingRequestDone(); | 616 PendingRequestDone(); |
| 581 } | 617 } |
| 582 | 618 |
| 619 void MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal( | |
| 620 const base::FilePath& source_file_path, | |
| 621 const base::FilePath& device_file_path, | |
| 622 const CopyFileFromLocalSuccessCallback& success_callback, | |
| 623 const ErrorCallback& error_callback) { | |
| 624 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 625 | |
| 626 uint32 parent_id; | |
| 627 if (CachedPathToId(device_file_path.DirName(), &parent_id)) { | |
| 628 const uint32 source_file_descriptor = | |
| 629 open(source_file_path.value().c_str(), O_RDONLY); | |
|
Lei Zhang
2015/02/24 23:35:48
open() can fail.
yawano
2015/02/26 07:27:54
Done.
| |
| 630 | |
| 631 CopyFileFromLocalSuccessCallback success_callback_wrapper = | |
| 632 base::Bind(&MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal, | |
| 633 weak_ptr_factory_.GetWeakPtr(), success_callback, | |
| 634 source_file_descriptor); | |
| 635 | |
| 636 ErrorCallback error_callback_wrapper = base::Bind( | |
| 637 &MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError, | |
| 638 weak_ptr_factory_.GetWeakPtr(), error_callback, source_file_descriptor); | |
| 639 | |
| 640 base::Closure closure = base::Bind(base::Bind( | |
|
Lei Zhang
2015/02/24 23:35:48
I think you copied a typo from elsewhere in the fi
yawano
2015/02/26 07:27:54
Done.
| |
| 641 &CopyFileFromLocalOnUIThread, storage_name_, source_file_descriptor, | |
| 642 parent_id, device_file_path.BaseName().value(), | |
| 643 success_callback_wrapper, error_callback_wrapper)); | |
| 644 EnsureInitAndRunTask(PendingTaskInfo( | |
| 645 base::FilePath(), content::BrowserThread::UI, FROM_HERE, closure)); | |
| 646 } else { | |
| 647 error_callback.Run(base::File::FILE_ERROR_INVALID_OPERATION); | |
| 648 } | |
| 649 | |
| 650 PendingRequestDone(); | |
| 651 } | |
| 652 | |
| 583 void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask( | 653 void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask( |
| 584 const PendingTaskInfo& task_info) { | 654 const PendingTaskInfo& task_info) { |
| 585 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 655 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 586 if ((init_state_ == INITIALIZED) && !task_in_progress_) { | 656 if ((init_state_ == INITIALIZED) && !task_in_progress_) { |
| 587 RunTask(task_info); | 657 RunTask(task_info); |
| 588 return; | 658 return; |
| 589 } | 659 } |
| 590 | 660 |
| 591 // Only *Internal functions have empty paths. Since they are the continuation | 661 // Only *Internal functions have empty paths. Since they are the continuation |
| 592 // of the current running task, they get to cut in line. | 662 // of the current running task, they get to cut in line. |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 847 | 917 |
| 848 void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed( | 918 void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed( |
| 849 base::File::Error /* error */) { | 919 base::File::Error /* error */) { |
| 850 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 920 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 851 // When filling the cache fails for the task at the front of the queue, clear | 921 // When filling the cache fails for the task at the front of the queue, clear |
| 852 // the path of the task so it will not try to do any more caching. Instead, | 922 // the path of the task so it will not try to do any more caching. Instead, |
| 853 // the task will just run and fail the CachedPathToId() lookup. | 923 // the task will just run and fail the CachedPathToId() lookup. |
| 854 pending_tasks_.front().path.clear(); | 924 pending_tasks_.front().path.clear(); |
| 855 } | 925 } |
| 856 | 926 |
| 927 void MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal( | |
| 928 const CopyFileFromLocalSuccessCallback& success_callback, | |
| 929 const uint32 source_file_descriptor) { | |
| 930 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 931 | |
| 932 close(source_file_descriptor); | |
|
Lei Zhang
2015/02/24 23:35:48
Wrap this in IGNORE_EINTR()
yawano
2015/02/26 07:27:54
Done.
| |
| 933 success_callback.Run(); | |
| 934 PendingRequestDone(); | |
| 935 } | |
| 936 | |
| 937 void MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError( | |
| 938 const ErrorCallback& error_callback, | |
| 939 const uint32 source_file_descriptor, | |
| 940 base::File::Error error) { | |
| 941 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 942 | |
| 943 close(source_file_descriptor); | |
| 944 error_callback.Run(error); | |
| 945 PendingRequestDone(); | |
| 946 } | |
| 947 | |
| 857 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( | 948 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( |
| 858 const ErrorCallback& error_callback, | 949 const ErrorCallback& error_callback, |
| 859 uint32 file_id, | 950 uint32 file_id, |
| 860 base::File::Error error) { | 951 base::File::Error error) { |
| 861 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 952 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 862 | 953 |
| 863 FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(file_id); | 954 FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(file_id); |
| 864 if (it != file_id_to_node_map_.end()) { | 955 if (it != file_id_to_node_map_.end()) { |
| 865 MTPFileNode* parent = it->second->parent(); | 956 MTPFileNode* parent = it->second->parent(); |
| 866 if (parent) { | 957 if (parent) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 940 *id = current_node->file_id(); | 1031 *id = current_node->file_id(); |
| 941 return true; | 1032 return true; |
| 942 } | 1033 } |
| 943 | 1034 |
| 944 void CreateMTPDeviceAsyncDelegate( | 1035 void CreateMTPDeviceAsyncDelegate( |
| 945 const std::string& device_location, | 1036 const std::string& device_location, |
| 946 const CreateMTPDeviceAsyncDelegateCallback& callback) { | 1037 const CreateMTPDeviceAsyncDelegateCallback& callback) { |
| 947 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 1038 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 948 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); | 1039 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); |
| 949 } | 1040 } |
| OLD | NEW |