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 15 matching lines...) Expand all Loading... | |
26 // Does nothing. | 26 // Does nothing. |
27 // This method is used to handle the results of | 27 // This method is used to handle the results of |
28 // MediaTransferProtocolManager::CloseStorage method call. | 28 // MediaTransferProtocolManager::CloseStorage method call. |
29 void DoNothing(bool error) { | 29 void DoNothing(bool error) { |
30 } | 30 } |
31 | 31 |
32 device::MediaTransferProtocolManager* GetMediaTransferProtocolManager() { | 32 device::MediaTransferProtocolManager* GetMediaTransferProtocolManager() { |
33 return StorageMonitor::GetInstance()->media_transfer_protocol_manager(); | 33 return StorageMonitor::GetInstance()->media_transfer_protocol_manager(); |
34 } | 34 } |
35 | 35 |
36 base::File::Info FileInfoFromMTPFileEntry(const MtpFileEntry& file_entry) { | |
37 base::File::Info file_entry_info; | |
38 file_entry_info.size = file_entry.file_size(); | |
39 file_entry_info.is_directory = | |
40 file_entry.file_type() == MtpFileEntry::FILE_TYPE_FOLDER; | |
41 file_entry_info.is_symbolic_link = false; | |
42 file_entry_info.last_modified = | |
43 base::Time::FromTimeT(file_entry.modification_time()); | |
44 file_entry_info.last_accessed = file_entry_info.last_modified; | |
45 file_entry_info.creation_time = base::Time(); | |
46 return file_entry_info; | |
47 } | |
48 | |
36 } // namespace | 49 } // namespace |
37 | 50 |
38 MTPDeviceTaskHelper::MTPDeviceTaskHelper() | 51 MTPDeviceTaskHelper::MTPDeviceTaskHelper() |
39 : weak_ptr_factory_(this) { | 52 : weak_ptr_factory_(this) { |
40 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 53 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
41 } | 54 } |
42 | 55 |
43 MTPDeviceTaskHelper::~MTPDeviceTaskHelper() { | 56 MTPDeviceTaskHelper::~MTPDeviceTaskHelper() { |
44 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 57 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
45 } | 58 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 | 117 |
105 if (!read_file_worker_) | 118 if (!read_file_worker_) |
106 read_file_worker_.reset(new MTPReadFileWorker(device_handle_)); | 119 read_file_worker_.reset(new MTPReadFileWorker(device_handle_)); |
107 read_file_worker_->WriteDataIntoSnapshotFile(request_info, | 120 read_file_worker_->WriteDataIntoSnapshotFile(request_info, |
108 snapshot_file_info); | 121 snapshot_file_info); |
109 } | 122 } |
110 | 123 |
111 void MTPDeviceTaskHelper::ReadBytes( | 124 void MTPDeviceTaskHelper::ReadBytes( |
112 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { | 125 const MTPDeviceAsyncDelegate::ReadBytesRequest& request) { |
113 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 126 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
127 DCHECK(request.buf); | |
128 DCHECK(request.buf_len >= 0); | |
114 if (device_handle_.empty()) { | 129 if (device_handle_.empty()) { |
115 return HandleDeviceError(request.error_callback, | 130 return HandleDeviceError(request.error_callback, |
116 base::File::FILE_ERROR_FAILED); | 131 base::File::FILE_ERROR_FAILED); |
117 } | 132 } |
118 | 133 |
119 GetMediaTransferProtocolManager()->ReadFileChunkByPath( | 134 GetMediaTransferProtocolManager()->GetFileInfoByPath( |
120 device_handle_, | 135 device_handle_, request.device_file_relative_path, |
121 request.device_file_relative_path, | 136 base::Bind(&MTPDeviceTaskHelper::OnGetFileInfoToReadBytes, |
122 base::checked_cast<uint32>(request.offset), | |
123 base::checked_cast<uint32>(request.buf_len), | |
124 base::Bind(&MTPDeviceTaskHelper::OnDidReadBytes, | |
125 weak_ptr_factory_.GetWeakPtr(), request)); | 137 weak_ptr_factory_.GetWeakPtr(), request)); |
126 } | 138 } |
127 | 139 |
128 void MTPDeviceTaskHelper::CloseStorage() const { | 140 void MTPDeviceTaskHelper::CloseStorage() const { |
129 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 141 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
130 if (device_handle_.empty()) | 142 if (device_handle_.empty()) |
131 return; | 143 return; |
132 GetMediaTransferProtocolManager()->CloseStorage(device_handle_, | 144 GetMediaTransferProtocolManager()->CloseStorage(device_handle_, |
133 base::Bind(&DoNothing)); | 145 base::Bind(&DoNothing)); |
134 } | 146 } |
(...skipping 13 matching lines...) Expand all Loading... | |
148 const GetFileInfoSuccessCallback& success_callback, | 160 const GetFileInfoSuccessCallback& success_callback, |
149 const ErrorCallback& error_callback, | 161 const ErrorCallback& error_callback, |
150 const MtpFileEntry& file_entry, | 162 const MtpFileEntry& file_entry, |
151 bool error) const { | 163 bool error) const { |
152 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
153 if (error) { | 165 if (error) { |
154 return HandleDeviceError(error_callback, | 166 return HandleDeviceError(error_callback, |
155 base::File::FILE_ERROR_NOT_FOUND); | 167 base::File::FILE_ERROR_NOT_FOUND); |
156 } | 168 } |
157 | 169 |
158 base::File::Info file_entry_info; | 170 content::BrowserThread::PostTask( |
159 file_entry_info.size = file_entry.file_size(); | 171 content::BrowserThread::IO, |
160 file_entry_info.is_directory = | 172 FROM_HERE, |
161 file_entry.file_type() == MtpFileEntry::FILE_TYPE_FOLDER; | 173 base::Bind(success_callback, FileInfoFromMTPFileEntry(file_entry))); |
162 file_entry_info.is_symbolic_link = false; | |
163 file_entry_info.last_modified = | |
164 base::Time::FromTimeT(file_entry.modification_time()); | |
165 file_entry_info.last_accessed = file_entry_info.last_modified; | |
166 file_entry_info.creation_time = base::Time(); | |
167 content::BrowserThread::PostTask(content::BrowserThread::IO, | |
168 FROM_HERE, | |
169 base::Bind(success_callback, | |
170 file_entry_info)); | |
171 } | 174 } |
172 | 175 |
173 void MTPDeviceTaskHelper::OnDidReadDirectoryByPath( | 176 void MTPDeviceTaskHelper::OnDidReadDirectoryByPath( |
174 const ReadDirectorySuccessCallback& success_callback, | 177 const ReadDirectorySuccessCallback& success_callback, |
175 const ErrorCallback& error_callback, | 178 const ErrorCallback& error_callback, |
176 const std::vector<MtpFileEntry>& file_entries, | 179 const std::vector<MtpFileEntry>& file_entries, |
177 bool error) const { | 180 bool error) const { |
178 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 181 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
179 if (error) | 182 if (error) |
180 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); | 183 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); |
181 | 184 |
182 fileapi::AsyncFileUtil::EntryList entries; | 185 fileapi::AsyncFileUtil::EntryList entries; |
183 base::FilePath current; | 186 base::FilePath current; |
184 MTPDeviceObjectEnumerator file_enum(file_entries); | 187 MTPDeviceObjectEnumerator file_enum(file_entries); |
185 while (!(current = file_enum.Next()).empty()) { | 188 while (!(current = file_enum.Next()).empty()) { |
186 fileapi::DirectoryEntry entry; | 189 fileapi::DirectoryEntry entry; |
187 entry.name = fileapi::VirtualPath::BaseName(current).value(); | 190 entry.name = fileapi::VirtualPath::BaseName(current).value(); |
188 entry.is_directory = file_enum.IsDirectory(); | 191 entry.is_directory = file_enum.IsDirectory(); |
189 entry.size = file_enum.Size(); | 192 entry.size = file_enum.Size(); |
190 entry.last_modified_time = file_enum.LastModifiedTime(); | 193 entry.last_modified_time = file_enum.LastModifiedTime(); |
191 entries.push_back(entry); | 194 entries.push_back(entry); |
192 } | 195 } |
193 content::BrowserThread::PostTask(content::BrowserThread::IO, | 196 content::BrowserThread::PostTask(content::BrowserThread::IO, |
194 FROM_HERE, | 197 FROM_HERE, |
195 base::Bind(success_callback, entries)); | 198 base::Bind(success_callback, entries)); |
196 } | 199 } |
197 | 200 |
201 void MTPDeviceTaskHelper::OnGetFileInfoToReadBytes( | |
202 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, | |
203 const MtpFileEntry& file_entry, | |
204 bool error) { | |
205 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
206 if (error) { | |
207 return HandleDeviceError(request.error_callback, | |
208 base::File::FILE_ERROR_FAILED); | |
209 } | |
210 | |
211 base::File::Info file_info = FileInfoFromMTPFileEntry(file_entry); | |
212 if (file_info.is_directory) { | |
213 return HandleDeviceError(request.error_callback, | |
214 base::File::FILE_ERROR_NOT_A_FILE); | |
215 } else if (file_info.size < 0 || file_info.size > kuint32max || | |
216 request.offset >= file_info.size) { | |
217 return HandleDeviceError(request.error_callback, | |
218 base::File::FILE_ERROR_FAILED); | |
219 } | |
220 | |
221 int bytes_to_read = std::min( | |
222 request.buf_len, | |
223 base::checked_cast<int>(file_info.size - request.offset)); | |
vandebo (ex-Chrome)
2014/02/27 19:32:26
This changed from saturated_cast to checked_cast..
tommycli
2014/02/27 20:18:06
I changed some of these around. I also added some
| |
224 | |
225 GetMediaTransferProtocolManager()->ReadFileChunkByPath( | |
226 device_handle_, | |
227 request.device_file_relative_path, | |
228 base::checked_cast<uint32>(request.offset), | |
229 base::checked_cast<uint32>(bytes_to_read), | |
230 base::Bind(&MTPDeviceTaskHelper::OnDidReadBytes, | |
231 weak_ptr_factory_.GetWeakPtr(), request, file_info)); | |
232 } | |
233 | |
198 void MTPDeviceTaskHelper::OnDidReadBytes( | 234 void MTPDeviceTaskHelper::OnDidReadBytes( |
199 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, | 235 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, |
236 const base::File::Info& file_info, | |
200 const std::string& data, | 237 const std::string& data, |
201 bool error) const { | 238 bool error) const { |
202 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 239 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
203 if (error) { | 240 if (error) { |
204 return HandleDeviceError(request.error_callback, | 241 return HandleDeviceError(request.error_callback, |
205 base::File::FILE_ERROR_FAILED); | 242 base::File::FILE_ERROR_FAILED); |
206 } | 243 } |
207 | 244 |
208 CHECK_LE(base::checked_cast<int>(data.length()), request.buf_len); | 245 CHECK_LE(base::checked_cast<int>(data.length()), request.buf_len); |
209 std::copy(data.begin(), data.end(), request.buf->data()); | 246 std::copy(data.begin(), data.end(), request.buf->data()); |
210 | 247 |
211 content::BrowserThread::PostTask(content::BrowserThread::IO, | 248 content::BrowserThread::PostTask(content::BrowserThread::IO, |
212 FROM_HERE, | 249 FROM_HERE, |
213 base::Bind(request.success_callback, | 250 base::Bind(request.success_callback, |
214 data.length())); | 251 file_info, data.length())); |
215 } | 252 } |
216 | 253 |
217 void MTPDeviceTaskHelper::HandleDeviceError( | 254 void MTPDeviceTaskHelper::HandleDeviceError( |
218 const ErrorCallback& error_callback, | 255 const ErrorCallback& error_callback, |
219 base::File::Error error) const { | 256 base::File::Error error) const { |
220 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 257 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
221 content::BrowserThread::PostTask(content::BrowserThread::IO, | 258 content::BrowserThread::PostTask(content::BrowserThread::IO, |
222 FROM_HERE, | 259 FROM_HERE, |
223 base::Bind(error_callback, error)); | 260 base::Bind(error_callback, error)); |
224 } | 261 } |
OLD | NEW |