- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / linux / mtp_device_delegate_impl_linux.h
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 #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_
7
8 #include <queue>
9
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"
17
18 namespace base {
19 class FilePath;
20 }
21
22 struct SnapshotRequestInfo;
23
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&);
35
36   enum InitializationState {
37     UNINITIALIZED = 0,
38     PENDING_INIT,
39     INITIALIZED
40   };
41
42   // Used to represent pending task details.
43   struct PendingTaskInfo {
44     PendingTaskInfo(const tracked_objects::Location& location,
45                     const base::Closure& task);
46     ~PendingTaskInfo();
47
48     const tracked_objects::Location location;
49     const base::Closure task;
50   };
51
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);
56
57   // Destructed via CancelPendingTasksAndDeleteDelegate().
58   virtual ~MTPDeviceDelegateImplLinux();
59
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;
74
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.
77   //
78   // If the device is already initialized, post the |task_info.task| immediately
79   // on the UI thread.
80   //
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
83   // initialized.
84   void EnsureInitAndRunTask(const PendingTaskInfo& task_info);
85
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
88   // the UI thread.
89   //
90   // |snapshot_file_info| specifies the metadata details of the snapshot file.
91   void WriteDataIntoSnapshotFile(
92       const base::PlatformFileInfo& snapshot_file_info);
93
94   // Processes the next pending request.
95   void ProcessNextPendingRequest();
96
97   // Handles the device initialization event. |succeeded| indicates whether
98   // device initialization succeeded.
99   //
100   // If the device is successfully initialized, runs the next pending task.
101   void OnInitCompleted(bool succeeded);
102
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);
108
109   // Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
110   // get the |root| directory metadata details. |file_info| specifies the |root|
111   // directory details.
112   //
113   // If |root| is a directory, post a task on the UI thread to read the |root|
114   // directory file entries.
115   //
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);
123
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.
127   //
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);
133
134   // Called when ReadDirectory() succeeds.
135   //
136   // |file_list| contains the directory file entries.
137   // |success_callback| is invoked to notify the caller about the directory
138   // file entries.
139   void OnDidReadDirectory(const ReadDirectorySuccessCallback& success_callback,
140                           const fileapi::AsyncFileUtil::EntryList& file_list);
141
142   // Called when WriteDataIntoSnapshotFile() succeeds.
143   //
144   // |snapshot_file_info| specifies the snapshot file metadata details.
145   //
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);
151
152   // Called when WriteDataIntoSnapshotFile() fails.
153   //
154   // |error| specifies the platform file error code.
155   //
156   // |current_snapshot_request_info_.error_callback| is invoked to notify the
157   // caller about |error|.
158   void OnWriteDataIntoSnapshotFileError(base::PlatformFileError error);
159
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);
164
165   // MTP device initialization state.
166   InitializationState init_state_;
167
168   // Used to make sure only one task is in progress at any time.
169   bool task_in_progress_;
170
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_;
174
175   // MTP device storage name (e.g. "usb:2,2:81282").
176   std::string storage_name_;
177
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_;
181
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_;
188
189   // For callbacks that may run after destruction.
190   base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_;
191
192   DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux);
193 };
194
195 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_