Bug 1407498 - Don't query loadingprincipal in common case. r=baku

We queried 'loadingprincipal' attribute on the common call path, however
this should be queried if it's loaded by System Principal.

Also rename loadingprincipal to triggeringprincipal
This commit is contained in:
Yoshi Huang 2017-10-11 17:07:35 +08:00
parent 4274c680a7
commit 26c9043304
13 changed files with 74 additions and 68 deletions

View File

@ -851,7 +851,7 @@ function makePreview(row) {
let serial = Components.classes["@mozilla.org/network/serialization-helper;1"]
.getService(Components.interfaces.nsISerializationHelper);
let loadingPrincipalStr = serial.serializeToString(gDocInfo.principal);
let triggeringPrinStr = serial.serializeToString(gDocInfo.principal);
if ((item.HTMLLinkElement || item.HTMLInputElement ||
item.HTMLImageElement || item.SVGImageElement ||
(item.HTMLObjectElement && mimeType && mimeType.startsWith("image/")) ||
@ -910,14 +910,14 @@ function makePreview(row) {
setItemValue("imagedimensiontext", imageSize);
}, {once: true});
newImage.setAttribute("loadingprincipal", loadingPrincipalStr);
newImage.setAttribute("triggeringprincipal", triggeringPrinStr);
newImage.setAttribute("src", url);
} else {
// Handle the case where newImage is not used for width & height
if (item.HTMLVideoElement && isProtocolAllowed) {
newImage = document.createElementNS("http://www.w3.org/1999/xhtml", "video");
newImage.id = "thepreviewimage";
newImage.setAttribute("loadingprincipal", loadingPrincipalStr);
newImage.setAttribute("triggeringprincipal", triggeringPrinStr);
newImage.src = url;
newImage.controls = true;
width = physWidth = item.videoWidth;
@ -928,7 +928,7 @@ function makePreview(row) {
} else if (item.HTMLAudioElement && isProtocolAllowed) {
newImage = new Audio;
newImage.id = "thepreviewimage";
newImage.setAttribute("loadingprincipal", loadingPrincipalStr);
newImage.setAttribute("triggeringprincipal", triggeringPrinStr);
newImage.src = url;
newImage.controls = true;
isAudio = true;

View File

@ -7752,7 +7752,7 @@
anonid="tab-throbber"
class="tab-throbber"
layer="true"/>
<xul:image xbl:inherits="src=image,loadingprincipal=iconloadingprincipal,requestcontextid,fadein,pinned,selected=visuallyselected,busy,crashed,sharing"
<xul:image xbl:inherits="src=image,triggeringprincipal=iconloadingprincipal,requestcontextid,fadein,pinned,selected=visuallyselected,busy,crashed,sharing"
anonid="tab-icon-image"
class="tab-icon-image"
validate="never"

View File

@ -29,7 +29,7 @@ function testFirstPartyDomain(pageInfo) {
// For <img>, we will query imgIRequest.imagePrincipal later, so we wait
// for loadend event. For <audio> and <video>, so far we only can get
// the loadingprincipal attribute on the node, so we simply wait for
// the triggeringprincipal attribute on the node, so we simply wait for
// loadstart.
if (i == 0) {
await waitForEvent(preview, "loadend");
@ -48,10 +48,10 @@ function testFirstPartyDomain(pageInfo) {
"imagePrincipal should have firstPartyDomain set to " + EXPECTED_DOMAIN);
}
// Check the node has the attribute 'loadingprincipal'.
// Check the node has the attribute 'triggeringprincipal'.
let serial = Components.classes["@mozilla.org/network/serialization-helper;1"]
.getService(Components.interfaces.nsISerializationHelper);
let loadingPrincipalStr = preview.getAttribute("loadingprincipal");
let loadingPrincipalStr = preview.getAttribute("triggeringprincipal");
let loadingPrincipal = serial.deserializeObject(loadingPrincipalStr);
Assert.equal(loadingPrincipal.originAttributes.firstPartyDomain, EXPECTED_DOMAIN,
"loadingPrincipal should have firstPartyDomain set to " + EXPECTED_DOMAIN);

View File

@ -10480,59 +10480,59 @@ nsContentUtils::AppendNativeAnonymousChildren(
}
/* static */ bool
nsContentUtils::GetLoadingPrincipalForXULNode(nsIContent* aLoadingNode,
nsIPrincipal* aDefaultPrincipal,
nsIPrincipal** aLoadingPrincipal)
nsContentUtils::QueryTriggeringPrincipal(nsIContent* aLoadingNode,
nsIPrincipal* aDefaultPrincipal,
nsIPrincipal** aTriggeringPrincipal)
{
MOZ_ASSERT(aLoadingNode);
MOZ_ASSERT(aLoadingPrincipal);
MOZ_ASSERT(aTriggeringPrincipal);
bool result = false;
nsCOMPtr<nsIPrincipal> loadingPrincipal = aDefaultPrincipal;
if (!loadingPrincipal) {
loadingPrincipal = aLoadingNode->NodePrincipal();
}
// If aLoadingNode is content, bail out early.
if (!aLoadingNode->NodePrincipal()->GetIsSystemPrincipal()) {
loadingPrincipal.forget(aTriggeringPrincipal);
return result;
}
nsAutoString loadingStr;
aLoadingNode->GetAttr(kNameSpaceID_None, nsGkAtoms::loadingprincipal,
aLoadingNode->GetAttr(kNameSpaceID_None, nsGkAtoms::triggeringprincipal,
loadingStr);
// Fall back to mContent's principal if 'loadingprincipal' isn't specified,
// or if the doc isn't loaded by System Principal.
if (loadingStr.IsEmpty() ||
!aLoadingNode->OwnerDoc()->NodePrincipal()->GetIsSystemPrincipal()) {
loadingPrincipal.forget(aLoadingPrincipal);
// Fall back if 'triggeringprincipal' isn't specified,
if (loadingStr.IsEmpty()) {
loadingPrincipal.forget(aTriggeringPrincipal);
return result;
}
nsCOMPtr<nsISupports> 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");
nsCOMPtr<nsIPrincipal> serializedPrin = do_QueryInterface(serializedPrincipal);
if (serializedPrin) {
result = true;
serializedPrin.forget(aTriggeringPrincipal);
} else {
// Fallback if the deserialization is failed.
loadingPrincipal = aLoadingNode->NodePrincipal();
loadingPrincipal.forget(aTriggeringPrincipal);
}
loadingPrincipal.forget(aLoadingPrincipal);
return result;
}
/* static */ void
nsContentUtils::GetContentPolicyTypeForUIImageLoading(nsIContent* aLoadingNode,
nsIPrincipal** aLoadingPrincipal,
nsIPrincipal** aTriggeringPrincipal,
nsContentPolicyType& aContentPolicyType,
uint64_t* aRequestContextID)
{
MOZ_ASSERT(aRequestContextID);
bool result = GetLoadingPrincipalForXULNode(aLoadingNode, aLoadingPrincipal);
bool result = QueryTriggeringPrincipal(aLoadingNode, aTriggeringPrincipal);
if (result) {
// Set the content policy type to TYPE_INTERNAL_IMAGE_FAVICON for
// indicating it's a favicon loading.

View File

@ -3083,24 +3083,25 @@ public:
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).
* Query triggeringPrincipal if there's a 'triggeringprincipal' attribute on
* aLoadingNode, if no such attribute is specified, aDefaultPrincipal is
* returned if it is provided, otherwise the NodePrincipal of aLoadingNode is
* returned.
*
* Return true if aLoadingPrincipal has 'loadingprincipal' attributes, and
* the value 'loadingprincipal' is also successfully deserialized, otherwise
* Return true if aLoadingNode has a 'triggeringprincipal' attribute, and
* the value 'triggeringprincipal' is also successfully deserialized, otherwise
* return false.
*/
static bool
GetLoadingPrincipalForXULNode(nsIContent* aLoadingNode,
nsIPrincipal* aDefaultPrincipal,
nsIPrincipal** aTriggeringPrincipal);
QueryTriggeringPrincipal(nsIContent* aLoadingNode,
nsIPrincipal* aDefaultPrincipal,
nsIPrincipal** aTriggeringPrincipal);
static bool
GetLoadingPrincipalForXULNode(nsIContent* aLoadingNode,
nsIPrincipal** aTriggeringPrincipal)
QueryTriggeringPrincipal(nsIContent* aLoadingNode,
nsIPrincipal** aTriggeringPrincipal)
{
return GetLoadingPrincipalForXULNode(aLoadingNode, nullptr, aTriggeringPrincipal);
return QueryTriggeringPrincipal(aLoadingNode, nullptr, aTriggeringPrincipal);
}
/**
@ -3110,7 +3111,7 @@ public:
*/
static void
GetContentPolicyTypeForUIImageLoading(nsIContent* aLoadingNode,
nsIPrincipal** aLoadingPrincipal,
nsIPrincipal** aTriggeringPrincipal,
nsContentPolicyType& aContentPolicyType,
uint64_t* aRequestContextID);

View File

@ -644,7 +644,7 @@ GK_ATOM(listing, "listing")
GK_ATOM(listitem, "listitem")
GK_ATOM(listrows, "listrows")
GK_ATOM(load, "load")
GK_ATOM(loadingprincipal, "loadingprincipal")
GK_ATOM(triggeringprincipal, "triggeringprincipal")
GK_ATOM(localedir, "localedir")
GK_ATOM(localName, "local-name")
GK_ATOM(longdesc, "longdesc")

View File

@ -1044,10 +1044,10 @@ nsImageLoadingContent::LoadImage(nsIURI* aNewURI,
nsCOMPtr<nsIPrincipal> triggeringPrincipal;
bool result =
nsContentUtils::GetLoadingPrincipalForXULNode(content, aTriggeringPrincipal,
getter_AddRefs(triggeringPrincipal));
nsContentUtils::QueryTriggeringPrincipal(content, aTriggeringPrincipal,
getter_AddRefs(triggeringPrincipal));
// If result is true, which means this node has specified 'loadingprincipal'
// If result is true, which means this node has specified 'triggeringprincipal'
// attribute on it, so we use favicon as the policy type.
nsContentPolicyType policyType = result ?
nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON:

View File

@ -1193,16 +1193,17 @@ public:
? nsIContentPolicy::TYPE_INTERNAL_AUDIO :
nsIContentPolicy::TYPE_INTERNAL_VIDEO;
// If aElement has 'loadingprincipal' attribute, we will use the value as
// If aElement has 'triggeringprincipal' attribute, we will use the value as
// triggeringPrincipal for the channel, otherwise it will default to use
// aElement->NodePrincipal().
// This function returns true when aElement has 'loadingprincipal', so if
// This function returns true when aElement has 'triggeringprincipal', so if
// setAttrs is true we will override the origin attributes on the channel
// later.
nsCOMPtr<nsIPrincipal> triggeringPrincipal;
bool setAttrs = nsContentUtils::GetLoadingPrincipalForXULNode(aElement,
aElement->mLoadingSrcTriggeringPrincipal,
getter_AddRefs(triggeringPrincipal));
bool setAttrs =
nsContentUtils::QueryTriggeringPrincipal(aElement,
aElement->mLoadingSrcTriggeringPrincipal,
getter_AddRefs(triggeringPrincipal));
nsCOMPtr<nsILoadGroup> loadGroup = aElement->GetDocumentLoadGroup();
nsCOMPtr<nsIChannel> channel;

View File

@ -763,21 +763,21 @@ ChannelMediaResource::RecreateChannel()
nsContentPolicyType contentPolicyType = element->IsHTMLElement(nsGkAtoms::audio) ?
nsIContentPolicy::TYPE_INTERNAL_AUDIO : nsIContentPolicy::TYPE_INTERNAL_VIDEO;
// If element has 'loadingprincipal' attribute, we will use the value as
// loadingPrincipal for the channel, otherwise it will default to use
// If element has 'triggeringprincipal' attribute, we will use the value as
// triggeringPrincipal for the channel, otherwise it will default to use
// aElement->NodePrincipal().
// This function returns true when element has 'loadingprincipal', so if
// This function returns true when element has 'triggeringprincipal', so if
// setAttrs is true we will override the origin attributes on the channel
// later.
nsCOMPtr<nsIPrincipal> loadingPrincipal;
nsCOMPtr<nsIPrincipal> triggeringPrincipal;
bool setAttrs =
nsContentUtils::GetLoadingPrincipalForXULNode(element,
getter_AddRefs(loadingPrincipal));
nsContentUtils::QueryTriggeringPrincipal(element,
getter_AddRefs(triggeringPrincipal));
nsresult rv = NS_NewChannelWithTriggeringPrincipal(getter_AddRefs(mChannel),
mURI,
element,
loadingPrincipal,
triggeringPrincipal,
securityFlags,
contentPolicyType,
loadGroup,
@ -789,7 +789,7 @@ ChannelMediaResource::RecreateChannel()
nsCOMPtr<nsILoadInfo> loadInfo = mChannel->GetLoadInfo();
if (loadInfo) {
// The function simply returns NS_OK, so we ignore the return value.
Unused << loadInfo->SetOriginAttributes(loadingPrincipal->OriginAttributesRef());
Unused << loadInfo->SetOriginAttributes(triggeringPrincipal->OriginAttributesRef());
}
}

View File

@ -239,10 +239,10 @@ nsImageBoxFrame::UpdateImage()
nsIDocument* doc = mContent->GetComposedDoc();
if (doc) {
nsContentPolicyType contentPolicyType;
nsCOMPtr<nsIPrincipal> loadingPrincipal;
nsCOMPtr<nsIPrincipal> triggeringPrincipal;
uint64_t requestContextID = 0;
nsContentUtils::GetContentPolicyTypeForUIImageLoading(mContent,
getter_AddRefs(loadingPrincipal),
getter_AddRefs(triggeringPrincipal),
contentPolicyType,
&requestContextID);
@ -253,10 +253,14 @@ nsImageBoxFrame::UpdateImage()
doc,
baseURI);
if (uri) {
nsresult rv = nsContentUtils::LoadImage(uri, mContent, doc, loadingPrincipal, requestContextID,
doc->GetDocumentURI(), doc->GetReferrerPolicy(),
nsresult rv = nsContentUtils::LoadImage(uri, mContent, doc,
triggeringPrincipal,
requestContextID,
doc->GetDocumentURI(),
doc->GetReferrerPolicy(),
mListener, mLoadFlags,
EmptyString(), getter_AddRefs(mImageRequest),
EmptyString(),
getter_AddRefs(mImageRequest),
contentPolicyType);
if (NS_SUCCEEDED(rv) && mImageRequest) {

View File

@ -214,7 +214,7 @@
<content>
<xul:hbox class="menu-iconic-left" align="center" pack="center"
xbl:inherits="selected,_moz-menuactive,disabled,checked">
<xul:image class="menu-iconic-icon" xbl:inherits="src=image,loadingprincipal=iconloadingprincipal,validate,src"/>
<xul:image class="menu-iconic-icon" xbl:inherits="src=image,triggeringprincipal=iconloadingprincipal,validate,src"/>
</xul:hbox>
<xul:label class="menu-iconic-text" flex="1" xbl:inherits="value=label,accesskey,crop,highlightable" crop="right"/>
<xul:label class="menu-iconic-highlightable-text" xbl:inherits="xbl:text=label,crop,accesskey,highlightable" crop="right"/>

View File

@ -56,7 +56,7 @@ protected:
nsresult OnFrameComplete(imgIRequest* aRequest);
nsCOMPtr<nsIContent> mContent;
nsCOMPtr<nsIPrincipal> mLoadingPrincipal;
nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
nsContentPolicyType mContentType;
RefPtr<imgRequestProxy> mIconRequest;
nsMenuObjectX* mMenuObject; // [weak]

View File

@ -58,7 +58,7 @@ nsMenuItemIconX::nsMenuItemIconX(nsMenuObjectX* aMenuItem,
nsIContent* aContent,
NSMenuItem* aNativeMenuItem)
: mContent(aContent)
, mLoadingPrincipal(aContent->NodePrincipal())
, mTriggeringPrincipal(aContent->NodePrincipal())
, mContentType(nsIContentPolicy::TYPE_INTERNAL_IMAGE)
, mMenuObject(aMenuItem)
, mLoadedIcon(false)
@ -213,7 +213,7 @@ nsMenuItemIconX::GetIconURI(nsIURI** aIconURI)
} else {
uint64_t dummy = 0;
nsContentUtils::GetContentPolicyTypeForUIImageLoading(mContent,
getter_AddRefs(mLoadingPrincipal),
getter_AddRefs(mTriggeringPrincipal),
mContentType,
&dummy);
}
@ -318,7 +318,7 @@ nsMenuItemIconX::LoadIcon(nsIURI* aIconURI)
nsresult rv = loader->LoadImage(aIconURI, nullptr, nullptr,
mozilla::net::RP_Unset,
mLoadingPrincipal, 0, loadGroup, this,
mTriggeringPrincipal, 0, loadGroup, this,
mContent, document, nsIRequest::LOAD_NORMAL, nullptr,
mContentType, EmptyString(),
/* aUseUrgentStartForChannel */ false,