Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      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.
     12  *
     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.
     24  */
     25 
     26 #include "config.h"
     27 
     28 #include "core/html/MediaDocument.h"
     29 
     30 #include "bindings/v8/ExceptionStatePlaceholder.h"
     31 #include "core/HTMLNames.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"
     45 
     46 namespace WebCore {
     47 
     48 using namespace HTMLNames;
     49 
     50 // FIXME: Share more code with PluginDocumentParser.
     51 class MediaDocumentParser : public RawDataDocumentParser {
     52 public:
     53     static PassRefPtrWillBeRawPtr<MediaDocumentParser> create(MediaDocument* document)
     54     {
     55         return adoptRefWillBeNoop(new MediaDocumentParser(document));
     56     }
     57 
     58 private:
     59     explicit MediaDocumentParser(Document* document)
     60         : RawDataDocumentParser(document)
     61         , m_didBuildDocumentStructure(false)
     62     {
     63     }
     64 
     65     virtual void appendBytes(const char*, size_t) OVERRIDE;
     66 
     67     void createDocumentStructure();
     68 
     69     bool m_didBuildDocumentStructure;
     70 };
     71 
     72 void MediaDocumentParser::createDocumentStructure()
     73 {
     74     ASSERT(document());
     75     RefPtrWillBeRawPtr<HTMLHtmlElement> rootElement = HTMLHtmlElement::create(*document());
     76     rootElement->insertedByParser();
     77     document()->appendChild(rootElement);
     78 
     79     if (document()->frame())
     80         document()->frame()->loader().dispatchDocumentElementAvailable();
     81 
     82     RefPtrWillBeRawPtr<HTMLHeadElement> head = HTMLHeadElement::create(*document());
     83     RefPtrWillBeRawPtr<HTMLMetaElement> meta = HTMLMetaElement::create(*document());
     84     meta->setAttribute(nameAttr, "viewport");
     85     meta->setAttribute(contentAttr, "width=device-width");
     86     head->appendChild(meta.release());
     87 
     88     RefPtrWillBeRawPtr<HTMLVideoElement> media = HTMLVideoElement::create(*document());
     89     media->setAttribute(controlsAttr, "");
     90     media->setAttribute(autoplayAttr, "");
     91     media->setAttribute(nameAttr, "media");
     92 
     93     RefPtrWillBeRawPtr<HTMLSourceElement> source = HTMLSourceElement::create(*document());
     94     source->setSrc(document()->url());
     95 
     96     if (DocumentLoader* loader = document()->loader())
     97         source->setType(loader->responseMIMEType());
     98 
     99     media->appendChild(source.release());
    100 
    101     RefPtrWillBeRawPtr<HTMLBodyElement> body = HTMLBodyElement::create(*document());
    102     body->appendChild(media.release());
    103 
    104     rootElement->appendChild(head.release());
    105     rootElement->appendChild(body.release());
    106 
    107     m_didBuildDocumentStructure = true;
    108 }
    109 
    110 void MediaDocumentParser::appendBytes(const char*, size_t)
    111 {
    112     if (m_didBuildDocumentStructure)
    113         return;
    114 
    115     createDocumentStructure();
    116     finish();
    117 }
    118 
    119 MediaDocument::MediaDocument(const DocumentInit& initializer)
    120     : HTMLDocument(initializer, MediaDocumentClass)
    121 {
    122     setCompatibilityMode(QuirksMode);
    123     lockCompatibilityMode();
    124 }
    125 
    126 PassRefPtrWillBeRawPtr<DocumentParser> MediaDocument::createParser()
    127 {
    128     return MediaDocumentParser::create(this);
    129 }
    130 
    131 void MediaDocument::defaultEventHandler(Event* event)
    132 {
    133     Node* targetNode = event->target()->toNode();
    134     if (!targetNode)
    135         return;
    136 
    137     if (event->type() == EventTypeNames::keydown && event->isKeyboardEvent()) {
    138         HTMLVideoElement* video = Traversal<HTMLVideoElement>::firstWithin(*targetNode);
    139         if (!video)
    140             return;
    141 
    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();
    147         }
    148     }
    149 }
    150 
    151 }
    152