| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "app/os_exchange_data_provider_win.h" | |
| 6 | |
| 7 #include "app/l10n_util.h" | |
| 8 #include "base/file_path.h" | |
| 9 #include "base/i18n/file_util_icu.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/pickle.h" | |
| 12 #include "base/scoped_handle.h" | |
| 13 #include "base/stl_util-inl.h" | |
| 14 #include "base/utf_string_conversions.h" | |
| 15 #include "base/win/scoped_hglobal.h" | |
| 16 #include "googleurl/src/gurl.h" | |
| 17 #include "grit/app_strings.h" | |
| 18 #include "net/base/net_util.h" | |
| 19 #include "ui/base/clipboard/clipboard_util_win.h" | |
| 20 | |
| 21 using ui::ClipboardUtil; | |
| 22 | |
| 23 // Creates a new STGMEDIUM object to hold the specified text. The caller | |
| 24 // owns the resulting object. The "Bytes" version does not NULL terminate, the | |
| 25 // string version does. | |
| 26 static STGMEDIUM* GetStorageForBytes(const char* data, size_t bytes); | |
| 27 static STGMEDIUM* GetStorageForWString(const std::wstring& data); | |
| 28 static STGMEDIUM* GetStorageForString(const std::string& data); | |
| 29 // Creates the contents of an Internet Shortcut file for the given URL. | |
| 30 static void GetInternetShortcutFileContents(const GURL& url, std::string* data); | |
| 31 // Creates a valid file name given a suggested title and URL. | |
| 32 static void CreateValidFileNameFromTitle(const GURL& url, | |
| 33 const std::wstring& title, | |
| 34 std::wstring* validated); | |
| 35 // Creates a new STGMEDIUM object to hold a file. | |
| 36 static STGMEDIUM* GetStorageForFileName(const std::wstring& full_path); | |
| 37 // Creates a File Descriptor for the creation of a file to the given URL and | |
| 38 // returns a handle to it. | |
| 39 static STGMEDIUM* GetStorageForFileDescriptor( | |
| 40 const std::wstring& valid_file_name); | |
| 41 | |
| 42 /////////////////////////////////////////////////////////////////////////////// | |
| 43 // FormatEtcEnumerator | |
| 44 | |
| 45 // | |
| 46 // This object implements an enumeration interface. The existence of an | |
| 47 // implementation of this interface is exposed to clients through | |
| 48 // OSExchangeData's EnumFormatEtc method. Our implementation is nobody's | |
| 49 // business but our own, so it lives in this file. | |
| 50 // | |
| 51 // This Windows API is truly a gem. It wants to be an enumerator but assumes | |
| 52 // some sort of sequential data (why not just use an array?). See comments | |
| 53 // throughout. | |
| 54 // | |
| 55 class FormatEtcEnumerator : public IEnumFORMATETC { | |
| 56 public: | |
| 57 FormatEtcEnumerator(DataObjectImpl::StoredData::const_iterator begin, | |
| 58 DataObjectImpl::StoredData::const_iterator end); | |
| 59 ~FormatEtcEnumerator(); | |
| 60 | |
| 61 // IEnumFORMATETC implementation: | |
| 62 HRESULT __stdcall Next( | |
| 63 ULONG count, FORMATETC* elements_array, ULONG* elements_fetched); | |
| 64 HRESULT __stdcall Skip(ULONG skip_count); | |
| 65 HRESULT __stdcall Reset(); | |
| 66 HRESULT __stdcall Clone(IEnumFORMATETC** clone); | |
| 67 | |
| 68 // IUnknown implementation: | |
| 69 HRESULT __stdcall QueryInterface(const IID& iid, void** object); | |
| 70 ULONG __stdcall AddRef(); | |
| 71 ULONG __stdcall Release(); | |
| 72 | |
| 73 private: | |
| 74 // This can only be called from |CloneFromOther|, since it initializes the | |
| 75 // contents_ from the other enumerator's contents. | |
| 76 FormatEtcEnumerator() : ref_count_(0) { | |
| 77 } | |
| 78 | |
| 79 // Clone a new FormatEtc from another instance of this enumeration. | |
| 80 static FormatEtcEnumerator* CloneFromOther(const FormatEtcEnumerator* other); | |
| 81 | |
| 82 private: | |
| 83 // We are _forced_ to use a vector as our internal data model as Windows' | |
| 84 // retarded IEnumFORMATETC API assumes a deterministic ordering of elements | |
| 85 // through methods like Next and Skip. This exposes the underlying data | |
| 86 // structure to the user. Bah. | |
| 87 std::vector<FORMATETC*> contents_; | |
| 88 | |
| 89 // The cursor of the active enumeration - an index into |contents_|. | |
| 90 size_t cursor_; | |
| 91 | |
| 92 LONG ref_count_; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(FormatEtcEnumerator); | |
| 95 }; | |
| 96 | |
| 97 // Safely makes a copy of all of the relevant bits of a FORMATETC object. | |
| 98 static void CloneFormatEtc(FORMATETC* source, FORMATETC* clone) { | |
| 99 *clone = *source; | |
| 100 if (source->ptd) { | |
| 101 source->ptd = | |
| 102 static_cast<DVTARGETDEVICE*>(CoTaskMemAlloc(sizeof(DVTARGETDEVICE))); | |
| 103 *(clone->ptd) = *(source->ptd); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 FormatEtcEnumerator::FormatEtcEnumerator( | |
| 108 DataObjectImpl::StoredData::const_iterator start, | |
| 109 DataObjectImpl::StoredData::const_iterator end) | |
| 110 : ref_count_(0), cursor_(0) { | |
| 111 // Copy FORMATETC data from our source into ourselves. | |
| 112 while (start != end) { | |
| 113 FORMATETC* format_etc = new FORMATETC; | |
| 114 CloneFormatEtc(&(*start)->format_etc, format_etc); | |
| 115 contents_.push_back(format_etc); | |
| 116 ++start; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 FormatEtcEnumerator::~FormatEtcEnumerator() { | |
| 121 STLDeleteContainerPointers(contents_.begin(), contents_.end()); | |
| 122 } | |
| 123 | |
| 124 STDMETHODIMP FormatEtcEnumerator::Next( | |
| 125 ULONG count, FORMATETC* elements_array, ULONG* elements_fetched) { | |
| 126 // MSDN says |elements_fetched| is allowed to be NULL if count is 1. | |
| 127 if (!elements_fetched) | |
| 128 DCHECK(count == 1); | |
| 129 | |
| 130 // This method copies count elements into |elements_array|. | |
| 131 ULONG index = 0; | |
| 132 while (cursor_ < contents_.size() && index < count) { | |
| 133 CloneFormatEtc(contents_[cursor_], &elements_array[index]); | |
| 134 ++cursor_; | |
| 135 ++index; | |
| 136 } | |
| 137 // The out param is for how many we actually copied. | |
| 138 if (elements_fetched) | |
| 139 *elements_fetched = index; | |
| 140 | |
| 141 // If the two don't agree, then we fail. | |
| 142 return index == count ? S_OK : S_FALSE; | |
| 143 } | |
| 144 | |
| 145 STDMETHODIMP FormatEtcEnumerator::Skip(ULONG skip_count) { | |
| 146 cursor_ += skip_count; | |
| 147 // MSDN implies it's OK to leave the enumerator trashed. | |
| 148 // "Whatever you say, boss" | |
| 149 return cursor_ <= contents_.size() ? S_OK : S_FALSE; | |
| 150 } | |
| 151 | |
| 152 STDMETHODIMP FormatEtcEnumerator::Reset() { | |
| 153 cursor_ = 0; | |
| 154 return S_OK; | |
| 155 } | |
| 156 | |
| 157 STDMETHODIMP FormatEtcEnumerator::Clone(IEnumFORMATETC** clone) { | |
| 158 // Clone the current enumerator in its exact state, including cursor. | |
| 159 FormatEtcEnumerator* e = CloneFromOther(this); | |
| 160 e->AddRef(); | |
| 161 *clone = e; | |
| 162 return S_OK; | |
| 163 } | |
| 164 | |
| 165 STDMETHODIMP FormatEtcEnumerator::QueryInterface(const IID& iid, | |
| 166 void** object) { | |
| 167 *object = NULL; | |
| 168 if (IsEqualIID(iid, IID_IUnknown) || IsEqualIID(iid, IID_IEnumFORMATETC)) { | |
| 169 *object = this; | |
| 170 } else { | |
| 171 return E_NOINTERFACE; | |
| 172 } | |
| 173 AddRef(); | |
| 174 return S_OK; | |
| 175 } | |
| 176 | |
| 177 ULONG FormatEtcEnumerator::AddRef() { | |
| 178 return InterlockedIncrement(&ref_count_); | |
| 179 } | |
| 180 | |
| 181 ULONG FormatEtcEnumerator::Release() { | |
| 182 if (InterlockedDecrement(&ref_count_) == 0) { | |
| 183 ULONG copied_refcnt = ref_count_; | |
| 184 delete this; | |
| 185 return copied_refcnt; | |
| 186 } | |
| 187 return ref_count_; | |
| 188 } | |
| 189 | |
| 190 // static | |
| 191 FormatEtcEnumerator* FormatEtcEnumerator::CloneFromOther( | |
| 192 const FormatEtcEnumerator* other) { | |
| 193 FormatEtcEnumerator* e = new FormatEtcEnumerator; | |
| 194 // Copy FORMATETC data from our source into ourselves. | |
| 195 std::vector<FORMATETC*>::const_iterator start = other->contents_.begin(); | |
| 196 while (start != other->contents_.end()) { | |
| 197 FORMATETC* format_etc = new FORMATETC; | |
| 198 CloneFormatEtc(*start, format_etc); | |
| 199 e->contents_.push_back(format_etc); | |
| 200 ++start; | |
| 201 } | |
| 202 // Carry over | |
| 203 e->cursor_ = other->cursor_; | |
| 204 return e; | |
| 205 } | |
| 206 | |
| 207 /////////////////////////////////////////////////////////////////////////////// | |
| 208 // OSExchangeDataProviderWin, public: | |
| 209 | |
| 210 // static | |
| 211 bool OSExchangeDataProviderWin::HasPlainTextURL(IDataObject* source) { | |
| 212 std::wstring plain_text; | |
| 213 return (ClipboardUtil::GetPlainText(source, &plain_text) && | |
| 214 !plain_text.empty() && GURL(plain_text).is_valid()); | |
| 215 } | |
| 216 | |
| 217 // static | |
| 218 bool OSExchangeDataProviderWin::GetPlainTextURL(IDataObject* source, | |
| 219 GURL* url) { | |
| 220 std::wstring plain_text; | |
| 221 if (ClipboardUtil::GetPlainText(source, &plain_text) && | |
| 222 !plain_text.empty()) { | |
| 223 GURL gurl(plain_text); | |
| 224 if (gurl.is_valid()) { | |
| 225 *url = gurl; | |
| 226 return true; | |
| 227 } | |
| 228 } | |
| 229 return false; | |
| 230 } | |
| 231 | |
| 232 // static | |
| 233 DataObjectImpl* OSExchangeDataProviderWin::GetDataObjectImpl( | |
| 234 const OSExchangeData& data) { | |
| 235 return static_cast<const OSExchangeDataProviderWin*>(&data.provider())-> | |
| 236 data_.get(); | |
| 237 } | |
| 238 | |
| 239 // static | |
| 240 IDataObject* OSExchangeDataProviderWin::GetIDataObject( | |
| 241 const OSExchangeData& data) { | |
| 242 return static_cast<const OSExchangeDataProviderWin*>(&data.provider())-> | |
| 243 data_object(); | |
| 244 } | |
| 245 | |
| 246 // static | |
| 247 IAsyncOperation* OSExchangeDataProviderWin::GetIAsyncOperation( | |
| 248 const OSExchangeData& data) { | |
| 249 return static_cast<const OSExchangeDataProviderWin*>(&data.provider())-> | |
| 250 async_operation(); | |
| 251 } | |
| 252 | |
| 253 OSExchangeDataProviderWin::OSExchangeDataProviderWin(IDataObject* source) | |
| 254 : data_(new DataObjectImpl()), | |
| 255 source_object_(source) { | |
| 256 } | |
| 257 | |
| 258 OSExchangeDataProviderWin::OSExchangeDataProviderWin() | |
| 259 : data_(new DataObjectImpl()), | |
| 260 source_object_(data_.get()) { | |
| 261 } | |
| 262 | |
| 263 OSExchangeDataProviderWin::~OSExchangeDataProviderWin() { | |
| 264 } | |
| 265 | |
| 266 void OSExchangeDataProviderWin::SetString(const std::wstring& data) { | |
| 267 STGMEDIUM* storage = GetStorageForWString(data); | |
| 268 data_->contents_.push_back( | |
| 269 new DataObjectImpl::StoredDataInfo(CF_UNICODETEXT, storage)); | |
| 270 | |
| 271 // Also add plain text. | |
| 272 storage = GetStorageForString(WideToUTF8(data)); | |
| 273 data_->contents_.push_back( | |
| 274 new DataObjectImpl::StoredDataInfo(CF_TEXT, storage)); | |
| 275 } | |
| 276 | |
| 277 void OSExchangeDataProviderWin::SetURL(const GURL& url, | |
| 278 const std::wstring& title) { | |
| 279 // NOTE WELL: | |
| 280 // Every time you change the order of the first two CLIPFORMATS that get | |
| 281 // added here, you need to update the EnumerationViaCOM test case in | |
| 282 // the _unittest.cc file to reflect the new arrangement otherwise that test | |
| 283 // will fail! It assumes an insertion order. | |
| 284 | |
| 285 // Add text/x-moz-url for drags from Firefox | |
| 286 std::wstring x_moz_url_str = UTF8ToWide(url.spec()); | |
| 287 x_moz_url_str += '\n'; | |
| 288 x_moz_url_str += title; | |
| 289 STGMEDIUM* storage = GetStorageForWString(x_moz_url_str); | |
| 290 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | |
| 291 ClipboardUtil::GetMozUrlFormat()->cfFormat, storage)); | |
| 292 | |
| 293 // Add a .URL shortcut file for dragging to Explorer. | |
| 294 std::wstring valid_file_name; | |
| 295 CreateValidFileNameFromTitle(url, title, &valid_file_name); | |
| 296 std::string shortcut_url_file_contents; | |
| 297 GetInternetShortcutFileContents(url, &shortcut_url_file_contents); | |
| 298 SetFileContents(valid_file_name, shortcut_url_file_contents); | |
| 299 | |
| 300 // Add a UniformResourceLocator link for apps like IE and Word. | |
| 301 storage = GetStorageForWString(UTF8ToWide(url.spec())); | |
| 302 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | |
| 303 ClipboardUtil::GetUrlWFormat()->cfFormat, storage)); | |
| 304 storage = GetStorageForString(url.spec()); | |
| 305 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | |
| 306 ClipboardUtil::GetUrlFormat()->cfFormat, storage)); | |
| 307 | |
| 308 // TODO(beng): add CF_HTML. | |
| 309 // http://code.google.com/p/chromium/issues/detail?id=6767 | |
| 310 | |
| 311 // Also add text representations (these should be last since they're the | |
| 312 // least preferable). | |
| 313 storage = GetStorageForWString(UTF8ToWide(url.spec())); | |
| 314 data_->contents_.push_back( | |
| 315 new DataObjectImpl::StoredDataInfo(CF_UNICODETEXT, storage)); | |
| 316 storage = GetStorageForString(url.spec()); | |
| 317 data_->contents_.push_back( | |
| 318 new DataObjectImpl::StoredDataInfo(CF_TEXT, storage)); | |
| 319 } | |
| 320 | |
| 321 void OSExchangeDataProviderWin::SetFilename(const std::wstring& full_path) { | |
| 322 STGMEDIUM* storage = GetStorageForFileName(full_path); | |
| 323 DataObjectImpl::StoredDataInfo* info = | |
| 324 new DataObjectImpl::StoredDataInfo(CF_HDROP, storage); | |
| 325 data_->contents_.push_back(info); | |
| 326 } | |
| 327 | |
| 328 void OSExchangeDataProviderWin::SetPickledData(CLIPFORMAT format, | |
| 329 const Pickle& data) { | |
| 330 STGMEDIUM* storage = GetStorageForString( | |
| 331 std::string(static_cast<const char *>(data.data()), | |
| 332 static_cast<size_t>(data.size()))); | |
| 333 data_->contents_.push_back( | |
| 334 new DataObjectImpl::StoredDataInfo(format, storage)); | |
| 335 } | |
| 336 | |
| 337 void OSExchangeDataProviderWin::SetFileContents( | |
| 338 const std::wstring& filename, | |
| 339 const std::string& file_contents) { | |
| 340 // Add CFSTR_FILEDESCRIPTOR | |
| 341 STGMEDIUM* storage = GetStorageForFileDescriptor(filename); | |
| 342 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | |
| 343 ClipboardUtil::GetFileDescriptorFormat()->cfFormat, storage)); | |
| 344 | |
| 345 // Add CFSTR_FILECONTENTS | |
| 346 storage = GetStorageForBytes(file_contents.data(), file_contents.length()); | |
| 347 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | |
| 348 ClipboardUtil::GetFileContentFormatZero(), storage)); | |
| 349 } | |
| 350 | |
| 351 void OSExchangeDataProviderWin::SetHtml(const std::wstring& html, | |
| 352 const GURL& base_url) { | |
| 353 // Add both MS CF_HTML and text/html format. CF_HTML should be in utf-8. | |
| 354 std::string utf8_html = WideToUTF8(html); | |
| 355 std::string url = base_url.is_valid() ? base_url.spec() : std::string(); | |
| 356 | |
| 357 std::string cf_html = ClipboardUtil::HtmlToCFHtml(utf8_html, url); | |
| 358 STGMEDIUM* storage = GetStorageForBytes(cf_html.c_str(), cf_html.size()); | |
| 359 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | |
| 360 ClipboardUtil::GetHtmlFormat()->cfFormat, storage)); | |
| 361 | |
| 362 STGMEDIUM* storage_plain = GetStorageForBytes(utf8_html.c_str(), | |
| 363 utf8_html.size()); | |
| 364 data_->contents_.push_back(new DataObjectImpl::StoredDataInfo( | |
| 365 ClipboardUtil::GetTextHtmlFormat()->cfFormat, storage_plain)); | |
| 366 } | |
| 367 | |
| 368 bool OSExchangeDataProviderWin::GetString(std::wstring* data) const { | |
| 369 return ClipboardUtil::GetPlainText(source_object_, data); | |
| 370 } | |
| 371 | |
| 372 bool OSExchangeDataProviderWin::GetURLAndTitle(GURL* url, | |
| 373 std::wstring* title) const { | |
| 374 std::wstring url_str; | |
| 375 bool success = ClipboardUtil::GetUrl(source_object_, &url_str, title, true); | |
| 376 if (success) { | |
| 377 GURL test_url(url_str); | |
| 378 if (test_url.is_valid()) { | |
| 379 *url = test_url; | |
| 380 return true; | |
| 381 } | |
| 382 } else if (GetPlainTextURL(source_object_, url)) { | |
| 383 title->clear(); | |
| 384 return true; | |
| 385 } | |
| 386 return false; | |
| 387 } | |
| 388 | |
| 389 bool OSExchangeDataProviderWin::GetFilename(std::wstring* full_path) const { | |
| 390 std::vector<std::wstring> filenames; | |
| 391 bool success = ClipboardUtil::GetFilenames(source_object_, &filenames); | |
| 392 if (success) | |
| 393 full_path->assign(filenames[0]); | |
| 394 return success; | |
| 395 } | |
| 396 | |
| 397 bool OSExchangeDataProviderWin::GetPickledData(CLIPFORMAT format, | |
| 398 Pickle* data) const { | |
| 399 DCHECK(data); | |
| 400 FORMATETC format_etc = | |
| 401 { format, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; | |
| 402 bool success = false; | |
| 403 STGMEDIUM medium; | |
| 404 if (SUCCEEDED(source_object_->GetData(&format_etc, &medium))) { | |
| 405 if (medium.tymed & TYMED_HGLOBAL) { | |
| 406 base::win::ScopedHGlobal<char> c_data(medium.hGlobal); | |
| 407 DCHECK(c_data.Size() > 0); | |
| 408 // Need to subtract 1 as SetPickledData adds an extra byte to the end. | |
| 409 *data = Pickle(c_data.get(), static_cast<int>(c_data.Size() - 1)); | |
| 410 success = true; | |
| 411 } | |
| 412 ReleaseStgMedium(&medium); | |
| 413 } | |
| 414 return success; | |
| 415 } | |
| 416 | |
| 417 bool OSExchangeDataProviderWin::GetFileContents( | |
| 418 std::wstring* filename, | |
| 419 std::string* file_contents) const { | |
| 420 return ClipboardUtil::GetFileContents(source_object_, filename, | |
| 421 file_contents); | |
| 422 } | |
| 423 | |
| 424 bool OSExchangeDataProviderWin::GetHtml(std::wstring* html, | |
| 425 GURL* base_url) const { | |
| 426 std::string url; | |
| 427 bool success = ClipboardUtil::GetHtml(source_object_, html, &url); | |
| 428 if (success) | |
| 429 *base_url = GURL(url); | |
| 430 return success; | |
| 431 } | |
| 432 | |
| 433 bool OSExchangeDataProviderWin::HasString() const { | |
| 434 return ClipboardUtil::HasPlainText(source_object_); | |
| 435 } | |
| 436 | |
| 437 bool OSExchangeDataProviderWin::HasURL() const { | |
| 438 return (ClipboardUtil::HasUrl(source_object_) || | |
| 439 HasPlainTextURL(source_object_)); | |
| 440 } | |
| 441 | |
| 442 bool OSExchangeDataProviderWin::HasFile() const { | |
| 443 return ClipboardUtil::HasFilenames(source_object_); | |
| 444 } | |
| 445 | |
| 446 bool OSExchangeDataProviderWin::HasFileContents() const { | |
| 447 return ClipboardUtil::HasFileContents(source_object_); | |
| 448 } | |
| 449 | |
| 450 bool OSExchangeDataProviderWin::HasHtml() const { | |
| 451 return ClipboardUtil::HasHtml(source_object_); | |
| 452 } | |
| 453 | |
| 454 bool OSExchangeDataProviderWin::HasCustomFormat(CLIPFORMAT format) const { | |
| 455 FORMATETC format_etc = | |
| 456 { format, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; | |
| 457 return (source_object_->QueryGetData(&format_etc) == S_OK); | |
| 458 } | |
| 459 | |
| 460 void OSExchangeDataProviderWin::SetDownloadFileInfo( | |
| 461 const OSExchangeData::DownloadFileInfo& download) { | |
| 462 // If the filename is not provided, set stoarge to NULL to indicate that | |
| 463 // the delay rendering will be used. | |
| 464 STGMEDIUM* storage = NULL; | |
| 465 if (!download.filename.empty()) | |
| 466 storage = GetStorageForFileName(download.filename.value()); | |
| 467 | |
| 468 // Add CF_HDROP. | |
| 469 DataObjectImpl::StoredDataInfo* info = new DataObjectImpl::StoredDataInfo( | |
| 470 ClipboardUtil::GetCFHDropFormat()->cfFormat, storage); | |
| 471 info->downloader = download.downloader; | |
| 472 data_->contents_.push_back(info); | |
| 473 } | |
| 474 | |
| 475 /////////////////////////////////////////////////////////////////////////////// | |
| 476 // DataObjectImpl, IDataObject implementation: | |
| 477 | |
| 478 // The following function, DuplicateMedium, is derived from WCDataObject.cpp | |
| 479 // in the WebKit source code. This is the license information for the file: | |
| 480 /* | |
| 481 * Copyright (C) 2007 Apple Inc. All rights reserved. | |
| 482 * | |
| 483 * Redistribution and use in source and binary forms, with or without | |
| 484 * modification, are permitted provided that the following conditions | |
| 485 * are met: | |
| 486 * 1. Redistributions of source code must retain the above copyright | |
| 487 * notice, this list of conditions and the following disclaimer. | |
| 488 * 2. Redistributions in binary form must reproduce the above copyright | |
| 489 * notice, this list of conditions and the following disclaimer in the | |
| 490 * documentation and/or other materials provided with the distribution. | |
| 491 * | |
| 492 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 493 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 494 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 495 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 496 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 497 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 498 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 499 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 500 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 501 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 502 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 503 */ | |
| 504 static void DuplicateMedium(CLIPFORMAT source_clipformat, | |
| 505 STGMEDIUM* source, | |
| 506 STGMEDIUM* destination) { | |
| 507 switch (source->tymed) { | |
| 508 case TYMED_HGLOBAL: | |
| 509 destination->hGlobal = | |
| 510 static_cast<HGLOBAL>(OleDuplicateData( | |
| 511 source->hGlobal, source_clipformat, 0)); | |
| 512 break; | |
| 513 case TYMED_MFPICT: | |
| 514 destination->hMetaFilePict = | |
| 515 static_cast<HMETAFILEPICT>(OleDuplicateData( | |
| 516 source->hMetaFilePict, source_clipformat, 0)); | |
| 517 break; | |
| 518 case TYMED_GDI: | |
| 519 destination->hBitmap = | |
| 520 static_cast<HBITMAP>(OleDuplicateData( | |
| 521 source->hBitmap, source_clipformat, 0)); | |
| 522 break; | |
| 523 case TYMED_ENHMF: | |
| 524 destination->hEnhMetaFile = | |
| 525 static_cast<HENHMETAFILE>(OleDuplicateData( | |
| 526 source->hEnhMetaFile, source_clipformat, 0)); | |
| 527 break; | |
| 528 case TYMED_FILE: | |
| 529 destination->lpszFileName = | |
| 530 static_cast<LPOLESTR>(OleDuplicateData( | |
| 531 source->lpszFileName, source_clipformat, 0)); | |
| 532 break; | |
| 533 case TYMED_ISTREAM: | |
| 534 destination->pstm = source->pstm; | |
| 535 destination->pstm->AddRef(); | |
| 536 break; | |
| 537 case TYMED_ISTORAGE: | |
| 538 destination->pstg = source->pstg; | |
| 539 destination->pstg->AddRef(); | |
| 540 break; | |
| 541 } | |
| 542 | |
| 543 destination->tymed = source->tymed; | |
| 544 destination->pUnkForRelease = source->pUnkForRelease; | |
| 545 if (destination->pUnkForRelease) | |
| 546 destination->pUnkForRelease->AddRef(); | |
| 547 } | |
| 548 | |
| 549 DataObjectImpl::DataObjectImpl() | |
| 550 : is_aborting_(false), | |
| 551 in_async_mode_(false), | |
| 552 async_operation_started_(false), | |
| 553 observer_(NULL) { | |
| 554 } | |
| 555 | |
| 556 DataObjectImpl::~DataObjectImpl() { | |
| 557 StopDownloads(); | |
| 558 STLDeleteContainerPointers(contents_.begin(), contents_.end()); | |
| 559 if (observer_) | |
| 560 observer_->OnDataObjectDisposed(); | |
| 561 } | |
| 562 | |
| 563 void DataObjectImpl::StopDownloads() { | |
| 564 for (StoredData::iterator iter = contents_.begin(); | |
| 565 iter != contents_.end(); ++iter) { | |
| 566 if ((*iter)->downloader.get()) { | |
| 567 (*iter)->downloader->Stop(); | |
| 568 (*iter)->downloader = 0; | |
| 569 } | |
| 570 } | |
| 571 } | |
| 572 | |
| 573 void DataObjectImpl::OnDownloadCompleted(const FilePath& file_path) { | |
| 574 CLIPFORMAT hdrop_format = ClipboardUtil::GetCFHDropFormat()->cfFormat; | |
| 575 DataObjectImpl::StoredData::iterator iter = contents_.begin(); | |
| 576 for (; iter != contents_.end(); ++iter) { | |
| 577 if ((*iter)->format_etc.cfFormat == hdrop_format) { | |
| 578 // Release the old storage. | |
| 579 if ((*iter)->owns_medium) { | |
| 580 ReleaseStgMedium((*iter)->medium); | |
| 581 delete (*iter)->medium; | |
| 582 } | |
| 583 | |
| 584 // Update the storage. | |
| 585 (*iter)->owns_medium = true; | |
| 586 (*iter)->medium = GetStorageForFileName(file_path.value()); | |
| 587 | |
| 588 break; | |
| 589 } | |
| 590 } | |
| 591 DCHECK(iter != contents_.end()); | |
| 592 } | |
| 593 | |
| 594 void DataObjectImpl::OnDownloadAborted() { | |
| 595 } | |
| 596 | |
| 597 HRESULT DataObjectImpl::GetData(FORMATETC* format_etc, STGMEDIUM* medium) { | |
| 598 if (is_aborting_) | |
| 599 return DV_E_FORMATETC; | |
| 600 | |
| 601 StoredData::iterator iter = contents_.begin(); | |
| 602 while (iter != contents_.end()) { | |
| 603 if ((*iter)->format_etc.cfFormat == format_etc->cfFormat && | |
| 604 (*iter)->format_etc.lindex == format_etc->lindex && | |
| 605 ((*iter)->format_etc.tymed & format_etc->tymed)) { | |
| 606 // If medium is NULL, delay-rendering will be used. | |
| 607 if ((*iter)->medium) { | |
| 608 DuplicateMedium((*iter)->format_etc.cfFormat, (*iter)->medium, medium); | |
| 609 } else { | |
| 610 // Check if the left button is down. | |
| 611 bool is_left_button_down = (GetKeyState(VK_LBUTTON) & 0x8000) != 0; | |
| 612 | |
| 613 bool wait_for_data = false; | |
| 614 if ((*iter)->in_delay_rendering) { | |
| 615 // Make sure the left button is up. Sometimes the drop target, like | |
| 616 // Shell, might be too aggresive in calling GetData when the left | |
| 617 // button is not released. | |
| 618 if (is_left_button_down) | |
| 619 return DV_E_FORMATETC; | |
| 620 | |
| 621 // In async mode, we do not want to start waiting for the data before | |
| 622 // the async operation is started. This is because we want to postpone | |
| 623 // until Shell kicks off a background thread to do the work so that | |
| 624 // we do not block the UI thread. | |
| 625 if (!in_async_mode_ || async_operation_started_) | |
| 626 wait_for_data = true; | |
| 627 } else { | |
| 628 // If the left button is up and the target has not requested the data | |
| 629 // yet, it probably means that the target does not support delay- | |
| 630 // rendering. So instead, we wait for the data. | |
| 631 if (is_left_button_down) { | |
| 632 (*iter)->in_delay_rendering = true; | |
| 633 memset(medium, 0, sizeof(STGMEDIUM)); | |
| 634 } else { | |
| 635 wait_for_data = true; | |
| 636 } | |
| 637 } | |
| 638 | |
| 639 if (!wait_for_data) | |
| 640 return DV_E_FORMATETC; | |
| 641 | |
| 642 // Notify the observer we start waiting for the data. This gives | |
| 643 // an observer a chance to end the drag and drop. | |
| 644 if (observer_) | |
| 645 observer_->OnWaitForData(); | |
| 646 | |
| 647 // Now we can start the download. | |
| 648 if ((*iter)->downloader.get()) { | |
| 649 if (!(*iter)->downloader->Start(this)) { | |
| 650 is_aborting_ = true; | |
| 651 return DV_E_FORMATETC; | |
| 652 } | |
| 653 } | |
| 654 | |
| 655 // The stored data should have been updated with the final version. | |
| 656 // So we just need to call this function again to retrieve it. | |
| 657 return GetData(format_etc, medium); | |
| 658 } | |
| 659 return S_OK; | |
| 660 } | |
| 661 ++iter; | |
| 662 } | |
| 663 | |
| 664 return DV_E_FORMATETC; | |
| 665 } | |
| 666 | |
| 667 HRESULT DataObjectImpl::GetDataHere(FORMATETC* format_etc, | |
| 668 STGMEDIUM* medium) { | |
| 669 return DATA_E_FORMATETC; | |
| 670 } | |
| 671 | |
| 672 HRESULT DataObjectImpl::QueryGetData(FORMATETC* format_etc) { | |
| 673 StoredData::const_iterator iter = contents_.begin(); | |
| 674 while (iter != contents_.end()) { | |
| 675 if ((*iter)->format_etc.cfFormat == format_etc->cfFormat) | |
| 676 return S_OK; | |
| 677 ++iter; | |
| 678 } | |
| 679 return DV_E_FORMATETC; | |
| 680 } | |
| 681 | |
| 682 HRESULT DataObjectImpl::GetCanonicalFormatEtc( | |
| 683 FORMATETC* format_etc, FORMATETC* result) { | |
| 684 format_etc->ptd = NULL; | |
| 685 return E_NOTIMPL; | |
| 686 } | |
| 687 | |
| 688 HRESULT DataObjectImpl::SetData( | |
| 689 FORMATETC* format_etc, STGMEDIUM* medium, BOOL should_release) { | |
| 690 STGMEDIUM* local_medium = new STGMEDIUM; | |
| 691 if (should_release) { | |
| 692 *local_medium = *medium; | |
| 693 } else { | |
| 694 DuplicateMedium(format_etc->cfFormat, medium, local_medium); | |
| 695 } | |
| 696 | |
| 697 DataObjectImpl::StoredDataInfo* info = | |
| 698 new DataObjectImpl::StoredDataInfo(format_etc->cfFormat, local_medium); | |
| 699 info->medium->tymed = format_etc->tymed; | |
| 700 info->owns_medium = !!should_release; | |
| 701 contents_.push_back(info); | |
| 702 | |
| 703 return S_OK; | |
| 704 } | |
| 705 | |
| 706 HRESULT DataObjectImpl::EnumFormatEtc( | |
| 707 DWORD direction, IEnumFORMATETC** enumerator) { | |
| 708 if (direction == DATADIR_GET) { | |
| 709 FormatEtcEnumerator* e = | |
| 710 new FormatEtcEnumerator(contents_.begin(), contents_.end()); | |
| 711 e->AddRef(); | |
| 712 *enumerator = e; | |
| 713 return S_OK; | |
| 714 } | |
| 715 return E_NOTIMPL; | |
| 716 } | |
| 717 | |
| 718 HRESULT DataObjectImpl::DAdvise( | |
| 719 FORMATETC* format_etc, DWORD advf, IAdviseSink* sink, DWORD* connection) { | |
| 720 return OLE_E_ADVISENOTSUPPORTED; | |
| 721 } | |
| 722 | |
| 723 HRESULT DataObjectImpl::DUnadvise(DWORD connection) { | |
| 724 return OLE_E_ADVISENOTSUPPORTED; | |
| 725 } | |
| 726 | |
| 727 HRESULT DataObjectImpl::EnumDAdvise(IEnumSTATDATA** enumerator) { | |
| 728 return OLE_E_ADVISENOTSUPPORTED; | |
| 729 } | |
| 730 | |
| 731 /////////////////////////////////////////////////////////////////////////////// | |
| 732 // DataObjectImpl, IAsyncOperation implementation: | |
| 733 | |
| 734 HRESULT DataObjectImpl::EndOperation( | |
| 735 HRESULT result, IBindCtx* reserved, DWORD effects) { | |
| 736 async_operation_started_ = false; | |
| 737 return S_OK; | |
| 738 } | |
| 739 | |
| 740 HRESULT DataObjectImpl::GetAsyncMode(BOOL* is_op_async) { | |
| 741 *is_op_async = in_async_mode_ ? TRUE : FALSE; | |
| 742 return S_OK; | |
| 743 } | |
| 744 | |
| 745 HRESULT DataObjectImpl::InOperation(BOOL* in_async_op) { | |
| 746 *in_async_op = async_operation_started_ ? TRUE : FALSE; | |
| 747 return S_OK; | |
| 748 } | |
| 749 | |
| 750 HRESULT DataObjectImpl::SetAsyncMode(BOOL do_op_async) { | |
| 751 in_async_mode_ = (do_op_async == TRUE); | |
| 752 return S_OK; | |
| 753 } | |
| 754 | |
| 755 HRESULT DataObjectImpl::StartOperation(IBindCtx* reserved) { | |
| 756 async_operation_started_ = true; | |
| 757 return S_OK; | |
| 758 } | |
| 759 | |
| 760 /////////////////////////////////////////////////////////////////////////////// | |
| 761 // DataObjectImpl, IUnknown implementation: | |
| 762 | |
| 763 HRESULT DataObjectImpl::QueryInterface(const IID& iid, void** object) { | |
| 764 if (!object) | |
| 765 return E_POINTER; | |
| 766 if (IsEqualIID(iid, IID_IDataObject) || IsEqualIID(iid, IID_IUnknown)) { | |
| 767 *object = static_cast<IDataObject*>(this); | |
| 768 } else if (in_async_mode_ && IsEqualIID(iid, IID_IAsyncOperation)) { | |
| 769 *object = static_cast<IAsyncOperation*>(this); | |
| 770 } else { | |
| 771 *object = NULL; | |
| 772 return E_NOINTERFACE; | |
| 773 } | |
| 774 AddRef(); | |
| 775 return S_OK; | |
| 776 } | |
| 777 | |
| 778 ULONG DataObjectImpl::AddRef() { | |
| 779 base::RefCountedThreadSafe<DownloadFileObserver>::AddRef(); | |
| 780 return 0; | |
| 781 } | |
| 782 | |
| 783 ULONG DataObjectImpl::Release() { | |
| 784 base::RefCountedThreadSafe<DownloadFileObserver>::Release(); | |
| 785 return 0; | |
| 786 } | |
| 787 | |
| 788 /////////////////////////////////////////////////////////////////////////////// | |
| 789 // DataObjectImpl, private: | |
| 790 | |
| 791 static STGMEDIUM* GetStorageForBytes(const char* data, size_t bytes) { | |
| 792 HANDLE handle = GlobalAlloc(GPTR, static_cast<int>(bytes)); | |
| 793 base::win::ScopedHGlobal<char> scoped(handle); | |
| 794 size_t allocated = static_cast<size_t>(GlobalSize(handle)); | |
| 795 memcpy(scoped.get(), data, allocated); | |
| 796 | |
| 797 STGMEDIUM* storage = new STGMEDIUM; | |
| 798 storage->hGlobal = handle; | |
| 799 storage->tymed = TYMED_HGLOBAL; | |
| 800 storage->pUnkForRelease = NULL; | |
| 801 return storage; | |
| 802 } | |
| 803 | |
| 804 template<class T> | |
| 805 static HGLOBAL CopyStringToGlobalHandle(const T& payload) { | |
| 806 int bytes = static_cast<int>(payload.size() + 1) * sizeof(T::value_type); | |
| 807 HANDLE handle = GlobalAlloc(GPTR, bytes); | |
| 808 void* data = GlobalLock(handle); | |
| 809 size_t allocated = static_cast<size_t>(GlobalSize(handle)); | |
| 810 memcpy(data, payload.c_str(), allocated); | |
| 811 static_cast<T::value_type*>(data)[payload.size()] = '\0'; | |
| 812 GlobalUnlock(handle); | |
| 813 return handle; | |
| 814 } | |
| 815 | |
| 816 static STGMEDIUM* GetStorageForWString(const std::wstring& data) { | |
| 817 STGMEDIUM* storage = new STGMEDIUM; | |
| 818 storage->hGlobal = CopyStringToGlobalHandle<std::wstring>(data); | |
| 819 storage->tymed = TYMED_HGLOBAL; | |
| 820 storage->pUnkForRelease = NULL; | |
| 821 return storage; | |
| 822 } | |
| 823 | |
| 824 static STGMEDIUM* GetStorageForString(const std::string& data) { | |
| 825 STGMEDIUM* storage = new STGMEDIUM; | |
| 826 storage->hGlobal = CopyStringToGlobalHandle<std::string>(data); | |
| 827 storage->tymed = TYMED_HGLOBAL; | |
| 828 storage->pUnkForRelease = NULL; | |
| 829 return storage; | |
| 830 } | |
| 831 | |
| 832 static void GetInternetShortcutFileContents(const GURL& url, | |
| 833 std::string* data) { | |
| 834 DCHECK(data); | |
| 835 static const std::string kInternetShortcutFileStart = | |
| 836 "[InternetShortcut]\r\nURL="; | |
| 837 static const std::string kInternetShortcutFileEnd = | |
| 838 "\r\n"; | |
| 839 *data = kInternetShortcutFileStart + url.spec() + kInternetShortcutFileEnd; | |
| 840 } | |
| 841 | |
| 842 static void CreateValidFileNameFromTitle(const GURL& url, | |
| 843 const std::wstring& title, | |
| 844 std::wstring* validated) { | |
| 845 if (title.empty()) { | |
| 846 if (url.is_valid()) { | |
| 847 *validated = net::GetSuggestedFilename( | |
| 848 url, std::string(), std::string(), FilePath()).ToWStringHack(); | |
| 849 } else { | |
| 850 // Nothing else can be done, just use a default. | |
| 851 *validated = | |
| 852 l10n_util::GetStringUTF16(IDS_APP_UNTITLED_SHORTCUT_FILE_NAME); | |
| 853 } | |
| 854 } else { | |
| 855 *validated = title; | |
| 856 file_util::ReplaceIllegalCharactersInPath(validated, '-'); | |
| 857 } | |
| 858 static const wchar_t extension[] = L".url"; | |
| 859 static const size_t max_length = MAX_PATH - arraysize(extension); | |
| 860 if (validated->size() > max_length) | |
| 861 validated->erase(max_length); | |
| 862 *validated += extension; | |
| 863 } | |
| 864 | |
| 865 static STGMEDIUM* GetStorageForFileName(const std::wstring& full_path) { | |
| 866 const size_t kDropSize = sizeof(DROPFILES); | |
| 867 const size_t kTotalBytes = | |
| 868 kDropSize + (full_path.length() + 2) * sizeof(wchar_t); | |
| 869 HANDLE hdata = GlobalAlloc(GMEM_MOVEABLE, kTotalBytes); | |
| 870 | |
| 871 base::win::ScopedHGlobal<DROPFILES> locked_mem(hdata); | |
| 872 DROPFILES* drop_files = locked_mem.get(); | |
| 873 drop_files->pFiles = sizeof(DROPFILES); | |
| 874 drop_files->fWide = TRUE; | |
| 875 wchar_t* data = reinterpret_cast<wchar_t*>( | |
| 876 reinterpret_cast<BYTE*>(drop_files) + kDropSize); | |
| 877 const size_t copy_size = (full_path.length() + 1) * sizeof(wchar_t); | |
| 878 memcpy(data, full_path.c_str(), copy_size); | |
| 879 data[full_path.length() + 1] = L'\0'; // Double NULL | |
| 880 | |
| 881 STGMEDIUM* storage = new STGMEDIUM; | |
| 882 storage->tymed = TYMED_HGLOBAL; | |
| 883 storage->hGlobal = drop_files; | |
| 884 storage->pUnkForRelease = NULL; | |
| 885 return storage; | |
| 886 } | |
| 887 | |
| 888 static STGMEDIUM* GetStorageForFileDescriptor( | |
| 889 const std::wstring& valid_file_name) { | |
| 890 DCHECK(!valid_file_name.empty() && valid_file_name.size() + 1 <= MAX_PATH); | |
| 891 HANDLE handle = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR)); | |
| 892 FILEGROUPDESCRIPTOR* descriptor = | |
| 893 reinterpret_cast<FILEGROUPDESCRIPTOR*>(GlobalLock(handle)); | |
| 894 | |
| 895 descriptor->cItems = 1; | |
| 896 wcscpy_s(descriptor->fgd[0].cFileName, | |
| 897 valid_file_name.size() + 1, | |
| 898 valid_file_name.c_str()); | |
| 899 descriptor->fgd[0].dwFlags = FD_LINKUI; | |
| 900 | |
| 901 GlobalUnlock(handle); | |
| 902 | |
| 903 STGMEDIUM* storage = new STGMEDIUM; | |
| 904 storage->hGlobal = handle; | |
| 905 storage->tymed = TYMED_HGLOBAL; | |
| 906 storage->pUnkForRelease = NULL; | |
| 907 return storage; | |
| 908 } | |
| 909 | |
| 910 | |
| 911 /////////////////////////////////////////////////////////////////////////////// | |
| 912 // OSExchangeData, public: | |
| 913 | |
| 914 // static | |
| 915 OSExchangeData::Provider* OSExchangeData::CreateProvider() { | |
| 916 return new OSExchangeDataProviderWin(); | |
| 917 } | |
| 918 | |
| 919 // static | |
| 920 OSExchangeData::CustomFormat OSExchangeData::RegisterCustomFormat( | |
| 921 const std::string& type) { | |
| 922 return RegisterClipboardFormat(ASCIIToWide(type).c_str()); | |
| 923 } | |
| OLD | NEW |