Bug 1800972 [Linux] Factor out image drop r=emilio
authorstransky <stransky@redhat.com>
Tue, 22 Nov 2022 08:14:26 +0000 (2022-11-22)
changeset 643169 bfddce18f8bbd6e46bb858f08e02181c24fe2d9b
parent 643168 e2332b082030e205ed480464e5fb7a5e8eea7a46
child 643170 a90dc6dca2872fc41222d7264c6683ab3cbbf2da
push id173454
push userstransky@redhat.com
push dateTue, 22 Nov 2022 08:17:18 +0000 (2022-11-22)
treeherderautoland@575345f98b95 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemilio
bugs1800972
milestone109.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1800972 [Linux] Factor out image drop r=emilio Differential Revision: https://phabricator.services.mozilla.com/D162467
widget/gtk/nsDragService.cpp
widget/gtk/nsDragService.h
--- a/widget/gtk/nsDragService.cpp
+++ b/widget/gtk/nsDragService.cpp
@@ -1747,16 +1747,41 @@ nsresult nsDragService::CreateTempFile(n
         return NS_OK;
       }
     }
   }
 
   return NS_ERROR_FAILURE;
 }
 
+bool nsDragService::SourceDataGetImage(nsITransferable* aItem,
+                                       GtkSelectionData* aSelectionData) {
+  LOGDRAGSERVICE("nsDragService::SourceDataGetImage()");
+
+  nsresult rv;
+  nsCOMPtr<nsISupports> data;
+  rv = aItem->GetTransferData(kNativeImageMime, getter_AddRefs(data));
+  NS_ENSURE_SUCCESS(rv, false);
+
+  LOGDRAGSERVICE("  posting image\n");
+  nsCOMPtr<imgIContainer> image = do_QueryInterface(data);
+  if (!image) {
+    LOGDRAGSERVICE("  do_QueryInterface failed\n");
+    return false;
+  }
+  RefPtr<GdkPixbuf> pixbuf = nsImageToPixbuf::ImageToPixbuf(image);
+  if (!pixbuf) {
+    LOGDRAGSERVICE("  ImageToPixbuf failed\n");
+    return false;
+  }
+  gtk_selection_data_set_pixbuf(aSelectionData, pixbuf);
+  LOGDRAGSERVICE("  image data set\n");
+  return true;
+}
+
 bool nsDragService::SourceDataGetXDND(nsITransferable* aItem,
                                       GdkDragContext* aContext,
                                       GtkSelectionData* aSelectionData) {
   LOGDRAGSERVICE("nsDragService::SourceDataGetXDND");
 
   // Indicate failure by default.
   GdkAtom target = gtk_selection_data_get_target(aSelectionData);
   gtk_selection_data_set(aSelectionData, target, 8, (guchar*)"E", 1);
@@ -1886,17 +1911,16 @@ void nsDragService::SourceDataGet(GtkWid
   PRUint32 dragItems;
   mSourceDataItems->GetLength(&dragItems);
   LOGDRAGSERVICE("  source data items %d", dragItems);
 #endif
 
   // if someone was asking for text/plain, lookup unicode instead so
   // we can convert it.
   bool needToDoConversionToPlainText = false;
-  bool needToDoConversionToImage = false;
 
   nsDependentCString mimeFlavor(requestedTypeName.get());
   const char* actualFlavor = nullptr;
   if (mimeFlavor.EqualsLiteral(kTextMime) ||
       mimeFlavor.EqualsLiteral(gTextPlainUTF8Type)) {
     actualFlavor = kUnicodeMime;
     needToDoConversionToPlainText = true;
     LOGDRAGSERVICE("  convert %s => %s", actualFlavor, requestedTypeName.get());
@@ -1949,69 +1973,53 @@ void nsDragService::SourceDataGet(GtkWid
       return;
     }
     // if someone was asking for image we need to convert it
     // from kNativeImageMime
   } else if (mimeFlavor.EqualsLiteral(kPNGImageMime) ||
              mimeFlavor.EqualsLiteral(kJPEGImageMime) ||
              mimeFlavor.EqualsLiteral(kJPGImageMime) ||
              mimeFlavor.EqualsLiteral(kGIFImageMime)) {
-    actualFlavor = kNativeImageMime;
-    needToDoConversionToImage = true;
-    LOGDRAGSERVICE("  convert %s => %s", actualFlavor, requestedTypeName.get());
+    if (SourceDataGetImage(item, aSelectionData)) {
+      return;
+    }
   } else {
     actualFlavor = requestedTypeName.get();
     LOGDRAGSERVICE("  use %s", requestedTypeName.get());
   }
   nsresult rv;
   nsCOMPtr<nsISupports> data;
   rv = item->GetTransferData(actualFlavor, getter_AddRefs(data));
   if (NS_SUCCEEDED(rv)) {
-    if (needToDoConversionToImage) {
-      LOGDRAGSERVICE("  posting image\n");
-      nsCOMPtr<imgIContainer> image = do_QueryInterface(data);
-      if (!image) {
-        LOGDRAGSERVICE("  do_QueryInterface failed\n");
-        return;
-      }
-      RefPtr<GdkPixbuf> pixbuf = nsImageToPixbuf::ImageToPixbuf(image);
-      if (!pixbuf) {
-        LOGDRAGSERVICE("  ImageToPixbuf failed\n");
-        return;
-      }
-      gtk_selection_data_set_pixbuf(aSelectionData, pixbuf);
-      LOGDRAGSERVICE("  image data set\n");
-    } else {
-      void* tmpData = nullptr;
-      uint32_t tmpDataLen = 0;
+    void* tmpData = nullptr;
+    uint32_t tmpDataLen = 0;
 
-      nsPrimitiveHelpers::CreateDataFromPrimitive(
-          nsDependentCString(actualFlavor), data, &tmpData, &tmpDataLen);
-      // if required, do the extra work to convert unicode to plain
-      // text and replace the output values with the plain text.
-      if (needToDoConversionToPlainText) {
-        char* plainTextData = nullptr;
-        char16_t* castedUnicode = reinterpret_cast<char16_t*>(tmpData);
-        uint32_t plainTextLen = 0;
-        UTF16ToNewUTF8(castedUnicode, tmpDataLen / 2, &plainTextData,
-                       &plainTextLen);
-        if (tmpData) {
-          // this was not allocated using glib
-          free(tmpData);
-          tmpData = plainTextData;
-          tmpDataLen = plainTextLen;
-        }
+    nsPrimitiveHelpers::CreateDataFromPrimitive(
+        nsDependentCString(actualFlavor), data, &tmpData, &tmpDataLen);
+    // if required, do the extra work to convert unicode to plain
+    // text and replace the output values with the plain text.
+    if (needToDoConversionToPlainText) {
+      char* plainTextData = nullptr;
+      char16_t* castedUnicode = reinterpret_cast<char16_t*>(tmpData);
+      uint32_t plainTextLen = 0;
+      UTF16ToNewUTF8(castedUnicode, tmpDataLen / 2, &plainTextData,
+                     &plainTextLen);
+      if (tmpData) {
+        // this was not allocated using glib
+        free(tmpData);
+        tmpData = plainTextData;
+        tmpDataLen = plainTextLen;
       }
-      if (tmpData) {
-        // this copies the data
-        gtk_selection_data_set(aSelectionData, target, 8, (guchar*)tmpData,
-                               tmpDataLen);
-        // this wasn't allocated with glib
-        free(tmpData);
-      }
+    }
+    if (tmpData) {
+      // this copies the data
+      gtk_selection_data_set(aSelectionData, target, 8, (guchar*)tmpData,
+                             tmpDataLen);
+      // this wasn't allocated with glib
+      free(tmpData);
     }
   } else {
     if (mimeFlavor.EqualsLiteral(gTextUriListType)) {
       // fall back for text/uri-list
       LOGDRAGSERVICE("  fall back to %s\n", requestedTypeName.get());
       nsAutoCString list;
       CreateURIList(mSourceDataItems, list);
       gtk_selection_data_set(aSelectionData, target, 8, (guchar*)list.get(),
--- a/widget/gtk/nsDragService.h
+++ b/widget/gtk/nsDragService.h
@@ -88,16 +88,18 @@ class nsDragService final : public nsBas
   //  END PUBLIC API
 
   // These methods are public only so that they can be called from functions
   // with C calling conventions.  They are called for drags started with the
   // invisible widget.
   void SourceEndDragSession(GdkDragContext* aContext, gint aResult);
   void SourceDataGet(GtkWidget* widget, GdkDragContext* context,
                      GtkSelectionData* selection_data, guint32 aTime);
+  bool SourceDataGetImage(nsITransferable* aItem,
+                          GtkSelectionData* aSelectionData);
   bool SourceDataGetXDND(nsITransferable* aItem, GdkDragContext* aContext,
                          GtkSelectionData* aSelectionData);
 
   void SourceBeginDrag(GdkDragContext* aContext);
 
   // set the drag icon during drag-begin
   void SetDragIcon(GdkDragContext* aContext);