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

Unified Diff: chrome/browser/media_gallery/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: Rebase to current image capture api state Created 8 years 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/mtp_device_delegate_impl_mac.mm
diff --git a/chrome/browser/media_gallery/mtp_device_delegate_impl_mac.mm b/chrome/browser/media_gallery/mtp_device_delegate_impl_mac.mm
new file mode 100644
index 0000000000000000000000000000000000000000..53fdf8aabcdb53e0da682ae7d7b5674808a8d4fd
--- /dev/null
+++ b/chrome/browser/media_gallery/mtp_device_delegate_impl_mac.mm
@@ -0,0 +1,335 @@
+// 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/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() {}
+
+ 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)
+ : media_task_runner_(media_task_runner),
+ received_all_files_(false) {
+ std::string device_id = FilePath(location).BaseName().value();
+ MediaStorageUtil::Type type;
+ 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
+ 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.
+ 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;
Lei Zhang 2012/12/21 21:05:44 Isn't this block the same as: *file_info = i->sec
+ 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_FAILED;
+ }
+}
+
+scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator>
+MTPDeviceDelegateImplMac::CreateFileEnumerator(const FilePath& root,
+ 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
+ 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;
+ // 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_->PostTask(
+ FROM_HERE,
+ 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.
+ base::Unretained(this)));
+}
+
+void MTPDeviceDelegateImplMac::DeleteDelegateOnTaskRunner() {
+ delete 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;
+
+LOG(INFO) << "Downloaded file " << name;
Lei Zhang 2012/12/21 21:05:44 debug statement
Greg Billock 2012/12/22 00:10:28 Done.
+ download_errors_[name] = error;
+ download_events_[name]->Signal();
+}
+
+FilePath MTPDeviceDelegateImplMac::GetFile(int index) {
+ 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.
+ 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
+

Powered by Google App Engine
This is Rietveld 408576698