2 * Copyright (C) 2006, 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 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.
26 #include "PluginDocument.h"
28 #include "DocumentLoader.h"
30 #include "FrameLoaderClient.h"
31 #include "FrameView.h"
32 #include "HTMLEmbedElement.h"
33 #include "HTMLHtmlElement.h"
34 #include "HTMLNames.h"
35 #include "MainResourceLoader.h"
38 #include "RawDataDocumentParser.h"
39 #include "RenderEmbeddedObject.h"
44 using namespace HTMLNames;
46 // FIXME: Share more code with MediaDocumentParser.
47 class PluginDocumentParser : public RawDataDocumentParser {
49 static PassRefPtr<PluginDocumentParser> create(PluginDocument* document)
51 return adoptRef(new PluginDocumentParser(document));
55 PluginDocumentParser(Document* document)
56 : RawDataDocumentParser(document)
61 virtual void appendBytes(DocumentWriter*, const char*, size_t);
63 void createDocumentStructure();
65 HTMLEmbedElement* m_embedElement;
68 void PluginDocumentParser::createDocumentStructure()
71 RefPtr<Element> rootElement = document()->createElement(htmlTag, false);
72 document()->appendChild(rootElement, ec);
73 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
74 static_cast<HTMLHtmlElement*>(rootElement.get())->insertedByParser();
77 if (document()->frame() && document()->frame()->loader())
78 document()->frame()->loader()->dispatchDocumentElementAvailable();
80 RefPtr<Element> body = document()->createElement(bodyTag, false);
81 body->setAttribute(marginwidthAttr, "0");
82 body->setAttribute(marginheightAttr, "0");
83 body->setAttribute(bgcolorAttr, "rgb(38,38,38)");
85 rootElement->appendChild(body, ec);
87 RefPtr<Element> embedElement = document()->createElement(embedTag, false);
89 m_embedElement = static_cast<HTMLEmbedElement*>(embedElement.get());
90 m_embedElement->setAttribute(widthAttr, "100%");
91 m_embedElement->setAttribute(heightAttr, "100%");
93 m_embedElement->setAttribute(nameAttr, "plugin");
94 m_embedElement->setAttribute(srcAttr, document()->url().string());
96 DocumentLoader* loader = document()->loader();
99 m_embedElement->setAttribute(typeAttr, loader->writer()->mimeType());
101 static_cast<PluginDocument*>(document())->setPluginNode(m_embedElement);
103 body->appendChild(embedElement, ec);
106 void PluginDocumentParser::appendBytes(DocumentWriter*, const char*, size_t)
111 createDocumentStructure();
113 Frame* frame = document()->frame();
116 Settings* settings = frame->settings();
117 if (!settings || !frame->loader()->subframeLoader()->allowPlugins(NotAboutToInstantiatePlugin))
120 document()->updateLayout();
122 // Below we assume that renderer->widget() to have been created by
123 // document()->updateLayout(). However, in some cases, updateLayout() will
124 // recurse too many times and delay its post-layout tasks (such as creating
125 // the widget). Here we kick off the pending post-layout tasks so that we
126 // can synchronously redirect data to the plugin.
127 frame->view()->flushAnyPendingPostLayoutTasks();
129 if (RenderPart* renderer = m_embedElement->renderPart()) {
130 if (Widget* widget = renderer->widget()) {
131 frame->loader()->client()->redirectDataToPlugin(widget);
132 // In a plugin document, the main resource is the plugin. If we have a null widget, that means
133 // the loading of the plugin was cancelled, which gives us a null mainResourceLoader(), so we
134 // need to have this call in a null check of the widget or of mainResourceLoader().
135 frame->loader()->activeDocumentLoader()->mainResourceLoader()->setShouldBufferData(DoNotBufferData);
142 PluginDocument::PluginDocument(Frame* frame, const KURL& url)
143 : HTMLDocument(frame, url)
144 , m_shouldLoadPluginManually(true)
146 setCompatibilityMode(QuirksMode);
147 lockCompatibilityMode();
150 PassRefPtr<DocumentParser> PluginDocument::createParser()
152 return PluginDocumentParser::create(this);
155 Widget* PluginDocument::pluginWidget()
157 if (m_pluginNode && m_pluginNode->renderer()) {
158 ASSERT(m_pluginNode->renderer()->isEmbeddedObject());
159 return toRenderEmbeddedObject(m_pluginNode->renderer())->widget();
164 Node* PluginDocument::pluginNode()
166 return m_pluginNode.get();
169 void PluginDocument::detach()
171 // Release the plugin node so that we don't have a circular reference.
173 HTMLDocument::detach();
176 void PluginDocument::cancelManualPluginLoad()
178 // PluginDocument::cancelManualPluginLoad should only be called once, but there are issues
179 // with how many times we call beforeload on object elements. <rdar://problem/8441094>.
180 if (!shouldLoadPluginManually())
183 frame()->loader()->activeDocumentLoader()->mainResourceLoader()->cancel();
184 setShouldLoadPluginManually(false);