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 // This class opens the media transfer protocol (MTP) storage device for |
| 6 // communication. This class is created, destroyed and operated on the blocking |
| 7 // thread. It may take a while to open the device for communication. |
| 8 |
| 9 #ifndef CHROME_BROWSER_MEDIA_GALLERY_LINUX_MTP_OPEN_STORAGE_WORKER_H_ |
| 10 #define CHROME_BROWSER_MEDIA_GALLERY_LINUX_MTP_OPEN_STORAGE_WORKER_H_ |
| 11 |
| 12 #include <string> |
| 13 |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/sequenced_task_runner_helpers.h" |
| 16 #include "base/synchronization/cancellation_flag.h" |
| 17 #include "chrome/browser/media_gallery/linux/mtp_device_operations_utils.h" |
| 18 |
| 19 namespace base { |
| 20 class SequencedTaskRunner; |
| 21 class WaitableEvent; |
| 22 } |
| 23 |
| 24 namespace chrome { |
| 25 |
| 26 typedef struct WorkerDeleter<class MTPOpenStorageWorker> |
| 27 MTPOpenStorageWorkerDeleter; |
| 28 |
| 29 // Worker class to open a MTP device for communication. This class is |
| 30 // instantiated and destructed on |media_task_runner_|. In order to post a |
| 31 // request on Dbus thread, the caller should run on UI thread. Therefore, this |
| 32 // class posts the open device request on UI thread and receives the response |
| 33 // on UI thread. |
| 34 class MTPOpenStorageWorker |
| 35 : public base::RefCountedThreadSafe<MTPOpenStorageWorker, |
| 36 MTPOpenStorageWorkerDeleter> { |
| 37 public: |
| 38 // Constructed on |media_task_runner_| thread. |
| 39 MTPOpenStorageWorker(const std::string& name, |
| 40 base::SequencedTaskRunner* task_runner, |
| 41 base::WaitableEvent* task_completed_event, |
| 42 base::WaitableEvent* shutdown_event); |
| 43 |
| 44 // This function is invoked on |media_task_runner_| to post the task on UI |
| 45 // thread. This blocks the |media_task_runner_| until the task is complete. |
| 46 void Run(); |
| 47 |
| 48 // Returns a device handle string if the OpenStorage() request was |
| 49 // successfully completed or an empty string otherwise. |
| 50 const std::string& device_handle() const { return device_handle_; } |
| 51 |
| 52 // Returns the |media_task_runner_| associated with this worker object. |
| 53 // This function is exposed for WorkerDeleter struct to access the |
| 54 // |media_task_runner_|. |
| 55 base::SequencedTaskRunner* media_task_runner() const { |
| 56 return media_task_runner_.get(); |
| 57 } |
| 58 |
| 59 private: |
| 60 friend struct WorkerDeleter<MTPOpenStorageWorker>; |
| 61 friend class base::DeleteHelper<MTPOpenStorageWorker>; |
| 62 friend class base::RefCountedThreadSafe<MTPOpenStorageWorker, |
| 63 MTPOpenStorageWorkerDeleter>; |
| 64 |
| 65 // Destructed via MTPOpenStorageWorkerDeleter struct. |
| 66 virtual ~MTPOpenStorageWorker(); |
| 67 |
| 68 // Dispatches a request to MediaTransferProtocolManager to open the MTP |
| 69 // storage for communication. This is called on UI thread. |
| 70 void DoWorkOnUIThread(); |
| 71 |
| 72 // Query callback for DoWorkOnUIThread(). |error| is set to true if the device |
| 73 // did not open successfully. This function signals to unblock |
| 74 // |media_task_runner_|. |
| 75 void OnDidWorkOnUIThread(const std::string& device_handle, bool error); |
| 76 |
| 77 // The MTP device storage name. |
| 78 const std::string storage_name_; |
| 79 |
| 80 // Stores a reference to |media_task_runner_| to destruct this object on the |
| 81 // correct thread. |
| 82 scoped_refptr<base::SequencedTaskRunner> media_task_runner_; |
| 83 |
| 84 // |media_task_runner_| can wait on this event until the required operation |
| 85 // is complete. |
| 86 // TODO(kmadhusu): Remove this WaitableEvent after modifying the |
| 87 // DeviceMediaFileUtil functions as asynchronous functions. |
| 88 base::WaitableEvent* on_task_completed_event_; |
| 89 |
| 90 // Stores a reference to waitable event associated with the shut down message. |
| 91 base::WaitableEvent* on_shutdown_event_; |
| 92 |
| 93 // Stores the result of OpenStorage() request. |
| 94 std::string device_handle_; |
| 95 |
| 96 // Set to ignore the request results. This will be set when |
| 97 // MTPDeviceDelegateImplLinux object is about to be deleted. |
| 98 // |on_task_completed_event_| and |on_shutdown_event_| should not be |
| 99 // dereferenced when this is set. |
| 100 base::CancellationFlag cancel_tasks_flag_; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(MTPOpenStorageWorker); |
| 103 }; |
| 104 |
| 105 } // namespace chrome |
| 106 |
| 107 #endif // CHROME_BROWSER_MEDIA_GALLERY_LINUX_MTP_OPEN_STORAGE_WORKER_H_ |
OLD | NEW |