Remove stray platform_file.h from chrome and components
[chromium-blink-merge.git] / chrome / browser / media_galleries / linux / mtp_device_delegate_impl_linux.h
blob0a698e6c7c2b18bb9ec5c1507211b3f408bc8fa0
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 "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
15 #include "webkit/browser/fileapi/async_file_util.h"
17 namespace base {
18 class FilePath;
21 struct SnapshotRequestInfo;
23 // MTPDeviceDelegateImplLinux communicates with the media transfer protocol
24 // (MTP) device to complete file system operations. These operations are
25 // performed asynchronously. Instantiate this class per MTP device storage.
26 // MTPDeviceDelegateImplLinux lives on the IO thread.
27 // MTPDeviceDelegateImplLinux does a call-and-reply to the UI thread
28 // to dispatch the requests to MediaTransferProtocolManager.
29 class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
30 private:
31 friend void CreateMTPDeviceAsyncDelegate(
32 const std::string&,
33 const CreateMTPDeviceAsyncDelegateCallback&);
35 enum InitializationState {
36 UNINITIALIZED = 0,
37 PENDING_INIT,
38 INITIALIZED
41 // Used to represent pending task details.
42 struct PendingTaskInfo {
43 PendingTaskInfo(const tracked_objects::Location& location,
44 const base::Closure& task);
45 ~PendingTaskInfo();
47 const tracked_objects::Location location;
48 const base::Closure task;
51 // Should only be called by CreateMTPDeviceAsyncDelegate() factory call.
52 // Defer the device initializations until the first file operation request.
53 // Do all the initializations in EnsureInitAndRunTask() function.
54 explicit MTPDeviceDelegateImplLinux(const std::string& device_location);
56 // Destructed via CancelPendingTasksAndDeleteDelegate().
57 virtual ~MTPDeviceDelegateImplLinux();
59 // MTPDeviceAsyncDelegate:
60 virtual void GetFileInfo(const base::FilePath& file_path,
61 const GetFileInfoSuccessCallback& success_callback,
62 const ErrorCallback& error_callback) OVERRIDE;
63 virtual void ReadDirectory(
64 const base::FilePath& root,
65 const ReadDirectorySuccessCallback& success_callback,
66 const ErrorCallback& error_callback) OVERRIDE;
67 virtual void CreateSnapshotFile(
68 const base::FilePath& device_file_path,
69 const base::FilePath& local_path,
70 const CreateSnapshotFileSuccessCallback& success_callback,
71 const ErrorCallback& error_callback) OVERRIDE;
72 virtual bool IsStreaming() OVERRIDE;
73 virtual void ReadBytes(
74 const base::FilePath& device_file_path,
75 net::IOBuffer* buf, int64 offset, int buf_len,
76 const ReadBytesSuccessCallback& success_callback,
77 const ErrorCallback& error_callback) OVERRIDE;
78 virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE;
80 // Ensures the device is initialized for communication by doing a
81 // call-and-reply to the UI thread. |task_info.task| runs on the UI thread.
83 // If the device is already initialized, post the |task_info.task| immediately
84 // on the UI thread.
86 // If the device is uninitialized, store the |task_info| in a pending task
87 // list and runs all the pending tasks once the device is successfully
88 // initialized.
89 void EnsureInitAndRunTask(const PendingTaskInfo& task_info);
91 // Writes data from the device to the snapshot file path based on the
92 // parameters in |current_snapshot_request_info_| by doing a call-and-reply to
93 // the UI thread.
95 // |snapshot_file_info| specifies the metadata details of the snapshot file.
96 void WriteDataIntoSnapshotFile(const base::File::Info& snapshot_file_info);
98 // Processes the next pending request.
99 void ProcessNextPendingRequest();
101 // Handles the device initialization event. |succeeded| indicates whether
102 // device initialization succeeded.
104 // If the device is successfully initialized, runs the next pending task.
105 void OnInitCompleted(bool succeeded);
107 // Called when GetFileInfo() succeeds. |file_info| specifies the
108 // requested file details. |success_callback| is invoked to notify the caller
109 // about the requested file details.
110 void OnDidGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
111 const base::File::Info& file_info);
113 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
114 // get the |root| directory metadata details. |file_info| specifies the |root|
115 // directory details.
117 // If |root| is a directory, post a task on the UI thread to read the |root|
118 // directory file entries.
120 // If |root| is not a directory, |error_callback| is invoked to notify the
121 // caller about the file error and process the next pending request.
122 void OnDidGetFileInfoToReadDirectory(
123 const std::string& root,
124 const ReadDirectorySuccessCallback& success_callback,
125 const ErrorCallback& error_callback,
126 const base::File::Info& file_info);
128 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
129 // create the snapshot file of |snapshot_request_info.device_file_path|.
130 // |file_info| specifies the device file metadata details.
132 // Posts a task on the UI thread to copy the data contents of the device file
133 // to the snapshot file.
134 void OnDidGetFileInfoToCreateSnapshotFile(
135 scoped_ptr<SnapshotRequestInfo> snapshot_request_info,
136 const base::File::Info& file_info);
138 // Called when ReadDirectory() succeeds.
140 // |file_list| contains the directory file entries.
141 // |success_callback| is invoked to notify the caller about the directory
142 // file entries.
143 void OnDidReadDirectory(const ReadDirectorySuccessCallback& success_callback,
144 const fileapi::AsyncFileUtil::EntryList& file_list);
146 // Called when WriteDataIntoSnapshotFile() succeeds.
148 // |snapshot_file_info| specifies the snapshot file metadata details.
150 // |current_snapshot_request_info_.success_callback| is invoked to notify the
151 // caller about |snapshot_file_info|.
152 void OnDidWriteDataIntoSnapshotFile(
153 const base::File::Info& snapshot_file_info,
154 const base::FilePath& snapshot_file_path);
156 // Called when WriteDataIntoSnapshotFile() fails.
158 // |error| specifies the file error code.
160 // |current_snapshot_request_info_.error_callback| is invoked to notify the
161 // caller about |error|.
162 void OnWriteDataIntoSnapshotFileError(base::File::Error error);
164 // Called when ReadBytes() succeeds.
166 // |success_callback| is invoked to notify the caller about the read bytes.
167 // |bytes_read| is the number of bytes read.
168 void OnDidReadBytes(const ReadBytesSuccessCallback& success_callback,
169 const base::File::Info& file_info, int bytes_read);
171 // Handles the device file |error|. |error_callback| is invoked to notify the
172 // caller about the file error.
173 void HandleDeviceFileError(const ErrorCallback& error_callback,
174 base::File::Error error);
176 // MTP device initialization state.
177 InitializationState init_state_;
179 // Used to make sure only one task is in progress at any time.
180 bool task_in_progress_;
182 // Registered file system device path. This path does not
183 // correspond to a real device path (e.g. "/usb:2,2:81282").
184 const base::FilePath device_path_;
186 // MTP device storage name (e.g. "usb:2,2:81282").
187 std::string storage_name_;
189 // A list of pending tasks that needs to be run when the device is
190 // initialized or when the current task in progress is complete.
191 std::queue<PendingTaskInfo> pending_tasks_;
193 // Used to track the current snapshot file request. A snapshot file is created
194 // incrementally. CreateSnapshotFile request reads the device file and writes
195 // to the snapshot file in chunks. In order to retain the order of the
196 // snapshot file requests, make sure there is only one active snapshot file
197 // request at any time.
198 scoped_ptr<SnapshotRequestInfo> current_snapshot_request_info_;
200 // For callbacks that may run after destruction.
201 base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_;
203 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux);
206 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_