Use LayerTreeHostImpl id instead of its address as id in trace events
[chromium-blink-merge.git] / content / renderer / webclipboard_impl.cc
blob317ec7d283d707849e2829b8035760fd2530d4b7
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/pickle.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/common/clipboard_format.h"
12 #include "content/public/common/drop_data.h"
13 #include "content/renderer/clipboard_utils.h"
14 #include "content/renderer/drop_data_builder.h"
15 #include "content/renderer/scoped_clipboard_writer_glue.h"
16 #include "third_party/WebKit/public/platform/WebData.h"
17 #include "third_party/WebKit/public/platform/WebDragData.h"
18 #include "third_party/WebKit/public/platform/WebImage.h"
19 #include "third_party/WebKit/public/platform/WebSize.h"
20 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebURL.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"
26 #include "url/gurl.h"
28 using blink::WebClipboard;
29 using blink::WebData;
30 using blink::WebDragData;
31 using blink::WebImage;
32 using blink::WebString;
33 using blink::WebURL;
34 using blink::WebVector;
36 namespace content {
38 WebClipboardImpl::WebClipboardImpl(ClipboardClient* client)
39 : client_(client) {
42 WebClipboardImpl::~WebClipboardImpl() {
45 uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) {
46 ui::ClipboardType clipboard_type;
47 if (!ConvertBufferType(buffer, &clipboard_type))
48 return 0;
50 return client_->GetSequenceNumber(clipboard_type);
53 bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
54 ui::ClipboardType clipboard_type = ui::CLIPBOARD_TYPE_COPY_PASTE;
56 if (!ConvertBufferType(buffer, &clipboard_type))
57 return false;
59 switch (format) {
60 case FormatPlainText:
61 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_PLAINTEXT,
62 clipboard_type);
63 case FormatHTML:
64 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_HTML, clipboard_type);
65 case FormatSmartPaste:
66 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_SMART_PASTE,
67 clipboard_type);
68 case FormatBookmark:
69 return client_->IsFormatAvailable(CLIPBOARD_FORMAT_BOOKMARK,
70 clipboard_type);
71 default:
72 NOTREACHED();
75 return false;
78 WebVector<WebString> WebClipboardImpl::readAvailableTypes(
79 Buffer buffer, bool* contains_filenames) {
80 ui::ClipboardType clipboard_type;
81 std::vector<base::string16> types;
82 if (ConvertBufferType(buffer, &clipboard_type)) {
83 client_->ReadAvailableTypes(clipboard_type, &types, contains_filenames);
85 return types;
88 WebString WebClipboardImpl::readPlainText(Buffer buffer) {
89 ui::ClipboardType clipboard_type;
90 if (!ConvertBufferType(buffer, &clipboard_type))
91 return WebString();
93 base::string16 text;
94 client_->ReadText(clipboard_type, &text);
95 return text;
98 WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url,
99 unsigned* fragment_start,
100 unsigned* fragment_end) {
101 ui::ClipboardType clipboard_type;
102 if (!ConvertBufferType(buffer, &clipboard_type))
103 return WebString();
105 base::string16 html_stdstr;
106 GURL gurl;
107 client_->ReadHTML(clipboard_type, &html_stdstr, &gurl,
108 static_cast<uint32*>(fragment_start),
109 static_cast<uint32*>(fragment_end));
110 *source_url = gurl;
111 return html_stdstr;
114 WebData WebClipboardImpl::readImage(Buffer buffer) {
115 ui::ClipboardType clipboard_type;
116 if (!ConvertBufferType(buffer, &clipboard_type))
117 return WebData();
119 std::string png_data;
120 client_->ReadImage(clipboard_type, &png_data);
121 return WebData(png_data);
124 WebString WebClipboardImpl::readCustomData(Buffer buffer,
125 const WebString& type) {
126 ui::ClipboardType clipboard_type;
127 if (!ConvertBufferType(buffer, &clipboard_type))
128 return WebString();
130 base::string16 data;
131 client_->ReadCustomData(clipboard_type, type, &data);
132 return data;
135 void WebClipboardImpl::writePlainText(const WebString& plain_text) {
136 ScopedClipboardWriterGlue scw(client_);
137 scw.WriteText(plain_text);
140 void WebClipboardImpl::writeHTML(
141 const WebString& html_text, const WebURL& source_url,
142 const WebString& plain_text, bool write_smart_paste) {
143 ScopedClipboardWriterGlue scw(client_);
144 scw.WriteHTML(html_text, source_url.spec());
145 scw.WriteText(plain_text);
147 if (write_smart_paste)
148 scw.WriteWebSmartPaste();
151 void WebClipboardImpl::writeImage(const WebImage& image,
152 const WebURL& url,
153 const WebString& title) {
154 ScopedClipboardWriterGlue scw(client_);
156 if (!image.isNull()) {
157 const SkBitmap& bitmap = image.getSkBitmap();
158 SkAutoLockPixels locked(bitmap);
159 scw.WriteBitmapFromPixels(bitmap.getPixels(), image.size());
162 if (!url.isEmpty()) {
163 scw.WriteBookmark(title, url.spec());
164 #if !defined(OS_MACOSX)
165 // 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
168 // the image if there is also a text format on the clipboard.
169 // We also don't want to write HTML on a Mac, since Mail.app prefers to use
170 // the image markup over attaching the actual image. See
171 // http://crbug.com/33016 for details.
172 scw.WriteHTML(base::UTF8ToUTF16(URLToImageMarkup(url, title)),
173 std::string());
174 #endif
178 void WebClipboardImpl::writeDataObject(const WebDragData& data) {
179 ScopedClipboardWriterGlue scw(client_);
181 const DropData& data_object = DropDataBuilder::Build(data);
182 // TODO(dcheng): Properly support text/uri-list here.
183 if (!data_object.text.is_null())
184 scw.WriteText(data_object.text.string());
185 if (!data_object.html.is_null())
186 scw.WriteHTML(data_object.html.string(), std::string());
187 // If there is no custom data, avoid calling WritePickledData. This ensures
188 // that ScopedClipboardWriterGlue's dtor remains a no-op if the page didn't
189 // modify the DataTransfer object, which is important to avoid stomping on
190 // any clipboard contents written by extension functions such as
191 // chrome.bookmarkManagerPrivate.copy.
192 if (!data_object.custom_data.empty()) {
193 Pickle pickle;
194 ui::WriteCustomDataToPickle(data_object.custom_data, &pickle);
195 scw.WritePickledData(pickle, ui::Clipboard::GetWebCustomDataFormatType());
199 bool WebClipboardImpl::ConvertBufferType(Buffer buffer,
200 ui::ClipboardType* result) {
201 *result = ui::CLIPBOARD_TYPE_COPY_PASTE;
202 switch (buffer) {
203 case BufferStandard:
204 break;
205 case BufferSelection:
206 #if defined(USE_X11) && !defined(OS_CHROMEOS)
207 *result = ui::CLIPBOARD_TYPE_SELECTION;
208 break;
209 #else
210 // Chrome OS and non-X11 unix builds do not support
211 // the X selection clipboad.
212 // TODO: remove the need for this case, see http://crbug.com/361753
213 return false;
214 #endif
215 default:
216 NOTREACHED();
217 return false;
219 return true;
222 } // namespace content