Bug 1890689 apply drift correction to input rate instead of output rate r=pehrsons
[gecko.git] / uriloader / exthandler / unix / nsGNOMERegistry.cpp
blobdad4adbfd766c20c95397dbfc3bee30788444f41
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 "nsMIMEInfoUnix.h"
9 #include "nsIGIOService.h"
11 /* static */
12 bool nsGNOMERegistry::HandlerExists(const char* aProtocolScheme) {
13 nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
14 if (!giovfs) {
15 return false;
18 nsCOMPtr<nsIHandlerApp> app;
19 return NS_SUCCEEDED(giovfs->GetAppForURIScheme(
20 nsDependentCString(aProtocolScheme), getter_AddRefs(app)));
23 // XXX Check HandlerExists() before calling LoadURL.
25 /* static */
26 nsresult nsGNOMERegistry::LoadURL(nsIURI* aURL) {
27 nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
28 if (!giovfs) {
29 return NS_ERROR_FAILURE;
32 return giovfs->ShowURI(aURL);
35 /* static */
36 void nsGNOMERegistry::GetAppDescForScheme(const nsACString& aScheme,
37 nsAString& aDesc) {
38 nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
39 if (!giovfs) return;
41 nsCOMPtr<nsIHandlerApp> app;
42 if (NS_FAILED(giovfs->GetAppForURIScheme(aScheme, getter_AddRefs(app))))
43 return;
45 app->GetName(aDesc);
48 /* static */
49 already_AddRefed<nsMIMEInfoBase> nsGNOMERegistry::GetFromExtension(
50 const nsACString& aFileExt) {
51 nsAutoCString mimeType;
52 nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
53 if (!giovfs) {
54 return nullptr;
57 // Get the MIME type from the extension, then call GetFromType to
58 // fill in the MIMEInfo.
59 if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
60 mimeType.EqualsLiteral("application/octet-stream")) {
61 return nullptr;
64 RefPtr<nsMIMEInfoBase> mi = GetFromType(mimeType);
65 if (mi) {
66 mi->AppendExtension(aFileExt);
69 return mi.forget();
72 /* static */
73 already_AddRefed<nsMIMEInfoBase> nsGNOMERegistry::GetFromType(
74 const nsACString& aMIMEType) {
75 RefPtr<nsMIMEInfoUnix> mimeInfo = new nsMIMEInfoUnix(aMIMEType);
76 NS_ENSURE_TRUE(mimeInfo, nullptr);
78 nsAutoString name;
79 nsAutoCString description;
81 nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
82 if (!giovfs) {
83 return nullptr;
86 nsCOMPtr<nsIHandlerApp> handlerApp;
87 if (NS_FAILED(
88 giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(handlerApp))) ||
89 !handlerApp) {
90 return nullptr;
92 handlerApp->GetName(name);
93 giovfs->GetDescriptionForMimeType(aMIMEType, description);
95 mimeInfo->SetDefaultDescription(name);
97 mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
98 mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(description));
100 return mimeInfo.forget();