Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: chrome/browser/media_galleries/linux/mtp_device_task_helper.cc

Issue 180783005: MTP Streaming: Optimize block reading (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
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));
114 if (device_handle_.empty()) { 127 if (device_handle_.empty()) {
115 return HandleDeviceError(request.error_callback, 128 return HandleDeviceError(request.error_callback,
116 base::File::FILE_ERROR_FAILED); 129 base::File::FILE_ERROR_FAILED);
117 } 130 }
118 131
119 GetMediaTransferProtocolManager()->ReadFileChunkByPath( 132 GetMediaTransferProtocolManager()->GetFileInfoByPath(
120 device_handle_, 133 device_handle_, request.device_file_relative_path,
121 request.device_file_relative_path, 134 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)); 135 weak_ptr_factory_.GetWeakPtr(), request));
126 } 136 }
127 137
128 void MTPDeviceTaskHelper::CloseStorage() const { 138 void MTPDeviceTaskHelper::CloseStorage() const {
129 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 139 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
130 if (device_handle_.empty()) 140 if (device_handle_.empty())
131 return; 141 return;
132 GetMediaTransferProtocolManager()->CloseStorage(device_handle_, 142 GetMediaTransferProtocolManager()->CloseStorage(device_handle_,
133 base::Bind(&DoNothing)); 143 base::Bind(&DoNothing));
134 } 144 }
(...skipping 13 matching lines...) Expand all
148 const GetFileInfoSuccessCallback& success_callback, 158 const GetFileInfoSuccessCallback& success_callback,
149 const ErrorCallback& error_callback, 159 const ErrorCallback& error_callback,
150 const MtpFileEntry& file_entry, 160 const MtpFileEntry& file_entry,
151 bool error) const { 161 bool error) const {
152 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 162 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
153 if (error) { 163 if (error) {
154 return HandleDeviceError(error_callback, 164 return HandleDeviceError(error_callback,
155 base::File::FILE_ERROR_NOT_FOUND); 165 base::File::FILE_ERROR_NOT_FOUND);
156 } 166 }
157 167
158 base::File::Info file_entry_info; 168 content::BrowserThread::PostTask(
159 file_entry_info.size = file_entry.file_size(); 169 content::BrowserThread::IO,
160 file_entry_info.is_directory = 170 FROM_HERE,
161 file_entry.file_type() == MtpFileEntry::FILE_TYPE_FOLDER; 171 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 } 172 }
172 173
173 void MTPDeviceTaskHelper::OnDidReadDirectoryByPath( 174 void MTPDeviceTaskHelper::OnDidReadDirectoryByPath(
174 const ReadDirectorySuccessCallback& success_callback, 175 const ReadDirectorySuccessCallback& success_callback,
175 const ErrorCallback& error_callback, 176 const ErrorCallback& error_callback,
176 const std::vector<MtpFileEntry>& file_entries, 177 const std::vector<MtpFileEntry>& file_entries,
177 bool error) const { 178 bool error) const {
178 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 179 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
179 if (error) 180 if (error)
180 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED); 181 return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED);
181 182
182 fileapi::AsyncFileUtil::EntryList entries; 183 fileapi::AsyncFileUtil::EntryList entries;
183 base::FilePath current; 184 base::FilePath current;
184 MTPDeviceObjectEnumerator file_enum(file_entries); 185 MTPDeviceObjectEnumerator file_enum(file_entries);
185 while (!(current = file_enum.Next()).empty()) { 186 while (!(current = file_enum.Next()).empty()) {
186 fileapi::DirectoryEntry entry; 187 fileapi::DirectoryEntry entry;
187 entry.name = fileapi::VirtualPath::BaseName(current).value(); 188 entry.name = fileapi::VirtualPath::BaseName(current).value();
188 entry.is_directory = file_enum.IsDirectory(); 189 entry.is_directory = file_enum.IsDirectory();
189 entry.size = file_enum.Size(); 190 entry.size = file_enum.Size();
190 entry.last_modified_time = file_enum.LastModifiedTime(); 191 entry.last_modified_time = file_enum.LastModifiedTime();
191 entries.push_back(entry); 192 entries.push_back(entry);
192 } 193 }
193 content::BrowserThread::PostTask(content::BrowserThread::IO, 194 content::BrowserThread::PostTask(content::BrowserThread::IO,
194 FROM_HERE, 195 FROM_HERE,
195 base::Bind(success_callback, entries)); 196 base::Bind(success_callback, entries));
196 } 197 }
197 198
199 void MTPDeviceTaskHelper::OnGetFileInfoToReadBytes(
200 const MTPDeviceAsyncDelegate::ReadBytesRequest& request,
201 const MtpFileEntry& file_entry,
202 bool error) {
203 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
204 DCHECK(request.buf);
205 DCHECK(request.buf_len >= 0);
206 DCHECK_GE(request.offset, 0);
207 if (error) {
208 return HandleDeviceError(request.error_callback,
209 base::File::FILE_ERROR_FAILED);
210 }
211
212 base::File::Info file_info = FileInfoFromMTPFileEntry(file_entry);
213 if (file_info.is_directory) {
214 return HandleDeviceError(request.error_callback,
215 base::File::FILE_ERROR_NOT_A_FILE);
216 } else if (file_info.size < 0 || file_info.size > kuint32max ||
217 request.offset >= file_info.size) {
218 return HandleDeviceError(request.error_callback,
219 base::File::FILE_ERROR_FAILED);
220 }
221
222 uint32 bytes_to_read = std::min(
223 base::checked_cast<uint32>(request.buf_len),
224 base::saturated_cast<uint32>(file_info.size - request.offset));
225
226 GetMediaTransferProtocolManager()->ReadFileChunkByPath(
227 device_handle_,
228 request.device_file_relative_path,
229 base::checked_cast<uint32>(request.offset),
230 bytes_to_read,
231 base::Bind(&MTPDeviceTaskHelper::OnDidReadBytes,
232 weak_ptr_factory_.GetWeakPtr(), request, file_info));
233 }
234
198 void MTPDeviceTaskHelper::OnDidReadBytes( 235 void MTPDeviceTaskHelper::OnDidReadBytes(
199 const MTPDeviceAsyncDelegate::ReadBytesRequest& request, 236 const MTPDeviceAsyncDelegate::ReadBytesRequest& request,
237 const base::File::Info& file_info,
200 const std::string& data, 238 const std::string& data,
201 bool error) const { 239 bool error) const {
202 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 240 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
203 if (error) { 241 if (error) {
204 return HandleDeviceError(request.error_callback, 242 return HandleDeviceError(request.error_callback,
205 base::File::FILE_ERROR_FAILED); 243 base::File::FILE_ERROR_FAILED);
206 } 244 }
207 245
208 CHECK_LE(base::checked_cast<int>(data.length()), request.buf_len); 246 CHECK_LE(base::checked_cast<int>(data.length()), request.buf_len);
209 std::copy(data.begin(), data.end(), request.buf->data()); 247 std::copy(data.begin(), data.end(), request.buf->data());
210 248
211 content::BrowserThread::PostTask(content::BrowserThread::IO, 249 content::BrowserThread::PostTask(content::BrowserThread::IO,
212 FROM_HERE, 250 FROM_HERE,
213 base::Bind(request.success_callback, 251 base::Bind(request.success_callback,
214 data.length())); 252 file_info, data.length()));
215 } 253 }
216 254
217 void MTPDeviceTaskHelper::HandleDeviceError( 255 void MTPDeviceTaskHelper::HandleDeviceError(
218 const ErrorCallback& error_callback, 256 const ErrorCallback& error_callback,
219 base::File::Error error) const { 257 base::File::Error error) const {
220 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 258 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
221 content::BrowserThread::PostTask(content::BrowserThread::IO, 259 content::BrowserThread::PostTask(content::BrowserThread::IO,
222 FROM_HERE, 260 FROM_HERE,
223 base::Bind(error_callback, error)); 261 base::Bind(error_callback, error));
224 } 262 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698