[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" |
[email protected] | 2ffbb48 | 2011-12-05 23:41:17 | [diff] [blame^] | 8 | #include "base/pickle.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 9 | #include "base/string_util.h" |
[email protected] | b1e4eb50 | 2010-03-07 20:53:33 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 11 | #include "googleurl/src/gurl.h" |
| 12 | #include "net/base/escape.h" |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 3034b681 | 2011-12-03 00:00:14 | [diff] [blame] | 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebData.h" |
| 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebDragData.h" |
| 16 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebImage.h" |
| 17 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
| 18 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 19 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
| 20 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 21 | #include "ui/base/clipboard/clipboard.h" |
[email protected] | 2ffbb48 | 2011-12-05 23:41:17 | [diff] [blame^] | 22 | #include "ui/base/clipboard/custom_data_helper.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 23 | #include "webkit/glue/scoped_clipboard_writer_glue.h" |
[email protected] | 2ffbb48 | 2011-12-05 23:41:17 | [diff] [blame^] | 24 | #include "webkit/glue/webdropdata.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 25 | #include "webkit/glue/webkit_glue.h" |
| 26 | |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 27 | #if WEBKIT_USING_CG |
| 28 | #include "skia/ext/skia_utils_mac.h" |
| 29 | #endif |
| 30 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 31 | using WebKit::WebClipboard; |
[email protected] | 3d4a691 | 2011-03-15 20:56:19 | [diff] [blame] | 32 | using WebKit::WebData; |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 33 | using WebKit::WebDragData; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 34 | using WebKit::WebImage; |
| 35 | using WebKit::WebString; |
| 36 | using WebKit::WebURL; |
[email protected] | 97c2c03 | 2010-07-07 22:43:41 | [diff] [blame] | 37 | using WebKit::WebVector; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 38 | |
| 39 | namespace webkit_glue { |
| 40 | |
[email protected] | b9a0b1b3 | 2009-03-30 23:09:37 | [diff] [blame] | 41 | // Static |
| 42 | std::string WebClipboardImpl::URLToMarkup(const WebURL& url, |
| 43 | const WebString& title) { |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 44 | std::string markup("<a href=\""); |
| 45 | markup.append(url.spec()); |
| 46 | markup.append("\">"); |
| 47 | // TODO(darin): HTML escape this |
[email protected] | a0200c5 | 2011-10-13 14:19:27 | [diff] [blame] | 48 | markup.append(net::EscapeForHTML(UTF16ToUTF8(title))); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 49 | markup.append("</a>"); |
| 50 | return markup; |
| 51 | } |
| 52 | |
[email protected] | b9a0b1b3 | 2009-03-30 23:09:37 | [diff] [blame] | 53 | // Static |
| 54 | std::string WebClipboardImpl::URLToImageMarkup(const WebURL& url, |
| 55 | const WebString& title) { |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 56 | std::string markup("<img src=\""); |
| 57 | markup.append(url.spec()); |
| 58 | markup.append("\""); |
| 59 | if (!title.isEmpty()) { |
| 60 | markup.append(" alt=\""); |
[email protected] | a0200c5 | 2011-10-13 14:19:27 | [diff] [blame] | 61 | markup.append(net::EscapeForHTML(UTF16ToUTF8(title))); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 62 | markup.append("\""); |
| 63 | } |
| 64 | markup.append("/>"); |
| 65 | return markup; |
| 66 | } |
| 67 | |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 68 | WebClipboardImpl::WebClipboardImpl(ClipboardClient* client) |
| 69 | : client_(client) { |
| 70 | } |
| 71 | |
[email protected] | 04bf3ad | 2010-08-27 21:23:48 | [diff] [blame] | 72 | WebClipboardImpl::~WebClipboardImpl() { |
| 73 | } |
| 74 | |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 75 | uint64 WebClipboardImpl::getSequenceNumber() { |
[email protected] | 9cf12c3 | 2011-11-10 19:28:07 | [diff] [blame] | 76 | return sequenceNumber(BufferStandard); |
| 77 | } |
| 78 | |
| 79 | uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) { |
| 80 | ui::Clipboard::Buffer buffer_type; |
| 81 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 82 | return 0; |
| 83 | |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 84 | return client_->GetSequenceNumber(buffer_type); |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 87 | bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 88 | ui::Clipboard::FormatType format_type; |
| 89 | ui::Clipboard::Buffer buffer_type; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 90 | |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 91 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 92 | return false; |
| 93 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 94 | switch (format) { |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 95 | case FormatPlainText: |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 96 | return client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 97 | buffer_type) || |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 98 | client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 99 | buffer_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 100 | case FormatHTML: |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 101 | format_type = ui::Clipboard::GetHtmlFormatType(); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 102 | break; |
| 103 | case FormatSmartPaste: |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 104 | format_type = ui::Clipboard::GetWebKitSmartPasteFormatType(); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 105 | break; |
| 106 | case FormatBookmark: |
| 107 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 108 | format_type = ui::Clipboard::GetUrlWFormatType(); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 109 | break; |
| 110 | #endif |
| 111 | default: |
| 112 | NOTREACHED(); |
| 113 | return false; |
| 114 | } |
| 115 | |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 116 | return client_->IsFormatAvailable(format_type, buffer_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 119 | WebVector<WebString> WebClipboardImpl::readAvailableTypes( |
| 120 | Buffer buffer, bool* contains_filenames) { |
| 121 | ui::Clipboard::Buffer buffer_type; |
| 122 | std::vector<string16> types; |
| 123 | if (ConvertBufferType(buffer, &buffer_type)) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 124 | client_->ReadAvailableTypes(buffer_type, &types, contains_filenames); |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 125 | } |
| 126 | return types; |
| 127 | } |
| 128 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 129 | WebString WebClipboardImpl::readPlainText(Buffer buffer) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 130 | ui::Clipboard::Buffer buffer_type; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 131 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 132 | return WebString(); |
| 133 | |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 134 | if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 135 | buffer_type)) { |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 136 | string16 text; |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 137 | client_->ReadText(buffer_type, &text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 138 | if (!text.empty()) |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 139 | return text; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 142 | if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 143 | buffer_type)) { |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 144 | std::string text; |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 145 | client_->ReadAsciiText(buffer_type, &text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 146 | if (!text.empty()) |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 147 | return ASCIIToUTF16(text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return WebString(); |
| 151 | } |
| 152 | |
[email protected] | a96790b | 2011-10-06 22:24:10 | [diff] [blame] | 153 | WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url, |
| 154 | unsigned* fragment_start, |
| 155 | unsigned* fragment_end) { |
| 156 | ui::Clipboard::Buffer buffer_type; |
| 157 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 158 | return WebString(); |
| 159 | |
| 160 | string16 html_stdstr; |
| 161 | GURL gurl; |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 162 | client_->ReadHTML(buffer_type, &html_stdstr, &gurl, |
[email protected] | a96790b | 2011-10-06 22:24:10 | [diff] [blame] | 163 | static_cast<uint32*>(fragment_start), |
| 164 | static_cast<uint32*>(fragment_end)); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 165 | *source_url = gurl; |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 166 | return html_stdstr; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 3d4a691 | 2011-03-15 20:56:19 | [diff] [blame] | 169 | WebData WebClipboardImpl::readImage(Buffer buffer) { |
| 170 | ui::Clipboard::Buffer buffer_type; |
| 171 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 172 | return WebData(); |
| 173 | |
| 174 | std::string png_data; |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 175 | client_->ReadImage(buffer_type, &png_data); |
[email protected] | 3d4a691 | 2011-03-15 20:56:19 | [diff] [blame] | 176 | return WebData(png_data); |
| 177 | } |
| 178 | |
[email protected] | c6562f4 | 2011-12-04 06:19:37 | [diff] [blame] | 179 | WebString WebClipboardImpl::readCustomData(Buffer buffer, |
| 180 | const WebString& type) { |
| 181 | ui::Clipboard::Buffer buffer_type; |
| 182 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 183 | return WebString(); |
| 184 | |
| 185 | string16 data; |
| 186 | client_->ReadCustomData(buffer_type, type, &data); |
| 187 | return data; |
| 188 | } |
| 189 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 190 | void WebClipboardImpl::writeHTML( |
| 191 | const WebString& html_text, const WebURL& source_url, |
| 192 | const WebString& plain_text, bool write_smart_paste) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 193 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 194 | scw.WriteHTML(html_text, source_url.spec()); |
| 195 | scw.WriteText(plain_text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 196 | |
| 197 | if (write_smart_paste) |
| 198 | scw.WriteWebSmartPaste(); |
| 199 | } |
| 200 | |
[email protected] | 8c95a75 | 2009-09-25 10:54:22 | [diff] [blame] | 201 | void WebClipboardImpl::writePlainText(const WebString& plain_text) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 202 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | 8c95a75 | 2009-09-25 10:54:22 | [diff] [blame] | 203 | scw.WriteText(plain_text); |
| 204 | } |
| 205 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 206 | void WebClipboardImpl::writeURL(const WebURL& url, const WebString& title) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 207 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 208 | |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 209 | scw.WriteBookmark(title, url.spec()); |
| 210 | scw.WriteHTML(UTF8ToUTF16(URLToMarkup(url, title)), ""); |
[email protected] | 39a749c | 2011-01-28 02:40:46 | [diff] [blame] | 211 | scw.WriteText(UTF8ToUTF16(std::string(url.spec()))); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void WebClipboardImpl::writeImage( |
| 215 | const WebImage& image, const WebURL& url, const WebString& title) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 216 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 217 | |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 218 | if (!image.isNull()) { |
| 219 | #if WEBKIT_USING_SKIA |
| 220 | const SkBitmap& bitmap = image.getSkBitmap(); |
| 221 | #elif WEBKIT_USING_CG |
| 222 | const SkBitmap& bitmap = gfx::CGImageToSkBitmap(image.getCGImageRef()); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 223 | #endif |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 224 | SkAutoLockPixels locked(bitmap); |
| 225 | scw.WriteBitmapFromPixels(bitmap.getPixels(), image.size()); |
| 226 | } |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 227 | |
[email protected] | 59d9aacf | 2009-11-30 22:59:25 | [diff] [blame] | 228 | if (!url.isEmpty()) { |
| 229 | scw.WriteBookmark(title, url.spec()); |
[email protected] | 0930323 | 2011-06-09 19:28:31 | [diff] [blame] | 230 | #if !defined(OS_MACOSX) |
| 231 | // When writing the image, we also write the image markup so that pasting |
| 232 | // into rich text editors, such as Gmail, reveals the image. We also don't |
| 233 | // want to call writeText(), since some applications (WordPad) don't pick |
| 234 | // the image if there is also a text format on the clipboard. |
| 235 | // We also don't want to write HTML on a Mac, since Mail.app prefers to use |
| 236 | // the image markup over attaching the actual image. See |
| 237 | // http://crbug.com/33016 for details. |
[email protected] | 59d9aacf | 2009-11-30 22:59:25 | [diff] [blame] | 238 | scw.WriteHTML(UTF8ToUTF16(URLToImageMarkup(url, title)), ""); |
[email protected] | 0930323 | 2011-06-09 19:28:31 | [diff] [blame] | 239 | #endif |
[email protected] | 59d9aacf | 2009-11-30 22:59:25 | [diff] [blame] | 240 | } |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 243 | void WebClipboardImpl::writeDataObject(const WebDragData& data) { |
| 244 | // TODO(dcheng): This actually results in a double clear of the clipboard. |
| 245 | // Once in WebKit, and once here when the clipboard writer goes out of scope. |
| 246 | // The same is true of the other WebClipboard::write* methods. |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 247 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 248 | |
[email protected] | 2ffbb48 | 2011-12-05 23:41:17 | [diff] [blame^] | 249 | WebDropData data_object(data); |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 250 | // TODO(dcheng): Properly support text/uri-list here. |
[email protected] | 2ffbb48 | 2011-12-05 23:41:17 | [diff] [blame^] | 251 | scw.WriteText(data_object.plain_text); |
| 252 | scw.WriteHTML(data_object.text_html, ""); |
| 253 | Pickle pickle; |
| 254 | ui::WriteCustomDataToPickle(data_object.custom_data, &pickle); |
| 255 | scw.WritePickledData(pickle, ui::Clipboard::GetWebCustomDataFormatType()); |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 258 | bool WebClipboardImpl::ConvertBufferType(Buffer buffer, |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 259 | ui::Clipboard::Buffer* result) { |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 260 | switch (buffer) { |
| 261 | case BufferStandard: |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 262 | *result = ui::Clipboard::BUFFER_STANDARD; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 263 | break; |
| 264 | case BufferSelection: |
[email protected] | c2273e8 | 2011-11-14 18:51:25 | [diff] [blame] | 265 | #if defined(USE_X11) && !defined(USE_AURA) |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 266 | *result = ui::Clipboard::BUFFER_SELECTION; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 267 | break; |
| 268 | #endif |
| 269 | default: |
| 270 | NOTREACHED(); |
| 271 | return false; |
| 272 | } |
| 273 | return true; |
| 274 | } |
| 275 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 276 | } // namespace webkit_glue |