OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // Many of these functions are based on those found in | 5 // Many of these functions are based on those found in |
6 // webkit/port/platform/PasteboardWin.cpp | 6 // webkit/port/platform/PasteboardWin.cpp |
7 | 7 |
8 #include "base/clipboard.h" | 8 #include "base/clipboard.h" |
9 | 9 |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 size_t url_len) { | 183 size_t url_len) { |
184 std::string markup(markup_data, markup_len); | 184 std::string markup(markup_data, markup_len); |
185 std::string url; | 185 std::string url; |
186 | 186 |
187 if (url_len > 0) | 187 if (url_len > 0) |
188 url.assign(url_data, url_len); | 188 url.assign(url_data, url_len); |
189 | 189 |
190 std::string html_fragment = ClipboardUtil::HtmlToCFHtml(markup, url); | 190 std::string html_fragment = ClipboardUtil::HtmlToCFHtml(markup, url); |
191 HGLOBAL glob = CreateGlobalData(html_fragment); | 191 HGLOBAL glob = CreateGlobalData(html_fragment); |
192 | 192 |
193 WriteToClipboard(GetHtmlFormatType(), glob); | 193 WriteToClipboard(StringToInt(GetHtmlFormatType()), glob); |
194 } | 194 } |
195 | 195 |
196 void Clipboard::WriteBookmark(const char* title_data, | 196 void Clipboard::WriteBookmark(const char* title_data, |
197 size_t title_len, | 197 size_t title_len, |
198 const char* url_data, | 198 const char* url_data, |
199 size_t url_len) { | 199 size_t url_len) { |
200 std::string bookmark(title_data, title_len); | 200 std::string bookmark(title_data, title_len); |
201 bookmark.append(1, L'\n'); | 201 bookmark.append(1, L'\n'); |
202 bookmark.append(url_data, url_len); | 202 bookmark.append(url_data, url_len); |
203 | 203 |
204 string16 wide_bookmark = UTF8ToWide(bookmark); | 204 string16 wide_bookmark = UTF8ToWide(bookmark); |
205 HGLOBAL glob = CreateGlobalData(wide_bookmark); | 205 HGLOBAL glob = CreateGlobalData(wide_bookmark); |
206 | 206 |
207 WriteToClipboard(GetUrlWFormatType(), glob); | 207 WriteToClipboard(StringToInt(GetUrlWFormatType()), glob); |
208 } | 208 } |
209 | 209 |
210 void Clipboard::WriteHyperlink(const char* title_data, | 210 void Clipboard::WriteHyperlink(const char* title_data, |
211 size_t title_len, | 211 size_t title_len, |
212 const char* url_data, | 212 const char* url_data, |
213 size_t url_len) { | 213 size_t url_len) { |
214 // Store as a bookmark. | 214 // Store as a bookmark. |
215 WriteBookmark(title_data, title_len, url_data, url_len); | 215 WriteBookmark(title_data, title_len, url_data, url_len); |
216 | 216 |
217 std::string title(title_data, title_len), | 217 std::string title(title_data, title_len), |
218 url(url_data, url_len), | 218 url(url_data, url_len), |
219 link("<a href=\""); | 219 link("<a href=\""); |
220 | 220 |
221 // Construct the hyperlink. | 221 // Construct the hyperlink. |
222 link.append(url); | 222 link.append(url); |
223 link.append("\">"); | 223 link.append("\">"); |
224 link.append(title); | 224 link.append(title); |
225 link.append("</a>"); | 225 link.append("</a>"); |
226 | 226 |
227 // Store hyperlink as html. | 227 // Store hyperlink as html. |
228 WriteHTML(link.c_str(), link.size(), NULL, 0); | 228 WriteHTML(link.c_str(), link.size(), NULL, 0); |
229 } | 229 } |
230 | 230 |
231 void Clipboard::WriteWebSmartPaste() { | 231 void Clipboard::WriteWebSmartPaste() { |
232 DCHECK(clipboard_owner_); | 232 DCHECK(clipboard_owner_); |
233 ::SetClipboardData(GetWebKitSmartPasteFormatType(), NULL); | 233 ::SetClipboardData(StringToInt(GetWebKitSmartPasteFormatType()), NULL); |
234 } | 234 } |
235 | 235 |
236 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { | 236 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { |
237 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data); | 237 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data); |
238 HDC dc = ::GetDC(NULL); | 238 HDC dc = ::GetDC(NULL); |
239 | 239 |
240 // This doesn't actually cost us a memcpy when the bitmap comes from the | 240 // This doesn't actually cost us a memcpy when the bitmap comes from the |
241 // renderer as we load it into the bitmap using setPixels which just sets a | 241 // renderer as we load it into the bitmap using setPixels which just sets a |
242 // pointer. Someone has to memcpy it into GDI, it might as well be us here. | 242 // pointer. Someone has to memcpy it into GDI, it might as well be us here. |
243 | 243 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 DROPFILES* drop_files = reinterpret_cast<DROPFILES*>(data); | 364 DROPFILES* drop_files = reinterpret_cast<DROPFILES*>(data); |
365 drop_files->pFiles = sizeof(DROPFILES); | 365 drop_files->pFiles = sizeof(DROPFILES); |
366 drop_files->fWide = TRUE; | 366 drop_files->fWide = TRUE; |
367 | 367 |
368 memcpy(data + sizeof(DROPFILES), file_data, file_len); | 368 memcpy(data + sizeof(DROPFILES), file_data, file_len); |
369 | 369 |
370 ::GlobalUnlock(hdata); | 370 ::GlobalUnlock(hdata); |
371 WriteToClipboard(CF_HDROP, hdata); | 371 WriteToClipboard(CF_HDROP, hdata); |
372 } | 372 } |
373 | 373 |
374 void Clipboard::WriteToClipboard(FormatType format, HANDLE handle) { | 374 void Clipboard::WriteToClipboard(unsigned int format, HANDLE handle) { |
375 DCHECK(clipboard_owner_); | 375 DCHECK(clipboard_owner_); |
376 if (handle && !::SetClipboardData(format, handle)) { | 376 if (handle && !::SetClipboardData(format, handle)) { |
377 DCHECK(ERROR_CLIPBOARD_NOT_OPEN != GetLastError()); | 377 DCHECK(ERROR_CLIPBOARD_NOT_OPEN != GetLastError()); |
378 FreeData(format, handle); | 378 FreeData(format, handle); |
379 } | 379 } |
380 } | 380 } |
381 | 381 |
382 bool Clipboard::IsFormatAvailable(unsigned int format) const { | 382 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format) const { |
383 return ::IsClipboardFormatAvailable(format) != FALSE; | 383 return ::IsClipboardFormatAvailable(StringToInt(format)) != FALSE; |
384 } | 384 } |
385 | 385 |
386 void Clipboard::ReadText(string16* result) const { | 386 void Clipboard::ReadText(string16* result) const { |
387 if (!result) { | 387 if (!result) { |
388 NOTREACHED(); | 388 NOTREACHED(); |
389 return; | 389 return; |
390 } | 390 } |
391 | 391 |
392 result->clear(); | 392 result->clear(); |
393 | 393 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 markup->clear(); | 430 markup->clear(); |
431 | 431 |
432 if (src_url) | 432 if (src_url) |
433 src_url->clear(); | 433 src_url->clear(); |
434 | 434 |
435 // Acquire the clipboard. | 435 // Acquire the clipboard. |
436 ScopedClipboard clipboard; | 436 ScopedClipboard clipboard; |
437 if (!clipboard.Acquire(GetClipboardWindow())) | 437 if (!clipboard.Acquire(GetClipboardWindow())) |
438 return; | 438 return; |
439 | 439 |
440 HANDLE data = ::GetClipboardData(GetHtmlFormatType()); | 440 HANDLE data = ::GetClipboardData(StringToInt(GetHtmlFormatType())); |
441 if (!data) | 441 if (!data) |
442 return; | 442 return; |
443 | 443 |
444 std::string html_fragment(static_cast<const char*>(::GlobalLock(data))); | 444 std::string html_fragment(static_cast<const char*>(::GlobalLock(data))); |
445 ::GlobalUnlock(data); | 445 ::GlobalUnlock(data); |
446 | 446 |
447 std::string markup_utf8; | 447 std::string markup_utf8; |
448 ClipboardUtil::CFHtmlToHtml(html_fragment, &markup_utf8, src_url); | 448 ClipboardUtil::CFHtmlToHtml(html_fragment, &markup_utf8, src_url); |
449 markup->assign(UTF8ToWide(markup_utf8)); | 449 markup->assign(UTF8ToWide(markup_utf8)); |
450 } | 450 } |
451 | 451 |
452 void Clipboard::ReadBookmark(string16* title, std::string* url) const { | 452 void Clipboard::ReadBookmark(string16* title, std::string* url) const { |
453 if (title) | 453 if (title) |
454 title->clear(); | 454 title->clear(); |
455 | 455 |
456 if (url) | 456 if (url) |
457 url->clear(); | 457 url->clear(); |
458 | 458 |
459 // Acquire the clipboard. | 459 // Acquire the clipboard. |
460 ScopedClipboard clipboard; | 460 ScopedClipboard clipboard; |
461 if (!clipboard.Acquire(GetClipboardWindow())) | 461 if (!clipboard.Acquire(GetClipboardWindow())) |
462 return; | 462 return; |
463 | 463 |
464 HANDLE data = ::GetClipboardData(GetUrlWFormatType()); | 464 HANDLE data = ::GetClipboardData(StringToInt(GetUrlWFormatType())); |
465 if (!data) | 465 if (!data) |
466 return; | 466 return; |
467 | 467 |
468 string16 bookmark(static_cast<const char16*>(::GlobalLock(data))); | 468 string16 bookmark(static_cast<const char16*>(::GlobalLock(data))); |
469 ::GlobalUnlock(data); | 469 ::GlobalUnlock(data); |
470 | 470 |
471 ParseBookmarkClipboardFormat(bookmark, title, url); | 471 ParseBookmarkClipboardFormat(bookmark, title, url); |
472 } | 472 } |
473 | 473 |
474 // Read a file in HDROP format from the clipboard. | 474 // Read a file in HDROP format from the clipboard. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 | 529 |
530 if (url) { | 530 if (url) { |
531 const size_t url_start = bookmark.find_first_not_of(kDelim, title_end); | 531 const size_t url_start = bookmark.find_first_not_of(kDelim, title_end); |
532 if (url_start != string16::npos) | 532 if (url_start != string16::npos) |
533 *url = UTF16ToUTF8(bookmark.substr(url_start, string16::npos)); | 533 *url = UTF16ToUTF8(bookmark.substr(url_start, string16::npos)); |
534 } | 534 } |
535 } | 535 } |
536 | 536 |
537 // static | 537 // static |
538 Clipboard::FormatType Clipboard::GetUrlFormatType() { | 538 Clipboard::FormatType Clipboard::GetUrlFormatType() { |
539 return ClipboardUtil::GetUrlFormat()->cfFormat; | 539 return IntToString(ClipboardUtil::GetUrlFormat()->cfFormat); |
540 } | 540 } |
541 | 541 |
542 // static | 542 // static |
543 Clipboard::FormatType Clipboard::GetUrlWFormatType() { | 543 Clipboard::FormatType Clipboard::GetUrlWFormatType() { |
544 return ClipboardUtil::GetUrlWFormat()->cfFormat; | 544 return IntToString(ClipboardUtil::GetUrlWFormat()->cfFormat); |
545 } | 545 } |
546 | 546 |
547 // static | 547 // static |
548 Clipboard::FormatType Clipboard::GetMozUrlFormatType() { | 548 Clipboard::FormatType Clipboard::GetMozUrlFormatType() { |
549 return ClipboardUtil::GetMozUrlFormat()->cfFormat; | 549 return IntToString(ClipboardUtil::GetMozUrlFormat()->cfFormat); |
550 } | 550 } |
551 | 551 |
552 // static | 552 // static |
553 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { | 553 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { |
554 return ClipboardUtil::GetPlainTextFormat()->cfFormat; | 554 return IntToString(ClipboardUtil::GetPlainTextFormat()->cfFormat); |
555 } | 555 } |
556 | 556 |
557 // static | 557 // static |
558 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { | 558 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { |
559 return ClipboardUtil::GetPlainTextWFormat()->cfFormat; | 559 return IntToString(ClipboardUtil::GetPlainTextWFormat()->cfFormat); |
560 } | 560 } |
561 | 561 |
562 // static | 562 // static |
563 Clipboard::FormatType Clipboard::GetFilenameFormatType() { | 563 Clipboard::FormatType Clipboard::GetFilenameFormatType() { |
564 return ClipboardUtil::GetFilenameFormat()->cfFormat; | 564 return IntToString(ClipboardUtil::GetFilenameFormat()->cfFormat); |
565 } | 565 } |
566 | 566 |
567 // static | 567 // static |
568 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { | 568 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { |
569 return ClipboardUtil::GetFilenameWFormat()->cfFormat; | 569 return IntToString(ClipboardUtil::GetFilenameWFormat()->cfFormat); |
570 } | 570 } |
571 | 571 |
572 // MS HTML Format | 572 // MS HTML Format |
573 // static | 573 // static |
574 Clipboard::FormatType Clipboard::GetHtmlFormatType() { | 574 Clipboard::FormatType Clipboard::GetHtmlFormatType() { |
575 return ClipboardUtil::GetHtmlFormat()->cfFormat; | 575 return IntToString(ClipboardUtil::GetHtmlFormat()->cfFormat); |
576 } | 576 } |
577 | 577 |
578 // static | 578 // static |
579 Clipboard::FormatType Clipboard::GetBitmapFormatType() { | 579 Clipboard::FormatType Clipboard::GetBitmapFormatType() { |
580 return CF_BITMAP; | 580 return IntToString(CF_BITMAP); |
581 } | 581 } |
582 | 582 |
583 // Firefox text/html | 583 // Firefox text/html |
584 // static | 584 // static |
585 Clipboard::FormatType Clipboard::GetTextHtmlFormatType() { | 585 Clipboard::FormatType Clipboard::GetTextHtmlFormatType() { |
586 return ClipboardUtil::GetTextHtmlFormat()->cfFormat; | 586 return IntToString(ClipboardUtil::GetTextHtmlFormat()->cfFormat); |
587 } | 587 } |
588 | 588 |
589 // static | 589 // static |
590 Clipboard::FormatType Clipboard::GetCFHDropFormatType() { | 590 Clipboard::FormatType Clipboard::GetCFHDropFormatType() { |
591 return ClipboardUtil::GetCFHDropFormat()->cfFormat; | 591 return IntToString(ClipboardUtil::GetCFHDropFormat()->cfFormat); |
592 } | 592 } |
593 | 593 |
594 // static | 594 // static |
595 Clipboard::FormatType Clipboard::GetFileDescriptorFormatType() { | 595 Clipboard::FormatType Clipboard::GetFileDescriptorFormatType() { |
596 return ClipboardUtil::GetFileDescriptorFormat()->cfFormat; | 596 return IntToString(ClipboardUtil::GetFileDescriptorFormat()->cfFormat); |
597 } | 597 } |
598 | 598 |
599 // static | 599 // static |
600 Clipboard::FormatType Clipboard::GetFileContentFormatZeroType() { | 600 Clipboard::FormatType Clipboard::GetFileContentFormatZeroType() { |
601 return ClipboardUtil::GetFileContentFormatZero()->cfFormat; | 601 return IntToString(ClipboardUtil::GetFileContentFormatZero()->cfFormat); |
602 } | 602 } |
603 | 603 |
604 // static | 604 // static |
605 void Clipboard::DuplicateRemoteHandles(base::ProcessHandle process, | 605 void Clipboard::DuplicateRemoteHandles(base::ProcessHandle process, |
606 ObjectMap* objects) { | 606 ObjectMap* objects) { |
607 for (ObjectMap::iterator iter = objects->begin(); iter != objects->end(); | 607 for (ObjectMap::iterator iter = objects->begin(); iter != objects->end(); |
608 ++iter) { | 608 ++iter) { |
609 if (iter->first == CBF_SMBITMAP) { | 609 if (iter->first == CBF_SMBITMAP) { |
610 // There is a shared memory handle encoded on the first ObjectMapParam. | 610 // There is a shared memory handle encoded on the first ObjectMapParam. |
611 // Use it to open a local handle to the memory. | 611 // Use it to open a local handle to the memory. |
612 char* bitmap_data = &(iter->second[0].front()); | 612 char* bitmap_data = &(iter->second[0].front()); |
613 base::SharedMemoryHandle* remote_bitmap_handle = | 613 base::SharedMemoryHandle* remote_bitmap_handle = |
614 reinterpret_cast<base::SharedMemoryHandle*>(bitmap_data); | 614 reinterpret_cast<base::SharedMemoryHandle*>(bitmap_data); |
615 | 615 |
616 base::SharedMemory* bitmap = new base::SharedMemory(*remote_bitmap_handle, | 616 base::SharedMemory* bitmap = new base::SharedMemory(*remote_bitmap_handle, |
617 false, process); | 617 false, process); |
618 | 618 |
619 // We store the object where the remote handle was located so it can | 619 // We store the object where the remote handle was located so it can |
620 // be retrieved by the UI thread (see WriteBitmapFromSharedMemory()). | 620 // be retrieved by the UI thread (see WriteBitmapFromSharedMemory()). |
621 iter->second[0].clear(); | 621 iter->second[0].clear(); |
622 for (size_t i = 0; i < sizeof(bitmap); i++) | 622 for (size_t i = 0; i < sizeof(bitmap); i++) |
623 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); | 623 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); |
624 } | 624 } |
625 } | 625 } |
626 } | 626 } |
627 | 627 |
628 // static | 628 // static |
629 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { | 629 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { |
630 return ClipboardUtil::GetWebKitSmartPasteFormat()->cfFormat; | 630 return IntToString(ClipboardUtil::GetWebKitSmartPasteFormat()->cfFormat); |
631 } | 631 } |
632 | 632 |
633 // static | 633 // static |
634 void Clipboard::FreeData(FormatType format, HANDLE data) { | 634 void Clipboard::FreeData(unsigned int format, HANDLE data) { |
635 if (format == CF_BITMAP) | 635 if (format == CF_BITMAP) |
636 ::DeleteObject(static_cast<HBITMAP>(data)); | 636 ::DeleteObject(static_cast<HBITMAP>(data)); |
637 else | 637 else |
638 ::GlobalFree(data); | 638 ::GlobalFree(data); |
639 } | 639 } |
640 | 640 |
641 HWND Clipboard::GetClipboardWindow() const { | 641 HWND Clipboard::GetClipboardWindow() const { |
642 if (!clipboard_owner_ && create_window_) { | 642 if (!clipboard_owner_ && create_window_) { |
643 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass", | 643 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass", |
644 L"ClipboardOwnerWindow", | 644 L"ClipboardOwnerWindow", |
645 0, 0, 0, 0, 0, | 645 0, 0, 0, 0, 0, |
646 HWND_MESSAGE, | 646 HWND_MESSAGE, |
647 0, 0, 0); | 647 0, 0, 0); |
648 } | 648 } |
649 return clipboard_owner_; | 649 return clipboard_owner_; |
650 } | 650 } |
OLD | NEW |