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/media_galleries/linux/mtp_device_task_helper.h" | 5 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 base::Bind(callback, true)); | 68 base::Bind(callback, true)); |
69 return; | 69 return; |
70 } | 70 } |
71 GetMediaTransferProtocolManager()->OpenStorage( | 71 GetMediaTransferProtocolManager()->OpenStorage( |
72 storage_name, mtpd::kReadOnlyMode, | 72 storage_name, mtpd::kReadOnlyMode, |
73 base::Bind(&MTPDeviceTaskHelper::OnDidOpenStorage, | 73 base::Bind(&MTPDeviceTaskHelper::OnDidOpenStorage, |
74 weak_ptr_factory_.GetWeakPtr(), | 74 weak_ptr_factory_.GetWeakPtr(), |
75 callback)); | 75 callback)); |
76 } | 76 } |
77 | 77 |
78 void MTPDeviceTaskHelper::GetFileInfoById( | 78 void MTPDeviceTaskHelper::GetFileInfo( |
79 uint32 file_id, | 79 uint32 file_id, |
80 const GetFileInfoSuccessCallback& success_callback, | 80 const GetFileInfoSuccessCallback& success_callback, |
81 const ErrorCallback& error_callback) { | 81 const ErrorCallback& error_callback) { |
82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
83 if (device_handle_.empty()) | 83 if (device_handle_.empty()) |
84 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); | 84 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); |
85 | 85 |
86 GetMediaTransferProtocolManager()->GetFileInfoById( | 86 GetMediaTransferProtocolManager()->GetFileInfo( |
87 device_handle_, file_id, | 87 device_handle_, file_id, |
88 base::Bind(&MTPDeviceTaskHelper::OnGetFileInfo, | 88 base::Bind(&MTPDeviceTaskHelper::OnGetFileInfo, |
89 weak_ptr_factory_.GetWeakPtr(), | 89 weak_ptr_factory_.GetWeakPtr(), |
90 success_callback, | 90 success_callback, |
91 error_callback)); | 91 error_callback)); |
92 } | 92 } |
93 | 93 |
94 void MTPDeviceTaskHelper::ReadDirectoryById( | 94 void MTPDeviceTaskHelper::ReadDirectory( |
95 uint32 dir_id, | 95 uint32 dir_id, |
96 const ReadDirectorySuccessCallback& success_callback, | 96 const ReadDirectorySuccessCallback& success_callback, |
97 const ErrorCallback& error_callback) { | 97 const ErrorCallback& error_callback) { |
98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
99 if (device_handle_.empty()) | 99 if (device_handle_.empty()) |
100 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); | 100 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); |
101 | 101 |
102 GetMediaTransferProtocolManager()->ReadDirectoryById( | 102 GetMediaTransferProtocolManager()->ReadDirectory( |
103 device_handle_, dir_id, | 103 device_handle_, dir_id, |
104 base::Bind(&MTPDeviceTaskHelper::OnDidReadDirectoryById, | 104 base::Bind(&MTPDeviceTaskHelper::OnDidReadDirectory, |
105 weak_ptr_factory_.GetWeakPtr(), | 105 weak_ptr_factory_.GetWeakPtr(), |
106 success_callback, | 106 success_callback, |
107 error_callback)); | 107 error_callback)); |
108 } | 108 } |
109 | 109 |
110 void MTPDeviceTaskHelper::WriteDataIntoSnapshotFile( | 110 void MTPDeviceTaskHelper::WriteDataIntoSnapshotFile( |
111 const SnapshotRequestInfo& request_info, | 111 const SnapshotRequestInfo& request_info, |
112 const base::File::Info& snapshot_file_info) { | 112 const base::File::Info& snapshot_file_info) { |
113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
114 if (device_handle_.empty()) { | 114 if (device_handle_.empty()) { |
115 return HandleDeviceError(request_info.error_callback, | 115 return HandleDeviceError(request_info.error_callback, |
116 base::File::FILE_ERROR_FAILED); | 116 base::File::FILE_ERROR_FAILED); |
117 } | 117 } |
118 | 118 |
119 if (!read_file_worker_) | 119 if (!read_file_worker_) |
120 read_file_worker_.reset(new MTPReadFileWorker(device_handle_)); | 120 read_file_worker_.reset(new MTPReadFileWorker(device_handle_)); |
121 read_file_worker_->WriteDataIntoSnapshotFile(request_info, | 121 read_file_worker_->WriteDataIntoSnapshotFile(request_info, |
122 snapshot_file_info); | 122 snapshot_file_info); |
123 } | 123 } |
124 | 124 |
125 void MTPDeviceTaskHelper::ReadBytes( | 125 void MTPDeviceTaskHelper::ReadBytes( |
126 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { | 126 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { |
127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
128 if (device_handle_.empty()) { | 128 if (device_handle_.empty()) { |
129 return HandleDeviceError(request.error_callback, | 129 return HandleDeviceError(request.error_callback, |
130 base::File::FILE_ERROR_FAILED); | 130 base::File::FILE_ERROR_FAILED); |
131 } | 131 } |
132 | 132 |
133 GetMediaTransferProtocolManager()->GetFileInfoById( | 133 GetMediaTransferProtocolManager()->GetFileInfo( |
134 device_handle_, request.file_id, | 134 device_handle_, request.file_id, |
135 base::Bind(&MTPDeviceTaskHelper::OnGetFileInfoToReadBytes, | 135 base::Bind(&MTPDeviceTaskHelper::OnGetFileInfoToReadBytes, |
136 weak_ptr_factory_.GetWeakPtr(), request)); | 136 weak_ptr_factory_.GetWeakPtr(), request)); |
137 } | 137 } |
138 | 138 |
139 void MTPDeviceTaskHelper::CloseStorage() const { | 139 void MTPDeviceTaskHelper::CloseStorage() const { |
140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
141 if (device_handle_.empty()) | 141 if (device_handle_.empty()) |
142 return; | 142 return; |
143 GetMediaTransferProtocolManager()->CloseStorage(device_handle_, | 143 GetMediaTransferProtocolManager()->CloseStorage(device_handle_, |
(...skipping 21 matching lines...) Expand all Loading... |
165 return HandleDeviceError(error_callback, | 165 return HandleDeviceError(error_callback, |
166 base::File::FILE_ERROR_NOT_FOUND); | 166 base::File::FILE_ERROR_NOT_FOUND); |
167 } | 167 } |
168 | 168 |
169 content::BrowserThread::PostTask( | 169 content::BrowserThread::PostTask( |
170 content::BrowserThread::IO, | 170 content::BrowserThread::IO, |
171 FROM_HERE, | 171 FROM_HERE, |
172 base::Bind(success_callback, FileInfoFromMTPFileEntry(file_entry))); | 172 base::Bind(success_callback, FileInfoFromMTPFileEntry(file_entry))); |
173 } | 173 } |
174 | 174 |
175 void MTPDeviceTaskHelper::OnDidReadDirectoryById( | 175 void MTPDeviceTaskHelper::OnDidReadDirectory( |
176 const ReadDirectorySuccessCallback& success_callback, | 176 const ReadDirectorySuccessCallback& success_callback, |
177 const ErrorCallback& error_callback, | 177 const ErrorCallback& error_callback, |
178 const std::vector<MtpFileEntry>& file_entries, | 178 const std::vector<MtpFileEntry>& file_entries, |
179 bool has_more, | 179 bool has_more, |
180 bool error) const { | 180 bool error) const { |
181 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 181 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
182 if (error) | 182 if (error) |
183 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); | 183 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); |
184 | 184 |
185 fileapi::AsyncFileUtil::EntryList entries; | 185 fileapi::AsyncFileUtil::EntryList entries; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 FROM_HERE, | 230 FROM_HERE, |
231 base::Bind(request.success_callback, | 231 base::Bind(request.success_callback, |
232 file_info, 0u)); | 232 file_info, 0u)); |
233 return; | 233 return; |
234 } | 234 } |
235 | 235 |
236 uint32 bytes_to_read = std::min( | 236 uint32 bytes_to_read = std::min( |
237 base::checked_cast<uint32>(request.buf_len), | 237 base::checked_cast<uint32>(request.buf_len), |
238 base::saturated_cast<uint32>(file_info.size - request.offset)); | 238 base::saturated_cast<uint32>(file_info.size - request.offset)); |
239 | 239 |
240 GetMediaTransferProtocolManager()->ReadFileChunkById( | 240 GetMediaTransferProtocolManager()->ReadFileChunk( |
241 device_handle_, | 241 device_handle_, |
242 request.file_id, | 242 request.file_id, |
243 base::checked_cast<uint32>(request.offset), | 243 base::checked_cast<uint32>(request.offset), |
244 bytes_to_read, | 244 bytes_to_read, |
245 base::Bind(&MTPDeviceTaskHelper::OnDidReadBytes, | 245 base::Bind(&MTPDeviceTaskHelper::OnDidReadBytes, |
246 weak_ptr_factory_.GetWeakPtr(), request, file_info)); | 246 weak_ptr_factory_.GetWeakPtr(), request, file_info)); |
247 } | 247 } |
248 | 248 |
249 void MTPDeviceTaskHelper::OnDidReadBytes( | 249 void MTPDeviceTaskHelper::OnDidReadBytes( |
250 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, | 250 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, |
(...skipping 16 matching lines...) Expand all Loading... |
267 } | 267 } |
268 | 268 |
269 void MTPDeviceTaskHelper::HandleDeviceError( | 269 void MTPDeviceTaskHelper::HandleDeviceError( |
270 const ErrorCallback& error_callback, | 270 const ErrorCallback& error_callback, |
271 base::File::Error error) const { | 271 base::File::Error error) const { |
272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
273 content::BrowserThread::PostTask(content::BrowserThread::IO, | 273 content::BrowserThread::PostTask(content::BrowserThread::IO, |
274 FROM_HERE, | 274 FROM_HERE, |
275 base::Bind(error_callback, error)); | 275 base::Bind(error_callback, error)); |
276 } | 276 } |
OLD | NEW |