Chromium Code Reviews| Index: chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac.mm |
| diff --git a/chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac.mm b/chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aeb17a131e55a1145f96574dbeff2044261d1df5 |
| --- /dev/null |
| +++ b/chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac.mm |
| @@ -0,0 +1,328 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac.h" |
| + |
| +#include "base/memory/scoped_nsobject.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "base/sequenced_task_runner_helpers.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "chrome/browser/media_gallery/mtp_device_delegate_impl.h" |
| +#include "chrome/browser/system_monitor/image_capture_device.h" |
| +#include "chrome/browser/system_monitor/image_capture_device_manager.h" |
| +#include "chrome/browser/system_monitor/media_storage_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace chrome { |
| + |
| +// This class handles the UI-thread hand-offs needed to interface |
| +// with the ImageCapture library. It will forward callbacks to |
| +// its delegate on the task runner with which it is created. All |
| +// interactions with it are done on the UI thread, but it may be |
| +// created/destroyed on another thread. |
| +class CameraDeviceInterface |
| + : public ImageCaptureDeviceListener, |
| + public base::SupportsWeakPtr<CameraDeviceInterface> { |
| + public: |
| + CameraDeviceInterface(MTPDeviceDelegateImplMac* delegate, |
| + base::SequencedTaskRunner* task_runner) |
| + : delegate_(delegate), |
| + task_runner_(task_runner) {} |
| + virtual ~CameraDeviceInterface() {} |
|
Lei Zhang
2012/12/22 02:44:35
Should this be private since CloseCameraSessionAnd
Greg Billock
2012/12/22 03:11:41
I had it like that, but then TaskRunner::DeleteSoo
Lei Zhang
2013/01/02 23:46:54
You just need to add: friend class base::DeleteHel
Greg Billock
2013/01/03 22:41:32
I think I must have switched this around. It's in
|
| + |
| + void OpenCameraSession(const std::string& device_id); |
| + void CloseCameraSessionAndDelete(); |
| + |
| + void DownloadFile(const std::string& name, const FilePath& local_path); |
| + |
| + // ImageCaptureDeviceListener |
| + virtual void ItemAdded(const std::string& name, |
| + const base::PlatformFileInfo& info) OVERRIDE; |
| + virtual void NoMoreItems() OVERRIDE; |
| + virtual void DownloadedFile(const std::string& name, |
| + base::PlatformFileError error) OVERRIDE; |
| + virtual void DeviceRemoved() OVERRIDE; |
| + |
| + private: |
| + scoped_nsobject<ImageCaptureDevice> camera_device_; |
| + |
| + // Weak pointer |
| + MTPDeviceDelegateImplMac* delegate_; |
| + |
| + // Weak pointer |
| + base::SequencedTaskRunner* task_runner_; |
| +}; |
| + |
| +void CameraDeviceInterface::OpenCameraSession(const std::string& device_id) { |
| + camera_device_.reset( |
| + [ImageCaptureDeviceManager::deviceForUUID(device_id) retain]); |
| + [camera_device_ setListener:AsWeakPtr()]; |
| + [camera_device_ open]; |
| +} |
| + |
| +void CameraDeviceInterface::CloseCameraSessionAndDelete() { |
| + if (camera_device_.get()) { |
| + [camera_device_ close]; |
| + [camera_device_ setListener:base::WeakPtr<CameraDeviceInterface>()]; |
| + } |
| + |
| + delete this; |
| +} |
| + |
| +void CameraDeviceInterface::DownloadFile(const std::string& name, |
| + const FilePath& local_path) { |
| + [camera_device_ downloadFile:name localPath:local_path]; |
| +} |
| + |
| +void CameraDeviceInterface::ItemAdded(const std::string& name, |
| + const base::PlatformFileInfo& info) { |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&MTPDeviceDelegateImplMac::ItemAdded, |
| + base::Unretained(delegate_), name, info)); |
| +} |
| + |
| +void CameraDeviceInterface::NoMoreItems() { |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&MTPDeviceDelegateImplMac::NoMoreItems, |
| + base::Unretained(delegate_))); |
| +} |
| + |
| +void CameraDeviceInterface::DownloadedFile(const std::string& name, |
| + base::PlatformFileError error) { |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&MTPDeviceDelegateImplMac::DownloadedFile, |
| + base::Unretained(delegate_), name, error)); |
| +} |
| + |
| +void CameraDeviceInterface::DeviceRemoved() { |
| + [camera_device_ close]; |
| + camera_device_.reset(); |
| +} |
| + |
| +MTPDeviceDelegateImplMac::MTPDeviceDelegateImplMac( |
| + const FilePath::StringType& location, |
| + base::SequencedTaskRunner* media_task_runner) |
| + : root_path_(location), |
| + media_task_runner_(media_task_runner), |
| + received_all_files_(false) { |
| + std::string device_id = FilePath(location).BaseName().value(); |
| + MediaStorageUtil::Type type; |
| +// !!! Parse this manually |
| + DCHECK(MediaStorageUtil::CrackDeviceId(device_id, &type, &device_id_)); |
| + DCHECK_EQ(MediaStorageUtil::MAC_IMAGE_CAPTURE, type); |
| + |
| + // Make a synthetic entry for the root of the filesystem. |
| + base::PlatformFileInfo info; |
| + info.is_directory = true; |
| + file_info_[root_path_.value()] = info; |
| + |
| + camera_interface_.reset(new CameraDeviceInterface(this, media_task_runner)); |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&CameraDeviceInterface::OpenCameraSession, |
| + base::Unretained(camera_interface_.get()), |
| + device_id_)); |
| +} |
| + |
| +MTPDeviceDelegateImplMac::~MTPDeviceDelegateImplMac() { |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + DCHECK(enumerators_.size() == 0); |
| +} |
| + |
| +base::PlatformFileError MTPDeviceDelegateImplMac::GetFileInfo( |
| + const FilePath& file_path, |
| + base::PlatformFileInfo* file_info) { |
| + base::hash_map<FilePath::StringType, |
| + base::PlatformFileInfo>::const_iterator i = |
| + file_info_.find(file_path.value()); |
| + if (i != file_info_.end()) { |
| + file_info->size = i->second.size; |
| + file_info->is_directory = i->second.is_directory; |
| + file_info->is_symbolic_link = i->second.is_symbolic_link; |
| + file_info->last_modified = i->second.last_modified; |
| + file_info->last_accessed = i->second.last_accessed; |
| + file_info->creation_time = i->second.creation_time; |
| + return base::PLATFORM_FILE_OK; |
| + } else { |
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| + } |
| +} |
| + |
| +scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> |
| +MTPDeviceDelegateImplMac::CreateFileEnumerator(const FilePath& root, |
| + bool recursive) { |
| + Enumerator* enumerator = new Enumerator(this); |
| + enumerators_.push_back(enumerator); |
| + return make_scoped_ptr(enumerator) |
| + .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>(); |
| +} |
| + |
| +base::PlatformFileError MTPDeviceDelegateImplMac::CreateSnapshotFile( |
| + const FilePath& device_file_path, |
| + const FilePath& local_path, |
| + base::PlatformFileInfo* file_info) { |
| + std::string name = device_file_path.BaseName().value(); |
| + base::PlatformFileError error = GetFileInfo(device_file_path, file_info); |
| + if (error != base::PLATFORM_FILE_OK) |
| + return error; |
| + |
| + // Set up to wait for download. |
| + base::WaitableEvent waiter(true, false); |
| + download_events_[name] = &waiter; |
|
Lei Zhang
2012/12/22 02:44:35
You got |download_events_| here, but can there eve
Greg Billock
2012/12/22 03:11:41
I meant to ask you this. Is this always called in
Lei Zhang
2013/01/02 23:46:54
I believe that to be the case.
Greg Billock
2013/01/03 22:41:32
OK. Sweetened this a bit.
|
| + // Start the download in the UI thread. |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&CameraDeviceInterface::DownloadFile, |
| + base::Unretained(camera_interface_.get()), |
| + name, local_path)); |
| + waiter.Wait(); |
| + download_events_.erase(download_events_.find(name)); |
| + base::hash_map<std::string, base::PlatformFileError>::iterator iter = |
| + download_errors_.find(name); |
| + error = iter->second; |
| + download_errors_.erase(iter); |
| + |
| + // Modify the last modified time to null. This prevents the time stamp |
| + // verification in LocalFileStreamReader. |
| + file_info->last_modified = base::Time(); |
| + |
| + return error; |
| +} |
| + |
| +void MTPDeviceDelegateImplMac::CancelPendingTasksAndDeleteDelegate() { |
| + // Artificially pretend that we have already gotten all items we're going |
| + // to get. |
| + NoMoreItems(); |
| + |
| + // Artificially wake up any downloads pending with an error code. |
| + for (base::hash_map<std::string, base::WaitableEvent*>::const_iterator iter = |
| + download_events_.begin(); iter != download_events_.end(); ++iter) { |
| + download_errors_[iter->first] = base::PLATFORM_FILE_ERROR_FAILED; |
| + iter->second->Signal(); |
| + } |
| + |
| + // Schedule the camera session to be closed and the interface deleted. |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&CameraDeviceInterface::CloseCameraSessionAndDelete, |
| + base::Unretained(camera_interface_.release()))); |
| + |
| + media_task_runner_->DeleteSoon(FROM_HERE, this); |
| +} |
| + |
| +void MTPDeviceDelegateImplMac::ItemAdded( |
| + const std::string& name, const base::PlatformFileInfo& info) { |
| + // Make sure if we're canceled and enumerators are awake, that |
| + // they will stay consistent. May need to revisit this if we need |
| + // notifications of files added after we think we're done. |
| + if (received_all_files_) |
| + return; |
| + |
| + if (info.is_directory) |
| + return; |
| + FilePath fp = root_path_.Append(name); |
| + file_info_[fp.value()] = info; |
| + file_paths_.push_back(fp.value()); |
| + |
| + for (std::vector<Enumerator*>::const_iterator iter = enumerators_.begin(); |
| + iter != enumerators_.end(); |
| + ++iter) { |
| + (*iter)->ItemsChanged(); |
| + } |
| +} |
| + |
| +void MTPDeviceDelegateImplMac::NoMoreItems() { |
| + received_all_files_ = true; |
| + |
| + for (std::vector<Enumerator*>::const_iterator iter = enumerators_.begin(); |
| + iter != enumerators_.end(); |
| + ++iter) { |
| + (*iter)->ItemsChanged(); |
| + } |
| +} |
| + |
| +void MTPDeviceDelegateImplMac::DownloadedFile( |
| + const std::string& name, base::PlatformFileError error) { |
| + // If we're cancelled and deleting, we have already signaled all enumerators. |
| + if (!camera_interface_.get()) |
| + return; |
| + |
| + download_errors_[name] = error; |
| + download_events_[name]->Signal(); |
| +} |
| + |
| +FilePath MTPDeviceDelegateImplMac::GetFile(size_t index) { |
| + if (index >= file_paths_.size()) |
| + return FilePath(); |
| + else |
| + return FilePath(file_paths_[index]); |
| +} |
| + |
| +bool MTPDeviceDelegateImplMac::HasAllFiles() { |
| + return received_all_files_; |
| +} |
| + |
| +void MTPDeviceDelegateImplMac::RemoveEnumerator(Enumerator* enumerator) { |
| + for (std::vector<Enumerator*>::iterator iter = enumerators_.begin(); |
| + iter != enumerators_.end(); |
| + ++iter) { |
| + if (*iter == enumerator) { |
| + enumerators_.erase(iter); |
| + break; |
| + } |
| + } |
| +} |
| + |
| +MTPDeviceDelegateImplMac::Enumerator::Enumerator( |
| + MTPDeviceDelegateImplMac* delegate) |
| + : delegate_(delegate), |
| + position_(0), |
| + wait_for_items_(false, false) {} |
| + |
| +MTPDeviceDelegateImplMac::Enumerator::~Enumerator() { |
| + delegate_->RemoveEnumerator(this); |
| +} |
| + |
| +FilePath MTPDeviceDelegateImplMac::Enumerator::Next() { |
| + FilePath fp = delegate_->GetFile(position_); |
| + while (fp.empty() && !delegate_->HasAllFiles()) { |
| + wait_for_items_.Wait(); |
| + fp = delegate_->GetFile(position_); |
| + } |
| + |
| + position_++; |
| + return fp; |
| +} |
| + |
| +int64 MTPDeviceDelegateImplMac::Enumerator::Size() { |
| + FilePath fp = delegate_->GetFile(position_ - 1); |
| + base::PlatformFileInfo info; |
| + delegate_->GetFileInfo(fp, &info); |
| + return info.size; |
| + return 0; |
| +} |
| + |
| +base::Time MTPDeviceDelegateImplMac::Enumerator::LastModifiedTime() { |
| + FilePath fp = delegate_->GetFile(position_ - 1); |
| + base::PlatformFileInfo info; |
| + delegate_->GetFileInfo(fp, &info); |
| + return info.last_modified; |
| +} |
| + |
| +bool MTPDeviceDelegateImplMac::Enumerator::IsDirectory() { |
| + FilePath fp = delegate_->GetFile(position_ - 1); |
| + base::PlatformFileInfo info; |
| + delegate_->GetFileInfo(fp, &info); |
| + return info.is_directory; |
| +} |
| + |
| +void MTPDeviceDelegateImplMac::Enumerator::ItemsChanged() { |
| + wait_for_items_.Signal(); |
| +} |
| + |
| +void CreateMTPDeviceDelegate(const std::string& device_location, |
| + base::SequencedTaskRunner* media_task_runner, |
| + const CreateMTPDeviceDelegateCallback& cb) { |
| + cb.Run(new MTPDeviceDelegateImplMac(device_location, media_task_runner)); |
| +} |
| + |
| +} // namespace chrome |
| + |