OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. |
| 4 |
| 5 #include "webkit/glue/webclipboard_impl.h" |
| 6 |
| 7 #include "WebImage.h" |
| 8 #include "WebString.h" |
| 9 #include "WebURL.h" |
| 10 |
| 11 #include "base/clipboard.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/string_util.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "net/base/escape.h" |
| 16 #include "webkit/glue/scoped_clipboard_writer_glue.h" |
| 17 #include "webkit/glue/webkit_glue.h" |
| 18 |
| 19 using WebKit::WebClipboard; |
| 20 using WebKit::WebImage; |
| 21 using WebKit::WebString; |
| 22 using WebKit::WebURL; |
| 23 |
| 24 namespace webkit_glue { |
| 25 |
| 26 static std::string URLToMarkup(const WebURL& url, const WebString& title) { |
| 27 std::string markup("<a href=\""); |
| 28 markup.append(url.spec()); |
| 29 markup.append("\">"); |
| 30 // TODO(darin): HTML escape this |
| 31 markup.append(EscapeForHTML(UTF16ToUTF8(title))); |
| 32 markup.append("</a>"); |
| 33 return markup; |
| 34 } |
| 35 |
| 36 static std::string URLToImageMarkup(const WebURL& url, |
| 37 const WebString& title) { |
| 38 std::string markup("<img src=\""); |
| 39 markup.append(url.spec()); |
| 40 markup.append("\""); |
| 41 if (!title.isEmpty()) { |
| 42 markup.append(" alt=\""); |
| 43 markup.append(EscapeForHTML(UTF16ToUTF8(title))); |
| 44 markup.append("\""); |
| 45 } |
| 46 markup.append("/>"); |
| 47 return markup; |
| 48 } |
| 49 |
| 50 bool WebClipboardImpl::isFormatAvailable(Format format) { |
| 51 Clipboard::FormatType format_type; |
| 52 |
| 53 switch (format) { |
| 54 case FormatHTML: |
| 55 format_type = Clipboard::GetHtmlFormatType(); |
| 56 break; |
| 57 case FormatSmartPaste: |
| 58 format_type = Clipboard::GetWebKitSmartPasteFormatType(); |
| 59 break; |
| 60 case FormatBookmark: |
| 61 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 62 format_type = Clipboard::GetUrlWFormatType(); |
| 63 break; |
| 64 #endif |
| 65 default: |
| 66 NOTREACHED(); |
| 67 return false; |
| 68 } |
| 69 |
| 70 return ClipboardIsFormatAvailable(format_type); |
| 71 } |
| 72 |
| 73 WebString WebClipboardImpl::readPlainText() { |
| 74 if (ClipboardIsFormatAvailable(Clipboard::GetPlainTextWFormatType())) { |
| 75 std::wstring text; |
| 76 ClipboardReadText(&text); |
| 77 if (!text.empty()) |
| 78 return WideToUTF16(text); |
| 79 } |
| 80 |
| 81 if (ClipboardIsFormatAvailable(Clipboard::GetPlainTextFormatType())) { |
| 82 std::string text; |
| 83 ClipboardReadAsciiText(&text); |
| 84 if (!text.empty()) |
| 85 return UTF8ToUTF16(text); |
| 86 } |
| 87 |
| 88 return WebString(); |
| 89 } |
| 90 |
| 91 WebString WebClipboardImpl::readHTML(WebURL* source_url) { |
| 92 std::wstring html_stdstr; |
| 93 GURL gurl; |
| 94 ClipboardReadHTML(&html_stdstr, &gurl); |
| 95 *source_url = gurl; |
| 96 return WideToUTF16(html_stdstr); |
| 97 } |
| 98 |
| 99 void WebClipboardImpl::writeHTML( |
| 100 const WebString& html_text, const WebURL& source_url, |
| 101 const WebString& plain_text, bool write_smart_paste) { |
| 102 ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); |
| 103 scw.WriteHTML(UTF16ToWide(html_text), source_url.spec()); |
| 104 scw.WriteText(UTF16ToWide(plain_text)); |
| 105 |
| 106 if (write_smart_paste) |
| 107 scw.WriteWebSmartPaste(); |
| 108 } |
| 109 |
| 110 void WebClipboardImpl::writeURL(const WebURL& url, const WebString& title) { |
| 111 ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); |
| 112 |
| 113 scw.WriteBookmark(UTF16ToWide(title), url.spec()); |
| 114 scw.WriteHTML(UTF8ToWide(URLToMarkup(url, title)), ""); |
| 115 scw.WriteText(UTF8ToWide(url.spec())); |
| 116 } |
| 117 |
| 118 void WebClipboardImpl::writeImage( |
| 119 const WebImage& image, const WebURL& url, const WebString& title) { |
| 120 ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); |
| 121 |
| 122 #if WEBKIT_USING(SKIA) |
| 123 if (!image.isNull()) |
| 124 scw.WriteBitmapFromPixels(image.pixels(), image.size()); |
| 125 #endif |
| 126 |
| 127 if (!url.isEmpty()) { |
| 128 scw.WriteBookmark(UTF16ToWide(title), url.spec()); |
| 129 scw.WriteHTML(UTF8ToWide(URLToImageMarkup(url, title)), ""); |
| 130 scw.WriteText(UTF8ToWide(url.spec())); |
| 131 } |
| 132 } |
| 133 |
| 134 } // namespace webkit_glue |
OLD | NEW |