Bug 1544739 - Part 7: Update GNOME shell service - Add WriteImage helper. r=frg a=frg
authorIan Neal <iann_cvs@blueyonder.co.uk>
Wed, 24 Apr 2019 18:54:00 +0200
changeset 32186 88e664e02706550303c46775597ab97bcada55f0
parent 32185 efe6ede30b74f68834e39462412a246d75047f9e
child 32187 92feee89deec2b67c46a4b83dfef9819e6c94cdd
push id187
push userfrgrahl@gmx.net
push dateWed, 24 Apr 2019 16:55:18 +0000
treeherdercomm-esr60@33e9c7be6768 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrg, frg
bugs1544739
Bug 1544739 - Part 7: Update GNOME shell service - Add WriteImage helper. r=frg a=frg
suite/components/shell/nsGNOMEShellService.cpp
--- a/suite/components/shell/nsGNOMEShellService.cpp
+++ b/suite/components/shell/nsGNOMEShellService.cpp
@@ -21,17 +21,19 @@
 #include "nsIProcess.h"
 #include "nsServiceManagerUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsIDOMElement.h"
 #include "nsIImageLoadingContent.h"
 #include "imgIRequest.h"
 #include "imgIContainer.h"
 #include "mozilla/Sprintf.h"
+#if defined(MOZ_WIDGET_GTK)
 #include "nsIImageToPixbuf.h"
+#endif
 #include "nsXULAppAPI.h"
 
 #include <glib.h>
 #include <glib-object.h>
 #include <gtk/gtk.h>
 #include <gdk/gdk.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #include <limits.h>
@@ -260,70 +262,64 @@ NS_IMETHODIMP
 nsGNOMEShellService::GetCanSetDesktopBackground(bool* aResult)
 {
   // for Gnome or desktops using the same GSettings keys
   const char *currentDesktop = getenv("XDG_CURRENT_DESKTOP");
   if (currentDesktop && strstr(currentDesktop, "GNOME") != nullptr) {
     *aResult = true;
     return NS_OK;
   }
+
   const char *gnomeSession = getenv("GNOME_DESKTOP_SESSION_ID");
   if (gnomeSession) {
     *aResult = true;
   } else {
     *aResult = false;
   }
 
   return NS_OK;
 }
 
+static nsresult WriteImage(const nsCString &aPath, imgIContainer *aImage) {
+#if !defined(MOZ_WIDGET_GTK)
+  return NS_ERROR_NOT_AVAILABLE;
+#else
+  nsCOMPtr<nsIImageToPixbuf> imgToPixbuf =
+      do_GetService("@mozilla.org/widget/image-to-gdk-pixbuf;1");
+  if (!imgToPixbuf) return NS_ERROR_NOT_AVAILABLE;
+
+  GdkPixbuf *pixbuf = imgToPixbuf->ConvertImageToPixbuf(aImage);
+  if (!pixbuf) return NS_ERROR_NOT_AVAILABLE;
+
+  gboolean res = gdk_pixbuf_save(pixbuf, aPath.get(), "png", nullptr, nullptr);
+
+  g_object_unref(pixbuf);
+  return res ? NS_OK : NS_ERROR_FAILURE;
+#endif
+}
+
 NS_IMETHODIMP
 nsGNOMEShellService::SetDesktopBackground(nsIDOMElement* aElement,
                                           int32_t aPosition,
                                           const nsACString& aImageName)
 {
-  nsCString brandName;
-  nsresult rv = GetBrandName(brandName);
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsresult rv;
+  nsCOMPtr<nsIImageLoadingContent> imageContent =
+      do_QueryInterface(aElement, &rv);
+  if (!imageContent) return rv;
 
-  // build the file name
-  nsCString filePath(PR_GetEnv("HOME"));
-  filePath.Append('/');
-  filePath.Append(brandName);
-  filePath.AppendLiteral("_wallpaper.png");
-
-  // get the image container
-  nsCOMPtr<nsIImageLoadingContent> imageContent(do_QueryInterface(aElement, &rv));
-  NS_ENSURE_SUCCESS(rv, rv);
-
+  // Get the image container.
   nsCOMPtr<imgIRequest> request;
   rv = imageContent->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
                                 getter_AddRefs(request));
-  NS_ENSURE_TRUE(request, rv);
-
+  if (!request) return rv;
   nsCOMPtr<imgIContainer> container;
   rv = request->GetImage(getter_AddRefs(container));
-  NS_ENSURE_TRUE(request, rv);
-
-  nsCOMPtr<nsIImageToPixbuf> imgToPixbuf(do_GetService("@mozilla.org/widget/image-to-gdk-pixbuf;1"));
-  if (!imgToPixbuf)
-    return NS_ERROR_NOT_AVAILABLE;
-
-  GdkPixbuf* pixbuf = imgToPixbuf->ConvertImageToPixbuf(container);
-  if (!pixbuf)
-    return NS_ERROR_NOT_AVAILABLE;
-
-  // write the image to a file in the home dir
-  gboolean res = gdk_pixbuf_save(pixbuf, filePath.get(), "png",
-                                 nullptr, nullptr);
-
-  g_object_unref(pixbuf);
-  if (!res)
-    return NS_ERROR_FAILURE;
-
+  if (!container) return rv;
+ 
   // Set desktop wallpaper filling style.
   nsAutoCString options;
   switch (aPosition) {
     case BACKGROUND_TILE:
       options.AssignLiteral("wallpaper");
       break;
     case BACKGROUND_STRETCH:
       options.AssignLiteral("stretched");
@@ -334,34 +330,50 @@ nsGNOMEShellService::SetDesktopBackgroun
     case BACKGROUND_FIT:
       options.AssignLiteral("scaled");
       break;
     default:
       options.AssignLiteral("centered");
       break;
   }
 
-  nsCOMPtr<nsIGSettingsService> gsettings(do_GetService(NS_GSETTINGSSERVICE_CONTRACTID));
+  // Write the background file to the home directory.
+  nsCString filePath(PR_GetEnv("HOME"));
+
+  nsCString brandName;
+  rv = GetBrandName(brandName);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // Build the file name.
+  filePath.Append('/');
+  filePath.Append(brandName);
+  filePath.AppendLiteral("_wallpaper.png");
+
+  // Write the image to a file in the home dir.
+  rv = WriteImage(filePath, container);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsCOMPtr<nsIGSettingsService> gsettings =
+      do_GetService(NS_GSETTINGSSERVICE_CONTRACTID);
   if (gsettings) {
     nsCOMPtr<nsIGSettingsCollection> background_settings;
     gsettings->GetCollectionForSchema(NS_LITERAL_CSTRING(kDesktopBGSchema),
                                       getter_AddRefs(background_settings));
     if (background_settings) {
       gchar *file_uri = g_filename_to_uri(filePath.get(), nullptr, nullptr);
-      if (!file_uri)
-       return NS_ERROR_FAILURE;
+      if (!file_uri) return NS_ERROR_FAILURE;
 
       background_settings->SetString(NS_LITERAL_CSTRING(kDesktopOptionGSKey),
                                      options);
       background_settings->SetString(NS_LITERAL_CSTRING(kDesktopImageGSKey),
                                      nsDependentCString(file_uri));
       g_free(file_uri);
       background_settings->SetBoolean(NS_LITERAL_CSTRING(kDesktopDrawBGGSKey),
                                       true);
-      return NS_OK;
+      return rv;
     }
   }
 
   return NS_ERROR_FAILURE;
 }
 
 #define COLOR_16_TO_8_BIT(_c) ((_c) >> 8)