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.
28 #include "core/html/MediaDocument.h"
30 #include "HTMLNames.h"
31 #include "bindings/v8/ExceptionStatePlaceholder.h"
32 #include "core/dom/ElementTraversal.h"
33 #include "core/dom/RawDataDocumentParser.h"
34 #include "core/events/KeyboardEvent.h"
35 #include "core/frame/LocalFrame.h"
36 #include "core/html/HTMLBodyElement.h"
37 #include "core/html/HTMLHeadElement.h"
38 #include "core/html/HTMLHtmlElement.h"
39 #include "core/html/HTMLMetaElement.h"
40 #include "core/html/HTMLSourceElement.h"
41 #include "core/html/HTMLVideoElement.h"
42 #include "core/loader/DocumentLoader.h"
43 #include "core/loader/FrameLoader.h"
44 #include "platform/KeyboardCodes.h"
48 using namespace HTMLNames;
50 // FIXME: Share more code with PluginDocumentParser.
51 class MediaDocumentParser : public RawDataDocumentParser {
53 static PassRefPtr<MediaDocumentParser> create(MediaDocument* document)
55 return adoptRef(new MediaDocumentParser(document));
59 explicit MediaDocumentParser(Document* document)
60 : RawDataDocumentParser(document)
61 , m_didBuildDocumentStructure(false)
65 virtual void appendBytes(const char*, size_t) OVERRIDE;
67 void createDocumentStructure();
69 bool m_didBuildDocumentStructure;
72 void MediaDocumentParser::createDocumentStructure()
75 RefPtr<HTMLHtmlElement> rootElement = HTMLHtmlElement::create(*document());
76 rootElement->insertedByParser();
77 document()->appendChild(rootElement);
79 if (document()->frame())
80 document()->frame()->loader().dispatchDocumentElementAvailable();
82 RefPtr<HTMLHeadElement> head = HTMLHeadElement::create(*document());
83 RefPtr<HTMLMetaElement> meta = HTMLMetaElement::create(*document());
84 meta->setAttribute(nameAttr, "viewport");
85 meta->setAttribute(contentAttr, "width=device-width");
86 head->appendChild(meta.release());
88 RefPtr<HTMLVideoElement> media = HTMLVideoElement::create(*document());
89 media->setAttribute(controlsAttr, "");
90 media->setAttribute(autoplayAttr, "");
91 media->setAttribute(nameAttr, "media");
93 RefPtr<HTMLSourceElement> source = HTMLSourceElement::create(*document());
94 source->setSrc(document()->url());
96 if (DocumentLoader* loader = document()->loader())
97 source->setType(loader->responseMIMEType());
99 media->appendChild(source.release());
101 RefPtr<HTMLBodyElement> body = HTMLBodyElement::create(*document());
102 body->appendChild(media.release());
104 rootElement->appendChild(head.release());
105 rootElement->appendChild(body.release());
107 m_didBuildDocumentStructure = true;
110 void MediaDocumentParser::appendBytes(const char*, size_t)
112 if (m_didBuildDocumentStructure)
115 createDocumentStructure();
119 MediaDocument::MediaDocument(const DocumentInit& initializer)
120 : HTMLDocument(initializer, MediaDocumentClass)
122 setCompatibilityMode(QuirksMode);
123 lockCompatibilityMode();
126 PassRefPtr<DocumentParser> MediaDocument::createParser()
128 return MediaDocumentParser::create(this);
131 void MediaDocument::defaultEventHandler(Event* event)
133 Node* targetNode = event->target()->toNode();
137 if (event->type() == EventTypeNames::keydown && event->isKeyboardEvent()) {
138 HTMLVideoElement* video = Traversal<HTMLVideoElement>::firstWithin(*targetNode);
142 KeyboardEvent* keyboardEvent = toKeyboardEvent(event);
143 if (keyboardEvent->keyIdentifier() == "U+0020" || keyboardEvent->keyCode() == VKEY_MEDIA_PLAY_PAUSE) {
144 // space or media key (play/pause)
145 video->togglePlayState();
146 event->setDefaultHandled();