#include "config.h"
#if ENABLE(VIDEO)
#include "MediaDocument.h"
#include "DocumentLoader.h"
#include "EventNames.h"
#include "ExceptionCodePlaceholder.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "HTMLEmbedElement.h"
#include "HTMLHtmlElement.h"
#include "HTMLNames.h"
#include "HTMLSourceElement.h"
#include "HTMLVideoElement.h"
#include "KeyboardEvent.h"
#include "NodeList.h"
#include "RawDataDocumentParser.h"
#include "ScriptController.h"
#include "ShadowRoot.h"
namespace WebCore {
using namespace HTMLNames;
class MediaDocumentParser final : public RawDataDocumentParser {
public:
static PassRefPtr<MediaDocumentParser> create(MediaDocument& document)
{
return adoptRef(new MediaDocumentParser(document));
}
private:
MediaDocumentParser(Document& document)
: RawDataDocumentParser(document)
, m_mediaElement(0)
{
}
virtual void appendBytes(DocumentWriter&, const char*, size_t) override;
void createDocumentStructure();
HTMLMediaElement* m_mediaElement;
};
void MediaDocumentParser::createDocumentStructure()
{
RefPtr<Element> rootElement = document()->createElement(htmlTag, false);
document()->appendChild(rootElement, IGNORE_EXCEPTION);
document()->setCSSTarget(rootElement.get());
toHTMLHtmlElement(rootElement.get())->insertedByParser();
if (document()->frame())
document()->frame()->injectUserScripts(InjectAtDocumentStart);
#if PLATFORM(IOS)
RefPtr<Element> headElement = document()->createElement(headTag, false);
rootElement->appendChild(headElement, IGNORE_EXCEPTION);
RefPtr<Element> metaElement = document()->createElement(metaTag, false);
metaElement->setAttribute(nameAttr, "viewport");
metaElement->setAttribute(contentAttr, "width=device-width,initial-scale=1,user-scalable=no");
headElement->appendChild(metaElement, IGNORE_EXCEPTION);
#endif
RefPtr<Element> body = document()->createElement(bodyTag, false);
rootElement->appendChild(body, IGNORE_EXCEPTION);
RefPtr<Element> mediaElement = document()->createElement(videoTag, false);
m_mediaElement = toHTMLVideoElement(mediaElement.get());
m_mediaElement->setAttribute(controlsAttr, "");
m_mediaElement->setAttribute(autoplayAttr, "");
m_mediaElement->setAttribute(nameAttr, "media");
StringBuilder elementStyle;
elementStyle.appendLiteral("max-width: 100%; max-height: 100%;");
#if PLATFORM(IOS)
elementStyle.appendLiteral("width: 100%; height: 100%;");
#endif
m_mediaElement->setAttribute(styleAttr, elementStyle.toString());
RefPtr<Element> sourceElement = document()->createElement(sourceTag, false);
HTMLSourceElement& source = toHTMLSourceElement(*sourceElement);
source.setSrc(document()->url());
if (DocumentLoader* loader = document()->loader())
source.setType(loader->responseMIMEType());
m_mediaElement->appendChild(sourceElement, IGNORE_EXCEPTION);
body->appendChild(mediaElement, IGNORE_EXCEPTION);
Frame* frame = document()->frame();
if (!frame)
return;
frame->loader().activeDocumentLoader()->setMainResourceDataBufferingPolicy(DoNotBufferData);
}
void MediaDocumentParser::appendBytes(DocumentWriter&, const char*, size_t)
{
if (m_mediaElement)
return;
createDocumentStructure();
finish();
}
MediaDocument::MediaDocument(Frame* frame, const URL& url)
: HTMLDocument(frame, url, MediaDocumentClass)
, m_replaceMediaElementTimer(this, &MediaDocument::replaceMediaElementTimerFired)
{
setCompatibilityMode(DocumentCompatibilityMode::QuirksMode);
lockCompatibilityMode();
}
MediaDocument::~MediaDocument()
{
ASSERT(!m_replaceMediaElementTimer.isActive());
}
PassRefPtr<DocumentParser> MediaDocument::createParser()
{
return MediaDocumentParser::create(*this);
}
static inline HTMLVideoElement* descendentVideoElement(ContainerNode& node)
{
if (isHTMLVideoElement(node))
return toHTMLVideoElement(&node);
RefPtr<NodeList> nodeList = node.getElementsByTagNameNS(videoTag.namespaceURI(), videoTag.localName());
if (nodeList.get()->length() > 0)
return toHTMLVideoElement(nodeList.get()->item(0));
return 0;
}
static inline HTMLVideoElement* ancestorVideoElement(Node* node)
{
while (node && !node->hasTagName(videoTag))
node = node->parentOrShadowHostNode();
return toHTMLVideoElement(node);
}
void MediaDocument::defaultEventHandler(Event* event)
{
Node* targetNode = event->target()->toNode();
if (!targetNode)
return;
if (HTMLVideoElement* video = ancestorVideoElement(targetNode)) {
if (event->type() == eventNames().clickEvent) {
if (!video->canPlay()) {
video->pause();
event->setDefaultHandled();
}
} else if (event->type() == eventNames().dblclickEvent) {
if (video->canPlay()) {
video->play();
event->setDefaultHandled();
}
}
}
if (!targetNode->isContainerNode())
return;
ContainerNode& targetContainer = toContainerNode(*targetNode);
if (event->type() == eventNames().keydownEvent && event->isKeyboardEvent()) {
HTMLVideoElement* video = descendentVideoElement(targetContainer);
if (!video)
return;
KeyboardEvent* keyboardEvent = toKeyboardEvent(event);
if (keyboardEvent->keyIdentifier() == "U+0020") { if (video->paused()) {
if (video->canPlay())
video->play();
} else
video->pause();
event->setDefaultHandled();
}
}
}
void MediaDocument::mediaElementSawUnsupportedTracks()
{
m_replaceMediaElementTimer.startOneShot(0);
}
void MediaDocument::replaceMediaElementTimerFired(Timer<MediaDocument>&)
{
HTMLElement* htmlBody = body();
if (!htmlBody)
return;
htmlBody->setAttribute(marginwidthAttr, "0");
htmlBody->setAttribute(marginheightAttr, "0");
if (HTMLVideoElement* videoElement = descendentVideoElement(*htmlBody)) {
RefPtr<Element> element = Document::createElement(embedTag, false);
HTMLEmbedElement* embedElement = toHTMLEmbedElement(element.get());
embedElement->setAttribute(widthAttr, "100%");
embedElement->setAttribute(heightAttr, "100%");
embedElement->setAttribute(nameAttr, "plugin");
embedElement->setAttribute(srcAttr, url().string());
DocumentLoader* documentLoader = loader();
ASSERT(documentLoader);
if (documentLoader)
embedElement->setAttribute(typeAttr, documentLoader->writer().mimeType());
videoElement->parentNode()->replaceChild(embedElement, videoElement, IGNORE_EXCEPTION);
}
}
}
#endif