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..c93ffb78d1e6a1173d011bd370a41743a36cae6a |
| --- /dev/null |
| +++ b/chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc |
| @@ -0,0 +1,203 @@ |
| +// 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> |
| +#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 "chrome/browser/system_monitor/removable_storage_notifications.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace chrome { |
| + |
| +namespace { |
| + |
| +// Gets the details of the MTP partition 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::GetBlockingPool()->RunsTasksOnCurrentThread()); |
|
Peter Kasting
2012/12/20 21:24:20
Based on discussion with rsleevi, I suggest two DC
kmadhusu
2012/12/21 01:45:23
Done. Removed the DCHECK.
|
| + 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()); |
| + RemovableStorageNotifications* notifications = |
| + RemovableStorageNotifications::GetInstance(); |
| + DCHECK(notifications); |
| + return notifications->GetMTPStorageInfoFromDeviceId( |
| + UTF16ToUTF8(storage_device_id), pnp_device_id, storage_object_id); |
| +} |
| + |
| +} // namespace |
| + |
| +void CreateMTPDeviceDelegate(const string16& device_location, |
| + base::SequencedTaskRunner* media_task_runner, |
| + const CreateMTPDeviceDelegateCallback& callback) { |
| + DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
|
Peter Kasting
2012/12/20 21:24:20
The second change is here. Rather than check that
kmadhusu
2012/12/21 01:45:23
Done.
|
| + callback.Run(new MTPDeviceDelegateImplWin(device_location, |
| + media_task_runner)); |
| +} |
| + |
| +// MTPDeviceDelegateImplWin --------------------------------------------------- |
| + |
| +MTPDeviceDelegateImplWin::MTPDeviceDelegateImplWin( |
| + const string16& fs_root_path, |
| + base::SequencedTaskRunner* media_task_runner) |
| + : registered_device_path_(fs_root_path), |
| + media_task_runner_(media_task_runner), |
| + cancel_pending_tasks_(false) { |
| + DCHECK(media_task_runner_.get()); |
| + bool success = GetStorageInfoFromStoragePath( |
| + registered_device_path_, &pnp_device_id_, &storage_object_id_); |
| + DCHECK(success); |
| +} |
| + |
| +MTPDeviceDelegateImplWin::~MTPDeviceDelegateImplWin() { |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| +} |
| + |
| +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) { |
| + scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> |
| + file_enumerator(new fileapi::FileSystemFileUtil::EmptyFileEnumerator()); |
| + if (root.value().empty() || !LazyInit()) |
| + return file_enumerator.Pass(); |
| + |
| + string16 object_id = GetFileObjectIdFromPath(root.value()); |
| + if (object_id.empty()) |
| + return file_enumerator.Pass(); |
| + |
| + MTPDeviceObjectEntries entries; |
| + if (!MTPDeviceOperationsUtil::GetDirectoryEntries(device_.get(), object_id, |
| + &entries) || |
| + entries.empty()) |
| + return file_enumerator.Pass(); |
| + |
| + if (recursive) { |
| + file_enumerator.reset( |
| + new RecursiveMTPDeviceObjectEnumerator(device_.get(), entries)); |
| + } else { |
| + file_enumerator.reset(new MTPDeviceObjectEnumerator(entries)); |
| + } |
| + return file_enumerator.Pass(); |
| +} |
| + |
| +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); |
| + |
| + // LocalFileStreamReader is used to read the contents of the snapshot file. |
| + // Snapshot file modification time does not match the last modified time |
| + // of the original media file. Therefore, set the last modified time to null |
| + // in order to avoid the verification in LocalFileStreamReader. |
| + // |
| + // Users will use HTML5 FileSystem Entry getMetadata() interface to get actual |
| + // last modified time of the media file. |
| + 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::UI)); |
| + // Caution: This function is called on the UI thread. Access only the thread |
| + // safe member variables in this function. |
|
Peter Kasting
2012/12/19 20:17:58
Nit: Given your DCHECK above, this is probably not
kmadhusu
2012/12/20 20:37:29
Removed.
|
| + { |
| + base::AutoLock locked(cancel_tasks_lock_); |
| + cancel_pending_tasks_ = true; |
| + } |
| + media_task_runner_->DeleteSoon(FROM_HERE, this); |
| +} |
| + |
| +bool MTPDeviceDelegateImplWin::LazyInit() { |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + { |
| + // Device delegate will be destructed soon. |
|
Peter Kasting
2012/12/19 20:17:58
Nit: Perhaps this should instead be:
Both OpenDev
kmadhusu
2012/12/20 20:37:29
Done.
|
| + 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(media_task_runner_->RunsTasksOnCurrentThread()); |
| + 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; |
| +} |
| + |
| +} // namespace chrome |