NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / media_galleries / linux / mtp_device_delegate_impl_linux.h
blob1534b0d86394bfcf3bf1e1274e6eda872dcd3a92
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.
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_
8 #include <queue>
10 #include "base/callback.h"
11 #include "base/location.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/platform_file.h"
15 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
16 #include "webkit/browser/fileapi/async_file_util.h"
18 namespace base {
19 class FilePath;
22 struct SnapshotRequestInfo;
24 // MTPDeviceDelegateImplLinux communicates with the media transfer protocol
25 // (MTP) device to complete file system operations. These operations are
26 // performed asynchronously. Instantiate this class per MTP device storage.
27 // MTPDeviceDelegateImplLinux lives on the IO thread.
28 // MTPDeviceDelegateImplLinux does a call-and-reply to the UI thread
29 // to dispatch the requests to MediaTransferProtocolManager.
30 class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
31 private:
32 friend void CreateMTPDeviceAsyncDelegate(
33 const std::string&,
34 const CreateMTPDeviceAsyncDelegateCallback&);
36 enum InitializationState {
37 UNINITIALIZED = 0,
38 PENDING_INIT,
39 INITIALIZED
42 // Used to represent pending task details.
43 struct PendingTaskInfo {
44 PendingTaskInfo(const tracked_objects::Location& location,
45 const base::Closure& task);
46 ~PendingTaskInfo();
48 const tracked_objects::Location location;
49 const base::Closure task;
52 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call.
53 // Defer the device initializations until the first file operation request.
54 // Do all the initializations in EnsureInitAndRunTask() function.
55 explicit MTPDeviceDelegateImplLinux(const std::string& device_location);
57 // Destructed via CancelPendingTasksAndDeleteDelegate().
58 virtual ~MTPDeviceDelegateImplLinux();
60 // MTPDeviceAsyncDelegate:
61 virtual void GetFileInfo(const base::FilePath& file_path,
62 const GetFileInfoSuccessCallback& success_callback,
63 const ErrorCallback& error_callback) OVERRIDE;
64 virtual void ReadDirectory(
65 const base::FilePath& root,
66 const ReadDirectorySuccessCallback& success_callback,
67 const ErrorCallback& error_callback) OVERRIDE;
68 virtual void CreateSnapshotFile(
69 const base::FilePath& device_file_path,
70 const base::FilePath& local_path,
71 const CreateSnapshotFileSuccessCallback& success_callback,
72 const ErrorCallback& error_callback) OVERRIDE;
73 virtual bool IsStreaming() OVERRIDE;
74 virtual void ReadBytes(
75 const base::FilePath& device_file_path,
76 net::IOBuffer* buf, int64 offset, int buf_len,
77 const ReadBytesSuccessCallback& success_callback,
78 const ErrorCallback& error_callback) OVERRIDE;
79 virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE;
81 // Ensures the device is initialized for communication by doing a
82 // call-and-reply to the UI thread. |task_info.task| runs on the UI thread.
84 // If the device is already initialized, post the |task_info.task| immediately
85 // on the UI thread.
87 // If the device is uninitialized, store the |task_info| in a pending task
88 // list and runs all the pending tasks once the device is successfully
89 // initialized.
90 void EnsureInitAndRunTask(const PendingTaskInfo& task_info);
92 // Writes data from the device to the snapshot file path based on the
93 // parameters in |current_snapshot_request_info_| by doing a call-and-reply to
94 // the UI thread.
96 // |snapshot_file_info| specifies the metadata details of the snapshot file.
97 void WriteDataIntoSnapshotFile(const base::File::Info& snapshot_file_info);
99 // Processes the next pending request.
100 void ProcessNextPendingRequest();
102 // Handles the device initialization event. |succeeded| indicates whether
103 // device initialization succeeded.
105 // If the device is successfully initialized, runs the next pending task.
106 void OnInitCompleted(bool succeeded);
108 // Called when GetFileInfo() succeeds. |file_info| specifies the
109 // requested file details. |success_callback| is invoked to notify the caller
110 // about the requested file details.
111 void OnDidGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
112 const base::File::Info& file_info);
114 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
115 // get the |root| directory metadata details. |file_info| specifies the |root|
116 // directory details.
118 // If |root| is a directory, post a task on the UI thread to read the |root|
119 // directory file entries.
121 // If |root| is not a directory, |error_callback| is invoked to notify the
122 // caller about the platform file error and process the next pending request.
123 void OnDidGetFileInfoToReadDirectory(
124 const std::string& root,
125 const ReadDirectorySuccessCallback& success_callback,
126 const ErrorCallback& error_callback,
127 const base::File::Info& file_info);
129 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
130 // create the snapshot file of |snapshot_request_info.device_file_path|.
131 // |file_info| specifies the device file metadata details.
133 // Posts a task on the UI thread to copy the data contents of the device file
134 // to the snapshot file.
135 void OnDidGetFileInfoToCreateSnapshotFile(
136 scoped_ptr<SnapshotRequestInfo> snapshot_request_info,
137 const base::File::Info& file_info);
139 // Called when GetFileInfo() succeeds to read a range of bytes.
140 void OnDidGetFileInfoToReadBytes(const ReadBytesRequest& request,
141 const base::File::Info& file_info);
143 // Called when ReadDirectory() succeeds.
145 // |file_list| contains the directory file entries.
146 // |success_callback| is invoked to notify the caller about the directory
147 // file entries.
148 void OnDidReadDirectory(const ReadDirectorySuccessCallback& success_callback,
149 const fileapi::AsyncFileUtil::EntryList& file_list);
151 // Called when WriteDataIntoSnapshotFile() succeeds.
153 // |snapshot_file_info| specifies the snapshot file metadata details.
155 // |current_snapshot_request_info_.success_callback| is invoked to notify the
156 // caller about |snapshot_file_info|.
157 void OnDidWriteDataIntoSnapshotFile(
158 const base::File::Info& snapshot_file_info,
159 const base::FilePath& snapshot_file_path);
161 // Called when WriteDataIntoSnapshotFile() fails.
163 // |error| specifies the platform file error code.
165 // |current_snapshot_request_info_.error_callback| is invoked to notify the
166 // caller about |error|.
167 void OnWriteDataIntoSnapshotFileError(base::File::Error error);
169 // Called when ReadBytes() succeeds.
171 // |success_callback| is invoked to notify the caller about the read bytes.
172 // |bytes_read| is the number of bytes read.
173 void OnDidReadBytes(const ReadBytesSuccessCallback& success_callback,
174 int bytes_read);
176 // Handles the device file |error|. |error_callback| is invoked to notify the
177 // caller about the file error.
178 void HandleDeviceFileError(const ErrorCallback& error_callback,
179 base::File::Error error);
181 // MTP device initialization state.
182 InitializationState init_state_;
184 // Used to make sure only one task is in progress at any time.
185 bool task_in_progress_;
187 // Registered file system device path. This path does not
188 // correspond to a real device path (e.g. "/usb:2,2:81282").
189 const base::FilePath device_path_;
191 // MTP device storage name (e.g. "usb:2,2:81282").
192 std::string storage_name_;
194 // A list of pending tasks that needs to be run when the device is
195 // initialized or when the current task in progress is complete.
196 std::queue<PendingTaskInfo> pending_tasks_;
198 // Used to track the current snapshot file request. A snapshot file is created
199 // incrementally. CreateSnapshotFile request reads the device file and writes
200 // to the snapshot file in chunks. In order to retain the order of the
201 // snapshot file requests, make sure there is only one active snapshot file
202 // request at any time.
203 scoped_ptr<SnapshotRequestInfo> current_snapshot_request_info_;
205 // For callbacks that may run after destruction.
206 base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_;
208 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux);
211 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_