| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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> |
| 8 #include <vector> |
| 9 |
| 7 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 8 #include "base/i18n/file_util_icu.h" | 11 #include "base/i18n/file_util_icu.h" |
| 9 #include "base/logging.h" | 12 #include "base/logging.h" |
| 10 #include "base/memory/scoped_handle.h" | 13 #include "base/memory/scoped_handle.h" |
| 11 #include "base/pickle.h" | 14 #include "base/pickle.h" |
| 12 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 13 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 14 #include "base/win/scoped_hglobal.h" | 17 #include "base/win/scoped_hglobal.h" |
| 15 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 16 #include "grit/ui_strings.h" | 19 #include "grit/ui_strings.h" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 } | 120 } |
| 118 | 121 |
| 119 FormatEtcEnumerator::~FormatEtcEnumerator() { | 122 FormatEtcEnumerator::~FormatEtcEnumerator() { |
| 120 STLDeleteContainerPointers(contents_.begin(), contents_.end()); | 123 STLDeleteContainerPointers(contents_.begin(), contents_.end()); |
| 121 } | 124 } |
| 122 | 125 |
| 123 STDMETHODIMP FormatEtcEnumerator::Next( | 126 STDMETHODIMP FormatEtcEnumerator::Next( |
| 124 ULONG count, FORMATETC* elements_array, ULONG* elements_fetched) { | 127 ULONG count, FORMATETC* elements_array, ULONG* elements_fetched) { |
| 125 // MSDN says |elements_fetched| is allowed to be NULL if count is 1. | 128 // MSDN says |elements_fetched| is allowed to be NULL if count is 1. |
| 126 if (!elements_fetched) | 129 if (!elements_fetched) |
| 127 DCHECK(count == 1); | 130 DCHECK_EQ(count, 1ul); |
| 128 | 131 |
| 129 // This method copies count elements into |elements_array|. | 132 // This method copies count elements into |elements_array|. |
| 130 ULONG index = 0; | 133 ULONG index = 0; |
| 131 while (cursor_ < contents_.size() && index < count) { | 134 while (cursor_ < contents_.size() && index < count) { |
| 132 CloneFormatEtc(contents_[cursor_], &elements_array[index]); | 135 CloneFormatEtc(contents_[cursor_], &elements_array[index]); |
| 133 ++cursor_; | 136 ++cursor_; |
| 134 ++index; | 137 ++index; |
| 135 } | 138 } |
| 136 // The out param is for how many we actually copied. | 139 // The out param is for how many we actually copied. |
| 137 if (elements_fetched) | 140 if (elements_fetched) |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 bool OSExchangeDataProviderWin::GetPickledData(CLIPFORMAT format, | 399 bool OSExchangeDataProviderWin::GetPickledData(CLIPFORMAT format, |
| 397 Pickle* data) const { | 400 Pickle* data) const { |
| 398 DCHECK(data); | 401 DCHECK(data); |
| 399 FORMATETC format_etc = | 402 FORMATETC format_etc = |
| 400 { format, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; | 403 { format, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; |
| 401 bool success = false; | 404 bool success = false; |
| 402 STGMEDIUM medium; | 405 STGMEDIUM medium; |
| 403 if (SUCCEEDED(source_object_->GetData(&format_etc, &medium))) { | 406 if (SUCCEEDED(source_object_->GetData(&format_etc, &medium))) { |
| 404 if (medium.tymed & TYMED_HGLOBAL) { | 407 if (medium.tymed & TYMED_HGLOBAL) { |
| 405 base::win::ScopedHGlobal<char> c_data(medium.hGlobal); | 408 base::win::ScopedHGlobal<char> c_data(medium.hGlobal); |
| 406 DCHECK(c_data.Size() > 0); | 409 DCHECK_GT(c_data.Size(), 0u); |
| 407 // Need to subtract 1 as SetPickledData adds an extra byte to the end. | 410 // Need to subtract 1 as SetPickledData adds an extra byte to the end. |
| 408 *data = Pickle(c_data.get(), static_cast<int>(c_data.Size() - 1)); | 411 *data = Pickle(c_data.get(), static_cast<int>(c_data.Size() - 1)); |
| 409 success = true; | 412 success = true; |
| 410 } | 413 } |
| 411 ReleaseStgMedium(&medium); | 414 ReleaseStgMedium(&medium); |
| 412 } | 415 } |
| 413 return success; | 416 return success; |
| 414 } | 417 } |
| 415 | 418 |
| 416 bool OSExchangeDataProviderWin::GetFileContents( | 419 bool OSExchangeDataProviderWin::GetFileContents( |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 drop_files->pFiles = sizeof(DROPFILES); | 899 drop_files->pFiles = sizeof(DROPFILES); |
| 897 drop_files->fWide = TRUE; | 900 drop_files->fWide = TRUE; |
| 898 wchar_t* data = reinterpret_cast<wchar_t*>( | 901 wchar_t* data = reinterpret_cast<wchar_t*>( |
| 899 reinterpret_cast<BYTE*>(drop_files) + kDropSize); | 902 reinterpret_cast<BYTE*>(drop_files) + kDropSize); |
| 900 const size_t copy_size = (path.value().length() + 1) * sizeof(wchar_t); | 903 const size_t copy_size = (path.value().length() + 1) * sizeof(wchar_t); |
| 901 memcpy(data, path.value().c_str(), copy_size); | 904 memcpy(data, path.value().c_str(), copy_size); |
| 902 data[path.value().length() + 1] = L'\0'; // Double NULL | 905 data[path.value().length() + 1] = L'\0'; // Double NULL |
| 903 | 906 |
| 904 STGMEDIUM* storage = new STGMEDIUM; | 907 STGMEDIUM* storage = new STGMEDIUM; |
| 905 storage->tymed = TYMED_HGLOBAL; | 908 storage->tymed = TYMED_HGLOBAL; |
| 906 storage->hGlobal = drop_files; | 909 storage->hGlobal = hdata; |
| 907 storage->pUnkForRelease = NULL; | 910 storage->pUnkForRelease = NULL; |
| 908 return storage; | 911 return storage; |
| 909 } | 912 } |
| 910 | 913 |
| 911 static STGMEDIUM* GetStorageForFileDescriptor( | 914 static STGMEDIUM* GetStorageForFileDescriptor( |
| 912 const FilePath& path) { | 915 const FilePath& path) { |
| 913 string16 valid_file_name = path.value(); | 916 string16 file_name = path.value(); |
| 914 DCHECK(!valid_file_name.empty() && valid_file_name.size() + 1 <= MAX_PATH); | 917 DCHECK(!file_name.empty()); |
| 915 HANDLE handle = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR)); | 918 HANDLE hdata = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR)); |
| 916 FILEGROUPDESCRIPTOR* descriptor = | 919 base::win::ScopedHGlobal<FILEGROUPDESCRIPTOR> locked_mem(hdata); |
| 917 reinterpret_cast<FILEGROUPDESCRIPTOR*>(GlobalLock(handle)); | |
| 918 | 920 |
| 921 FILEGROUPDESCRIPTOR* descriptor = locked_mem.get(); |
| 919 descriptor->cItems = 1; | 922 descriptor->cItems = 1; |
| 920 wcscpy_s(descriptor->fgd[0].cFileName, | |
| 921 valid_file_name.size() + 1, | |
| 922 valid_file_name.c_str()); | |
| 923 descriptor->fgd[0].dwFlags = FD_LINKUI; | 923 descriptor->fgd[0].dwFlags = FD_LINKUI; |
| 924 | 924 wcsncpy_s(descriptor->fgd[0].cFileName, |
| 925 GlobalUnlock(handle); | 925 MAX_PATH, |
| 926 file_name.c_str(), |
| 927 std::min(file_name.size(), MAX_PATH - 1u)); |
| 926 | 928 |
| 927 STGMEDIUM* storage = new STGMEDIUM; | 929 STGMEDIUM* storage = new STGMEDIUM; |
| 928 storage->hGlobal = handle; | |
| 929 storage->tymed = TYMED_HGLOBAL; | 930 storage->tymed = TYMED_HGLOBAL; |
| 931 storage->hGlobal = hdata; |
| 930 storage->pUnkForRelease = NULL; | 932 storage->pUnkForRelease = NULL; |
| 931 return storage; | 933 return storage; |
| 932 } | 934 } |
| 933 | 935 |
| 934 | |
| 935 /////////////////////////////////////////////////////////////////////////////// | 936 /////////////////////////////////////////////////////////////////////////////// |
| 936 // OSExchangeData, public: | 937 // OSExchangeData, public: |
| 937 | 938 |
| 938 // static | 939 // static |
| 939 OSExchangeData::Provider* OSExchangeData::CreateProvider() { | 940 OSExchangeData::Provider* OSExchangeData::CreateProvider() { |
| 940 return new OSExchangeDataProviderWin(); | 941 return new OSExchangeDataProviderWin(); |
| 941 } | 942 } |
| 942 | 943 |
| 943 // static | 944 // static |
| 944 OSExchangeData::CustomFormat OSExchangeData::RegisterCustomFormat( | 945 OSExchangeData::CustomFormat OSExchangeData::RegisterCustomFormat( |
| 945 const std::string& type) { | 946 const std::string& type) { |
| 946 return RegisterClipboardFormat(ASCIIToUTF16(type).c_str()); | 947 return RegisterClipboardFormat(ASCIIToUTF16(type).c_str()); |
| 947 } | 948 } |
| 948 | 949 |
| 949 } // namespace ui | 950 } // namespace ui |
| OLD | NEW |