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..bc8304fa0b94f7b528f716647e820a2fce72593f |
| --- /dev/null |
| +++ b/chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc |
| @@ -0,0 +1,188 @@ |
| +// 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/memory/ref_counted.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "base/sequenced_task_runner_helpers.h" |
| +#include "base/string_split.h" |
| +#include "base/string_util.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/mtp_get_storage_info_worker.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 { |
| + |
| +void CreateMTPDeviceDelegate(const string16& device_location, |
| + base::SequencedTaskRunner* media_task_runner, |
| + const CreateMTPDeviceDelegateCallback& callback) { |
| + DCHECK(media_task_runner && media_task_runner->RunsTasksOnCurrentThread()); |
| + callback.Run(new MTPDeviceDelegateImplWin(device_location, |
| + media_task_runner)); |
| +} |
| + |
| +// MTPDeviceDelegateImplWin --------------------------------------------------- |
| + |
| +bool MTPDeviceDelegateImplWin::IsCancelTasksFlagSet() { |
| + base::AutoLock locked(cancel_tasks_lock_); |
| + return cancel_pending_tasks_; |
| +} |
| + |
| +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), |
| + task_completed_event_(false, false) { |
| + DCHECK(media_task_runner_.get()); |
| +} |
| + |
| +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; |
| +} |
| + |
| +void MTPDeviceDelegateImplWin::CancelPendingTasksAndDeleteDelegate() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + { |
| + base::AutoLock locked(cancel_tasks_lock_); |
| + cancel_pending_tasks_ = true; |
| + } |
| + task_completed_event_.Signal(); |
|
kmadhusu
2013/01/04 22:14:33
Due to a bad merge conflict, this code was not add
kmadhusu
2013/01/04 22:16:42
Just to be clear, MTPDeviceDelegateImplWin::Cancel
|
| + media_task_runner_->DeleteSoon(FROM_HERE, this); |
| +} |
| + |
| +bool MTPDeviceDelegateImplWin::LazyInit() { |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + // Both OpenDevice() (which we call below) and any operations our callers do |
| + // may take some time. Abort them immediately if we're in the process of |
| + // shutting down. |
| + if (IsCancelTasksFlagSet()) |
| + return false; |
| + |
| + if (device_.get()) |
| + return true; // Already successfully initialized. |
| + |
| + scoped_refptr<MTPGetStorageInfoWorker> worker( |
| + new MTPGetStorageInfoWorker(registered_device_path_, |
| + media_task_runner_, |
| + this, |
| + &task_completed_event_)); |
| + worker->Run(); |
| + bool success = worker->GetDeviceStorageInfo(&pnp_device_id_, |
| + &storage_object_id_); |
| + DCHECK(success); |
| + 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 |