[email protected] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 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] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 5 | #include "content/renderer/webclipboard_impl.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 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] | e464fee | 2013-06-11 12:42:20 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 11 | #include "content/public/common/drop_data.h" |
| 12 | #include "content/renderer/drop_data_builder.h" |
[email protected] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 13 | #include "content/renderer/scoped_clipboard_writer_glue.h" |
[email protected] | c1088446 | 2013-05-30 00:22:09 | [diff] [blame] | 14 | #include "third_party/WebKit/public/platform/WebData.h" |
| 15 | #include "third_party/WebKit/public/platform/WebDragData.h" |
| 16 | #include "third_party/WebKit/public/platform/WebImage.h" |
| 17 | #include "third_party/WebKit/public/platform/WebSize.h" |
| 18 | #include "third_party/WebKit/public/platform/WebString.h" |
| 19 | #include "third_party/WebKit/public/platform/WebURL.h" |
| 20 | #include "third_party/WebKit/public/platform/WebVector.h" |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 21 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 22 | #include "ui/base/clipboard/clipboard.h" |
[email protected] | 2ffbb48 | 2011-12-05 23:41:17 | [diff] [blame] | 23 | #include "ui/base/clipboard/custom_data_helper.h" |
[email protected] | 707e1c4 | 2013-07-09 21:18:58 | [diff] [blame^] | 24 | #include "url/gurl.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 25 | #include "webkit/glue/webkit_glue.h" |
[email protected] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 26 | #include "webkit/renderer/clipboard_utils.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 27 | |
| 28 | using WebKit::WebClipboard; |
[email protected] | 3d4a691 | 2011-03-15 20:56:19 | [diff] [blame] | 29 | using WebKit::WebData; |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 30 | using WebKit::WebDragData; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 31 | using WebKit::WebImage; |
| 32 | using WebKit::WebString; |
| 33 | using WebKit::WebURL; |
[email protected] | 97c2c03 | 2010-07-07 22:43:41 | [diff] [blame] | 34 | using WebKit::WebVector; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 35 | |
[email protected] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 36 | namespace content { |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 37 | |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 38 | WebClipboardImpl::WebClipboardImpl(ClipboardClient* client) |
| 39 | : client_(client) { |
| 40 | } |
| 41 | |
[email protected] | 04bf3ad | 2010-08-27 21:23:48 | [diff] [blame] | 42 | WebClipboardImpl::~WebClipboardImpl() { |
| 43 | } |
| 44 | |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 45 | uint64 WebClipboardImpl::getSequenceNumber() { |
[email protected] | 9cf12c3 | 2011-11-10 19:28:07 | [diff] [blame] | 46 | return sequenceNumber(BufferStandard); |
| 47 | } |
| 48 | |
| 49 | uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) { |
| 50 | ui::Clipboard::Buffer buffer_type; |
| 51 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 52 | return 0; |
| 53 | |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 54 | return client_->GetSequenceNumber(buffer_type); |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 57 | bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { |
[email protected] | 757d4e54 | 2012-01-20 22:23:42 | [diff] [blame] | 58 | ui::Clipboard::Buffer buffer_type = ui::Clipboard::BUFFER_STANDARD; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 59 | |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 60 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 61 | return false; |
| 62 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 63 | switch (format) { |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 64 | case FormatPlainText: |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 65 | return client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 66 | buffer_type) || |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 67 | client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 68 | buffer_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 69 | case FormatHTML: |
[email protected] | ff53be6 | 2011-12-07 23:28:29 | [diff] [blame] | 70 | return client_->IsFormatAvailable(ui::Clipboard::GetHtmlFormatType(), |
| 71 | buffer_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 72 | case FormatSmartPaste: |
[email protected] | ff53be6 | 2011-12-07 23:28:29 | [diff] [blame] | 73 | return client_->IsFormatAvailable( |
| 74 | ui::Clipboard::GetWebKitSmartPasteFormatType(), buffer_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 75 | case FormatBookmark: |
| 76 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | ff53be6 | 2011-12-07 23:28:29 | [diff] [blame] | 77 | return client_->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(), |
| 78 | buffer_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 79 | #endif |
| 80 | default: |
| 81 | NOTREACHED(); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | ff53be6 | 2011-12-07 23:28:29 | [diff] [blame] | 84 | return false; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 87 | WebVector<WebString> WebClipboardImpl::readAvailableTypes( |
| 88 | Buffer buffer, bool* contains_filenames) { |
| 89 | ui::Clipboard::Buffer buffer_type; |
[email protected] | 8275835 | 2013-03-29 19:21:31 | [diff] [blame] | 90 | std::vector<base::string16> types; |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 91 | if (ConvertBufferType(buffer, &buffer_type)) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 92 | client_->ReadAvailableTypes(buffer_type, &types, contains_filenames); |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 93 | } |
| 94 | return types; |
| 95 | } |
| 96 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 97 | WebString WebClipboardImpl::readPlainText(Buffer buffer) { |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 98 | ui::Clipboard::Buffer buffer_type; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 99 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 100 | return WebString(); |
| 101 | |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 102 | if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 103 | buffer_type)) { |
[email protected] | 8275835 | 2013-03-29 19:21:31 | [diff] [blame] | 104 | base::string16 text; |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 105 | client_->ReadText(buffer_type, &text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 106 | if (!text.empty()) |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 107 | return text; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 110 | if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 111 | buffer_type)) { |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 112 | std::string text; |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 113 | client_->ReadAsciiText(buffer_type, &text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 114 | if (!text.empty()) |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 115 | return ASCIIToUTF16(text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return WebString(); |
| 119 | } |
| 120 | |
[email protected] | a96790b | 2011-10-06 22:24:10 | [diff] [blame] | 121 | WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url, |
| 122 | unsigned* fragment_start, |
| 123 | unsigned* fragment_end) { |
| 124 | ui::Clipboard::Buffer buffer_type; |
| 125 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 126 | return WebString(); |
| 127 | |
[email protected] | 8275835 | 2013-03-29 19:21:31 | [diff] [blame] | 128 | base::string16 html_stdstr; |
[email protected] | a96790b | 2011-10-06 22:24:10 | [diff] [blame] | 129 | GURL gurl; |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 130 | client_->ReadHTML(buffer_type, &html_stdstr, &gurl, |
[email protected] | a96790b | 2011-10-06 22:24:10 | [diff] [blame] | 131 | static_cast<uint32*>(fragment_start), |
| 132 | static_cast<uint32*>(fragment_end)); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 133 | *source_url = gurl; |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 134 | return html_stdstr; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | 3d4a691 | 2011-03-15 20:56:19 | [diff] [blame] | 137 | WebData WebClipboardImpl::readImage(Buffer buffer) { |
| 138 | ui::Clipboard::Buffer buffer_type; |
| 139 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 140 | return WebData(); |
| 141 | |
| 142 | std::string png_data; |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 143 | client_->ReadImage(buffer_type, &png_data); |
[email protected] | 3d4a691 | 2011-03-15 20:56:19 | [diff] [blame] | 144 | return WebData(png_data); |
| 145 | } |
| 146 | |
[email protected] | c6562f4 | 2011-12-04 06:19:37 | [diff] [blame] | 147 | WebString WebClipboardImpl::readCustomData(Buffer buffer, |
| 148 | const WebString& type) { |
| 149 | ui::Clipboard::Buffer buffer_type; |
| 150 | if (!ConvertBufferType(buffer, &buffer_type)) |
| 151 | return WebString(); |
| 152 | |
[email protected] | 8275835 | 2013-03-29 19:21:31 | [diff] [blame] | 153 | base::string16 data; |
[email protected] | c6562f4 | 2011-12-04 06:19:37 | [diff] [blame] | 154 | client_->ReadCustomData(buffer_type, type, &data); |
| 155 | return data; |
| 156 | } |
| 157 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 158 | void WebClipboardImpl::writeHTML( |
| 159 | const WebString& html_text, const WebURL& source_url, |
| 160 | const WebString& plain_text, bool write_smart_paste) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 161 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 162 | scw.WriteHTML(html_text, source_url.spec()); |
| 163 | scw.WriteText(plain_text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 164 | |
| 165 | if (write_smart_paste) |
| 166 | scw.WriteWebSmartPaste(); |
| 167 | } |
| 168 | |
[email protected] | 8c95a75 | 2009-09-25 10:54:22 | [diff] [blame] | 169 | void WebClipboardImpl::writePlainText(const WebString& plain_text) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 170 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | 8c95a75 | 2009-09-25 10:54:22 | [diff] [blame] | 171 | scw.WriteText(plain_text); |
| 172 | } |
| 173 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 174 | void WebClipboardImpl::writeURL(const WebURL& url, const WebString& title) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 175 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 176 | |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 177 | scw.WriteBookmark(title, url.spec()); |
[email protected] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 178 | scw.WriteHTML(UTF8ToUTF16(webkit_clipboard::URLToMarkup(url, title)), |
| 179 | std::string()); |
[email protected] | 39a749c | 2011-01-28 02:40:46 | [diff] [blame] | 180 | scw.WriteText(UTF8ToUTF16(std::string(url.spec()))); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void WebClipboardImpl::writeImage( |
| 184 | const WebImage& image, const WebURL& url, const WebString& title) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 185 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 186 | |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 187 | if (!image.isNull()) { |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 188 | const SkBitmap& bitmap = image.getSkBitmap(); |
[email protected] | 8973f52 | 2009-07-03 05:58:45 | [diff] [blame] | 189 | SkAutoLockPixels locked(bitmap); |
| 190 | scw.WriteBitmapFromPixels(bitmap.getPixels(), image.size()); |
| 191 | } |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 192 | |
[email protected] | 59d9aacf | 2009-11-30 22:59:25 | [diff] [blame] | 193 | if (!url.isEmpty()) { |
| 194 | scw.WriteBookmark(title, url.spec()); |
[email protected] | 0930323 | 2011-06-09 19:28:31 | [diff] [blame] | 195 | #if !defined(OS_MACOSX) |
| 196 | // When writing the image, we also write the image markup so that pasting |
| 197 | // into rich text editors, such as Gmail, reveals the image. We also don't |
| 198 | // want to call writeText(), since some applications (WordPad) don't pick |
| 199 | // the image if there is also a text format on the clipboard. |
| 200 | // We also don't want to write HTML on a Mac, since Mail.app prefers to use |
| 201 | // the image markup over attaching the actual image. See |
| 202 | // http://crbug.com/33016 for details. |
[email protected] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 203 | scw.WriteHTML(UTF8ToUTF16(webkit_clipboard::URLToImageMarkup(url, title)), |
| 204 | std::string()); |
[email protected] | 0930323 | 2011-06-09 19:28:31 | [diff] [blame] | 205 | #endif |
[email protected] | 59d9aacf | 2009-11-30 22:59:25 | [diff] [blame] | 206 | } |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 209 | void WebClipboardImpl::writeDataObject(const WebDragData& data) { |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 210 | ScopedClipboardWriterGlue scw(client_); |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 211 | |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 212 | const DropData& data_object = DropDataBuilder::Build(data); |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 213 | // TODO(dcheng): Properly support text/uri-list here. |
[email protected] | da41ae9 | 2012-06-16 01:28:17 | [diff] [blame] | 214 | if (!data_object.text.is_null()) |
| 215 | scw.WriteText(data_object.text.string()); |
| 216 | if (!data_object.html.is_null()) |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 217 | scw.WriteHTML(data_object.html.string(), std::string()); |
[email protected] | fbf7541 | 2012-01-07 01:26:52 | [diff] [blame] | 218 | // If there is no custom data, avoid calling WritePickledData. This ensures |
| 219 | // that ScopedClipboardWriterGlue's dtor remains a no-op if the page didn't |
| 220 | // modify the DataTransfer object, which is important to avoid stomping on |
| 221 | // any clipboard contents written by extension functions such as |
[email protected] | a19258af | 2012-09-21 06:36:33 | [diff] [blame] | 222 | // chrome.bookmarkManagerPrivate.copy. |
[email protected] | fbf7541 | 2012-01-07 01:26:52 | [diff] [blame] | 223 | if (!data_object.custom_data.empty()) { |
| 224 | Pickle pickle; |
| 225 | ui::WriteCustomDataToPickle(data_object.custom_data, &pickle); |
| 226 | scw.WritePickledData(pickle, ui::Clipboard::GetWebCustomDataFormatType()); |
| 227 | } |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 230 | bool WebClipboardImpl::ConvertBufferType(Buffer buffer, |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 231 | ui::Clipboard::Buffer* result) { |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 232 | switch (buffer) { |
| 233 | case BufferStandard: |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 234 | *result = ui::Clipboard::BUFFER_STANDARD; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 235 | break; |
| 236 | case BufferSelection: |
[email protected] | 264b319 | 2012-08-10 21:56:46 | [diff] [blame] | 237 | #if defined(USE_X11) |
| 238 | #if defined(OS_CHROMEOS) |
| 239 | // Chrome OS only supports the standard clipboard, |
| 240 | // but not the X selection clipboad. |
| 241 | return false; |
| 242 | #else |
[email protected] | 2dfeaf9 | 2011-01-10 21:08:21 | [diff] [blame] | 243 | *result = ui::Clipboard::BUFFER_SELECTION; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 244 | break; |
| 245 | #endif |
[email protected] | 264b319 | 2012-08-10 21:56:46 | [diff] [blame] | 246 | #endif |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 247 | default: |
| 248 | NOTREACHED(); |
| 249 | return false; |
| 250 | } |
| 251 | return true; |
| 252 | } |
| 253 | |
[email protected] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 254 | } // namespace content |
| 255 | |