Skip to content

Commit

Permalink
Bug 694870 - Only support GIO in Linux file and protocol handler. r=r…
Browse files Browse the repository at this point in the history
…oc, r=karlt
  • Loading branch information
alexhenrie committed Feb 16, 2015
1 parent 2947b8b commit 746235e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 167 deletions.
132 changes: 33 additions & 99 deletions uriloader/exthandler/unix/nsGNOMERegistry.cpp
Expand Up @@ -6,97 +6,50 @@
#include "nsGNOMERegistry.h"
#include "nsString.h"
#include "nsIComponentManager.h"
#include "nsIFile.h"
#include "nsMIMEInfoUnix.h"
#include "nsAutoPtr.h"
#include "nsIGConfService.h"
#include "nsIGnomeVFSService.h"
#include "nsIGIOService.h"

#ifdef MOZ_WIDGET_GTK
#include <glib.h>
#include <glib-object.h>
#endif

/* static */ bool
nsGNOMERegistry::HandlerExists(const char *aProtocolScheme)
{
nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
if (giovfs) {
nsCOMPtr<nsIGIOMimeApp> app;
if (NS_FAILED(giovfs->GetAppForURIScheme(nsDependentCString(aProtocolScheme),
getter_AddRefs(app))))
return false;
else
return true;
} else if (gconf) {
bool isEnabled;
nsAutoCString handler;
if (NS_FAILED(gconf->GetAppForProtocol(nsDependentCString(aProtocolScheme), &isEnabled, handler)))
return false;

return isEnabled;
if (!giovfs) {
return false;
}

return false;
nsCOMPtr<nsIGIOMimeApp> app;
return NS_SUCCEEDED(giovfs->GetAppForURIScheme(nsDependentCString(aProtocolScheme),
getter_AddRefs(app)));
}

// XXX Check HandlerExists() before calling LoadURL.
//
// If there is not a registered handler for the protocol, gnome_url_show()
// falls back to using gnomevfs modules. See bug 389632. We don't want
// this fallback to happen as we are not sure of the safety of all gnomevfs
// modules and MIME-default applications. (gnomevfs should be handled in
// nsGnomeVFSProtocolHandler.)

/* static */ nsresult
nsGNOMERegistry::LoadURL(nsIURI *aURL)
{
nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
if (giovfs)
return giovfs->ShowURI(aURL);

nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
if (gnomevfs)
return gnomevfs->ShowURI(aURL);
if (!giovfs) {
return NS_ERROR_FAILURE;
}

return NS_ERROR_FAILURE;
return giovfs->ShowURI(aURL);
}

/* static */ void
nsGNOMERegistry::GetAppDescForScheme(const nsACString& aScheme,
nsAString& aDesc)
{
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
if (!gconf && !giovfs)
if (!giovfs)
return;

nsAutoCString name;
if (giovfs) {
nsCOMPtr<nsIGIOMimeApp> app;
if (NS_FAILED(giovfs->GetAppForURIScheme(aScheme, getter_AddRefs(app))))
return;

app->GetName(name);
} else {
bool isEnabled;
if (NS_FAILED(gconf->GetAppForProtocol(aScheme, &isEnabled, name)))
return;

if (!name.IsEmpty()) {
// Try to only provide the executable name, as it is much simpler than with the path and arguments
int32_t firstSpace = name.FindChar(' ');
if (firstSpace != kNotFound) {
name.Truncate(firstSpace);
int32_t lastSlash = name.RFindChar('/');
if (lastSlash != kNotFound) {
name.Cut(0, lastSlash + 1);
}
}
}
}
nsCOMPtr<nsIGIOMimeApp> app;
if (NS_FAILED(giovfs->GetAppForURIScheme(aScheme, getter_AddRefs(app))))
return;

app->GetName(name);

CopyUTF8toUTF16(name, aDesc);
}
Expand All @@ -107,23 +60,15 @@ nsGNOMERegistry::GetFromExtension(const nsACString& aFileExt)
{
nsAutoCString mimeType;
nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
if (!giovfs) {
return nullptr;
}

if (giovfs) {
// Get the MIME type from the extension, then call GetFromType to
// fill in the MIMEInfo.
if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
mimeType.EqualsLiteral("application/octet-stream")) {
return nullptr;
}
} else {
/* Fallback to GnomeVFS */
nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
if (!gnomevfs)
return nullptr;

if (NS_FAILED(gnomevfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
mimeType.EqualsLiteral("application/octet-stream"))
return nullptr;
// Get the MIME type from the extension, then call GetFromType to
// fill in the MIMEInfo.
if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
mimeType.EqualsLiteral("application/octet-stream")) {
return nullptr;
}

nsRefPtr<nsMIMEInfoBase> mi = GetFromType(mimeType);
Expand All @@ -144,28 +89,17 @@ nsGNOMERegistry::GetFromType(const nsACString& aMIMEType)
nsAutoCString description;

nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
if (giovfs) {
nsCOMPtr<nsIGIOMimeApp> gioHandlerApp;
if (NS_FAILED(giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gioHandlerApp))) ||
!gioHandlerApp) {
return nullptr;
}
gioHandlerApp->GetName(name);
giovfs->GetDescriptionForMimeType(aMIMEType, description);
} else {
/* Fallback to GnomeVFS*/
nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
if (!gnomevfs)
return nullptr;

nsCOMPtr<nsIGnomeVFSMimeApp> gnomeHandlerApp;
if (NS_FAILED(gnomevfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gnomeHandlerApp))) ||
!gnomeHandlerApp) {
return nullptr;
}
gnomeHandlerApp->GetName(name);
gnomevfs->GetDescriptionForMimeType(aMIMEType, description);
if (!giovfs) {
return nullptr;
}

nsCOMPtr<nsIGIOMimeApp> gioHandlerApp;
if (NS_FAILED(giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gioHandlerApp))) ||
!gioHandlerApp) {
return nullptr;
}
gioHandlerApp->GetName(name);
giovfs->GetDescriptionForMimeType(aMIMEType, description);

mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(name));
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
Expand Down
58 changes: 16 additions & 42 deletions uriloader/exthandler/unix/nsMIMEInfoUnix.cpp
Expand Up @@ -16,7 +16,6 @@
#include "nsIGIOService.h"
#include "nsNetCID.h"
#include "nsIIOService.h"
#include "nsIGnomeVFSService.h"
#include "nsAutoPtr.h"
#ifdef MOZ_ENABLE_DBUS
#include "nsDBusHandlerApp.h"
Expand Down Expand Up @@ -50,7 +49,7 @@ nsMIMEInfoUnix::GetHasDefaultHandler(bool *_retval)

*_retval = false;

if (mClass == eProtocolInfo) {
if (mClass == eProtocolInfo) {
*_retval = nsGNOMERegistry::HandlerExists(mSchemeOrType.get());
} else {
nsRefPtr<nsMIMEInfoBase> mimeInfo = nsGNOMERegistry::GetFromType(mSchemeOrType);
Expand Down Expand Up @@ -104,51 +103,26 @@ nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile *aFile)
#endif

nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
nsAutoCString uriSpec;
if (giovfs) {
// nsGIOMimeApp->Launch wants a URI string instead of local file
nsresult rv;
nsCOMPtr<nsIIOService> ioservice = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> uri;
rv = ioservice->NewFileURI(aFile, getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
uri->GetSpec(uriSpec);
}

nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
if (giovfs) {
nsCOMPtr<nsIGIOMimeApp> app;
if (NS_SUCCEEDED(giovfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app)
return app->Launch(uriSpec);
} else if (gnomevfs) {
/* Fallback to GnomeVFS */
nsCOMPtr<nsIGnomeVFSMimeApp> app;
if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app)
return app->Launch(nativePath);
if (!giovfs) {
return NS_ERROR_FAILURE;
}

// If we haven't got an app we try to get a valid one by searching for the
// extension mapped type
nsRefPtr<nsMIMEInfoBase> mimeInfo = nsGNOMERegistry::GetFromExtension(nativePath);
if (mimeInfo) {
nsAutoCString type;
mimeInfo->GetType(type);
if (giovfs) {
nsCOMPtr<nsIGIOMimeApp> app;
if (NS_SUCCEEDED(giovfs->GetAppForMimeType(type, getter_AddRefs(app))) && app)
return app->Launch(uriSpec);
} else if (gnomevfs) {
nsCOMPtr<nsIGnomeVFSMimeApp> app;
if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(type, getter_AddRefs(app))) && app)
return app->Launch(nativePath);
}
}
// nsGIOMimeApp->Launch wants a URI string instead of local file
nsresult rv;
nsCOMPtr<nsIIOService> ioservice = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> uri;
rv = ioservice->NewFileURI(aFile, getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString uriSpec;
uri->GetSpec(uriSpec);

if (!mDefaultApplication)
nsCOMPtr<nsIGIOMimeApp> app;
if (NS_FAILED(giovfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) || !app) {
return NS_ERROR_FILE_NOT_FOUND;
}

return LaunchWithIProcess(mDefaultApplication, nativePath);
return app->Launch(uriSpec);
}

#if defined(MOZ_ENABLE_CONTENTACTION)
Expand Down
36 changes: 10 additions & 26 deletions uriloader/exthandler/unix/nsOSHelperAppService.cpp
Expand Up @@ -1150,7 +1150,7 @@ nsresult nsOSHelperAppService::OSProtocolHandlerExists(const char * aProtocolSch
#endif

#ifdef MOZ_WIDGET_GTK
// Check the GConf registry for a protocol handler
// Check the GNOME registry for a protocol handler
*aHandlerExists = nsGNOMERegistry::HandlerExists(aProtocolScheme);
#endif

Expand Down Expand Up @@ -1369,22 +1369,6 @@ nsOSHelperAppService::GetFromType(const nsCString& aMIMEType) {
NS_LossyConvertUTF16toASCII(handler).get(),
NS_LossyConvertUTF16toASCII(mailcap_description).get()));

#ifdef MOZ_WIDGET_GTK
nsRefPtr<nsMIMEInfoBase> gnomeInfo;
if (handler.IsEmpty()) {
// No useful data yet. Check the GNOME registry. Unfortunately, newer
// GNOME versions no longer have type-to-extension mappings, so we might
// get back a MIMEInfo without any extensions set. In that case we'll have
// to look in our mime.types files for the extensions.
LOG(("Looking in GNOME registry\n"));
gnomeInfo = nsGNOMERegistry::GetFromType(aMIMEType);
if (gnomeInfo && gnomeInfo->HasExtensions()) {
LOG(("Got MIMEInfo from GNOME registry, and it has extensions set\n"));
return gnomeInfo.forget();
}
}
#endif

// Now look up our extensions
nsAutoString extensions, mime_types_description;
LookUpExtensionsAndDescription(majorType,
Expand All @@ -1393,13 +1377,16 @@ nsOSHelperAppService::GetFromType(const nsCString& aMIMEType) {
mime_types_description);

#ifdef MOZ_WIDGET_GTK
if (gnomeInfo) {
LOG(("Got MIMEInfo from GNOME registry without extensions; setting them "
"to %s\n", NS_LossyConvertUTF16toASCII(extensions).get()));
if (handler.IsEmpty()) {
nsRefPtr<nsMIMEInfoBase> gnomeInfo = nsGNOMERegistry::GetFromType(aMIMEType);
if (gnomeInfo) {
LOG(("Got MIMEInfo from GNOME registry without extensions; setting them "
"to %s\n", NS_LossyConvertUTF16toASCII(extensions).get()));

NS_ASSERTION(!gnomeInfo->HasExtensions(), "How'd that happen?");
gnomeInfo->SetFileExtensions(NS_ConvertUTF16toUTF8(extensions));
return gnomeInfo.forget();
NS_ASSERTION(!gnomeInfo->HasExtensions(), "How'd that happen?");
gnomeInfo->SetFileExtensions(NS_ConvertUTF16toUTF8(extensions));
return gnomeInfo.forget();
}
}
#endif

Expand Down Expand Up @@ -1526,9 +1513,6 @@ nsOSHelperAppService::GetProtocolHandlerInfoFromOS(const nsACString &aScheme,
{
NS_ASSERTION(!aScheme.IsEmpty(), "No scheme was specified!");

// We must check that a registered handler exists so that gnome_url_show
// doesn't fallback to gnomevfs.
// See nsGNOMERegistry::LoadURL and bug 389632.
nsresult rv = OSProtocolHandlerExists(nsPromiseFlatCString(aScheme).get(),
found);
if (NS_FAILED(rv))
Expand Down

0 comments on commit 746235e

Please sign in to comment.