From 58d0d5ec004fb5c69c76b6c1980c29559cd92163 Mon Sep 17 00:00:00 2001 From: Yoshi Huang Date: Fri, 15 Sep 2017 18:27:47 +0800 Subject: [PATCH] Bug 1376971 - Part 2: add GetLoadingPrincipalForXULNode. r=baku Refactor GetContentPolicyTypeForUIImageLoading to have another function called GetLoadingPrincipalForXULNode. --- dom/base/nsContentUtils.cpp | 83 +++++++++++++++++++++++-------------- dom/base/nsContentUtils.h | 13 ++++++ 2 files changed, 66 insertions(+), 30 deletions(-) diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 264f236b3f2d..e945afbdc4d0 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -10444,6 +10444,44 @@ nsContentUtils::AppendNativeAnonymousChildren( } } +/* static */ bool +nsContentUtils::GetLoadingPrincipalForXULNode(nsIContent* aLoadingNode, + nsIPrincipal** aLoadingPrincipal) +{ + MOZ_ASSERT(aLoadingNode); + MOZ_ASSERT(aLoadingPrincipal); + + bool result = false; + nsCOMPtr loadingPrincipal = aLoadingNode->NodePrincipal(); + nsAutoString loadingStr; + aLoadingNode->GetAttr(kNameSpaceID_None, nsGkAtoms::loadingprincipal, + loadingStr); + if (loadingStr.IsEmpty()) { + // Fall back to mContent's principal (SystemPrincipal) if 'loadingprincipal' + // isn't specified. + loadingPrincipal.forget(aLoadingPrincipal); + return result; + } + + nsCOMPtr serializedPrincipal; + NS_DeserializeObject(NS_ConvertUTF16toUTF8(loadingStr), + getter_AddRefs(serializedPrincipal)); + loadingPrincipal = do_QueryInterface(serializedPrincipal); + if (loadingPrincipal) { + // We only allow specifying loadingprincipal attribute on a node loaded by + // SystemPrincipal. + MOZ_ASSERT(nsContentUtils::IsSystemPrincipal(aLoadingNode->NodePrincipal()), + "aLoadingNode Should be loaded with SystemPrincipal"); + + result = true; + } else { + // Fallback if the deserialization is failed. + loadingPrincipal = aLoadingNode->NodePrincipal(); + } + + loadingPrincipal.forget(aLoadingPrincipal); + return result; +} /* static */ void nsContentUtils::GetContentPolicyTypeForUIImageLoading(nsIContent* aLoadingNode, @@ -10453,38 +10491,23 @@ nsContentUtils::GetContentPolicyTypeForUIImageLoading(nsIContent* aLoadingNode, { MOZ_ASSERT(aRequestContextID); - // Use the serialized loadingPrincipal from the image element. Fall back - // to mContent's principal (SystemPrincipal) if not available. - aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE; - nsCOMPtr loadingPrincipal = aLoadingNode->NodePrincipal(); - nsAutoString imageLoadingPrincipal; - aLoadingNode->GetAttr(kNameSpaceID_None, nsGkAtoms::loadingprincipal, - imageLoadingPrincipal); - if (!imageLoadingPrincipal.IsEmpty()) { - nsCOMPtr serializedPrincipal; - NS_DeserializeObject(NS_ConvertUTF16toUTF8(imageLoadingPrincipal), - getter_AddRefs(serializedPrincipal)); - loadingPrincipal = do_QueryInterface(serializedPrincipal); + bool result = GetLoadingPrincipalForXULNode(aLoadingNode, aLoadingPrincipal); + if (result) { + // Set the content policy type to TYPE_INTERNAL_IMAGE_FAVICON for + // indicating it's a favicon loading. + aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON; - if (loadingPrincipal) { - // Set the content policy type to TYPE_INTERNAL_IMAGE_FAVICON for - // indicating it's a favicon loading. - aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON; - - nsAutoString requestContextID; - aLoadingNode->GetAttr(kNameSpaceID_None, nsGkAtoms::requestcontextid, - requestContextID); - nsresult rv; - int64_t val = requestContextID.ToInteger64(&rv); - *aRequestContextID = NS_SUCCEEDED(rv) - ? val - : 0; - } else { - // Fallback if the deserialization is failed. - loadingPrincipal = aLoadingNode->NodePrincipal(); - } + nsAutoString requestContextID; + aLoadingNode->GetAttr(kNameSpaceID_None, nsGkAtoms::requestcontextid, + requestContextID); + nsresult rv; + int64_t val = requestContextID.ToInteger64(&rv); + *aRequestContextID = NS_SUCCEEDED(rv) + ? val + : 0; + } else { + aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE; } - loadingPrincipal.forget(aLoadingPrincipal); } /* static */ nsresult diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index f26c67ac95e4..ee64b58fd180 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -3038,6 +3038,19 @@ public: nsTArray& aKids, uint32_t aFlags); + /** + * Query loadingPrincipal if it is specified as 'loadingprincipal' attribute on + * aLoadingNode, otherwise the NodePrincipal of aLoadingNode is returned + * (which is System Principal). + * + * Return true if aLoadingPrincipal has 'loadingprincipal' attributes, and + * the value 'loadingprincipal' is also successfully deserialized, otherwise + * return false. + */ + static bool + GetLoadingPrincipalForXULNode(nsIContent* aLoadingNode, + nsIPrincipal** aLoadingPrincipal); + /** * Returns the content policy type that should be used for loading images * for displaying in the UI. The sources of such images can be ,