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..011037c7fcaacc25354bccad8e146cde4a8a3e58 |
| --- /dev/null |
| +++ b/chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc |
| @@ -0,0 +1,192 @@ |
| +// 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/common/chrome_notification_types.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_service.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); |
| +} |
| + |
| +} // namespace |
| + |
| + |
| +// MTPDeviceDelegateImplWin --------------------------------------------------- |
| + |
| +MTPDeviceDelegateImplWin::MTPDeviceDelegateImplWin(const string16& fs_root_path) |
| + : registered_device_path_(fs_root_path) { |
| + 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); |
| + registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
| + content::NotificationService::AllSources()); |
| +} |
| + |
| +MTPDeviceDelegateImplWin::~MTPDeviceDelegateImplWin() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| +} |
| + |
| +base::PlatformFileError MTPDeviceDelegateImplWin::GetFileInfo( |
| + const FilePath& file_path, |
| + base::PlatformFileInfo* file_info) { |
| + if (app_terminating_flag_.IsSet() || !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); |
| +} |
| + |
| +fileapi::FileSystemFileUtil::AbstractFileEnumerator* |
| + MTPDeviceDelegateImplWin::CreateFileEnumerator(const FilePath& root, |
| + bool recursive) { |
| + if (app_terminating_flag_.IsSet() || root.value().empty() || !LazyInit()) |
| + return new fileapi::FileSystemFileUtil::EmptyFileEnumerator(); |
| + string16 object_id = GetFileObjectIdFromPath(root.value()); |
| + if (object_id.empty()) |
| + return new fileapi::FileSystemFileUtil::EmptyFileEnumerator(); |
| + |
| + MTPDeviceObjectEntries entries; |
| + bool success = MTPDeviceOperationsUtil::GetDirectoryEntries(device_.get(), |
| + object_id, |
| + &entries); |
| + if (!success || entries.empty()) |
| + return new fileapi::FileSystemFileUtil::EmptyFileEnumerator(); |
| + |
| + if (recursive) |
| + return new RecursiveMTPDeviceObjectEnumerator(device_.get(), entries); |
| + return new MTPDeviceObjectEnumerator(entries); |
| +} |
| + |
| +base::PlatformFileError MTPDeviceDelegateImplWin::CreateSnapshotFile( |
| + const FilePath& device_file_path, |
| + const FilePath& local_path, |
| + base::PlatformFileInfo* file_info) { |
| + if (app_terminating_flag_.IsSet() || !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; |
| + std::string file_data; |
| + bool success = MTPDeviceOperationsUtil::GetFileObjectData(device_.get(), |
| + file_object_id, |
| + &file_data); |
| + if (!success || file_data.empty()) |
| + return base::PLATFORM_FILE_ERROR_FAILED; |
| + int size = static_cast<int>(file_data.length()); |
| + if (file_util::WriteFile(local_path, file_data.c_str(), size) != size) |
| + 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 |
| + // verification in LocalFileStreamReader. |
| + file_info->last_modified = base::Time(); |
| + return error; |
| +} |
| + |
| +base::SequencedTaskRunner* MTPDeviceDelegateImplWin::media_task_runner() { |
| + return media_task_runner_.get(); |
| +} |
| + |
| +void MTPDeviceDelegateImplWin::DeleteOnCorrectThread() const { |
| + if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| + delete this; |
| + return; |
| + } |
| + content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE, |
| + this); |
| +} |
| + |
| +void MTPDeviceDelegateImplWin::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + DCHECK_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); |
| + app_terminating_flag_.Set(); |
| +} |
| + |
| +bool MTPDeviceDelegateImplWin::LazyInit() { |
| + DCHECK(media_task_runner_); |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + 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()); |
| + 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_, L""); |
|
Peter Kasting
2012/11/01 21:38:37
Nit: L"" -> std::string()
kmadhusu
2012/11/02 03:27:16
Changed L"" -> string16(). Did you mean string16()
|
| + DCHECK(!actual_file_path.empty()); |
| + typedef std::vector<string16> PathComponents; |
| + PathComponents path_components; |
| + base::SplitString(actual_file_path, L'\\', &path_components); |
| + DCHECK_GE(path_components.size(), static_cast<size_t>(1)); |
|
Peter Kasting
2012/11/01 21:38:37
Nit: How about just DCHECK(!path_components.empty(
kmadhusu
2012/11/02 03:27:16
I would like to keep this as it is.
For e.g., if
Peter Kasting
2012/11/02 04:08:11
Your existing check is "path_components.size() >=
kmadhusu
2012/11/29 23:58:14
Thanks for the explanation. You are right. DCHECK(
|
| + 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); |
| + parent_id = file_object_id; |
| + } |
| + return file_object_id; |
| +} |
| + |
| +} // namespace chrome |