1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "mozilla/AsyncEventDispatcher.h"
8 #include "mozilla/dom/BindContext.h"
9 #include "mozilla/dom/HTMLMetaElement.h"
10 #include "mozilla/dom/HTMLMetaElementBinding.h"
11 #include "mozilla/dom/nsCSPService.h"
12 #include "mozilla/dom/nsCSPUtils.h"
13 #include "mozilla/dom/ViewportMetaData.h"
14 #include "mozilla/Logging.h"
15 #include "mozilla/StaticPrefs_security.h"
16 #include "nsContentUtils.h"
17 #include "nsSandboxFlags.h"
18 #include "nsStyleConsts.h"
19 #include "nsIXMLContentSink.h"
20
21 static mozilla::LazyLogModule gMetaElementLog("nsMetaElement");
22 #define LOG(msg) MOZ_LOG(gMetaElementLog, mozilla::LogLevel::Debug, msg)
23 #define LOG_ENABLED() MOZ_LOG_TEST(gMetaElementLog, mozilla::LogLevel::Debug)
24
25 NS_IMPL_NS_NEW_HTML_ELEMENT(Meta)
26
27 namespace mozilla::dom {
28
HTMLMetaElement(already_AddRefed<mozilla::dom::NodeInfo> && aNodeInfo)29 HTMLMetaElement::HTMLMetaElement(
30 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
31 : nsGenericHTMLElement(std::move(aNodeInfo)) {}
32
33 HTMLMetaElement::~HTMLMetaElement() = default;
34
NS_IMPL_ELEMENT_CLONE(HTMLMetaElement)35 NS_IMPL_ELEMENT_CLONE(HTMLMetaElement)
36
37 nsresult HTMLMetaElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
38 const nsAttrValue* aValue,
39 const nsAttrValue* aOldValue,
40 nsIPrincipal* aSubjectPrincipal,
41 bool aNotify) {
42 if (aNameSpaceID == kNameSpaceID_None) {
43 if (Document* document = GetUncomposedDoc()) {
44 if (aName == nsGkAtoms::content) {
45 if (const nsAttrValue* name = GetParsedAttr(nsGkAtoms::name)) {
46 MetaAddedOrChanged(*document, *name, ChangeKind::ContentChange);
47 }
48 CreateAndDispatchEvent(*document, u"DOMMetaChanged"_ns);
49 } else if (aName == nsGkAtoms::name) {
50 if (aOldValue) {
51 MetaRemoved(*document, *aOldValue, ChangeKind::NameChange);
52 }
53 if (aValue) {
54 MetaAddedOrChanged(*document, *aValue, ChangeKind::NameChange);
55 }
56 CreateAndDispatchEvent(*document, u"DOMMetaChanged"_ns);
57 }
58 }
59 }
60
61 return nsGenericHTMLElement::AfterSetAttr(
62 aNameSpaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify);
63 }
64
BindToTree(BindContext & aContext,nsINode & aParent)65 nsresult HTMLMetaElement::BindToTree(BindContext& aContext, nsINode& aParent) {
66 nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
67 NS_ENSURE_SUCCESS(rv, rv);
68 if (!IsInUncomposedDoc()) {
69 return rv;
70 }
71 Document& doc = aContext.OwnerDoc();
72
73 bool shouldProcessMeta = true;
74 // We don't want to call ProcessMETATag when we are pretty print
75 // the document
76 if (doc.IsXMLDocument()) {
77 if (nsCOMPtr<nsIXMLContentSink> xmlSink =
78 do_QueryInterface(doc.GetCurrentContentSink())) {
79 if (xmlSink->IsPrettyPrintXML() &&
80 xmlSink->IsPrettyPrintHasSpecialRoot()) {
81 shouldProcessMeta = false;
82 }
83 }
84 }
85
86 if (shouldProcessMeta) {
87 doc.ProcessMETATag(this);
88 }
89
90 if (AttrValueIs(kNameSpaceID_None, nsGkAtoms::httpEquiv, nsGkAtoms::headerCSP,
91 eIgnoreCase)) {
92 // only accept <meta http-equiv="Content-Security-Policy" content=""> if it
93 // appears in the <head> element.
94 Element* headElt = doc.GetHeadElement();
95 if (headElt && IsInclusiveDescendantOf(headElt)) {
96 nsAutoString content;
97 GetContent(content);
98
99 if (LOG_ENABLED()) {
100 nsAutoCString documentURIspec;
101 if (nsIURI* documentURI = doc.GetDocumentURI()) {
102 documentURI->GetAsciiSpec(documentURIspec);
103 }
104
105 LOG(
106 ("HTMLMetaElement %p sets CSP '%s' on document=%p, "
107 "document-uri=%s",
108 this, NS_ConvertUTF16toUTF8(content).get(), &doc,
109 documentURIspec.get()));
110 }
111 CSP_ApplyMetaCSPToDoc(doc, content);
112 }
113 }
114
115 if (const nsAttrValue* name = GetParsedAttr(nsGkAtoms::name)) {
116 MetaAddedOrChanged(doc, *name, ChangeKind::TreeChange);
117 }
118 CreateAndDispatchEvent(doc, u"DOMMetaAdded"_ns);
119 return rv;
120 }
121
UnbindFromTree(bool aNullParent)122 void HTMLMetaElement::UnbindFromTree(bool aNullParent) {
123 if (Document* oldDoc = GetUncomposedDoc()) {
124 if (const nsAttrValue* name = GetParsedAttr(nsGkAtoms::name)) {
125 MetaRemoved(*oldDoc, *name, ChangeKind::TreeChange);
126 }
127 CreateAndDispatchEvent(*oldDoc, u"DOMMetaRemoved"_ns);
128 }
129 nsGenericHTMLElement::UnbindFromTree(aNullParent);
130 }
131
CreateAndDispatchEvent(Document &,const nsAString & aEventName)132 void HTMLMetaElement::CreateAndDispatchEvent(Document&,
133 const nsAString& aEventName) {
134 RefPtr<AsyncEventDispatcher> asyncDispatcher = new AsyncEventDispatcher(
135 this, aEventName, CanBubble::eYes, ChromeOnlyDispatch::eYes);
136 asyncDispatcher->RunDOMEventWhenSafe();
137 }
138
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)139 JSObject* HTMLMetaElement::WrapNode(JSContext* aCx,
140 JS::Handle<JSObject*> aGivenProto) {
141 return HTMLMetaElement_Binding::Wrap(aCx, this, aGivenProto);
142 }
143
MetaAddedOrChanged(Document & aDoc,const nsAttrValue & aName,ChangeKind aChangeKind)144 void HTMLMetaElement::MetaAddedOrChanged(Document& aDoc,
145 const nsAttrValue& aName,
146 ChangeKind aChangeKind) {
147 nsAutoString content;
148 const bool hasContent = GetAttr(nsGkAtoms::content, content);
149 if (aName.Equals(nsGkAtoms::viewport, eIgnoreCase)) {
150 if (hasContent) {
151 aDoc.SetMetaViewportData(MakeUnique<ViewportMetaData>(content));
152 }
153 return;
154 }
155
156 if (aName.Equals(nsGkAtoms::referrer, eIgnoreCase)) {
157 content = nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(
158 content);
159 return aDoc.UpdateReferrerInfoFromMeta(content,
160 /* aPreload = */ false);
161 }
162 if (aName.Equals(nsGkAtoms::color_scheme, eIgnoreCase)) {
163 if (aChangeKind != ChangeKind::ContentChange) {
164 return aDoc.AddColorSchemeMeta(*this);
165 }
166 return aDoc.RecomputeColorScheme();
167 }
168 }
169
MetaRemoved(Document & aDoc,const nsAttrValue & aName,ChangeKind aChangeKind)170 void HTMLMetaElement::MetaRemoved(Document& aDoc, const nsAttrValue& aName,
171 ChangeKind aChangeKind) {
172 MOZ_ASSERT(aChangeKind != ChangeKind::ContentChange,
173 "Content change can't trigger removal");
174 if (aName.Equals(nsGkAtoms::color_scheme, eIgnoreCase)) {
175 return aDoc.RemoveColorSchemeMeta(*this);
176 }
177 }
178
179 } // namespace mozilla::dom
180