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_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_
8 #include "base/callback.h"
9 #include "base/files/file.h"
10 #include "base/memory/ref_counted.h"
11 #include "storage/browser/fileapi/async_file_util.h"
12 #include "storage/browser/fileapi/watcher_manager.h"
23 // Asynchronous delegate for media transfer protocol (MTP) device to perform
24 // media device file system operations. Class that implements this
25 // delegate does the actual communication with the MTP device.
26 // The lifetime of the delegate is managed by the MTPDeviceMapService class.
27 // Member functions and callbacks run on the IO thread.
28 class MTPDeviceAsyncDelegate
{
30 // A callback to be called when GetFileInfo method call succeeds.
31 typedef base::Callback
<
32 void(const base::File::Info
& file_info
)> GetFileInfoSuccessCallback
;
34 // A callback to be called when CreateDirectory method call succeeds.
35 typedef base::Closure CreateDirectorySuccessCallback
;
37 // A callback to be called when ReadDirectory method call succeeds.
38 typedef base::Callback
<
39 void(const storage::AsyncFileUtil::EntryList
& file_list
, bool has_more
)>
40 ReadDirectorySuccessCallback
;
42 // A callback to be called when GetFileInfo/ReadDirectory/CreateSnapshot
44 typedef base::Callback
<void(base::File::Error error
)> ErrorCallback
;
46 // A callback to be called when CreateSnapshotFile method call succeeds.
47 typedef base::Callback
<
48 void(const base::File::Info
& file_info
,
49 const base::FilePath
& local_path
)> CreateSnapshotFileSuccessCallback
;
51 // A callback to be called when ReadBytes method call succeeds.
52 typedef base::Callback
<
53 void(const base::File::Info
& file_info
,
54 int bytes_read
)> ReadBytesSuccessCallback
;
56 struct ReadBytesRequest
{
57 ReadBytesRequest(uint32 file_id
,
58 net::IOBuffer
* buf
, int64 offset
, int buf_len
,
59 const ReadBytesSuccessCallback
& success_callback
,
60 const ErrorCallback
& error_callback
);
64 scoped_refptr
<net::IOBuffer
> buf
;
67 ReadBytesSuccessCallback success_callback
;
68 ErrorCallback error_callback
;
71 // A callback to be called to create a temporary file. Path to the temporary
72 // file is returned as base::FilePath. The caller is responsible to manage
73 // life time of the temporary file.
74 typedef base::Callback
<base::FilePath()> CreateTemporaryFileCallback
;
76 // A callback to be called when CopyFileLocal method call succeeds.
77 typedef base::Closure CopyFileLocalSuccessCallback
;
79 // A callback to be called when MoveFileLocal method call succeeds.
80 typedef base::Closure MoveFileLocalSuccessCallback
;
82 // A callback to be called when CopyFileFromLocal method call succeeds.
83 typedef base::Closure CopyFileFromLocalSuccessCallback
;
85 // A callback to be called when DeleteFile method call succeeds.
86 typedef base::Closure DeleteFileSuccessCallback
;
88 // A callback to be called when DeleteDirectory method call succeeds.
89 typedef base::Closure DeleteDirectorySuccessCallback
;
91 typedef storage::AsyncFileUtil::CopyFileProgressCallback
92 CopyFileProgressCallback
;
94 // Gets information about the given |file_path| and invokes the appropriate
95 // callback asynchronously when complete.
96 virtual void GetFileInfo(
97 const base::FilePath
& file_path
,
98 const GetFileInfoSuccessCallback
& success_callback
,
99 const ErrorCallback
& error_callback
) = 0;
101 // Creates a directory to |directory_path|. When |exclusive| is true, this
102 // returns base::File::FILE_ERROR_EXISTS if a directory already exists for
103 // |directory_path|. When |recursive| is true, the directory is created
104 // recursively to |directory_path|.
105 virtual void CreateDirectory(
106 const base::FilePath
& directory_path
,
107 const bool exclusive
,
108 const bool recursive
,
109 const CreateDirectorySuccessCallback
& success_callback
,
110 const ErrorCallback
& error_callback
) = 0;
112 // Enumerates the |root| directory contents and invokes the appropriate
113 // callback asynchronously when complete.
114 virtual void ReadDirectory(
115 const base::FilePath
& root
,
116 const ReadDirectorySuccessCallback
& success_callback
,
117 const ErrorCallback
& error_callback
) = 0;
119 // Copy the contents of |device_file_path| to |local_path|. Invokes the
120 // appropriate callback asynchronously when complete.
121 virtual void CreateSnapshotFile(
122 const base::FilePath
& device_file_path
,
123 const base::FilePath
& local_path
,
124 const CreateSnapshotFileSuccessCallback
& success_callback
,
125 const ErrorCallback
& error_callback
) = 0;
127 // Platform-specific implementations that are streaming don't create a local
128 // snapshot file. Blobs are instead FileSystemURL backed and read in a stream.
129 virtual bool IsStreaming() = 0;
131 // Reads up to |buf_len| bytes from |device_file_path| into |buf|. Invokes the
132 // appropriate callback asynchronously when complete. Only valid when
133 // IsStreaming() is true.
134 virtual void ReadBytes(const base::FilePath
& device_file_path
,
135 const scoped_refptr
<net::IOBuffer
>& buf
,
138 const ReadBytesSuccessCallback
& success_callback
,
139 const ErrorCallback
& error_callback
) = 0;
141 // Returns true if storage is opened for read only.
142 virtual bool IsReadOnly() const = 0;
144 // Copies a file |source_file_path| to |device_file_path|.
145 // |create_temporary_file_callback| can be used to create a temporary file.
146 virtual void CopyFileLocal(
147 const base::FilePath
& source_file_path
,
148 const base::FilePath
& device_file_path
,
149 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
150 const CopyFileProgressCallback
& progress_callback
,
151 const CopyFileLocalSuccessCallback
& success_callback
,
152 const ErrorCallback
& error_callback
) = 0;
154 // Moves a file |source_file_path| to |device_file_path|.
155 // |create_temporary_file_callback| can be used to create a temporary file.
156 virtual void MoveFileLocal(
157 const base::FilePath
& source_file_path
,
158 const base::FilePath
& device_file_path
,
159 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
160 const MoveFileLocalSuccessCallback
& success_callback
,
161 const ErrorCallback
& error_callback
) = 0;
163 // Copies a file from |source_file_path| to |device_file_path|.
164 virtual void CopyFileFromLocal(
165 const base::FilePath
& source_file_path
,
166 const base::FilePath
& device_file_path
,
167 const CopyFileFromLocalSuccessCallback
& success_callback
,
168 const ErrorCallback
& error_callback
) = 0;
170 // Deletes a file at |file_path|.
171 virtual void DeleteFile(const base::FilePath
& file_path
,
172 const DeleteFileSuccessCallback
& success_callback
,
173 const ErrorCallback
& error_callback
) = 0;
175 // Deletes a directory at |file_path|. The directory must be empty.
176 virtual void DeleteDirectory(
177 const base::FilePath
& file_path
,
178 const DeleteDirectorySuccessCallback
& success_callback
,
179 const ErrorCallback
& error_callback
) = 0;
181 // Adds watcher to |file_path| as |origin|.
182 virtual void AddWatcher(
184 const base::FilePath
& file_path
,
185 const bool recursive
,
186 const storage::WatcherManager::StatusCallback
& callback
,
187 const storage::WatcherManager::NotificationCallback
&
188 notification_callback
) = 0;
190 // Removes watcher from |file_path| of |origin|.
191 virtual void RemoveWatcher(
193 const base::FilePath
& file_path
,
194 const bool recursive
,
195 const storage::WatcherManager::StatusCallback
& callback
) = 0;
198 // (1) Browser application is in shutdown mode (or)
199 // (2) Last extension using this MTP device is destroyed (or)
200 // (3) Attached MTP device is removed (or)
201 // (4) User revoked the MTP device gallery permission.
202 // Ownership of |MTPDeviceAsyncDelegate| is handed off to the delegate
203 // implementation class by this call. This function should take care of
204 // cancelling all the pending tasks before deleting itself.
205 virtual void CancelPendingTasksAndDeleteDelegate() = 0;
208 // Always destruct this object via CancelPendingTasksAndDeleteDelegate().
209 virtual ~MTPDeviceAsyncDelegate() {}
212 typedef base::Callback
<void(MTPDeviceAsyncDelegate
*)>
213 CreateMTPDeviceAsyncDelegateCallback
;
215 void CreateMTPDeviceAsyncDelegate(
216 const base::FilePath::StringType
& device_location
,
217 const bool read_only
,
218 const CreateMTPDeviceAsyncDelegateCallback
& callback
);
220 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_