Bug 1890277: part 1) Add CSP parser tests for `require-trusted-types-for`. r=tschuster
[gecko.git] / uriloader / exthandler / unix / nsMIMEInfoUnix.cpp
blob038ba672d940bf897551618874dfa6fe52ac0c1c
1 /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsMIMEInfoUnix.h"
8 #include "nsGNOMERegistry.h"
9 #include "nsIGIOService.h"
10 #include "nsNetCID.h"
11 #include "nsIIOService.h"
12 #include "nsLocalFile.h"
14 #ifdef MOZ_ENABLE_DBUS
15 # include "nsDBusHandlerApp.h"
16 #endif
18 nsresult nsMIMEInfoUnix::LoadUriInternal(nsIURI* aURI) {
19 return nsGNOMERegistry::LoadURL(aURI);
22 NS_IMETHODIMP nsMIMEInfoUnix::GetDefaultExecutable(nsIFile** aExecutable) {
23 // This needs to be implemented before FirefoxBridge will work on Linux.
24 // To implement this and be consistent, GetHasDefaultHandler and
25 // LaunchDefaultWithFile should probably be made to be consistent.
26 // Right now, they aren't. GetHasDefaultHandler reports true in cases
27 // where calling LaunchDefaultWithFile will fail due to not finding the
28 // right executable.
30 return NS_ERROR_NOT_IMPLEMENTED;
33 NS_IMETHODIMP
34 nsMIMEInfoUnix::GetHasDefaultHandler(bool* _retval) {
35 // if a default app is set, it means the application has been set from
36 // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to
37 // give the GNOME answer.
38 if (GetDefaultApplication()) {
39 return nsMIMEInfoImpl::GetHasDefaultHandler(_retval);
42 *_retval = false;
44 if (mClass == eProtocolInfo) {
45 *_retval = nsGNOMERegistry::HandlerExists(mSchemeOrType.get());
46 } else {
47 RefPtr<nsMIMEInfoBase> mimeInfo =
48 nsGNOMERegistry::GetFromType(mSchemeOrType);
49 if (!mimeInfo) {
50 nsAutoCString ext;
51 nsresult rv = GetPrimaryExtension(ext);
52 if (NS_SUCCEEDED(rv)) {
53 mimeInfo = nsGNOMERegistry::GetFromExtension(ext);
56 if (mimeInfo) *_retval = true;
59 if (*_retval) return NS_OK;
61 return NS_OK;
64 nsresult nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile* aFile) {
65 // if a default app is set, it means the application has been set from
66 // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to
67 // give the GNOME answer.
68 if (GetDefaultApplication()) {
69 return nsMIMEInfoImpl::LaunchDefaultWithFile(aFile);
72 nsAutoCString nativePath;
73 aFile->GetNativePath(nativePath);
75 nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
76 if (!giovfs) {
77 return NS_ERROR_FAILURE;
80 // nsGIOMimeApp->Launch wants a URI string instead of local file
81 nsresult rv;
82 nsCOMPtr<nsIIOService> ioservice =
83 do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
84 NS_ENSURE_SUCCESS(rv, rv);
85 nsCOMPtr<nsIURI> uri;
86 rv = ioservice->NewFileURI(aFile, getter_AddRefs(uri));
87 NS_ENSURE_SUCCESS(rv, rv);
89 nsCOMPtr<nsIHandlerApp> app;
90 if (NS_FAILED(
91 giovfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) ||
92 !app) {
93 return NS_ERROR_FILE_NOT_FOUND;
96 return app->LaunchWithURI(uri, nullptr);