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 // 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 "ui/base/clipboard/clipboard.h" | 8 #include "ui/base/clipboard/clipboard.h" |
9 | 9 |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
11 #include <shellapi.h> | 11 #include <shellapi.h> |
12 | 12 |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/shared_memory.h" | 16 #include "base/shared_memory.h" |
17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
19 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
20 #include "base/utf_offset_string_conversions.h" | 20 #include "base/utf_offset_string_conversions.h" |
21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
22 #include "base/win/scoped_gdi_object.h" | 22 #include "base/win/scoped_gdi_object.h" |
23 #include "base/win/scoped_hdc.h" | 23 #include "base/win/scoped_hdc.h" |
24 #include "base/win/wrapped_window_proc.h" | 24 #include "base/win/wrapped_window_proc.h" |
25 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
26 #include "ui/base/clipboard/clipboard_util_win.h" | 26 #include "ui/base/clipboard/clipboard_util_win.h" |
27 #include "ui/base/clipboard/custom_data_helper.h" | |
28 #include "ui/gfx/canvas_skia.h" | 27 #include "ui/gfx/canvas_skia.h" |
29 #include "ui/gfx/size.h" | 28 #include "ui/gfx/size.h" |
30 | 29 |
31 namespace ui { | 30 namespace ui { |
32 | 31 |
33 namespace { | 32 namespace { |
34 | 33 |
35 // A scoper to manage acquiring and automatically releasing the clipboard. | 34 // A scoper to manage acquiring and automatically releasing the clipboard. |
36 class ScopedClipboard { | 35 class ScopedClipboard { |
37 public: | 36 public: |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 const FORMATETC* textFormat = ClipboardUtil::GetPlainTextFormat(); | 370 const FORMATETC* textFormat = ClipboardUtil::GetPlainTextFormat(); |
372 const FORMATETC* htmlFormat = ClipboardUtil::GetHtmlFormat(); | 371 const FORMATETC* htmlFormat = ClipboardUtil::GetHtmlFormat(); |
373 types->clear(); | 372 types->clear(); |
374 if (::IsClipboardFormatAvailable(textFormat->cfFormat)) | 373 if (::IsClipboardFormatAvailable(textFormat->cfFormat)) |
375 types->push_back(UTF8ToUTF16(kMimeTypeText)); | 374 types->push_back(UTF8ToUTF16(kMimeTypeText)); |
376 if (::IsClipboardFormatAvailable(htmlFormat->cfFormat)) | 375 if (::IsClipboardFormatAvailable(htmlFormat->cfFormat)) |
377 types->push_back(UTF8ToUTF16(kMimeTypeHTML)); | 376 types->push_back(UTF8ToUTF16(kMimeTypeHTML)); |
378 if (::IsClipboardFormatAvailable(CF_DIB)) | 377 if (::IsClipboardFormatAvailable(CF_DIB)) |
379 types->push_back(UTF8ToUTF16(kMimeTypePNG)); | 378 types->push_back(UTF8ToUTF16(kMimeTypePNG)); |
380 *contains_filenames = false; | 379 *contains_filenames = false; |
381 | |
382 // Acquire the clipboard. | |
383 ScopedClipboard clipboard; | |
384 if (!clipboard.Acquire(GetClipboardWindow())) | |
385 return; | |
386 | |
387 HANDLE hdata = ::GetClipboardData( | |
388 ClipboardUtil::GetWebCustomDataFormat()->cfFormat); | |
389 if (!hdata) | |
390 return; | |
391 | |
392 ReadCustomDataTypes(::GlobalLock(hdata), ::GlobalSize(hdata), types); | |
393 ::GlobalUnlock(hdata); | |
394 } | 380 } |
395 | 381 |
396 void Clipboard::ReadText(Clipboard::Buffer buffer, string16* result) const { | 382 void Clipboard::ReadText(Clipboard::Buffer buffer, string16* result) const { |
397 DCHECK_EQ(buffer, BUFFER_STANDARD); | 383 DCHECK_EQ(buffer, BUFFER_STANDARD); |
398 if (!result) { | 384 if (!result) { |
399 NOTREACHED(); | 385 NOTREACHED(); |
400 return; | 386 return; |
401 } | 387 } |
402 | 388 |
403 result->clear(); | 389 result->clear(); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 MakeBitmapOpaque(device_bitmap); | 540 MakeBitmapOpaque(device_bitmap); |
555 } | 541 } |
556 } | 542 } |
557 | 543 |
558 return canvas.ExtractBitmap(); | 544 return canvas.ExtractBitmap(); |
559 } | 545 } |
560 | 546 |
561 void Clipboard::ReadCustomData(Buffer buffer, | 547 void Clipboard::ReadCustomData(Buffer buffer, |
562 const string16& type, | 548 const string16& type, |
563 string16* result) const { | 549 string16* result) const { |
564 DCHECK_EQ(buffer, BUFFER_STANDARD); | 550 // TODO(dcheng): Implement this. |
565 | 551 NOTIMPLEMENTED(); |
566 // Acquire the clipboard. | |
567 ScopedClipboard clipboard; | |
568 if (!clipboard.Acquire(GetClipboardWindow())) | |
569 return; | |
570 | |
571 HANDLE hdata = ::GetClipboardData( | |
572 ClipboardUtil::GetWebCustomDataFormat()->cfFormat); | |
573 if (!hdata) | |
574 return; | |
575 | |
576 ReadCustomDataForType(::GlobalLock(hdata), ::GlobalSize(hdata), type, result); | |
577 ::GlobalUnlock(hdata); | |
578 } | 552 } |
579 | 553 |
580 void Clipboard::ReadBookmark(string16* title, std::string* url) const { | 554 void Clipboard::ReadBookmark(string16* title, std::string* url) const { |
581 if (title) | 555 if (title) |
582 title->clear(); | 556 title->clear(); |
583 | 557 |
584 if (url) | 558 if (url) |
585 url->clear(); | 559 url->clear(); |
586 | 560 |
587 // Acquire the clipboard. | 561 // Acquire the clipboard. |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 return base::IntToString(ClipboardUtil::GetFileContentFormatZero()->cfFormat); | 726 return base::IntToString(ClipboardUtil::GetFileContentFormatZero()->cfFormat); |
753 } | 727 } |
754 | 728 |
755 // static | 729 // static |
756 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { | 730 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { |
757 return base::IntToString( | 731 return base::IntToString( |
758 ClipboardUtil::GetWebKitSmartPasteFormat()->cfFormat); | 732 ClipboardUtil::GetWebKitSmartPasteFormat()->cfFormat); |
759 } | 733 } |
760 | 734 |
761 // static | 735 // static |
762 Clipboard::FormatType Clipboard::GetWebCustomDataFormatType() { | |
763 // TODO(dcheng): Clean up the duplicated constant. | |
764 // Clipboard::WritePickledData() takes a FormatType, but all the callers | |
765 // assume that it's a raw string. As a result, we return the format name here | |
766 // rather than returning a string-ified version of the registered clipboard | |
767 // format ID. | |
768 return "Chromium Web Custom MIME Data Format"; | |
769 } | |
770 | |
771 // static | |
772 void Clipboard::FreeData(unsigned int format, HANDLE data) { | 736 void Clipboard::FreeData(unsigned int format, HANDLE data) { |
773 if (format == CF_BITMAP) | 737 if (format == CF_BITMAP) |
774 ::DeleteObject(static_cast<HBITMAP>(data)); | 738 ::DeleteObject(static_cast<HBITMAP>(data)); |
775 else | 739 else |
776 ::GlobalFree(data); | 740 ::GlobalFree(data); |
777 } | 741 } |
778 | 742 |
779 HWND Clipboard::GetClipboardWindow() const { | 743 HWND Clipboard::GetClipboardWindow() const { |
780 if (!clipboard_owner_ && create_window_) { | 744 if (!clipboard_owner_ && create_window_) { |
781 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass", | 745 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass", |
782 L"ClipboardOwnerWindow", | 746 L"ClipboardOwnerWindow", |
783 0, 0, 0, 0, 0, | 747 0, 0, 0, 0, 0, |
784 HWND_MESSAGE, | 748 HWND_MESSAGE, |
785 0, 0, 0); | 749 0, 0, 0); |
786 } | 750 } |
787 return clipboard_owner_; | 751 return clipboard_owner_; |
788 } | 752 } |
789 | 753 |
790 } // namespace ui | 754 } // namespace ui |
OLD | NEW |