Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6440)

Unified Diff: chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac.mm

Issue 11416089: [Media Galleries] Filesystem interface for Mac PTP/MTP devices using ImageCaptureCore (part 3) (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Review fixes Created 7 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3070c11cf304a671b8bedcde7534c16f556b0d17
--- /dev/null
+++ b/chrome/browser/media_gallery/mac/mtp_device_delegate_impl_mac.mm
@@ -0,0 +1,305 @@
+// 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 DeviceListenerImpl
+ : public ImageCaptureDeviceListener,
+ public base::SupportsWeakPtr<DeviceListenerImpl> {
+ public:
+ DeviceListenerImpl(MTPDeviceDelegateImplMac* delegate,
+ base::SequencedTaskRunner* task_runner)
+ : delegate_(delegate),
+ task_runner_(task_runner) {}
+ virtual ~DeviceListenerImpl() {}
+
+ 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_;
+};
sail 2013/01/03 23:10:43 DISALLOW_COPY_AND_ASSIGN
Greg Billock 2013/01/04 18:05:05 Done.
+
+void DeviceListenerImpl::OpenCameraSession(const std::string& device_id) {
+ camera_device_.reset(
+ [ImageCaptureDeviceManager::deviceForUUID(device_id) retain]);
+ [camera_device_ setListener:AsWeakPtr()];
+ [camera_device_ open];
+}
+
+void DeviceListenerImpl::CloseCameraSessionAndDelete() {
+ [camera_device_ close];
+ [camera_device_ setListener:base::WeakPtr<DeviceListenerImpl>()];
+
+ delete this;
+}
+
+void DeviceListenerImpl::DownloadFile(const std::string& name,
+ const FilePath& local_path) {
+ [camera_device_ downloadFile:name localPath:local_path];
+}
+
+void DeviceListenerImpl::ItemAdded(const std::string& name,
+ const base::PlatformFileInfo& info) {
+ task_runner_->PostTask(FROM_HERE,
+ base::Bind(&MTPDeviceDelegateImplMac::ItemAdded,
+ base::Unretained(delegate_), name, info));
+}
+
+void DeviceListenerImpl::NoMoreItems() {
+ task_runner_->PostTask(FROM_HERE,
+ base::Bind(&MTPDeviceDelegateImplMac::NoMoreItems,
+ base::Unretained(delegate_)));
+}
+
+void DeviceListenerImpl::DownloadedFile(const std::string& name,
+ base::PlatformFileError error) {
+ task_runner_->PostTask(FROM_HERE,
+ base::Bind(&MTPDeviceDelegateImplMac::DownloadedFile,
+ base::Unretained(delegate_), name, error));
+}
+
+void DeviceListenerImpl::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),
+ enumerator_(NULL),
+ received_all_files_(false) {
+ std::string device_id = FilePath(location).BaseName().value();
+ MediaStorageUtil::Type type;
+ bool cracked = MediaStorageUtil::CrackDeviceId(device_id, &type, &device_id_);
+ DCHECK(cracked);
+ 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 DeviceListenerImpl(this, media_task_runner));
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&DeviceListenerImpl::OpenCameraSession,
+ base::Unretained(camera_interface_.get()),
+ device_id_));
+}
+
+MTPDeviceDelegateImplMac::~MTPDeviceDelegateImplMac() {
+ DCHECK(media_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(enumerator_ == NULL);
+}
+
+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())
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND;
+
+ *file_info = i->second;
+ return base::PLATFORM_FILE_OK;
+}
+
+scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator>
+MTPDeviceDelegateImplMac::CreateFileEnumerator(const FilePath& root,
+ bool recursive) {
+ DCHECK(!enumerator_);
+ enumerator_ = new Enumerator(this);
+ 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) {
+ 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);
+ DCHECK(file_download_event_ == NULL);
+ file_download_event_ = &waiter;
+
+ // Start the download in the UI thread.
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&DeviceListenerImpl::DownloadFile,
+ base::Unretained(camera_interface_.get()),
+ device_file_path.BaseName().value(), local_path));
+ waiter.Wait();
+ file_download_event_ = NULL;
+ error = file_download_error_;
+
+ // 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.
+ if (file_download_event_) {
+ file_download_error_ = base::PLATFORM_FILE_ERROR_FAILED;
+ file_download_event_->Signal();
+ }
+
+ // Schedule the camera session to be closed and the interface deleted.
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&DeviceListenerImpl::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);
+
+ if (enumerator_)
+ enumerator_->ItemsChanged();
+}
+
+void MTPDeviceDelegateImplMac::NoMoreItems() {
+ received_all_files_ = true;
+
+ if (enumerator_)
+ enumerator_->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;
+
+ file_download_error_ = error;
+ file_download_event_->Signal();
+}
+
+FilePath MTPDeviceDelegateImplMac::GetFile(size_t index) {
+ if (index >= file_paths_.size())
+ return FilePath();
+ else
+ return file_paths_[index];
+}
+
+bool MTPDeviceDelegateImplMac::ReceivedAllFiles() {
+ return received_all_files_;
+}
+
+void MTPDeviceDelegateImplMac::RemoveEnumerator(Enumerator* enumerator) {
+ DCHECK(enumerator_ == enumerator);
+ enumerator_ = NULL;
+}
+
+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_->ReceivedAllFiles()) {
+ 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;
+}
+
+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
+

Powered by Google App Engine
This is Rietveld 408576698