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
3 // found in the LICENSE file.
5 #include "content/renderer/webclipboard_impl.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/common/clipboard_format.h"
11 #include "content/public/common/drop_data.h"
12 #include "content/renderer/clipboard_utils.h"
13 #include "content/renderer/drop_data_builder.h"
14 #include "content/renderer/renderer_clipboard_delegate.h"
15 #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"
24 using blink::WebClipboard
;
26 using blink::WebDragData
;
27 using blink::WebImage
;
28 using blink::WebString
;
30 using blink::WebVector
;
34 WebClipboardImpl::WebClipboardImpl(RendererClipboardDelegate
* delegate
)
35 : delegate_(delegate
) {
39 WebClipboardImpl::~WebClipboardImpl() {
42 uint64
WebClipboardImpl::sequenceNumber(Buffer buffer
) {
43 ui::ClipboardType clipboard_type
;
44 if (!ConvertBufferType(buffer
, &clipboard_type
))
47 return delegate_
->GetSequenceNumber(clipboard_type
);
50 bool WebClipboardImpl::isFormatAvailable(Format format
, Buffer buffer
) {
51 ui::ClipboardType clipboard_type
= ui::CLIPBOARD_TYPE_COPY_PASTE
;
53 if (!ConvertBufferType(buffer
, &clipboard_type
))
58 return delegate_
->IsFormatAvailable(CLIPBOARD_FORMAT_PLAINTEXT
,
61 return delegate_
->IsFormatAvailable(CLIPBOARD_FORMAT_HTML
,
63 case FormatSmartPaste
:
64 return delegate_
->IsFormatAvailable(CLIPBOARD_FORMAT_SMART_PASTE
,
67 return delegate_
->IsFormatAvailable(CLIPBOARD_FORMAT_BOOKMARK
,
76 WebVector
<WebString
> WebClipboardImpl::readAvailableTypes(
77 Buffer buffer
, bool* contains_filenames
) {
78 ui::ClipboardType clipboard_type
;
79 std::vector
<base::string16
> types
;
80 if (ConvertBufferType(buffer
, &clipboard_type
)) {
81 delegate_
->ReadAvailableTypes(clipboard_type
, &types
, contains_filenames
);
86 WebString
WebClipboardImpl::readPlainText(Buffer buffer
) {
87 ui::ClipboardType clipboard_type
;
88 if (!ConvertBufferType(buffer
, &clipboard_type
))
92 delegate_
->ReadText(clipboard_type
, &text
);
96 WebString
WebClipboardImpl::readHTML(Buffer buffer
, WebURL
* source_url
,
97 unsigned* fragment_start
,
98 unsigned* fragment_end
) {
99 ui::ClipboardType clipboard_type
;
100 if (!ConvertBufferType(buffer
, &clipboard_type
))
103 base::string16 html_stdstr
;
105 delegate_
->ReadHTML(clipboard_type
, &html_stdstr
, &gurl
,
106 static_cast<uint32
*>(fragment_start
),
107 static_cast<uint32
*>(fragment_end
));
112 WebData
WebClipboardImpl::readImage(Buffer buffer
) {
113 ui::ClipboardType clipboard_type
;
114 if (!ConvertBufferType(buffer
, &clipboard_type
))
117 std::string png_data
;
118 delegate_
->ReadImage(clipboard_type
, &png_data
);
119 return WebData(png_data
);
122 WebString
WebClipboardImpl::readCustomData(Buffer buffer
,
123 const WebString
& type
) {
124 ui::ClipboardType clipboard_type
;
125 if (!ConvertBufferType(buffer
, &clipboard_type
))
129 delegate_
->ReadCustomData(clipboard_type
, type
, &data
);
133 void WebClipboardImpl::writePlainText(const WebString
& plain_text
) {
134 delegate_
->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE
, plain_text
);
135 delegate_
->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE
);
138 void WebClipboardImpl::writeHTML(
139 const WebString
& html_text
, const WebURL
& source_url
,
140 const WebString
& plain_text
, bool write_smart_paste
) {
141 delegate_
->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE
, html_text
, source_url
);
142 delegate_
->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE
, plain_text
);
144 if (write_smart_paste
)
145 delegate_
->WriteSmartPasteMarker(ui::CLIPBOARD_TYPE_COPY_PASTE
);
146 delegate_
->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE
);
149 void WebClipboardImpl::writeImage(const WebImage
& image
,
151 const WebString
& title
) {
152 DCHECK(!image
.isNull());
153 const SkBitmap
& bitmap
= image
.getSkBitmap();
154 if (!delegate_
->WriteImage(ui::CLIPBOARD_TYPE_COPY_PASTE
, bitmap
))
157 if (!url
.isEmpty()) {
158 delegate_
->WriteBookmark(ui::CLIPBOARD_TYPE_COPY_PASTE
, url
, title
);
159 #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.
167 delegate_
->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE
,
168 base::UTF8ToUTF16(URLToImageMarkup(url
, title
)),
172 delegate_
->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE
);
175 void WebClipboardImpl::writeDataObject(const WebDragData
& data
) {
176 const DropData
& data_object
= DropDataBuilder::Build(data
);
177 // TODO(dcheng): Properly support text/uri-list here.
178 // 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.
181 if (!data_object
.text
.is_null())
182 delegate_
->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE
,
183 data_object
.text
.string());
184 if (!data_object
.html
.is_null())
185 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
);
193 bool WebClipboardImpl::ConvertBufferType(Buffer buffer
,
194 ui::ClipboardType
* result
) {
195 *result
= ui::CLIPBOARD_TYPE_COPY_PASTE
;
199 case BufferSelection
:
200 #if defined(USE_X11) && !defined(OS_CHROMEOS)
201 *result
= ui::CLIPBOARD_TYPE_SELECTION
;
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
216 } // namespace content