Re-land: C++ readability review
[chromium-blink-merge.git] / chrome / browser / media_galleries / mac / mtp_device_delegate_impl_mac.h
blobd01874c73e1f23d2e39be490cc45c01f405bad25
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_
8 #include <list>
9 #include <map>
10 #include <vector>
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
22 // will be ignored.)
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
26 // to the UI thread.
27 class MTPDeviceDelegateImplMac : public MTPDeviceAsyncDelegate {
28 public:
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,
40 const bool exclusive,
41 const bool recursive,
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,
59 int64 offset,
60 int buf_len,
61 const ReadBytesSuccessCallback& success_callback,
62 const ErrorCallback& error_callback) override;
63 bool IsReadOnly() const override;
64 void CopyFileLocal(
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;
71 void MoveFileLocal(
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 CancelPendingTasksAndDeleteDelegate() override;
90 // Forward delegates for ImageCaptureDeviceListener. These are
91 // invoked in callbacks of the ImageCapture library on the UI thread.
92 virtual void ItemAdded(const std::string& name,
93 const base::File::Info& info);
94 virtual void NoMoreItems();
95 virtual void DownloadedFile(const std::string& name,
96 base::File::Error error);
98 // Scheduled when early directory reads are requested. The
99 // timeout will signal an ABORT error to the caller if the
100 // device metadata cannot be read.
101 void ReadDirectoryTimeout(const base::FilePath& root);
103 private:
104 class DeviceListener;
106 ~MTPDeviceDelegateImplMac() override;
108 // Delegate for GetFileInfo, called on the UI thread.
109 void GetFileInfoImpl(const base::FilePath& file_path,
110 base::File::Info* file_info,
111 base::File::Error* error);
113 // Delegate for ReadDirectory, called on the UI thread.
114 void ReadDirectoryImpl(
115 const base::FilePath& root,
116 const ReadDirectorySuccessCallback& success_callback,
117 const ErrorCallback& error_callback);
119 // Delegate for CreateSnapshotFile, called on the UI thread.
120 void DownloadFile(
121 const base::FilePath& device_file_path,
122 const base::FilePath& local_path,
123 const CreateSnapshotFileSuccessCallback& success_callback,
124 const ErrorCallback& error_callback);
126 // Public for closures; should not be called except by
127 // CancelTasksAndDeleteDelegate.
128 void CancelAndDelete();
130 // Cancels any outstanding downloads.
131 void CancelDownloads();
133 // If necessary, notifies the ReadDirectory callback that all data
134 // has been read.
135 void NotifyReadDir();
137 std::string device_id_;
138 base::FilePath root_path_;
140 // Interface object for the camera underlying this MTP session.
141 scoped_ptr<DeviceListener> camera_interface_;
143 // Stores a map from filename to file metadata received from the camera.
144 base::hash_map<base::FilePath::StringType,
145 base::File::Info> file_info_;
147 // List of filenames received from the camera.
148 std::vector<base::FilePath> file_paths_;
150 // Set to true when all file metadata has been received from the camera.
151 bool received_all_files_;
153 struct ReadFileRequest {
154 ReadFileRequest();
155 ReadFileRequest(const std::string& request_file,
156 const base::FilePath& snapshot_filename,
157 CreateSnapshotFileSuccessCallback success_cb,
158 ErrorCallback error_cb);
159 ~ReadFileRequest();
161 std::string request_file;
162 base::FilePath snapshot_file;
163 CreateSnapshotFileSuccessCallback success_callback;
164 ErrorCallback error_callback;
167 typedef std::list<ReadFileRequest> ReadFileTransactionList;
169 struct ReadDirectoryRequest {
170 ReadDirectoryRequest(const base::FilePath& dir,
171 ReadDirectorySuccessCallback success_cb,
172 ErrorCallback error_cb);
173 ~ReadDirectoryRequest();
175 base::FilePath directory;
176 ReadDirectorySuccessCallback success_callback;
177 ErrorCallback error_callback;
180 typedef std::list<ReadDirectoryRequest> ReadDirTransactionList;
182 ReadFileTransactionList read_file_transactions_;
183 ReadDirTransactionList read_dir_transactions_;
185 base::WeakPtrFactory<MTPDeviceDelegateImplMac> weak_factory_;
187 DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplMac);
190 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MAC_MTP_DEVICE_DELEGATE_IMPL_MAC_H_