[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 4 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 5 | #include "webkit/glue/webclipboard_impl.h" |
| 6 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 7 | #include "base/logging.h" |
| 8 | #include "base/string_util.h" |
[email protected] | b1e4eb50 | 2010-03-07 20:53:33 | [diff] [blame] | 9 | #include "base/utf_string_conversions.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 10 | #include "googleurl/src/gurl.h" |
| 11 | #include "net/base/escape.h" |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 12 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | c1d9cdc | 2011-01-17 06:50:01 | [diff] [blame] | 13 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebImage.h" |
| 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
| 16 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
| 17 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 18 | #include "ui/base/clipboard/clipboard.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 19 | #include "webkit/glue/scoped_clipboard_writer_glue.h" |
| 20 | #include "webkit/glue/webkit_glue.h" |
| 21 | |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 22 | #if WEBKIT_USING_CG |
| 23 | #include "skia/ext/skia_utils_mac.h" |
| 24 | #endif |
| 25 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 26 | using WebKit::WebClipboard; |
| 27 | using WebKit::WebImage; |
| 28 | using WebKit::WebString; |
| 29 | using WebKit::WebURL; |
[email protected] | 97c2c03 | 2010-07-07 22:43:41 | [diff] [blame] | 30 | using WebKit::WebVector; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 31 | |
| 32 | namespace webkit_glue { |
| 33 | |
[email protected] | b9a0b1b3 | 2009-03-30 23:09:37 | [diff] [blame] | 34 | // Static |
| 35 | std::string WebClipboardImpl::URLToMarkup(const WebURL& url, |
| 36 | const WebString& title) { |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 37 | std::string markup("<a href=\""); |
| 38 | markup.append(url.spec()); |
| 39 | markup.append("\">"); |
| 40 | // TODO(darin): HTML escape this |
| 41 | markup.append(EscapeForHTML(UTF16ToUTF8(title))); |
| 42 | markup.append("</a>"); |
| 43 | return markup; |
| 44 | } |
| 45 | |
[email protected] | b9a0b1b3 | 2009-03-30 23:09:37 | [diff] [blame] | 46 | // Static |
| 47 | std::string WebClipboardImpl::URLToImageMarkup(const WebURL& url, |
| 48 | const WebString& title) { |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 49 | std::string markup("<img src=\""); |
| 50 | markup.append(url.spec()); |
| 51 | markup.append("\""); |
| 52 | if (!title.isEmpty()) { |
| 53 | markup.append(" alt=\""); |
| 54 | markup.append(EscapeForHTML(UTF16ToUTF8(title))); |
| 55 | markup.append("\""); |
| 56 | } |
| 57 | markup.append("/>"); |
| 58 | return markup; |
| 59 | } |
| 60 | |
[email protected] | 04bf3ad | 2010-08-27 21:23:48 | [diff] [blame] | 61 | WebClipboardImpl::~WebClipboardImpl() { |
| 62 | } |
| 63 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 64 | bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 65 | ui::Clipboard::FormatType format_type; |
| 66 | ui::Clipboard::Buffer buffer_type; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 67 | |
| 68 | switch (format) { |
| 69 | case FormatHTML: |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 70 | format_type = ui::Clipboard::GetHtmlFormatType(); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 71 | break; |
| 72 | case FormatSmartPaste: |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 73 | format_type = ui::Clipboard::GetWebKitSmartPasteFormatType(); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 74 | break; |
| 75 | case FormatBookmark: |
| 76 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 77 | format_type = ui::Clipboard::GetUrlWFormatType(); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 78 | break; |
| 79 | #endif |
| 80 | default: |
| 81 | NOTREACHED(); |
| 82 | return false; |
| 83 | } |
| 84 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 85 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 86 | return false; |
| 87 | |
| 88 | return ClipboardIsFormatAvailable(format_type, buffer_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 91 | WebString WebClipboardImpl::readPlainText(Buffer buffer) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 92 | ui::Clipboard::Buffer buffer_type; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 93 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 94 | return WebString(); |
| 95 | |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 96 | if (ClipboardIsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 97 | buffer_type)) { |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 98 | string16 text; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 99 | ClipboardReadText(buffer_type, &text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 100 | if (!text.empty()) |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 101 | return text; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 104 | if (ClipboardIsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 105 | buffer_type)) { |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 106 | std::string text; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 107 | ClipboardReadAsciiText(buffer_type, &text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 108 | if (!text.empty()) |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 109 | return ASCIIToUTF16(text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return WebString(); |
| 113 | } |
| 114 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 115 | WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 116 | ui::Clipboard::Buffer buffer_type; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 117 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 118 | return WebString(); |
| 119 | |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 120 | string16 html_stdstr; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 121 | GURL gurl; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 122 | ClipboardReadHTML(buffer_type, &html_stdstr, &gurl); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 123 | *source_url = gurl; |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 124 | return html_stdstr; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void WebClipboardImpl::writeHTML( |
| 128 | const WebString& html_text, const WebURL& source_url, |
| 129 | const WebString& plain_text, bool write_smart_paste) { |
| 130 | ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 131 | scw.WriteHTML(html_text, source_url.spec()); |
| 132 | scw.WriteText(plain_text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 133 | |
| 134 | if (write_smart_paste) |
| 135 | scw.WriteWebSmartPaste(); |
| 136 | } |
| 137 | |
[email protected] | 8c95a75 | 2009-09-25 10:54:22 | [diff] [blame] | 138 | void WebClipboardImpl::writePlainText(const WebString& plain_text) { |
| 139 | ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); |
| 140 | scw.WriteText(plain_text); |
| 141 | } |
| 142 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 143 | void WebClipboardImpl::writeURL(const WebURL& url, const WebString& title) { |
| 144 | ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); |
| 145 | |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 146 | scw.WriteBookmark(title, url.spec()); |
| 147 | scw.WriteHTML(UTF8ToUTF16(URLToMarkup(url, title)), ""); |
[email protected] | 39a749c | 2011-01-28 02:40:46 | [diff] [blame^] | 148 | scw.WriteText(UTF8ToUTF16(std::string(url.spec()))); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void WebClipboardImpl::writeImage( |
| 152 | const WebImage& image, const WebURL& url, const WebString& title) { |
| 153 | ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); |
| 154 | |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 155 | if (!image.isNull()) { |
| 156 | #if WEBKIT_USING_SKIA |
| 157 | const SkBitmap& bitmap = image.getSkBitmap(); |
| 158 | #elif WEBKIT_USING_CG |
| 159 | const SkBitmap& bitmap = gfx::CGImageToSkBitmap(image.getCGImageRef()); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 160 | #endif |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 161 | SkAutoLockPixels locked(bitmap); |
| 162 | scw.WriteBitmapFromPixels(bitmap.getPixels(), image.size()); |
| 163 | } |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 164 | |
[email protected] | 59d9aacf | 2009-11-30 22:59:25 | [diff] [blame] | 165 | // When writing the image, we also write the image markup so that pasting |
| 166 | // into rich text editors, such as Gmail, reveals the image. We also don't |
| 167 | // want to call writeText(), since some applications (WordPad) don't pick the |
| 168 | // image if there is also a text format on the clipboard. |
| 169 | if (!url.isEmpty()) { |
| 170 | scw.WriteBookmark(title, url.spec()); |
| 171 | scw.WriteHTML(UTF8ToUTF16(URLToImageMarkup(url, title)), ""); |
| 172 | } |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | 4dfd2d9 | 2010-06-18 20:28:18 | [diff] [blame] | 175 | void WebClipboardImpl::writeData(const WebKit::WebDragData& data) { |
| 176 | // TODO(dcheng): Implement this stub. |
| 177 | } |
| 178 | |
[email protected] | 97c2c03 | 2010-07-07 22:43:41 | [diff] [blame] | 179 | WebVector<WebString> WebClipboardImpl::readAvailableTypes( |
| 180 | Buffer buffer, bool* contains_filenames) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 181 | ui::Clipboard::Buffer buffer_type; |
[email protected] | 97c2c03 | 2010-07-07 22:43:41 | [diff] [blame] | 182 | std::vector<string16> types; |
| 183 | if (ConvertBufferType(buffer, &buffer_type)) { |
| 184 | ClipboardReadAvailableTypes(buffer_type, &types, contains_filenames); |
| 185 | } |
| 186 | return types; |
| 187 | } |
| 188 | |
| 189 | bool WebClipboardImpl::readData(Buffer buffer, const WebString& type, |
| 190 | WebString* data, WebString* metadata) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 191 | ui::Clipboard::Buffer buffer_type; |
[email protected] | 97c2c03 | 2010-07-07 22:43:41 | [diff] [blame] | 192 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 193 | return false; |
| 194 | |
| 195 | string16 data_out; |
| 196 | string16 metadata_out; |
| 197 | bool result = ClipboardReadData(buffer_type, type, &data_out, &metadata_out); |
| 198 | if (result) { |
| 199 | *data = data_out; |
| 200 | *metadata = metadata_out; |
| 201 | } |
| 202 | return result; |
| 203 | } |
| 204 | |
| 205 | WebVector<WebString> WebClipboardImpl::readFilenames(Buffer buffer) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 206 | ui::Clipboard::Buffer buffer_type; |
[email protected] | 97c2c03 | 2010-07-07 22:43:41 | [diff] [blame] | 207 | std::vector<string16> filenames; |
| 208 | if (ConvertBufferType(buffer, &buffer_type)) { |
| 209 | ClipboardReadFilenames(buffer_type, &filenames); |
| 210 | } |
| 211 | return filenames; |
| 212 | } |
| 213 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 214 | bool WebClipboardImpl::ConvertBufferType(Buffer buffer, |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 215 | ui::Clipboard::Buffer* result) { |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 216 | switch (buffer) { |
| 217 | case BufferStandard: |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 218 | *result = ui::Clipboard::BUFFER_STANDARD; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 219 | break; |
[email protected] | 97c2c03 | 2010-07-07 22:43:41 | [diff] [blame] | 220 | case BufferDrag: |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 221 | *result = ui::Clipboard::BUFFER_DRAG; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 222 | case BufferSelection: |
[email protected] | 10a6e77b | 2009-12-31 01:03:52 | [diff] [blame] | 223 | #if defined(USE_X11) |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 224 | *result = ui::Clipboard::BUFFER_SELECTION; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 225 | break; |
| 226 | #endif |
| 227 | default: |
| 228 | NOTREACHED(); |
| 229 | return false; |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 234 | } // namespace webkit_glue |