Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / media_galleries / win / mtp_device_delegate_impl_win.h
blob5a8282bb1eb9eee5e6e4ec16f3f79c208a26910d
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_WIN_MTP_DEVICE_DELEGATE_IMPL_WIN_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_WIN_MTP_DEVICE_DELEGATE_IMPL_WIN_H_
8 #include <queue>
10 #include "base/callback.h"
11 #include "base/files/file.h"
12 #include "base/location.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/win/scoped_comptr.h"
17 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
18 #include "storage/browser/fileapi/async_file_util.h"
20 namespace base {
21 class FilePath;
22 class SequencedTaskRunner;
25 class SnapshotFileDetails;
26 struct SnapshotRequestInfo;
28 // MTPDeviceDelegateImplWin is used to communicate with the media transfer
29 // protocol (MTP) device to complete file system operations. These operations
30 // are performed asynchronously on a blocking pool thread since the device
31 // access may be slow and may take a long time to complete. MTP
32 // device can have multiple data storage partitions. MTPDeviceDelegateImplWin
33 // is instantiated per MTP device storage partition using
34 // CreateMTPDeviceAsyncDelegate(). MTPDeviceDelegateImplWin lives on the IO
35 // thread.
36 class MTPDeviceDelegateImplWin : public MTPDeviceAsyncDelegate {
37 public:
38 // Structure used to represent MTP device storage partition details.
39 struct StorageDeviceInfo {
40 StorageDeviceInfo(const base::string16& pnp_device_id,
41 const base::string16& registered_device_path,
42 const base::string16& storage_object_id);
44 // The PnP Device Id, used to open the device for communication,
45 // e.g. "\\?\usb#vid_04a9&pid_3073#12#{6ac27878-a6fa-4155-ba85-f1d4f33}".
46 const base::string16 pnp_device_id;
48 // The media file system root path, which is obtained during the
49 // registration of MTP device storage partition as a file system,
50 // e.g. "\\MTP:StorageSerial:SID-{10001,E,9823}:237483".
51 const base::string16 registered_device_path;
53 // The MTP device storage partition object identifier, used to enumerate the
54 // storage contents, e.g. "s10001".
55 const base::string16 storage_object_id;
58 private:
59 friend void OnGetStorageInfoCreateDelegate(
60 const base::string16& device_location,
61 const CreateMTPDeviceAsyncDelegateCallback& callback,
62 base::string16* pnp_device_id,
63 base::string16* storage_object_id,
64 bool succeeded);
66 enum InitializationState {
67 UNINITIALIZED = 0,
68 PENDING_INIT,
69 INITIALIZED
72 // Used to represent pending task details.
73 struct PendingTaskInfo {
74 PendingTaskInfo(const tracked_objects::Location& location,
75 const base::Callback<base::File::Error(void)>& task,
76 const base::Callback<void(base::File::Error)>& reply);
77 ~PendingTaskInfo();
79 const tracked_objects::Location location;
80 const base::Callback<base::File::Error(void)> task;
81 const base::Callback<void(base::File::Error)> reply;
84 // Defers the device initializations until the first file operation request.
85 // Do all the initializations in EnsureInitAndRunTask() function.
86 MTPDeviceDelegateImplWin(const base::string16& registered_device_path,
87 const base::string16& pnp_device_id,
88 const base::string16& storage_object_id);
90 // Destructed via CancelPendingTasksAndDeleteDelegate().
91 ~MTPDeviceDelegateImplWin() override;
93 // MTPDeviceAsyncDelegate:
94 void GetFileInfo(const base::FilePath& file_path,
95 const GetFileInfoSuccessCallback& success_callback,
96 const ErrorCallback& error_callback) override;
97 void CreateDirectory(
98 const base::FilePath& directory_path,
99 const bool exclusive,
100 const bool recursive,
101 const CreateDirectorySuccessCallback& success_callback,
102 const ErrorCallback& error_callback) override;
103 void ReadDirectory(
104 const base::FilePath& root,
105 const ReadDirectorySuccessCallback& success_callback,
106 const ErrorCallback& error_callback) override;
107 void CreateSnapshotFile(
108 const base::FilePath& device_file_path,
109 const base::FilePath& local_path,
110 const CreateSnapshotFileSuccessCallback& success_callback,
111 const ErrorCallback& error_callback) override;
112 bool IsStreaming() override;
113 void ReadBytes(const base::FilePath& device_file_path,
114 const scoped_refptr<net::IOBuffer>& buf,
115 int64 offset,
116 int buf_len,
117 const ReadBytesSuccessCallback& success_callback,
118 const ErrorCallback& error_callback) override;
119 bool IsReadOnly() const override;
120 void CopyFileLocal(
121 const base::FilePath& source_file_path,
122 const base::FilePath& device_file_path,
123 const CreateTemporaryFileCallback& create_temporary_file_callback,
124 const CopyFileProgressCallback& progress_callback,
125 const CopyFileLocalSuccessCallback& success_callback,
126 const ErrorCallback& error_callback) override;
127 void MoveFileLocal(
128 const base::FilePath& source_file_path,
129 const base::FilePath& device_file_path,
130 const CreateTemporaryFileCallback& create_temporary_file_callback,
131 const MoveFileLocalSuccessCallback& success_callback,
132 const ErrorCallback& error_callback) override;
133 void CopyFileFromLocal(
134 const base::FilePath& source_file_path,
135 const base::FilePath& device_file_path,
136 const CopyFileFromLocalSuccessCallback& success_callback,
137 const ErrorCallback& error_callback) override;
138 void DeleteFile(const base::FilePath& file_path,
139 const DeleteFileSuccessCallback& success_callback,
140 const ErrorCallback& error_callback) override;
141 void DeleteDirectory(const base::FilePath& file_path,
142 const DeleteDirectorySuccessCallback& success_callback,
143 const ErrorCallback& error_callback) override;
144 void AddWatcher(const GURL& origin,
145 const base::FilePath& file_path,
146 const bool recursive,
147 const storage::WatcherManager::StatusCallback& callback,
148 const storage::WatcherManager::NotificationCallback&
149 notification_callback) override;
150 void RemoveWatcher(
151 const GURL& origin,
152 const base::FilePath& file_path,
153 const bool recursive,
154 const storage::WatcherManager::StatusCallback& callback) override;
155 void CancelPendingTasksAndDeleteDelegate() override;
157 // Ensures the device is initialized for communication by doing a
158 // call-and-reply to a blocking pool thread. |task_info.task| runs on a
159 // blocking pool thread and |task_info.reply| runs on the IO thread.
161 // If the device is already initialized, post the |task_info.task|
162 // immediately on a blocking pool thread.
164 // If the device is uninitialized, store the |task_info| in a pending task
165 // list and then runs all the pending tasks once the device is successfully
166 // initialized.
167 void EnsureInitAndRunTask(const PendingTaskInfo& task_info);
169 // Writes data chunk from the device to the snapshot file path based on the
170 // parameters in |current_snapshot_details_| by doing a call-and-reply to a
171 // blocking pool thread.
172 void WriteDataChunkIntoSnapshotFile();
174 // Processes the next pending request.
175 void ProcessNextPendingRequest();
177 // Handles the event that the device is initialized. |succeeded| indicates
178 // whether device initialization succeeded or not. If the device is
179 // successfully initialized, runs the next pending task.
180 void OnInitCompleted(bool succeeded);
182 // Called when GetFileInfo() completes. |file_info| specifies the requested
183 // file details. |error| specifies the platform file error code.
185 // If the GetFileInfo() succeeds, |success_callback| is invoked to notify the
186 // caller about the |file_info| details.
188 // If the GetFileInfo() fails, |file_info| is not set and |error_callback| is
189 // invoked to notify the caller about the platform file |error|.
190 void OnGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
191 const ErrorCallback& error_callback,
192 base::File::Info* file_info,
193 base::File::Error error);
195 // Called when ReadDirectory() completes. |file_list| contains the directory
196 // file entries information. |error| specifies the platform file error code.
198 // If the ReadDirectory() succeeds, |success_callback| is invoked to notify
199 // the caller about the directory file entries.
201 // If the ReadDirectory() fails, |file_list| is not set and |error_callback|
202 // is invoked to notify the caller about the platform file |error|.
203 void OnDidReadDirectory(const ReadDirectorySuccessCallback& success_callback,
204 const ErrorCallback& error_callback,
205 storage::AsyncFileUtil::EntryList* file_list,
206 base::File::Error error);
208 // Called when the get file stream request completes.
209 // |file_details.request_info| contains the CreateSnapshot request param
210 // details. |error| specifies the platform file error code.
212 // If the file stream of the device file is successfully
213 // fetched, |file_details| will contain the required details for the creation
214 // of the snapshot file.
216 // If the get file stream request fails, |error| is set accordingly.
217 void OnGetFileStream(scoped_ptr<SnapshotFileDetails> file_details,
218 base::File::Error error);
220 // Called when WriteDataChunkIntoSnapshotFile() completes.
221 // |bytes_written| specifies the number of bytes written into the
222 // |snapshot_file_path| during the last write operation.
224 // If the write operation succeeds, |bytes_written| is set to a non-zero
225 // value.
227 // If the write operation fails, |bytes_written| is set to zero.
228 void OnWroteDataChunkIntoSnapshotFile(
229 const base::FilePath& snapshot_file_path,
230 DWORD bytes_written);
232 // Portable device initialization state.
233 InitializationState init_state_;
235 // The task runner where the device operation tasks runs.
236 scoped_refptr<base::SequencedTaskRunner> media_task_runner_;
238 // Device storage partition details
239 // (e.g. device path, PnP device id and storage object id).
240 StorageDeviceInfo storage_device_info_;
242 // Used to track the current state of the snapshot file (e.g how many bytes
243 // written to the snapshot file, optimal data transfer size, source file
244 // stream, etc).
246 // A snapshot file is created incrementally. CreateSnapshotFile request reads
247 // and writes the snapshot file data in chunks. In order to retain the order
248 // of the snapshot file requests, make sure there is only one active snapshot
249 // file request at any time.
250 scoped_ptr<SnapshotFileDetails> current_snapshot_details_;
252 // A list of pending tasks that needs to be run when the device is
253 // initialized or when the current task in progress is complete.
254 std::queue<PendingTaskInfo> pending_tasks_;
256 // Used to make sure only one task is in progress at any time.
257 bool task_in_progress_;
259 // For callbacks that may run after destruction.
260 base::WeakPtrFactory<MTPDeviceDelegateImplWin> weak_ptr_factory_;
262 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplWin);
265 #endif // CHROME_BROWSER_MEDIA_GALLERIES_WIN_MTP_DEVICE_DELEGATE_IMPL_WIN_H_