uriloader/exthandler/unix/nsGNOMERegistry.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "nsGNOMERegistry.h"
     7 #include "nsString.h"
     8 #include "nsIComponentManager.h"
     9 #include "nsIFile.h"
    10 #include "nsMIMEInfoUnix.h"
    11 #include "nsAutoPtr.h"
    12 #include "nsIGConfService.h"
    13 #include "nsIGnomeVFSService.h"
    14 #include "nsIGIOService.h"
    16 #ifdef MOZ_WIDGET_GTK
    17 #include <glib.h>
    18 #include <glib-object.h>
    19 #endif
    21 /* static */ bool
    22 nsGNOMERegistry::HandlerExists(const char *aProtocolScheme)
    23 {
    24   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
    25   nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
    26   if (giovfs) {
    27     nsCOMPtr<nsIGIOMimeApp> app;
    28     if (NS_FAILED(giovfs->GetAppForURIScheme(nsDependentCString(aProtocolScheme),
    29                                              getter_AddRefs(app))))
    30       return false;
    31     else
    32       return true;
    33   } else if (gconf) {
    34     bool isEnabled;
    35     nsAutoCString handler;
    36     if (NS_FAILED(gconf->GetAppForProtocol(nsDependentCString(aProtocolScheme), &isEnabled, handler)))
    37       return false;
    39     return isEnabled;
    40   }
    42   return false;
    43 }
    45 // XXX Check HandlerExists() before calling LoadURL.
    46 //
    47 // If there is not a registered handler for the protocol, gnome_url_show()
    48 // falls back to using gnomevfs modules.  See bug 389632.  We don't want
    49 // this fallback to happen as we are not sure of the safety of all gnomevfs
    50 // modules and MIME-default applications.  (gnomevfs should be handled in
    51 // nsGnomeVFSProtocolHandler.)
    53 /* static */ nsresult
    54 nsGNOMERegistry::LoadURL(nsIURI *aURL)
    55 {
    56   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
    57   if (giovfs)
    58     return giovfs->ShowURI(aURL);
    60   nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
    61   if (gnomevfs)
    62     return gnomevfs->ShowURI(aURL);
    64   return NS_ERROR_FAILURE;
    65 }
    67 /* static */ void
    68 nsGNOMERegistry::GetAppDescForScheme(const nsACString& aScheme,
    69                                      nsAString& aDesc)
    70 {
    71   nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
    72   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
    73   if (!gconf && !giovfs)
    74     return;
    76   nsAutoCString name;
    77   if (giovfs) {
    78     nsCOMPtr<nsIGIOMimeApp> app;
    79     if (NS_FAILED(giovfs->GetAppForURIScheme(aScheme, getter_AddRefs(app))))
    80       return;
    82     app->GetName(name);
    83   } else {
    84     bool isEnabled;
    85     if (NS_FAILED(gconf->GetAppForProtocol(aScheme, &isEnabled, name)))
    86       return;
    88     if (!name.IsEmpty()) {
    89       // Try to only provide the executable name, as it is much simpler than with the path and arguments
    90       int32_t firstSpace = name.FindChar(' ');
    91       if (firstSpace != kNotFound) {
    92         name.Truncate(firstSpace);
    93         int32_t lastSlash = name.RFindChar('/');
    94         if (lastSlash != kNotFound) {
    95           name.Cut(0, lastSlash + 1);
    96         }
    97       }
    98     }
    99   }
   101   CopyUTF8toUTF16(name, aDesc);
   102 }
   105 /* static */ already_AddRefed<nsMIMEInfoBase>
   106 nsGNOMERegistry::GetFromExtension(const nsACString& aFileExt)
   107 {
   108   nsAutoCString mimeType;
   109   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
   111   if (giovfs) {
   112     // Get the MIME type from the extension, then call GetFromType to
   113     // fill in the MIMEInfo.
   114     if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
   115         mimeType.EqualsLiteral("application/octet-stream")) {
   116       return nullptr;
   117     }
   118   } else {
   119     /* Fallback to GnomeVFS */
   120     nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
   121     if (!gnomevfs)
   122       return nullptr;
   124     if (NS_FAILED(gnomevfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
   125         mimeType.EqualsLiteral("application/octet-stream"))
   126       return nullptr;
   127   }
   129   nsRefPtr<nsMIMEInfoBase> mi = GetFromType(mimeType);
   130   if (mi) {
   131     mi->AppendExtension(aFileExt);
   132   }
   134   return mi.forget();
   135 }
   137 /* static */ already_AddRefed<nsMIMEInfoBase>
   138 nsGNOMERegistry::GetFromType(const nsACString& aMIMEType)
   139 {
   140   nsRefPtr<nsMIMEInfoUnix> mimeInfo = new nsMIMEInfoUnix(aMIMEType);
   141   NS_ENSURE_TRUE(mimeInfo, nullptr);
   143   nsAutoCString name;
   144   nsAutoCString description;
   146   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
   147   if (giovfs) {
   148     nsCOMPtr<nsIGIOMimeApp> gioHandlerApp;
   149     if (NS_FAILED(giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gioHandlerApp))) ||
   150         !gioHandlerApp) {
   151       return nullptr;
   152     }
   153     gioHandlerApp->GetName(name);
   154     giovfs->GetDescriptionForMimeType(aMIMEType, description);
   155   } else {
   156     /* Fallback to GnomeVFS*/
   157     nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
   158     if (!gnomevfs)
   159       return nullptr;
   161     nsCOMPtr<nsIGnomeVFSMimeApp> gnomeHandlerApp;
   162     if (NS_FAILED(gnomevfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gnomeHandlerApp))) ||
   163         !gnomeHandlerApp) {
   164       return nullptr;
   165     }
   166     gnomeHandlerApp->GetName(name);
   167     gnomevfs->GetDescriptionForMimeType(aMIMEType, description);
   168   }
   170   mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(name));
   171   mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
   172   mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(description));
   174   return mimeInfo.forget();
   175 }

mercurial