Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/media_gallery/linux/read_mtp_file_worker.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 #include <sys/stat.h> | |
| 9 #include <sys/types.h> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/file_util.h" | |
| 13 #include "base/sequenced_task_runner.h" | |
| 14 #include "base/synchronization/waitable_event.h" | |
| 15 #include "chrome/browser/media_transfer_protocol/media_transfer_protocol_manager .h" | |
| 16 #include "chrome/browser/media_transfer_protocol/mtp_file_entry.pb.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 19 | |
| 20 using content::BrowserThread; | |
| 21 | |
| 22 namespace chrome { | |
| 23 | |
| 24 ReadMTPFileWorker::ReadMTPFileWorker(const std::string& handle, | |
| 25 const std::string& src_path, | |
| 26 uint32 total_size, | |
| 27 const FilePath& dest_path, | |
| 28 base::SequencedTaskRunner* task_runner, | |
| 29 base::WaitableEvent* task_completed_event, | |
| 30 base::WaitableEvent* shutdown_event) | |
| 31 : device_handle_(handle), | |
| 32 src_path_(src_path), | |
| 33 total_bytes_(total_size), | |
| 34 dest_path_(dest_path), | |
| 35 bytes_read_(0), | |
| 36 error_occurred_(false), | |
| 37 media_task_runner_(task_runner), | |
| 38 on_task_completed_event_(task_completed_event), | |
| 39 on_shutdown_event_(shutdown_event) { | |
| 40 DCHECK(on_task_completed_event_); | |
| 41 DCHECK(on_shutdown_event_); | |
| 42 } | |
| 43 | |
| 44 void ReadMTPFileWorker::Run() { | |
| 45 if (on_shutdown_event_->IsSignaled()) { | |
| 46 // Process is in shutdown mode. | |
| 47 // Do not post any task on |media_task_runner_|. | |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 int dest_fd = open(dest_path_.value().c_str(), O_WRONLY); | |
| 52 if (dest_fd < 0) | |
| 53 return; | |
| 54 file_util::ScopedFD dest_fd_scoper(&dest_fd); | |
| 55 | |
| 56 while (bytes_read_ < total_bytes_) { | |
| 57 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 58 base::Bind(&ReadMTPFileWorker::DoWorkOnUIThread, | |
| 59 this)); | |
| 60 on_task_completed_event_->Wait(); | |
| 61 if (error_occurred_) | |
| 62 break; | |
| 63 if (on_shutdown_event_->IsSignaled()) { | |
| 64 cancel_tasks_flag_.Set(); | |
| 65 break; | |
| 66 } | |
| 67 | |
| 68 int bytes_written = | |
| 69 file_util::WriteFileDescriptor(dest_fd, data_.data(), data_.size()); | |
| 70 if (static_cast<int>(data_.size()) != bytes_written) | |
| 71 break; | |
| 72 | |
| 73 bytes_read_ += data_.size(); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 bool ReadMTPFileWorker::Succeeded() const { | |
| 78 return (!error_occurred_ && (bytes_read_ == total_bytes_)); | |
| 79 } | |
| 80 | |
| 81 ReadMTPFileWorker::~ReadMTPFileWorker() { | |
| 82 // This object must be destructed on |media_task_runner_|. | |
| 83 } | |
| 84 | |
| 85 void ReadMTPFileWorker::DoWorkOnUIThread() { | |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 87 if (cancel_tasks_flag_.IsSet()) | |
| 88 return; | |
| 89 | |
| 90 GetMediaTransferProtocolManager()->ReadFileChunkByPath( | |
| 91 device_handle_, src_path_, bytes_read_, BytesToRead(), | |
| 92 base::Bind(&ReadMTPFileWorker::OnDidWorkOnUIThread, this)); | |
| 93 } | |
| 94 | |
| 95 void ReadMTPFileWorker::OnDidWorkOnUIThread(const std::string& data, | |
| 96 bool error) { | |
| 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 98 if (cancel_tasks_flag_.IsSet()) | |
| 99 return; | |
| 100 error_occurred_ = (error || (data.size() != BytesToRead())); | |
|
Lei Zhang
2012/12/03 22:23:54
nit: don't go overboard with parenthesis.
| |
| 101 if (!error_occurred_) | |
| 102 data_ = data; | |
| 103 on_task_completed_event_->Signal(); | |
| 104 } | |
| 105 | |
| 106 uint32 ReadMTPFileWorker::BytesToRead() const { | |
| 107 // Read data in 1 MB chunks. | |
| 108 static const uint32 kReadChunkSize = 1024 * 1024; | |
| 109 return std::min(kReadChunkSize, total_bytes_ - bytes_read_); | |
| 110 } | |
| 111 | |
| 112 } // namespace chrome | |
| OLD | NEW |