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_
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"
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
{
32 friend void CreateMTPDeviceAsyncDelegate(
34 const CreateMTPDeviceAsyncDelegateCallback
&);
36 enum InitializationState
{
42 // Used to represent pending task details.
43 struct PendingTaskInfo
{
44 PendingTaskInfo(const tracked_objects::Location
& location
,
45 const base::Closure
& task
);
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 void CancelPendingTasksAndDeleteDelegate() OVERRIDE
;
75 // Ensures the device is initialized for communication by doing a
76 // call-and-reply to the UI thread. |task_info.task| runs on the UI thread.
78 // If the device is already initialized, post the |task_info.task| immediately
81 // If the device is uninitialized, store the |task_info| in a pending task
82 // list and runs all the pending tasks once the device is successfully
84 void EnsureInitAndRunTask(const PendingTaskInfo
& task_info
);
86 // Writes data from the device to the snapshot file path based on the
87 // parameters in |current_snapshot_request_info_| by doing a call-and-reply to
90 // |snapshot_file_info| specifies the metadata details of the snapshot file.
91 void WriteDataIntoSnapshotFile(
92 const base::PlatformFileInfo
& snapshot_file_info
);
94 // Processes the next pending request.
95 void ProcessNextPendingRequest();
97 // Handles the device initialization event. |succeeded| indicates whether
98 // device initialization succeeded.
100 // If the device is successfully initialized, runs the next pending task.
101 void OnInitCompleted(bool succeeded
);
103 // Called when GetFileInfo() succeeds. |file_info| specifies the
104 // requested file details. |success_callback| is invoked to notify the caller
105 // about the requested file details.
106 void OnDidGetFileInfo(const GetFileInfoSuccessCallback
& success_callback
,
107 const base::PlatformFileInfo
& file_info
);
109 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
110 // get the |root| directory metadata details. |file_info| specifies the |root|
111 // directory details.
113 // If |root| is a directory, post a task on the UI thread to read the |root|
114 // directory file entries.
116 // If |root| is not a directory, |error_callback| is invoked to notify the
117 // caller about the platform file error and process the next pending request.
118 void OnDidGetFileInfoToReadDirectory(
119 const std::string
& root
,
120 const ReadDirectorySuccessCallback
& success_callback
,
121 const ErrorCallback
& error_callback
,
122 const base::PlatformFileInfo
& file_info
);
124 // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
125 // create the snapshot file of |snapshot_request_info.device_file_path|.
126 // |file_info| specifies the device file metadata details.
128 // Posts a task on the UI thread to copy the data contents of the device file
129 // to the snapshot file.
130 void OnDidGetFileInfoToCreateSnapshotFile(
131 scoped_ptr
<SnapshotRequestInfo
> snapshot_request_info
,
132 const base::PlatformFileInfo
& file_info
);
134 // Called when ReadDirectory() succeeds.
136 // |file_list| contains the directory file entries.
137 // |success_callback| is invoked to notify the caller about the directory
139 void OnDidReadDirectory(const ReadDirectorySuccessCallback
& success_callback
,
140 const fileapi::AsyncFileUtil::EntryList
& file_list
);
142 // Called when WriteDataIntoSnapshotFile() succeeds.
144 // |snapshot_file_info| specifies the snapshot file metadata details.
146 // |current_snapshot_request_info_.success_callback| is invoked to notify the
147 // caller about |snapshot_file_info|.
148 void OnDidWriteDataIntoSnapshotFile(
149 const base::PlatformFileInfo
& snapshot_file_info
,
150 const base::FilePath
& snapshot_file_path
);
152 // Called when WriteDataIntoSnapshotFile() fails.
154 // |error| specifies the platform file error code.
156 // |current_snapshot_request_info_.error_callback| is invoked to notify the
157 // caller about |error|.
158 void OnWriteDataIntoSnapshotFileError(base::PlatformFileError error
);
160 // Handles the device file |error|. |error_callback| is invoked to notify the
161 // caller about the file error.
162 void HandleDeviceFileError(const ErrorCallback
& error_callback
,
163 base::PlatformFileError error
);
165 // MTP device initialization state.
166 InitializationState init_state_
;
168 // Used to make sure only one task is in progress at any time.
169 bool task_in_progress_
;
171 // Registered file system device path. This path does not
172 // correspond to a real device path (e.g. "/usb:2,2:81282").
173 const base::FilePath device_path_
;
175 // MTP device storage name (e.g. "usb:2,2:81282").
176 std::string storage_name_
;
178 // A list of pending tasks that needs to be run when the device is
179 // initialized or when the current task in progress is complete.
180 std::queue
<PendingTaskInfo
> pending_tasks_
;
182 // Used to track the current snapshot file request. A snapshot file is created
183 // incrementally. CreateSnapshotFile request reads the device file and writes
184 // to the snapshot file in chunks. In order to retain the order of the
185 // snapshot file requests, make sure there is only one active snapshot file
186 // request at any time.
187 scoped_ptr
<SnapshotRequestInfo
> current_snapshot_request_info_
;
189 // For callbacks that may run after destruction.
190 base::WeakPtrFactory
<MTPDeviceDelegateImplLinux
> weak_ptr_factory_
;
192 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux
);
195 #endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_