/src/mozilla-central/dom/html/HTMLMetaElement.cpp
Line | Count | Source (jump to first uncovered line) |
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/HTMLMetaElement.h" |
9 | | #include "mozilla/dom/HTMLMetaElementBinding.h" |
10 | | #include "mozilla/dom/nsCSPService.h" |
11 | | #include "mozilla/Logging.h" |
12 | | #include "nsContentUtils.h" |
13 | | #include "nsStyleConsts.h" |
14 | | #include "nsIContentSecurityPolicy.h" |
15 | | |
16 | | static mozilla::LazyLogModule gMetaElementLog("nsMetaElement"); |
17 | 0 | #define LOG(msg) MOZ_LOG(gMetaElementLog, mozilla::LogLevel::Debug, msg) |
18 | 0 | #define LOG_ENABLED() MOZ_LOG_TEST(gMetaElementLog, mozilla::LogLevel::Debug) |
19 | | |
20 | | NS_IMPL_NS_NEW_HTML_ELEMENT(Meta) |
21 | | |
22 | | namespace mozilla { |
23 | | namespace dom { |
24 | | |
25 | | HTMLMetaElement::HTMLMetaElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) |
26 | | : nsGenericHTMLElement(std::move(aNodeInfo)) |
27 | 0 | { |
28 | 0 | } |
29 | | |
30 | | HTMLMetaElement::~HTMLMetaElement() |
31 | 0 | { |
32 | 0 | } |
33 | | |
34 | | |
35 | | NS_IMPL_ELEMENT_CLONE(HTMLMetaElement) |
36 | | |
37 | | |
38 | | void |
39 | | HTMLMetaElement::SetMetaReferrer(nsIDocument* aDocument) |
40 | 0 | { |
41 | 0 | if (!aDocument || |
42 | 0 | !AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, nsGkAtoms::referrer, eIgnoreCase)) { |
43 | 0 | return; |
44 | 0 | } |
45 | 0 | nsAutoString content; |
46 | 0 | GetContent(content); |
47 | 0 |
|
48 | 0 | Element* headElt = aDocument->GetHeadElement(); |
49 | 0 | if (headElt && nsContentUtils::ContentIsDescendantOf(this, headElt)) { |
50 | 0 | content = nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(content); |
51 | 0 | aDocument->SetHeaderData(nsGkAtoms::referrer, content); |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | | nsresult |
56 | | HTMLMetaElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName, |
57 | | const nsAttrValue* aValue, |
58 | | const nsAttrValue* aOldValue, |
59 | | nsIPrincipal* aSubjectPrincipal, |
60 | | bool aNotify) |
61 | 0 | { |
62 | 0 | if (aNameSpaceID == kNameSpaceID_None) { |
63 | 0 | nsIDocument *document = GetUncomposedDoc(); |
64 | 0 | if (aName == nsGkAtoms::content) { |
65 | 0 | if (document && AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, |
66 | 0 | nsGkAtoms::viewport, eIgnoreCase)) { |
67 | 0 | nsAutoString content; |
68 | 0 | GetContent(content); |
69 | 0 | nsContentUtils::ProcessViewportInfo(document, content); |
70 | 0 | } |
71 | 0 | CreateAndDispatchEvent(document, NS_LITERAL_STRING("DOMMetaChanged")); |
72 | 0 | } |
73 | 0 | // Update referrer policy when it got changed from JS |
74 | 0 | SetMetaReferrer(document); |
75 | 0 | } |
76 | 0 |
|
77 | 0 | return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName, aValue, |
78 | 0 | aOldValue, aSubjectPrincipal, aNotify); |
79 | 0 | } |
80 | | |
81 | | nsresult |
82 | | HTMLMetaElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, |
83 | | nsIContent* aBindingParent) |
84 | 0 | { |
85 | 0 | nsresult rv = nsGenericHTMLElement::BindToTree(aDocument, aParent, |
86 | 0 | aBindingParent); |
87 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
88 | 0 | if (aDocument && |
89 | 0 | AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, nsGkAtoms::viewport, eIgnoreCase)) { |
90 | 0 | nsAutoString content; |
91 | 0 | GetContent(content); |
92 | 0 | nsContentUtils::ProcessViewportInfo(aDocument, content); |
93 | 0 | } |
94 | 0 |
|
95 | 0 | if (StaticPrefs::security_csp_enable() && aDocument && |
96 | 0 | !aDocument->IsLoadedAsData() && |
97 | 0 | AttrValueIs(kNameSpaceID_None, nsGkAtoms::httpEquiv, nsGkAtoms::headerCSP, eIgnoreCase)) { |
98 | 0 |
|
99 | 0 | // only accept <meta http-equiv="Content-Security-Policy" content=""> if it appears |
100 | 0 | // in the <head> element. |
101 | 0 | Element* headElt = aDocument->GetHeadElement(); |
102 | 0 | if (headElt && nsContentUtils::ContentIsDescendantOf(this, headElt)) { |
103 | 0 |
|
104 | 0 | nsAutoString content; |
105 | 0 | GetContent(content); |
106 | 0 | content = nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(content); |
107 | 0 |
|
108 | 0 | nsIPrincipal* principal = aDocument->NodePrincipal(); |
109 | 0 | nsCOMPtr<nsIContentSecurityPolicy> csp; |
110 | 0 | principal->EnsureCSP(aDocument, getter_AddRefs(csp)); |
111 | 0 | if (csp) { |
112 | 0 | if (LOG_ENABLED()) { |
113 | 0 | nsAutoCString documentURIspec; |
114 | 0 | nsIURI* documentURI = aDocument->GetDocumentURI(); |
115 | 0 | if (documentURI) { |
116 | 0 | documentURI->GetAsciiSpec(documentURIspec); |
117 | 0 | } |
118 | 0 |
|
119 | 0 | LOG(("HTMLMetaElement %p sets CSP '%s' on document=%p, document-uri=%s", |
120 | 0 | this, NS_ConvertUTF16toUTF8(content).get(), aDocument, documentURIspec.get())); |
121 | 0 | } |
122 | 0 |
|
123 | 0 | // Multiple CSPs (delivered through either header of meta tag) need to be |
124 | 0 | // joined together, see: |
125 | 0 | // https://w3c.github.io/webappsec/specs/content-security-policy/#delivery-html-meta-element |
126 | 0 | rv = csp->AppendPolicy(content, |
127 | 0 | false, // csp via meta tag can not be report only |
128 | 0 | true); // delivered through the meta tag |
129 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
130 | 0 | aDocument->ApplySettingsFromCSP(false); |
131 | 0 | } |
132 | 0 | } |
133 | 0 | } |
134 | 0 |
|
135 | 0 | // Referrer Policy spec requires a <meta name="referrer" tag to be in the |
136 | 0 | // <head> element. |
137 | 0 | SetMetaReferrer(aDocument); |
138 | 0 | CreateAndDispatchEvent(aDocument, NS_LITERAL_STRING("DOMMetaAdded")); |
139 | 0 | return rv; |
140 | 0 | } |
141 | | |
142 | | void |
143 | | HTMLMetaElement::UnbindFromTree(bool aDeep, bool aNullParent) |
144 | 0 | { |
145 | 0 | nsCOMPtr<nsIDocument> oldDoc = GetUncomposedDoc(); |
146 | 0 | CreateAndDispatchEvent(oldDoc, NS_LITERAL_STRING("DOMMetaRemoved")); |
147 | 0 | nsGenericHTMLElement::UnbindFromTree(aDeep, aNullParent); |
148 | 0 | } |
149 | | |
150 | | void |
151 | | HTMLMetaElement::CreateAndDispatchEvent(nsIDocument* aDoc, |
152 | | const nsAString& aEventName) |
153 | 0 | { |
154 | 0 | if (!aDoc) |
155 | 0 | return; |
156 | 0 | |
157 | 0 | RefPtr<AsyncEventDispatcher> asyncDispatcher = |
158 | 0 | new AsyncEventDispatcher(this, |
159 | 0 | aEventName, |
160 | 0 | CanBubble::eYes, |
161 | 0 | ChromeOnlyDispatch::eYes); |
162 | 0 | asyncDispatcher->RunDOMEventWhenSafe(); |
163 | 0 | } |
164 | | |
165 | | JSObject* |
166 | | HTMLMetaElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
167 | 0 | { |
168 | 0 | return HTMLMetaElement_Binding::Wrap(aCx, this, aGivenProto); |
169 | 0 | } |
170 | | |
171 | | } // namespace dom |
172 | | } // namespace mozilla |