Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // MTPDeviceDelegateImplWin implementation. | |
| 6 | |
| 7 #include "chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.h" | |
| 8 | |
| 9 #include <portabledevice.h> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/file_path.h" | |
| 13 #include "base/file_util.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/sequenced_task_runner.h" | |
| 16 #include "base/sequenced_task_runner_helpers.h" | |
| 17 #include "base/string_split.h" | |
| 18 #include "base/string_util.h" | |
| 19 #include "base/threading/thread_restrictions.h" | |
| 20 #include "base/utf_string_conversions.h" | |
| 21 #include "chrome/browser/media_gallery/win/mtp_device_object_entry.h" | |
| 22 #include "chrome/browser/media_gallery/win/mtp_device_object_enumerator.h" | |
| 23 #include "chrome/browser/media_gallery/win/mtp_device_operations_util.h" | |
| 24 #include "chrome/browser/media_gallery/win/recursive_mtp_device_object_enumerato r.h" | |
| 25 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi n.h" | |
| 26 #include "chrome/browser/system_monitor/removable_storage_notifications.h" | |
| 27 #include "content/public/browser/browser_thread.h" | |
| 28 | |
| 29 namespace chrome { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // Gets the details of the MTP partition storage specified by the | |
| 34 // |storage_path| on the UI thread. If the storage details are valid, creates a | |
| 35 // MTP device delegate on the media task runner. | |
| 36 void GetStorageInfoAndMaybeCreateDelegate( | |
| 37 const string16& storage_path, | |
| 38 const scoped_refptr<base::SequencedTaskRunner>& media_task_runner, | |
| 39 const CreateMTPDeviceDelegateCallback& callback) { | |
| 40 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 41 DCHECK(!storage_path.empty()); | |
| 42 DCHECK(media_task_runner.get()); | |
| 43 string16 storage_device_id; | |
| 44 RemoveChars(storage_path, L"\\\\", &storage_device_id); | |
| 45 DCHECK(!storage_device_id.empty()); | |
| 46 RemovableStorageNotifications* notifications = | |
| 47 RemovableStorageNotifications::GetInstance(); | |
| 48 DCHECK(notifications); | |
| 49 string16 pnp_device_id; | |
| 50 string16 storage_object_id; | |
| 51 if ((!notifications->GetMTPStorageInfoFromDeviceId( | |
| 52 UTF16ToUTF8(storage_device_id), &pnp_device_id, | |
| 53 &storage_object_id)) || | |
| 54 pnp_device_id.empty() || | |
| 55 storage_object_id.empty()) | |
| 56 return; | |
| 57 media_task_runner->PostTask(FROM_HERE, | |
| 58 base::Bind(&OnGotStorageInfoCreateDelegate, | |
| 59 storage_path, | |
| 60 media_task_runner, | |
| 61 callback, | |
| 62 pnp_device_id, | |
| 63 storage_object_id)); | |
| 64 } | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 // Used by GetStorageInfoAndMaybeCreateDelegate() to create the MTP device | |
| 69 // delegate on the media task runner. | |
| 70 void OnGotStorageInfoCreateDelegate( | |
| 71 const string16& device_location, | |
| 72 base::SequencedTaskRunner* media_task_runner, | |
| 73 const CreateMTPDeviceDelegateCallback& callback, | |
| 74 const string16& pnp_device_id, | |
| 75 const string16& storage_object_id) { | |
| 76 DCHECK(media_task_runner); | |
| 77 DCHECK(media_task_runner->RunsTasksOnCurrentThread()); | |
| 78 callback.Run(new MTPDeviceDelegateImplWin(device_location, | |
| 79 pnp_device_id, | |
| 80 storage_object_id, | |
| 81 media_task_runner)); | |
| 82 } | |
| 83 | |
| 84 void CreateMTPDeviceDelegate(const string16& device_location, | |
| 85 base::SequencedTaskRunner* media_task_runner, | |
| 86 const CreateMTPDeviceDelegateCallback& callback) { | |
| 87 DCHECK(media_task_runner); | |
| 88 DCHECK(media_task_runner->RunsTasksOnCurrentThread()); | |
| 89 content::BrowserThread::PostTask( | |
| 90 content::BrowserThread::UI, FROM_HERE, | |
| 91 base::Bind(&GetStorageInfoAndMaybeCreateDelegate, | |
| 92 device_location, | |
| 93 make_scoped_refptr(media_task_runner), | |
| 94 callback)); | |
| 95 } | |
| 96 | |
| 97 // MTPDeviceDelegateImplWin --------------------------------------------------- | |
| 98 | |
| 99 MTPDeviceDelegateImplWin::MTPDeviceDelegateImplWin( | |
| 100 const string16& fs_root_path, | |
| 101 const string16& pnp_device_id, | |
| 102 const string16& storage_object_id, | |
| 103 base::SequencedTaskRunner* media_task_runner) | |
| 104 : registered_device_path_(fs_root_path), | |
| 105 pnp_device_id_(pnp_device_id), | |
| 106 storage_object_id_(storage_object_id), | |
| 107 media_task_runner_(media_task_runner), | |
| 108 cancel_pending_tasks_(false) { | |
| 109 DCHECK(!pnp_device_id_.empty()); | |
| 110 DCHECK(!storage_object_id_.empty()); | |
| 111 DCHECK(media_task_runner_.get()); | |
| 112 } | |
| 113 | |
| 114 MTPDeviceDelegateImplWin::~MTPDeviceDelegateImplWin() { | |
| 115 DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); | |
| 116 } | |
| 117 | |
| 118 base::PlatformFileError MTPDeviceDelegateImplWin::GetFileInfo( | |
| 119 const FilePath& file_path, | |
| 120 base::PlatformFileInfo* file_info) { | |
| 121 if (!LazyInit()) | |
| 122 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 123 string16 object_id = GetFileObjectIdFromPath(file_path.value()); | |
| 124 if (object_id.empty()) | |
| 125 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 126 return media_transfer_protocol::GetFileEntryInfo(device_.get(), object_id, | |
| 127 file_info); | |
| 128 } | |
| 129 | |
| 130 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> | |
| 131 MTPDeviceDelegateImplWin::CreateFileEnumerator(const FilePath& root, | |
| 132 bool recursive) { | |
| 133 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> | |
| 134 file_enumerator(new fileapi::FileSystemFileUtil::EmptyFileEnumerator()); | |
| 135 if (root.value().empty() || !LazyInit()) | |
|
Lei Zhang
2013/01/14 23:25:30
nit: root.empty()
kmadhusu
2013/01/15 19:08:17
Done.
| |
| 136 return file_enumerator.Pass(); | |
| 137 | |
| 138 string16 object_id = GetFileObjectIdFromPath(root.value()); | |
| 139 if (object_id.empty()) | |
| 140 return file_enumerator.Pass(); | |
| 141 | |
| 142 MTPDeviceObjectEntries entries; | |
| 143 if (!media_transfer_protocol::GetDirectoryEntries(device_.get(), object_id, | |
| 144 &entries) || | |
| 145 entries.empty()) | |
| 146 return file_enumerator.Pass(); | |
| 147 | |
| 148 if (recursive) { | |
| 149 file_enumerator.reset( | |
| 150 new RecursiveMTPDeviceObjectEnumerator(device_.get(), entries)); | |
| 151 } else { | |
| 152 file_enumerator.reset(new MTPDeviceObjectEnumerator(entries)); | |
| 153 } | |
| 154 return file_enumerator.Pass(); | |
| 155 } | |
| 156 | |
| 157 base::PlatformFileError MTPDeviceDelegateImplWin::CreateSnapshotFile( | |
| 158 const FilePath& device_file_path, | |
| 159 const FilePath& local_path, | |
| 160 base::PlatformFileInfo* file_info) { | |
| 161 if (!LazyInit()) | |
| 162 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 163 string16 file_object_id = GetFileObjectIdFromPath(device_file_path.value()); | |
| 164 if (file_object_id.empty()) | |
| 165 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 166 if (!media_transfer_protocol::WriteFileObjectData(device_.get(), | |
| 167 file_object_id, | |
| 168 local_path)) | |
| 169 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 170 base::PlatformFileError error = GetFileInfo(device_file_path, file_info); | |
|
Lei Zhang
2013/01/14 23:25:30
You should do this before writing the file to disk
kmadhusu
2013/01/15 19:08:17
Done.
| |
| 171 | |
| 172 // LocalFileStreamReader is used to read the contents of the snapshot file. | |
| 173 // Snapshot file modification time does not match the last modified time | |
| 174 // of the original media file. Therefore, set the last modified time to null | |
| 175 // in order to avoid the verification in LocalFileStreamReader. | |
| 176 // | |
| 177 // Users will use HTML5 FileSystem Entry getMetadata() interface to get the | |
| 178 // actual last modified time of the media file. | |
| 179 file_info->last_modified = base::Time(); | |
| 180 return error; | |
| 181 } | |
| 182 | |
| 183 void MTPDeviceDelegateImplWin::CancelPendingTasksAndDeleteDelegate() { | |
| 184 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 185 { | |
| 186 base::AutoLock locked(cancel_tasks_lock_); | |
| 187 cancel_pending_tasks_ = true; | |
| 188 } | |
| 189 media_task_runner_->DeleteSoon(FROM_HERE, this); | |
| 190 } | |
| 191 | |
| 192 bool MTPDeviceDelegateImplWin::LazyInit() { | |
| 193 DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); | |
| 194 // Both OpenDevice() (which we call below) and any operations our callers do | |
| 195 // may take some time. Abort them immediately if we're in the process of | |
| 196 // shutting down. | |
| 197 { | |
| 198 base::AutoLock locked(cancel_tasks_lock_); | |
| 199 if (cancel_pending_tasks_) | |
| 200 return false; | |
| 201 } | |
| 202 if (device_.get()) | |
| 203 return true; // Already successfully initialized. | |
| 204 return media_transfer_protocol::OpenDevice(pnp_device_id_, &device_); | |
| 205 } | |
| 206 | |
| 207 string16 MTPDeviceDelegateImplWin::GetFileObjectIdFromPath( | |
| 208 const string16& file_path) { | |
| 209 DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); | |
| 210 DCHECK(!file_path.empty()); | |
| 211 if (registered_device_path_ == file_path) | |
| 212 return storage_object_id_; | |
| 213 | |
| 214 string16 actual_file_path(file_path); | |
| 215 ReplaceFirstSubstringAfterOffset(&actual_file_path, 0, | |
| 216 registered_device_path_, string16()); | |
| 217 DCHECK(!actual_file_path.empty()); | |
| 218 typedef std::vector<string16> PathComponents; | |
| 219 PathComponents path_components; | |
| 220 base::SplitString(actual_file_path, L'\\', &path_components); | |
| 221 DCHECK(!path_components.empty()); | |
| 222 string16 parent_id(storage_object_id_); | |
| 223 string16 file_object_id; | |
| 224 for (PathComponents::const_iterator path_iter = path_components.begin() + 1; | |
| 225 path_iter != path_components.end(); ++path_iter) { | |
| 226 file_object_id = media_transfer_protocol::GetObjectIdFromName(device_, | |
| 227 parent_id, | |
| 228 *path_iter); | |
| 229 if (file_object_id.empty()) | |
| 230 break; | |
| 231 parent_id = file_object_id; | |
| 232 } | |
| 233 return file_object_id; | |
| 234 } | |
| 235 | |
| 236 } // namespace chrome | |
| OLD | NEW |