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 ReadDirectory method call succeeds.
33 typedef base::Callback<
34 void(const storage::AsyncFileUtil::EntryList& file_list, bool has_more)>
35 ReadDirectorySuccessCallback;
37 // A callback to be called when GetFileInfo/ReadDirectory/CreateSnapshot
39 typedef base::Callback<void(base::File::Error error)> ErrorCallback;
41 // A callback to be called when CreateSnapshotFile method call succeeds.
42 typedef base::Callback<
43 void(const base::File::Info& file_info,
44 const base::FilePath& local_path)> CreateSnapshotFileSuccessCallback;
46 // A callback to be called when ReadBytes method call succeeds.
47 typedef base::Callback<
48 void(const base::File::Info& file_info,
49 int bytes_read)> ReadBytesSuccessCallback;
51 struct ReadBytesRequest {
52 ReadBytesRequest(uint32 file_id,
53 net::IOBuffer* buf, int64 offset, int buf_len,
54 const ReadBytesSuccessCallback& success_callback,
55 const ErrorCallback& error_callback);
59 scoped_refptr<net::IOBuffer> buf;
62 ReadBytesSuccessCallback success_callback;
63 ErrorCallback error_callback;
66 // Gets information about the given |file_path| and invokes the appropriate
67 // callback asynchronously when complete.
68 virtual void GetFileInfo(
69 const base::FilePath& file_path,
70 const GetFileInfoSuccessCallback& success_callback,
71 const ErrorCallback& error_callback) = 0;
73 // Enumerates the |root| directory contents and invokes the appropriate
74 // callback asynchronously when complete.
75 virtual void ReadDirectory(
76 const base::FilePath& root,
77 const ReadDirectorySuccessCallback& success_callback,
78 const ErrorCallback& error_callback) = 0;
80 // Copy the contents of |device_file_path| to |local_path|. Invokes the
81 // appropriate callback asynchronously when complete.
82 virtual void CreateSnapshotFile(
83 const base::FilePath& device_file_path,
84 const base::FilePath& local_path,
85 const CreateSnapshotFileSuccessCallback& success_callback,
86 const ErrorCallback& error_callback) = 0;
88 // Platform-specific implementations that are streaming don't create a local
89 // snapshot file. Blobs are instead FileSystemURL backed and read in a stream.
90 virtual bool IsStreaming() = 0;
92 // Reads up to |buf_len| bytes from |device_file_path| into |buf|. Invokes the
93 // appropriate callback asynchronously when complete. Only valid when
94 // IsStreaming() is true.
95 virtual void ReadBytes(const base::FilePath& device_file_path,
96 const scoped_refptr<net::IOBuffer>& buf,
99 const ReadBytesSuccessCallback& success_callback,
100 const ErrorCallback& error_callback) = 0;
103 // (1) Browser application is in shutdown mode (or)
104 // (2) Last extension using this MTP device is destroyed (or)
105 // (3) Attached MTP device is removed (or)
106 // (4) User revoked the MTP device gallery permission.
107 // Ownership of |MTPDeviceAsyncDelegate| is handed off to the delegate
108 // implementation class by this call. This function should take care of
109 // cancelling all the pending tasks before deleting itself.
110 virtual void CancelPendingTasksAndDeleteDelegate() = 0;
113 // Always destruct this object via CancelPendingTasksAndDeleteDelegate().
114 virtual ~MTPDeviceAsyncDelegate() {}
117 typedef base::Callback<void(MTPDeviceAsyncDelegate*)>
118 CreateMTPDeviceAsyncDelegateCallback;
120 void CreateMTPDeviceAsyncDelegate(
121 const base::FilePath::StringType& device_location,
122 const CreateMTPDeviceAsyncDelegateCallback& callback);
124 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_