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. 4 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_ 6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_ 7 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 13 namespace base { 14 class FilePath; 15 } 16 17 namespace net { 18 class IOBuffer; 19 } 20 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 { 27 public: 28 // A callback to be called when GetFileInfo method call succeeds. 29 typedef base::Callback< 30 void(const base::File::Info& file_info)> GetFileInfoSuccessCallback; 31 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; 36 37 // A callback to be called when GetFileInfo/ReadDirectory/CreateSnapshot 38 // method call fails. 39 typedef base::Callback<void(base::File::Error error)> ErrorCallback; 40 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; 45 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; 50 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); 56 ~ReadBytesRequest(); 57 58 uint32 file_id; 59 scoped_refptr<net::IOBuffer> buf; 60 int64 offset; 61 int buf_len; 62 ReadBytesSuccessCallback success_callback; 63 ErrorCallback error_callback; 64 }; 65 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; 72 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; 79 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; 87 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; 91 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, 97 int64 offset, 98 int buf_len, 99 const ReadBytesSuccessCallback& success_callback, 100 const ErrorCallback& error_callback) = 0; 101 102 // Called when the 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; 111 112 protected: 113 // Always destruct this object via CancelPendingTasksAndDeleteDelegate(). ~MTPDeviceAsyncDelegate()114 virtual ~MTPDeviceAsyncDelegate() {} 115 }; 116 117 typedef base::Callback<void(MTPDeviceAsyncDelegate*)> 118 CreateMTPDeviceAsyncDelegateCallback; 119 120 void CreateMTPDeviceAsyncDelegate( 121 const base::FilePath::StringType& device_location, 122 const CreateMTPDeviceAsyncDelegateCallback& callback); 123 124 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_ASYNC_DELEGATE_H_ 125