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

Side by Side Diff: chrome/browser/media_galleries/win/mtp_device_delegate_impl_win.cc

Issue 145303002: Convert Media Galleries to use base::File (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 11 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 // MTPDeviceDelegateImplWin implementation. 5 // MTPDeviceDelegateImplWin implementation.
6 6
7 #include "chrome/browser/media_galleries/win/mtp_device_delegate_impl_win.h" 7 #include "chrome/browser/media_galleries/win/mtp_device_delegate_impl_win.h"
8 8
9 #include <portabledevice.h> 9 #include <portabledevice.h>
10 10
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 bool init_succeeded = device.get() != NULL; 141 bool init_succeeded = device.get() != NULL;
142 if (init_succeeded) { 142 if (init_succeeded) {
143 PortableDeviceMapService::GetInstance()->AddPortableDevice( 143 PortableDeviceMapService::GetInstance()->AddPortableDevice(
144 registered_device_path, device.get()); 144 registered_device_path, device.get());
145 } 145 }
146 return init_succeeded; 146 return init_succeeded;
147 } 147 }
148 148
149 // Gets the |file_path| details from the MTP device specified by the 149 // Gets the |file_path| details from the MTP device specified by the
150 // |device_info.registered_device_path|. On success, |error| is set to 150 // |device_info.registered_device_path|. On success, |error| is set to
151 // base::PLATFORM_FILE_OK and fills in |file_info|. On failure, |error| is set 151 // base::File::FILE_OK and fills in |file_info|. On failure, |error| is set
152 // to corresponding platform file error and |file_info| is not set. 152 // to corresponding platform file error and |file_info| is not set.
153 base::PlatformFileError GetFileInfoOnBlockingPoolThread( 153 base::File::Error GetFileInfoOnBlockingPoolThread(
154 const MTPDeviceDelegateImplWin::StorageDeviceInfo& device_info, 154 const MTPDeviceDelegateImplWin::StorageDeviceInfo& device_info,
155 const base::FilePath& file_path, 155 const base::FilePath& file_path,
156 base::PlatformFileInfo* file_info) { 156 base::File::Info* file_info) {
157 base::ThreadRestrictions::AssertIOAllowed(); 157 base::ThreadRestrictions::AssertIOAllowed();
158 DCHECK(!device_info.registered_device_path.empty()); 158 DCHECK(!device_info.registered_device_path.empty());
159 DCHECK(!file_path.empty()); 159 DCHECK(!file_path.empty());
160 DCHECK(file_info); 160 DCHECK(file_info);
161 IPortableDevice* device = 161 IPortableDevice* device =
162 PortableDeviceMapService::GetInstance()->GetPortableDevice( 162 PortableDeviceMapService::GetInstance()->GetPortableDevice(
163 device_info.registered_device_path); 163 device_info.registered_device_path);
164 if (!device) 164 if (!device)
165 return base::PLATFORM_FILE_ERROR_FAILED; 165 return base::File::FILE_ERROR_FAILED;
166 166
167 base::string16 object_id = 167 base::string16 object_id =
168 GetFileObjectIdFromPathOnBlockingPoolThread(device_info, file_path); 168 GetFileObjectIdFromPathOnBlockingPoolThread(device_info, file_path);
169 if (object_id.empty()) 169 if (object_id.empty())
170 return base::PLATFORM_FILE_ERROR_FAILED; 170 return base::File::FILE_ERROR_FAILED;
171 return media_transfer_protocol::GetFileEntryInfo(device, object_id, 171 return media_transfer_protocol::GetFileEntryInfo(device, object_id,
172 file_info); 172 file_info);
173 } 173 }
174 174
175 // Reads the |root| directory file entries on a blocking pool thread. On 175 // Reads the |root| directory file entries on a blocking pool thread. On
176 // success, |error| is set to base::PLATFORM_FILE_OK and |entries| contains the 176 // success, |error| is set to base::File::FILE_OK and |entries| contains the
177 // directory file entries. On failure, |error| is set to platform file error 177 // directory file entries. On failure, |error| is set to platform file error
178 // and |entries| is not set. 178 // and |entries| is not set.
179 base::PlatformFileError ReadDirectoryOnBlockingPoolThread( 179 base::File::Error ReadDirectoryOnBlockingPoolThread(
180 const MTPDeviceDelegateImplWin::StorageDeviceInfo& device_info, 180 const MTPDeviceDelegateImplWin::StorageDeviceInfo& device_info,
181 const base::FilePath& root, 181 const base::FilePath& root,
182 fileapi::AsyncFileUtil::EntryList* entries) { 182 fileapi::AsyncFileUtil::EntryList* entries) {
183 base::ThreadRestrictions::AssertIOAllowed(); 183 base::ThreadRestrictions::AssertIOAllowed();
184 DCHECK(!root.empty()); 184 DCHECK(!root.empty());
185 DCHECK(entries); 185 DCHECK(entries);
186 base::PlatformFileInfo file_info; 186 base::File::Info file_info;
187 base::PlatformFileError error = GetFileInfoOnBlockingPoolThread(device_info, 187 base::File::Error error = GetFileInfoOnBlockingPoolThread(device_info, root,
188 root, 188 &file_info);
189 &file_info); 189 if (error != base::File::FILE_OK)
190 if (error != base::PLATFORM_FILE_OK)
191 return error; 190 return error;
192 191
193 if (!file_info.is_directory) 192 if (!file_info.is_directory)
194 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; 193 return base::File::FILE_ERROR_NOT_A_DIRECTORY;
195 194
196 base::FilePath current; 195 base::FilePath current;
197 scoped_ptr<MTPDeviceObjectEnumerator> file_enum = 196 scoped_ptr<MTPDeviceObjectEnumerator> file_enum =
198 CreateFileEnumeratorOnBlockingPoolThread(device_info, root); 197 CreateFileEnumeratorOnBlockingPoolThread(device_info, root);
199 if (!file_enum) 198 if (!file_enum)
200 return error; 199 return error;
201 200
202 while (!(current = file_enum->Next()).empty()) { 201 while (!(current = file_enum->Next()).empty()) {
203 fileapi::DirectoryEntry entry; 202 fileapi::DirectoryEntry entry;
204 entry.is_directory = file_enum->IsDirectory(); 203 entry.is_directory = file_enum->IsDirectory();
205 entry.name = fileapi::VirtualPath::BaseName(current).value(); 204 entry.name = fileapi::VirtualPath::BaseName(current).value();
206 entry.size = file_enum->Size(); 205 entry.size = file_enum->Size();
207 entry.last_modified_time = file_enum->LastModifiedTime(); 206 entry.last_modified_time = file_enum->LastModifiedTime();
208 entries->push_back(entry); 207 entries->push_back(entry);
209 } 208 }
210 return error; 209 return error;
211 } 210 }
212 211
213 // Gets the device file stream object on a blocking pool thread. 212 // Gets the device file stream object on a blocking pool thread.
214 // |device_info| contains the device storage partition details. 213 // |device_info| contains the device storage partition details.
215 // On success, returns base::PLATFORM_FILE_OK and file stream details are set in 214 // On success, returns base::File::FILE_OK and file stream details are set in
216 // |file_details|. On failure, returns a platform file error and file stream 215 // |file_details|. On failure, returns a platform file error and file stream
217 // details are not set in |file_details|. 216 // details are not set in |file_details|.
218 base::PlatformFileError GetFileStreamOnBlockingPoolThread( 217 base::File::Error GetFileStreamOnBlockingPoolThread(
219 const MTPDeviceDelegateImplWin::StorageDeviceInfo& device_info, 218 const MTPDeviceDelegateImplWin::StorageDeviceInfo& device_info,
220 SnapshotFileDetails* file_details) { 219 SnapshotFileDetails* file_details) {
221 base::ThreadRestrictions::AssertIOAllowed(); 220 base::ThreadRestrictions::AssertIOAllowed();
222 DCHECK(file_details); 221 DCHECK(file_details);
223 DCHECK(!file_details->request_info().device_file_path.empty()); 222 DCHECK(!file_details->request_info().device_file_path.empty());
224 DCHECK(!file_details->request_info().snapshot_file_path.empty()); 223 DCHECK(!file_details->request_info().snapshot_file_path.empty());
225 IPortableDevice* device = 224 IPortableDevice* device =
226 PortableDeviceMapService::GetInstance()->GetPortableDevice( 225 PortableDeviceMapService::GetInstance()->GetPortableDevice(
227 device_info.registered_device_path); 226 device_info.registered_device_path);
228 if (!device) 227 if (!device)
229 return base::PLATFORM_FILE_ERROR_FAILED; 228 return base::File::FILE_ERROR_FAILED;
230 229
231 base::string16 file_object_id = 230 base::string16 file_object_id =
232 GetFileObjectIdFromPathOnBlockingPoolThread( 231 GetFileObjectIdFromPathOnBlockingPoolThread(
233 device_info, file_details->request_info().device_file_path); 232 device_info, file_details->request_info().device_file_path);
234 if (file_object_id.empty()) 233 if (file_object_id.empty())
235 return base::PLATFORM_FILE_ERROR_FAILED; 234 return base::File::FILE_ERROR_FAILED;
236 235
237 base::PlatformFileInfo file_info; 236 base::File::Info file_info;
238 base::PlatformFileError error = 237 base::File::Error error =
239 GetFileInfoOnBlockingPoolThread( 238 GetFileInfoOnBlockingPoolThread(
240 device_info, 239 device_info,
241 file_details->request_info().device_file_path, 240 file_details->request_info().device_file_path,
242 &file_info); 241 &file_info);
243 if (error != base::PLATFORM_FILE_OK) 242 if (error != base::File::FILE_OK)
244 return error; 243 return error;
245 244
246 DWORD optimal_transfer_size = 0; 245 DWORD optimal_transfer_size = 0;
247 base::win::ScopedComPtr<IStream> file_stream; 246 base::win::ScopedComPtr<IStream> file_stream;
248 if (file_info.size > 0) { 247 if (file_info.size > 0) {
249 HRESULT hr = media_transfer_protocol::GetFileStreamForObject( 248 HRESULT hr = media_transfer_protocol::GetFileStreamForObject(
250 device, 249 device,
251 file_object_id, 250 file_object_id,
252 file_stream.Receive(), 251 file_stream.Receive(),
253 &optimal_transfer_size); 252 &optimal_transfer_size);
254 if (hr != S_OK) 253 if (hr != S_OK)
255 return base::PLATFORM_FILE_ERROR_FAILED; 254 return base::File::FILE_ERROR_FAILED;
256 } 255 }
257 256
258 // LocalFileStreamReader is used to read the contents of the snapshot file. 257 // LocalFileStreamReader is used to read the contents of the snapshot file.
259 // Snapshot file modification time does not match the last modified time 258 // Snapshot file modification time does not match the last modified time
260 // of the original media file. Therefore, set the last modified time to null 259 // of the original media file. Therefore, set the last modified time to null
261 // in order to avoid the verification in LocalFileStreamReader. 260 // in order to avoid the verification in LocalFileStreamReader.
262 // 261 //
263 // Users will use HTML5 FileSystem Entry getMetadata() interface to get the 262 // Users will use HTML5 FileSystem Entry getMetadata() interface to get the
264 // actual last modified time of the media file. 263 // actual last modified time of the media file.
265 file_info.last_modified = base::Time(); 264 file_info.last_modified = base::Time();
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 const base::string16& registered_device_path, 340 const base::string16& registered_device_path,
342 const base::string16& storage_object_id) 341 const base::string16& storage_object_id)
343 : pnp_device_id(pnp_device_id), 342 : pnp_device_id(pnp_device_id),
344 registered_device_path(registered_device_path), 343 registered_device_path(registered_device_path),
345 storage_object_id(storage_object_id) { 344 storage_object_id(storage_object_id) {
346 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 345 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
347 } 346 }
348 347
349 MTPDeviceDelegateImplWin::PendingTaskInfo::PendingTaskInfo( 348 MTPDeviceDelegateImplWin::PendingTaskInfo::PendingTaskInfo(
350 const tracked_objects::Location& location, 349 const tracked_objects::Location& location,
351 const base::Callback<base::PlatformFileError(void)>& task, 350 const base::Callback<base::File::Error(void)>& task,
352 const base::Callback<void(base::PlatformFileError)>& reply) 351 const base::Callback<void(base::File::Error)>& reply)
353 : location(location), 352 : location(location),
354 task(task), 353 task(task),
355 reply(reply) { 354 reply(reply) {
356 } 355 }
357 356
358 MTPDeviceDelegateImplWin::MTPDeviceDelegateImplWin( 357 MTPDeviceDelegateImplWin::MTPDeviceDelegateImplWin(
359 const base::string16& registered_device_path, 358 const base::string16& registered_device_path,
360 const base::string16& pnp_device_id, 359 const base::string16& pnp_device_id,
361 const base::string16& storage_object_id) 360 const base::string16& storage_object_id)
362 : storage_device_info_(pnp_device_id, registered_device_path, 361 : storage_device_info_(pnp_device_id, registered_device_path,
(...skipping 12 matching lines...) Expand all
375 MTPDeviceDelegateImplWin::~MTPDeviceDelegateImplWin() { 374 MTPDeviceDelegateImplWin::~MTPDeviceDelegateImplWin() {
376 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 375 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
377 } 376 }
378 377
379 void MTPDeviceDelegateImplWin::GetFileInfo( 378 void MTPDeviceDelegateImplWin::GetFileInfo(
380 const base::FilePath& file_path, 379 const base::FilePath& file_path,
381 const GetFileInfoSuccessCallback& success_callback, 380 const GetFileInfoSuccessCallback& success_callback,
382 const ErrorCallback& error_callback) { 381 const ErrorCallback& error_callback) {
383 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 382 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
384 DCHECK(!file_path.empty()); 383 DCHECK(!file_path.empty());
385 base::PlatformFileInfo* file_info = new base::PlatformFileInfo; 384 base::File::Info* file_info = new base::File::Info;
386 EnsureInitAndRunTask( 385 EnsureInitAndRunTask(
387 PendingTaskInfo(FROM_HERE, 386 PendingTaskInfo(FROM_HERE,
388 base::Bind(&GetFileInfoOnBlockingPoolThread, 387 base::Bind(&GetFileInfoOnBlockingPoolThread,
389 storage_device_info_, 388 storage_device_info_,
390 file_path, 389 file_path,
391 base::Unretained(file_info)), 390 base::Unretained(file_info)),
392 base::Bind(&MTPDeviceDelegateImplWin::OnGetFileInfo, 391 base::Bind(&MTPDeviceDelegateImplWin::OnGetFileInfo,
393 weak_ptr_factory_.GetWeakPtr(), 392 weak_ptr_factory_.GetWeakPtr(),
394 success_callback, 393 success_callback,
395 error_callback, 394 error_callback,
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 void MTPDeviceDelegateImplWin::OnInitCompleted(bool succeeded) { 523 void MTPDeviceDelegateImplWin::OnInitCompleted(bool succeeded) {
525 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 524 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
526 init_state_ = succeeded ? INITIALIZED : UNINITIALIZED; 525 init_state_ = succeeded ? INITIALIZED : UNINITIALIZED;
527 task_in_progress_ = false; 526 task_in_progress_ = false;
528 ProcessNextPendingRequest(); 527 ProcessNextPendingRequest();
529 } 528 }
530 529
531 void MTPDeviceDelegateImplWin::OnGetFileInfo( 530 void MTPDeviceDelegateImplWin::OnGetFileInfo(
532 const GetFileInfoSuccessCallback& success_callback, 531 const GetFileInfoSuccessCallback& success_callback,
533 const ErrorCallback& error_callback, 532 const ErrorCallback& error_callback,
534 base::PlatformFileInfo* file_info, 533 base::File::Info* file_info,
535 base::PlatformFileError error) { 534 base::File::Error error) {
536 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 535 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
537 DCHECK(file_info); 536 DCHECK(file_info);
538 if (error == base::PLATFORM_FILE_OK) 537 if (error == base::File::FILE_OK)
539 success_callback.Run(*file_info); 538 success_callback.Run(*file_info);
540 else 539 else
541 error_callback.Run(error); 540 error_callback.Run(error);
542 task_in_progress_ = false; 541 task_in_progress_ = false;
543 ProcessNextPendingRequest(); 542 ProcessNextPendingRequest();
544 } 543 }
545 544
546 void MTPDeviceDelegateImplWin::OnDidReadDirectory( 545 void MTPDeviceDelegateImplWin::OnDidReadDirectory(
547 const ReadDirectorySuccessCallback& success_callback, 546 const ReadDirectorySuccessCallback& success_callback,
548 const ErrorCallback& error_callback, 547 const ErrorCallback& error_callback,
549 fileapi::AsyncFileUtil::EntryList* file_list, 548 fileapi::AsyncFileUtil::EntryList* file_list,
550 base::PlatformFileError error) { 549 base::File::Error error) {
551 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 550 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
552 DCHECK(file_list); 551 DCHECK(file_list);
553 if (error == base::PLATFORM_FILE_OK) 552 if (error == base::File::FILE_OK)
554 success_callback.Run(*file_list, false /*no more entries*/); 553 success_callback.Run(*file_list, false /*no more entries*/);
555 else 554 else
556 error_callback.Run(error); 555 error_callback.Run(error);
557 task_in_progress_ = false; 556 task_in_progress_ = false;
558 ProcessNextPendingRequest(); 557 ProcessNextPendingRequest();
559 } 558 }
560 559
561 void MTPDeviceDelegateImplWin::OnGetFileStream( 560 void MTPDeviceDelegateImplWin::OnGetFileStream(
562 scoped_ptr<SnapshotFileDetails> file_details, 561 scoped_ptr<SnapshotFileDetails> file_details,
563 base::PlatformFileError error) { 562 base::File::Error error) {
564 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 563 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
565 DCHECK(file_details); 564 DCHECK(file_details);
566 DCHECK(!file_details->request_info().device_file_path.empty()); 565 DCHECK(!file_details->request_info().device_file_path.empty());
567 DCHECK(!file_details->request_info().snapshot_file_path.empty()); 566 DCHECK(!file_details->request_info().snapshot_file_path.empty());
568 DCHECK(!current_snapshot_details_.get()); 567 DCHECK(!current_snapshot_details_.get());
569 if (error != base::PLATFORM_FILE_OK) { 568 if (error != base::File::FILE_OK) {
570 file_details->request_info().error_callback.Run(error); 569 file_details->request_info().error_callback.Run(error);
571 task_in_progress_ = false; 570 task_in_progress_ = false;
572 ProcessNextPendingRequest(); 571 ProcessNextPendingRequest();
573 return; 572 return;
574 } 573 }
575 DCHECK(file_details->file_info().size == 0 || 574 DCHECK(file_details->file_info().size == 0 ||
576 file_details->device_file_stream()); 575 file_details->device_file_stream());
577 current_snapshot_details_.reset(file_details.release()); 576 current_snapshot_details_.reset(file_details.release());
578 WriteDataChunkIntoSnapshotFile(); 577 WriteDataChunkIntoSnapshotFile();
579 } 578 }
(...skipping 28 matching lines...) Expand all
608 if (should_continue) { 607 if (should_continue) {
609 WriteDataChunkIntoSnapshotFile(); 608 WriteDataChunkIntoSnapshotFile();
610 return; 609 return;
611 } 610 }
612 if (succeeded) { 611 if (succeeded) {
613 current_snapshot_details_->request_info().success_callback.Run( 612 current_snapshot_details_->request_info().success_callback.Run(
614 current_snapshot_details_->file_info(), 613 current_snapshot_details_->file_info(),
615 current_snapshot_details_->request_info().snapshot_file_path); 614 current_snapshot_details_->request_info().snapshot_file_path);
616 } else { 615 } else {
617 current_snapshot_details_->request_info().error_callback.Run( 616 current_snapshot_details_->request_info().error_callback.Run(
618 base::PLATFORM_FILE_ERROR_FAILED); 617 base::File::FILE_ERROR_FAILED);
619 } 618 }
620 task_in_progress_ = false; 619 task_in_progress_ = false;
621 current_snapshot_details_.reset(); 620 current_snapshot_details_.reset();
622 ProcessNextPendingRequest(); 621 ProcessNextPendingRequest();
623 } 622 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698