Bug 512305 - Use xdg-settings for Firefox Snap. r?jhorak draft
authorOlivier Tilloy <olivier@tilloy.net>
Thu, 01 Mar 2018 10:35:03 -0600
changeset 761893 a9aa805dd3e3
parent 761787 e33efdb3e151
push id101033
push usermozilla@kaply.com
push dateThu, 01 Mar 2018 16:53:43 +0000
reviewersjhorak
bugs512305
milestone60.0a1
Bug 512305 - Use xdg-settings for Firefox Snap. r?jhorak MozReview-Commit-ID: CJvgaLK373j
browser/components/shell/nsGNOMEShellService.cpp
--- a/browser/components/shell/nsGNOMEShellService.cpp
+++ b/browser/components/shell/nsGNOMEShellService.cpp
@@ -77,16 +77,22 @@ static const MimeTypeAssociation appType
 #define kDesktopColorKey DG_BACKGROUND "/primary_color"
 
 #define kDesktopBGSchema "org.gnome.desktop.background"
 #define kDesktopImageGSKey "picture-uri"
 #define kDesktopOptionGSKey "picture-options"
 #define kDesktopDrawBGGSKey "draw-background"
 #define kDesktopColorGSKey "primary-color"
 
+static bool
+IsRunningAsASnap()
+{
+  return (PR_GetEnv("SNAP") != nullptr);
+}
+
 nsresult
 nsGNOMEShellService::Init()
 {
   nsresult rv;
 
   if (gfxPlatform::IsHeadless()) {
     return NS_ERROR_NOT_AVAILABLE;
   }
@@ -202,16 +208,38 @@ nsGNOMEShellService::CheckHandlerMatches
 
 NS_IMETHODIMP
 nsGNOMEShellService::IsDefaultBrowser(bool aStartupCheck,
                                       bool aForAllTypes,
                                       bool* aIsDefaultBrowser)
 {
   *aIsDefaultBrowser = false;
 
+  if (IsRunningAsASnap()) {
+    const gchar *argv[] = { "xdg-settings", "check", "default-web-browser",
+                            "firefox_firefox.desktop", nullptr };
+    GSpawnFlags flags = static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
+                                                 G_SPAWN_STDERR_TO_DEV_NULL);
+    gchar *output = nullptr;
+    gint exit_status = 0;
+    if (!g_spawn_sync(nullptr, (gchar **) argv, nullptr, flags, nullptr,
+                      nullptr, &output, nullptr, &exit_status, nullptr)) {
+      return NS_OK;
+    }
+    if (exit_status != 0) {
+      g_free(output);
+      return NS_OK;
+    }
+    if (strcmp(output, "yes\n") == 0) {
+      *aIsDefaultBrowser = true;
+    }
+    g_free(output);
+    return NS_OK;
+  }
+
   nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
 
   bool enabled;
   nsAutoCString handler;
   nsCOMPtr<nsIGIOMimeApp> gioApp;
 
   for (unsigned int i = 0; i < ArrayLength(appProtocols); ++i) {
@@ -252,16 +280,27 @@ NS_IMETHODIMP
 nsGNOMEShellService::SetDefaultBrowser(bool aClaimAllTypes,
                                        bool aForAllUsers)
 {
 #ifdef DEBUG
   if (aForAllUsers)
     NS_WARNING("Setting the default browser for all users is not yet supported");
 #endif
 
+  if (IsRunningAsASnap()) {
+    const gchar *argv[] = { "xdg-settings", "set", "default-web-browser",
+                            "firefox_firefox.desktop", nullptr };
+    GSpawnFlags flags = static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
+                                                 G_SPAWN_STDOUT_TO_DEV_NULL |
+                                                 G_SPAWN_STDERR_TO_DEV_NULL);
+    g_spawn_sync(nullptr, (gchar **) argv, nullptr, flags, nullptr, nullptr,
+                 nullptr, nullptr, nullptr, nullptr);
+    return NS_OK;
+  }
+
   nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
   if (gconf) {
     nsAutoCString appKeyValue;
     if (mAppIsInPath) {
       // mAppPath is in the users path, so use only the basename as the launcher
       gchar *tmp = g_path_get_basename(mAppPath.get());
       appKeyValue = tmp;