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

Unified Diff: chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc

Issue 11297002: [Media Gallery] Added code to support mtp device media file system on Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments Created 8 years, 2 months 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/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..77f3f57f3303079d0b59254b357c94632774c97f
--- /dev/null
+++ b/chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc
@@ -0,0 +1,212 @@
+// 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 {
+
+// Returns the object id of the file object specified by the |file_path|.
+// E.g.: If the |file_path| is "\\MTP:StorageSerial:SID-{10001,,1922}:125\DCIM"
+// and |registered_device_path| is "\\MTP:StorageSerial:SID-{100,,1922}:125",
+// this function returns the object id of "DCIM" folder.
+string16 GetFileObjectIdFromPath(IPortableDevice* device,
Lei Zhang 2012/11/01 02:34:57 In this case, GetFileObjectIdFromPath() would have
kmadhusu 2012/11/01 17:59:00 Done.
+ const string16& registered_device_path,
+ const string16& file_path,
+ const string16& root_storage_object_id) {
+ DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
+ DCHECK(device);
+ DCHECK(!registered_device_path.empty());
+ DCHECK(!file_path.empty());
+ DCHECK(!root_storage_object_id.empty());
+ if (registered_device_path == file_path)
+ return root_storage_object_id;
+
+ string16 actual_file_path(file_path);
+ ReplaceFirstSubstringAfterOffset(&actual_file_path, 0,
+ registered_device_path, L"");
+ DCHECK(!actual_file_path.empty());
+ typedef std::vector<string16> PathComponents;
+ PathComponents path_components;
+ base::SplitString(actual_file_path, L'\\', &path_components);
+ DCHECK(path_components.size() >= 1);
Lei Zhang 2012/11/01 02:34:57 DCHECK_GE
kmadhusu 2012/11/01 17:59:00 Done.
+ string16 parent_id(root_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;
+}
+
+// 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_unique_id;
+ RemoveChars(storage_path, L"\\\\", &storage_unique_id);
+ DCHECK(!storage_unique_id.empty());
+ RemovableDeviceNotificationsWindowWin* notifications =
+ RemovableDeviceNotificationsWindowWin::GetInstance();
+ return notifications->GetMTPStorageInfoFromUniqueId(
+ UTF16ToUTF8(storage_unique_id), pnp_device_id, storage_object_id);
+}
+
+} // namespace
+
+
+// MTPDeviceDelegateImplWin ---------------------------------------------------
+
+MTPDeviceDelegateImplWin::MTPDeviceDelegateImplWin(const string16& fs_root_path)
+ : registered_device_path_(fs_root_path),
+ app_terminating_(false) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
+ media_task_runner_ = pool->GetSequencedTaskRunner(
+ pool->GetNamedSequenceToken("media-task-runner"));
+
+ if (!GetStorageInfoFromStoragePath(registered_device_path_,
Lei Zhang 2012/11/01 02:34:57 Unless the compiler objects, I'd prefer this as:
kmadhusu 2012/11/01 17:59:00 Done.
+ &pnp_device_id_, &storage_object_id_)) {
+ NOTREACHED();
+ }
+ 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_ || !LazyInit())
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ string16 object_id = GetFileObjectIdFromPath(device_.get(),
+ registered_device_path_,
+ file_path.value(),
+ storage_object_id_);
+ 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_ || root.value().empty() || !LazyInit())
+ return new fileapi::FileSystemFileUtil::EmptyFileEnumerator();
+ string16 object_id = GetFileObjectIdFromPath(device_.get(),
+ registered_device_path_,
+ root.value(),
+ storage_object_id_);
+ 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_ || !LazyInit())
+ return base::PLATFORM_FILE_ERROR_FAILED;
+ string16 file_object_id = GetFileObjectIdFromPath(device_.get(),
+ registered_device_path_,
+ device_file_path.value(),
+ storage_object_id_);
+ 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
+ // verfication in LocalFileStreamReader.
Lei Zhang 2012/11/01 02:34:57 typo
kmadhusu 2012/11/01 17:59:00 Done.
+ 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_ = true;
+}
+
+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_);
+}
+
+} // namespace chrome

Powered by Google App Engine
This is Rietveld 408576698