Bug 512305 - Use xdg-settings for Firefox Snap. r=jhorak
authorOlivier Tilloy <olivier@tilloy.net>
Thu, 01 Mar 2018 10:35:03 -0600
changeset 406287 581dc5ffd572
parent 406286 10c6cc561b13
child 406288 705748b426e1
push id60502
push usermozilla@kaply.com
push dateFri, 02 Mar 2018 13:42:51 +0000
treeherderautoland@581dc5ffd572 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjhorak
bugs512305
milestone60.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 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;