| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/webclipboard_impl.h" | 5 #include "content/renderer/webclipboard_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/pickle.h" |
| 8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/common/clipboard_format.h" | 11 #include "content/common/clipboard_format.h" |
| 11 #include "content/public/common/drop_data.h" | 12 #include "content/public/common/drop_data.h" |
| 12 #include "content/renderer/clipboard_utils.h" | 13 #include "content/renderer/clipboard_utils.h" |
| 13 #include "content/renderer/drop_data_builder.h" | 14 #include "content/renderer/drop_data_builder.h" |
| 14 #include "content/renderer/renderer_clipboard_delegate.h" | 15 #include "content/renderer/scoped_clipboard_writer_glue.h" |
| 15 #include "third_party/WebKit/public/platform/WebData.h" | 16 #include "third_party/WebKit/public/platform/WebData.h" |
| 16 #include "third_party/WebKit/public/platform/WebDragData.h" | 17 #include "third_party/WebKit/public/platform/WebDragData.h" |
| 17 #include "third_party/WebKit/public/platform/WebImage.h" | 18 #include "third_party/WebKit/public/platform/WebImage.h" |
| 18 #include "third_party/WebKit/public/platform/WebSize.h" | 19 #include "third_party/WebKit/public/platform/WebSize.h" |
| 19 #include "third_party/WebKit/public/platform/WebString.h" | 20 #include "third_party/WebKit/public/platform/WebString.h" |
| 20 #include "third_party/WebKit/public/platform/WebURL.h" | 21 #include "third_party/WebKit/public/platform/WebURL.h" |
| 21 #include "third_party/WebKit/public/platform/WebVector.h" | 22 #include "third_party/WebKit/public/platform/WebVector.h" |
| 23 #include "third_party/skia/include/core/SkBitmap.h" |
| 24 #include "ui/base/clipboard/clipboard.h" |
| 25 #include "ui/base/clipboard/custom_data_helper.h" |
| 22 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 23 | 27 |
| 24 using blink::WebClipboard; | 28 using blink::WebClipboard; |
| 25 using blink::WebData; | 29 using blink::WebData; |
| 26 using blink::WebDragData; | 30 using blink::WebDragData; |
| 27 using blink::WebImage; | 31 using blink::WebImage; |
| 28 using blink::WebString; | 32 using blink::WebString; |
| 29 using blink::WebURL; | 33 using blink::WebURL; |
| 30 using blink::WebVector; | 34 using blink::WebVector; |
| 31 | 35 |
| 32 namespace content { | 36 namespace content { |
| 33 | 37 |
| 34 WebClipboardImpl::WebClipboardImpl(RendererClipboardDelegate* delegate) | 38 WebClipboardImpl::WebClipboardImpl(ClipboardClient* client) |
| 35 : delegate_(delegate) { | 39 : client_(client) { |
| 36 DCHECK(delegate); | |
| 37 } | 40 } |
| 38 | 41 |
| 39 WebClipboardImpl::~WebClipboardImpl() { | 42 WebClipboardImpl::~WebClipboardImpl() { |
| 40 } | 43 } |
| 41 | 44 |
| 42 uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) { | 45 uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) { |
| 43 ui::ClipboardType clipboard_type; | 46 ui::ClipboardType clipboard_type; |
| 44 if (!ConvertBufferType(buffer, &clipboard_type)) | 47 if (!ConvertBufferType(buffer, &clipboard_type)) |
| 45 return 0; | 48 return 0; |
| 46 | 49 |
| 47 return delegate_->GetSequenceNumber(clipboard_type); | 50 return client_->GetSequenceNumber(clipboard_type); |
| 48 } | 51 } |
| 49 | 52 |
| 50 bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { | 53 bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { |
| 51 ui::ClipboardType clipboard_type = ui::CLIPBOARD_TYPE_COPY_PASTE; | 54 ui::ClipboardType clipboard_type = ui::CLIPBOARD_TYPE_COPY_PASTE; |
| 52 | 55 |
| 53 if (!ConvertBufferType(buffer, &clipboard_type)) | 56 if (!ConvertBufferType(buffer, &clipboard_type)) |
| 54 return false; | 57 return false; |
| 55 | 58 |
| 56 switch (format) { | 59 switch (format) { |
| 57 case FormatPlainText: | 60 case FormatPlainText: |
| 58 return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_PLAINTEXT, | 61 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_PLAINTEXT, |
| 59 clipboard_type); | 62 clipboard_type); |
| 60 case FormatHTML: | 63 case FormatHTML: |
| 61 return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_HTML, | 64 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_HTML, clipboard_type); |
| 62 clipboard_type); | |
| 63 case FormatSmartPaste: | 65 case FormatSmartPaste: |
| 64 return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_SMART_PASTE, | 66 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_SMART_PASTE, |
| 65 clipboard_type); | 67 clipboard_type); |
| 66 case FormatBookmark: | 68 case FormatBookmark: |
| 67 return delegate_->IsFormatAvailable(CLIPBOARD_FORMAT_BOOKMARK, | 69 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_BOOKMARK, |
| 68 clipboard_type); | 70 clipboard_type); |
| 69 default: | 71 default: |
| 70 NOTREACHED(); | 72 NOTREACHED(); |
| 71 } | 73 } |
| 72 | 74 |
| 73 return false; | 75 return false; |
| 74 } | 76 } |
| 75 | 77 |
| 76 WebVector<WebString> WebClipboardImpl::readAvailableTypes( | 78 WebVector<WebString> WebClipboardImpl::readAvailableTypes( |
| 77 Buffer buffer, bool* contains_filenames) { | 79 Buffer buffer, bool* contains_filenames) { |
| 78 ui::ClipboardType clipboard_type; | 80 ui::ClipboardType clipboard_type; |
| 79 std::vector<base::string16> types; | 81 std::vector<base::string16> types; |
| 80 if (ConvertBufferType(buffer, &clipboard_type)) { | 82 if (ConvertBufferType(buffer, &clipboard_type)) { |
| 81 delegate_->ReadAvailableTypes(clipboard_type, &types, contains_filenames); | 83 client_->ReadAvailableTypes(clipboard_type, &types, contains_filenames); |
| 82 } | 84 } |
| 83 return types; | 85 return types; |
| 84 } | 86 } |
| 85 | 87 |
| 86 WebString WebClipboardImpl::readPlainText(Buffer buffer) { | 88 WebString WebClipboardImpl::readPlainText(Buffer buffer) { |
| 87 ui::ClipboardType clipboard_type; | 89 ui::ClipboardType clipboard_type; |
| 88 if (!ConvertBufferType(buffer, &clipboard_type)) | 90 if (!ConvertBufferType(buffer, &clipboard_type)) |
| 89 return WebString(); | 91 return WebString(); |
| 90 | 92 |
| 91 base::string16 text; | 93 base::string16 text; |
| 92 delegate_->ReadText(clipboard_type, &text); | 94 client_->ReadText(clipboard_type, &text); |
| 93 return text; | 95 return text; |
| 94 } | 96 } |
| 95 | 97 |
| 96 WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url, | 98 WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url, |
| 97 unsigned* fragment_start, | 99 unsigned* fragment_start, |
| 98 unsigned* fragment_end) { | 100 unsigned* fragment_end) { |
| 99 ui::ClipboardType clipboard_type; | 101 ui::ClipboardType clipboard_type; |
| 100 if (!ConvertBufferType(buffer, &clipboard_type)) | 102 if (!ConvertBufferType(buffer, &clipboard_type)) |
| 101 return WebString(); | 103 return WebString(); |
| 102 | 104 |
| 103 base::string16 html_stdstr; | 105 base::string16 html_stdstr; |
| 104 GURL gurl; | 106 GURL gurl; |
| 105 delegate_->ReadHTML(clipboard_type, | 107 client_->ReadHTML(clipboard_type, &html_stdstr, &gurl, |
| 106 &html_stdstr, | 108 static_cast<uint32*>(fragment_start), |
| 107 &gurl, | 109 static_cast<uint32*>(fragment_end)); |
| 108 static_cast<uint32*>(fragment_start), | |
| 109 static_cast<uint32*>(fragment_end)); | |
| 110 *source_url = gurl; | 110 *source_url = gurl; |
| 111 return html_stdstr; | 111 return html_stdstr; |
| 112 } | 112 } |
| 113 | 113 |
| 114 WebData WebClipboardImpl::readImage(Buffer buffer) { | 114 WebData WebClipboardImpl::readImage(Buffer buffer) { |
| 115 ui::ClipboardType clipboard_type; | 115 ui::ClipboardType clipboard_type; |
| 116 if (!ConvertBufferType(buffer, &clipboard_type)) | 116 if (!ConvertBufferType(buffer, &clipboard_type)) |
| 117 return WebData(); | 117 return WebData(); |
| 118 | 118 |
| 119 std::string png_data; | 119 std::string png_data; |
| 120 delegate_->ReadImage(clipboard_type, &png_data); | 120 client_->ReadImage(clipboard_type, &png_data); |
| 121 return WebData(png_data); | 121 return WebData(png_data); |
| 122 } | 122 } |
| 123 | 123 |
| 124 WebString WebClipboardImpl::readCustomData(Buffer buffer, | 124 WebString WebClipboardImpl::readCustomData(Buffer buffer, |
| 125 const WebString& type) { | 125 const WebString& type) { |
| 126 ui::ClipboardType clipboard_type; | 126 ui::ClipboardType clipboard_type; |
| 127 if (!ConvertBufferType(buffer, &clipboard_type)) | 127 if (!ConvertBufferType(buffer, &clipboard_type)) |
| 128 return WebString(); | 128 return WebString(); |
| 129 | 129 |
| 130 base::string16 data; | 130 base::string16 data; |
| 131 delegate_->ReadCustomData(clipboard_type, type, &data); | 131 client_->ReadCustomData(clipboard_type, type, &data); |
| 132 return data; | 132 return data; |
| 133 } | 133 } |
| 134 | 134 |
| 135 void WebClipboardImpl::writePlainText(const WebString& plain_text) { | 135 void WebClipboardImpl::writePlainText(const WebString& plain_text) { |
| 136 delegate_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, plain_text); | 136 ScopedClipboardWriterGlue scw(client_); |
| 137 delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE); | 137 scw.WriteText(plain_text); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void WebClipboardImpl::writeHTML( | 140 void WebClipboardImpl::writeHTML( |
| 141 const WebString& html_text, const WebURL& source_url, | 141 const WebString& html_text, const WebURL& source_url, |
| 142 const WebString& plain_text, bool write_smart_paste) { | 142 const WebString& plain_text, bool write_smart_paste) { |
| 143 delegate_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE, html_text, source_url); | 143 ScopedClipboardWriterGlue scw(client_); |
| 144 delegate_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, plain_text); | 144 scw.WriteHTML(html_text, source_url.spec()); |
| 145 scw.WriteText(plain_text); |
| 145 | 146 |
| 146 if (write_smart_paste) | 147 if (write_smart_paste) |
| 147 delegate_->WriteSmartPasteMarker(ui::CLIPBOARD_TYPE_COPY_PASTE); | 148 scw.WriteWebSmartPaste(); |
| 148 delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE); | |
| 149 } | 149 } |
| 150 | 150 |
| 151 void WebClipboardImpl::writeImage(const WebImage& image, | 151 void WebClipboardImpl::writeImage(const WebImage& image, |
| 152 const WebURL& url, | 152 const WebURL& url, |
| 153 const WebString& title) { | 153 const WebString& title) { |
| 154 DCHECK(!image.isNull()); | 154 ScopedClipboardWriterGlue scw(client_); |
| 155 const SkBitmap& bitmap = image.getSkBitmap(); | 155 |
| 156 if (!delegate_->WriteImage(ui::CLIPBOARD_TYPE_COPY_PASTE, bitmap)) | 156 if (!image.isNull()) { |
| 157 return; | 157 const SkBitmap& bitmap = image.getSkBitmap(); |
| 158 // WriteBitmapFromPixels expects 32-bit data. |
| 159 DCHECK_EQ(bitmap.colorType(), kN32_SkColorType); |
| 160 |
| 161 SkAutoLockPixels locked(bitmap); |
| 162 void *pixels = bitmap.getPixels(); |
| 163 // TODO(piman): this should not be NULL, but it is. crbug.com/369621 |
| 164 if (!pixels) |
| 165 return; |
| 166 scw.WriteBitmapFromPixels(pixels, image.size()); |
| 167 } |
| 158 | 168 |
| 159 if (!url.isEmpty()) { | 169 if (!url.isEmpty()) { |
| 160 delegate_->WriteBookmark(ui::CLIPBOARD_TYPE_COPY_PASTE, url, title); | 170 scw.WriteBookmark(title, url.spec()); |
| 161 #if !defined(OS_MACOSX) | 171 #if !defined(OS_MACOSX) |
| 162 // When writing the image, we also write the image markup so that pasting | 172 // When writing the image, we also write the image markup so that pasting |
| 163 // into rich text editors, such as Gmail, reveals the image. We also don't | 173 // into rich text editors, such as Gmail, reveals the image. We also don't |
| 164 // want to call writeText(), since some applications (WordPad) don't pick | 174 // want to call writeText(), since some applications (WordPad) don't pick |
| 165 // the image if there is also a text format on the clipboard. | 175 // the image if there is also a text format on the clipboard. |
| 166 // We also don't want to write HTML on a Mac, since Mail.app prefers to use | 176 // We also don't want to write HTML on a Mac, since Mail.app prefers to use |
| 167 // the image markup over attaching the actual image. See | 177 // the image markup over attaching the actual image. See |
| 168 // http://crbug.com/33016 for details. | 178 // http://crbug.com/33016 for details. |
| 169 delegate_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE, | 179 scw.WriteHTML(base::UTF8ToUTF16(URLToImageMarkup(url, title)), |
| 170 base::UTF8ToUTF16(URLToImageMarkup(url, title)), | 180 std::string()); |
| 171 GURL()); | |
| 172 #endif | 181 #endif |
| 173 } | 182 } |
| 174 delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE); | |
| 175 } | 183 } |
| 176 | 184 |
| 177 void WebClipboardImpl::writeDataObject(const WebDragData& data) { | 185 void WebClipboardImpl::writeDataObject(const WebDragData& data) { |
| 186 ScopedClipboardWriterGlue scw(client_); |
| 187 |
| 178 const DropData& data_object = DropDataBuilder::Build(data); | 188 const DropData& data_object = DropDataBuilder::Build(data); |
| 179 // TODO(dcheng): Properly support text/uri-list here. | 189 // TODO(dcheng): Properly support text/uri-list here. |
| 180 // Avoid calling the WriteFoo functions if there is no data associated with a | |
| 181 // type. This prevents stomping on clipboard contents that might have been | |
| 182 // written by extension functions such as chrome.bookmarkManagerPrivate.copy. | |
| 183 if (!data_object.text.is_null()) | 190 if (!data_object.text.is_null()) |
| 184 delegate_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, | 191 scw.WriteText(data_object.text.string()); |
| 185 data_object.text.string()); | |
| 186 if (!data_object.html.is_null()) | 192 if (!data_object.html.is_null()) |
| 187 delegate_->WriteHTML( | 193 scw.WriteHTML(data_object.html.string(), std::string()); |
| 188 ui::CLIPBOARD_TYPE_COPY_PASTE, data_object.html.string(), GURL()); | 194 // If there is no custom data, avoid calling WritePickledData. This ensures |
| 189 if (!data_object.custom_data.empty()) | 195 // that ScopedClipboardWriterGlue's dtor remains a no-op if the page didn't |
| 190 delegate_->WriteCustomData(ui::CLIPBOARD_TYPE_COPY_PASTE, | 196 // modify the DataTransfer object, which is important to avoid stomping on |
| 191 data_object.custom_data); | 197 // any clipboard contents written by extension functions such as |
| 192 delegate_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE); | 198 // chrome.bookmarkManagerPrivate.copy. |
| 199 if (!data_object.custom_data.empty()) { |
| 200 Pickle pickle; |
| 201 ui::WriteCustomDataToPickle(data_object.custom_data, &pickle); |
| 202 scw.WritePickledData(pickle, ui::Clipboard::GetWebCustomDataFormatType()); |
| 203 } |
| 193 } | 204 } |
| 194 | 205 |
| 195 bool WebClipboardImpl::ConvertBufferType(Buffer buffer, | 206 bool WebClipboardImpl::ConvertBufferType(Buffer buffer, |
| 196 ui::ClipboardType* result) { | 207 ui::ClipboardType* result) { |
| 197 *result = ui::CLIPBOARD_TYPE_COPY_PASTE; | 208 *result = ui::CLIPBOARD_TYPE_COPY_PASTE; |
| 198 switch (buffer) { | 209 switch (buffer) { |
| 199 case BufferStandard: | 210 case BufferStandard: |
| 200 break; | 211 break; |
| 201 case BufferSelection: | 212 case BufferSelection: |
| 202 #if defined(USE_X11) && !defined(OS_CHROMEOS) | 213 #if defined(USE_X11) && !defined(OS_CHROMEOS) |
| 203 *result = ui::CLIPBOARD_TYPE_SELECTION; | 214 *result = ui::CLIPBOARD_TYPE_SELECTION; |
| 204 break; | 215 break; |
| 205 #else | 216 #else |
| 206 // Chrome OS and non-X11 unix builds do not support | 217 // Chrome OS and non-X11 unix builds do not support |
| 207 // the X selection clipboad. | 218 // the X selection clipboad. |
| 208 // TODO: remove the need for this case, see http://crbug.com/361753 | 219 // TODO: remove the need for this case, see http://crbug.com/361753 |
| 209 return false; | 220 return false; |
| 210 #endif | 221 #endif |
| 211 default: | 222 default: |
| 212 NOTREACHED(); | 223 NOTREACHED(); |
| 213 return false; | 224 return false; |
| 214 } | 225 } |
| 215 return true; | 226 return true; |
| 216 } | 227 } |
| 217 | 228 |
| 218 } // namespace content | 229 } // namespace content |
| OLD | NEW |