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.
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MAC_MTP_DEVICE_DELEGATE_IMPL_MAC_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_MAC_MTP_DEVICE_DELEGATE_IMPL_MAC_H_
12 #include "base/containers/hash_tables.h"
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
18 // Delegate for presenting an Image Capture device through the filesystem
19 // API. The synthetic filesystem will be rooted at the constructed location,
20 // and names of all files notified through the ItemAdded call will be
21 // all appear as children of that directory. (ItemAdded calls with directories
23 // Note on thread management: This class is thread-compatible: it can be created
24 // on any thread, but then mutates all state on the UI thread. The async
25 // delegate interface can be invoked on any thread, as it simply forwards calls
27 class MTPDeviceDelegateImplMac
: public MTPDeviceAsyncDelegate
{
29 MTPDeviceDelegateImplMac(const std::string
& device_id
,
30 const base::FilePath::StringType
& synthetic_path
);
32 // MTPDeviceAsyncDelegate implementation. These functions are called on the
33 // IO thread by the async filesystem file util. They forward to
34 // similarly-named methods on the UI thread.
35 void GetFileInfo(const base::FilePath
& file_path
,
36 const GetFileInfoSuccessCallback
& success_callback
,
37 const ErrorCallback
& error_callback
) override
;
39 void CreateDirectory(const base::FilePath
& directory_path
,
42 const CreateDirectorySuccessCallback
& success_callback
,
43 const ErrorCallback
& error_callback
) override
;
45 // Note: passed absolute paths, but expects relative paths in reply.
46 void ReadDirectory(const base::FilePath
& root
,
47 const ReadDirectorySuccessCallback
& success_callback
,
48 const ErrorCallback
& error_callback
) override
;
50 // Note: passed absolute paths.
51 void CreateSnapshotFile(
52 const base::FilePath
& device_file_path
,
53 const base::FilePath
& local_path
,
54 const CreateSnapshotFileSuccessCallback
& success_callback
,
55 const ErrorCallback
& error_callback
) override
;
56 bool IsStreaming() override
;
57 void ReadBytes(const base::FilePath
& device_file_path
,
58 const scoped_refptr
<net::IOBuffer
>& buf
,
61 const ReadBytesSuccessCallback
& success_callback
,
62 const ErrorCallback
& error_callback
) override
;
63 bool IsReadOnly() const override
;
65 const base::FilePath
& source_file_path
,
66 const base::FilePath
& device_file_path
,
67 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
68 const CopyFileProgressCallback
& progress_callback
,
69 const CopyFileLocalSuccessCallback
& success_callback
,
70 const ErrorCallback
& error_callback
) override
;
72 const base::FilePath
& source_file_path
,
73 const base::FilePath
& device_file_path
,
74 const CreateTemporaryFileCallback
& create_temporary_file_callback
,
75 const MoveFileLocalSuccessCallback
& success_callback
,
76 const ErrorCallback
& error_callback
) override
;
77 void CopyFileFromLocal(
78 const base::FilePath
& source_file_path
,
79 const base::FilePath
& device_file_path
,
80 const CopyFileFromLocalSuccessCallback
& success_callback
,
81 const ErrorCallback
& error_callback
) override
;
82 void DeleteFile(const base::FilePath
& file_path
,
83 const DeleteFileSuccessCallback
& success_callback
,
84 const ErrorCallback
& error_callback
) override
;
85 void DeleteDirectory(const base::FilePath
& file_path
,
86 const DeleteDirectorySuccessCallback
& success_callback
,
87 const ErrorCallback
& error_callback
) override
;
88 void AddWatcher(const GURL
& origin
,
89 const base::FilePath
& file_path
,
91 const storage::WatcherManager::StatusCallback
& callback
,
92 const storage::WatcherManager::NotificationCallback
&
93 notification_callback
) override
;
96 const base::FilePath
& file_path
,
98 const storage::WatcherManager::StatusCallback
& callback
) override
;
99 void CancelPendingTasksAndDeleteDelegate() override
;
101 // Forward delegates for ImageCaptureDeviceListener. These are
102 // invoked in callbacks of the ImageCapture library on the UI thread.
103 virtual void ItemAdded(const std::string
& name
,
104 const base::File::Info
& info
);
105 virtual void NoMoreItems();
106 virtual void DownloadedFile(const std::string
& name
,
107 base::File::Error error
);
109 // Scheduled when early directory reads are requested. The
110 // timeout will signal an ABORT error to the caller if the
111 // device metadata cannot be read.
112 void ReadDirectoryTimeout(const base::FilePath
& root
);
115 class DeviceListener
;
117 ~MTPDeviceDelegateImplMac() override
;
119 // Delegate for GetFileInfo, called on the UI thread.
120 void GetFileInfoImpl(const base::FilePath
& file_path
,
121 base::File::Info
* file_info
,
122 base::File::Error
* error
);
124 // Delegate for ReadDirectory, called on the UI thread.
125 void ReadDirectoryImpl(
126 const base::FilePath
& root
,
127 const ReadDirectorySuccessCallback
& success_callback
,
128 const ErrorCallback
& error_callback
);
130 // Delegate for CreateSnapshotFile, called on the UI thread.
132 const base::FilePath
& device_file_path
,
133 const base::FilePath
& local_path
,
134 const CreateSnapshotFileSuccessCallback
& success_callback
,
135 const ErrorCallback
& error_callback
);
137 // Public for closures; should not be called except by
138 // CancelTasksAndDeleteDelegate.
139 void CancelAndDelete();
141 // Cancels any outstanding downloads.
142 void CancelDownloads();
144 // If necessary, notifies the ReadDirectory callback that all data
146 void NotifyReadDir();
148 std::string device_id_
;
149 base::FilePath root_path_
;
151 // Interface object for the camera underlying this MTP session.
152 scoped_ptr
<DeviceListener
> camera_interface_
;
154 // Stores a map from filename to file metadata received from the camera.
155 base::hash_map
<base::FilePath::StringType
,
156 base::File::Info
> file_info_
;
158 // List of filenames received from the camera.
159 std::vector
<base::FilePath
> file_paths_
;
161 // Set to true when all file metadata has been received from the camera.
162 bool received_all_files_
;
164 struct ReadFileRequest
{
166 ReadFileRequest(const std::string
& request_file
,
167 const base::FilePath
& snapshot_filename
,
168 CreateSnapshotFileSuccessCallback success_cb
,
169 ErrorCallback error_cb
);
172 std::string request_file
;
173 base::FilePath snapshot_file
;
174 CreateSnapshotFileSuccessCallback success_callback
;
175 ErrorCallback error_callback
;
178 typedef std::list
<ReadFileRequest
> ReadFileTransactionList
;
180 struct ReadDirectoryRequest
{
181 ReadDirectoryRequest(const base::FilePath
& dir
,
182 ReadDirectorySuccessCallback success_cb
,
183 ErrorCallback error_cb
);
184 ~ReadDirectoryRequest();
186 base::FilePath directory
;
187 ReadDirectorySuccessCallback success_callback
;
188 ErrorCallback error_callback
;
191 typedef std::list
<ReadDirectoryRequest
> ReadDirTransactionList
;
193 ReadFileTransactionList read_file_transactions_
;
194 ReadDirTransactionList read_dir_transactions_
;
196 base::WeakPtrFactory
<MTPDeviceDelegateImplMac
> weak_factory_
;
198 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplMac
);
201 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MAC_MTP_DEVICE_DELEGATE_IMPL_MAC_H_