1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.net.MalformedURLException;
18 import java.util.Map;
19
20 import org.htmlunit.SgmlPage;
21 import org.htmlunit.html.csp.ContentSecurityPolicy;
22 import org.htmlunit.javascript.PostponedAction;
23 import org.htmlunit.javascript.host.event.Event;
24 import org.htmlunit.util.StringUtils;
25
26
27
28
29
30
31
32
33 public abstract class HtmlMedia extends HtmlElement {
34
35 private boolean childrenChecked_;
36 private String checkedSrc_;
37
38
39
40
41
42
43
44
45 HtmlMedia(final String qualifiedName, final SgmlPage page,
46 final Map<String, DomAttr> attributes) {
47 super(qualifiedName, page, attributes);
48 }
49
50
51
52
53
54
55 @Override
56 protected void onAddedToPage() {
57 super.onAddedToPage();
58 childrenChecked_ = false;
59 final String src = getAttributeDirect(SRC_ATTRIBUTE);
60
61 if (ATTRIBUTE_NOT_DEFINED != src && !src.equals(checkedSrc_)) {
62 checkSrc(src);
63 }
64 if (getFirstElementChild() != null) {
65 checkChildrenContentSecurityPolicy();
66 }
67 }
68
69
70
71
72 @Override
73 public void onAllChildrenAddedToPage(final boolean postponed) {
74 super.onAllChildrenAddedToPage(postponed);
75 if (!childrenChecked_) {
76 checkChildrenContentSecurityPolicy();
77 }
78 }
79
80
81
82
83
84
85 @Override
86 protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String value,
87 final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
88 final String qualifiedNameLC = StringUtils.toRootLowerCase(qualifiedName);
89 final boolean changed = SRC_ATTRIBUTE.equals(qualifiedNameLC)
90 && !getAttributeNS(namespaceURI, qualifiedNameLC).equals(value);
91
92 super.setAttributeNS(namespaceURI, qualifiedName, value,
93 notifyAttributeChangeListeners, notifyMutationObservers);
94
95 if (changed) {
96 checkSrc(value);
97 }
98 }
99
100 private void checkSrc(final String src) {
101 checkedSrc_ = src;
102 if (!ContentSecurityPolicy.allowsMedia(this, src)) {
103 fireErrorEvent(this);
104 }
105 }
106
107
108
109
110 private static void fireErrorEvent(final HtmlElement element) {
111 final HtmlPage page = element.getHtmlPageOrNull();
112 if (page == null || !page.getWebClient().isJavaScriptEngineEnabled()) {
113 return;
114 }
115
116 final PostponedAction action = new PostponedAction(page, "HtmlMedia.blocked") {
117 @Override
118 public void execute() {
119 element.fireEvent(Event.TYPE_ERROR);
120 }
121 };
122 if (READY_STATE_LOADING.equals(page.getReadyState())) {
123 page.addAfterLoadAction(action);
124 }
125 else {
126 page.getWebClient().getJavaScriptEngine().addPostponedAction(action);
127 }
128 }
129
130
131
132
133
134 private void checkChildrenContentSecurityPolicy() {
135 childrenChecked_ = true;
136 final HtmlPage page = getHtmlPageOrNull();
137 if (page == null || page.getContentSecurityPolicy() == null) {
138 return;
139 }
140
141 if (!hasAttribute(SRC_ATTRIBUTE)) {
142 for (final DomNode child : getChildren()) {
143 if (child instanceof HtmlSource source) {
144 final String sourceSrc = source.getAttributeDirect(SRC_ATTRIBUTE);
145 if (!StringUtils.isBlank(sourceSrc)) {
146 if (ContentSecurityPolicy.allowsMedia(source, sourceSrc)) {
147 break;
148 }
149 fireErrorEvent(source);
150 }
151 }
152 }
153 }
154
155 for (final DomNode child : getChildren()) {
156 if (child instanceof HtmlTrack track && track.hasAttribute("default")) {
157 if (!ContentSecurityPolicy.allowsMedia(track, track.getAttributeDirect(SRC_ATTRIBUTE))) {
158 fireErrorEvent(track);
159 }
160 break;
161 }
162 }
163 }
164
165
166
167
168
169
170
171
172 public final String getSrcAttribute() {
173 return getSrcAttributeNormalized();
174 }
175
176
177
178
179
180
181 public String getSrc() {
182 final String src = getSrcAttribute();
183 if (StringUtils.isEmptyString(src)) {
184 return src;
185 }
186 try {
187 final HtmlPage page = (HtmlPage) getPage();
188 return page.getFullyQualifiedUrl(src).toExternalForm();
189 }
190 catch (final MalformedURLException e) {
191 final String msg = "Unable to create fully qualified URL for src attribute of media " + e.getMessage();
192 throw new RuntimeException(msg, e);
193 }
194 }
195
196
197
198
199
200 public void setSrc(final String src) {
201 setAttribute(SRC_ATTRIBUTE, src);
202 }
203
204
205
206
207
208
209
210
211 public String getCurrentSrc() {
212 return "";
213 }
214 }