blob: 19f2362b1c0609861dd60cbfa13eca479cc79488 [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]e464fee2013-06-11 12:42:208#include "base/strings/string_util.h"
[email protected]906265872013-06-07 22:40:459#include "base/strings/utf_string_conversions.h"
[email protected]5c2f32e72014-03-20 20:22:1710#include "content/common/clipboard_format.h"
[email protected]dc293a72013-07-01 11:11:2211#include "content/public/common/drop_data.h"
[email protected]f9866fa2013-10-02 16:30:4512#include "content/renderer/clipboard_utils.h"
[email protected]dc293a72013-07-01 11:11:2213#include "content/renderer/drop_data_builder.h"
dcheng112adc892014-11-20 07:16:4914#include "content/renderer/renderer_clipboard_delegate.h"
[email protected]c10884462013-05-30 00:22:0915#include "third_party/WebKit/public/platform/WebData.h"
16#include "third_party/WebKit/public/platform/WebDragData.h"
17#include "third_party/WebKit/public/platform/WebImage.h"
18#include "third_party/WebKit/public/platform/WebSize.h"
19#include "third_party/WebKit/public/platform/WebString.h"
20#include "third_party/WebKit/public/platform/WebURL.h"
21#include "third_party/WebKit/public/platform/WebVector.h"
[email protected]707e1c42013-07-09 21:18:5822#include "url/gurl.h"
[email protected]c62ce3e2009-02-26 00:15:2023
[email protected]180ef242013-11-07 06:50:4624using blink::WebClipboard;
25using blink::WebData;
26using blink::WebDragData;
27using blink::WebImage;
28using blink::WebString;
29using blink::WebURL;
30using blink::WebVector;
[email protected]c62ce3e2009-02-26 00:15:2031
[email protected]bb7538f2013-06-21 00:40:2832namespace content {
[email protected]c62ce3e2009-02-26 00:15:2033
dcheng112adc892014-11-20 07:16:4934WebClipboardImpl::WebClipboardImpl(RendererClipboardDelegate* delegate)
35 : delegate_(delegate) {
36 DCHECK(delegate);
[email protected]0de5d8602011-11-22 03:48:5237}
38
[email protected]04bf3ad2010-08-27 21:23:4839WebClipboardImpl::~WebClipboardImpl() {
40}
41
[email protected]9cf12c32011-11-10 19:28:0742uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) {
[email protected]d68334c2013-10-12 17:02:0343 ui::ClipboardType clipboard_type;
44 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]9cf12c32011-11-10 19:28:0745 return 0;
46
dcheng112adc892014-11-20 07:16:4947 return delegate_->GetSequenceNumber(clipboard_type);
[email protected]36780a12011-11-04 18:16:4448}
49
[email protected]65a6150d2009-09-08 22:16:0550bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
[email protected]d68334c2013-10-12 17:02:0351 ui::ClipboardType clipboard_type = ui::CLIPBOARD_TYPE_COPY_PASTE;
[email protected]c62ce3e2009-02-26 00:15:2052
[email protected]d68334c2013-10-12 17:02:0353 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]606876c2011-03-24 17:13:3854 return false;
55
[email protected]c62ce3e2009-02-26 00:15:2056 switch (format) {
[email protected]606876c2011-03-24 17:13:3857 case FormatPlainText:
dcheng112adc892014-11-20 07:16:4958 return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_PLAINTEXT,
59 clipboard_type);
[email protected]c62ce3e2009-02-26 00:15:2060 case FormatHTML:
dcheng112adc892014-11-20 07:16:4961 return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_HTML,
62 clipboard_type);
[email protected]c62ce3e2009-02-26 00:15:2063 case FormatSmartPaste:
dcheng112adc892014-11-20 07:16:4964 return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_SMART_PASTE,
65 clipboard_type);
[email protected]5c2f32e72014-03-20 20:22:1766 case FormatBookmark:
dcheng112adc892014-11-20 07:16:4967 return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_BOOKMARK,
68 clipboard_type);
[email protected]c62ce3e2009-02-26 00:15:2069 default:
70 NOTREACHED();
[email protected]c62ce3e2009-02-26 00:15:2071 }
72
[email protected]ff53be62011-12-07 23:28:2973 return false;
[email protected]c62ce3e2009-02-26 00:15:2074}
75
[email protected]36780a12011-11-04 18:16:4476WebVector<WebString> WebClipboardImpl::readAvailableTypes(
77 Buffer buffer, bool* contains_filenames) {
[email protected]d68334c2013-10-12 17:02:0378 ui::ClipboardType clipboard_type;
[email protected]82758352013-03-29 19:21:3179 std::vector<base::string16> types;
[email protected]d68334c2013-10-12 17:02:0380 if (ConvertBufferType(buffer, &clipboard_type)) {
dcheng112adc892014-11-20 07:16:4981 delegate_->ReadAvailableTypes(clipboard_type, &types, contains_filenames);
[email protected]36780a12011-11-04 18:16:4482 }
83 return types;
84}
85
[email protected]65a6150d2009-09-08 22:16:0586WebString WebClipboardImpl::readPlainText(Buffer buffer) {
[email protected]d68334c2013-10-12 17:02:0387 ui::ClipboardType clipboard_type;
88 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]65a6150d2009-09-08 22:16:0589 return WebString();
90
[email protected]5c2f32e72014-03-20 20:22:1791 base::string16 text;
dcheng112adc892014-11-20 07:16:4992 delegate_->ReadText(clipboard_type, &text);
[email protected]5c2f32e72014-03-20 20:22:1793 return text;
[email protected]c62ce3e2009-02-26 00:15:2094}
95
[email protected]a96790b2011-10-06 22:24:1096WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url,
97 unsigned* fragment_start,
98 unsigned* fragment_end) {
[email protected]d68334c2013-10-12 17:02:0399 ui::ClipboardType clipboard_type;
100 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]a96790b2011-10-06 22:24:10101 return WebString();
102
[email protected]82758352013-03-29 19:21:31103 base::string16 html_stdstr;
[email protected]a96790b2011-10-06 22:24:10104 GURL gurl;
dcheng112adc892014-11-20 07:16:49105 delegate_->ReadHTML(clipboard_type, &html_stdstr, &gurl,
106 static_cast<uint32*>(fragment_start),
107 static_cast<uint32*>(fragment_end));
[email protected]c62ce3e2009-02-26 00:15:20108 *source_url = gurl;
[email protected]3a2a5d22009-03-04 03:36:36109 return html_stdstr;
[email protected]c62ce3e2009-02-26 00:15:20110}
111
[email protected]3d4a6912011-03-15 20:56:19112WebData WebClipboardImpl::readImage(Buffer buffer) {
[email protected]d68334c2013-10-12 17:02:03113 ui::ClipboardType clipboard_type;
114 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]3d4a6912011-03-15 20:56:19115 return WebData();
116
117 std::string png_data;
dcheng112adc892014-11-20 07:16:49118 delegate_->ReadImage(clipboard_type, &png_data);
[email protected]3d4a6912011-03-15 20:56:19119 return WebData(png_data);
120}
121
[email protected]c6562f42011-12-04 06:19:37122WebString WebClipboardImpl::readCustomData(Buffer buffer,
123 const WebString& type) {
[email protected]d68334c2013-10-12 17:02:03124 ui::ClipboardType clipboard_type;
125 if (!ConvertBufferType(buffer, &clipboard_type))
[email protected]c6562f42011-12-04 06:19:37126 return WebString();
127
[email protected]82758352013-03-29 19:21:31128 base::string16 data;
dcheng112adc892014-11-20 07:16:49129 delegate_->ReadCustomData(clipboard_type, type, &data);
[email protected]c6562f42011-12-04 06:19:37130 return data;
131}
132
[email protected]d9a0eae92013-10-25 16:38:55133void WebClipboardImpl::writePlainText(const WebString& plain_text) {
dcheng112adc892014-11-20 07:16:49134 delegate_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, plain_text);
135 delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE);
[email protected]d9a0eae92013-10-25 16:38:55136}
137
[email protected]c62ce3e2009-02-26 00:15:20138void WebClipboardImpl::writeHTML(
139 const WebString& html_text, const WebURL& source_url,
140 const WebString& plain_text, bool write_smart_paste) {
dcheng112adc892014-11-20 07:16:49141 delegate_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE, html_text, source_url);
142 delegate_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, plain_text);
[email protected]c62ce3e2009-02-26 00:15:20143
144 if (write_smart_paste)
dcheng112adc892014-11-20 07:16:49145 delegate_->WriteSmartPasteMarker(ui::CLIPBOARD_TYPE_COPY_PASTE);
146 delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE);
[email protected]c62ce3e2009-02-26 00:15:20147}
148
[email protected]f9866fa2013-10-02 16:30:45149void WebClipboardImpl::writeImage(const WebImage& image,
150 const WebURL& url,
151 const WebString& title) {
dcheng112adc892014-11-20 07:16:49152 DCHECK(!image.isNull());
153 const SkBitmap& bitmap = image.getSkBitmap();
154 if (!delegate_->WriteImage(ui::CLIPBOARD_TYPE_COPY_PASTE, bitmap))
155 return;
[email protected]c62ce3e2009-02-26 00:15:20156
[email protected]59d9aacf2009-11-30 22:59:25157 if (!url.isEmpty()) {
dcheng112adc892014-11-20 07:16:49158 delegate_->WriteBookmark(ui::CLIPBOARD_TYPE_COPY_PASTE, url, title);
[email protected]09303232011-06-09 19:28:31159#if !defined(OS_MACOSX)
160 // When writing the image, we also write the image markup so that pasting
161 // into rich text editors, such as Gmail, reveals the image. We also don't
162 // want to call writeText(), since some applications (WordPad) don't pick
163 // the image if there is also a text format on the clipboard.
164 // We also don't want to write HTML on a Mac, since Mail.app prefers to use
165 // the image markup over attaching the actual image. See
166 // http://crbug.com/33016 for details.
dcheng112adc892014-11-20 07:16:49167 delegate_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE,
168 base::UTF8ToUTF16(URLToImageMarkup(url, title)),
169 GURL());
[email protected]09303232011-06-09 19:28:31170#endif
[email protected]59d9aacf2009-11-30 22:59:25171 }
dcheng112adc892014-11-20 07:16:49172 delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE);
[email protected]c62ce3e2009-02-26 00:15:20173}
174
[email protected]3f0318292011-11-18 21:49:00175void WebClipboardImpl::writeDataObject(const WebDragData& data) {
[email protected]dc293a72013-07-01 11:11:22176 const DropData& data_object = DropDataBuilder::Build(data);
[email protected]3f0318292011-11-18 21:49:00177 // TODO(dcheng): Properly support text/uri-list here.
dcheng112adc892014-11-20 07:16:49178 // Avoid calling the WriteFoo functions if there is no data associated with a
179 // type. This prevents stomping on clipboard contents that might have been
180 // written by extension functions such as chrome.bookmarkManagerPrivate.copy.
[email protected]da41ae92012-06-16 01:28:17181 if (!data_object.text.is_null())
dcheng112adc892014-11-20 07:16:49182 delegate_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE,
183 data_object.text.string());
[email protected]da41ae92012-06-16 01:28:17184 if (!data_object.html.is_null())
dcheng112adc892014-11-20 07:16:49185 delegate_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE,
186 data_object.html.string(), GURL());
187 if (!data_object.custom_data.empty())
188 delegate_->WriteCustomData(ui::CLIPBOARD_TYPE_COPY_PASTE,
189 data_object.custom_data);
190 delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE);
[email protected]3f0318292011-11-18 21:49:00191}
192
[email protected]65a6150d2009-09-08 22:16:05193bool WebClipboardImpl::ConvertBufferType(Buffer buffer,
[email protected]d68334c2013-10-12 17:02:03194 ui::ClipboardType* result) {
195 *result = ui::CLIPBOARD_TYPE_COPY_PASTE;
[email protected]65a6150d2009-09-08 22:16:05196 switch (buffer) {
197 case BufferStandard:
[email protected]65a6150d2009-09-08 22:16:05198 break;
199 case BufferSelection:
[email protected]3e3d5202014-04-12 00:06:59200#if defined(USE_X11) && !defined(OS_CHROMEOS)
[email protected]d68334c2013-10-12 17:02:03201 *result = ui::CLIPBOARD_TYPE_SELECTION;
[email protected]65a6150d2009-09-08 22:16:05202 break;
[email protected]3e3d5202014-04-12 00:06:59203#else
204 // Chrome OS and non-X11 unix builds do not support
205 // the X selection clipboad.
206 // TODO: remove the need for this case, see http://crbug.com/361753
207 return false;
[email protected]264b3192012-08-10 21:56:46208#endif
[email protected]65a6150d2009-09-08 22:16:05209 default:
210 NOTREACHED();
211 return false;
212 }
213 return true;
214}
215
[email protected]bb7538f2013-06-21 00:40:28216} // namespace content