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"
21 // Asynchronous delegate for media transfer protocol (MTP) device to perform
22 // media device file system operations. Class that implements this
23 // delegate does the actual communication with the MTP device.
24 // The lifetime of the delegate is managed by the MTPDeviceMapService class.
25 // Member functions and callbacks run on the IO thread.
26 class MTPDeviceAsyncDelegate
{
28 // A callback to be called when GetFileInfo method call succeeds.
29 typedef base::Callback
<
30 void(const base::File::Info
& file_info
)> GetFileInfoSuccessCallback
;
32 // A callback to be called when CreateDirectory method call succeeds.
33 typedef base::Closure CreateDirectorySuccessCallback
;
35 // A callback to be called when ReadDirectory method call succeeds.
36 typedef base::Callback
<
37 void(const storage::AsyncFileUtil::EntryList
& file_list
, bool has_more
)>
38 ReadDirectorySuccessCallback
;
40 // A callback to be called when GetFileInfo/ReadDirectory/CreateSnapshot
42 typedef base::Callback
<void(base::File::Error error
)> ErrorCallback
;
44 // A callback to be called when CreateSnapshotFile method call succeeds.
45 typedef base::Callback
<
46 void(const base::File::Info
& file_info
,
47 const base::FilePath
& local_path
)> CreateSnapshotFileSuccessCallback
;
49 // A callback to be called when ReadBytes method call succeeds.
50 typedef base::Callback
<
51 void(const base::File::Info
& file_info
,
52 int bytes_read
)> ReadBytesSuccessCallback
;
54 struct ReadBytesRequest
{
55 ReadBytesRequest(uint32 file_id
,
56 net::IOBuffer
* buf
, int64 offset
, int buf_len
,
57 const ReadBytesSuccessCallback
& success_callback
,
58 const ErrorCallback
& error_callback
);
62 scoped_refptr
<net::IOBuffer
> buf
;
65 ReadBytesSuccessCallback success_callback
;
66 ErrorCallback error_callback
;
69 // A callback to be called to create a temporary file. Path to the temporary
70 // file is returned as base::FilePath. The caller is responsible to manage
71 // life time of the temporary file.
72 typedef base::Callback
<base::FilePath()> CreateTemporaryFileCallback
;
74 // A callback to be called when CopyFileLocal method call succeeds.
75 typedef base::Closure CopyFileLocalSuccessCallback
;
77 // A callback to be called when MoveFileLocal method call succeeds.
78 typedef base::Closure MoveFileLocalSuccessCallback
;
80 // A callback to be called when CopyFileFromLocal method call succeeds.
81 typedef base::Closure CopyFileFromLocalSuccessCallback
;
83 // A callback to be called when DeleteFile method call succeeds.
84 typedef base::Closure DeleteFileSuccessCallback
;
86 // A callback to be called when DeleteDirectory method call succeeds.
87 typedef base::Closure DeleteDirectorySuccessCallback
;
89 typedef storage::AsyncFileUtil::CopyFileProgressCallback
90 CopyFileProgressCallback
;
92 // Gets information about the given |file_path| and invokes the appropriate
93 // callback asynchronously when complete.
94 virtual void GetFileInfo(
95 const base::FilePath
& file_path
,
96 const GetFileInfoSuccessCallback
& success_callback
,
97 const ErrorCallback
& error_callback
) = 0;
99 // Creates a directory to |directory_path|. When |exclusive| is true, this
100 // returns base::File::FILE_ERROR_EXISTS if a directory already exists for
101 // |directory_path|. When |recursive| is true, the directory is created
102 // recursively to |directory_path|.
103 virtual void CreateDirectory(
104 const base::FilePath
& directory_path
,
105 const bool exclusive
,
106 const bool recursive
,
107 const CreateDirectorySuccessCallback
& success_callback
,
108 const ErrorCallback
& error_callback
) = 0;
110 // Enumerates the |root| directory contents and invokes the appropriate
111 // callback asynchronously when complete.
112 virtual void ReadDirectory(
113 const base::FilePath
& root
,
114 const ReadDirectorySuccessCallback
& success_callback
,
115 const ErrorCallback
& error_callback
) = 0;
117 // Copy the contents of |device_file_path| to |local_path|. Invokes the
118 // appropriate callback asynchronously when complete.
119 virtual void CreateSnapshotFile(
120 const base::FilePath
& device_file_path
,
121 const base::FilePath
& local_path
,
122 const CreateSnapshotFileSuccessCallback
& success_callback
,
123 const ErrorCallback
& error_callback
) = 0;
125 // Platform-specific implementations that are streaming don't create a local
126 // snapshot file. Blobs are instead FileSystemURL backed and read in a stream.
127 virtual bool IsStreaming() = 0;
129 // Reads up to |buf_len| bytes from |device_file_path| into |buf|. Invokes the
130 // appropriate callback asynchronously when complete. Only valid when
131 // IsStreaming() is true.
132 virtual void ReadBytes(const base::FilePath
& device_file_path
,
133 const scoped_refptr
<net::IOBuffer
>& buf
,
136 const ReadBytesSuccessCallback
& success_callback
,
137 const ErrorCallback
& error_callback
) = 0;
139 // Returns true if storage is opened for read only.
140 virtual bool IsReadOnly() const = 0;
142 // Copies a file |source_file_path| to |device_file_path|.
143 // |create_temporary_file_callback| can be used to create a temporary file.
144 virtual void CopyFileLocal(
145 const base::FilePath
& source_file_path
,
146 const base::FilePath
& device_file_path
,
147 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
148 const CopyFileProgressCallback
& progress_callback
,
149 const CopyFileLocalSuccessCallback
& success_callback
,
150 const ErrorCallback
& error_callback
) = 0;
152 // Moves a file |source_file_path| to |device_file_path|.
153 // |create_temporary_file_callback| can be used to create a temporary file.
154 virtual void MoveFileLocal(
155 const base::FilePath
& source_file_path
,
156 const base::FilePath
& device_file_path
,
157 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
158 const MoveFileLocalSuccessCallback
& success_callback
,
159 const ErrorCallback
& error_callback
) = 0;
161 // Copies a file from |source_file_path| to |device_file_path|.
162 virtual void CopyFileFromLocal(
163 const base::FilePath
& source_file_path
,
164 const base::FilePath
& device_file_path
,
165 const CopyFileFromLocalSuccessCallback
& success_callback
,
166 const ErrorCallback
& error_callback
) = 0;
168 // Deletes a file at |file_path|.
169 virtual void DeleteFile(const base::FilePath
& file_path
,
170 const DeleteFileSuccessCallback
& success_callback
,
171 const ErrorCallback
& error_callback
) = 0;
173 // Deletes a directory at |file_path|. The directory must be empty.
174 virtual void DeleteDirectory(
175 const base::FilePath
& file_path
,
176 const DeleteDirectorySuccessCallback
& success_callback
,
177 const ErrorCallback
& error_callback
) = 0;
180 // (1) Browser application is in shutdown mode (or)
181 // (2) Last extension using this MTP device is destroyed (or)
182 // (3) Attached MTP device is removed (or)
183 // (4) User revoked the MTP device gallery permission.
184 // Ownership of |MTPDeviceAsyncDelegate| is handed off to the delegate
185 // implementation class by this call. This function should take care of
186 // cancelling all the pending tasks before deleting itself.
187 virtual void CancelPendingTasksAndDeleteDelegate() = 0;
190 // Always destruct this object via CancelPendingTasksAndDeleteDelegate().
191 virtual ~MTPDeviceAsyncDelegate() {}
194 typedef base::Callback
<void(MTPDeviceAsyncDelegate
*)>
195 CreateMTPDeviceAsyncDelegateCallback
;
197 void CreateMTPDeviceAsyncDelegate(
198 const base::FilePath::StringType
& device_location
,
199 const bool read_only
,
200 const CreateMTPDeviceAsyncDelegateCallback
& callback
);
202 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_