a8a561
diff -up thunderbird-45.0/mozilla/b2g/installer/package-manifest.in.moz-1134537-gnomevfsservice thunderbird-45.0/mozilla/b2g/installer/package-manifest.in
a8a561
--- thunderbird-45.0/mozilla/b2g/installer/package-manifest.in.moz-1134537-gnomevfsservice	2016-04-07 23:33:14.000000000 +0200
a8a561
+++ thunderbird-45.0/mozilla/b2g/installer/package-manifest.in	2016-05-09 13:19:52.317048303 +0200
a8a561
@@ -912,6 +912,12 @@ bin/libfreebl_32int64_3.so
a8a561
 @RESPATH@/crashreporter-override.ini
a8a561
 #endif
a8a561
 
a8a561
+; [Extensions]
a8a561
+;
a8a561
+#ifdef MOZ_ENABLE_GNOMEVFS
a8a561
+bin/components/@DLL_PREFIX@nkgnomevfs@DLL_SUFFIX@
a8a561
+#endif
a8a561
+
a8a561
 [b2g]
a8a561
 #ifndef MOZ_WIDGET_GONK
a8a561
 #ifdef XP_WIN32
a8a561
diff -up thunderbird-45.0/mozilla/browser/installer/package-manifest.in.moz-1134537-gnomevfsservice thunderbird-45.0/mozilla/browser/installer/package-manifest.in
a8a561
--- thunderbird-45.0/mozilla/browser/installer/package-manifest.in.moz-1134537-gnomevfsservice	2016-05-09 13:19:52.318048303 +0200
a8a561
+++ thunderbird-45.0/mozilla/browser/installer/package-manifest.in	2016-05-09 13:24:23.525852797 +0200
a8a561
@@ -495,6 +495,9 @@
a8a561
 #ifdef XP_MACOSX
a8a561
 @RESPATH@/browser/components/SafariProfileMigrator.js
a8a561
 #endif
a8a561
+#ifdef MOZ_ENABLE_GNOMEVFS
a8a561
+@RESPATH@/components/@DLL_PREFIX@nkgnomevfs@DLL_SUFFIX@
a8a561
+#endif
a8a561
 @RESPATH@/components/nsINIProcessor.manifest
a8a561
 @RESPATH@/components/nsINIProcessor.js
a8a561
 @RESPATH@/components/nsPrompter.manifest
a8a561
diff -up thunderbird-45.0/mozilla/config/system-headers.moz-1134537-gnomevfsservice thunderbird-45.0/mozilla/config/system-headers
a8a561
--- thunderbird-45.0/mozilla/config/system-headers.moz-1134537-gnomevfsservice	2016-05-09 13:19:52.298048317 +0200
a8a561
+++ thunderbird-45.0/mozilla/config/system-headers	2016-05-09 13:19:52.318048303 +0200
a8a561
@@ -630,6 +630,14 @@ libgnome/libgnome.h
a8a561
 libgnomeui/gnome-icon-lookup.h
a8a561
 libgnomeui/gnome-icon-theme.h
a8a561
 libgnomeui/gnome-ui-init.h
a8a561
+libgnomevfs/gnome-vfs-file-info.h
a8a561
+libgnomevfs/gnome-vfs.h
a8a561
+libgnomevfs/gnome-vfs-init.h
a8a561
+libgnomevfs/gnome-vfs-mime.h
a8a561
+libgnomevfs/gnome-vfs-mime-handlers.h
a8a561
+libgnomevfs/gnome-vfs-mime-utils.h
a8a561
+libgnomevfs/gnome-vfs-ops.h
a8a561
+libgnomevfs/gnome-vfs-standard-callbacks.h
a8a561
 lib$routines.h
a8a561
 limits
a8a561
 limits.h
a8a561
diff -up thunderbird-45.0/mozilla/configure.in.moz-1134537-gnomevfsservice thunderbird-45.0/mozilla/configure.in
a8a561
--- thunderbird-45.0/mozilla/configure.in.moz-1134537-gnomevfsservice	2016-05-09 13:19:52.299048316 +0200
a8a561
+++ thunderbird-45.0/mozilla/configure.in	2016-05-09 13:21:31.457976836 +0200
a8a561
@@ -71,6 +71,7 @@ GTK3_VERSION=3.4.0
a8a561
 GDK_VERSION_MIN_REQUIRED=GDK_VERSION_3_4
a8a561
 WINDRES_VERSION=2.14.90
a8a561
 W32API_VERSION=3.14
a8a561
+GNOMEVFS_VERSION=2.0
a8a561
 GNOMEUI_VERSION=2.2.0
a8a561
 GCONF_VERSION=1.2.1
a8a561
 STARTUP_NOTIFICATION_VERSION=0.8
a8a561
@@ -4632,7 +4633,7 @@ then
a8a561
 fi
a8a561
 
a8a561
 dnl ========================================================
a8a561
-dnl = GIO and GConf support module
a8a561
+dnl = GnomeVFS, GIO and GConf support module
a8a561
 dnl ========================================================
a8a561
 
a8a561
 if test "$MOZ_X11"
a8a561
@@ -4646,6 +4647,31 @@ then
a8a561
     fi
a8a561
 
a8a561
     dnl ========================================================
a8a561
+    dnl = GnomeVFS support module
a8a561
+    dnl ========================================================
a8a561
+    MOZ_ARG_ENABLE_BOOL(gnomevfs,
a8a561
+    [  --enable-gnomevfs       Enable GnomeVFS support (default: disabled)],
a8a561
+        MOZ_ENABLE_GNOMEVFS=force,
a8a561
+        MOZ_ENABLE_GNOMEVFS=)
a8a561
+
a8a561
+    if test "$MOZ_ENABLE_GNOMEVFS"
a8a561
+    then
a8a561
+        PKG_CHECK_MODULES(MOZ_GNOMEVFS, gnome-vfs-2.0 >= $GNOMEVFS_VERSION gnome-vfs-module-2.0 >= $GNOMEVFS_VERSION,[
a8a561
+            MOZ_GNOMEVFS_LIBS=`echo $MOZ_GNOMEVFS_LIBS | sed 's/-llinc\>//'`
a8a561
+            MOZ_ENABLE_GNOMEVFS=1
a8a561
+            AC_DEFINE(MOZ_ENABLE_GNOMEVFS)
a8a561
+        ],[
a8a561
+            if test "$MOZ_ENABLE_GNOMEVFS" = "force"
a8a561
+            then
a8a561
+                AC_MSG_ERROR([* * * Could not find gnome-vfs-module-2.0 >= $GNOMEVFS_VERSION])
a8a561
+            fi
a8a561
+            MOZ_ENABLE_GNOMEVFS=
a8a561
+        ])
a8a561
+    fi
a8a561
+
a8a561
+    AC_SUBST(MOZ_ENABLE_GNOMEVFS)
a8a561
+
a8a561
+    dnl ========================================================
a8a561
     dnl = GIO support module
a8a561
     dnl ========================================================
a8a561
     MOZ_ARG_DISABLE_BOOL(gio,
a8a561
@@ -6297,6 +6323,19 @@ MOZ_ARG_ENABLE_STRING(extensions,
a8a561
 done],
a8a561
     MOZ_EXTENSIONS="$MOZ_EXTENSIONS_DEFAULT")
a8a561
 
a8a561
+if test -z "$MOZ_ENABLE_GNOMEVFS" -a `echo "$MOZ_EXTENSIONS" | grep -c gnomevfs` -ne 0; then
a8a561
+    # Suppress warning on non-X11 platforms
a8a561
+    if test -n "$MOZ_X11"; then
a8a561
+        AC_MSG_WARN([Removing gnomevfs from MOZ_EXTENSIONS due to no --enable-gnomevfs.])
a8a561
+    fi
a8a561
+    MOZ_EXTENSIONS=`echo $MOZ_EXTENSIONS | sed -e 's|gnomevfs||'`
a8a561
+fi
a8a561
+
a8a561
+dnl Do not build gnomevfs with libxul based apps
a8a561
+if test -n "$LIBXUL_SDK_DIR" -a `echo "$MOZ_EXTENSIONS" | grep -c gnomevfs` -ne 0; then
a8a561
+    MOZ_EXTENSIONS=`echo $MOZ_EXTENSIONS | sed -e 's|gnomevfs||'`
a8a561
+fi
a8a561
+
a8a561
 if test -z "$MOZ_ENABLE_GIO" -a `echo "$MOZ_EXTENSIONS" | grep -c gio` -ne 0; then
a8a561
     # Suppress warning on non-X11 platforms
a8a561
     if test -n "$MOZ_X11"; then
a8a561
diff -up thunderbird-45.0/mozilla/extensions/gnomevfs/moz.build.moz-1134537-gnomevfsservice thunderbird-45.0/mozilla/extensions/gnomevfs/moz.build
a8a561
--- thunderbird-45.0/mozilla/extensions/gnomevfs/moz.build.moz-1134537-gnomevfsservice	2016-05-09 13:19:52.321048300 +0200
a8a561
+++ thunderbird-45.0/mozilla/extensions/gnomevfs/moz.build	2016-05-09 13:19:52.321048300 +0200
a8a561
@@ -0,0 +1,19 @@
a8a561
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
a8a561
+# vim: set filetype=python:
a8a561
+# This Source Code Form is subject to the terms of the Mozilla Public
a8a561
+# License, v. 2.0. If a copy of the MPL was not distributed with this
a8a561
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
a8a561
+
a8a561
+SOURCES += [
a8a561
+    'nsGnomeVFSProtocolHandler.cpp',
a8a561
+]
a8a561
+
a8a561
+XPCOMBinaryComponent('nkgnomevfs')
a8a561
+
a8a561
+# make sure this component is never statically linked into the main
a8a561
+# application.  this is necessary since we don't want to force users
a8a561
+# to install gnome-vfs2 in order to use the rest of mozilla ;-)
a8a561
+
a8a561
+CXXFLAGS += CONFIG['MOZ_GNOMEVFS_CFLAGS']
a8a561
+
a8a561
+OS_LIBS += CONFIG['MOZ_GNOMEVFS_LIBS']
a8a561
diff -up thunderbird-45.0/mozilla/extensions/gnomevfs/nsGnomeVFSProtocolHandler.cpp.moz-1134537-gnomevfsservice thunderbird-45.0/mozilla/extensions/gnomevfs/nsGnomeVFSProtocolHandler.cpp
a8a561
--- thunderbird-45.0/mozilla/extensions/gnomevfs/nsGnomeVFSProtocolHandler.cpp.moz-1134537-gnomevfsservice	2016-05-09 13:19:52.322048300 +0200
a8a561
+++ thunderbird-45.0/mozilla/extensions/gnomevfs/nsGnomeVFSProtocolHandler.cpp	2016-05-09 13:19:52.322048300 +0200
a8a561
@@ -0,0 +1,988 @@
a8a561
+/* vim:set ts=2 sw=2 et cindent: */
a8a561
+/* This Source Code Form is subject to the terms of the Mozilla Public
a8a561
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
a8a561
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
a8a561
+
a8a561
+// GnomeVFS v2.2.2 is missing G_BEGIN_DECLS in gnome-vfs-module-callback.h
a8a561
+extern "C" {
a8a561
+#include <libgnomevfs/gnome-vfs.h>
a8a561
+#include <libgnomevfs/gnome-vfs-standard-callbacks.h>
a8a561
+#include <libgnomevfs/gnome-vfs-mime-utils.h>
a8a561
+}
a8a561
+
a8a561
+#include <algorithm>
a8a561
+
a8a561
+#include "nsServiceManagerUtils.h"
a8a561
+#include "nsComponentManagerUtils.h"
a8a561
+#include "mozilla/ModuleUtils.h"
a8a561
+#include "nsIInterfaceRequestorUtils.h"
a8a561
+#include "nsIPrefService.h"
a8a561
+#include "nsIPrefBranch.h"
a8a561
+#include "nsIObserver.h"
a8a561
+#include "nsThreadUtils.h"
a8a561
+#include "nsProxyRelease.h"
a8a561
+#include "nsIAuthPrompt.h"
a8a561
+#include "nsIStringBundle.h"
a8a561
+#include "nsIStandardURL.h"
a8a561
+#include "nsIURL.h"
a8a561
+#include "nsMimeTypes.h"
a8a561
+#include "nsNetUtil.h"
a8a561
+#include "nsINetUtil.h"
a8a561
+#include "nsAutoPtr.h"
a8a561
+#include "nsError.h"
a8a561
+#include "prlog.h"
a8a561
+#include "prtime.h"
a8a561
+#include "prprf.h"
a8a561
+#include "plstr.h"
a8a561
+#include "mozilla/Attributes.h"
a8a561
+
a8a561
+#define MOZ_GNOMEVFS_SCHEME              "moz-gnomevfs"
a8a561
+#define MOZ_GNOMEVFS_SUPPORTED_PROTOCOLS "network.gnomevfs.supported-protocols"
a8a561
+
a8a561
+//-----------------------------------------------------------------------------
a8a561
+
a8a561
+// NSPR_LOG_MODULES=gnomevfs:5
a8a561
+#ifdef PR_LOGGING
a8a561
+static PRLogModuleInfo *sGnomeVFSLog;
a8a561
+#define LOG(args) PR_LOG(sGnomeVFSLog, PR_LOG_DEBUG, args)
a8a561
+#else
a8a561
+#define LOG(args)
a8a561
+#endif
a8a561
+
a8a561
+//-----------------------------------------------------------------------------
a8a561
+
a8a561
+static nsresult
a8a561
+MapGnomeVFSResult(GnomeVFSResult result)
a8a561
+{
a8a561
+  switch (result)
a8a561
+  {
a8a561
+    case GNOME_VFS_OK:                           return NS_OK;
a8a561
+    case GNOME_VFS_ERROR_NOT_FOUND:              return NS_ERROR_FILE_NOT_FOUND;
a8a561
+    case GNOME_VFS_ERROR_INTERNAL:               return NS_ERROR_UNEXPECTED;
a8a561
+    case GNOME_VFS_ERROR_BAD_PARAMETERS:         return NS_ERROR_INVALID_ARG;
a8a561
+    case GNOME_VFS_ERROR_NOT_SUPPORTED:          return NS_ERROR_NOT_AVAILABLE;
a8a561
+    case GNOME_VFS_ERROR_CORRUPTED_DATA:         return NS_ERROR_FILE_CORRUPTED;
a8a561
+    case GNOME_VFS_ERROR_TOO_BIG:                return NS_ERROR_FILE_TOO_BIG;
a8a561
+    case GNOME_VFS_ERROR_NO_SPACE:               return NS_ERROR_FILE_NO_DEVICE_SPACE;
a8a561
+    case GNOME_VFS_ERROR_READ_ONLY:
a8a561
+    case GNOME_VFS_ERROR_READ_ONLY_FILE_SYSTEM:  return NS_ERROR_FILE_READ_ONLY;
a8a561
+    case GNOME_VFS_ERROR_INVALID_URI:
a8a561
+    case GNOME_VFS_ERROR_INVALID_HOST_NAME:      return NS_ERROR_MALFORMED_URI;
a8a561
+    case GNOME_VFS_ERROR_ACCESS_DENIED:
a8a561
+    case GNOME_VFS_ERROR_NOT_PERMITTED:
a8a561
+    case GNOME_VFS_ERROR_LOGIN_FAILED:           return NS_ERROR_FILE_ACCESS_DENIED;
a8a561
+    case GNOME_VFS_ERROR_EOF:                    return NS_BASE_STREAM_CLOSED;
a8a561
+    case GNOME_VFS_ERROR_NOT_A_DIRECTORY:        return NS_ERROR_FILE_NOT_DIRECTORY;
a8a561
+    case GNOME_VFS_ERROR_IN_PROGRESS:            return NS_ERROR_IN_PROGRESS;
a8a561
+    case GNOME_VFS_ERROR_FILE_EXISTS:            return NS_ERROR_FILE_ALREADY_EXISTS;
a8a561
+    case GNOME_VFS_ERROR_IS_DIRECTORY:           return NS_ERROR_FILE_IS_DIRECTORY;
a8a561
+    case GNOME_VFS_ERROR_NO_MEMORY:              return NS_ERROR_OUT_OF_MEMORY;
a8a561
+    case GNOME_VFS_ERROR_HOST_NOT_FOUND:
a8a561
+    case GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS:    return NS_ERROR_UNKNOWN_HOST;
a8a561
+    case GNOME_VFS_ERROR_CANCELLED:
a8a561
+    case GNOME_VFS_ERROR_INTERRUPTED:            return NS_ERROR_ABORT;
a8a561
+    case GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY:    return NS_ERROR_FILE_DIR_NOT_EMPTY;
a8a561
+    case GNOME_VFS_ERROR_NAME_TOO_LONG:          return NS_ERROR_FILE_NAME_TOO_LONG;
a8a561
+    case GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE:  return NS_ERROR_UNKNOWN_PROTOCOL;
a8a561
+
a8a561
+    /* No special mapping for these error codes...
a8a561
+
a8a561
+    case GNOME_VFS_ERROR_GENERIC:
a8a561
+    case GNOME_VFS_ERROR_IO:
a8a561
+    case GNOME_VFS_ERROR_WRONG_FORMAT:
a8a561
+    case GNOME_VFS_ERROR_BAD_FILE:
a8a561
+    case GNOME_VFS_ERROR_NOT_OPEN:
a8a561
+    case GNOME_VFS_ERROR_INVALID_OPEN_MODE:
a8a561
+    case GNOME_VFS_ERROR_TOO_MANY_OPEN_FILES:
a8a561
+    case GNOME_VFS_ERROR_LOOP:
a8a561
+    case GNOME_VFS_ERROR_DIRECTORY_BUSY:
a8a561
+    case GNOME_VFS_ERROR_TOO_MANY_LINKS:
a8a561
+    case GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM:
a8a561
+    case GNOME_VFS_ERROR_SERVICE_OBSOLETE:
a8a561
+    case GNOME_VFS_ERROR_PROTOCOL_ERROR:
a8a561
+    case GNOME_VFS_ERROR_NO_MASTER_BROWSER:
a8a561
+
a8a561
+    */
a8a561
+
a8a561
+    // Make GCC happy
a8a561
+    default:
a8a561
+      return NS_ERROR_FAILURE;
a8a561
+  }
a8a561
+
a8a561
+  return NS_ERROR_FAILURE;
a8a561
+}
a8a561
+
a8a561
+//-----------------------------------------------------------------------------
a8a561
+
a8a561
+static void
a8a561
+ProxiedAuthCallback(gconstpointer in,
a8a561
+                    gsize         in_size,
a8a561
+                    gpointer      out,
a8a561
+                    gsize         out_size,
a8a561
+                    gpointer      callback_data)
a8a561
+{
a8a561
+  GnomeVFSModuleCallbackAuthenticationIn *authIn =
a8a561
+      (GnomeVFSModuleCallbackAuthenticationIn *) in;
a8a561
+  GnomeVFSModuleCallbackAuthenticationOut *authOut =
a8a561
+      (GnomeVFSModuleCallbackAuthenticationOut *) out;
a8a561
+
a8a561
+  LOG(("gnomevfs: ProxiedAuthCallback [uri=%s]\n", authIn->uri));
a8a561
+
a8a561
+  // Without a channel, we have no way of getting a prompter.
a8a561
+  nsIChannel *channel = (nsIChannel *) callback_data;
a8a561
+  if (!channel)
a8a561
+    return;
a8a561
+
a8a561
+  nsCOMPtr<nsIAuthPrompt> prompt;
a8a561
+  NS_QueryNotificationCallbacks(channel, prompt);
a8a561
+
a8a561
+  // If no auth prompt, then give up.  We could failover to using the
a8a561
+  // WindowWatcher service, but that might defeat a consumer's purposeful
a8a561
+  // attempt to disable authentication (for whatever reason).
a8a561
+  if (!prompt)
a8a561
+    return;
a8a561
+
a8a561
+  // Parse out the host and port...
a8a561
+  nsCOMPtr<nsIURI> uri;
a8a561
+  channel->GetURI(getter_AddRefs(uri));
a8a561
+  if (!uri)
a8a561
+    return;
a8a561
+
a8a561
+#ifdef DEBUG
a8a561
+  {
a8a561
+    //
a8a561
+    // Make sure authIn->uri is consistent with the channel's URI.
a8a561
+    //
a8a561
+    // XXX This check is probably not IDN safe, and it might incorrectly
a8a561
+    //     fire as a result of escaping differences.  It's unclear what
a8a561
+    //     kind of transforms GnomeVFS might have applied to the URI spec
a8a561
+    //     that we originally gave to it.  In spite of the likelihood of
a8a561
+    //     false hits, this check is probably still valuable.
a8a561
+    //
a8a561
+    nsAutoCString spec;
a8a561
+    uri->GetSpec(spec);
a8a561
+    int uriLen = strlen(authIn->uri);
a8a561
+    if (!StringHead(spec, uriLen).Equals(nsDependentCString(authIn->uri, uriLen)))
a8a561
+    {
a8a561
+      LOG(("gnomevfs: [spec=%s authIn->uri=%s]\n", spec.get(), authIn->uri));
a8a561
+      NS_ERROR("URI mismatch");
a8a561
+    }
a8a561
+  }
a8a561
+#endif
a8a561
+
a8a561
+  nsAutoCString scheme, hostPort;
a8a561
+  uri->GetScheme(scheme);
a8a561
+  uri->GetHostPort(hostPort);
a8a561
+
a8a561
+  // It doesn't make sense for either of these strings to be empty.  What kind
a8a561
+  // of funky URI is this?
a8a561
+  if (scheme.IsEmpty() || hostPort.IsEmpty())
a8a561
+    return;
a8a561
+
a8a561
+  // Construct the single signon key.  Altering the value of this key will
a8a561
+  // cause people's remembered passwords to be forgotten.  Think carefully
a8a561
+  // before changing the way this key is constructed.
a8a561
+  nsAutoString key, realm;
a8a561
+
a8a561
+  NS_ConvertUTF8toUTF16 dispHost(scheme);
a8a561
+  dispHost.AppendLiteral("://");
a8a561
+  dispHost.Append(NS_ConvertUTF8toUTF16(hostPort));
a8a561
+
a8a561
+  key = dispHost;
a8a561
+  if (authIn->realm)
a8a561
+  {
a8a561
+    // We assume the realm string is ASCII.  That might be a bogus assumption,
a8a561
+    // but we have no idea what encoding GnomeVFS is using, so for now we'll
a8a561
+    // limit ourselves to ISO-Latin-1.  XXX What is a better solution?
a8a561
+    realm.Append('"');
a8a561
+    realm.Append(NS_ConvertASCIItoUTF16(authIn->realm));
a8a561
+    realm.Append('"');
a8a561
+    key.Append(' ');
a8a561
+    key.Append(realm);
a8a561
+  }
a8a561
+
a8a561
+  // Construct the message string...
a8a561
+  //
a8a561
+  // We use Necko's string bundle here.  This code really should be encapsulated
a8a561
+  // behind some Necko API, after all this code is based closely on the code in
a8a561
+  // nsHttpChannel.cpp.
a8a561
+
a8a561
+  nsCOMPtr<nsIStringBundleService> bundleSvc =
a8a561
+      do_GetService(NS_STRINGBUNDLE_CONTRACTID);
a8a561
+  if (!bundleSvc)
a8a561
+    return;
a8a561
+
a8a561
+  nsCOMPtr<nsIStringBundle> bundle;
a8a561
+  bundleSvc->CreateBundle("chrome://global/locale/commonDialogs.properties",
a8a561
+                          getter_AddRefs(bundle));
a8a561
+  if (!bundle)
a8a561
+    return;
a8a561
+
a8a561
+  nsString message;
a8a561
+  if (!realm.IsEmpty())
a8a561
+  {
a8a561
+    const char16_t *strings[] = { realm.get(), dispHost.get() };
a8a561
+    bundle->FormatStringFromName(MOZ_UTF16("EnterUserPasswordForRealm"),
a8a561
+                                 strings, 2, getter_Copies(message));
a8a561
+  }
a8a561
+  else
a8a561
+  {
a8a561
+    const char16_t *strings[] = { dispHost.get() };
a8a561
+    bundle->FormatStringFromName(MOZ_UTF16("EnterUserPasswordFor"),
a8a561
+                                 strings, 1, getter_Copies(message));
a8a561
+  }
a8a561
+  if (message.IsEmpty())
a8a561
+    return;
a8a561
+
a8a561
+  // Prompt the user...
a8a561
+  nsresult rv;
a8a561
+  bool retval = false;
a8a561
+  char16_t *user = nullptr, *pass = nullptr;
a8a561
+
a8a561
+  rv = prompt->PromptUsernameAndPassword(nullptr, message.get(),
a8a561
+                                         key.get(),
a8a561
+                                         nsIAuthPrompt::SAVE_PASSWORD_PERMANENTLY,
a8a561
+                                         &user, &pass, &retval);
a8a561
+  if (NS_FAILED(rv))
a8a561
+    return;
a8a561
+  if (!retval || !user || !pass)
a8a561
+    return;
a8a561
+
a8a561
+  // XXX We need to convert the UTF-16 username and password from our dialog to
a8a561
+  // strings that GnomeVFS can understand.  It's unclear what encoding GnomeVFS
a8a561
+  // expects, so for now we assume 7-bit ASCII.  Hopefully, we can get a better
a8a561
+  // solution at some point.
a8a561
+
a8a561
+  // One copy is never enough...
a8a561
+  authOut->username = g_strdup(NS_LossyConvertUTF16toASCII(user).get());
a8a561
+  authOut->password = g_strdup(NS_LossyConvertUTF16toASCII(pass).get());
a8a561
+
a8a561
+  nsMemory::Free(user);
a8a561
+  nsMemory::Free(pass);
a8a561
+}
a8a561
+
a8a561
+struct nsGnomeVFSAuthCallbackEvent : public nsRunnable
a8a561
+{
a8a561
+  gconstpointer in;
a8a561
+  gsize         in_size;
a8a561
+  gpointer      out;
a8a561
+  gsize         out_size;
a8a561
+  gpointer      callback_data;
a8a561
+
a8a561
+  NS_IMETHOD Run() {
a8a561
+    ProxiedAuthCallback(in, in_size, out, out_size, callback_data);
a8a561
+    return NS_OK;
a8a561
+  }
a8a561
+};
a8a561
+
a8a561
+static void
a8a561
+AuthCallback(gconstpointer in,
a8a561
+             gsize         in_size,
a8a561
+             gpointer      out,
a8a561
+             gsize         out_size,
a8a561
+             gpointer      callback_data)
a8a561
+{
a8a561
+  // Need to proxy this callback over to the main thread.  Synchronous dispatch
a8a561
+  // is required in order to provide data to the GnomeVFS callback.
a8a561
+
a8a561
+  nsRefPtr<nsGnomeVFSAuthCallbackEvent> ev = new nsGnomeVFSAuthCallbackEvent();
a8a561
+  if (!ev)
a8a561
+    return;  // OOM
a8a561
+
a8a561
+  ev->in = in;
a8a561
+  ev->in_size = in_size;
a8a561
+  ev->out = out;
a8a561
+  ev->out_size = out_size;
a8a561
+  ev->callback_data = callback_data;
a8a561
+
a8a561
+  NS_DispatchToMainThread(ev, NS_DISPATCH_SYNC);
a8a561
+}
a8a561
+
a8a561
+//-----------------------------------------------------------------------------
a8a561
+
a8a561
+static gint
a8a561
+FileInfoComparator(gconstpointer a, gconstpointer b)
a8a561
+{
a8a561
+  const GnomeVFSFileInfo *ia = (const GnomeVFSFileInfo *) a;
a8a561
+  const GnomeVFSFileInfo *ib = (const GnomeVFSFileInfo *) b;
a8a561
+
a8a561
+  return strcasecmp(ia->name, ib->name);
a8a561
+}
a8a561
+
a8a561
+//-----------------------------------------------------------------------------
a8a561
+
a8a561
+class nsGnomeVFSInputStream : public nsIInputStream
a8a561
+{
a8a561
+  public:
a8a561
+    NS_DECL_THREADSAFE_ISUPPORTS
a8a561
+    NS_DECL_NSIINPUTSTREAM
a8a561
+
a8a561
+    nsGnomeVFSInputStream(const nsCString &uriSpec)
a8a561
+      : mSpec(uriSpec)
a8a561
+      , mChannel(nullptr)
a8a561
+      , mHandle(nullptr)
a8a561
+      , mBytesRemaining(UINT64_MAX)
a8a561
+      , mStatus(NS_OK)
a8a561
+      , mDirList(nullptr)
a8a561
+      , mDirListPtr(nullptr)
a8a561
+      , mDirBufCursor(0)
a8a561
+      , mDirOpen(false) {}
a8a561
+
a8a561
+   ~nsGnomeVFSInputStream() { Close(); }
a8a561
+
a8a561
+    void SetChannel(nsIChannel *channel)
a8a561
+    {
a8a561
+      // We need to hold an owning reference to our channel.  This is done
a8a561
+      // so we can access the channel's notification callbacks to acquire
a8a561
+      // a reference to a nsIAuthPrompt if we need to handle a GnomeVFS
a8a561
+      // authentication callback.
a8a561
+      //
a8a561
+      // However, the channel can only be accessed on the main thread, so
a8a561
+      // we have to be very careful with ownership.  Moreover, it doesn't
a8a561
+      // support threadsafe addref/release, so proxying is the answer.
a8a561
+      //
a8a561
+      // Also, it's important to note that this likely creates a reference
a8a561
+      // cycle since the channel likely owns this stream.  This reference
a8a561
+      // cycle is broken in our Close method.
a8a561
+
a8a561
+      NS_ADDREF(mChannel = channel);
a8a561
+    }
a8a561
+
a8a561
+  private:
a8a561
+    GnomeVFSResult DoOpen();
a8a561
+    GnomeVFSResult DoRead(char *aBuf, uint32_t aCount, uint32_t *aCountRead);
a8a561
+    nsresult       SetContentTypeOfChannel(const char *contentType);
a8a561
+
a8a561
+  private:
a8a561
+    nsCString                mSpec;
a8a561
+    nsIChannel              *mChannel; // manually refcounted
a8a561
+    GnomeVFSHandle          *mHandle;
a8a561
+    uint64_t                 mBytesRemaining;
a8a561
+    nsresult                 mStatus;
a8a561
+    GList                   *mDirList;
a8a561
+    GList                   *mDirListPtr;
a8a561
+    nsCString                mDirBuf;
a8a561
+    uint32_t                 mDirBufCursor;
a8a561
+    bool                     mDirOpen;
a8a561
+};
a8a561
+
a8a561
+GnomeVFSResult
a8a561
+nsGnomeVFSInputStream::DoOpen()
a8a561
+{
a8a561
+  GnomeVFSResult rv;
a8a561
+
a8a561
+  NS_ASSERTION(mHandle == nullptr, "already open");
a8a561
+
a8a561
+  // Push a callback handler on the stack for this thread, so we can intercept
a8a561
+  // authentication requests from GnomeVFS.  We'll use the channel to get a
a8a561
+  // nsIAuthPrompt instance.
a8a561
+
a8a561
+  gnome_vfs_module_callback_push(GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION,
a8a561
+                                 AuthCallback, mChannel, nullptr);
a8a561
+
a8a561
+  // Query the mime type first (this could return nullptr). 
a8a561
+  //
a8a561
+  // XXX We need to do this up-front in order to determine how to open the URI.
a8a561
+  //     Unfortunately, the error code GNOME_VFS_ERROR_IS_DIRECTORY is not
a8a561
+  //     always returned by gnome_vfs_open when we pass it a URI to a directory!
a8a561
+  //     Otherwise, we could have used that as a way to failover to opening the
a8a561
+  //     URI as a directory.  Also, it would have been ideal if
a8a561
+  //     gnome_vfs_get_file_info_from_handle were actually implemented by the
a8a561
+  //     smb:// module, since that would have allowed us to potentially save a
a8a561
+  //     round trip to the server to discover the mime type of the document in
a8a561
+  //     the case where gnome_vfs_open would have been used.  (Oh well!  /me
a8a561
+  //     throws hands up in the air and moves on...)
a8a561
+
a8a561
+  GnomeVFSFileInfo info = {0};
a8a561
+  rv = gnome_vfs_get_file_info(mSpec.get(), &info, GnomeVFSFileInfoOptions(
a8a561
+                               GNOME_VFS_FILE_INFO_DEFAULT |
a8a561
+                               GNOME_VFS_FILE_INFO_FOLLOW_LINKS));
a8a561
+  if (rv == GNOME_VFS_OK)
a8a561
+  {
a8a561
+    if (info.type == GNOME_VFS_FILE_TYPE_DIRECTORY)
a8a561
+    {
a8a561
+      rv = gnome_vfs_directory_list_load(&mDirList, mSpec.get(),
a8a561
+                                         GNOME_VFS_FILE_INFO_DEFAULT);
a8a561
+
a8a561
+      LOG(("gnomevfs: gnome_vfs_directory_list_load returned %d (%s) [spec=\"%s\"]\n",
a8a561
+          rv, gnome_vfs_result_to_string(rv), mSpec.get()));
a8a561
+    }
a8a561
+    else
a8a561
+    {
a8a561
+      rv = gnome_vfs_open(&mHandle, mSpec.get(), GNOME_VFS_OPEN_READ);
a8a561
+
a8a561
+      LOG(("gnomevfs: gnome_vfs_open returned %d (%s) [spec=\"%s\"]\n",
a8a561
+          rv, gnome_vfs_result_to_string(rv), mSpec.get()));
a8a561
+    }
a8a561
+  }
a8a561
+
a8a561
+  gnome_vfs_module_callback_pop(GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION);
a8a561
+
a8a561
+  if (rv == GNOME_VFS_OK)
a8a561
+  {
a8a561
+    if (mHandle)
a8a561
+    {
a8a561
+      // Here we set the content type of the channel to the value of the mime
a8a561
+      // type determined by GnomeVFS.  However, if GnomeVFS is telling us that
a8a561
+      // the document is binary, we'll ignore that and keep the channel's
a8a561
+      // content type unspecified.  That will enable our content type sniffing
a8a561
+      // algorithms.  This should provide more consistent mime type handling.
a8a561
+
a8a561
+      if (info.mime_type && (strcmp(info.mime_type, APPLICATION_OCTET_STREAM) != 0))
a8a561
+        SetContentTypeOfChannel(info.mime_type);
a8a561
+
a8a561
+      mBytesRemaining = info.size;
a8a561
+
a8a561
+      // Update the content length attribute on the channel.  We do this
a8a561
+      // synchronously without proxying.  This hack is not as bad as it looks!
a8a561
+      if (mBytesRemaining > INT64_MAX) {
a8a561
+        mChannel->SetContentLength(-1);
a8a561
+      } else {
a8a561
+        mChannel->SetContentLength(mBytesRemaining);
a8a561
+      }
a8a561
+    }
a8a561
+    else
a8a561
+    {
a8a561
+      mDirOpen = true;
a8a561
+
a8a561
+      // Sort mDirList
a8a561
+      mDirList = g_list_sort(mDirList, FileInfoComparator);
a8a561
+      mDirListPtr = mDirList;
a8a561
+
a8a561
+      // Write base URL (make sure it ends with a '/')
a8a561
+      mDirBuf.AppendLiteral("300: ");
a8a561
+      mDirBuf.Append(mSpec);
a8a561
+      if (mSpec.get()[mSpec.Length() - 1] != '/')
a8a561
+        mDirBuf.Append('/');
a8a561
+      mDirBuf.Append('\n');
a8a561
+
a8a561
+      // Write column names
a8a561
+      mDirBuf.AppendLiteral("200: filename content-length last-modified file-type\n");
a8a561
+
a8a561
+      // Write charset (assume UTF-8)
a8a561
+      // XXX is this correct?
a8a561
+      mDirBuf.AppendLiteral("301: UTF-8\n");
a8a561
+
a8a561
+      SetContentTypeOfChannel(APPLICATION_HTTP_INDEX_FORMAT);
a8a561
+    }
a8a561
+  }
a8a561
+
a8a561
+  gnome_vfs_file_info_clear(&info;;
a8a561
+  return rv;
a8a561
+}
a8a561
+
a8a561
+GnomeVFSResult
a8a561
+nsGnomeVFSInputStream::DoRead(char *aBuf, uint32_t aCount, uint32_t *aCountRead)
a8a561
+{
a8a561
+  GnomeVFSResult rv;
a8a561
+
a8a561
+  if (mHandle)
a8a561
+  {
a8a561
+    GnomeVFSFileSize bytesRead;
a8a561
+    rv = gnome_vfs_read(mHandle, aBuf, aCount, &bytesRead);
a8a561
+    if (rv == GNOME_VFS_OK)
a8a561
+    {
a8a561
+      // aCount is 32-bit, so aCountRead is under 32-bit value.
a8a561
+      *aCountRead = (uint32_t) bytesRead;
a8a561
+      mBytesRemaining -= *aCountRead;
a8a561
+    }
a8a561
+  }
a8a561
+  else if (mDirOpen)
a8a561
+  {
a8a561
+    rv = GNOME_VFS_OK;
a8a561
+
a8a561
+    while (aCount && rv != GNOME_VFS_ERROR_EOF)
a8a561
+    {
a8a561
+      // Copy data out of our buffer
a8a561
+      uint32_t bufLen = mDirBuf.Length() - mDirBufCursor;
a8a561
+      if (bufLen)
a8a561
+      {
a8a561
+        uint32_t n = std::min(bufLen, aCount);
a8a561
+        memcpy(aBuf, mDirBuf.get() + mDirBufCursor, n);
a8a561
+        *aCountRead += n;
a8a561
+        aBuf += n;
a8a561
+        aCount -= n;
a8a561
+        mDirBufCursor += n;
a8a561
+      }
a8a561
+
a8a561
+      if (!mDirListPtr)    // Are we at the end of the directory list?
a8a561
+      {
a8a561
+        rv = GNOME_VFS_ERROR_EOF;
a8a561
+      }
a8a561
+      else if (aCount)     // Do we need more data?
a8a561
+      {
a8a561
+        GnomeVFSFileInfo *info = (GnomeVFSFileInfo *) mDirListPtr->data;
a8a561
+
a8a561
+        // Prune '.' and '..' from directory listing.
a8a561
+        if (info->name[0] == '.' &&
a8a561
+               (info->name[1] == '\0' ||
a8a561
+                   (info->name[1] == '.' && info->name[2] == '\0')))
a8a561
+        {
a8a561
+          mDirListPtr = mDirListPtr->next;
a8a561
+          continue;
a8a561
+        }
a8a561
+
a8a561
+        mDirBuf.AssignLiteral("201: ");
a8a561
+
a8a561
+        // The "filename" field
a8a561
+        nsCString escName;
a8a561
+        nsCOMPtr<nsINetUtil> nu = do_GetService(NS_NETUTIL_CONTRACTID);
a8a561
+        if (nu) {
a8a561
+          nu->EscapeString(nsDependentCString(info->name),
a8a561
+                           nsINetUtil::ESCAPE_URL_PATH, escName);
a8a561
+
a8a561
+          mDirBuf.Append(escName);
a8a561
+          mDirBuf.Append(' ');
a8a561
+        }
a8a561
+
a8a561
+        // The "content-length" field
a8a561
+        // XXX truncates size from 64-bit to 32-bit
a8a561
+        mDirBuf.AppendInt(int32_t(info->size));
a8a561
+        mDirBuf.Append(' ');
a8a561
+
a8a561
+        // The "last-modified" field
a8a561
+        // 
a8a561
+        // NSPR promises: PRTime is compatible with time_t
a8a561
+        // we just need to convert from seconds to microseconds
a8a561
+        PRExplodedTime tm;
a8a561
+        PRTime pt = ((PRTime) info->mtime) * 1000000;
a8a561
+        PR_ExplodeTime(pt, PR_GMTParameters, &tm;;
a8a561
+        {
a8a561
+          char buf[64];
a8a561
+          PR_FormatTimeUSEnglish(buf, sizeof(buf),
a8a561
+              "%a,%%20%d%%20%b%%20%Y%%20%H:%M:%S%%20GMT ", &tm;;
a8a561
+          mDirBuf.Append(buf);
a8a561
+        }
a8a561
+
a8a561
+        // The "file-type" field
a8a561
+        switch (info->type)
a8a561
+        {
a8a561
+          case GNOME_VFS_FILE_TYPE_REGULAR:
a8a561
+            mDirBuf.AppendLiteral("FILE ");
a8a561
+            break;
a8a561
+          case GNOME_VFS_FILE_TYPE_DIRECTORY:
a8a561
+            mDirBuf.AppendLiteral("DIRECTORY ");
a8a561
+            break;
a8a561
+          case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK:
a8a561
+            mDirBuf.AppendLiteral("SYMBOLIC-LINK ");
a8a561
+            break;
a8a561
+          default:
a8a561
+            break;
a8a561
+        }
a8a561
+
a8a561
+        mDirBuf.Append('\n');
a8a561
+
a8a561
+        mDirBufCursor = 0;
a8a561
+        mDirListPtr = mDirListPtr->next;
a8a561
+      }
a8a561
+    }
a8a561
+  }
a8a561
+  else
a8a561
+  {
a8a561
+    NS_NOTREACHED("reading from what?");
a8a561
+    rv = GNOME_VFS_ERROR_GENERIC;
a8a561
+  }
a8a561
+
a8a561
+  return rv;
a8a561
+}
a8a561
+
a8a561
+// This class is used to implement SetContentTypeOfChannel.
a8a561
+class nsGnomeVFSSetContentTypeEvent : public nsRunnable
a8a561
+{
a8a561
+  public:
a8a561
+    nsGnomeVFSSetContentTypeEvent(nsIChannel *channel, const char *contentType)
a8a561
+      : mChannel(channel), mContentType(contentType)
a8a561
+    {
a8a561
+      // stash channel reference in mChannel.  no AddRef here!  see note
a8a561
+      // in SetContentTypeOfchannel.
a8a561
+    }
a8a561
+
a8a561
+    NS_IMETHOD Run()
a8a561
+    {
a8a561
+      mChannel->SetContentType(mContentType);
a8a561
+      return NS_OK;
a8a561
+    }
a8a561
+
a8a561
+  private: 
a8a561
+    nsIChannel *mChannel;
a8a561
+    nsCString   mContentType;
a8a561
+};
a8a561
+
a8a561
+nsresult
a8a561
+nsGnomeVFSInputStream::SetContentTypeOfChannel(const char *contentType)
a8a561
+{
a8a561
+  // We need to proxy this call over to the main thread.  We post an
a8a561
+  // asynchronous event in this case so that we don't delay reading data, and
a8a561
+  // we know that this is safe to do since the channel's reference will be
a8a561
+  // released asynchronously as well.  We trust the ordering of the main
a8a561
+  // thread's event queue to protect us against memory corruption.
a8a561
+
a8a561
+  nsresult rv;
a8a561
+  nsCOMPtr<nsIRunnable> ev =
a8a561
+      new nsGnomeVFSSetContentTypeEvent(mChannel, contentType);
a8a561
+  if (!ev)
a8a561
+  {
a8a561
+    rv = NS_ERROR_OUT_OF_MEMORY;
a8a561
+  }
a8a561
+  else
a8a561
+  {
a8a561
+    rv = NS_DispatchToMainThread(ev);
a8a561
+  }
a8a561
+  return rv;
a8a561
+}
a8a561
+
a8a561
+NS_IMPL_ISUPPORTS(nsGnomeVFSInputStream, nsIInputStream)
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSInputStream::Close()
a8a561
+{
a8a561
+  if (mHandle)
a8a561
+  {
a8a561
+    gnome_vfs_close(mHandle);
a8a561
+    mHandle = nullptr;
a8a561
+  }
a8a561
+
a8a561
+  if (mDirList)
a8a561
+  {
a8a561
+    // Destroy the list of GnomeVFSFileInfo objects...
a8a561
+    g_list_foreach(mDirList, (GFunc) gnome_vfs_file_info_unref, nullptr);
a8a561
+    g_list_free(mDirList);
a8a561
+    mDirList = nullptr;
a8a561
+    mDirListPtr = nullptr;
a8a561
+  }
a8a561
+
a8a561
+  if (mChannel)
a8a561
+  {
a8a561
+    nsresult rv = NS_OK;
a8a561
+
a8a561
+    nsCOMPtr<nsIThread> thread = do_GetMainThread();
a8a561
+    if (thread)
a8a561
+      rv = NS_ProxyRelease(thread, mChannel);
a8a561
+
a8a561
+    NS_ASSERTION(thread && NS_SUCCEEDED(rv), "leaking channel reference");
a8a561
+    mChannel = nullptr;
a8a561
+  }
a8a561
+
a8a561
+  mSpec.Truncate(); // free memory
a8a561
+
a8a561
+  // Prevent future reads from re-opening the handle.
a8a561
+  if (NS_SUCCEEDED(mStatus))
a8a561
+    mStatus = NS_BASE_STREAM_CLOSED;
a8a561
+
a8a561
+  return NS_OK;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSInputStream::Available(uint64_t *aResult)
a8a561
+{
a8a561
+  if (NS_FAILED(mStatus))
a8a561
+    return mStatus;
a8a561
+
a8a561
+  *aResult = mBytesRemaining;
a8a561
+  return NS_OK;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSInputStream::Read(char *aBuf,
a8a561
+                            uint32_t aCount,
a8a561
+                            uint32_t *aCountRead)
a8a561
+{
a8a561
+  *aCountRead = 0;
a8a561
+
a8a561
+  if (mStatus == NS_BASE_STREAM_CLOSED)
a8a561
+    return NS_OK;
a8a561
+  if (NS_FAILED(mStatus))
a8a561
+    return mStatus;
a8a561
+
a8a561
+  GnomeVFSResult rv = GNOME_VFS_OK;
a8a561
+
a8a561
+  // If this is our first-time through here, then open the URI.
a8a561
+  if (!mHandle && !mDirOpen)
a8a561
+    rv = DoOpen();
a8a561
+  
a8a561
+  if (rv == GNOME_VFS_OK)
a8a561
+    rv = DoRead(aBuf, aCount, aCountRead);
a8a561
+
a8a561
+  if (rv != GNOME_VFS_OK)
a8a561
+  {
a8a561
+    // If we reach here, we hit some kind of error.  EOF is not an error.
a8a561
+    mStatus = MapGnomeVFSResult(rv);
a8a561
+    if (mStatus == NS_BASE_STREAM_CLOSED)
a8a561
+      return NS_OK;
a8a561
+
a8a561
+    LOG(("gnomevfs: result %d [%s] mapped to 0x%x\n",
a8a561
+        rv, gnome_vfs_result_to_string(rv), mStatus));
a8a561
+  }
a8a561
+  return mStatus;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSInputStream::ReadSegments(nsWriteSegmentFun aWriter,
a8a561
+                                    void *aClosure,
a8a561
+                                    uint32_t aCount,
a8a561
+                                    uint32_t *aResult)
a8a561
+{
a8a561
+  // There is no way to implement this using GnomeVFS, but fortunately
a8a561
+  // that doesn't matter.  Because we are a blocking input stream, Necko
a8a561
+  // isn't going to call our ReadSegments method.
a8a561
+  NS_NOTREACHED("nsGnomeVFSInputStream::ReadSegments");
a8a561
+  return NS_ERROR_NOT_IMPLEMENTED;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSInputStream::IsNonBlocking(bool *aResult)
a8a561
+{
a8a561
+  *aResult = false;
a8a561
+  return NS_OK;
a8a561
+}
a8a561
+
a8a561
+//-----------------------------------------------------------------------------
a8a561
+
a8a561
+class nsGnomeVFSProtocolHandler : public nsIProtocolHandler
a8a561
+                                , public nsIObserver
a8a561
+{
a8a561
+  public:
a8a561
+    NS_DECL_ISUPPORTS
a8a561
+    NS_DECL_NSIPROTOCOLHANDLER
a8a561
+    NS_DECL_NSIOBSERVER
a8a561
+
a8a561
+    nsresult Init();
a8a561
+
a8a561
+  private:
a8a561
+    void   InitSupportedProtocolsPref(nsIPrefBranch *prefs);
a8a561
+    bool IsSupportedProtocol(const nsCString &spec);
a8a561
+
a8a561
+    nsCString mSupportedProtocols;
a8a561
+};
a8a561
+
a8a561
+NS_IMPL_ISUPPORTS(nsGnomeVFSProtocolHandler, nsIProtocolHandler, nsIObserver)
a8a561
+
a8a561
+nsresult
a8a561
+nsGnomeVFSProtocolHandler::Init()
a8a561
+{
a8a561
+#ifdef PR_LOGGING
a8a561
+  sGnomeVFSLog = PR_NewLogModule("gnomevfs");
a8a561
+#endif
a8a561
+
a8a561
+  if (!gnome_vfs_initialized())
a8a561
+  {
a8a561
+    if (!gnome_vfs_init())
a8a561
+    {
a8a561
+      NS_WARNING("gnome_vfs_init failed");
a8a561
+      return NS_ERROR_UNEXPECTED;
a8a561
+    }
a8a561
+  }
a8a561
+
a8a561
+  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
a8a561
+  if (prefs)
a8a561
+  {
a8a561
+    InitSupportedProtocolsPref(prefs);
a8a561
+    prefs->AddObserver(MOZ_GNOMEVFS_SUPPORTED_PROTOCOLS, this, false);
a8a561
+  }
a8a561
+
a8a561
+  return NS_OK;
a8a561
+}
a8a561
+
a8a561
+void
a8a561
+nsGnomeVFSProtocolHandler::InitSupportedProtocolsPref(nsIPrefBranch *prefs)
a8a561
+{
a8a561
+  // read preferences
a8a561
+  nsresult rv = prefs->GetCharPref(MOZ_GNOMEVFS_SUPPORTED_PROTOCOLS,
a8a561
+                                   getter_Copies(mSupportedProtocols));
a8a561
+  if (NS_SUCCEEDED(rv)) {
a8a561
+    mSupportedProtocols.StripWhitespace();
a8a561
+    ToLowerCase(mSupportedProtocols);
a8a561
+  }
a8a561
+  else
a8a561
+    mSupportedProtocols.AssignLiteral("smb:,sftp:"); // use defaults
a8a561
+
a8a561
+  LOG(("gnomevfs: supported protocols \"%s\"\n", mSupportedProtocols.get()));
a8a561
+}
a8a561
+
a8a561
+bool
a8a561
+nsGnomeVFSProtocolHandler::IsSupportedProtocol(const nsCString &aSpec)
a8a561
+{
a8a561
+  const char *specString = aSpec.get();
a8a561
+  const char *colon = strchr(specString, ':');
a8a561
+  if (!colon)
a8a561
+    return false;
a8a561
+
a8a561
+  uint32_t length = colon - specString + 1;
a8a561
+
a8a561
+  // <scheme> + ':'
a8a561
+  nsCString scheme(specString, length);
a8a561
+
a8a561
+  char *found = PL_strcasestr(mSupportedProtocols.get(), scheme.get());
a8a561
+  if (!found)
a8a561
+    return false;
a8a561
+
a8a561
+  if (found[length] != ',' && found[length] != '\0')
a8a561
+    return false;
a8a561
+
a8a561
+  return true;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSProtocolHandler::GetScheme(nsACString &aScheme)
a8a561
+{
a8a561
+  aScheme.Assign(MOZ_GNOMEVFS_SCHEME);
a8a561
+  return NS_OK;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSProtocolHandler::GetDefaultPort(int32_t *aDefaultPort)
a8a561
+{
a8a561
+  *aDefaultPort = -1;
a8a561
+  return NS_OK;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSProtocolHandler::GetProtocolFlags(uint32_t *aProtocolFlags)
a8a561
+{
a8a561
+  // Is URI_STD true of all GnomeVFS URI types?
a8a561
+  *aProtocolFlags = URI_STD | URI_DANGEROUS_TO_LOAD;
a8a561
+  return NS_OK;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSProtocolHandler::NewURI(const nsACString &aSpec,
a8a561
+                                  const char *aOriginCharset,
a8a561
+                                  nsIURI *aBaseURI,
a8a561
+                                  nsIURI **aResult)
a8a561
+{
a8a561
+  const nsCString flatSpec(aSpec);
a8a561
+  LOG(("gnomevfs: NewURI [spec=%s]\n", flatSpec.get()));
a8a561
+
a8a561
+  if (!aBaseURI)
a8a561
+  {
a8a561
+    //
a8a561
+    // XXX This check is used to limit the gnome-vfs protocols we support.  For
a8a561
+    //     security reasons, it is best that we limit the protocols we support to
a8a561
+    //     those with known characteristics.  We might want to lessen this
a8a561
+    //     restriction if it proves to be too heavy handed.  A black list of
a8a561
+    //     protocols we don't want to support might be better.  For example, we
a8a561
+    //     probably don't want to try to load "start-here:" inside the browser.
a8a561
+    //     There are others that fall into this category, which are best handled
a8a561
+    //     externally by Nautilus (or another app like it).
a8a561
+    //
a8a561
+    if (!IsSupportedProtocol(flatSpec))
a8a561
+      return NS_ERROR_UNKNOWN_PROTOCOL;
a8a561
+
a8a561
+    // Verify that GnomeVFS supports this URI scheme.
a8a561
+    GnomeVFSURI *uri = gnome_vfs_uri_new(flatSpec.get());
a8a561
+    if (!uri)
a8a561
+      return NS_ERROR_UNKNOWN_PROTOCOL;
a8a561
+  }
a8a561
+
a8a561
+  //
a8a561
+  // XXX Can we really assume that all gnome-vfs URIs can be parsed using
a8a561
+  //     nsStandardURL?  We probably really need to implement nsIURI/nsIURL
a8a561
+  //     in terms of the gnome_vfs_uri_XXX methods, but at least this works
a8a561
+  //     correctly for smb:// URLs ;-)
a8a561
+  //
a8a561
+  //     Also, it might not be possible to fully implement nsIURI/nsIURL in
a8a561
+  //     terms of GnomeVFSURI since some Necko methods have no GnomeVFS
a8a561
+  //     equivalent.
a8a561
+  //
a8a561
+  nsresult rv;
a8a561
+  nsCOMPtr<nsIStandardURL> url =
a8a561
+      do_CreateInstance(NS_STANDARDURL_CONTRACTID, &rv;;
a8a561
+  if (NS_FAILED(rv))
a8a561
+    return rv;
a8a561
+
a8a561
+  rv = url->Init(nsIStandardURL::URLTYPE_STANDARD, -1, flatSpec,
a8a561
+                 aOriginCharset, aBaseURI);
a8a561
+  if (NS_SUCCEEDED(rv))
a8a561
+    rv = CallQueryInterface(url, aResult);
a8a561
+
a8a561
+  return rv;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSProtocolHandler::NewChannel2(nsIURI* aURI,
a8a561
+                                       nsILoadInfo* aLoadInfo,
a8a561
+                                       nsIChannel** aResult)
a8a561
+{
a8a561
+  NS_ENSURE_ARG_POINTER(aURI);
a8a561
+  nsresult rv;
a8a561
+
a8a561
+  nsAutoCString spec;
a8a561
+  rv = aURI->GetSpec(spec);
a8a561
+  if (NS_FAILED(rv))
a8a561
+    return rv;
a8a561
+
a8a561
+  nsRefPtr<nsGnomeVFSInputStream> stream = new nsGnomeVFSInputStream(spec);
a8a561
+  if (!stream)
a8a561
+  {
a8a561
+    rv = NS_ERROR_OUT_OF_MEMORY;
a8a561
+  }
a8a561
+  else
a8a561
+  {
a8a561
+    // start out assuming an unknown content-type.  we'll set the content-type
a8a561
+    // to something better once we open the URI.
a8a561
+    rv = NS_NewInputStreamChannel(aResult, aURI, stream,
a8a561
+                                  NS_LITERAL_CSTRING(UNKNOWN_CONTENT_TYPE));
a8a561
+    if (NS_SUCCEEDED(rv))
a8a561
+      stream->SetChannel(*aResult);
a8a561
+  }
a8a561
+  return rv;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSProtocolHandler::NewChannel(nsIURI *aURI, nsIChannel **aResult)
a8a561
+{
a8a561
+    return NewChannel2(aURI, nullptr, aResult);
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSProtocolHandler::AllowPort(int32_t aPort,
a8a561
+                                     const char *aScheme,
a8a561
+                                     bool *aResult)
a8a561
+{
a8a561
+  // Don't override anything.
a8a561
+  *aResult = false; 
a8a561
+  return NS_OK;
a8a561
+}
a8a561
+
a8a561
+NS_IMETHODIMP
a8a561
+nsGnomeVFSProtocolHandler::Observe(nsISupports *aSubject,
a8a561
+                                   const char *aTopic,
a8a561
+                                   const char16_t *aData)
a8a561
+{
a8a561
+  if (strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0) {
a8a561
+    nsCOMPtr<nsIPrefBranch> prefs = do_QueryInterface(aSubject);
a8a561
+    InitSupportedProtocolsPref(prefs);
a8a561
+  }
a8a561
+  return NS_OK;
a8a561
+}
a8a561
+
a8a561
+//-----------------------------------------------------------------------------
a8a561
+
a8a561
+#define NS_GNOMEVFSPROTOCOLHANDLER_CID               \
a8a561
+{ /* 9b6dc177-a2e4-49e1-9c98-0a8384de7f6c */         \
a8a561
+    0x9b6dc177,                                      \
a8a561
+    0xa2e4,                                          \
a8a561
+    0x49e1,                                          \
a8a561
+    {0x9c, 0x98, 0x0a, 0x83, 0x84, 0xde, 0x7f, 0x6c} \
a8a561
+}
a8a561
+
a8a561
+NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsGnomeVFSProtocolHandler, Init)
a8a561
+NS_DEFINE_NAMED_CID(NS_GNOMEVFSPROTOCOLHANDLER_CID);
a8a561
+
a8a561
+static const mozilla::Module::CIDEntry kVFSCIDs[] = {
a8a561
+  { &kNS_GNOMEVFSPROTOCOLHANDLER_CID, false, nullptr, nsGnomeVFSProtocolHandlerConstructor },
a8a561
+  { nullptr }
a8a561
+};
a8a561
+
a8a561
+static const mozilla::Module::ContractIDEntry kVFSContracts[] = {
a8a561
+  { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX MOZ_GNOMEVFS_SCHEME, &kNS_GNOMEVFSPROTOCOLHANDLER_CID },
a8a561
+  { nullptr }
a8a561
+};
a8a561
+
a8a561
+static const mozilla::Module kVFSModule = {
a8a561
+  mozilla::Module::kVersion,
a8a561
+  kVFSCIDs,
a8a561
+  kVFSContracts
a8a561
+};
a8a561
+
a8a561
+NSMODULE_DEFN(nsGnomeVFSModule) = &kVFSModule;
a8a561
diff -up thunderbird-45.0/mozilla/mobile/android/installer/package-manifest.in.moz-1134537-gnomevfsservice thunderbird-45.0/mozilla/mobile/android/installer/package-manifest.in
a8a561
--- thunderbird-45.0/mozilla/mobile/android/installer/package-manifest.in.moz-1134537-gnomevfsservice	2016-05-09 13:19:52.322048300 +0200
a8a561
+++ thunderbird-45.0/mozilla/mobile/android/installer/package-manifest.in	2016-05-09 13:25:02.680824572 +0200
a8a561
@@ -613,6 +613,12 @@ bin/libfreebl_32int64_3.so
a8a561
 @BINPATH@/crashreporter-override.ini
a8a561
 #endif
a8a561
 
a8a561
+; [Extensions]
a8a561
+;
a8a561
+#ifdef MOZ_ENABLE_GNOMEVFS
a8a561
+bin/components/@DLL_PREFIX@nkgnomevfs@DLL_SUFFIX@
a8a561
+#endif
a8a561
+
a8a561
 [mobile]
a8a561
 @BINPATH@/chrome/icons/
a8a561
 @BINPATH@/chrome/chrome@JAREXT@