Bug 1497580 - use gtk_target_list_add_text_targets for text/unicode clipboard data r=karlt
authorTom Schuster <evilpies@gmail.com>
Wed, 10 Oct 2018 21:44:41 +0000
changeset 489050 80b93ae22c27962c405c52328f53c49870bf4b9f
parent 489049 8da12a6048fba4e59a860386b075f4d9070f79bf
child 489051 06f4b6597f74d01d0dce767a83ed537612cb7173
push id246
push userfmarier@mozilla.com
push dateSat, 13 Oct 2018 00:15:40 +0000
reviewerskarlt
bugs1497580
milestone64.0a1
Bug 1497580 - use gtk_target_list_add_text_targets for text/unicode clipboard data r=karlt Using gtk_targets_include_text actually leads to a small behavior difference, because gtk_targets_include_text also includes text/plain. But gtk_selection_data_set_text seems to correctly convert UTF-8 to plain text. Differential Revision: https://phabricator.services.mozilla.com/D8283
widget/gtk/mozgtk/mozgtk.c
widget/gtk/nsClipboard.cpp
--- a/widget/gtk/mozgtk/mozgtk.c
+++ b/widget/gtk/mozgtk/mozgtk.c
@@ -410,16 +410,17 @@ STUB(gtk_socket_new)
 STUB(gtk_spin_button_new)
 STUB(gtk_statusbar_new)
 STUB(gtk_style_lookup_icon_set)
 STUB(gtk_table_attach)
 STUB(gtk_table_get_type)
 STUB(gtk_table_new)
 STUB(gtk_target_list_add)
 STUB(gtk_target_list_add_image_targets)
+STUB(gtk_target_list_add_text_targets)
 STUB(gtk_target_list_new)
 STUB(gtk_target_list_unref)
 STUB(gtk_targets_include_image)
 STUB(gtk_targets_include_text)
 STUB(gtk_target_table_free)
 STUB(gtk_target_table_new_from_list)
 STUB(gtk_text_view_new)
 STUB(gtk_toggle_button_get_active)
--- a/widget/gtk/nsClipboard.cpp
+++ b/widget/gtk/nsClipboard.cpp
@@ -8,16 +8,17 @@
 #include "mozilla/ArrayUtils.h"
 
 #include "nsArrayUtils.h"
 #include "nsClipboard.h"
 #include "nsClipboardX11.h"
 #if defined(MOZ_WAYLAND)
 #include "nsClipboardWayland.h"
 #endif
+#include "nsContentUtils.h"
 #include "HeadlessClipboard.h"
 #include "nsSupportsPrimitives.h"
 #include "nsString.h"
 #include "nsReadableUtils.h"
 #include "nsPrimitiveHelpers.h"
 #include "nsIServiceManager.h"
 #include "nsImageToPixbuf.h"
 #include "nsStringStream.h"
@@ -158,32 +159,24 @@ nsClipboard::SetData(nsITransferable *aT
         return rv;
     }
 
     // Add all the flavors to this widget's supported type.
     bool imagesAdded = false;
     for (uint32_t i = 0; i < flavors.Length(); i++) {
         nsCString& flavorStr = flavors[i];
 
-        // special case text/unicode since we can handle all of
-        // the string types
+        // Special case text/unicode since we can handle all of the string types.
         if (flavorStr.EqualsLiteral(kUnicodeMime)) {
-            gtk_target_list_add(list, gdk_atom_intern("UTF8_STRING", FALSE), 0, 0);
-            gtk_target_list_add(list, gdk_atom_intern("COMPOUND_TEXT", FALSE), 0, 0);
-            gtk_target_list_add(list, gdk_atom_intern("TEXT", FALSE), 0, 0);
-            gtk_target_list_add(list, GDK_SELECTION_TYPE_STRING, 0, 0);
+            gtk_target_list_add_text_targets(list, 0);
             continue;
         }
 
-        if (flavorStr.EqualsLiteral(kNativeImageMime) ||
-            flavorStr.EqualsLiteral(kPNGImageMime) ||
-            flavorStr.EqualsLiteral(kJPEGImageMime) ||
-            flavorStr.EqualsLiteral(kJPGImageMime) ||
-            flavorStr.EqualsLiteral(kGIFImageMime)) {
-            // don't bother adding image targets twice
+        if (nsContentUtils::IsFlavorImage(flavorStr)) {
+            // Don't bother adding image targets twice
             if (!imagesAdded) {
                 // accept any writable image type
                 gtk_target_list_add_image_targets(list, 0, TRUE);
                 imagesAdded = true;
             }
             continue;
         }
 
@@ -477,45 +470,37 @@ nsClipboard::SelectionGetEvent(GtkClipbo
     }
 
     nsresult rv;
     nsCOMPtr<nsISupports> item;
     uint32_t len;
 
     GdkAtom selectionTarget = gtk_selection_data_get_target(aSelectionData);
 
-    // Check to see if the selection data includes any of the string
-    // types that we support.
-    if (selectionTarget == gdk_atom_intern ("STRING", FALSE) ||
-        selectionTarget == gdk_atom_intern ("TEXT", FALSE) ||
-        selectionTarget == gdk_atom_intern ("COMPOUND_TEXT", FALSE) ||
-        selectionTarget == gdk_atom_intern ("UTF8_STRING", FALSE)) {
+    // Check to see if the selection data is some text type.
+    if (gtk_targets_include_text(&selectionTarget, 1)) {
         // Try to convert our internal type into a text string.  Get
         // the transferable for this clipboard and try to get the
         // text/unicode type for it.
         rv = trans->GetTransferData("text/unicode", getter_AddRefs(item),
                                     &len);
         if (!item || NS_FAILED(rv))
             return;
 
         nsCOMPtr<nsISupportsString> wideString;
         wideString = do_QueryInterface(item);
         if (!wideString)
             return;
 
         nsAutoString ucs2string;
         wideString->GetData(ucs2string);
-        char *utf8string = ToNewUTF8String(ucs2string);
-        if (!utf8string)
-            return;
+        NS_ConvertUTF16toUTF8 utf8string(ucs2string);
 
-        gtk_selection_data_set_text (aSelectionData, utf8string,
-                                     strlen(utf8string));
-
-        free(utf8string);
+        gtk_selection_data_set_text(aSelectionData, utf8string.get(),
+                                    utf8string.Length());
         return;
     }
 
     // Check to see if the selection data is an image type
     if (gtk_targets_include_image(&selectionTarget, 1, TRUE)) {
         // Look through our transfer data for the image
         static const char* const imageMimeTypes[] = {
             kNativeImageMime, kPNGImageMime, kJPEGImageMime, kJPGImageMime, kGIFImageMime };