Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(432)

Side by Side Diff: ui/base/clipboard/clipboard_linux.cc

Issue 7977025: aura: Make 'ui' buildable without gtk. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/base/clipboard/clipboard_gtk.cc ('k') | ui/base/clipboard/clipboard_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // 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.
4
5 #include "ui/base/clipboard/clipboard.h"
6
7 #include <gtk/gtk.h>
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <utility>
12
13 #include "base/file_path.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/utf_string_conversions.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/gfx/canvas_skia.h"
19 #include "ui/gfx/gtk_util.h"
20 #include "ui/gfx/size.h"
21
22 namespace ui {
23
24 namespace {
25
26 const char kMimeTypeBitmap[] = "image/bmp";
27 const char kMimeTypeMozillaURL[] = "text/x-moz-url";
28 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste";
29
30 std::string GdkAtomToString(const GdkAtom& atom) {
31 gchar* name = gdk_atom_name(atom);
32 std::string rv(name);
33 g_free(name);
34 return rv;
35 }
36
37 GdkAtom StringToGdkAtom(const std::string& str) {
38 return gdk_atom_intern(str.c_str(), FALSE);
39 }
40
41 // GtkClipboardGetFunc callback.
42 // GTK will call this when an application wants data we copied to the clipboard.
43 void GetData(GtkClipboard* clipboard,
44 GtkSelectionData* selection_data,
45 guint info,
46 gpointer user_data) {
47 Clipboard::TargetMap* data_map =
48 reinterpret_cast<Clipboard::TargetMap*>(user_data);
49
50 std::string target_string = GdkAtomToString(selection_data->target);
51 Clipboard::TargetMap::iterator iter = data_map->find(target_string);
52
53 if (iter == data_map->end())
54 return;
55
56 if (target_string == kMimeTypeBitmap) {
57 gtk_selection_data_set_pixbuf(selection_data,
58 reinterpret_cast<GdkPixbuf*>(iter->second.first));
59 } else {
60 gtk_selection_data_set(selection_data, selection_data->target, 8,
61 reinterpret_cast<guchar*>(iter->second.first),
62 iter->second.second);
63 }
64 }
65
66 // GtkClipboardClearFunc callback.
67 // We are guaranteed this will be called exactly once for each call to
68 // gtk_clipboard_set_with_data.
69 void ClearData(GtkClipboard* clipboard,
70 gpointer user_data) {
71 Clipboard::TargetMap* map =
72 reinterpret_cast<Clipboard::TargetMap*>(user_data);
73 // The same data may be inserted under multiple keys, so use a set to
74 // uniq them.
75 std::set<char*> ptrs;
76
77 for (Clipboard::TargetMap::iterator iter = map->begin();
78 iter != map->end(); ++iter) {
79 if (iter->first == kMimeTypeBitmap)
80 g_object_unref(reinterpret_cast<GdkPixbuf*>(iter->second.first));
81 else
82 ptrs.insert(iter->second.first);
83 }
84
85 for (std::set<char*>::iterator iter = ptrs.begin();
86 iter != ptrs.end(); ++iter) {
87 delete[] *iter;
88 }
89
90 delete map;
91 }
92
93 // Called on GdkPixbuf destruction; see WriteBitmap().
94 void GdkPixbufFree(guchar* pixels, gpointer data) {
95 free(pixels);
96 }
97
98 } // namespace
99
100 Clipboard::Clipboard() : clipboard_data_(NULL) {
101 #if !defined(USE_AURA)
102 clipboard_ = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
103 primary_selection_ = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
104 #endif
105 }
106
107 Clipboard::~Clipboard() {
108 gtk_clipboard_store(clipboard_);
109 }
110
111 void Clipboard::WriteObjects(const ObjectMap& objects) {
112 clipboard_data_ = new TargetMap();
113
114 for (ObjectMap::const_iterator iter = objects.begin();
115 iter != objects.end(); ++iter) {
116 DispatchObject(static_cast<ObjectType>(iter->first), iter->second);
117 }
118
119 SetGtkClipboard();
120 }
121
122 // When a URL is copied from a render view context menu (via "copy link
123 // location", for example), we additionally stick it in the X clipboard. This
124 // matches other linux browsers.
125 void Clipboard::DidWriteURL(const std::string& utf8_text) {
126 gtk_clipboard_set_text(primary_selection_, utf8_text.c_str(),
127 utf8_text.length());
128 }
129
130 // Take ownership of the GTK clipboard and inform it of the targets we support.
131 void Clipboard::SetGtkClipboard() {
132 scoped_array<GtkTargetEntry> targets(
133 new GtkTargetEntry[clipboard_data_->size()]);
134
135 int i = 0;
136 for (Clipboard::TargetMap::iterator iter = clipboard_data_->begin();
137 iter != clipboard_data_->end(); ++iter, ++i) {
138 targets[i].target = const_cast<char*>(iter->first.c_str());
139 targets[i].flags = 0;
140 targets[i].info = 0;
141 }
142
143 if (gtk_clipboard_set_with_data(clipboard_, targets.get(),
144 clipboard_data_->size(),
145 GetData, ClearData,
146 clipboard_data_)) {
147 gtk_clipboard_set_can_store(clipboard_,
148 targets.get(),
149 clipboard_data_->size());
150 }
151
152 // clipboard_data_ now owned by the GtkClipboard.
153 clipboard_data_ = NULL;
154 }
155
156 void Clipboard::WriteText(const char* text_data, size_t text_len) {
157 char* data = new char[text_len];
158 memcpy(data, text_data, text_len);
159
160 InsertMapping(kMimeTypeText, data, text_len);
161 InsertMapping("TEXT", data, text_len);
162 InsertMapping("STRING", data, text_len);
163 InsertMapping("UTF8_STRING", data, text_len);
164 InsertMapping("COMPOUND_TEXT", data, text_len);
165 }
166
167 void Clipboard::WriteHTML(const char* markup_data,
168 size_t markup_len,
169 const char* url_data,
170 size_t url_len) {
171 // TODO(estade): We need to expand relative links with |url_data|.
172 static const char* html_prefix = "<meta http-equiv=\"content-type\" "
173 "content=\"text/html; charset=utf-8\">";
174 size_t html_prefix_len = strlen(html_prefix);
175 size_t total_len = html_prefix_len + markup_len + 1;
176
177 char* data = new char[total_len];
178 snprintf(data, total_len, "%s", html_prefix);
179 memcpy(data + html_prefix_len, markup_data, markup_len);
180 // Some programs expect NULL-terminated data. See http://crbug.com/42624
181 data[total_len - 1] = '\0';
182
183 InsertMapping(kMimeTypeHTML, data, total_len);
184 }
185
186 // Write an extra flavor that signifies WebKit was the last to modify the
187 // pasteboard. This flavor has no data.
188 void Clipboard::WriteWebSmartPaste() {
189 InsertMapping(kMimeTypeWebkitSmartPaste, NULL, 0);
190 }
191
192 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) {
193 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data);
194
195 guchar* data =
196 gfx::BGRAToRGBA(reinterpret_cast<const uint8_t*>(pixel_data),
197 size->width(), size->height(), 0);
198
199 GdkPixbuf* pixbuf =
200 gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
201 8, size->width(), size->height(),
202 size->width() * 4, GdkPixbufFree, NULL);
203 // We store the GdkPixbuf*, and the size_t half of the pair is meaningless.
204 // Note that this contrasts with the vast majority of entries in our target
205 // map, which directly store the data and its length.
206 InsertMapping(kMimeTypeBitmap, reinterpret_cast<char*>(pixbuf), 0);
207 }
208
209 void Clipboard::WriteBookmark(const char* title_data, size_t title_len,
210 const char* url_data, size_t url_len) {
211 // Write as a mozilla url (UTF16: URL, newline, title).
212 string16 url = UTF8ToUTF16(std::string(url_data, url_len) + "\n");
213 string16 title = UTF8ToUTF16(std::string(title_data, title_len));
214 int data_len = 2 * (title.length() + url.length());
215
216 char* data = new char[data_len];
217 memcpy(data, url.data(), 2 * url.length());
218 memcpy(data + 2 * url.length(), title.data(), 2 * title.length());
219 InsertMapping(kMimeTypeMozillaURL, data, data_len);
220 }
221
222 void Clipboard::WriteData(const char* format_name, size_t format_len,
223 const char* data_data, size_t data_len) {
224 std::string format(format_name, format_len);
225 // We assume that certain mapping types are only written by trusted code.
226 // Therefore we must upkeep their integrity.
227 if (format == kMimeTypeBitmap)
228 return;
229 char* data = new char[data_len];
230 memcpy(data, data_data, data_len);
231 InsertMapping(format.c_str(), data, data_len);
232 }
233
234 // We do not use gtk_clipboard_wait_is_target_available because of
235 // a bug with the gtk clipboard. It caches the available targets
236 // and does not always refresh the cache when it is appropriate.
237 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format,
238 Clipboard::Buffer buffer) const {
239 GtkClipboard* clipboard = LookupBackingClipboard(buffer);
240 if (clipboard == NULL)
241 return false;
242
243 bool format_is_plain_text = GetPlainTextFormatType() == format;
244 if (format_is_plain_text) {
245 // This tries a number of common text targets.
246 if (gtk_clipboard_wait_is_text_available(clipboard))
247 return true;
248 }
249
250 bool retval = false;
251 GdkAtom* targets = NULL;
252 GtkSelectionData* data =
253 gtk_clipboard_wait_for_contents(clipboard,
254 gdk_atom_intern("TARGETS", false));
255
256 if (!data)
257 return false;
258
259 int num = 0;
260 gtk_selection_data_get_targets(data, &targets, &num);
261
262 // Some programs post data to the clipboard without any targets. If this is
263 // the case we attempt to make sense of the contents as text. This is pretty
264 // unfortunate since it means we have to actually copy the data to see if it
265 // is available, but at least this path shouldn't be hit for conforming
266 // programs.
267 if (num <= 0) {
268 if (format_is_plain_text) {
269 gchar* text = gtk_clipboard_wait_for_text(clipboard);
270 if (text) {
271 g_free(text);
272 retval = true;
273 }
274 }
275 }
276
277 GdkAtom format_atom = StringToGdkAtom(format);
278
279 for (int i = 0; i < num; i++) {
280 if (targets[i] == format_atom) {
281 retval = true;
282 break;
283 }
284 }
285
286 g_free(targets);
287 gtk_selection_data_free(data);
288
289 return retval;
290 }
291
292 bool Clipboard::IsFormatAvailableByString(const std::string& format,
293 Clipboard::Buffer buffer) const {
294 return IsFormatAvailable(format, buffer);
295 }
296
297 void Clipboard::ReadAvailableTypes(Clipboard::Buffer buffer,
298 std::vector<string16>* types,
299 bool* contains_filenames) const {
300 if (!types || !contains_filenames) {
301 NOTREACHED();
302 return;
303 }
304
305 types->clear();
306 if (IsFormatAvailable(GetPlainTextFormatType(), buffer))
307 types->push_back(UTF8ToUTF16(kMimeTypeText));
308 if (IsFormatAvailable(GetHtmlFormatType(), buffer))
309 types->push_back(UTF8ToUTF16(kMimeTypeHTML));
310 if (IsFormatAvailable(GetBitmapFormatType(), buffer))
311 types->push_back(UTF8ToUTF16(kMimeTypePNG));
312 *contains_filenames = false;
313 }
314
315
316 void Clipboard::ReadText(Clipboard::Buffer buffer, string16* result) const {
317 GtkClipboard* clipboard = LookupBackingClipboard(buffer);
318 if (clipboard == NULL)
319 return;
320
321 result->clear();
322 gchar* text = gtk_clipboard_wait_for_text(clipboard);
323
324 if (text == NULL)
325 return;
326
327 // TODO(estade): do we want to handle the possible error here?
328 UTF8ToUTF16(text, strlen(text), result);
329 g_free(text);
330 }
331
332 void Clipboard::ReadAsciiText(Clipboard::Buffer buffer,
333 std::string* result) const {
334 GtkClipboard* clipboard = LookupBackingClipboard(buffer);
335 if (clipboard == NULL)
336 return;
337
338 result->clear();
339 gchar* text = gtk_clipboard_wait_for_text(clipboard);
340
341 if (text == NULL)
342 return;
343
344 result->assign(text);
345 g_free(text);
346 }
347
348 void Clipboard::ReadFile(FilePath* file) const {
349 *file = FilePath();
350 }
351
352 // TODO(estade): handle different charsets.
353 // TODO(port): set *src_url.
354 void Clipboard::ReadHTML(Clipboard::Buffer buffer, string16* markup,
355 std::string* src_url) const {
356 GtkClipboard* clipboard = LookupBackingClipboard(buffer);
357 if (clipboard == NULL)
358 return;
359 markup->clear();
360
361 GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard,
362 StringToGdkAtom(GetHtmlFormatType()));
363
364 if (!data)
365 return;
366
367 // If the data starts with 0xFEFF, i.e., Byte Order Mark, assume it is
368 // UTF-16, otherwise assume UTF-8.
369 if (data->length >= 2 &&
370 reinterpret_cast<uint16_t*>(data->data)[0] == 0xFEFF) {
371 markup->assign(reinterpret_cast<uint16_t*>(data->data) + 1,
372 (data->length / 2) - 1);
373 } else {
374 UTF8ToUTF16(reinterpret_cast<char*>(data->data), data->length, markup);
375 }
376
377 // If there is a terminating NULL, drop it.
378 if (!markup->empty() && markup->at(markup->length() - 1) == '\0')
379 markup->resize(markup->length() - 1);
380
381 gtk_selection_data_free(data);
382 }
383
384 SkBitmap Clipboard::ReadImage(Buffer buffer) const {
385 ScopedGObject<GdkPixbuf>::Type pixbuf(
386 gtk_clipboard_wait_for_image(clipboard_));
387 if (!pixbuf.get())
388 return SkBitmap();
389
390 gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf.get()),
391 gdk_pixbuf_get_height(pixbuf.get()),
392 false);
393 {
394 skia::ScopedPlatformPaint scoped_platform_paint(&canvas);
395 cairo_t* context = scoped_platform_paint.GetPlatformSurface();
396 gdk_cairo_set_source_pixbuf(context, pixbuf.get(), 0.0, 0.0);
397 cairo_paint(context);
398 }
399 return canvas.ExtractBitmap();
400 }
401
402 void Clipboard::ReadBookmark(string16* title, std::string* url) const {
403 // TODO(estade): implement this.
404 NOTIMPLEMENTED();
405 }
406
407 void Clipboard::ReadData(const std::string& format, std::string* result) {
408 GtkSelectionData* data =
409 gtk_clipboard_wait_for_contents(clipboard_, StringToGdkAtom(format));
410 if (!data)
411 return;
412 result->assign(reinterpret_cast<char*>(data->data), data->length);
413 gtk_selection_data_free(data);
414 }
415
416 uint64 Clipboard::GetSequenceNumber() {
417 // TODO(cdn): implement this. For now this interface will advertise
418 // that the Linux clipboard never changes. That's fine as long as we
419 // don't rely on this signal.
420 return 0;
421 }
422
423 // static
424 Clipboard::FormatType Clipboard::GetPlainTextFormatType() {
425 return GdkAtomToString(GDK_TARGET_STRING);
426 }
427
428 // static
429 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() {
430 return GetPlainTextFormatType();
431 }
432
433 // static
434 Clipboard::FormatType Clipboard::GetHtmlFormatType() {
435 return std::string(kMimeTypeHTML);
436 }
437
438 // static
439 Clipboard::FormatType Clipboard::GetBitmapFormatType() {
440 return std::string(kMimeTypeBitmap);
441 }
442
443 // static
444 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() {
445 return std::string(kMimeTypeWebkitSmartPaste);
446 }
447
448 void Clipboard::InsertMapping(const char* key,
449 char* data,
450 size_t data_len) {
451 DCHECK(clipboard_data_->find(key) == clipboard_data_->end());
452 (*clipboard_data_)[key] = std::make_pair(data, data_len);
453 }
454
455 GtkClipboard* Clipboard::LookupBackingClipboard(Buffer clipboard) const {
456 switch (clipboard) {
457 case BUFFER_STANDARD:
458 return clipboard_;
459 case BUFFER_SELECTION:
460 return primary_selection_;
461 default:
462 NOTREACHED();
463 return NULL;
464 }
465 }
466
467 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/clipboard/clipboard_gtk.cc ('k') | ui/base/clipboard/clipboard_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698