Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/media_gallery/mtp_device_delegate_impl_mac.h" | |
| 6 | |
| 7 #include "base/memory/scoped_nsobject.h" | |
| 8 #include "base/sequenced_task_runner.h" | |
| 9 #include "base/sequenced_task_runner_helpers.h" | |
| 10 #include "base/threading/sequenced_worker_pool.h" | |
| 11 #include "chrome/browser/media_gallery/mtp_device_delegate_impl.h" | |
| 12 #include "chrome/browser/system_monitor/image_capture_device.h" | |
| 13 #include "chrome/browser/system_monitor/image_capture_device_manager.h" | |
| 14 #include "chrome/browser/system_monitor/media_storage_util.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 | |
| 17 namespace chrome { | |
| 18 | |
| 19 // This class handles the UI-thread hand-offs needed to interface | |
| 20 // with the ImageCapture library. It will forward callbacks to | |
| 21 // its delegate on the task runner with which it is created. All | |
| 22 // interactions with it are done on the UI thread, but it may be | |
| 23 // created/destroyed on another thread. | |
| 24 class CameraDeviceInterface | |
| 25 : public ImageCaptureDeviceListener, | |
| 26 public base::SupportsWeakPtr<CameraDeviceInterface> { | |
| 27 public: | |
| 28 CameraDeviceInterface(MTPDeviceDelegateImplMac* delegate, | |
| 29 base::SequencedTaskRunner* task_runner) | |
| 30 : delegate_(delegate), | |
| 31 task_runner_(task_runner) {} | |
| 32 virtual ~CameraDeviceInterface() {} | |
| 33 | |
| 34 void OpenCameraSession(const std::string& device_id); | |
| 35 void CloseCameraSessionAndDelete(); | |
| 36 | |
| 37 void DownloadFile(const std::string& name, const FilePath& local_path); | |
| 38 | |
| 39 // ImageCaptureDeviceListener | |
| 40 virtual void ItemAdded(const std::string& name, | |
| 41 const base::PlatformFileInfo& info) OVERRIDE; | |
| 42 virtual void NoMoreItems() OVERRIDE; | |
| 43 virtual void DownloadedFile(const std::string& name, | |
| 44 base::PlatformFileError error) OVERRIDE; | |
| 45 virtual void DeviceRemoved() OVERRIDE; | |
| 46 | |
| 47 private: | |
| 48 scoped_nsobject<ImageCaptureDevice> camera_device_; | |
| 49 | |
| 50 // Weak pointer | |
| 51 MTPDeviceDelegateImplMac* delegate_; | |
| 52 | |
| 53 // Weak pointer | |
| 54 base::SequencedTaskRunner* task_runner_; | |
| 55 }; | |
| 56 | |
| 57 void CameraDeviceInterface::OpenCameraSession(const std::string& device_id) { | |
| 58 camera_device_.reset( | |
| 59 [ImageCaptureDeviceManager::deviceForUUID(device_id) retain]); | |
| 60 [camera_device_ setListener:AsWeakPtr()]; | |
| 61 [camera_device_ open]; | |
| 62 } | |
| 63 | |
| 64 void CameraDeviceInterface::CloseCameraSessionAndDelete() { | |
| 65 if (camera_device_.get()) { | |
| 66 [camera_device_ close]; | |
| 67 [camera_device_ setListener:base::WeakPtr<CameraDeviceInterface>()]; | |
| 68 } | |
| 69 | |
| 70 delete this; | |
| 71 } | |
| 72 | |
| 73 void CameraDeviceInterface::DownloadFile(const std::string& name, | |
| 74 const FilePath& local_path) { | |
| 75 [camera_device_ downloadFile:name localPath:local_path]; | |
| 76 } | |
| 77 | |
| 78 void CameraDeviceInterface::ItemAdded(const std::string& name, | |
| 79 const base::PlatformFileInfo& info) { | |
| 80 task_runner_->PostTask(FROM_HERE, | |
| 81 base::Bind(&MTPDeviceDelegateImplMac::ItemAdded, | |
| 82 base::Unretained(delegate_), name, info)); | |
| 83 } | |
| 84 | |
| 85 void CameraDeviceInterface::NoMoreItems() { | |
| 86 task_runner_->PostTask(FROM_HERE, | |
| 87 base::Bind(&MTPDeviceDelegateImplMac::NoMoreItems, | |
| 88 base::Unretained(delegate_))); | |
| 89 } | |
| 90 | |
| 91 void CameraDeviceInterface::DownloadedFile(const std::string& name, | |
| 92 base::PlatformFileError error) { | |
| 93 task_runner_->PostTask(FROM_HERE, | |
| 94 base::Bind(&MTPDeviceDelegateImplMac::DownloadedFile, | |
| 95 base::Unretained(delegate_), name, error)); | |
| 96 } | |
| 97 | |
| 98 void CameraDeviceInterface::DeviceRemoved() { | |
| 99 [camera_device_ close]; | |
| 100 camera_device_.reset(); | |
| 101 } | |
| 102 | |
| 103 MTPDeviceDelegateImplMac::MTPDeviceDelegateImplMac( | |
| 104 const FilePath::StringType& location, | |
| 105 base::SequencedTaskRunner* media_task_runner) | |
| 106 : media_task_runner_(media_task_runner), | |
| 107 received_all_files_(false) { | |
| 108 std::string device_id = FilePath(location).BaseName().value(); | |
| 109 MediaStorageUtil::Type type; | |
| 110 DCHECK(MediaStorageUtil::CrackDeviceId(device_id, &type, &device_id_)); | |
|
Lei Zhang
2012/12/21 21:05:44
You won't get a |device_id_| in a release build.
Greg Billock
2012/12/22 00:10:28
I'm not seeing this. It looks like it's just gover
Lei Zhang
2012/12/22 00:18:30
What I'm trying to say is, this line won't get exe
Greg Billock
2012/12/22 03:11:41
Oh, duh. Of course. Fixing.
On 2012/12/22 00:18:3
| |
| 111 root_path_ = FilePath(location); | |
|
Lei Zhang
2012/12/21 21:05:44
Do this in the initializer list.
Greg Billock
2012/12/22 00:10:28
Done.
| |
| 112 DCHECK_EQ(MediaStorageUtil::MAC_IMAGE_CAPTURE, type); | |
| 113 | |
| 114 // Make a synthetic entry for the root of the filesystem. | |
| 115 base::PlatformFileInfo info; | |
| 116 info.is_directory = true; | |
| 117 file_info_[root_path_.value()] = info; | |
| 118 | |
| 119 camera_interface_.reset(new CameraDeviceInterface(this, media_task_runner)); | |
| 120 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 121 base::Bind(&CameraDeviceInterface::OpenCameraSession, | |
| 122 base::Unretained(camera_interface_.get()), | |
| 123 device_id_)); | |
| 124 } | |
| 125 | |
| 126 MTPDeviceDelegateImplMac::~MTPDeviceDelegateImplMac() { | |
| 127 DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); | |
| 128 DCHECK(enumerators_.size() == 0); | |
| 129 } | |
| 130 | |
| 131 base::PlatformFileError MTPDeviceDelegateImplMac::GetFileInfo( | |
| 132 const FilePath& file_path, | |
| 133 base::PlatformFileInfo* file_info) { | |
| 134 base::hash_map<FilePath::StringType, | |
| 135 base::PlatformFileInfo>::const_iterator i = | |
| 136 file_info_.find(file_path.value()); | |
| 137 if (i != file_info_.end()) { | |
| 138 file_info->size = i->second.size; | |
|
Lei Zhang
2012/12/21 21:05:44
Isn't this block the same as: *file_info = i->sec
| |
| 139 file_info->is_directory = i->second.is_directory; | |
| 140 file_info->is_symbolic_link = i->second.is_symbolic_link; | |
| 141 file_info->last_modified = i->second.last_modified; | |
| 142 file_info->last_accessed = i->second.last_accessed; | |
| 143 file_info->creation_time = i->second.creation_time; | |
| 144 return base::PLATFORM_FILE_OK; | |
| 145 } else { | |
| 146 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> | |
| 151 MTPDeviceDelegateImplMac::CreateFileEnumerator(const FilePath& root, | |
| 152 bool recursive) { | |
|
Lei Zhang
2012/12/21 21:05:44
You need to handle the recursive flag.
Greg Billock
2012/12/22 00:10:28
My synthetic directory has all files in the same d
Lei Zhang
2012/12/22 00:18:30
Oh, is there no way to get the directory structure
Greg Billock
2012/12/22 03:11:41
I think you can in theory -- if there's a USB and
Lei Zhang
2013/01/02 23:46:54
If we can have a directory structure for data from
Greg Billock
2013/01/04 18:05:05
I think PTP devices will report everything flatten
Lei Zhang
2013/01/04 20:28:19
It's true for some PTP devices, but most devices I
Greg Billock
2013/01/08 16:59:36
They have a DCIM folder with all the media flat in
| |
| 153 Enumerator* enumerator = new Enumerator(this); | |
| 154 enumerators_.push_back(enumerator); | |
| 155 return make_scoped_ptr(enumerator) | |
| 156 .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>(); | |
| 157 } | |
| 158 | |
| 159 base::PlatformFileError MTPDeviceDelegateImplMac::CreateSnapshotFile( | |
| 160 const FilePath& device_file_path, | |
| 161 const FilePath& local_path, | |
| 162 base::PlatformFileInfo* file_info) { | |
| 163 std::string name = device_file_path.BaseName().value(); | |
| 164 base::PlatformFileError error = GetFileInfo(device_file_path, file_info); | |
| 165 if (error != base::PLATFORM_FILE_OK) | |
| 166 return error; | |
| 167 | |
| 168 // Set up to wait for download. | |
| 169 base::WaitableEvent waiter(true, false); | |
| 170 download_events_[name] = &waiter; | |
| 171 // Start the download in the UI thread. | |
| 172 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 173 base::Bind(&CameraDeviceInterface::DownloadFile, | |
| 174 base::Unretained(camera_interface_.get()), | |
| 175 name, local_path)); | |
| 176 waiter.Wait(); | |
| 177 download_events_.erase(download_events_.find(name)); | |
| 178 base::hash_map<std::string, base::PlatformFileError>::iterator iter = | |
| 179 download_errors_.find(name); | |
| 180 error = iter->second; | |
| 181 download_errors_.erase(iter); | |
| 182 | |
| 183 // Modify the last modified time to null. This prevents the time stamp | |
| 184 // verification in LocalFileStreamReader. | |
| 185 file_info->last_modified = base::Time(); | |
| 186 | |
| 187 return error; | |
| 188 } | |
| 189 | |
| 190 void MTPDeviceDelegateImplMac::CancelPendingTasksAndDeleteDelegate() { | |
| 191 // Artificially pretend that we have already gotten all items we're going | |
| 192 // to get. | |
| 193 NoMoreItems(); | |
| 194 | |
| 195 // Artificially wake up any downloads pending with an error code. | |
| 196 for (base::hash_map<std::string, base::WaitableEvent*>::const_iterator iter = | |
| 197 download_events_.begin(); iter != download_events_.end(); ++iter) { | |
| 198 download_errors_[iter->first] = base::PLATFORM_FILE_ERROR_FAILED; | |
| 199 iter->second->Signal(); | |
| 200 } | |
| 201 | |
| 202 // Schedule the camera session to be closed and the interface deleted. | |
| 203 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 204 base::Bind(&CameraDeviceInterface::CloseCameraSessionAndDelete, | |
| 205 base::Unretained(camera_interface_.release()))); | |
| 206 | |
| 207 media_task_runner_->PostTask( | |
| 208 FROM_HERE, | |
| 209 base::Bind(&MTPDeviceDelegateImplMac::DeleteDelegateOnTaskRunner, | |
|
Lei Zhang
2012/12/21 21:05:44
You don't need DeleteDelegateOnTaskRunner.
media_
Greg Billock
2012/12/22 00:10:28
Done.
| |
| 210 base::Unretained(this))); | |
| 211 } | |
| 212 | |
| 213 void MTPDeviceDelegateImplMac::DeleteDelegateOnTaskRunner() { | |
| 214 delete this; | |
| 215 } | |
| 216 | |
| 217 void MTPDeviceDelegateImplMac::ItemAdded( | |
| 218 const std::string& name, const base::PlatformFileInfo& info) { | |
| 219 // Make sure if we're canceled and enumerators are awake, that | |
| 220 // they will stay consistent. May need to revisit this if we need | |
| 221 // notifications of files added after we think we're done. | |
| 222 if (received_all_files_) | |
| 223 return; | |
| 224 | |
| 225 if (info.is_directory) | |
| 226 return; | |
| 227 FilePath fp = root_path_.Append(name); | |
| 228 file_info_[fp.value()] = info; | |
| 229 file_paths_.push_back(fp.value()); | |
| 230 | |
| 231 for (std::vector<Enumerator*>::const_iterator iter = enumerators_.begin(); | |
| 232 iter != enumerators_.end(); | |
| 233 ++iter) { | |
| 234 (*iter)->ItemsChanged(); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 void MTPDeviceDelegateImplMac::NoMoreItems() { | |
| 239 received_all_files_ = true; | |
| 240 | |
| 241 for (std::vector<Enumerator*>::const_iterator iter = enumerators_.begin(); | |
| 242 iter != enumerators_.end(); | |
| 243 ++iter) { | |
| 244 (*iter)->ItemsChanged(); | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 void MTPDeviceDelegateImplMac::DownloadedFile( | |
| 249 const std::string& name, base::PlatformFileError error) { | |
| 250 // If we're cancelled and deleting, we have already signaled all enumerators. | |
| 251 if (!camera_interface_.get()) | |
| 252 return; | |
| 253 | |
| 254 LOG(INFO) << "Downloaded file " << name; | |
|
Lei Zhang
2012/12/21 21:05:44
debug statement
Greg Billock
2012/12/22 00:10:28
Done.
| |
| 255 download_errors_[name] = error; | |
| 256 download_events_[name]->Signal(); | |
| 257 } | |
| 258 | |
| 259 FilePath MTPDeviceDelegateImplMac::GetFile(int index) { | |
| 260 if (index >= static_cast<int>(file_paths_.size())) | |
|
Lei Zhang
2012/12/21 21:05:44
Make GetFile take a size_t. Make Enumerator::posit
Greg Billock
2012/12/22 00:10:28
Done.
| |
| 261 return FilePath(); | |
| 262 else | |
| 263 return FilePath(file_paths_[index]); | |
| 264 } | |
| 265 | |
| 266 bool MTPDeviceDelegateImplMac::HasAllFiles() { | |
| 267 return received_all_files_; | |
| 268 } | |
| 269 | |
| 270 void MTPDeviceDelegateImplMac::RemoveEnumerator(Enumerator* enumerator) { | |
| 271 for (std::vector<Enumerator*>::iterator iter = enumerators_.begin(); | |
| 272 iter != enumerators_.end(); | |
| 273 ++iter) { | |
| 274 if (*iter == enumerator) { | |
| 275 enumerators_.erase(iter); | |
| 276 break; | |
| 277 } | |
| 278 } | |
| 279 } | |
| 280 | |
| 281 MTPDeviceDelegateImplMac::Enumerator::Enumerator( | |
| 282 MTPDeviceDelegateImplMac* delegate) | |
| 283 : delegate_(delegate), | |
| 284 position_(0), | |
| 285 wait_for_items_(false, false) {} | |
| 286 | |
| 287 MTPDeviceDelegateImplMac::Enumerator::~Enumerator() { | |
| 288 delegate_->RemoveEnumerator(this); | |
| 289 } | |
| 290 | |
| 291 FilePath MTPDeviceDelegateImplMac::Enumerator::Next() { | |
| 292 FilePath fp = delegate_->GetFile(position_); | |
| 293 while (fp.empty() && !delegate_->HasAllFiles()) { | |
| 294 wait_for_items_.Wait(); | |
| 295 fp = delegate_->GetFile(position_); | |
| 296 } | |
| 297 | |
| 298 position_++; | |
| 299 return fp; | |
| 300 } | |
| 301 | |
| 302 int64 MTPDeviceDelegateImplMac::Enumerator::Size() { | |
| 303 FilePath fp = delegate_->GetFile(position_ - 1); | |
| 304 base::PlatformFileInfo info; | |
| 305 delegate_->GetFileInfo(fp, &info); | |
| 306 return info.size; | |
| 307 return 0; | |
| 308 } | |
| 309 | |
| 310 base::Time MTPDeviceDelegateImplMac::Enumerator::LastModifiedTime() { | |
| 311 FilePath fp = delegate_->GetFile(position_ - 1); | |
| 312 base::PlatformFileInfo info; | |
| 313 delegate_->GetFileInfo(fp, &info); | |
| 314 return info.last_modified; | |
| 315 } | |
| 316 | |
| 317 bool MTPDeviceDelegateImplMac::Enumerator::IsDirectory() { | |
| 318 FilePath fp = delegate_->GetFile(position_ - 1); | |
| 319 base::PlatformFileInfo info; | |
| 320 delegate_->GetFileInfo(fp, &info); | |
| 321 return info.is_directory; | |
| 322 } | |
| 323 | |
| 324 void MTPDeviceDelegateImplMac::Enumerator::ItemsChanged() { | |
| 325 wait_for_items_.Signal(); | |
| 326 } | |
| 327 | |
| 328 void CreateMTPDeviceDelegate(const std::string& device_location, | |
| 329 base::SequencedTaskRunner* media_task_runner, | |
| 330 const CreateMTPDeviceDelegateCallback& cb) { | |
| 331 cb.Run(new MTPDeviceDelegateImplMac(device_location, media_task_runner)); | |
| 332 } | |
| 333 | |
| 334 } // namespace chrome | |
| 335 | |
| OLD | NEW |