1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/uriloader/exthandler/unix/nsGNOMERegistry.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,175 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsGNOMERegistry.h" 1.10 +#include "nsString.h" 1.11 +#include "nsIComponentManager.h" 1.12 +#include "nsIFile.h" 1.13 +#include "nsMIMEInfoUnix.h" 1.14 +#include "nsAutoPtr.h" 1.15 +#include "nsIGConfService.h" 1.16 +#include "nsIGnomeVFSService.h" 1.17 +#include "nsIGIOService.h" 1.18 + 1.19 +#ifdef MOZ_WIDGET_GTK 1.20 +#include <glib.h> 1.21 +#include <glib-object.h> 1.22 +#endif 1.23 + 1.24 +/* static */ bool 1.25 +nsGNOMERegistry::HandlerExists(const char *aProtocolScheme) 1.26 +{ 1.27 + nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); 1.28 + nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); 1.29 + if (giovfs) { 1.30 + nsCOMPtr<nsIGIOMimeApp> app; 1.31 + if (NS_FAILED(giovfs->GetAppForURIScheme(nsDependentCString(aProtocolScheme), 1.32 + getter_AddRefs(app)))) 1.33 + return false; 1.34 + else 1.35 + return true; 1.36 + } else if (gconf) { 1.37 + bool isEnabled; 1.38 + nsAutoCString handler; 1.39 + if (NS_FAILED(gconf->GetAppForProtocol(nsDependentCString(aProtocolScheme), &isEnabled, handler))) 1.40 + return false; 1.41 + 1.42 + return isEnabled; 1.43 + } 1.44 + 1.45 + return false; 1.46 +} 1.47 + 1.48 +// XXX Check HandlerExists() before calling LoadURL. 1.49 +// 1.50 +// If there is not a registered handler for the protocol, gnome_url_show() 1.51 +// falls back to using gnomevfs modules. See bug 389632. We don't want 1.52 +// this fallback to happen as we are not sure of the safety of all gnomevfs 1.53 +// modules and MIME-default applications. (gnomevfs should be handled in 1.54 +// nsGnomeVFSProtocolHandler.) 1.55 + 1.56 +/* static */ nsresult 1.57 +nsGNOMERegistry::LoadURL(nsIURI *aURL) 1.58 +{ 1.59 + nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); 1.60 + if (giovfs) 1.61 + return giovfs->ShowURI(aURL); 1.62 + 1.63 + nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID); 1.64 + if (gnomevfs) 1.65 + return gnomevfs->ShowURI(aURL); 1.66 + 1.67 + return NS_ERROR_FAILURE; 1.68 +} 1.69 + 1.70 +/* static */ void 1.71 +nsGNOMERegistry::GetAppDescForScheme(const nsACString& aScheme, 1.72 + nsAString& aDesc) 1.73 +{ 1.74 + nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); 1.75 + nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); 1.76 + if (!gconf && !giovfs) 1.77 + return; 1.78 + 1.79 + nsAutoCString name; 1.80 + if (giovfs) { 1.81 + nsCOMPtr<nsIGIOMimeApp> app; 1.82 + if (NS_FAILED(giovfs->GetAppForURIScheme(aScheme, getter_AddRefs(app)))) 1.83 + return; 1.84 + 1.85 + app->GetName(name); 1.86 + } else { 1.87 + bool isEnabled; 1.88 + if (NS_FAILED(gconf->GetAppForProtocol(aScheme, &isEnabled, name))) 1.89 + return; 1.90 + 1.91 + if (!name.IsEmpty()) { 1.92 + // Try to only provide the executable name, as it is much simpler than with the path and arguments 1.93 + int32_t firstSpace = name.FindChar(' '); 1.94 + if (firstSpace != kNotFound) { 1.95 + name.Truncate(firstSpace); 1.96 + int32_t lastSlash = name.RFindChar('/'); 1.97 + if (lastSlash != kNotFound) { 1.98 + name.Cut(0, lastSlash + 1); 1.99 + } 1.100 + } 1.101 + } 1.102 + } 1.103 + 1.104 + CopyUTF8toUTF16(name, aDesc); 1.105 +} 1.106 + 1.107 + 1.108 +/* static */ already_AddRefed<nsMIMEInfoBase> 1.109 +nsGNOMERegistry::GetFromExtension(const nsACString& aFileExt) 1.110 +{ 1.111 + nsAutoCString mimeType; 1.112 + nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); 1.113 + 1.114 + if (giovfs) { 1.115 + // Get the MIME type from the extension, then call GetFromType to 1.116 + // fill in the MIMEInfo. 1.117 + if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) || 1.118 + mimeType.EqualsLiteral("application/octet-stream")) { 1.119 + return nullptr; 1.120 + } 1.121 + } else { 1.122 + /* Fallback to GnomeVFS */ 1.123 + nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID); 1.124 + if (!gnomevfs) 1.125 + return nullptr; 1.126 + 1.127 + if (NS_FAILED(gnomevfs->GetMimeTypeFromExtension(aFileExt, mimeType)) || 1.128 + mimeType.EqualsLiteral("application/octet-stream")) 1.129 + return nullptr; 1.130 + } 1.131 + 1.132 + nsRefPtr<nsMIMEInfoBase> mi = GetFromType(mimeType); 1.133 + if (mi) { 1.134 + mi->AppendExtension(aFileExt); 1.135 + } 1.136 + 1.137 + return mi.forget(); 1.138 +} 1.139 + 1.140 +/* static */ already_AddRefed<nsMIMEInfoBase> 1.141 +nsGNOMERegistry::GetFromType(const nsACString& aMIMEType) 1.142 +{ 1.143 + nsRefPtr<nsMIMEInfoUnix> mimeInfo = new nsMIMEInfoUnix(aMIMEType); 1.144 + NS_ENSURE_TRUE(mimeInfo, nullptr); 1.145 + 1.146 + nsAutoCString name; 1.147 + nsAutoCString description; 1.148 + 1.149 + nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); 1.150 + if (giovfs) { 1.151 + nsCOMPtr<nsIGIOMimeApp> gioHandlerApp; 1.152 + if (NS_FAILED(giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gioHandlerApp))) || 1.153 + !gioHandlerApp) { 1.154 + return nullptr; 1.155 + } 1.156 + gioHandlerApp->GetName(name); 1.157 + giovfs->GetDescriptionForMimeType(aMIMEType, description); 1.158 + } else { 1.159 + /* Fallback to GnomeVFS*/ 1.160 + nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID); 1.161 + if (!gnomevfs) 1.162 + return nullptr; 1.163 + 1.164 + nsCOMPtr<nsIGnomeVFSMimeApp> gnomeHandlerApp; 1.165 + if (NS_FAILED(gnomevfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gnomeHandlerApp))) || 1.166 + !gnomeHandlerApp) { 1.167 + return nullptr; 1.168 + } 1.169 + gnomeHandlerApp->GetName(name); 1.170 + gnomevfs->GetDescriptionForMimeType(aMIMEType, description); 1.171 + } 1.172 + 1.173 + mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(name)); 1.174 + mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault); 1.175 + mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(description)); 1.176 + 1.177 + return mimeInfo.forget(); 1.178 +}