OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" | 5 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 static STGMEDIUM* GetStorageForBytes(const char* data, size_t bytes); | 29 static STGMEDIUM* GetStorageForBytes(const char* data, size_t bytes); |
30 static STGMEDIUM* GetStorageForString16(const string16& data); | 30 static STGMEDIUM* GetStorageForString16(const string16& data); |
31 static STGMEDIUM* GetStorageForString(const std::string& data); | 31 static STGMEDIUM* GetStorageForString(const std::string& data); |
32 // Creates the contents of an Internet Shortcut file for the given URL. | 32 // Creates the contents of an Internet Shortcut file for the given URL. |
33 static void GetInternetShortcutFileContents(const GURL& url, std::string* data); | 33 static void GetInternetShortcutFileContents(const GURL& url, std::string* data); |
34 // Creates a valid file name given a suggested title and URL. | 34 // Creates a valid file name given a suggested title and URL. |
35 static void CreateValidFileNameFromTitle(const GURL& url, | 35 static void CreateValidFileNameFromTitle(const GURL& url, |
36 const string16& title, | 36 const string16& title, |
37 string16* validated); | 37 string16* validated); |
38 // Creates a new STGMEDIUM object to hold a file. | 38 // Creates a new STGMEDIUM object to hold a file. |
39 static STGMEDIUM* GetStorageForFileName(const FilePath& path); | 39 static STGMEDIUM* GetStorageForFileName(const base::FilePath& path); |
40 // Creates a File Descriptor for the creation of a file to the given URL and | 40 // Creates a File Descriptor for the creation of a file to the given URL and |
41 // returns a handle to it. | 41 // returns a handle to it. |
42 static STGMEDIUM* GetStorageForFileDescriptor(const FilePath& path); | 42 static STGMEDIUM* GetStorageForFileDescriptor(const base::FilePath& path); |
43 | 43 |
44 /////////////////////////////////////////////////////////////////////////////// | 44 /////////////////////////////////////////////////////////////////////////////// |
45 // FormatEtcEnumerator | 45 // FormatEtcEnumerator |
46 | 46 |
47 // | 47 // |
48 // This object implements an enumeration interface. The existence of an | 48 // This object implements an enumeration interface. The existence of an |
49 // implementation of this interface is exposed to clients through | 49 // implementation of this interface is exposed to clients through |
50 // OSExchangeData's EnumFormatEtc method. Our implementation is nobody's | 50 // OSExchangeData's EnumFormatEtc method. Our implementation is nobody's |
51 // business but our own, so it lives in this file. | 51 // business but our own, so it lives in this file. |
52 // | 52 // |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 x_moz_url_str += title; | 290 x_moz_url_str += title; |
291 STGMEDIUM* storage = GetStorageForString16(x_moz_url_str); | 291 STGMEDIUM* storage = GetStorageForString16(x_moz_url_str); |
292 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | 292 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( |
293 ClipboardUtil::GetMozUrlFormat()->cfFormat, storage)); | 293 ClipboardUtil::GetMozUrlFormat()->cfFormat, storage)); |
294 | 294 |
295 // Add a .URL shortcut file for dragging to Explorer. | 295 // Add a .URL shortcut file for dragging to Explorer. |
296 string16 valid_file_name; | 296 string16 valid_file_name; |
297 CreateValidFileNameFromTitle(url, title, &valid_file_name); | 297 CreateValidFileNameFromTitle(url, title, &valid_file_name); |
298 std::string shortcut_url_file_contents; | 298 std::string shortcut_url_file_contents; |
299 GetInternetShortcutFileContents(url, &shortcut_url_file_contents); | 299 GetInternetShortcutFileContents(url, &shortcut_url_file_contents); |
300 SetFileContents(FilePath(valid_file_name), shortcut_url_file_contents); | 300 SetFileContents(base::FilePath(valid_file_name), shortcut_url_file_contents); |
301 | 301 |
302 // Add a UniformResourceLocator link for apps like IE and Word. | 302 // Add a UniformResourceLocator link for apps like IE and Word. |
303 storage = GetStorageForString16(UTF8ToUTF16(url.spec())); | 303 storage = GetStorageForString16(UTF8ToUTF16(url.spec())); |
304 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | 304 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( |
305 ClipboardUtil::GetUrlWFormat()->cfFormat, storage)); | 305 ClipboardUtil::GetUrlWFormat()->cfFormat, storage)); |
306 storage = GetStorageForString(url.spec()); | 306 storage = GetStorageForString(url.spec()); |
307 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | 307 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( |
308 ClipboardUtil::GetUrlFormat()->cfFormat, storage)); | 308 ClipboardUtil::GetUrlFormat()->cfFormat, storage)); |
309 | 309 |
310 // TODO(beng): add CF_HTML. | 310 // TODO(beng): add CF_HTML. |
311 // http://code.google.com/p/chromium/issues/detail?id=6767 | 311 // http://code.google.com/p/chromium/issues/detail?id=6767 |
312 | 312 |
313 // Also add text representations (these should be last since they're the | 313 // Also add text representations (these should be last since they're the |
314 // least preferable). | 314 // least preferable). |
315 storage = GetStorageForString16(UTF8ToUTF16(url.spec())); | 315 storage = GetStorageForString16(UTF8ToUTF16(url.spec())); |
316 data_->contents_.push_back( | 316 data_->contents_.push_back( |
317 new DataObjectImpl::StoredDataInfo(CF_UNICODETEXT, storage)); | 317 new DataObjectImpl::StoredDataInfo(CF_UNICODETEXT, storage)); |
318 storage = GetStorageForString(url.spec()); | 318 storage = GetStorageForString(url.spec()); |
319 data_->contents_.push_back( | 319 data_->contents_.push_back( |
320 new DataObjectImpl::StoredDataInfo(CF_TEXT, storage)); | 320 new DataObjectImpl::StoredDataInfo(CF_TEXT, storage)); |
321 } | 321 } |
322 | 322 |
323 void OSExchangeDataProviderWin::SetFilename(const FilePath& path) { | 323 void OSExchangeDataProviderWin::SetFilename(const base::FilePath& path) { |
324 STGMEDIUM* storage = GetStorageForFileName(path); | 324 STGMEDIUM* storage = GetStorageForFileName(path); |
325 DataObjectImpl::StoredDataInfo* info = | 325 DataObjectImpl::StoredDataInfo* info = |
326 new DataObjectImpl::StoredDataInfo(CF_HDROP, storage); | 326 new DataObjectImpl::StoredDataInfo(CF_HDROP, storage); |
327 data_->contents_.push_back(info); | 327 data_->contents_.push_back(info); |
328 } | 328 } |
329 | 329 |
330 void OSExchangeDataProviderWin::SetFilenames( | 330 void OSExchangeDataProviderWin::SetFilenames( |
331 const std::vector<OSExchangeData::FileInfo>& filenames) { | 331 const std::vector<OSExchangeData::FileInfo>& filenames) { |
332 for (size_t i = 0; i < filenames.size(); ++i) { | 332 for (size_t i = 0; i < filenames.size(); ++i) { |
333 STGMEDIUM* storage = GetStorageForFileName(filenames[i].path); | 333 STGMEDIUM* storage = GetStorageForFileName(filenames[i].path); |
334 DataObjectImpl::StoredDataInfo* info = | 334 DataObjectImpl::StoredDataInfo* info = |
335 new DataObjectImpl::StoredDataInfo(CF_HDROP, storage); | 335 new DataObjectImpl::StoredDataInfo(CF_HDROP, storage); |
336 data_->contents_.push_back(info); | 336 data_->contents_.push_back(info); |
337 } | 337 } |
338 } | 338 } |
339 | 339 |
340 void OSExchangeDataProviderWin::SetPickledData(CLIPFORMAT format, | 340 void OSExchangeDataProviderWin::SetPickledData(CLIPFORMAT format, |
341 const Pickle& data) { | 341 const Pickle& data) { |
342 STGMEDIUM* storage = GetStorageForBytes(static_cast<const char*>(data.data()), | 342 STGMEDIUM* storage = GetStorageForBytes(static_cast<const char*>(data.data()), |
343 data.size()); | 343 data.size()); |
344 data_->contents_.push_back( | 344 data_->contents_.push_back( |
345 new DataObjectImpl::StoredDataInfo(format, storage)); | 345 new DataObjectImpl::StoredDataInfo(format, storage)); |
346 } | 346 } |
347 | 347 |
348 void OSExchangeDataProviderWin::SetFileContents( | 348 void OSExchangeDataProviderWin::SetFileContents( |
349 const FilePath& filename, | 349 const base::FilePath& filename, |
350 const std::string& file_contents) { | 350 const std::string& file_contents) { |
351 // Add CFSTR_FILEDESCRIPTOR | 351 // Add CFSTR_FILEDESCRIPTOR |
352 STGMEDIUM* storage = GetStorageForFileDescriptor(filename); | 352 STGMEDIUM* storage = GetStorageForFileDescriptor(filename); |
353 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | 353 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( |
354 ClipboardUtil::GetFileDescriptorFormat()->cfFormat, storage)); | 354 ClipboardUtil::GetFileDescriptorFormat()->cfFormat, storage)); |
355 | 355 |
356 // Add CFSTR_FILECONTENTS | 356 // Add CFSTR_FILECONTENTS |
357 storage = GetStorageForBytes(file_contents.data(), file_contents.length()); | 357 storage = GetStorageForBytes(file_contents.data(), file_contents.length()); |
358 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | 358 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( |
359 ClipboardUtil::GetFileContentFormatZero(), storage)); | 359 ClipboardUtil::GetFileContentFormatZero(), storage)); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 } else if (GetPlainTextURL(source_object_, url)) { | 393 } else if (GetPlainTextURL(source_object_, url)) { |
394 if (url->is_valid()) | 394 if (url->is_valid()) |
395 *title = net::GetSuggestedFilename(*url, "", "", "", "", std::string()); | 395 *title = net::GetSuggestedFilename(*url, "", "", "", "", std::string()); |
396 else | 396 else |
397 title->clear(); | 397 title->clear(); |
398 return true; | 398 return true; |
399 } | 399 } |
400 return false; | 400 return false; |
401 } | 401 } |
402 | 402 |
403 bool OSExchangeDataProviderWin::GetFilename(FilePath* path) const { | 403 bool OSExchangeDataProviderWin::GetFilename(base::FilePath* path) const { |
404 std::vector<string16> filenames; | 404 std::vector<string16> filenames; |
405 bool success = ClipboardUtil::GetFilenames(source_object_, &filenames); | 405 bool success = ClipboardUtil::GetFilenames(source_object_, &filenames); |
406 if (success) | 406 if (success) |
407 *path = FilePath(filenames[0]); | 407 *path = base::FilePath(filenames[0]); |
408 return success; | 408 return success; |
409 } | 409 } |
410 | 410 |
411 bool OSExchangeDataProviderWin::GetFilenames( | 411 bool OSExchangeDataProviderWin::GetFilenames( |
412 std::vector<OSExchangeData::FileInfo>* filenames) const { | 412 std::vector<OSExchangeData::FileInfo>* filenames) const { |
413 std::vector<string16> filenames_local; | 413 std::vector<string16> filenames_local; |
414 bool success = ClipboardUtil::GetFilenames(source_object_, &filenames_local); | 414 bool success = ClipboardUtil::GetFilenames(source_object_, &filenames_local); |
415 if (success) { | 415 if (success) { |
416 for (size_t i = 0; i < filenames_local.size(); ++i) | 416 for (size_t i = 0; i < filenames_local.size(); ++i) |
417 filenames->push_back( | 417 filenames->push_back( |
418 OSExchangeData::FileInfo(FilePath(filenames_local[i]), FilePath())); | 418 OSExchangeData::FileInfo(base::FilePath(filenames_local[i]), |
| 419 base::FilePath())); |
419 } | 420 } |
420 return success; | 421 return success; |
421 } | 422 } |
422 | 423 |
423 bool OSExchangeDataProviderWin::GetPickledData(CLIPFORMAT format, | 424 bool OSExchangeDataProviderWin::GetPickledData(CLIPFORMAT format, |
424 Pickle* data) const { | 425 Pickle* data) const { |
425 DCHECK(data); | 426 DCHECK(data); |
426 FORMATETC format_etc = | 427 FORMATETC format_etc = |
427 { format, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; | 428 { format, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; |
428 bool success = false; | 429 bool success = false; |
429 STGMEDIUM medium; | 430 STGMEDIUM medium; |
430 if (SUCCEEDED(source_object_->GetData(&format_etc, &medium))) { | 431 if (SUCCEEDED(source_object_->GetData(&format_etc, &medium))) { |
431 if (medium.tymed & TYMED_HGLOBAL) { | 432 if (medium.tymed & TYMED_HGLOBAL) { |
432 base::win::ScopedHGlobal<char> c_data(medium.hGlobal); | 433 base::win::ScopedHGlobal<char> c_data(medium.hGlobal); |
433 DCHECK_GT(c_data.Size(), 0u); | 434 DCHECK_GT(c_data.Size(), 0u); |
434 *data = Pickle(c_data.get(), static_cast<int>(c_data.Size())); | 435 *data = Pickle(c_data.get(), static_cast<int>(c_data.Size())); |
435 success = true; | 436 success = true; |
436 } | 437 } |
437 ReleaseStgMedium(&medium); | 438 ReleaseStgMedium(&medium); |
438 } | 439 } |
439 return success; | 440 return success; |
440 } | 441 } |
441 | 442 |
442 bool OSExchangeDataProviderWin::GetFileContents( | 443 bool OSExchangeDataProviderWin::GetFileContents( |
443 FilePath* filename, | 444 base::FilePath* filename, |
444 std::string* file_contents) const { | 445 std::string* file_contents) const { |
445 string16 filename_str; | 446 string16 filename_str; |
446 if (!ClipboardUtil::GetFileContents(source_object_, &filename_str, | 447 if (!ClipboardUtil::GetFileContents(source_object_, &filename_str, |
447 file_contents)) { | 448 file_contents)) { |
448 return false; | 449 return false; |
449 } | 450 } |
450 *filename = FilePath(filename_str); | 451 *filename = base::FilePath(filename_str); |
451 return true; | 452 return true; |
452 } | 453 } |
453 | 454 |
454 bool OSExchangeDataProviderWin::GetHtml(string16* html, | 455 bool OSExchangeDataProviderWin::GetHtml(string16* html, |
455 GURL* base_url) const { | 456 GURL* base_url) const { |
456 std::string url; | 457 std::string url; |
457 bool success = ClipboardUtil::GetHtml(source_object_, html, &url); | 458 bool success = ClipboardUtil::GetHtml(source_object_, html, &url); |
458 if (success) | 459 if (success) |
459 *base_url = GURL(url); | 460 *base_url = GURL(url); |
460 return success; | 461 return success; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 format.dwAspect == (*i)->format_etc.dwAspect && | 630 format.dwAspect == (*i)->format_etc.dwAspect && |
630 format.lindex == (*i)->format_etc.lindex && | 631 format.lindex == (*i)->format_etc.lindex && |
631 format.tymed == (*i)->format_etc.tymed) { | 632 format.tymed == (*i)->format_etc.tymed) { |
632 delete *i; | 633 delete *i; |
633 contents_.erase(i); | 634 contents_.erase(i); |
634 return; | 635 return; |
635 } | 636 } |
636 } | 637 } |
637 } | 638 } |
638 | 639 |
639 void DataObjectImpl::OnDownloadCompleted(const FilePath& file_path) { | 640 void DataObjectImpl::OnDownloadCompleted(const base::FilePath& file_path) { |
640 CLIPFORMAT hdrop_format = ClipboardUtil::GetCFHDropFormat()->cfFormat; | 641 CLIPFORMAT hdrop_format = ClipboardUtil::GetCFHDropFormat()->cfFormat; |
641 DataObjectImpl::StoredData::iterator iter = contents_.begin(); | 642 DataObjectImpl::StoredData::iterator iter = contents_.begin(); |
642 for (; iter != contents_.end(); ++iter) { | 643 for (; iter != contents_.end(); ++iter) { |
643 if ((*iter)->format_etc.cfFormat == hdrop_format) { | 644 if ((*iter)->format_etc.cfFormat == hdrop_format) { |
644 // Release the old storage. | 645 // Release the old storage. |
645 if ((*iter)->owns_medium) { | 646 if ((*iter)->owns_medium) { |
646 ReleaseStgMedium((*iter)->medium); | 647 ReleaseStgMedium((*iter)->medium); |
647 delete (*iter)->medium; | 648 delete (*iter)->medium; |
648 } | 649 } |
649 | 650 |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
927 *validated = title; | 928 *validated = title; |
928 file_util::ReplaceIllegalCharactersInPath(validated, '-'); | 929 file_util::ReplaceIllegalCharactersInPath(validated, '-'); |
929 } | 930 } |
930 static const wchar_t extension[] = L".url"; | 931 static const wchar_t extension[] = L".url"; |
931 static const size_t max_length = MAX_PATH - arraysize(extension); | 932 static const size_t max_length = MAX_PATH - arraysize(extension); |
932 if (validated->size() > max_length) | 933 if (validated->size() > max_length) |
933 validated->erase(max_length); | 934 validated->erase(max_length); |
934 *validated += extension; | 935 *validated += extension; |
935 } | 936 } |
936 | 937 |
937 static STGMEDIUM* GetStorageForFileName(const FilePath& path) { | 938 static STGMEDIUM* GetStorageForFileName(const base::FilePath& path) { |
938 const size_t kDropSize = sizeof(DROPFILES); | 939 const size_t kDropSize = sizeof(DROPFILES); |
939 const size_t kTotalBytes = | 940 const size_t kTotalBytes = |
940 kDropSize + (path.value().length() + 2) * sizeof(wchar_t); | 941 kDropSize + (path.value().length() + 2) * sizeof(wchar_t); |
941 HANDLE hdata = GlobalAlloc(GMEM_MOVEABLE, kTotalBytes); | 942 HANDLE hdata = GlobalAlloc(GMEM_MOVEABLE, kTotalBytes); |
942 | 943 |
943 base::win::ScopedHGlobal<DROPFILES> locked_mem(hdata); | 944 base::win::ScopedHGlobal<DROPFILES> locked_mem(hdata); |
944 DROPFILES* drop_files = locked_mem.get(); | 945 DROPFILES* drop_files = locked_mem.get(); |
945 drop_files->pFiles = sizeof(DROPFILES); | 946 drop_files->pFiles = sizeof(DROPFILES); |
946 drop_files->fWide = TRUE; | 947 drop_files->fWide = TRUE; |
947 wchar_t* data = reinterpret_cast<wchar_t*>( | 948 wchar_t* data = reinterpret_cast<wchar_t*>( |
948 reinterpret_cast<BYTE*>(drop_files) + kDropSize); | 949 reinterpret_cast<BYTE*>(drop_files) + kDropSize); |
949 const size_t copy_size = (path.value().length() + 1) * sizeof(wchar_t); | 950 const size_t copy_size = (path.value().length() + 1) * sizeof(wchar_t); |
950 memcpy(data, path.value().c_str(), copy_size); | 951 memcpy(data, path.value().c_str(), copy_size); |
951 data[path.value().length() + 1] = L'\0'; // Double NULL | 952 data[path.value().length() + 1] = L'\0'; // Double NULL |
952 | 953 |
953 STGMEDIUM* storage = new STGMEDIUM; | 954 STGMEDIUM* storage = new STGMEDIUM; |
954 storage->tymed = TYMED_HGLOBAL; | 955 storage->tymed = TYMED_HGLOBAL; |
955 storage->hGlobal = hdata; | 956 storage->hGlobal = hdata; |
956 storage->pUnkForRelease = NULL; | 957 storage->pUnkForRelease = NULL; |
957 return storage; | 958 return storage; |
958 } | 959 } |
959 | 960 |
960 static STGMEDIUM* GetStorageForFileDescriptor( | 961 static STGMEDIUM* GetStorageForFileDescriptor( |
961 const FilePath& path) { | 962 const base::FilePath& path) { |
962 string16 file_name = path.value(); | 963 string16 file_name = path.value(); |
963 DCHECK(!file_name.empty()); | 964 DCHECK(!file_name.empty()); |
964 HANDLE hdata = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR)); | 965 HANDLE hdata = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR)); |
965 base::win::ScopedHGlobal<FILEGROUPDESCRIPTOR> locked_mem(hdata); | 966 base::win::ScopedHGlobal<FILEGROUPDESCRIPTOR> locked_mem(hdata); |
966 | 967 |
967 FILEGROUPDESCRIPTOR* descriptor = locked_mem.get(); | 968 FILEGROUPDESCRIPTOR* descriptor = locked_mem.get(); |
968 descriptor->cItems = 1; | 969 descriptor->cItems = 1; |
969 descriptor->fgd[0].dwFlags = FD_LINKUI; | 970 descriptor->fgd[0].dwFlags = FD_LINKUI; |
970 wcsncpy_s(descriptor->fgd[0].cFileName, MAX_PATH, file_name.c_str(), | 971 wcsncpy_s(descriptor->fgd[0].cFileName, MAX_PATH, file_name.c_str(), |
971 std::min(file_name.size(), static_cast<size_t>(MAX_PATH - 1u))); | 972 std::min(file_name.size(), static_cast<size_t>(MAX_PATH - 1u))); |
(...skipping 13 matching lines...) Expand all Loading... |
985 return new OSExchangeDataProviderWin(); | 986 return new OSExchangeDataProviderWin(); |
986 } | 987 } |
987 | 988 |
988 // static | 989 // static |
989 OSExchangeData::CustomFormat OSExchangeData::RegisterCustomFormat( | 990 OSExchangeData::CustomFormat OSExchangeData::RegisterCustomFormat( |
990 const std::string& type) { | 991 const std::string& type) { |
991 return RegisterClipboardFormat(ASCIIToUTF16(type).c_str()); | 992 return RegisterClipboardFormat(ASCIIToUTF16(type).c_str()); |
992 } | 993 } |
993 | 994 |
994 } // namespace ui | 995 } // namespace ui |
OLD | NEW |