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 #ifndef CHROME_BROWSER_MEDIA_GALLERY_LINUX_READ_MTP_FILE_WORKER_H_ |
| 6 #define CHROME_BROWSER_MEDIA_GALLERY_LINUX_READ_MTP_FILE_WORKER_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/file_path.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/sequenced_task_runner_helpers.h" |
| 13 #include "base/synchronization/cancellation_flag.h" |
| 14 #include "chrome/browser/media_gallery/linux/mtp_device_operations_utils.h" |
| 15 |
| 16 namespace base { |
| 17 class SequencedTaskRunner; |
| 18 class WaitableEvent; |
| 19 } |
| 20 |
| 21 namespace chrome { |
| 22 |
| 23 typedef struct WorkerDeleter<class ReadMTPFileWorker> ReadMTPFileWorkerDeleter; |
| 24 |
| 25 // Worker class to read media device file data given a file |path|. |
| 26 class ReadMTPFileWorker |
| 27 : public base::RefCountedThreadSafe<ReadMTPFileWorker, |
| 28 ReadMTPFileWorkerDeleter> { |
| 29 public: |
| 30 // Constructed on |media_task_runner_| thread. |
| 31 ReadMTPFileWorker(const std::string& handle, |
| 32 const std::string& src_path, |
| 33 uint32 total_size, |
| 34 const FilePath& dest_path, |
| 35 base::SequencedTaskRunner* task_runner, |
| 36 base::WaitableEvent* task_completed_event, |
| 37 base::WaitableEvent* shutdown_event); |
| 38 |
| 39 // This function is invoked on |media_task_runner_| to post the task on UI |
| 40 // thread. This blocks the |media_task_runner_| until the task is complete. |
| 41 void Run(); |
| 42 |
| 43 bool Succeeded() const; |
| 44 |
| 45 // Returns the |media_task_runner_| associated with this worker object. |
| 46 // This function is exposed for WorkerDeleter struct to access the |
| 47 // |media_task_runner_|. |
| 48 base::SequencedTaskRunner* media_task_runner() const { |
| 49 return media_task_runner_.get(); |
| 50 } |
| 51 |
| 52 private: |
| 53 friend struct WorkerDeleter<ReadMTPFileWorker>; |
| 54 friend class base::DeleteHelper<ReadMTPFileWorker>; |
| 55 friend class base::RefCountedThreadSafe<ReadMTPFileWorker, |
| 56 ReadMTPFileWorkerDeleter>; |
| 57 |
| 58 // Destructed via ReadMTPFileWorkerDeleter. |
| 59 virtual ~ReadMTPFileWorker(); |
| 60 |
| 61 // Dispatches a request to MediaTransferProtocolManager to get the media file |
| 62 // contents. |
| 63 void DoWorkOnUIThread(); |
| 64 |
| 65 // Query callback for DoWorkOnUIThread(). On success, |data| has the media |
| 66 // file contents. On failure, |error| is set to true. This function signals |
| 67 // to unblock |media_task_runner_|. |
| 68 void OnDidWorkOnUIThread(const std::string& data, bool error); |
| 69 |
| 70 uint32 BytesToRead() const; |
| 71 |
| 72 // The device unique identifier to query the device. |
| 73 const std::string device_handle_; |
| 74 |
| 75 // The media device file path. |
| 76 const std::string src_path_; |
| 77 |
| 78 // Number of bytes to read. |
| 79 const uint32 total_bytes_; |
| 80 |
| 81 // Where to write the data read from the device. |
| 82 const FilePath dest_path_; |
| 83 |
| 84 /**************************************************************************** |
| 85 * The variables below are accessed on both |media_task_runner_| and the UI |
| 86 * thread. However, there's no concurrent access because the UI thread is in a |
| 87 * blocked state when access occurs on |media_task_runner_|. |
| 88 */ |
| 89 |
| 90 // Number of bytes read from the device. |
| 91 uint32 bytes_read_; |
| 92 |
| 93 // Temporary data storage. |
| 94 std::string data_; |
| 95 |
| 96 // Whether an error occurred during file transfer. |
| 97 bool error_occurred_; |
| 98 |
| 99 /***************************************************************************/ |
| 100 |
| 101 // A reference to |media_task_runner_| to destruct this object on the correct |
| 102 // thread. |
| 103 scoped_refptr<base::SequencedTaskRunner> media_task_runner_; |
| 104 |
| 105 // |media_task_runner_| can wait on this event until the required operation |
| 106 // is complete. |
| 107 // TODO(kmadhusu): Remove this WaitableEvent after modifying the |
| 108 // DeviceMediaFileUtil functions as asynchronous functions. |
| 109 base::WaitableEvent* on_task_completed_event_; |
| 110 |
| 111 // Stores a reference to waitable event associated with the shut down message. |
| 112 base::WaitableEvent* on_shutdown_event_; |
| 113 |
| 114 // Set to ignore the request results. This will be set when |
| 115 // MTPDeviceDelegateImplLinux object is about to be deleted. |
| 116 // |on_task_completed_event_| and |on_shutdown_event_| should not be |
| 117 // dereferenced when this is set. |
| 118 base::CancellationFlag cancel_tasks_flag_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(ReadMTPFileWorker); |
| 121 }; |
| 122 |
| 123 } // namespace chrome |
| 124 |
| 125 #endif // CHROME_BROWSER_MEDIA_GALLERY_LINUX_READ_MTP_FILE_WORKER_H_ |
OLD | NEW |