OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/base/clipboard/clipboard.h" | 5 #include "ui/base/clipboard/clipboard.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 #include <X11/extensions/Xfixes.h> | 8 #include <X11/extensions/Xfixes.h> |
9 #include <X11/Xatom.h> | 9 #include <X11/Xatom.h> |
10 #include <map> | 10 #include <map> |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 clipboard_sequence_number_++; | 99 clipboard_sequence_number_++; |
100 } else if (ev->selection == XA_PRIMARY) { | 100 } else if (ev->selection == XA_PRIMARY) { |
101 primary_sequence_number_++; | 101 primary_sequence_number_++; |
102 } else { | 102 } else { |
103 DLOG(ERROR) << "Unexpected selection atom: " << ev->selection; | 103 DLOG(ERROR) << "Unexpected selection atom: " << ev->selection; |
104 } | 104 } |
105 } | 105 } |
106 return GDK_FILTER_CONTINUE; | 106 return GDK_FILTER_CONTINUE; |
107 } | 107 } |
108 | 108 |
| 109 const char kSourceTagType[] = "org.chromium.source-tag"; |
109 const char kMimeTypeBitmap[] = "image/bmp"; | 110 const char kMimeTypeBitmap[] = "image/bmp"; |
110 const char kMimeTypeMozillaURL[] = "text/x-moz-url"; | 111 const char kMimeTypeMozillaURL[] = "text/x-moz-url"; |
111 const char kMimeTypePepperCustomData[] = "chromium/x-pepper-custom-data"; | 112 const char kMimeTypePepperCustomData[] = "chromium/x-pepper-custom-data"; |
112 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; | 113 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; |
113 | 114 |
114 std::string GdkAtomToString(const GdkAtom& atom) { | 115 std::string GdkAtomToString(const GdkAtom& atom) { |
115 gchar* name = gdk_atom_name(atom); | 116 gchar* name = gdk_atom_name(atom); |
116 std::string rv(name); | 117 std::string rv(name); |
117 g_free(name); | 118 g_free(name); |
118 return rv; | 119 return rv; |
(...skipping 26 matching lines...) Expand all Loading... |
145 gtk_selection_data_set(selection_data, | 146 gtk_selection_data_set(selection_data, |
146 gtk_selection_data_get_target(selection_data), 8, | 147 gtk_selection_data_get_target(selection_data), 8, |
147 reinterpret_cast<guchar*>(iter->second.first), | 148 reinterpret_cast<guchar*>(iter->second.first), |
148 iter->second.second); | 149 iter->second.second); |
149 } | 150 } |
150 } | 151 } |
151 | 152 |
152 // GtkClipboardClearFunc callback. | 153 // GtkClipboardClearFunc callback. |
153 // We are guaranteed this will be called exactly once for each call to | 154 // We are guaranteed this will be called exactly once for each call to |
154 // gtk_clipboard_set_with_data. | 155 // gtk_clipboard_set_with_data. |
155 void ClearData(GtkClipboard* clipboard, | 156 void ClearData(GtkClipboard* /*clipboard*/, |
156 gpointer user_data) { | 157 gpointer user_data) { |
157 Clipboard::TargetMap* map = | 158 Clipboard::TargetMap* map = |
158 reinterpret_cast<Clipboard::TargetMap*>(user_data); | 159 reinterpret_cast<Clipboard::TargetMap*>(user_data); |
159 // The same data may be inserted under multiple keys, so use a set to | 160 // The same data may be inserted under multiple keys, so use a set to |
160 // uniq them. | 161 // uniq them. |
161 std::set<char*> ptrs; | 162 std::set<char*> ptrs; |
162 | 163 |
163 for (Clipboard::TargetMap::iterator iter = map->begin(); | 164 for (Clipboard::TargetMap::iterator iter = map->begin(); |
164 iter != map->end(); ++iter) { | 165 iter != map->end(); ++iter) { |
165 if (iter->first == kMimeTypeBitmap) | 166 if (iter->first == kMimeTypeBitmap) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 DCHECK(CalledOnValidThread()); | 211 DCHECK(CalledOnValidThread()); |
211 clipboard_ = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); | 212 clipboard_ = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
212 primary_selection_ = gtk_clipboard_get(GDK_SELECTION_PRIMARY); | 213 primary_selection_ = gtk_clipboard_get(GDK_SELECTION_PRIMARY); |
213 } | 214 } |
214 | 215 |
215 Clipboard::~Clipboard() { | 216 Clipboard::~Clipboard() { |
216 DCHECK(CalledOnValidThread()); | 217 DCHECK(CalledOnValidThread()); |
217 gtk_clipboard_store(clipboard_); | 218 gtk_clipboard_store(clipboard_); |
218 } | 219 } |
219 | 220 |
220 void Clipboard::WriteObjects(Buffer buffer, const ObjectMap& objects) { | 221 void Clipboard::WriteObjectsImpl(Buffer buffer, |
| 222 const ObjectMap& objects, |
| 223 SourceTag tag) { |
221 DCHECK(CalledOnValidThread()); | 224 DCHECK(CalledOnValidThread()); |
222 clipboard_data_ = new TargetMap(); | 225 clipboard_data_ = new TargetMap(); |
223 | 226 |
224 for (ObjectMap::const_iterator iter = objects.begin(); | 227 for (ObjectMap::const_iterator iter = objects.begin(); |
225 iter != objects.end(); ++iter) { | 228 iter != objects.end(); ++iter) { |
226 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | 229 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
227 } | 230 } |
| 231 WriteSourceTag(tag); |
| 232 SetGtkClipboard(buffer); |
228 | 233 |
229 SetGtkClipboard(buffer); | 234 if (buffer == BUFFER_STANDARD) { |
| 235 ObjectMap::const_iterator text_iter = objects.find(CBF_TEXT); |
| 236 if (text_iter != objects.end()) { |
| 237 // Copy text and SourceTag to the selection clipboard. |
| 238 ObjectMap::const_iterator next_iter = text_iter; |
| 239 WriteObjectsImpl(BUFFER_SELECTION, |
| 240 ObjectMap(text_iter, ++next_iter), |
| 241 tag); |
| 242 } |
| 243 } |
230 } | 244 } |
231 | 245 |
232 // Take ownership of the GTK clipboard and inform it of the targets we support. | 246 // Take ownership of the GTK clipboard and inform it of the targets we support. |
233 void Clipboard::SetGtkClipboard(Buffer buffer) { | 247 void Clipboard::SetGtkClipboard(Buffer buffer) { |
234 scoped_ptr<GtkTargetEntry[]> targets( | 248 scoped_array<GtkTargetEntry> targets( |
235 new GtkTargetEntry[clipboard_data_->size()]); | 249 new GtkTargetEntry[clipboard_data_->size()]); |
236 | 250 |
237 int i = 0; | 251 int i = 0; |
238 for (Clipboard::TargetMap::iterator iter = clipboard_data_->begin(); | 252 for (Clipboard::TargetMap::iterator iter = clipboard_data_->begin(); |
239 iter != clipboard_data_->end(); ++iter, ++i) { | 253 iter != clipboard_data_->end(); ++iter, ++i) { |
240 targets[i].target = const_cast<char*>(iter->first.c_str()); | 254 targets[i].target = const_cast<char*>(iter->first.c_str()); |
241 targets[i].flags = 0; | 255 targets[i].flags = 0; |
242 targets[i].info = 0; | 256 targets[i].info = 0; |
243 } | 257 } |
244 | 258 |
245 GtkClipboard *clipboard = LookupBackingClipboard(buffer); | 259 GtkClipboard *clipboard = LookupBackingClipboard(buffer); |
246 | 260 |
247 if (gtk_clipboard_set_with_data(clipboard, targets.get(), | 261 if (gtk_clipboard_set_with_data(clipboard, targets.get(), |
248 clipboard_data_->size(), | 262 clipboard_data_->size(), |
249 GetData, ClearData, | 263 GetData, ClearData, |
250 clipboard_data_)) { | 264 clipboard_data_)) { |
251 gtk_clipboard_set_can_store(clipboard, | 265 gtk_clipboard_set_can_store(clipboard, |
252 targets.get(), | 266 targets.get(), |
253 clipboard_data_->size()); | 267 clipboard_data_->size()); |
254 } | 268 } |
255 | 269 |
256 if (buffer == BUFFER_STANDARD) { | |
257 Clipboard::TargetMap::iterator text_iter = clipboard_data_->find("TEXT"); | |
258 if (text_iter != clipboard_data_->end()) { | |
259 gtk_clipboard_set_text(primary_selection_, text_iter->second.first, | |
260 text_iter->second.second); | |
261 } | |
262 } | |
263 | |
264 // clipboard_data_ now owned by the GtkClipboard. | 270 // clipboard_data_ now owned by the GtkClipboard. |
265 clipboard_data_ = NULL; | 271 clipboard_data_ = NULL; |
266 } | 272 } |
267 | 273 |
268 void Clipboard::WriteText(const char* text_data, size_t text_len) { | 274 void Clipboard::WriteText(const char* text_data, size_t text_len) { |
269 char* data = new char[text_len]; | 275 char* data = new char[text_len]; |
270 memcpy(data, text_data, text_len); | 276 memcpy(data, text_data, text_len); |
271 | 277 |
272 InsertMapping(kMimeTypeText, data, text_len); | 278 InsertMapping(kMimeTypeText, data, text_len); |
273 InsertMapping("TEXT", data, text_len); | 279 InsertMapping("TEXT", data, text_len); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // We assume that certain mapping types are only written by trusted code. | 349 // We assume that certain mapping types are only written by trusted code. |
344 // Therefore we must upkeep their integrity. | 350 // Therefore we must upkeep their integrity. |
345 if (format.Equals(GetBitmapFormatType())) | 351 if (format.Equals(GetBitmapFormatType())) |
346 return; | 352 return; |
347 char* data = new char[data_len]; | 353 char* data = new char[data_len]; |
348 memcpy(data, data_data, data_len); | 354 memcpy(data, data_data, data_len); |
349 // TODO(dcheng): Maybe this map should use GdkAtoms... | 355 // TODO(dcheng): Maybe this map should use GdkAtoms... |
350 InsertMapping(GdkAtomToString(format.ToGdkAtom()).c_str(), data, data_len); | 356 InsertMapping(GdkAtomToString(format.ToGdkAtom()).c_str(), data, data_len); |
351 } | 357 } |
352 | 358 |
| 359 void Clipboard::WriteSourceTag(SourceTag tag) { |
| 360 if (tag != SourceTag()) { |
| 361 ObjectMapParam binary = SourceTag2Binary(tag); |
| 362 WriteData(GetSourceTagFormatType(), &binary[0], binary.size()); |
| 363 } |
| 364 } |
| 365 |
353 // We do not use gtk_clipboard_wait_is_target_available because of | 366 // We do not use gtk_clipboard_wait_is_target_available because of |
354 // a bug with the gtk clipboard. It caches the available targets | 367 // a bug with the gtk clipboard. It caches the available targets |
355 // and does not always refresh the cache when it is appropriate. | 368 // and does not always refresh the cache when it is appropriate. |
356 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, | 369 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, |
357 Clipboard::Buffer buffer) const { | 370 Clipboard::Buffer buffer) const { |
358 DCHECK(CalledOnValidThread()); | 371 DCHECK(CalledOnValidThread()); |
359 GtkClipboard* clipboard = LookupBackingClipboard(buffer); | 372 GtkClipboard* clipboard = LookupBackingClipboard(buffer); |
360 if (clipboard == NULL) | 373 if (clipboard == NULL) |
361 return false; | 374 return false; |
362 | 375 |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 type, result); | 582 type, result); |
570 gtk_selection_data_free(data); | 583 gtk_selection_data_free(data); |
571 } | 584 } |
572 | 585 |
573 void Clipboard::ReadBookmark(string16* title, std::string* url) const { | 586 void Clipboard::ReadBookmark(string16* title, std::string* url) const { |
574 // TODO(estade): implement this. | 587 // TODO(estade): implement this. |
575 NOTIMPLEMENTED(); | 588 NOTIMPLEMENTED(); |
576 } | 589 } |
577 | 590 |
578 void Clipboard::ReadData(const FormatType& format, std::string* result) const { | 591 void Clipboard::ReadData(const FormatType& format, std::string* result) const { |
| 592 ReadDataImpl(BUFFER_STANDARD, format, result); |
| 593 } |
| 594 |
| 595 void Clipboard::ReadDataImpl(Buffer buffer, |
| 596 const FormatType& format, |
| 597 std::string* result) const { |
579 DCHECK(CalledOnValidThread()); | 598 DCHECK(CalledOnValidThread()); |
| 599 result->clear(); |
| 600 GtkClipboard* clipboard = LookupBackingClipboard(buffer); |
580 GtkSelectionData* data = | 601 GtkSelectionData* data = |
581 gtk_clipboard_wait_for_contents(clipboard_, format.ToGdkAtom()); | 602 gtk_clipboard_wait_for_contents(clipboard, format.ToGdkAtom()); |
582 if (!data) | 603 if (!data) |
583 return; | 604 return; |
584 result->assign(reinterpret_cast<const char*>( | 605 result->assign(reinterpret_cast<const char*>( |
585 gtk_selection_data_get_data(data)), | 606 gtk_selection_data_get_data(data)), |
586 gtk_selection_data_get_length(data)); | 607 gtk_selection_data_get_length(data)); |
587 gtk_selection_data_free(data); | 608 gtk_selection_data_free(data); |
588 } | 609 } |
589 | 610 |
| 611 Clipboard::SourceTag Clipboard::ReadSourceTag(Buffer buffer) const { |
| 612 std::string result; |
| 613 ReadDataImpl(buffer, GetSourceTagFormatType(), &result); |
| 614 return Binary2SourceTag(result); |
| 615 } |
| 616 |
590 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { | 617 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { |
591 DCHECK(CalledOnValidThread()); | 618 DCHECK(CalledOnValidThread()); |
592 if (buffer == BUFFER_STANDARD) | 619 if (buffer == BUFFER_STANDARD) |
593 return SelectionChangeObserver::GetInstance()->clipboard_sequence_number(); | 620 return SelectionChangeObserver::GetInstance()->clipboard_sequence_number(); |
594 else | 621 else |
595 return SelectionChangeObserver::GetInstance()->primary_sequence_number(); | 622 return SelectionChangeObserver::GetInstance()->primary_sequence_number(); |
596 } | 623 } |
597 | 624 |
598 //static | 625 //static |
599 Clipboard::FormatType Clipboard::GetFormatType( | 626 Clipboard::FormatType Clipboard::GetFormatType( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeWebCustomData)); | 679 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeWebCustomData)); |
653 return type; | 680 return type; |
654 } | 681 } |
655 | 682 |
656 // static | 683 // static |
657 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() { | 684 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() { |
658 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypePepperCustomData)); | 685 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypePepperCustomData)); |
659 return type; | 686 return type; |
660 } | 687 } |
661 | 688 |
| 689 // static |
| 690 const Clipboard::FormatType& Clipboard::GetSourceTagFormatType() { |
| 691 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kSourceTagType)); |
| 692 return type; |
| 693 } |
| 694 |
662 void Clipboard::InsertMapping(const char* key, | 695 void Clipboard::InsertMapping(const char* key, |
663 char* data, | 696 char* data, |
664 size_t data_len) { | 697 size_t data_len) { |
665 DCHECK(clipboard_data_->find(key) == clipboard_data_->end()); | 698 DCHECK(clipboard_data_->find(key) == clipboard_data_->end()); |
666 (*clipboard_data_)[key] = std::make_pair(data, data_len); | 699 (*clipboard_data_)[key] = std::make_pair(data, data_len); |
667 } | 700 } |
668 | 701 |
669 GtkClipboard* Clipboard::LookupBackingClipboard(Buffer clipboard) const { | 702 GtkClipboard* Clipboard::LookupBackingClipboard(Buffer clipboard) const { |
670 switch (clipboard) { | 703 switch (clipboard) { |
671 case BUFFER_STANDARD: | 704 case BUFFER_STANDARD: |
672 return clipboard_; | 705 return clipboard_; |
673 case BUFFER_SELECTION: | 706 case BUFFER_SELECTION: |
674 return primary_selection_; | 707 return primary_selection_; |
675 default: | 708 default: |
676 NOTREACHED(); | 709 NOTREACHED(); |
677 return NULL; | 710 return NULL; |
678 } | 711 } |
679 } | 712 } |
680 | 713 |
681 } // namespace ui | 714 } // namespace ui |
OLD | NEW |