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 #include "chrome/browser/media_gallery/win/mtp_device_operations_util.h" | |
| 6 | |
| 7 #include <portabledevice.h> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/file_util.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/string_util.h" | |
| 14 #include "base/threading/thread_restrictions.h" | |
| 15 #include "base/time.h" | |
| 16 #include "base/win/scoped_co_mem.h" | |
| 17 #include "chrome/browser/system_monitor/removable_device_constants.h" | |
| 18 #include "chrome/common/chrome_constants.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 | |
| 21 namespace chrome { | |
| 22 | |
| 23 namespace media_transfer_protocol { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // On success, returns true and updates |client_info| with a reference to an | |
| 28 // IPortableDeviceValues interface that holds information about the | |
| 29 // application that communicates with the device. | |
| 30 bool GetClientInformation( | |
| 31 base::win::ScopedComPtr<IPortableDeviceValues>* client_info) { | |
| 32 base::ThreadRestrictions::AssertIOAllowed(); | |
| 33 DCHECK(client_info); | |
| 34 HRESULT hr = client_info->CreateInstance(__uuidof(PortableDeviceValues), | |
| 35 NULL, CLSCTX_INPROC_SERVER); | |
| 36 if (FAILED(hr)) { | |
| 37 DPLOG(ERROR) << "Failed to create an instance of IPortableDeviceValues"; | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 (*client_info)->SetStringValue(WPD_CLIENT_NAME, | |
| 42 chrome::kBrowserProcessExecutableName); | |
| 43 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_MAJOR_VERSION, 0); | |
| 44 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_MINOR_VERSION, 0); | |
| 45 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_REVISION, 0); | |
| 46 (*client_info)->SetUnsignedIntegerValue( | |
| 47 WPD_CLIENT_SECURITY_QUALITY_OF_SERVICE, SECURITY_IMPERSONATION); | |
| 48 (*client_info)->SetUnsignedIntegerValue(WPD_CLIENT_DESIRED_ACCESS, | |
| 49 GENERIC_READ); | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 // Gets the content interface of the portable |device|. On success, returns | |
| 54 // true and fills in |content| with the given portable |device| content. | |
| 55 bool GetDeviceContent(IPortableDevice* device, | |
| 56 IPortableDeviceContent** content) { | |
| 57 base::ThreadRestrictions::AssertIOAllowed(); | |
| 58 DCHECK(device); | |
| 59 DCHECK(content); | |
| 60 return SUCCEEDED(device->Content(content)); | |
| 61 } | |
| 62 | |
| 63 // Gets the portable |device| enumerator interface to enumerate the objects. | |
| 64 // |parent_id| specifies the parent object identifier. On success, returns | |
| 65 // true and fills in |enum_object_ids|. | |
| 66 bool GetDeviceObjectEnumerator(IPortableDevice* device, | |
| 67 const string16& parent_id, | |
| 68 IEnumPortableDeviceObjectIDs** enum_object_ids) { | |
| 69 base::ThreadRestrictions::AssertIOAllowed(); | |
| 70 DCHECK(device); | |
| 71 DCHECK(!parent_id.empty()); | |
| 72 DCHECK(enum_object_ids); | |
| 73 base::win::ScopedComPtr<IPortableDeviceContent> content; | |
| 74 if (!GetDeviceContent(device, content.Receive())) | |
| 75 return false; | |
| 76 | |
| 77 HRESULT hr = content->EnumObjects(0, parent_id.c_str(), NULL, | |
| 78 enum_object_ids); | |
| 79 return SUCCEEDED(hr); | |
| 80 } | |
| 81 | |
| 82 // Writes data from |stream| to the file specified by |local_path|. On success, | |
| 83 // returns true and the stream contents are appended to the file. | |
| 84 // TODO(kmadhusu) Deprecate this function after fixing crbug.com/110119. | |
| 85 bool WriteStreamContentsToFile(IStream* stream, | |
| 86 size_t optimal_transfer_size, | |
| 87 const FilePath& local_path) { | |
| 88 base::ThreadRestrictions::AssertIOAllowed(); | |
| 89 DCHECK(stream); | |
| 90 DCHECK_GT(optimal_transfer_size, 0u); | |
| 91 DCHECK(!local_path.empty()); | |
| 92 DWORD read = 0; | |
| 93 HRESULT hr = S_OK; | |
| 94 std::string buffer; | |
| 95 do { | |
| 96 hr = stream->Read(WriteInto(&buffer, optimal_transfer_size + 1), | |
| 97 optimal_transfer_size, &read); | |
| 98 // IStream::Read() returns S_FALSE when the actual number of bytes read from | |
| 99 // the stream object is less than the number of bytes requested (aka | |
| 100 // |optimal_transfer_size|). This indicates the end of the stream has been | |
| 101 // reached. Therefore, it is fine to return true when Read() returns | |
| 102 // S_FALSE. | |
| 103 if ((hr != S_OK) && (hr != S_FALSE)) | |
| 104 return false; | |
| 105 if (read) { | |
| 106 buffer.erase(read); | |
| 107 DCHECK_EQ(read, buffer.length()); | |
| 108 int data_len = static_cast<int>(buffer.length()); | |
| 109 if (file_util::AppendToFile(local_path, buffer.c_str(), data_len) != | |
| 110 data_len) | |
| 111 return false; | |
| 112 } | |
| 113 } while ((read > 0) && SUCCEEDED(hr)); | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 // Returns whether the object is a directory/folder/album. |properties_values| | |
| 118 // contains the object property key values. | |
| 119 bool IsDirectory(IPortableDeviceValues* properties_values) { | |
| 120 DCHECK(properties_values); | |
| 121 GUID content_type; | |
| 122 HRESULT hr = properties_values->GetGuidValue(WPD_OBJECT_CONTENT_TYPE, | |
| 123 &content_type); | |
| 124 if (FAILED(hr)) | |
| 125 return false; | |
| 126 return ((content_type == WPD_CONTENT_TYPE_AUDIO_ALBUM) || | |
| 127 (content_type == WPD_CONTENT_TYPE_FOLDER) || | |
| 128 (content_type == WPD_CONTENT_TYPE_FUNCTIONAL_OBJECT) || | |
| 129 (content_type == WPD_CONTENT_TYPE_IMAGE_ALBUM) || | |
| 130 (content_type == WPD_CONTENT_TYPE_MIXED_CONTENT_ALBUM) || | |
| 131 (content_type == WPD_CONTENT_TYPE_VIDEO_ALBUM)); | |
| 132 } | |
| 133 | |
| 134 // Returns the friendly name of the object from the property key values | |
| 135 // specified by the |properties_values|. | |
| 136 string16 GetObjectName(IPortableDeviceValues* properties_values, | |
| 137 bool is_directory) { | |
| 138 DCHECK(properties_values); | |
| 139 base::win::ScopedCoMem<char16> buffer; | |
| 140 REFPROPERTYKEY key = | |
| 141 is_directory ? WPD_OBJECT_NAME : WPD_OBJECT_ORIGINAL_FILE_NAME; | |
| 142 HRESULT hr = properties_values->GetStringValue(key, &buffer); | |
| 143 string16 result; | |
| 144 if (SUCCEEDED(hr)) | |
| 145 result.assign(buffer); | |
| 146 return result; | |
| 147 } | |
| 148 | |
| 149 // Gets the last modified time of the object from the property key values | |
| 150 // specified by the |properties_values|. On success, returns true and fills in | |
| 151 // |last_modified_time|. | |
| 152 bool GetLastModifiedTime(IPortableDeviceValues* properties_values, | |
| 153 base::Time* last_modified_time) { | |
| 154 DCHECK(properties_values); | |
| 155 DCHECK(last_modified_time); | |
| 156 PROPVARIANT last_modified_date = {0}; | |
| 157 PropVariantInit(&last_modified_date); | |
| 158 HRESULT hr = properties_values->GetValue(WPD_OBJECT_DATE_MODIFIED, | |
| 159 &last_modified_date); | |
| 160 if (FAILED(hr)) | |
| 161 return false; | |
| 162 | |
| 163 bool last_modified_time_set = (last_modified_date.vt == VT_DATE); | |
| 164 if (last_modified_time_set) { | |
| 165 SYSTEMTIME system_time; | |
| 166 FILETIME file_time; | |
| 167 if (VariantTimeToSystemTime(last_modified_date.date, &system_time) && | |
| 168 SystemTimeToFileTime(&system_time, &file_time)) | |
| 169 *last_modified_time = base::Time::FromFileTime(file_time); | |
| 170 else | |
| 171 last_modified_time_set = false; | |
| 172 } | |
| 173 PropVariantClear(&last_modified_date); | |
| 174 return last_modified_time_set; | |
| 175 } | |
| 176 | |
| 177 // Gets the size of the file object in bytes from the property key values | |
| 178 // specified by the |properties_values|. On success, returns true and fills | |
| 179 // in |size|. | |
| 180 bool GetObjectSize(IPortableDeviceValues* properties_values, int64* size) { | |
| 181 DCHECK(properties_values); | |
| 182 DCHECK(size); | |
| 183 ULONGLONG actual_size; | |
| 184 HRESULT hr = properties_values->GetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, | |
| 185 &actual_size); | |
| 186 if (SUCCEEDED(hr) && (actual_size <= kint64max)) | |
| 187 *size = static_cast<int64>(actual_size); | |
| 188 return SUCCEEDED(hr); | |
|
Ryan Sleevi
2013/01/11 17:40:46
BUG? This means if |actual_size| > kint64max, you
kmadhusu
2013/01/11 19:01:59
Fixed.
| |
| 189 } | |
| 190 | |
| 191 // Gets the details of the object specified by the |object_id| given the media | |
| 192 // transfer protocol |device|. On success, returns true and fills in |name|, | |
| 193 // |is_directory|, |size| and |last_modified_time|. | |
| 194 bool GetObjectDetails(IPortableDevice* device, | |
| 195 const string16 object_id, | |
| 196 string16* name, | |
| 197 bool* is_directory, | |
| 198 int64* size, | |
| 199 base::Time* last_modified_time) { | |
| 200 base::ThreadRestrictions::AssertIOAllowed(); | |
| 201 DCHECK(device); | |
| 202 DCHECK(!object_id.empty()); | |
| 203 DCHECK(name); | |
| 204 DCHECK(is_directory); | |
| 205 DCHECK(size); | |
| 206 DCHECK(last_modified_time); | |
| 207 base::win::ScopedComPtr<IPortableDeviceContent> content; | |
| 208 if (!GetDeviceContent(device, content.Receive())) | |
| 209 return false; | |
| 210 | |
| 211 base::win::ScopedComPtr<IPortableDeviceProperties> properties; | |
| 212 HRESULT hr = content->Properties(properties.Receive()); | |
| 213 if (FAILED(hr)) | |
| 214 return false; | |
| 215 | |
| 216 base::win::ScopedComPtr<IPortableDeviceKeyCollection> properties_to_read; | |
| 217 hr = properties_to_read.CreateInstance(__uuidof(PortableDeviceKeyCollection), | |
| 218 NULL, | |
| 219 CLSCTX_INPROC_SERVER); | |
| 220 if (FAILED(hr)) | |
| 221 return false; | |
| 222 | |
| 223 if (FAILED(properties_to_read->Add(WPD_OBJECT_CONTENT_TYPE)) || | |
| 224 FAILED(properties_to_read->Add(WPD_OBJECT_FORMAT)) || | |
| 225 FAILED(properties_to_read->Add(WPD_OBJECT_ORIGINAL_FILE_NAME)) || | |
| 226 FAILED(properties_to_read->Add(WPD_OBJECT_NAME)) || | |
| 227 FAILED(properties_to_read->Add(WPD_OBJECT_DATE_MODIFIED)) || | |
| 228 FAILED(properties_to_read->Add(WPD_OBJECT_SIZE))) | |
| 229 return false; | |
| 230 | |
| 231 base::win::ScopedComPtr<IPortableDeviceValues> properties_values; | |
| 232 hr = properties->GetValues(object_id.c_str(), | |
| 233 properties_to_read.get(), | |
| 234 properties_values.Receive()); | |
| 235 if (FAILED(hr)) | |
| 236 return false; | |
| 237 | |
| 238 *is_directory = IsDirectory(properties_values.get()); | |
| 239 *name = GetObjectName(properties_values.get(), *is_directory); | |
| 240 if (name->empty()) | |
| 241 return false; | |
| 242 | |
| 243 if (*is_directory) { | |
| 244 // Directory entry does not have size and last modified date property key | |
| 245 // values. | |
| 246 *size = 0; | |
| 247 *last_modified_time = base::Time(); | |
| 248 return true; | |
| 249 } | |
| 250 return (GetObjectSize(properties_values.get(), size) && | |
| 251 GetLastModifiedTime(properties_values.get(), last_modified_time)); | |
| 252 } | |
| 253 | |
| 254 // Creates an MTP device object entry for the given |device| and |object_id|. | |
| 255 // On success, returns true and fills in |entry|. | |
| 256 bool GetMTPDeviceObjectEntry(IPortableDevice* device, | |
| 257 const string16& object_id, | |
| 258 MTPDeviceObjectEntry* entry) { | |
| 259 base::ThreadRestrictions::AssertIOAllowed(); | |
| 260 DCHECK(device); | |
| 261 DCHECK(!object_id.empty()); | |
| 262 DCHECK(entry); | |
| 263 string16 name; | |
| 264 bool is_directory; | |
| 265 int64 size; | |
| 266 base::Time last_modified_time; | |
| 267 if (!GetObjectDetails(device, object_id, &name, &is_directory, &size, | |
| 268 &last_modified_time)) | |
| 269 return false; | |
| 270 *entry = MTPDeviceObjectEntry(object_id, name, is_directory, size, | |
| 271 last_modified_time); | |
| 272 return true; | |
| 273 } | |
| 274 | |
| 275 // Gets the entries of the directory specified by |directory_object_id| from | |
| 276 // the given MTP |device|. Set |get_all_entries| to true to get all the entries | |
| 277 // of the directory. To request a specific object entry, set |get_all_entries| | |
| 278 // to false and set |object_name| to the name of the required object. On | |
| 279 // success returns true and set |object_entries|. | |
| 280 bool GetMTPDeviceObjectEntries(IPortableDevice* device, | |
| 281 const string16& directory_object_id, | |
| 282 bool get_all_entries, | |
| 283 const string16& object_name, | |
| 284 MTPDeviceObjectEntries* object_entries) { | |
| 285 base::ThreadRestrictions::AssertIOAllowed(); | |
| 286 DCHECK(device); | |
| 287 DCHECK(!directory_object_id.empty()); | |
| 288 DCHECK(object_entries); | |
| 289 base::win::ScopedComPtr<IEnumPortableDeviceObjectIDs> enum_object_ids; | |
| 290 if (!GetDeviceObjectEnumerator(device, directory_object_id, | |
| 291 enum_object_ids.Receive())) | |
| 292 return false; | |
| 293 | |
| 294 // Loop calling Next() while S_OK is being returned. | |
| 295 DWORD num_objects_to_request = 10; | |
| 296 for (HRESULT hr = S_OK; hr == S_OK;) { | |
| 297 DWORD num_objects_fetched = 0; | |
| 298 scoped_ptr<char16*[]> object_ids(new char16*[num_objects_to_request]); | |
| 299 hr = enum_object_ids->Next(num_objects_to_request, | |
| 300 object_ids.get(), | |
| 301 &num_objects_fetched); | |
| 302 for (DWORD index = 0; index < num_objects_fetched; ++index) { | |
| 303 MTPDeviceObjectEntry entry; | |
| 304 if (GetMTPDeviceObjectEntry(device, | |
| 305 object_ids[index], | |
| 306 &entry)) { | |
| 307 if (get_all_entries) { | |
| 308 object_entries->push_back(entry); | |
| 309 } else if (entry.name == object_name) { | |
| 310 object_entries->push_back(entry); // Object entry found. | |
| 311 break; | |
| 312 } | |
| 313 } | |
| 314 } | |
| 315 for (DWORD index = 0; index < num_objects_fetched; ++index) | |
| 316 CoTaskMemFree(object_ids[index]); | |
| 317 } | |
| 318 return true; | |
| 319 } | |
| 320 | |
| 321 } // namespace | |
| 322 | |
| 323 bool OpenDevice(const string16& pnp_device_id, | |
| 324 base::win::ScopedComPtr<IPortableDevice>* device) { | |
| 325 base::ThreadRestrictions::AssertIOAllowed(); | |
| 326 DCHECK(!pnp_device_id.empty()); | |
| 327 DCHECK(device); | |
| 328 base::win::ScopedComPtr<IPortableDeviceValues> client_info; | |
| 329 if (!GetClientInformation(&client_info)) | |
| 330 return false; | |
| 331 HRESULT hr = device->CreateInstance(__uuidof(PortableDevice), NULL, | |
| 332 CLSCTX_INPROC_SERVER); | |
| 333 if (FAILED(hr)) | |
| 334 return false; | |
| 335 | |
| 336 hr = (*device)->Open(pnp_device_id.c_str(), client_info.get()); | |
| 337 if (SUCCEEDED(hr)) | |
| 338 return true; | |
| 339 if (hr == E_ACCESSDENIED) | |
| 340 DPLOG(ERROR) << "Access denied to open the device"; | |
| 341 return false; | |
| 342 } | |
| 343 | |
| 344 base::PlatformFileError GetFileEntryInfo( | |
| 345 IPortableDevice* device, | |
| 346 const string16& object_id, | |
| 347 base::PlatformFileInfo* file_entry_info) { | |
| 348 DCHECK(device); | |
| 349 DCHECK(!object_id.empty()); | |
| 350 DCHECK(file_entry_info); | |
| 351 MTPDeviceObjectEntry entry; | |
| 352 if (!GetMTPDeviceObjectEntry(device, object_id, &entry)) | |
| 353 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 354 | |
| 355 file_entry_info->size = entry.size; | |
| 356 file_entry_info->is_directory = entry.is_directory; | |
| 357 file_entry_info->is_symbolic_link = false; | |
| 358 file_entry_info->last_modified = entry.last_modified_time; | |
| 359 file_entry_info->last_accessed = entry.last_modified_time; | |
| 360 file_entry_info->creation_time = base::Time(); | |
| 361 return base::PLATFORM_FILE_OK; | |
| 362 } | |
| 363 | |
| 364 bool GetDirectoryEntries(IPortableDevice* device, | |
| 365 const string16& directory_object_id, | |
| 366 MTPDeviceObjectEntries* object_entries) { | |
| 367 return GetMTPDeviceObjectEntries(device, directory_object_id, true, | |
| 368 string16(), object_entries); | |
| 369 } | |
| 370 | |
| 371 bool WriteFileObjectData(IPortableDevice* device, | |
| 372 const string16& file_object_id, | |
| 373 const FilePath& local_path) { | |
| 374 base::ThreadRestrictions::AssertIOAllowed(); | |
| 375 DCHECK(device); | |
| 376 DCHECK(!file_object_id.empty()); | |
| 377 DCHECK(!local_path.value().empty()); | |
| 378 base::win::ScopedComPtr<IPortableDeviceContent> content; | |
| 379 if (!GetDeviceContent(device, content.Receive())) | |
| 380 return false; | |
| 381 | |
| 382 base::win::ScopedComPtr<IPortableDeviceResources> resources; | |
| 383 HRESULT hr = content->Transfer(resources.Receive()); | |
| 384 if (FAILED(hr)) | |
| 385 return false; | |
| 386 | |
| 387 base::win::ScopedComPtr<IStream> file_stream; | |
| 388 DWORD optimal_transfer_size = 0; | |
| 389 hr = resources->GetStream(file_object_id.c_str(), WPD_RESOURCE_DEFAULT, | |
| 390 STGM_READ, &optimal_transfer_size, | |
| 391 file_stream.Receive()); | |
| 392 if (FAILED(hr)) | |
| 393 return false; | |
| 394 return WriteStreamContentsToFile(file_stream.get(), optimal_transfer_size, | |
| 395 local_path); | |
| 396 } | |
| 397 | |
| 398 string16 GetObjectIdFromName(IPortableDevice* device, | |
| 399 const string16& parent_id, | |
| 400 const string16& object_name) { | |
| 401 MTPDeviceObjectEntries object_entries; | |
| 402 if (!GetMTPDeviceObjectEntries(device, parent_id, false, object_name, | |
| 403 &object_entries) || | |
| 404 object_entries.empty()) | |
| 405 return string16(); | |
| 406 DCHECK_EQ(1U, object_entries.size()); | |
| 407 return object_entries[0].object_id; | |
| 408 } | |
| 409 | |
| 410 } // namespace media_transfer_protocol | |
| 411 | |
| 412 } // namespace chrome | |
| OLD | NEW |