blob: 45697495fac4d14a4930ec70ad621079b2cc45f2 [file] [log] [blame]
[email protected]2dfeaf92011-01-10 21:08:211// 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]c62ce3e2009-02-26 00:15:204
[email protected]c62ce3e2009-02-26 00:15:205#include "webkit/glue/webclipboard_impl.h"
6
[email protected]c62ce3e2009-02-26 00:15:207#include "base/logging.h"
8#include "base/string_util.h"
[email protected]b1e4eb502010-03-07 20:53:339#include "base/utf_string_conversions.h"
[email protected]c62ce3e2009-02-26 00:15:2010#include "googleurl/src/gurl.h"
11#include "net/base/escape.h"
[email protected]8973f522009-07-03 05:58:4512#include "third_party/skia/include/core/SkBitmap.h"
[email protected]c1d9cdc2011-01-17 06:50:0113#include "third_party/WebKit/Source/WebKit/chromium/public/WebImage.h"
14#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
15#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
16#include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
17#include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h"
[email protected]2dfeaf92011-01-10 21:08:2118#include "ui/base/clipboard/clipboard.h"
[email protected]c62ce3e2009-02-26 00:15:2019#include "webkit/glue/scoped_clipboard_writer_glue.h"
20#include "webkit/glue/webkit_glue.h"
21
[email protected]8973f522009-07-03 05:58:4522#if WEBKIT_USING_CG
23#include "skia/ext/skia_utils_mac.h"
24#endif
25
[email protected]c62ce3e2009-02-26 00:15:2026using WebKit::WebClipboard;
27using WebKit::WebImage;
28using WebKit::WebString;
29using WebKit::WebURL;
[email protected]97c2c032010-07-07 22:43:4130using WebKit::WebVector;
[email protected]c62ce3e2009-02-26 00:15:2031
32namespace webkit_glue {
33
[email protected]b9a0b1b32009-03-30 23:09:3734// Static
35std::string WebClipboardImpl::URLToMarkup(const WebURL& url,
36 const WebString& title) {
[email protected]c62ce3e2009-02-26 00:15:2037 std::string markup("<a href=\"");
38 markup.append(url.spec());
39 markup.append("\">");
40 // TODO(darin): HTML escape this
41 markup.append(EscapeForHTML(UTF16ToUTF8(title)));
42 markup.append("</a>");
43 return markup;
44}
45
[email protected]b9a0b1b32009-03-30 23:09:3746// Static
47std::string WebClipboardImpl::URLToImageMarkup(const WebURL& url,
48 const WebString& title) {
[email protected]c62ce3e2009-02-26 00:15:2049 std::string markup("<img src=\"");
50 markup.append(url.spec());
51 markup.append("\"");
52 if (!title.isEmpty()) {
53 markup.append(" alt=\"");
54 markup.append(EscapeForHTML(UTF16ToUTF8(title)));
55 markup.append("\"");
56 }
57 markup.append("/>");
58 return markup;
59}
60
[email protected]04bf3ad2010-08-27 21:23:4861WebClipboardImpl::~WebClipboardImpl() {
62}
63
[email protected]65a6150d2009-09-08 22:16:0564bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
[email protected]2dfeaf92011-01-10 21:08:2165 ui::Clipboard::FormatType format_type;
66 ui::Clipboard::Buffer buffer_type;
[email protected]c62ce3e2009-02-26 00:15:2067
68 switch (format) {
69 case FormatHTML:
[email protected]2dfeaf92011-01-10 21:08:2170 format_type = ui::Clipboard::GetHtmlFormatType();
[email protected]c62ce3e2009-02-26 00:15:2071 break;
72 case FormatSmartPaste:
[email protected]2dfeaf92011-01-10 21:08:2173 format_type = ui::Clipboard::GetWebKitSmartPasteFormatType();
[email protected]c62ce3e2009-02-26 00:15:2074 break;
75 case FormatBookmark:
76#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]2dfeaf92011-01-10 21:08:2177 format_type = ui::Clipboard::GetUrlWFormatType();
[email protected]c62ce3e2009-02-26 00:15:2078 break;
79#endif
80 default:
81 NOTREACHED();
82 return false;
83 }
84
[email protected]65a6150d2009-09-08 22:16:0585 if (!ConvertBufferType(buffer, &buffer_type))
86 return false;
87
88 return ClipboardIsFormatAvailable(format_type, buffer_type);
[email protected]c62ce3e2009-02-26 00:15:2089}
90
[email protected]65a6150d2009-09-08 22:16:0591WebString WebClipboardImpl::readPlainText(Buffer buffer) {
[email protected]2dfeaf92011-01-10 21:08:2192 ui::Clipboard::Buffer buffer_type;
[email protected]65a6150d2009-09-08 22:16:0593 if (!ConvertBufferType(buffer, &buffer_type))
94 return WebString();
95
[email protected]2dfeaf92011-01-10 21:08:2196 if (ClipboardIsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(),
[email protected]65a6150d2009-09-08 22:16:0597 buffer_type)) {
[email protected]3a2a5d22009-03-04 03:36:3698 string16 text;
[email protected]65a6150d2009-09-08 22:16:0599 ClipboardReadText(buffer_type, &text);
[email protected]c62ce3e2009-02-26 00:15:20100 if (!text.empty())
[email protected]3a2a5d22009-03-04 03:36:36101 return text;
[email protected]c62ce3e2009-02-26 00:15:20102 }
103
[email protected]2dfeaf92011-01-10 21:08:21104 if (ClipboardIsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(),
[email protected]65a6150d2009-09-08 22:16:05105 buffer_type)) {
[email protected]c62ce3e2009-02-26 00:15:20106 std::string text;
[email protected]65a6150d2009-09-08 22:16:05107 ClipboardReadAsciiText(buffer_type, &text);
[email protected]c62ce3e2009-02-26 00:15:20108 if (!text.empty())
[email protected]3a2a5d22009-03-04 03:36:36109 return ASCIIToUTF16(text);
[email protected]c62ce3e2009-02-26 00:15:20110 }
111
112 return WebString();
113}
114
[email protected]65a6150d2009-09-08 22:16:05115WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url) {
[email protected]2dfeaf92011-01-10 21:08:21116 ui::Clipboard::Buffer buffer_type;
[email protected]65a6150d2009-09-08 22:16:05117 if (!ConvertBufferType(buffer, &buffer_type))
118 return WebString();
119
[email protected]3a2a5d22009-03-04 03:36:36120 string16 html_stdstr;
[email protected]c62ce3e2009-02-26 00:15:20121 GURL gurl;
[email protected]65a6150d2009-09-08 22:16:05122 ClipboardReadHTML(buffer_type, &html_stdstr, &gurl);
[email protected]c62ce3e2009-02-26 00:15:20123 *source_url = gurl;
[email protected]3a2a5d22009-03-04 03:36:36124 return html_stdstr;
[email protected]c62ce3e2009-02-26 00:15:20125}
126
127void WebClipboardImpl::writeHTML(
128 const WebString& html_text, const WebURL& source_url,
129 const WebString& plain_text, bool write_smart_paste) {
130 ScopedClipboardWriterGlue scw(ClipboardGetClipboard());
[email protected]3a2a5d22009-03-04 03:36:36131 scw.WriteHTML(html_text, source_url.spec());
132 scw.WriteText(plain_text);
[email protected]c62ce3e2009-02-26 00:15:20133
134 if (write_smart_paste)
135 scw.WriteWebSmartPaste();
136}
137
[email protected]8c95a752009-09-25 10:54:22138void WebClipboardImpl::writePlainText(const WebString& plain_text) {
139 ScopedClipboardWriterGlue scw(ClipboardGetClipboard());
140 scw.WriteText(plain_text);
141}
142
[email protected]c62ce3e2009-02-26 00:15:20143void WebClipboardImpl::writeURL(const WebURL& url, const WebString& title) {
144 ScopedClipboardWriterGlue scw(ClipboardGetClipboard());
145
[email protected]3a2a5d22009-03-04 03:36:36146 scw.WriteBookmark(title, url.spec());
147 scw.WriteHTML(UTF8ToUTF16(URLToMarkup(url, title)), "");
[email protected]39a749c2011-01-28 02:40:46148 scw.WriteText(UTF8ToUTF16(std::string(url.spec())));
[email protected]c62ce3e2009-02-26 00:15:20149}
150
151void WebClipboardImpl::writeImage(
152 const WebImage& image, const WebURL& url, const WebString& title) {
153 ScopedClipboardWriterGlue scw(ClipboardGetClipboard());
154
[email protected]8973f522009-07-03 05:58:45155 if (!image.isNull()) {
156#if WEBKIT_USING_SKIA
157 const SkBitmap& bitmap = image.getSkBitmap();
158#elif WEBKIT_USING_CG
159 const SkBitmap& bitmap = gfx::CGImageToSkBitmap(image.getCGImageRef());
[email protected]c62ce3e2009-02-26 00:15:20160#endif
[email protected]8973f522009-07-03 05:58:45161 SkAutoLockPixels locked(bitmap);
162 scw.WriteBitmapFromPixels(bitmap.getPixels(), image.size());
163 }
[email protected]c62ce3e2009-02-26 00:15:20164
[email protected]59d9aacf2009-11-30 22:59:25165 // 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 the
168 // image if there is also a text format on the clipboard.
169 if (!url.isEmpty()) {
170 scw.WriteBookmark(title, url.spec());
171 scw.WriteHTML(UTF8ToUTF16(URLToImageMarkup(url, title)), "");
172 }
[email protected]c62ce3e2009-02-26 00:15:20173}
174
[email protected]4dfd2d92010-06-18 20:28:18175void WebClipboardImpl::writeData(const WebKit::WebDragData& data) {
176 // TODO(dcheng): Implement this stub.
177}
178
[email protected]97c2c032010-07-07 22:43:41179WebVector<WebString> WebClipboardImpl::readAvailableTypes(
180 Buffer buffer, bool* contains_filenames) {
[email protected]2dfeaf92011-01-10 21:08:21181 ui::Clipboard::Buffer buffer_type;
[email protected]97c2c032010-07-07 22:43:41182 std::vector<string16> types;
183 if (ConvertBufferType(buffer, &buffer_type)) {
184 ClipboardReadAvailableTypes(buffer_type, &types, contains_filenames);
185 }
186 return types;
187}
188
189bool WebClipboardImpl::readData(Buffer buffer, const WebString& type,
190 WebString* data, WebString* metadata) {
[email protected]2dfeaf92011-01-10 21:08:21191 ui::Clipboard::Buffer buffer_type;
[email protected]97c2c032010-07-07 22:43:41192 if (!ConvertBufferType(buffer, &buffer_type))
193 return false;
194
195 string16 data_out;
196 string16 metadata_out;
197 bool result = ClipboardReadData(buffer_type, type, &data_out, &metadata_out);
198 if (result) {
199 *data = data_out;
200 *metadata = metadata_out;
201 }
202 return result;
203}
204
205WebVector<WebString> WebClipboardImpl::readFilenames(Buffer buffer) {
[email protected]2dfeaf92011-01-10 21:08:21206 ui::Clipboard::Buffer buffer_type;
[email protected]97c2c032010-07-07 22:43:41207 std::vector<string16> filenames;
208 if (ConvertBufferType(buffer, &buffer_type)) {
209 ClipboardReadFilenames(buffer_type, &filenames);
210 }
211 return filenames;
212}
213
[email protected]65a6150d2009-09-08 22:16:05214bool WebClipboardImpl::ConvertBufferType(Buffer buffer,
[email protected]2dfeaf92011-01-10 21:08:21215 ui::Clipboard::Buffer* result) {
[email protected]65a6150d2009-09-08 22:16:05216 switch (buffer) {
217 case BufferStandard:
[email protected]2dfeaf92011-01-10 21:08:21218 *result = ui::Clipboard::BUFFER_STANDARD;
[email protected]65a6150d2009-09-08 22:16:05219 break;
[email protected]97c2c032010-07-07 22:43:41220 case BufferDrag:
[email protected]2dfeaf92011-01-10 21:08:21221 *result = ui::Clipboard::BUFFER_DRAG;
[email protected]65a6150d2009-09-08 22:16:05222 case BufferSelection:
[email protected]10a6e77b2009-12-31 01:03:52223#if defined(USE_X11)
[email protected]2dfeaf92011-01-10 21:08:21224 *result = ui::Clipboard::BUFFER_SELECTION;
[email protected]65a6150d2009-09-08 22:16:05225 break;
226#endif
227 default:
228 NOTREACHED();
229 return false;
230 }
231 return true;
232}
233
[email protected]c62ce3e2009-02-26 00:15:20234} // namespace webkit_glue