Bug 1174915 - Allow changing meta referrer policy on the fly (r=baku,sstamm)

This commit is contained in:
Franziskus Kiefer 2015-06-19 17:42:49 -07:00
parent 8b804f721d
commit 653efda290
2 changed files with 33 additions and 14 deletions

View File

@ -48,14 +48,33 @@ HTMLMetaElement::SetItemValueText(const nsAString& aValue)
SetContent(aValue);
}
nsresult
HTMLMetaElement::SetMetaReferrer(nsIDocument* aDocument)
{
if (!aDocument ||
!AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, nsGkAtoms::referrer, eIgnoreCase)) {
return NS_OK;
}
nsAutoString content;
nsresult rv = GetContent(content);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
Element* headElt = aDocument->GetHeadElement();
if (headElt && nsContentUtils::ContentIsDescendantOf(this, headElt)) {
content = nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(content);
aDocument->SetHeaderData(nsGkAtoms::referrer, content);
}
return NS_OK;
}
nsresult
HTMLMetaElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
const nsAttrValue* aValue, bool aNotify)
{
if (aNameSpaceID == kNameSpaceID_None) {
nsIDocument *document = GetUncomposedDoc();
if (aName == nsGkAtoms::content) {
nsIDocument *document = GetUncomposedDoc();
if (document && AttrValueIs(kNameSpaceID_None, nsGkAtoms::name,
nsGkAtoms::viewport, eIgnoreCase)) {
nsAutoString content;
@ -65,6 +84,11 @@ HTMLMetaElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
}
CreateAndDispatchEvent(document, NS_LITERAL_STRING("DOMMetaChanged"));
}
// Update referrer policy when it got changed from JS
nsresult rv = SetMetaReferrer(document);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName, aValue,
@ -87,19 +111,11 @@ HTMLMetaElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
NS_ENSURE_SUCCESS(rv, rv);
nsContentUtils::ProcessViewportInfo(aDocument, content);
}
if (aDocument &&
AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, nsGkAtoms::referrer, eIgnoreCase)) {
nsAutoString content;
rv = GetContent(content);
NS_ENSURE_SUCCESS(rv, rv);
// Referrer Policy spec requires a <meta name="referrer" tag to be in the
// <head> element.
Element* headElt = aDocument->GetHeadElement();
if (headElt && nsContentUtils::ContentIsDescendantOf(this, headElt)) {
content = nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(content);
aDocument->SetHeaderData(nsGkAtoms::referrer, content);
}
// Referrer Policy spec requires a <meta name="referrer" tag to be in the
// <head> element.
rv = SetMetaReferrer(aDocument);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
CreateAndDispatchEvent(aDocument, NS_LITERAL_STRING("DOMMetaAdded"));
return rv;

View File

@ -67,6 +67,9 @@ protected:
virtual void GetItemValueText(DOMString& text) override;
virtual void SetItemValueText(const nsAString& text) override;
private:
nsresult SetMetaReferrer(nsIDocument* aDocument);
};
} // namespace dom