[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] | e464fee | 2013-06-11 12:42:20 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | 5c2f32e7 | 2014-03-20 20:22:17 | [diff] [blame] | 11 | #include "content/common/clipboard_format.h" |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 12 | #include "content/public/common/drop_data.h" |
[email protected] | f9866fa | 2013-10-02 16:30:45 | [diff] [blame] | 13 | #include "content/renderer/clipboard_utils.h" |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 14 | #include "content/renderer/drop_data_builder.h" |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 15 | #include "content/renderer/renderer_clipboard_delegate.h" |
[email protected] | c1088446 | 2013-05-30 00:22:09 | [diff] [blame] | 16 | #include "third_party/WebKit/public/platform/WebData.h" |
| 17 | #include "third_party/WebKit/public/platform/WebDragData.h" |
| 18 | #include "third_party/WebKit/public/platform/WebImage.h" |
| 19 | #include "third_party/WebKit/public/platform/WebSize.h" |
| 20 | #include "third_party/WebKit/public/platform/WebString.h" |
| 21 | #include "third_party/WebKit/public/platform/WebURL.h" |
| 22 | #include "third_party/WebKit/public/platform/WebVector.h" |
[email protected] | 707e1c4 | 2013-07-09 21:18:58 | [diff] [blame] | 23 | #include "url/gurl.h" |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 24 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 25 | using blink::WebClipboard; |
| 26 | using blink::WebData; |
| 27 | using blink::WebDragData; |
| 28 | using blink::WebImage; |
| 29 | using blink::WebString; |
| 30 | using blink::WebURL; |
| 31 | using blink::WebVector; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 32 | |
[email protected] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 33 | namespace content { |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 34 | |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 35 | WebClipboardImpl::WebClipboardImpl(RendererClipboardDelegate* delegate) |
| 36 | : delegate_(delegate) { |
| 37 | DCHECK(delegate); |
[email protected] | 0de5d860 | 2011-11-22 03:48:52 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | 04bf3ad | 2010-08-27 21:23:48 | [diff] [blame] | 40 | WebClipboardImpl::~WebClipboardImpl() { |
| 41 | } |
| 42 | |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 43 | uint64_t WebClipboardImpl::sequenceNumber(Buffer buffer) { |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 44 | ui::ClipboardType clipboard_type; |
| 45 | if (!ConvertBufferType(buffer, &clipboard_type)) |
[email protected] | 9cf12c3 | 2011-11-10 19:28:07 | [diff] [blame] | 46 | return 0; |
| 47 | |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 48 | return delegate_->GetSequenceNumber(clipboard_type); |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 51 | bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 52 | ui::ClipboardType clipboard_type = ui::CLIPBOARD_TYPE_COPY_PASTE; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 53 | |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 54 | if (!ConvertBufferType(buffer, &clipboard_type)) |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 55 | return false; |
| 56 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 57 | switch (format) { |
[email protected] | 606876c | 2011-03-24 17:13:38 | [diff] [blame] | 58 | case FormatPlainText: |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 59 | return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_PLAINTEXT, |
| 60 | clipboard_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 61 | case FormatHTML: |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 62 | return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_HTML, |
| 63 | clipboard_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 64 | case FormatSmartPaste: |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 65 | return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_SMART_PASTE, |
| 66 | clipboard_type); |
[email protected] | 5c2f32e7 | 2014-03-20 20:22:17 | [diff] [blame] | 67 | case FormatBookmark: |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 68 | return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_BOOKMARK, |
| 69 | clipboard_type); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 70 | default: |
| 71 | NOTREACHED(); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | ff53be6 | 2011-12-07 23:28:29 | [diff] [blame] | 74 | return false; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 77 | WebVector<WebString> WebClipboardImpl::readAvailableTypes( |
| 78 | Buffer buffer, bool* contains_filenames) { |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 79 | ui::ClipboardType clipboard_type; |
[email protected] | 8275835 | 2013-03-29 19:21:31 | [diff] [blame] | 80 | std::vector<base::string16> types; |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 81 | if (ConvertBufferType(buffer, &clipboard_type)) { |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 82 | delegate_->ReadAvailableTypes(clipboard_type, &types, contains_filenames); |
[email protected] | 36780a1 | 2011-11-04 18:16:44 | [diff] [blame] | 83 | } |
| 84 | return types; |
| 85 | } |
| 86 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 87 | WebString WebClipboardImpl::readPlainText(Buffer buffer) { |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 88 | ui::ClipboardType clipboard_type; |
| 89 | if (!ConvertBufferType(buffer, &clipboard_type)) |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 90 | return WebString(); |
| 91 | |
[email protected] | 5c2f32e7 | 2014-03-20 20:22:17 | [diff] [blame] | 92 | base::string16 text; |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 93 | delegate_->ReadText(clipboard_type, &text); |
[email protected] | 5c2f32e7 | 2014-03-20 20:22:17 | [diff] [blame] | 94 | return text; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | a96790b | 2011-10-06 22:24:10 | [diff] [blame] | 97 | WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url, |
| 98 | unsigned* fragment_start, |
| 99 | unsigned* fragment_end) { |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 100 | ui::ClipboardType clipboard_type; |
| 101 | if (!ConvertBufferType(buffer, &clipboard_type)) |
[email protected] | a96790b | 2011-10-06 22:24:10 | [diff] [blame] | 102 | return WebString(); |
| 103 | |
[email protected] | 8275835 | 2013-03-29 19:21:31 | [diff] [blame] | 104 | base::string16 html_stdstr; |
[email protected] | a96790b | 2011-10-06 22:24:10 | [diff] [blame] | 105 | GURL gurl; |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 106 | delegate_->ReadHTML(clipboard_type, &html_stdstr, &gurl, |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 107 | static_cast<uint32_t*>(fragment_start), |
| 108 | static_cast<uint32_t*>(fragment_end)); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 109 | *source_url = gurl; |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 110 | return html_stdstr; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 3d4a691 | 2011-03-15 20:56:19 | [diff] [blame] | 113 | WebData WebClipboardImpl::readImage(Buffer buffer) { |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 114 | ui::ClipboardType clipboard_type; |
| 115 | if (!ConvertBufferType(buffer, &clipboard_type)) |
[email protected] | 3d4a691 | 2011-03-15 20:56:19 | [diff] [blame] | 116 | return WebData(); |
| 117 | |
| 118 | std::string png_data; |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 119 | delegate_->ReadImage(clipboard_type, &png_data); |
[email protected] | 3d4a691 | 2011-03-15 20:56:19 | [diff] [blame] | 120 | return WebData(png_data); |
| 121 | } |
| 122 | |
[email protected] | c6562f4 | 2011-12-04 06:19:37 | [diff] [blame] | 123 | WebString WebClipboardImpl::readCustomData(Buffer buffer, |
| 124 | const WebString& type) { |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 125 | ui::ClipboardType clipboard_type; |
| 126 | if (!ConvertBufferType(buffer, &clipboard_type)) |
[email protected] | c6562f4 | 2011-12-04 06:19:37 | [diff] [blame] | 127 | return WebString(); |
| 128 | |
[email protected] | 8275835 | 2013-03-29 19:21:31 | [diff] [blame] | 129 | base::string16 data; |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 130 | delegate_->ReadCustomData(clipboard_type, type, &data); |
[email protected] | c6562f4 | 2011-12-04 06:19:37 | [diff] [blame] | 131 | return data; |
| 132 | } |
| 133 | |
[email protected] | d9a0eae9 | 2013-10-25 16:38:55 | [diff] [blame] | 134 | void WebClipboardImpl::writePlainText(const WebString& plain_text) { |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 135 | delegate_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, plain_text); |
| 136 | delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE); |
[email protected] | d9a0eae9 | 2013-10-25 16:38:55 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 139 | void WebClipboardImpl::writeHTML( |
| 140 | const WebString& html_text, const WebURL& source_url, |
| 141 | const WebString& plain_text, bool write_smart_paste) { |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 142 | delegate_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE, html_text, source_url); |
| 143 | delegate_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, plain_text); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 144 | |
| 145 | if (write_smart_paste) |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 146 | delegate_->WriteSmartPasteMarker(ui::CLIPBOARD_TYPE_COPY_PASTE); |
| 147 | delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | f9866fa | 2013-10-02 16:30:45 | [diff] [blame] | 150 | void WebClipboardImpl::writeImage(const WebImage& image, |
| 151 | const WebURL& url, |
| 152 | const WebString& title) { |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 153 | DCHECK(!image.isNull()); |
| 154 | const SkBitmap& bitmap = image.getSkBitmap(); |
| 155 | if (!delegate_->WriteImage(ui::CLIPBOARD_TYPE_COPY_PASTE, bitmap)) |
| 156 | return; |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 157 | |
[email protected] | 59d9aacf | 2009-11-30 22:59:25 | [diff] [blame] | 158 | if (!url.isEmpty()) { |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 159 | delegate_->WriteBookmark(ui::CLIPBOARD_TYPE_COPY_PASTE, url, title); |
[email protected] | 0930323 | 2011-06-09 19:28:31 | [diff] [blame] | 160 | #if !defined(OS_MACOSX) |
| 161 | // When writing the image, we also write the image markup so that pasting |
| 162 | // into rich text editors, such as Gmail, reveals the image. We also don't |
| 163 | // want to call writeText(), since some applications (WordPad) don't pick |
| 164 | // the image if there is also a text format on the clipboard. |
| 165 | // We also don't want to write HTML on a Mac, since Mail.app prefers to use |
| 166 | // the image markup over attaching the actual image. See |
| 167 | // http://crbug.com/33016 for details. |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 168 | delegate_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE, |
| 169 | base::UTF8ToUTF16(URLToImageMarkup(url, title)), |
| 170 | GURL()); |
[email protected] | 0930323 | 2011-06-09 19:28:31 | [diff] [blame] | 171 | #endif |
[email protected] | 59d9aacf | 2009-11-30 22:59:25 | [diff] [blame] | 172 | } |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 173 | delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE); |
[email protected] | c62ce3e | 2009-02-26 00:15:20 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 176 | void WebClipboardImpl::writeDataObject(const WebDragData& data) { |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 177 | const DropData& data_object = DropDataBuilder::Build(data); |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 178 | // TODO(dcheng): Properly support text/uri-list here. |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 179 | // Avoid calling the WriteFoo functions if there is no data associated with a |
| 180 | // type. This prevents stomping on clipboard contents that might have been |
| 181 | // written by extension functions such as chrome.bookmarkManagerPrivate.copy. |
[email protected] | da41ae9 | 2012-06-16 01:28:17 | [diff] [blame] | 182 | if (!data_object.text.is_null()) |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 183 | delegate_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, |
| 184 | data_object.text.string()); |
[email protected] | da41ae9 | 2012-06-16 01:28:17 | [diff] [blame] | 185 | if (!data_object.html.is_null()) |
dcheng | 112adc89 | 2014-11-20 07:16:49 | [diff] [blame] | 186 | delegate_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE, |
| 187 | data_object.html.string(), GURL()); |
| 188 | if (!data_object.custom_data.empty()) |
| 189 | delegate_->WriteCustomData(ui::CLIPBOARD_TYPE_COPY_PASTE, |
| 190 | data_object.custom_data); |
| 191 | delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE); |
[email protected] | 3f031829 | 2011-11-18 21:49:00 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 194 | bool WebClipboardImpl::ConvertBufferType(Buffer buffer, |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 195 | ui::ClipboardType* result) { |
| 196 | *result = ui::CLIPBOARD_TYPE_COPY_PASTE; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 197 | switch (buffer) { |
| 198 | case BufferStandard: |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 199 | break; |
| 200 | case BufferSelection: |
[email protected] | 3e3d520 | 2014-04-12 00:06:59 | [diff] [blame] | 201 | #if defined(USE_X11) && !defined(OS_CHROMEOS) |
[email protected] | d68334c | 2013-10-12 17:02:03 | [diff] [blame] | 202 | *result = ui::CLIPBOARD_TYPE_SELECTION; |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 203 | break; |
[email protected] | 3e3d520 | 2014-04-12 00:06:59 | [diff] [blame] | 204 | #else |
| 205 | // Chrome OS and non-X11 unix builds do not support |
| 206 | // the X selection clipboad. |
| 207 | // TODO: remove the need for this case, see http://crbug.com/361753 |
| 208 | return false; |
[email protected] | 264b319 | 2012-08-10 21:56:46 | [diff] [blame] | 209 | #endif |
[email protected] | 65a6150d | 2009-09-08 22:16:05 | [diff] [blame] | 210 | default: |
| 211 | NOTREACHED(); |
| 212 | return false; |
| 213 | } |
| 214 | return true; |
| 215 | } |
| 216 | |
[email protected] | bb7538f | 2013-06-21 00:40:28 | [diff] [blame] | 217 | } // namespace content |