Chromium Code Reviews| 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(), static_cast<size_t>(0)); |
| 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 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 = drop_files; |
| 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 valid_file_name = path.value(); |
| 914 DCHECK(!valid_file_name.empty() && valid_file_name.size() + 1 <= MAX_PATH); | 917 DCHECK(!valid_file_name.empty()); |
| 915 HANDLE handle = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR)); | 918 HANDLE handle = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR)); |
| 916 FILEGROUPDESCRIPTOR* descriptor = | 919 FILEGROUPDESCRIPTOR* descriptor = |
| 917 reinterpret_cast<FILEGROUPDESCRIPTOR*>(GlobalLock(handle)); | 920 reinterpret_cast<FILEGROUPDESCRIPTOR*>(GlobalLock(handle)); |
| 918 | 921 |
| 919 descriptor->cItems = 1; | 922 descriptor->cItems = 1; |
| 920 wcscpy_s(descriptor->fgd[0].cFileName, | 923 { |
| 921 valid_file_name.size() + 1, | 924 string16 extension = path.Extension(); |
| 922 valid_file_name.c_str()); | 925 int extension_len = extension.size(); |
| 926 size_t max_name_len = MAX_PATH - extension_len - 1; | |
|
sky
2011/09/06 15:55:05
Is it possible that extension_len is >= MAX_PATH -
| |
| 927 size_t orig_name_len = valid_file_name.size() - extension_len; | |
| 928 int name_len = std::min(max_name_len, orig_name_len); | |
| 929 | |
| 930 memcpy(descriptor->fgd[0].cFileName, | |
| 931 valid_file_name.c_str(), | |
| 932 sizeof(wchar_t) * name_len); | |
| 933 | |
| 934 wcscpy_s(descriptor->fgd[0].cFileName + name_len, | |
| 935 extension_len + 1, | |
| 936 extension.c_str()); | |
| 937 } | |
| 938 | |
| 923 descriptor->fgd[0].dwFlags = FD_LINKUI; | 939 descriptor->fgd[0].dwFlags = FD_LINKUI; |
| 924 | 940 |
| 925 GlobalUnlock(handle); | 941 GlobalUnlock(handle); |
| 926 | 942 |
| 927 STGMEDIUM* storage = new STGMEDIUM; | 943 STGMEDIUM* storage = new STGMEDIUM; |
| 928 storage->hGlobal = handle; | 944 storage->hGlobal = handle; |
| 929 storage->tymed = TYMED_HGLOBAL; | 945 storage->tymed = TYMED_HGLOBAL; |
| 930 storage->pUnkForRelease = NULL; | 946 storage->pUnkForRelease = NULL; |
| 931 return storage; | 947 return storage; |
| 932 } | 948 } |
| 933 | 949 |
| 934 | |
| 935 /////////////////////////////////////////////////////////////////////////////// | 950 /////////////////////////////////////////////////////////////////////////////// |
| 936 // OSExchangeData, public: | 951 // OSExchangeData, public: |
| 937 | 952 |
| 938 // static | 953 // static |
| 939 OSExchangeData::Provider* OSExchangeData::CreateProvider() { | 954 OSExchangeData::Provider* OSExchangeData::CreateProvider() { |
| 940 return new OSExchangeDataProviderWin(); | 955 return new OSExchangeDataProviderWin(); |
| 941 } | 956 } |
| 942 | 957 |
| 943 // static | 958 // static |
| 944 OSExchangeData::CustomFormat OSExchangeData::RegisterCustomFormat( | 959 OSExchangeData::CustomFormat OSExchangeData::RegisterCustomFormat( |
| 945 const std::string& type) { | 960 const std::string& type) { |
| 946 return RegisterClipboardFormat(ASCIIToUTF16(type).c_str()); | 961 return RegisterClipboardFormat(ASCIIToUTF16(type).c_str()); |
| 947 } | 962 } |
| 948 | 963 |
| 949 } // namespace ui | 964 } // namespace ui |
| OLD | NEW |