diff --git a/browser/extensions/pdfjs/content/PdfJs.jsm b/browser/extensions/pdfjs/content/PdfJs.jsm index 0ba78ff558bd..07fac75e3270 100644 --- a/browser/extensions/pdfjs/content/PdfJs.jsm +++ b/browser/extensions/pdfjs/content/PdfJs.jsm @@ -127,11 +127,20 @@ Factory.prototype = { var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); registrar.registerFactory(proto.classID, proto.classDescription, proto.contractID, factory); + + if (proto.classID2) { + this._classID2 = proto.classID2; + registrar.registerFactory(proto.classID2, proto.classDescription, + proto.contractID2, factory); + } }, unregister: function unregister() { var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); registrar.unregisterFactory(this._classID, this._factory); + if (this._classID2) { + registrar.unregisterFactory(this._classID2, this._factory); + } this._factory = null; } }; @@ -320,13 +329,6 @@ let PdfJs = { Cu.import('resource://pdf.js/PdfStreamConverter.jsm'); this._pdfStreamConverterFactory.register(PdfStreamConverter); - this._pdfRedirectorFactory = new Factory(); - Cu.import('resource://pdf.js/PdfRedirector.jsm'); - this._pdfRedirectorFactory.register(PdfRedirector); - - Svc.pluginHost.registerPlayPreviewMimeType(PDF_CONTENT_TYPE, true, - 'data:application/x-moz-playpreview-pdfjs;,'); - this._registered = true; }, @@ -338,12 +340,6 @@ let PdfJs = { Cu.unload('resource://pdf.js/PdfStreamConverter.jsm'); delete this._pdfStreamConverterFactory; - this._pdfRedirectorFactory.unregister(); - Cu.unload('resource://pdf.js/PdfRedirector.jsm'); - delete this._pdfRedirectorFactory; - - Svc.pluginHost.unregisterPlayPreviewMimeType(PDF_CONTENT_TYPE); - this._registered = false; } }; diff --git a/browser/extensions/pdfjs/content/PdfStreamConverter.jsm b/browser/extensions/pdfjs/content/PdfStreamConverter.jsm index cce6d276b2f0..54bc9a6c1be1 100644 --- a/browser/extensions/pdfjs/content/PdfStreamConverter.jsm +++ b/browser/extensions/pdfjs/content/PdfStreamConverter.jsm @@ -813,6 +813,9 @@ PdfStreamConverter.prototype = { classDescription: 'pdf.js Component', contractID: '@mozilla.org/streamconv;1?from=application/pdf&to=*/*', + classID2: Components.ID('{d0c5195d-e798-49d4-b1d3-9324328b2292}'), + contractID2: '@mozilla.org/streamconv;1?from=application/pdf&to=text/html', + QueryInterface: XPCOMUtils.generateQI([ Ci.nsISupports, Ci.nsIStreamConverter, diff --git a/docshell/base/nsWebNavigationInfo.cpp b/docshell/base/nsWebNavigationInfo.cpp index 95ef8d950d03..c4d81ba8137a 100644 --- a/docshell/base/nsWebNavigationInfo.cpp +++ b/docshell/base/nsWebNavigationInfo.cpp @@ -44,6 +44,13 @@ nsWebNavigationInfo::IsTypeSupported(const nsACString& aType, // to say for itself. *aIsTypeSupported = nsIWebNavigationInfo::UNSUPPORTED; + // We want to claim that the type for PDF documents is unsupported, + // so that the internal PDF viewer's stream converted will get used. + if (aType.LowerCaseEqualsLiteral("application/pdf") && + nsContentUtils::IsPDFJSEnabled()) { + return NS_OK; + } + const nsCString& flatType = PromiseFlatCString(aType); nsresult rv = IsTypeSupportedInternal(flatType, aIsTypeSupported); NS_ENSURE_SUCCESS(rv, rv); diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index d8d661676007..2b00b491b6e5 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -152,6 +152,7 @@ #include "nsIScriptGlobalObject.h" #include "nsIScriptObjectPrincipal.h" #include "nsIScriptSecurityManager.h" +#include "nsIStreamConverterService.h" #include "nsIStringBundle.h" #include "nsIURI.h" #include "nsIURL.h" @@ -6500,6 +6501,19 @@ nsContentUtils::AllowXULXBLForPrincipal(nsIPrincipal* aPrincipal) IsSitePermAllow(aPrincipal, "allowXULXBL")); } +bool +nsContentUtils::IsPDFJSEnabled() +{ + nsCOMPtr convServ = + do_GetService("@mozilla.org/streamConverters;1"); + nsresult rv = NS_ERROR_FAILURE; + bool canConvert = false; + if (convServ) { + rv = convServ->CanConvert("application/pdf", "text/html", &canConvert); + } + return NS_SUCCEEDED(rv) && canConvert; +} + already_AddRefed nsContentUtils::FindInternalContentViewer(const nsACString& aType, ContentViewerType* aLoaderType) @@ -7922,4 +7936,4 @@ nsContentUtils::SetFetchReferrerURIWithPolicy(nsIPrincipal* aPrincipal, net::ReferrerPolicy referrerPolicy = aDoc->GetReferrerPolicy(); return aChannel->SetReferrerWithPolicy(referrerURI, referrerPolicy); -} \ No newline at end of file +} diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 437711af10cd..a7dd9289da79 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -2070,6 +2070,11 @@ public: */ static void XPCOMShutdown(); + /** + * Checks if internal PDF viewer is enabled. + */ + static bool IsPDFJSEnabled(); + enum ContentViewerType { TYPE_UNSUPPORTED, diff --git a/dom/base/nsObjectLoadingContent.cpp b/dom/base/nsObjectLoadingContent.cpp index 5124a51e0f39..4af1e8eb84f2 100644 --- a/dom/base/nsObjectLoadingContent.cpp +++ b/dom/base/nsObjectLoadingContent.cpp @@ -552,6 +552,11 @@ IsPluginEnabledByExtension(nsIURI* uri, nsCString& mimeType) return false; } + // Disables any native PDF plugins, when internal PDF viewer is enabled. + if (ext.EqualsIgnoreCase("pdf") && nsContentUtils::IsPDFJSEnabled()) { + return false; + } + nsRefPtr pluginHost = nsPluginHost::GetInst(); if (!pluginHost) { @@ -2672,6 +2677,13 @@ nsObjectLoadingContent::GetTypeOfContent(const nsCString& aMIMEType) return eType_Image; } + // Faking support of the PDF content as a document for EMBED tags + // when internal PDF viewer is enabled. + if (aMIMEType.LowerCaseEqualsLiteral("application/pdf") && + nsContentUtils::IsPDFJSEnabled()) { + return eType_Document; + } + // SVGs load as documents, but are their own capability bool isSVG = aMIMEType.LowerCaseEqualsLiteral("image/svg+xml"); Capabilities supportType = isSVG ? eSupportSVG : eSupportDocuments;