WebCore/loader/PluginDocument.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2006, 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 COMPUTER, 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 COMPUTER, INC. OR
       
    17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    19  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    20  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    23  */
       
    24 
       
    25 #include "config.h"
       
    26 #include "PluginDocument.h"
       
    27 
       
    28 #include "DocumentLoader.h"
       
    29 #include "Frame.h"
       
    30 #include "FrameLoaderClient.h"
       
    31 #include "HTMLEmbedElement.h"
       
    32 #include "HTMLNames.h"
       
    33 #include "MainResourceLoader.h"
       
    34 #include "Page.h"
       
    35 #include "RawDataDocumentParser.h"
       
    36 #include "RenderEmbeddedObject.h"
       
    37 #include "Settings.h"
       
    38 
       
    39 namespace WebCore {
       
    40     
       
    41 using namespace HTMLNames;
       
    42 
       
    43 // FIXME: Share more code with MediaDocumentParser.
       
    44 class PluginDocumentParser : public RawDataDocumentParser {
       
    45 public:
       
    46     PluginDocumentParser(Document* document)
       
    47         : RawDataDocumentParser(document)
       
    48         , m_embedElement(0)
       
    49     {
       
    50     }
       
    51 
       
    52     static Widget* pluginWidgetFromDocument(Document*);
       
    53 
       
    54 private:
       
    55     virtual void appendBytes(DocumentWriter*, const char*, int, bool);
       
    56 
       
    57     void createDocumentStructure();
       
    58 
       
    59     HTMLEmbedElement* m_embedElement;
       
    60 };
       
    61 
       
    62 Widget* PluginDocumentParser::pluginWidgetFromDocument(Document* doc)
       
    63 {
       
    64     ASSERT(doc);
       
    65     RefPtr<Element> body = doc->body();
       
    66     if (body) {
       
    67         RefPtr<Node> node = body->firstChild();
       
    68         if (node && node->renderer()) {
       
    69             ASSERT(node->renderer()->isEmbeddedObject());
       
    70             return toRenderEmbeddedObject(node->renderer())->widget();
       
    71         }
       
    72     }
       
    73     return 0;
       
    74 }
       
    75 
       
    76 void PluginDocumentParser::createDocumentStructure()
       
    77 {
       
    78     ExceptionCode ec;
       
    79     RefPtr<Element> rootElement = document()->createElement(htmlTag, false);
       
    80     document()->appendChild(rootElement, ec);
       
    81 
       
    82     RefPtr<Element> body = document()->createElement(bodyTag, false);
       
    83     body->setAttribute(marginwidthAttr, "0");
       
    84     body->setAttribute(marginheightAttr, "0");
       
    85     body->setAttribute(bgcolorAttr, "rgb(38,38,38)");
       
    86 
       
    87     rootElement->appendChild(body, ec);
       
    88         
       
    89     RefPtr<Element> embedElement = document()->createElement(embedTag, false);
       
    90         
       
    91     m_embedElement = static_cast<HTMLEmbedElement*>(embedElement.get());
       
    92     m_embedElement->setAttribute(widthAttr, "100%");
       
    93     m_embedElement->setAttribute(heightAttr, "100%");
       
    94     
       
    95     m_embedElement->setAttribute(nameAttr, "plugin");
       
    96     m_embedElement->setAttribute(srcAttr, document()->url().string());
       
    97     m_embedElement->setAttribute(typeAttr, document()->frame()->loader()->writer()->mimeType());
       
    98     
       
    99     body->appendChild(embedElement, ec);    
       
   100 }
       
   101 
       
   102 void PluginDocumentParser::appendBytes(DocumentWriter*, const char*, int, bool)
       
   103 {
       
   104     ASSERT(!m_embedElement);
       
   105     if (m_embedElement)
       
   106         return;
       
   107 
       
   108     createDocumentStructure();
       
   109 
       
   110     Frame* frame = document()->frame();
       
   111     if (!frame)
       
   112         return;
       
   113     Settings* settings = frame->settings();
       
   114     if (!settings || !frame->loader()->subframeLoader()->allowPlugins(NotAboutToInstantiatePlugin))
       
   115         return;
       
   116 
       
   117     document()->updateLayout();
       
   118 
       
   119     if (RenderWidget* renderer = toRenderWidget(m_embedElement->renderer())) {
       
   120         frame->loader()->client()->redirectDataToPlugin(renderer->widget());
       
   121         frame->loader()->activeDocumentLoader()->mainResourceLoader()->setShouldBufferData(false);
       
   122     }
       
   123 
       
   124     finish();
       
   125 }
       
   126 
       
   127 PluginDocument::PluginDocument(Frame* frame, const KURL& url)
       
   128     : HTMLDocument(frame, url)
       
   129 {
       
   130     setParseMode(Compat);
       
   131 }
       
   132     
       
   133 DocumentParser* PluginDocument::createParser()
       
   134 {
       
   135     return new PluginDocumentParser(this);
       
   136 }
       
   137 
       
   138 Widget* PluginDocument::pluginWidget()
       
   139 {
       
   140     return PluginDocumentParser::pluginWidgetFromDocument(this);
       
   141 }
       
   142 
       
   143 Node* PluginDocument::pluginNode()
       
   144 {
       
   145     RefPtr<Element> body_element = body();
       
   146     if (body_element)
       
   147         return body_element->firstChild();
       
   148 
       
   149     return 0;
       
   150 }
       
   151 
       
   152 }