2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "MediaDocument.h"
32 #include "ChromeClient.h"
33 #include "DocumentLoader.h"
34 #include "EventNames.h"
35 #include "ExceptionCodePlaceholder.h"
37 #include "FrameLoader.h"
38 #include "FrameLoaderClient.h"
39 #include "HTMLEmbedElement.h"
40 #include "HTMLHtmlElement.h"
41 #include "HTMLNames.h"
42 #include "HTMLSourceElement.h"
43 #include "HTMLVideoElement.h"
44 #include "KeyboardEvent.h"
46 #include "RawDataDocumentParser.h"
47 #include "ScriptController.h"
48 #include "ShadowRoot.h"
52 using namespace HTMLNames;
54 // FIXME: Share more code with PluginDocumentParser.
55 class MediaDocumentParser final : public RawDataDocumentParser {
57 static Ref<MediaDocumentParser> create(MediaDocument& document)
59 return adoptRef(*new MediaDocumentParser(document));
63 MediaDocumentParser(MediaDocument& document)
64 : RawDataDocumentParser(document)
67 m_outgoingReferrer = document.outgoingReferrer();
70 virtual void appendBytes(DocumentWriter&, const char*, size_t) override;
72 void createDocumentStructure();
74 HTMLMediaElement* m_mediaElement;
75 String m_outgoingReferrer;
78 void MediaDocumentParser::createDocumentStructure()
80 RefPtr<Element> rootElement = document()->createElement(htmlTag, false);
81 document()->appendChild(rootElement, IGNORE_EXCEPTION);
82 document()->setCSSTarget(rootElement.get());
83 downcast<HTMLHtmlElement>(*rootElement).insertedByParser();
85 if (document()->frame())
86 document()->frame()->injectUserScripts(InjectAtDocumentStart);
89 RefPtr<Element> headElement = document()->createElement(headTag, false);
90 rootElement->appendChild(headElement, IGNORE_EXCEPTION);
92 RefPtr<Element> metaElement = document()->createElement(metaTag, false);
93 metaElement->setAttribute(nameAttr, "viewport");
94 metaElement->setAttribute(contentAttr, "width=device-width,initial-scale=1,user-scalable=no");
95 headElement->appendChild(metaElement, IGNORE_EXCEPTION);
98 RefPtr<Element> body = document()->createElement(bodyTag, false);
99 rootElement->appendChild(body, IGNORE_EXCEPTION);
101 RefPtr<Element> mediaElement = document()->createElement(videoTag, false);
103 m_mediaElement = downcast<HTMLVideoElement>(mediaElement.get());
104 m_mediaElement->setAttribute(controlsAttr, emptyAtom);
105 m_mediaElement->setAttribute(autoplayAttr, emptyAtom);
107 m_mediaElement->setAttribute(nameAttr, "media");
109 StringBuilder elementStyle;
110 elementStyle.appendLiteral("max-width: 100%; max-height: 100%;");
112 elementStyle.appendLiteral("width: 100%; height: 100%;");
114 m_mediaElement->setAttribute(styleAttr, elementStyle.toString());
116 RefPtr<Element> sourceElement = document()->createElement(sourceTag, false);
117 HTMLSourceElement& source = downcast<HTMLSourceElement>(*sourceElement);
118 source.setSrc(document()->url());
120 if (DocumentLoader* loader = document()->loader())
121 source.setType(loader->responseMIMEType());
123 m_mediaElement->appendChild(sourceElement, IGNORE_EXCEPTION);
124 body->appendChild(mediaElement, IGNORE_EXCEPTION);
126 Frame* frame = document()->frame();
130 frame->loader().activeDocumentLoader()->setMainResourceDataBufferingPolicy(DoNotBufferData);
131 frame->loader().setOutgoingReferrer(frame->document()->completeURL(m_outgoingReferrer));
134 void MediaDocumentParser::appendBytes(DocumentWriter&, const char*, size_t)
139 createDocumentStructure();
143 MediaDocument::MediaDocument(Frame* frame, const URL& url)
144 : HTMLDocument(frame, url, MediaDocumentClass)
145 , m_replaceMediaElementTimer(*this, &MediaDocument::replaceMediaElementTimerFired)
147 setCompatibilityMode(DocumentCompatibilityMode::QuirksMode);
148 lockCompatibilityMode();
150 m_outgoingReferrer = frame->loader().outgoingReferrer();
153 MediaDocument::~MediaDocument()
155 ASSERT(!m_replaceMediaElementTimer.isActive());
158 Ref<DocumentParser> MediaDocument::createParser()
160 return MediaDocumentParser::create(*this);
163 static inline HTMLVideoElement* descendentVideoElement(ContainerNode& node)
165 if (is<HTMLVideoElement>(node))
166 return downcast<HTMLVideoElement>(&node);
168 RefPtr<NodeList> nodeList = node.getElementsByTagNameNS(videoTag.namespaceURI(), videoTag.localName());
170 if (nodeList->length() > 0)
171 return downcast<HTMLVideoElement>(nodeList->item(0));
176 static inline HTMLVideoElement* ancestorVideoElement(Node* node)
178 while (node && !is<HTMLVideoElement>(*node))
179 node = node->parentOrShadowHostNode();
181 return downcast<HTMLVideoElement>(node);
184 void MediaDocument::defaultEventHandler(Event* event)
186 // Match the default Quicktime plugin behavior to allow
187 // clicking and double-clicking to pause and play the media.
188 Node* targetNode = event->target()->toNode();
192 if (HTMLVideoElement* video = ancestorVideoElement(targetNode)) {
193 if (event->type() == eventNames().clickEvent) {
194 if (!video->canPlay()) {
196 event->setDefaultHandled();
198 } else if (event->type() == eventNames().dblclickEvent) {
199 if (video->canPlay()) {
201 event->setDefaultHandled();
206 if (!is<ContainerNode>(*targetNode))
208 ContainerNode& targetContainer = downcast<ContainerNode>(*targetNode);
209 if (event->type() == eventNames().keydownEvent && is<KeyboardEvent>(*event)) {
210 HTMLVideoElement* video = descendentVideoElement(targetContainer);
214 KeyboardEvent& keyboardEvent = downcast<KeyboardEvent>(*event);
215 if (keyboardEvent.keyIdentifier() == "U+0020") { // space
216 if (video->paused()) {
217 if (video->canPlay())
221 keyboardEvent.setDefaultHandled();
226 void MediaDocument::mediaElementSawUnsupportedTracks()
228 // The HTMLMediaElement was told it has something that the underlying
229 // MediaPlayer cannot handle so we should switch from <video> to <embed>
230 // and let the plugin handle this. Don't do it immediately as this
231 // function may be called directly from a media engine callback, and
232 // replaceChild will destroy the element, media player, and media engine.
233 m_replaceMediaElementTimer.startOneShot(0);
236 void MediaDocument::replaceMediaElementTimerFired()
238 auto* htmlBody = bodyOrFrameset();
242 // Set body margin width and height to 0 as that is what a PluginDocument uses.
243 htmlBody->setAttribute(marginwidthAttr, "0");
244 htmlBody->setAttribute(marginheightAttr, "0");
246 if (HTMLVideoElement* videoElement = descendentVideoElement(*htmlBody)) {
247 RefPtr<Element> element = Document::createElement(embedTag, false);
248 HTMLEmbedElement& embedElement = downcast<HTMLEmbedElement>(*element);
250 embedElement.setAttribute(widthAttr, "100%");
251 embedElement.setAttribute(heightAttr, "100%");
252 embedElement.setAttribute(nameAttr, "plugin");
253 embedElement.setAttribute(srcAttr, url().string());
255 DocumentLoader* documentLoader = loader();
256 ASSERT(documentLoader);
258 embedElement.setAttribute(typeAttr, documentLoader->writer().mimeType());
260 videoElement->parentNode()->replaceChild(&embedElement, videoElement, IGNORE_EXCEPTION);
264 void MediaDocument::mediaElementNaturalSizeChanged(const IntSize& newSize)
269 if (newSize.isZero())
273 page()->chrome().client().mediaDocumentNaturalSizeChanged(newSize);