diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 621ae98854ac..f7e0c1c9b96e 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -216,6 +216,8 @@ #include "TabChild.h" #include "mozilla/dom/DocGroup.h" #include "mozilla/dom/TabGroup.h" +#include "nsIWebNavigationInfo.h" +#include "nsPluginHost.h" #include "nsIBidiKeyboard.h" @@ -9988,3 +9990,93 @@ nsContentUtils::GetClosestNonNativeAnonymousAncestor(Element* aElement) } return e; } + +/** + * Checks whether the given type is a supported document type for + * loading within the nsObjectLoadingContent specified by aContent. + * + * NOTE Helper method for nsContentUtils::HtmlObjectContentTypeForMIMEType. + * NOTE Does not take content policy or capabilities into account + */ +static bool +HtmlObjectContentSupportsDocument(const nsCString& aMimeType, + nsIContent* aContent) +{ + nsCOMPtr info( + do_GetService(NS_WEBNAVIGATION_INFO_CONTRACTID)); + if (!info) { + return false; + } + + nsCOMPtr webNav; + if (aContent) { + nsIDocument* currentDoc = aContent->GetComposedDoc(); + if (currentDoc) { + webNav = do_GetInterface(currentDoc->GetWindow()); + } + } + + uint32_t supported; + nsresult rv = info->IsTypeSupported(aMimeType, webNav, &supported); + + if (NS_FAILED(rv)) { + return false; + } + + if (supported != nsIWebNavigationInfo::UNSUPPORTED) { + // Don't want to support plugins as documents + return supported != nsIWebNavigationInfo::PLUGIN; + } + + // Try a stream converter + // NOTE: We treat any type we can convert from as a supported type. If a + // type is not actually supported, the URI loader will detect that and + // return an error, and we'll fallback. + nsCOMPtr convServ = + do_GetService("@mozilla.org/streamConverters;1"); + bool canConvert = false; + if (convServ) { + rv = convServ->CanConvert(aMimeType.get(), "*/*", &canConvert); + } + return NS_SUCCEEDED(rv) && canConvert; +} + +/* static */ uint32_t +nsContentUtils::HtmlObjectContentTypeForMIMEType(const nsCString& aMIMEType, + nsIContent* aContent) +{ + if (aMIMEType.IsEmpty()) { + return nsIObjectLoadingContent::TYPE_NULL; + } + + if (imgLoader::SupportImageWithMimeType(aMIMEType.get())) { + return nsIObjectLoadingContent::TYPE_IMAGE; + } + + // Faking support of the PDF content as a document for EMBED tags + // when internal PDF viewer is enabled. + if (aMIMEType.LowerCaseEqualsLiteral("application/pdf") && + IsPDFJSEnabled()) { + return nsIObjectLoadingContent::TYPE_DOCUMENT; + } + + // Faking support of the SWF content as a document for EMBED tags + // when internal SWF player is enabled. + if (aMIMEType.LowerCaseEqualsLiteral("application/x-shockwave-flash") && + IsSWFPlayerEnabled()) { + return nsIObjectLoadingContent::TYPE_DOCUMENT; + } + + if (HtmlObjectContentSupportsDocument(aMIMEType, aContent)) { + return nsIObjectLoadingContent::TYPE_DOCUMENT; + } + + RefPtr pluginHost = nsPluginHost::GetInst(); + if (pluginHost && + pluginHost->HavePluginForType(aMIMEType, nsPluginHost::eExcludeNone)) { + // ShouldPlay will handle checking for disabled plugins + return nsIObjectLoadingContent::TYPE_PLUGIN; + } + + return nsIObjectLoadingContent::TYPE_NULL; +} diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 9b2c83728f46..159343549fe6 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -2798,6 +2798,23 @@ public: */ static Element* GetClosestNonNativeAnonymousAncestor(Element* aElement); + /** + * Returns one of the nsIObjectLoadingContent::TYPE_ values describing the + * content type which will be used for the given MIME type when loaded within + * an nsObjectLoadingContent. + * + * NOTE: This method doesn't take capabilities into account. The caller must + * take that into account. + * + * @param aMIMEType The MIME type of the document being loaded. + * @param aContent The nsIContent object which is performing the load. May be + * nullptr in which case the docshell's plugin permissions + * will not be checked. + */ + static uint32_t + HtmlObjectContentTypeForMIMEType(const nsCString& aMIMEType, + nsIContent* aContent); + private: static bool InitializeEventTable(); diff --git a/dom/base/nsObjectLoadingContent.cpp b/dom/base/nsObjectLoadingContent.cpp index 0649849bc7ad..3db7f038cc67 100644 --- a/dom/base/nsObjectLoadingContent.cpp +++ b/dom/base/nsObjectLoadingContent.cpp @@ -452,12 +452,6 @@ URIEquals(nsIURI *a, nsIURI *b) return (!a && !b) || (a && b && NS_SUCCEEDED(a->Equals(b, &equal)) && equal); } -static bool -IsSupportedImage(const nsCString& aMimeType) -{ - return imgLoader::SupportImageWithMimeType(aMimeType.get()); -} - static void GetExtensionFromURI(nsIURI* uri, nsCString& ext) { @@ -557,50 +551,6 @@ nsObjectLoadingContent::MakePluginListener() } -bool -nsObjectLoadingContent::IsSupportedDocument(const nsCString& aMimeType) -{ - nsCOMPtr thisContent = - do_QueryInterface(static_cast(this)); - NS_ASSERTION(thisContent, "must be a content"); - - nsCOMPtr info( - do_GetService(NS_WEBNAVIGATION_INFO_CONTRACTID)); - if (!info) { - return false; - } - - nsCOMPtr webNav; - nsIDocument* currentDoc = thisContent->GetComposedDoc(); - if (currentDoc) { - webNav = do_GetInterface(currentDoc->GetWindow()); - } - - uint32_t supported; - nsresult rv = info->IsTypeSupported(aMimeType, webNav, &supported); - - if (NS_FAILED(rv)) { - return false; - } - - if (supported != nsIWebNavigationInfo::UNSUPPORTED) { - // Don't want to support plugins as documents - return supported != nsIWebNavigationInfo::PLUGIN; - } - - // Try a stream converter - // NOTE: We treat any type we can convert from as a supported type. If a - // type is not actually supported, the URI loader will detect that and - // return an error, and we'll fallback. - nsCOMPtr convServ = - do_GetService("@mozilla.org/streamConverters;1"); - bool canConvert = false; - if (convServ) { - rv = convServ->CanConvert(aMimeType.get(), "*/*", &canConvert); - } - return NS_SUCCEEDED(rv) && canConvert; -} - nsresult nsObjectLoadingContent::BindToTree(nsIDocument* aDocument, nsIContent* aParent, @@ -2761,43 +2711,26 @@ nsObjectLoadingContent::NotifyStateChanged(ObjectType aOldType, nsObjectLoadingContent::ObjectType nsObjectLoadingContent::GetTypeOfContent(const nsCString& aMIMEType) { - if (aMIMEType.IsEmpty()) { - return eType_Null; - } + nsCOMPtr thisContent = + do_QueryInterface(static_cast(this)); + NS_ASSERTION(thisContent, "must be a content"); + ObjectType type = static_cast( + nsContentUtils::HtmlObjectContentTypeForMIMEType(aMIMEType, thisContent)); + + // Switch the result type to eType_Null ic the capability is not present. uint32_t caps = GetCapabilities(); - - if ((caps & eSupportImages) && IsSupportedImage(aMIMEType)) { - return eType_Image; + if (!(caps & eSupportImages) && type == eType_Image) { + type = eType_Null; + } + if (!(caps & eSupportDocuments) && type == eType_Document) { + type = eType_Null; + } + if (!(caps & eSupportPlugins) && type == eType_Plugin) { + type = eType_Null; } - // 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; - } - - // Faking support of the SWF content as a document for EMBED tags - // when internal SWF player is enabled. - if (aMIMEType.LowerCaseEqualsLiteral("application/x-shockwave-flash") && - nsContentUtils::IsSWFPlayerEnabled()) { - return eType_Document; - } - - if ((caps & eSupportDocuments) && IsSupportedDocument(aMIMEType)) { - return eType_Document; - } - - RefPtr pluginHost = nsPluginHost::GetInst(); - if ((caps & eSupportPlugins) && - pluginHost && - pluginHost->HavePluginForType(aMIMEType, nsPluginHost::eExcludeNone)) { - // ShouldPlay will handle checking for disabled plugins - return eType_Plugin; - } - - return eType_Null; + return type; } nsPluginFrame* diff --git a/dom/base/nsObjectLoadingContent.h b/dom/base/nsObjectLoadingContent.h index e5ceb93520bb..7722ed37fc54 100644 --- a/dom/base/nsObjectLoadingContent.h +++ b/dom/base/nsObjectLoadingContent.h @@ -515,13 +515,6 @@ class nsObjectLoadingContent : public nsImageLoadingContent */ bool CheckProcessPolicy(int16_t *aContentPolicy); - /** - * Checks whether the given type is a supported document type - * - * NOTE Does not take content policy or capabilities into account - */ - bool IsSupportedDocument(const nsCString& aType); - /** * Gets the plugin instance and creates a plugin stream listener, assigning * it to mFinalListener