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" |
| 11 #include "base/strings/string_number_conversions.h" |
11 #include "chrome/browser/media_galleries/linux/mtp_device_object_enumerator.h" | 12 #include "chrome/browser/media_galleries/linux/mtp_device_object_enumerator.h" |
12 #include "chrome/browser/media_galleries/linux/mtp_read_file_worker.h" | 13 #include "chrome/browser/media_galleries/linux/mtp_read_file_worker.h" |
13 #include "chrome/browser/media_galleries/linux/snapshot_file_details.h" | 14 #include "chrome/browser/media_galleries/linux/snapshot_file_details.h" |
14 #include "components/storage_monitor/storage_monitor.h" | 15 #include "components/storage_monitor/storage_monitor.h" |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h" | 17 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h" |
17 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
18 #include "third_party/cros_system_api/dbus/service_constants.h" | 19 #include "third_party/cros_system_api/dbus/service_constants.h" |
19 #include "webkit/browser/fileapi/async_file_util.h" | 20 #include "webkit/browser/fileapi/async_file_util.h" |
20 #include "webkit/common/fileapi/file_system_util.h" | 21 #include "webkit/common/fileapi/file_system_util.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 base::Bind(callback, true)); | 68 base::Bind(callback, true)); |
68 return; | 69 return; |
69 } | 70 } |
70 GetMediaTransferProtocolManager()->OpenStorage( | 71 GetMediaTransferProtocolManager()->OpenStorage( |
71 storage_name, mtpd::kReadOnlyMode, | 72 storage_name, mtpd::kReadOnlyMode, |
72 base::Bind(&MTPDeviceTaskHelper::OnDidOpenStorage, | 73 base::Bind(&MTPDeviceTaskHelper::OnDidOpenStorage, |
73 weak_ptr_factory_.GetWeakPtr(), | 74 weak_ptr_factory_.GetWeakPtr(), |
74 callback)); | 75 callback)); |
75 } | 76 } |
76 | 77 |
77 void MTPDeviceTaskHelper::GetFileInfoByPath( | 78 void MTPDeviceTaskHelper::GetFileInfoById( |
78 const std::string& file_path, | 79 uint32 file_id, |
79 const GetFileInfoSuccessCallback& success_callback, | 80 const GetFileInfoSuccessCallback& success_callback, |
80 const ErrorCallback& error_callback) { | 81 const ErrorCallback& error_callback) { |
81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
82 if (device_handle_.empty()) | 83 if (device_handle_.empty()) |
83 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); | 84 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); |
84 | 85 |
85 GetMediaTransferProtocolManager()->GetFileInfoByPath( | 86 GetMediaTransferProtocolManager()->GetFileInfoById( |
86 device_handle_, file_path, | 87 device_handle_, file_id, |
87 base::Bind(&MTPDeviceTaskHelper::OnGetFileInfo, | 88 base::Bind(&MTPDeviceTaskHelper::OnGetFileInfo, |
88 weak_ptr_factory_.GetWeakPtr(), | 89 weak_ptr_factory_.GetWeakPtr(), |
89 success_callback, | 90 success_callback, |
90 error_callback)); | 91 error_callback)); |
91 } | 92 } |
92 | 93 |
93 void MTPDeviceTaskHelper::ReadDirectoryByPath( | 94 void MTPDeviceTaskHelper::ReadDirectoryById( |
94 const std::string& dir_path, | 95 uint32 dir_id, |
95 const ReadDirectorySuccessCallback& success_callback, | 96 const ReadDirectorySuccessCallback& success_callback, |
96 const ErrorCallback& error_callback) { | 97 const ErrorCallback& error_callback) { |
97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
98 if (device_handle_.empty()) | 99 if (device_handle_.empty()) |
99 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); | 100 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); |
100 | 101 |
101 GetMediaTransferProtocolManager()->ReadDirectoryByPath( | 102 GetMediaTransferProtocolManager()->ReadDirectoryById( |
102 device_handle_, dir_path, | 103 device_handle_, dir_id, |
103 base::Bind(&MTPDeviceTaskHelper::OnDidReadDirectoryByPath, | 104 base::Bind(&MTPDeviceTaskHelper::OnDidReadDirectoryById, |
104 weak_ptr_factory_.GetWeakPtr(), | 105 weak_ptr_factory_.GetWeakPtr(), |
105 success_callback, | 106 success_callback, |
106 error_callback)); | 107 error_callback)); |
107 } | 108 } |
108 | 109 |
109 void MTPDeviceTaskHelper::WriteDataIntoSnapshotFile( | 110 void MTPDeviceTaskHelper::WriteDataIntoSnapshotFile( |
110 const SnapshotRequestInfo& request_info, | 111 const SnapshotRequestInfo& request_info, |
111 const base::File::Info& snapshot_file_info) { | 112 const base::File::Info& snapshot_file_info) { |
112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
113 if (device_handle_.empty()) { | 114 if (device_handle_.empty()) { |
114 return HandleDeviceError(request_info.error_callback, | 115 return HandleDeviceError(request_info.error_callback, |
115 base::File::FILE_ERROR_FAILED); | 116 base::File::FILE_ERROR_FAILED); |
116 } | 117 } |
117 | 118 |
118 if (!read_file_worker_) | 119 if (!read_file_worker_) |
119 read_file_worker_.reset(new MTPReadFileWorker(device_handle_)); | 120 read_file_worker_.reset(new MTPReadFileWorker(device_handle_)); |
120 read_file_worker_->WriteDataIntoSnapshotFile(request_info, | 121 read_file_worker_->WriteDataIntoSnapshotFile(request_info, |
121 snapshot_file_info); | 122 snapshot_file_info); |
122 } | 123 } |
123 | 124 |
124 void MTPDeviceTaskHelper::ReadBytes( | 125 void MTPDeviceTaskHelper::ReadBytes( |
125 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { | 126 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { |
126 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
127 if (device_handle_.empty()) { | 128 if (device_handle_.empty()) { |
128 return HandleDeviceError(request.error_callback, | 129 return HandleDeviceError(request.error_callback, |
129 base::File::FILE_ERROR_FAILED); | 130 base::File::FILE_ERROR_FAILED); |
130 } | 131 } |
131 | 132 |
132 GetMediaTransferProtocolManager()->GetFileInfoByPath( | 133 GetMediaTransferProtocolManager()->GetFileInfoById( |
133 device_handle_, request.device_file_relative_path, | 134 device_handle_, request.file_id, |
134 base::Bind(&MTPDeviceTaskHelper::OnGetFileInfoToReadBytes, | 135 base::Bind(&MTPDeviceTaskHelper::OnGetFileInfoToReadBytes, |
135 weak_ptr_factory_.GetWeakPtr(), request)); | 136 weak_ptr_factory_.GetWeakPtr(), request)); |
136 } | 137 } |
137 | 138 |
138 void MTPDeviceTaskHelper::CloseStorage() const { | 139 void MTPDeviceTaskHelper::CloseStorage() const { |
139 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
140 if (device_handle_.empty()) | 141 if (device_handle_.empty()) |
141 return; | 142 return; |
142 GetMediaTransferProtocolManager()->CloseStorage(device_handle_, | 143 GetMediaTransferProtocolManager()->CloseStorage(device_handle_, |
143 base::Bind(&DoNothing)); | 144 base::Bind(&DoNothing)); |
(...skipping 20 matching lines...) Expand all Loading... |
164 return HandleDeviceError(error_callback, | 165 return HandleDeviceError(error_callback, |
165 base::File::FILE_ERROR_NOT_FOUND); | 166 base::File::FILE_ERROR_NOT_FOUND); |
166 } | 167 } |
167 | 168 |
168 content::BrowserThread::PostTask( | 169 content::BrowserThread::PostTask( |
169 content::BrowserThread::IO, | 170 content::BrowserThread::IO, |
170 FROM_HERE, | 171 FROM_HERE, |
171 base::Bind(success_callback, FileInfoFromMTPFileEntry(file_entry))); | 172 base::Bind(success_callback, FileInfoFromMTPFileEntry(file_entry))); |
172 } | 173 } |
173 | 174 |
174 void MTPDeviceTaskHelper::OnDidReadDirectoryByPath( | 175 void MTPDeviceTaskHelper::OnDidReadDirectoryById( |
175 const ReadDirectorySuccessCallback& success_callback, | 176 const ReadDirectorySuccessCallback& success_callback, |
176 const ErrorCallback& error_callback, | 177 const ErrorCallback& error_callback, |
177 const std::vector<MtpFileEntry>& file_entries, | 178 const std::vector<MtpFileEntry>& file_entries, |
178 bool error) const { | 179 bool error) const { |
179 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 180 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
180 if (error) | 181 if (error) |
181 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); | 182 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); |
182 | 183 |
183 fileapi::AsyncFileUtil::EntryList entries; | 184 fileapi::AsyncFileUtil::EntryList entries; |
184 base::FilePath current; | 185 base::FilePath current; |
185 MTPDeviceObjectEnumerator file_enum(file_entries); | 186 MTPDeviceObjectEnumerator file_enum(file_entries); |
186 while (!(current = file_enum.Next()).empty()) { | 187 while (!(current = file_enum.Next()).empty()) { |
187 fileapi::DirectoryEntry entry; | 188 fileapi::DirectoryEntry entry; |
188 entry.name = fileapi::VirtualPath::BaseName(current).value(); | 189 entry.name = fileapi::VirtualPath::BaseName(current).value(); |
| 190 uint32 file_id = 0; |
| 191 bool ret = file_enum.GetEntryId(&file_id); |
| 192 DCHECK(ret); |
| 193 entry.name.push_back(','); |
| 194 entry.name += base::UintToString(file_id); |
189 entry.is_directory = file_enum.IsDirectory(); | 195 entry.is_directory = file_enum.IsDirectory(); |
190 entry.size = file_enum.Size(); | 196 entry.size = file_enum.Size(); |
191 entry.last_modified_time = file_enum.LastModifiedTime(); | 197 entry.last_modified_time = file_enum.LastModifiedTime(); |
192 entries.push_back(entry); | 198 entries.push_back(entry); |
193 } | 199 } |
194 content::BrowserThread::PostTask(content::BrowserThread::IO, | 200 content::BrowserThread::PostTask(content::BrowserThread::IO, |
195 FROM_HERE, | 201 FROM_HERE, |
196 base::Bind(success_callback, entries)); | 202 base::Bind(success_callback, entries)); |
197 } | 203 } |
198 | 204 |
199 void MTPDeviceTaskHelper::OnGetFileInfoToReadBytes( | 205 void MTPDeviceTaskHelper::OnGetFileInfoToReadBytes( |
200 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, | 206 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, |
201 const MtpFileEntry& file_entry, | 207 const MtpFileEntry& file_entry, |
202 bool error) { | 208 bool error) { |
203 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 209 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
204 DCHECK(request.buf); | 210 DCHECK(request.buf); |
205 DCHECK(request.buf_len >= 0); | 211 DCHECK_GE(request.buf_len, 0); |
206 DCHECK_GE(request.offset, 0); | 212 DCHECK_GE(request.offset, 0); |
207 if (error) { | 213 if (error) { |
208 return HandleDeviceError(request.error_callback, | 214 return HandleDeviceError(request.error_callback, |
209 base::File::FILE_ERROR_FAILED); | 215 base::File::FILE_ERROR_FAILED); |
210 } | 216 } |
211 | 217 |
212 base::File::Info file_info = FileInfoFromMTPFileEntry(file_entry); | 218 base::File::Info file_info = FileInfoFromMTPFileEntry(file_entry); |
213 if (file_info.is_directory) { | 219 if (file_info.is_directory) { |
214 return HandleDeviceError(request.error_callback, | 220 return HandleDeviceError(request.error_callback, |
215 base::File::FILE_ERROR_NOT_A_FILE); | 221 base::File::FILE_ERROR_NOT_A_FILE); |
216 } else if (file_info.size < 0 || file_info.size > kuint32max || | 222 } else if (file_info.size < 0 || file_info.size > kuint32max || |
217 request.offset > file_info.size) { | 223 request.offset > file_info.size) { |
218 return HandleDeviceError(request.error_callback, | 224 return HandleDeviceError(request.error_callback, |
219 base::File::FILE_ERROR_FAILED); | 225 base::File::FILE_ERROR_FAILED); |
220 } else if (request.offset == file_info.size) { | 226 } else if (request.offset == file_info.size) { |
221 content::BrowserThread::PostTask(content::BrowserThread::IO, | 227 content::BrowserThread::PostTask(content::BrowserThread::IO, |
222 FROM_HERE, | 228 FROM_HERE, |
223 base::Bind(request.success_callback, | 229 base::Bind(request.success_callback, |
224 file_info, 0u)); | 230 file_info, 0u)); |
225 return; | 231 return; |
226 } | 232 } |
227 | 233 |
228 uint32 bytes_to_read = std::min( | 234 uint32 bytes_to_read = std::min( |
229 base::checked_cast<uint32>(request.buf_len), | 235 base::checked_cast<uint32>(request.buf_len), |
230 base::saturated_cast<uint32>(file_info.size - request.offset)); | 236 base::saturated_cast<uint32>(file_info.size - request.offset)); |
231 | 237 |
232 GetMediaTransferProtocolManager()->ReadFileChunkByPath( | 238 GetMediaTransferProtocolManager()->ReadFileChunkById( |
233 device_handle_, | 239 device_handle_, |
234 request.device_file_relative_path, | 240 request.file_id, |
235 base::checked_cast<uint32>(request.offset), | 241 base::checked_cast<uint32>(request.offset), |
236 bytes_to_read, | 242 bytes_to_read, |
237 base::Bind(&MTPDeviceTaskHelper::OnDidReadBytes, | 243 base::Bind(&MTPDeviceTaskHelper::OnDidReadBytes, |
238 weak_ptr_factory_.GetWeakPtr(), request, file_info)); | 244 weak_ptr_factory_.GetWeakPtr(), request, file_info)); |
239 } | 245 } |
240 | 246 |
241 void MTPDeviceTaskHelper::OnDidReadBytes( | 247 void MTPDeviceTaskHelper::OnDidReadBytes( |
242 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, | 248 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, |
243 const base::File::Info& file_info, | 249 const base::File::Info& file_info, |
244 const std::string& data, | 250 const std::string& data, |
(...skipping 14 matching lines...) Expand all Loading... |
259 } | 265 } |
260 | 266 |
261 void MTPDeviceTaskHelper::HandleDeviceError( | 267 void MTPDeviceTaskHelper::HandleDeviceError( |
262 const ErrorCallback& error_callback, | 268 const ErrorCallback& error_callback, |
263 base::File::Error error) const { | 269 base::File::Error error) const { |
264 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 270 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
265 content::BrowserThread::PostTask(content::BrowserThread::IO, | 271 content::BrowserThread::PostTask(content::BrowserThread::IO, |
266 FROM_HERE, | 272 FROM_HERE, |
267 base::Bind(error_callback, error)); | 273 base::Bind(error_callback, error)); |
268 } | 274 } |
OLD | NEW |