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

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: Modified LazyInit() to handle GetDeviceStorageInfo failure Created 7 years, 12 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..d161acaea3c05baccec2623290483032ba83aa07
--- /dev/null
+++ b/chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc
@@ -0,0 +1,187 @@
+// 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();
+ 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();
+ if (!worker->GetDeviceStorageInfo(&pnp_device_id_, &storage_object_id_))
+ return false;
+ 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

Powered by Google App Engine
This is Rietveld 408576698