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