Chromium Code Reviews| Index: chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc |
| diff --git a/chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc b/chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5e44f16307f74da0a95b131d7514ee86730bac90 |
| --- /dev/null |
| +++ b/chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc |
| @@ -0,0 +1,217 @@ |
| +// 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. |
| +// |
| +// MTPDeviceDelegateImplWin implementation. |
| + |
| +#include "chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.h" |
| + |
| +#include <portabledevice.h> |
| + |
|
Peter Kasting
2012/12/03 20:13:00
Nit: No blank line here
kmadhusu
2012/12/15 02:34:56
Done.
|
| +#include <vector> |
| + |
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "base/string_split.h" |
| +#include "base/string_util.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/media_gallery/win/mtp_device_object_entry.h" |
| +#include "chrome/browser/media_gallery/win/mtp_device_object_enumerator.h" |
| +#include "chrome/browser/media_gallery/win/mtp_device_operations_util.h" |
| +#include "chrome/browser/media_gallery/win/recursive_mtp_device_object_enumerator.h" |
| +#include "chrome/browser/system_monitor/removable_device_notifications_window_win.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace chrome { |
| + |
| +namespace { |
| + |
| +// Gets the details of the MTP storage specified by the |storage_path|. On |
| +// success, returns true and fills in |pnp_device_id| and |storage_object_id|. |
| +// On failure, returns false and |pnp_device_id| and |storage_object_id| are |
| +// not set. |
| +bool GetStorageInfoFromStoragePath(const string16& storage_path, |
| + string16* pnp_device_id, |
| + string16* storage_object_id) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + DCHECK(!storage_path.empty()); |
| + DCHECK(pnp_device_id); |
| + DCHECK(storage_object_id); |
| + string16 storage_device_id; |
| + RemoveChars(storage_path, L"\\\\", &storage_device_id); |
| + DCHECK(!storage_device_id.empty()); |
| + RemovableDeviceNotificationsWindowWin* notifications = |
| + RemovableDeviceNotificationsWindowWin::GetInstance(); |
| + return notifications->GetMTPStorageInfoFromDeviceId( |
| + UTF16ToUTF8(storage_device_id), pnp_device_id, storage_object_id); |
|
Peter Kasting
2012/12/03 20:13:00
Nit: Or just
return RemovableDeviceNotification
kmadhusu
2012/12/15 02:34:56
I need to do a static_cast before calling GetMTPSt
|
| +} |
| + |
| +} // namespace |
| + |
| + |
| +// MTPDeviceDelegateImplWin --------------------------------------------------- |
| + |
| +MTPDeviceDelegateImplWin::MTPDeviceDelegateImplWin(const string16& fs_root_path) |
| + : registered_device_path_(fs_root_path), |
| + cancel_pending_tasks_(false) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
| + media_task_runner_ = pool->GetSequencedTaskRunner( |
| + pool->GetNamedSequenceToken("media-task-runner")); |
| + |
| + bool success = GetStorageInfoFromStoragePath( |
| + registered_device_path_, &pnp_device_id_, &storage_object_id_); |
| + DCHECK(success); |
| +} |
| + |
| +base::PlatformFileError MTPDeviceDelegateImplWin::GetFileInfo( |
| + const FilePath& file_path, |
| + base::PlatformFileInfo* file_info) { |
| + if (!LazyInit()) |
| + return base::PLATFORM_FILE_ERROR_FAILED; |
| + string16 object_id = GetFileObjectIdFromPath(file_path.value()); |
| + if (object_id.empty()) |
| + return base::PLATFORM_FILE_ERROR_FAILED; |
| + return MTPDeviceOperationsUtil::GetFileEntryInfo(device_.get(), object_id, |
| + file_info); |
| +} |
| + |
| +scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> |
| + MTPDeviceDelegateImplWin::CreateFileEnumerator(const FilePath& root, |
| + bool recursive) { |
| + if (root.value().empty() || !LazyInit()) { |
|
Peter Kasting
2012/12/03 20:13:00
Nit: Because the code to create your scoped_ptrs i
kmadhusu
2012/12/15 02:34:56
Done.
|
| + return make_scoped_ptr( |
| + new fileapi::FileSystemFileUtil::EmptyFileEnumerator()) |
| + .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>(); |
| + } |
| + string16 object_id = GetFileObjectIdFromPath(root.value()); |
| + if (object_id.empty()) { |
| + return make_scoped_ptr( |
| + new fileapi::FileSystemFileUtil::EmptyFileEnumerator()) |
| + .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>(); |
| + } |
| + |
| + MTPDeviceObjectEntries entries; |
| + if (!MTPDeviceOperationsUtil::GetDirectoryEntries(device_.get(), object_id, |
| + &entries) || |
| + entries.empty()) { |
| + return make_scoped_ptr( |
| + new fileapi::FileSystemFileUtil::EmptyFileEnumerator()) |
| + .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>(); |
| + } |
| + |
| + if (recursive) { |
| + return make_scoped_ptr( |
| + new RecursiveMTPDeviceObjectEnumerator(device_.get(), entries)) |
| + .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>(); |
| + } |
| + return make_scoped_ptr(new MTPDeviceObjectEnumerator(entries)) |
| + .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>(); |
| +} |
| + |
| +base::PlatformFileError MTPDeviceDelegateImplWin::CreateSnapshotFile( |
| + const FilePath& device_file_path, |
| + const FilePath& local_path, |
| + base::PlatformFileInfo* file_info) { |
| + if (!LazyInit()) |
| + return base::PLATFORM_FILE_ERROR_FAILED; |
| + string16 file_object_id = GetFileObjectIdFromPath(device_file_path.value()); |
| + if (file_object_id.empty()) |
| + return base::PLATFORM_FILE_ERROR_FAILED; |
| + if (!MTPDeviceOperationsUtil::WriteFileObjectData(device_.get(), |
| + file_object_id, |
| + local_path)) |
| + return base::PLATFORM_FILE_ERROR_FAILED; |
| + base::PlatformFileError error = GetFileInfo(device_file_path, file_info); |
| + // Modify the last modified time to null. This prevents the time stamp |
|
Peter Kasting
2012/12/03 20:13:00
Nit: Modify -> Set
kmadhusu
2012/12/15 02:34:56
Done.
|
| + // verification in LocalFileStreamReader. |
|
Peter Kasting
2012/12/03 20:13:00
Nit: Can you be more detailed in describing what v
kmadhusu
2012/12/15 02:34:56
Done.
|
| + file_info->last_modified = base::Time(); |
| + return error; |
| +} |
| + |
| +base::SequencedTaskRunner* MTPDeviceDelegateImplWin::GetMediaTaskRunner() { |
| + return media_task_runner_.get(); |
| +} |
| + |
| +void MTPDeviceDelegateImplWin::CancelPendingTasksAndDeleteDelegate() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| + // Caution: This function is called on the IO thread. Access only the thread |
| + // safe member variables in this function. Do all the clean up operations in |
| + // DeleteDelegateOnTaskRunner(). |
| + { |
| + base::AutoLock locked(cancel_tasks_lock_); |
| + cancel_pending_tasks_ = true; |
| + } |
| + media_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&MTPDeviceDelegateImplWin::DeleteDelegateOnTaskRunner, |
| + base::Unretained(this))); |
| +} |
| + |
| +base::WeakPtr<fileapi::MTPDeviceDelegate> MTPDeviceDelegateImplWin:: |
| + GetAsWeakPtrOnIOThread() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| + base::WeakPtr<fileapi::MTPDeviceDelegate> delegate = AsWeakPtr(); |
| + // The weak pointer is instantiated on IO thread, but only dereferenced on |
| + // task runner thread. Therefore, detach from the current thread. |
| + DetachFromThread(); |
| + return delegate; |
| +} |
| + |
| +MTPDeviceDelegateImplWin::~MTPDeviceDelegateImplWin() { |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + // Do all the clean up operations on DeleteDelegateOnTaskRunner(). |
| +} |
| + |
| +bool MTPDeviceDelegateImplWin::LazyInit() { |
| + DCHECK(media_task_runner_); |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + { |
| + // Device delegate will be destructed soon. |
| + base::AutoLock locked(cancel_tasks_lock_); |
| + if (cancel_pending_tasks_) |
| + return false; |
| + } |
| + if (device_.get()) |
| + return true; // Already successfully initialized. |
| + DCHECK(!pnp_device_id_.empty()); |
| + return MTPDeviceOperationsUtil::OpenDevice(pnp_device_id_, &device_); |
| +} |
| + |
| +string16 MTPDeviceDelegateImplWin::GetFileObjectIdFromPath( |
| + const string16& file_path) { |
| + DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
|
Peter Kasting
2012/12/03 20:13:00
Nit: Why not use |media_task_runner_| here like yo
kmadhusu
2012/12/15 02:34:56
Done.
|
| + DCHECK(!file_path.empty()); |
| + if (registered_device_path_ == file_path) |
| + return storage_object_id_; |
| + |
| + string16 actual_file_path(file_path); |
| + ReplaceFirstSubstringAfterOffset(&actual_file_path, 0, |
| + registered_device_path_, string16()); |
| + DCHECK(!actual_file_path.empty()); |
| + typedef std::vector<string16> PathComponents; |
| + PathComponents path_components; |
| + base::SplitString(actual_file_path, L'\\', &path_components); |
| + DCHECK(!path_components.empty()); |
| + string16 parent_id(storage_object_id_); |
| + string16 file_object_id; |
| + for (PathComponents::const_iterator path_iter = path_components.begin() + 1; |
| + path_iter != path_components.end(); ++path_iter) { |
| + file_object_id = MTPDeviceOperationsUtil::GetObjectIdFromName(device_, |
| + parent_id, |
| + *path_iter); |
| + if (file_object_id.empty()) |
| + break; |
| + parent_id = file_object_id; |
| + } |
| + return file_object_id; |
| +} |
| + |
| +void MTPDeviceDelegateImplWin::DeleteDelegateOnTaskRunner() { |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + delete this; |
| +} |
| + |
| +} // namespace chrome |