blob: 2408eda86dcee3b5a4d2e0501d2922355bd9e667 [file] [log] [blame]
[email protected]bb7538f2013-06-21 00:40:281// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]2dfeaf92011-01-10 21:08:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]c62ce3e2009-02-26 00:15:204
[email protected]bb7538f2013-06-21 00:40:285#include "content/renderer/webclipboard_impl.h"
[email protected]c62ce3e2009-02-26 00:15:206
[email protected]c62ce3e2009-02-26 00:15:207#include "base/logging.h"
[email protected]2ffbb482011-12-05 23:41:178#include "base/pickle.h"
[email protected]e464fee2013-06-11 12:42:209#include "base/strings/string_util.h"
[email protected]906265872013-06-07 22:40:4510#include "base/strings/utf_string_conversions.h"
[email protected]5c2f32e72014-03-20 20:22:1711#include "content/common/clipboard_format.h"
[email protected]dc293a72013-07-01 11:11:2212#include "content/public/common/drop_data.h"
[email protected]f9866fa2013-10-02 16:30:4513#include "content/renderer/clipboard_utils.h"
[email protected]dc293a72013-07-01 11:11:2214#include "content/renderer/drop_data_builder.h"
[email protected]bb7538f2013-06-21 00:40:2815#include "content/renderer/scoped_clipboard_writer_glue.h"
[email protected]c10884462013-05-30 00:22:0916#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]8973f522009-07-03 05:58:4523#include "third_party/skia/include/core/SkBitmap.h"
[email protected]2dfeaf92011-01-10 21:08:2124#include "ui/base/clipboard/clipboard.h"
[email protected]2ffbb482011-12-05 23:41:1725#include "ui/base/clipboard/custom_data_helper.h"
[email protected]707e1c42013-07-09 21:18:5826#include "url/gurl.h"
[email protected]c62ce3e2009-02-26 00:15:2027
[email protected]180ef242013-11-07 06:50:4628using blink::WebClipboard;
29using blink::WebData;
30using blink::WebDragData;
31using blink::WebImage;
32using blink::WebString;
33using blink::WebURL;
34using blink::WebVector;
[email protected]c62ce3e2009-02-26 00:15:2035
[email protected]bb7538f2013-06-21 00:40:2836namespace content {
[email protected]c62ce3e2009-02-26 00:15:2037
[email protected]0de5d8602011-11-22 03:48:5238WebClipboardImpl::WebClipboardImpl(ClipboardClient* client)
39 : client_(client) {
40}
41
[email protected]04bf3ad2010-08-27 21:23:4842WebClipboardImpl::~WebClipboardImpl() {
43}
44
[email protected]9cf12c32011-11-10 19:28:0745uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) {
[email protected]d68334c2013-10-12 17:02:0346 ui::ClipboardType clipboard_type;
47 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]9cf12c32011-11-10 19:28:0748 return 0;
49
[email protected]d68334c2013-10-12 17:02:0350 return client_->GetSequenceNumber(clipboard_type);
[email protected]36780a12011-11-04 18:16:4451}
52
[email protected]65a6150d2009-09-08 22:16:0553bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
[email protected]d68334c2013-10-12 17:02:0354 ui::ClipboardType clipboard_type = ui::CLIPBOARD_TYPE_COPY_PASTE;
[email protected]c62ce3e2009-02-26 00:15:2055
[email protected]d68334c2013-10-12 17:02:0356 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]606876c2011-03-24 17:13:3857 return false;
58
[email protected]c62ce3e2009-02-26 00:15:2059 switch (format) {
[email protected]606876c2011-03-24 17:13:3860 case FormatPlainText:
[email protected]5c2f32e72014-03-20 20:22:1761 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_PLAINTEXT,
62 clipboard_type);
[email protected]c62ce3e2009-02-26 00:15:2063 case FormatHTML:
[email protected]5c2f32e72014-03-20 20:22:1764 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_HTML, clipboard_type);
[email protected]c62ce3e2009-02-26 00:15:2065 case FormatSmartPaste:
[email protected]5c2f32e72014-03-20 20:22:1766 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_SMART_PASTE,
[email protected]d68334c2013-10-12 17:02:0367 clipboard_type);
[email protected]5c2f32e72014-03-20 20:22:1768 case FormatBookmark:
69 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_BOOKMARK,
70 clipboard_type);
[email protected]c62ce3e2009-02-26 00:15:2071 default:
72 NOTREACHED();
[email protected]c62ce3e2009-02-26 00:15:2073 }
74
[email protected]ff53be62011-12-07 23:28:2975 return false;
[email protected]c62ce3e2009-02-26 00:15:2076}
77
[email protected]36780a12011-11-04 18:16:4478WebVector<WebString> WebClipboardImpl::readAvailableTypes(
79 Buffer buffer, bool* contains_filenames) {
[email protected]d68334c2013-10-12 17:02:0380 ui::ClipboardType clipboard_type;
[email protected]82758352013-03-29 19:21:3181 std::vector<base::string16> types;
[email protected]d68334c2013-10-12 17:02:0382 if (ConvertBufferType(buffer, &clipboard_type)) {
83 client_->ReadAvailableTypes(clipboard_type, &types, contains_filenames);
[email protected]36780a12011-11-04 18:16:4484 }
85 return types;
86}
87
[email protected]65a6150d2009-09-08 22:16:0588WebString WebClipboardImpl::readPlainText(Buffer buffer) {
[email protected]d68334c2013-10-12 17:02:0389 ui::ClipboardType clipboard_type;
90 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]65a6150d2009-09-08 22:16:0591 return WebString();
92
[email protected]5c2f32e72014-03-20 20:22:1793 base::string16 text;
94 client_->ReadText(clipboard_type, &text);
95 return text;
[email protected]c62ce3e2009-02-26 00:15:2096}
97
[email protected]a96790b2011-10-06 22:24:1098WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url,
99 unsigned* fragment_start,
100 unsigned* fragment_end) {
[email protected]d68334c2013-10-12 17:02:03101 ui::ClipboardType clipboard_type;
102 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]a96790b2011-10-06 22:24:10103 return WebString();
104
[email protected]82758352013-03-29 19:21:31105 base::string16 html_stdstr;
[email protected]a96790b2011-10-06 22:24:10106 GURL gurl;
[email protected]d68334c2013-10-12 17:02:03107 client_->ReadHTML(clipboard_type, &html_stdstr, &gurl,
[email protected]a96790b2011-10-06 22:24:10108 static_cast<uint32*>(fragment_start),
109 static_cast<uint32*>(fragment_end));
[email protected]c62ce3e2009-02-26 00:15:20110 *source_url = gurl;
[email protected]3a2a5d22009-03-04 03:36:36111 return html_stdstr;
[email protected]c62ce3e2009-02-26 00:15:20112}
113
[email protected]3d4a6912011-03-15 20:56:19114WebData WebClipboardImpl::readImage(Buffer buffer) {
[email protected]d68334c2013-10-12 17:02:03115 ui::ClipboardType clipboard_type;
116 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]3d4a6912011-03-15 20:56:19117 return WebData();
118
119 std::string png_data;
[email protected]d68334c2013-10-12 17:02:03120 client_->ReadImage(clipboard_type, &png_data);
[email protected]3d4a6912011-03-15 20:56:19121 return WebData(png_data);
122}
123
[email protected]c6562f42011-12-04 06:19:37124WebString WebClipboardImpl::readCustomData(Buffer buffer,
125 const WebString& type) {
[email protected]d68334c2013-10-12 17:02:03126 ui::ClipboardType clipboard_type;
127 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]c6562f42011-12-04 06:19:37128 return WebString();
129
[email protected]82758352013-03-29 19:21:31130 base::string16 data;
[email protected]d68334c2013-10-12 17:02:03131 client_->ReadCustomData(clipboard_type, type, &data);
[email protected]c6562f42011-12-04 06:19:37132 return data;
133}
134
[email protected]d9a0eae92013-10-25 16:38:55135void WebClipboardImpl::writePlainText(const WebString& plain_text) {
136 ScopedClipboardWriterGlue scw(client_);
137 scw.WriteText(plain_text);
138}
139
[email protected]c62ce3e2009-02-26 00:15:20140void WebClipboardImpl::writeHTML(
141 const WebString& html_text, const WebURL& source_url,
142 const WebString& plain_text, bool write_smart_paste) {
[email protected]0de5d8602011-11-22 03:48:52143 ScopedClipboardWriterGlue scw(client_);
[email protected]3a2a5d22009-03-04 03:36:36144 scw.WriteHTML(html_text, source_url.spec());
145 scw.WriteText(plain_text);
[email protected]c62ce3e2009-02-26 00:15:20146
147 if (write_smart_paste)
148 scw.WriteWebSmartPaste();
149}
150
[email protected]f9866fa2013-10-02 16:30:45151void WebClipboardImpl::writeImage(const WebImage& image,
152 const WebURL& url,
153 const WebString& title) {
[email protected]0de5d8602011-11-22 03:48:52154 ScopedClipboardWriterGlue scw(client_);
[email protected]c62ce3e2009-02-26 00:15:20155
[email protected]8973f522009-07-03 05:58:45156 if (!image.isNull()) {
[email protected]8973f522009-07-03 05:58:45157 const SkBitmap& bitmap = image.getSkBitmap();
[email protected]8973f522009-07-03 05:58:45158 SkAutoLockPixels locked(bitmap);
159 scw.WriteBitmapFromPixels(bitmap.getPixels(), image.size());
160 }
[email protected]c62ce3e2009-02-26 00:15:20161
[email protected]59d9aacf2009-11-30 22:59:25162 if (!url.isEmpty()) {
163 scw.WriteBookmark(title, url.spec());
[email protected]09303232011-06-09 19:28:31164#if !defined(OS_MACOSX)
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
168 // the image if there is also a text format on the clipboard.
169 // We also don't want to write HTML on a Mac, since Mail.app prefers to use
170 // the image markup over attaching the actual image. See
171 // http://crbug.com/33016 for details.
[email protected]32956122013-12-25 07:29:24172 scw.WriteHTML(base::UTF8ToUTF16(URLToImageMarkup(url, title)),
173 std::string());
[email protected]09303232011-06-09 19:28:31174#endif
[email protected]59d9aacf2009-11-30 22:59:25175 }
[email protected]c62ce3e2009-02-26 00:15:20176}
177
[email protected]3f0318292011-11-18 21:49:00178void WebClipboardImpl::writeDataObject(const WebDragData& data) {
[email protected]0de5d8602011-11-22 03:48:52179 ScopedClipboardWriterGlue scw(client_);
[email protected]3f0318292011-11-18 21:49:00180
[email protected]dc293a72013-07-01 11:11:22181 const DropData& data_object = DropDataBuilder::Build(data);
[email protected]3f0318292011-11-18 21:49:00182 // TODO(dcheng): Properly support text/uri-list here.
[email protected]da41ae92012-06-16 01:28:17183 if (!data_object.text.is_null())
184 scw.WriteText(data_object.text.string());
185 if (!data_object.html.is_null())
[email protected]007b3f82013-04-09 08:46:45186 scw.WriteHTML(data_object.html.string(), std::string());
[email protected]fbf75412012-01-07 01:26:52187 // If there is no custom data, avoid calling WritePickledData. This ensures
188 // that ScopedClipboardWriterGlue's dtor remains a no-op if the page didn't
189 // modify the DataTransfer object, which is important to avoid stomping on
190 // any clipboard contents written by extension functions such as
[email protected]a19258af2012-09-21 06:36:33191 // chrome.bookmarkManagerPrivate.copy.
[email protected]fbf75412012-01-07 01:26:52192 if (!data_object.custom_data.empty()) {
193 Pickle pickle;
194 ui::WriteCustomDataToPickle(data_object.custom_data, &pickle);
195 scw.WritePickledData(pickle, ui::Clipboard::GetWebCustomDataFormatType());
196 }
[email protected]3f0318292011-11-18 21:49:00197}
198
[email protected]65a6150d2009-09-08 22:16:05199bool WebClipboardImpl::ConvertBufferType(Buffer buffer,
[email protected]d68334c2013-10-12 17:02:03200 ui::ClipboardType* result) {
201 *result = ui::CLIPBOARD_TYPE_COPY_PASTE;
[email protected]65a6150d2009-09-08 22:16:05202 switch (buffer) {
203 case BufferStandard:
[email protected]65a6150d2009-09-08 22:16:05204 break;
205 case BufferSelection:
[email protected]264b3192012-08-10 21:56:46206#if defined(USE_X11)
207#if defined(OS_CHROMEOS)
208 // Chrome OS only supports the standard clipboard,
209 // but not the X selection clipboad.
210 return false;
211#else
[email protected]d68334c2013-10-12 17:02:03212 *result = ui::CLIPBOARD_TYPE_SELECTION;
[email protected]65a6150d2009-09-08 22:16:05213 break;
214#endif
[email protected]264b3192012-08-10 21:56:46215#endif
[email protected]65a6150d2009-09-08 22:16:05216 default:
217 NOTREACHED();
218 return false;
219 }
220 return true;
221}
222
[email protected]bb7538f2013-06-21 00:40:28223} // namespace content