OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_delegate_impl_linux.h" | 5 #include "chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h" | 10 #include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 // | 78 // |
79 // |storage_name| specifies the name of the storage device. | 79 // |storage_name| specifies the name of the storage device. |
80 // |success_callback| is called when the ReadDirectory request succeeds. | 80 // |success_callback| is called when the ReadDirectory request succeeds. |
81 // |error_callback| is called when the ReadDirectory request fails. | 81 // |error_callback| is called when the ReadDirectory request fails. |
82 // |success_callback| and |error_callback| runs on the IO thread. | 82 // |success_callback| and |error_callback| runs on the IO thread. |
83 void ReadDirectoryOnUIThread( | 83 void ReadDirectoryOnUIThread( |
84 const std::string& storage_name, | 84 const std::string& storage_name, |
85 const std::string& root, | 85 const std::string& root, |
86 const base::Callback< | 86 const base::Callback< |
87 void(const fileapi::AsyncFileUtil::EntryList&)>& success_callback, | 87 void(const fileapi::AsyncFileUtil::EntryList&)>& success_callback, |
88 const base::Callback<void(base::PlatformFileError)>& error_callback) { | 88 const base::Callback<void(base::File::Error)>& error_callback) { |
89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
90 MTPDeviceTaskHelper* task_helper = | 90 MTPDeviceTaskHelper* task_helper = |
91 GetDeviceTaskHelperForStorage(storage_name); | 91 GetDeviceTaskHelperForStorage(storage_name); |
92 if (!task_helper) | 92 if (!task_helper) |
93 return; | 93 return; |
94 task_helper->ReadDirectoryByPath(root, success_callback, error_callback); | 94 task_helper->ReadDirectoryByPath(root, success_callback, error_callback); |
95 } | 95 } |
96 | 96 |
97 // Gets the |file_path| details. | 97 // Gets the |file_path| details. |
98 // | 98 // |
99 // Called on the UI thread to dispatch the request to the | 99 // Called on the UI thread to dispatch the request to the |
100 // MediaTransferProtocolManager. | 100 // MediaTransferProtocolManager. |
101 // | 101 // |
102 // |storage_name| specifies the name of the storage device. | 102 // |storage_name| specifies the name of the storage device. |
103 // |success_callback| is called when the GetFileInfo request succeeds. | 103 // |success_callback| is called when the GetFileInfo request succeeds. |
104 // |error_callback| is called when the GetFileInfo request fails. | 104 // |error_callback| is called when the GetFileInfo request fails. |
105 // |success_callback| and |error_callback| runs on the IO thread. | 105 // |success_callback| and |error_callback| runs on the IO thread. |
106 void GetFileInfoOnUIThread( | 106 void GetFileInfoOnUIThread( |
107 const std::string& storage_name, | 107 const std::string& storage_name, |
108 const std::string& file_path, | 108 const std::string& file_path, |
109 const base::Callback<void(const base::PlatformFileInfo&)>& success_callback, | 109 const base::Callback<void(const base::File::Info&)>& success_callback, |
110 const base::Callback<void(base::PlatformFileError)>& error_callback) { | 110 const base::Callback<void(base::File::Error)>& error_callback) { |
111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
112 MTPDeviceTaskHelper* task_helper = | 112 MTPDeviceTaskHelper* task_helper = |
113 GetDeviceTaskHelperForStorage(storage_name); | 113 GetDeviceTaskHelperForStorage(storage_name); |
114 if (!task_helper) | 114 if (!task_helper) |
115 return; | 115 return; |
116 task_helper->GetFileInfoByPath(file_path, success_callback, error_callback); | 116 task_helper->GetFileInfoByPath(file_path, success_callback, error_callback); |
117 } | 117 } |
118 | 118 |
119 // Copies the contents of |device_file_path| to |snapshot_file_path|. | 119 // Copies the contents of |device_file_path| to |snapshot_file_path|. |
120 // | 120 // |
121 // Called on the UI thread to dispatch the request to the | 121 // Called on the UI thread to dispatch the request to the |
122 // MediaTransferProtocolManager. | 122 // MediaTransferProtocolManager. |
123 // | 123 // |
124 // |storage_name| specifies the name of the storage device. | 124 // |storage_name| specifies the name of the storage device. |
125 // |device_file_path| specifies the media device file path. | 125 // |device_file_path| specifies the media device file path. |
126 // |snapshot_file_path| specifies the platform path of the snapshot file. | 126 // |snapshot_file_path| specifies the platform path of the snapshot file. |
127 // |file_size| specifies the number of bytes that will be written to the | 127 // |file_size| specifies the number of bytes that will be written to the |
128 // snapshot file. | 128 // snapshot file. |
129 // |success_callback| is called when the copy operation succeeds. | 129 // |success_callback| is called when the copy operation succeeds. |
130 // |error_callback| is called when the copy operation fails. | 130 // |error_callback| is called when the copy operation fails. |
131 // |success_callback| and |error_callback| runs on the IO thread. | 131 // |success_callback| and |error_callback| runs on the IO thread. |
132 void WriteDataIntoSnapshotFileOnUIThread( | 132 void WriteDataIntoSnapshotFileOnUIThread( |
133 const std::string& storage_name, | 133 const std::string& storage_name, |
134 const SnapshotRequestInfo& request_info, | 134 const SnapshotRequestInfo& request_info, |
135 const base::PlatformFileInfo& snapshot_file_info) { | 135 const base::File::Info& snapshot_file_info) { |
136 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 136 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
137 MTPDeviceTaskHelper* task_helper = | 137 MTPDeviceTaskHelper* task_helper = |
138 GetDeviceTaskHelperForStorage(storage_name); | 138 GetDeviceTaskHelperForStorage(storage_name); |
139 if (!task_helper) | 139 if (!task_helper) |
140 return; | 140 return; |
141 task_helper->WriteDataIntoSnapshotFile(request_info, snapshot_file_info); | 141 task_helper->WriteDataIntoSnapshotFile(request_info, snapshot_file_info); |
142 } | 142 } |
143 | 143 |
144 // Closes the device storage specified by the |storage_name| and destroys the | 144 // Closes the device storage specified by the |storage_name| and destroys the |
145 // MTPDeviceTaskHelper object associated with the device storage. | 145 // MTPDeviceTaskHelper object associated with the device storage. |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 content::BrowserThread::UI, | 288 content::BrowserThread::UI, |
289 FROM_HERE, | 289 FROM_HERE, |
290 base::Bind(&OpenStorageOnUIThread, | 290 base::Bind(&OpenStorageOnUIThread, |
291 storage_name_, | 291 storage_name_, |
292 base::Bind(&MTPDeviceDelegateImplLinux::OnInitCompleted, | 292 base::Bind(&MTPDeviceDelegateImplLinux::OnInitCompleted, |
293 weak_ptr_factory_.GetWeakPtr()))); | 293 weak_ptr_factory_.GetWeakPtr()))); |
294 } | 294 } |
295 } | 295 } |
296 | 296 |
297 void MTPDeviceDelegateImplLinux::WriteDataIntoSnapshotFile( | 297 void MTPDeviceDelegateImplLinux::WriteDataIntoSnapshotFile( |
298 const base::PlatformFileInfo& file_info) { | 298 const base::File::Info& file_info) { |
299 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 299 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
300 DCHECK(current_snapshot_request_info_.get()); | 300 DCHECK(current_snapshot_request_info_.get()); |
301 DCHECK_GT(file_info.size, 0); | 301 DCHECK_GT(file_info.size, 0); |
302 task_in_progress_ = true; | 302 task_in_progress_ = true; |
303 SnapshotRequestInfo request_info( | 303 SnapshotRequestInfo request_info( |
304 current_snapshot_request_info_->device_file_path, | 304 current_snapshot_request_info_->device_file_path, |
305 current_snapshot_request_info_->snapshot_file_path, | 305 current_snapshot_request_info_->snapshot_file_path, |
306 base::Bind( | 306 base::Bind( |
307 &MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile, | 307 &MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile, |
308 weak_ptr_factory_.GetWeakPtr()), | 308 weak_ptr_factory_.GetWeakPtr()), |
(...skipping 26 matching lines...) Expand all Loading... |
335 | 335 |
336 void MTPDeviceDelegateImplLinux::OnInitCompleted(bool succeeded) { | 336 void MTPDeviceDelegateImplLinux::OnInitCompleted(bool succeeded) { |
337 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 337 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
338 init_state_ = succeeded ? INITIALIZED : UNINITIALIZED; | 338 init_state_ = succeeded ? INITIALIZED : UNINITIALIZED; |
339 task_in_progress_ = false; | 339 task_in_progress_ = false; |
340 ProcessNextPendingRequest(); | 340 ProcessNextPendingRequest(); |
341 } | 341 } |
342 | 342 |
343 void MTPDeviceDelegateImplLinux::OnDidGetFileInfo( | 343 void MTPDeviceDelegateImplLinux::OnDidGetFileInfo( |
344 const GetFileInfoSuccessCallback& success_callback, | 344 const GetFileInfoSuccessCallback& success_callback, |
345 const base::PlatformFileInfo& file_info) { | 345 const base::File::Info& file_info) { |
346 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 346 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
347 success_callback.Run(file_info); | 347 success_callback.Run(file_info); |
348 task_in_progress_ = false; | 348 task_in_progress_ = false; |
349 ProcessNextPendingRequest(); | 349 ProcessNextPendingRequest(); |
350 } | 350 } |
351 | 351 |
352 void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadDirectory( | 352 void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadDirectory( |
353 const std::string& root, | 353 const std::string& root, |
354 const ReadDirectorySuccessCallback& success_callback, | 354 const ReadDirectorySuccessCallback& success_callback, |
355 const ErrorCallback& error_callback, | 355 const ErrorCallback& error_callback, |
356 const base::PlatformFileInfo& file_info) { | 356 const base::File::Info& file_info) { |
357 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 357 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
358 DCHECK(task_in_progress_); | 358 DCHECK(task_in_progress_); |
359 if (!file_info.is_directory) { | 359 if (!file_info.is_directory) { |
360 return HandleDeviceFileError(error_callback, | 360 return HandleDeviceFileError(error_callback, |
361 base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY); | 361 base::File::FILE_ERROR_NOT_A_DIRECTORY); |
362 } | 362 } |
363 | 363 |
364 base::Closure task_closure = | 364 base::Closure task_closure = |
365 base::Bind(&ReadDirectoryOnUIThread, | 365 base::Bind(&ReadDirectoryOnUIThread, |
366 storage_name_, | 366 storage_name_, |
367 root, | 367 root, |
368 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadDirectory, | 368 base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadDirectory, |
369 weak_ptr_factory_.GetWeakPtr(), | 369 weak_ptr_factory_.GetWeakPtr(), |
370 success_callback), | 370 success_callback), |
371 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, | 371 base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError, |
372 weak_ptr_factory_.GetWeakPtr(), | 372 weak_ptr_factory_.GetWeakPtr(), |
373 error_callback)); | 373 error_callback)); |
374 content::BrowserThread::PostTask(content::BrowserThread::UI, | 374 content::BrowserThread::PostTask(content::BrowserThread::UI, |
375 FROM_HERE, | 375 FROM_HERE, |
376 task_closure); | 376 task_closure); |
377 } | 377 } |
378 | 378 |
379 void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile( | 379 void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile( |
380 scoped_ptr<SnapshotRequestInfo> snapshot_request_info, | 380 scoped_ptr<SnapshotRequestInfo> snapshot_request_info, |
381 const base::PlatformFileInfo& file_info) { | 381 const base::File::Info& file_info) { |
382 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 382 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
383 DCHECK(!current_snapshot_request_info_.get()); | 383 DCHECK(!current_snapshot_request_info_.get()); |
384 DCHECK(snapshot_request_info.get()); | 384 DCHECK(snapshot_request_info.get()); |
385 DCHECK(task_in_progress_); | 385 DCHECK(task_in_progress_); |
386 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 386 base::File::Error error = base::File::FILE_OK; |
387 if (file_info.is_directory) | 387 if (file_info.is_directory) |
388 error = base::PLATFORM_FILE_ERROR_NOT_A_FILE; | 388 error = base::File::FILE_ERROR_NOT_A_FILE; |
389 else if (file_info.size < 0 || file_info.size > kuint32max) | 389 else if (file_info.size < 0 || file_info.size > kuint32max) |
390 error = base::PLATFORM_FILE_ERROR_FAILED; | 390 error = base::File::FILE_ERROR_FAILED; |
391 | 391 |
392 if (error != base::PLATFORM_FILE_OK) | 392 if (error != base::File::FILE_OK) |
393 return HandleDeviceFileError(snapshot_request_info->error_callback, error); | 393 return HandleDeviceFileError(snapshot_request_info->error_callback, error); |
394 | 394 |
395 base::PlatformFileInfo snapshot_file_info(file_info); | 395 base::File::Info snapshot_file_info(file_info); |
396 // Modify the last modified time to null. This prevents the time stamp | 396 // Modify the last modified time to null. This prevents the time stamp |
397 // verfication in LocalFileStreamReader. | 397 // verfication in LocalFileStreamReader. |
398 snapshot_file_info.last_modified = base::Time(); | 398 snapshot_file_info.last_modified = base::Time(); |
399 | 399 |
400 current_snapshot_request_info_.reset(snapshot_request_info.release()); | 400 current_snapshot_request_info_.reset(snapshot_request_info.release()); |
401 if (file_info.size == 0) { | 401 if (file_info.size == 0) { |
402 // Empty snapshot file. | 402 // Empty snapshot file. |
403 return OnDidWriteDataIntoSnapshotFile( | 403 return OnDidWriteDataIntoSnapshotFile( |
404 snapshot_file_info, current_snapshot_request_info_->snapshot_file_path); | 404 snapshot_file_info, current_snapshot_request_info_->snapshot_file_path); |
405 } | 405 } |
406 WriteDataIntoSnapshotFile(snapshot_file_info); | 406 WriteDataIntoSnapshotFile(snapshot_file_info); |
407 } | 407 } |
408 | 408 |
409 void MTPDeviceDelegateImplLinux::OnDidReadDirectory( | 409 void MTPDeviceDelegateImplLinux::OnDidReadDirectory( |
410 const ReadDirectorySuccessCallback& success_callback, | 410 const ReadDirectorySuccessCallback& success_callback, |
411 const fileapi::AsyncFileUtil::EntryList& file_list) { | 411 const fileapi::AsyncFileUtil::EntryList& file_list) { |
412 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 412 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
413 success_callback.Run(file_list, false /*no more entries*/); | 413 success_callback.Run(file_list, false /*no more entries*/); |
414 task_in_progress_ = false; | 414 task_in_progress_ = false; |
415 ProcessNextPendingRequest(); | 415 ProcessNextPendingRequest(); |
416 } | 416 } |
417 | 417 |
418 void MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile( | 418 void MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile( |
419 const base::PlatformFileInfo& file_info, | 419 const base::File::Info& file_info, |
420 const base::FilePath& snapshot_file_path) { | 420 const base::FilePath& snapshot_file_path) { |
421 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 421 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
422 DCHECK(current_snapshot_request_info_.get()); | 422 DCHECK(current_snapshot_request_info_.get()); |
423 DCHECK(task_in_progress_); | 423 DCHECK(task_in_progress_); |
424 current_snapshot_request_info_->success_callback.Run( | 424 current_snapshot_request_info_->success_callback.Run( |
425 file_info, snapshot_file_path); | 425 file_info, snapshot_file_path); |
426 task_in_progress_ = false; | 426 task_in_progress_ = false; |
427 current_snapshot_request_info_.reset(); | 427 current_snapshot_request_info_.reset(); |
428 ProcessNextPendingRequest(); | 428 ProcessNextPendingRequest(); |
429 } | 429 } |
430 | 430 |
431 void MTPDeviceDelegateImplLinux::OnWriteDataIntoSnapshotFileError( | 431 void MTPDeviceDelegateImplLinux::OnWriteDataIntoSnapshotFileError( |
432 base::PlatformFileError error) { | 432 base::File::Error error) { |
433 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 433 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
434 DCHECK(current_snapshot_request_info_.get()); | 434 DCHECK(current_snapshot_request_info_.get()); |
435 DCHECK(task_in_progress_); | 435 DCHECK(task_in_progress_); |
436 current_snapshot_request_info_->error_callback.Run(error); | 436 current_snapshot_request_info_->error_callback.Run(error); |
437 task_in_progress_ = false; | 437 task_in_progress_ = false; |
438 current_snapshot_request_info_.reset(); | 438 current_snapshot_request_info_.reset(); |
439 ProcessNextPendingRequest(); | 439 ProcessNextPendingRequest(); |
440 } | 440 } |
441 | 441 |
442 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( | 442 void MTPDeviceDelegateImplLinux::HandleDeviceFileError( |
443 const ErrorCallback& error_callback, | 443 const ErrorCallback& error_callback, |
444 base::PlatformFileError error) { | 444 base::File::Error error) { |
445 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 445 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
446 error_callback.Run(error); | 446 error_callback.Run(error); |
447 task_in_progress_ = false; | 447 task_in_progress_ = false; |
448 ProcessNextPendingRequest(); | 448 ProcessNextPendingRequest(); |
449 } | 449 } |
450 | 450 |
451 void CreateMTPDeviceAsyncDelegate( | 451 void CreateMTPDeviceAsyncDelegate( |
452 const std::string& device_location, | 452 const std::string& device_location, |
453 const CreateMTPDeviceAsyncDelegateCallback& callback) { | 453 const CreateMTPDeviceAsyncDelegateCallback& callback) { |
454 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 454 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
455 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); | 455 callback.Run(new MTPDeviceDelegateImplLinux(device_location)); |
456 } | 456 } |
OLD | NEW |