initial import
[vuplus_webkit] / Source / WebCore / html / HTMLObjectElement.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2000 Stefan Schimanski (1Stein@gmx.de)
5  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2011 Apple Inc. All rights reserved.
6  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25 #include "HTMLObjectElement.h"
26
27 #include "Attribute.h"
28 #include "CSSValueKeywords.h"
29 #include "EventNames.h"
30 #include "ExceptionCode.h"
31 #include "FormDataList.h"
32 #include "Frame.h"
33 #include "HTMLDocument.h"
34 #include "HTMLFormElement.h"
35 #include "HTMLImageLoader.h"
36 #include "HTMLMetaElement.h"
37 #include "HTMLNames.h"
38 #include "HTMLParamElement.h"
39 #include "HTMLParserIdioms.h"
40 #include "MIMETypeRegistry.h"
41 #include "NodeList.h"
42 #include "Page.h"
43 #include "PluginViewBase.h"
44 #include "RenderEmbeddedObject.h"
45 #include "RenderImage.h"
46 #include "RenderWidget.h"
47 #include "ScriptEventListener.h"
48 #include "Settings.h"
49 #include "Text.h"
50 #include "Widget.h"
51
52 namespace WebCore {
53
54 using namespace HTMLNames;
55
56 inline HTMLObjectElement::HTMLObjectElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form, bool createdByParser) 
57     : HTMLPlugInImageElement(tagName, document, createdByParser, ShouldNotPreferPlugInsForImages)
58     , FormAssociatedElement(form)
59     , m_docNamedItem(true)
60     , m_useFallbackContent(false)
61 {
62     ASSERT(hasTagName(objectTag));
63     if (!this->form())
64         setForm(findFormAncestor());
65     if (this->form())
66         this->form()->registerFormElement(this);
67 }
68
69 inline HTMLObjectElement::~HTMLObjectElement()
70 {
71     if (form())
72         form()->removeFormElement(this);
73 }
74
75 PassRefPtr<HTMLObjectElement> HTMLObjectElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form, bool createdByParser)
76 {
77     return adoptRef(new HTMLObjectElement(tagName, document, form, createdByParser));
78 }
79
80 RenderWidget* HTMLObjectElement::renderWidgetForJSBindings()
81 {
82     document()->updateLayoutIgnorePendingStylesheets();
83     return renderPart(); // This will return 0 if the renderer is not a RenderPart.
84 }
85
86 void HTMLObjectElement::parseMappedAttribute(Attribute* attr)
87 {
88     if (attr->name() == formAttr)
89         formAttributeChanged();
90     else if (attr->name() == typeAttr) {
91         m_serviceType = attr->value().lower();
92         size_t pos = m_serviceType.find(";");
93         if (pos != notFound)
94             m_serviceType = m_serviceType.left(pos);
95         if (renderer())
96             setNeedsWidgetUpdate(true);
97         if (!isImageType() && m_imageLoader)
98             m_imageLoader.clear();
99     } else if (attr->name() == dataAttr) {
100         m_url = stripLeadingAndTrailingHTMLSpaces(attr->value());
101         if (renderer()) {
102             setNeedsWidgetUpdate(true);
103             if (isImageType()) {
104                 if (!m_imageLoader)
105                     m_imageLoader = adoptPtr(new HTMLImageLoader(this));
106                 m_imageLoader->updateFromElementIgnoringPreviousError();
107             }
108         }
109     } else if (attr->name() == classidAttr) {
110         m_classId = attr->value();
111         if (renderer())
112             setNeedsWidgetUpdate(true);
113     } else if (attr->name() == onloadAttr)
114         setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
115     else if (attr->name() == onbeforeloadAttr)
116         setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, attr));
117     else if (attr->name() == nameAttr) {
118         const AtomicString& newName = attr->value();
119         if (isDocNamedItem() && inDocument() && document()->isHTMLDocument()) {
120             HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
121             document->removeNamedItem(m_name);
122             document->addNamedItem(newName);
123         }
124         m_name = newName;
125     } else if (attr->name() == borderAttr)
126         applyBorderAttribute(attr);
127     else if (isIdAttributeName(attr->name())) {
128         const AtomicString& newId = attr->value();
129         if (isDocNamedItem() && inDocument() && document()->isHTMLDocument()) {
130             HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
131             document->removeExtraNamedItem(m_id);
132             document->addExtraNamedItem(newId);
133         }
134         m_id = newId;
135         // also call superclass
136         HTMLPlugInImageElement::parseMappedAttribute(attr);
137     } else
138         HTMLPlugInImageElement::parseMappedAttribute(attr);
139 }
140
141 static void mapDataParamToSrc(Vector<String>* paramNames, Vector<String>* paramValues)
142 {
143     // Some plugins don't understand the "data" attribute of the OBJECT tag (i.e. Real and WMP
144     // require "src" attribute).
145     int srcIndex = -1, dataIndex = -1;
146     for (unsigned int i = 0; i < paramNames->size(); ++i) {
147         if (equalIgnoringCase((*paramNames)[i], "src"))
148             srcIndex = i;
149         else if (equalIgnoringCase((*paramNames)[i], "data"))
150             dataIndex = i;
151     }
152     
153     if (srcIndex == -1 && dataIndex != -1) {
154         paramNames->append("src");
155         paramValues->append((*paramValues)[dataIndex]);
156     }
157 }
158
159 // FIXME: This function should not deal with url or serviceType!
160 void HTMLObjectElement::parametersForPlugin(Vector<String>& paramNames, Vector<String>& paramValues, String& url, String& serviceType)
161 {
162     HashSet<StringImpl*, CaseFoldingHash> uniqueParamNames;
163     String urlParameter;
164     
165     // Scan the PARAM children and store their name/value pairs.
166     // Get the URL and type from the params if we don't already have them.
167     for (Node* child = firstChild(); child; child = child->nextSibling()) {
168         if (!child->hasTagName(paramTag))
169             continue;
170
171         HTMLParamElement* p = static_cast<HTMLParamElement*>(child);
172         String name = p->name();
173         if (name.isEmpty())
174             continue;
175
176         uniqueParamNames.add(name.impl());
177         paramNames.append(p->name());
178         paramValues.append(p->value());
179
180         // FIXME: url adjustment does not belong in this function.
181         if (url.isEmpty() && urlParameter.isEmpty() && (equalIgnoringCase(name, "src") || equalIgnoringCase(name, "movie") || equalIgnoringCase(name, "code") || equalIgnoringCase(name, "url")))
182             urlParameter = stripLeadingAndTrailingHTMLSpaces(p->value());
183         // FIXME: serviceType calculation does not belong in this function.
184         if (serviceType.isEmpty() && equalIgnoringCase(name, "type")) {
185             serviceType = p->value();
186             size_t pos = serviceType.find(";");
187             if (pos != notFound)
188                 serviceType = serviceType.left(pos);
189         }
190     }
191     
192     // When OBJECT is used for an applet via Sun's Java plugin, the CODEBASE attribute in the tag
193     // points to the Java plugin itself (an ActiveX component) while the actual applet CODEBASE is
194     // in a PARAM tag. See <http://java.sun.com/products/plugin/1.2/docs/tags.html>. This means
195     // we have to explicitly suppress the tag's CODEBASE attribute if there is none in a PARAM,
196     // else our Java plugin will misinterpret it. [4004531]
197     String codebase;
198     if (MIMETypeRegistry::isJavaAppletMIMEType(serviceType)) {
199         codebase = "codebase";
200         uniqueParamNames.add(codebase.impl()); // pretend we found it in a PARAM already
201     }
202     
203     // Turn the attributes of the <object> element into arrays, but don't override <param> values.
204     NamedNodeMap* attributes = this->attributes(true);
205     if (attributes) {
206         for (unsigned i = 0; i < attributes->length(); ++i) {
207             Attribute* it = attributes->attributeItem(i);
208             const AtomicString& name = it->name().localName();
209             if (!uniqueParamNames.contains(name.impl())) {
210                 paramNames.append(name.string());
211                 paramValues.append(it->value().string());
212             }
213         }
214     }
215     
216     mapDataParamToSrc(&paramNames, &paramValues);
217     
218     // HTML5 says that an object resource's URL is specified by the object's data
219     // attribute, not by a param element. However, for compatibility, allow the
220     // resource's URL to be given by a param named "src", "movie", "code" or "url"
221     // if we know that resource points to a plug-in.
222     if (url.isEmpty() && !urlParameter.isEmpty()) {
223         SubframeLoader* loader = document()->frame()->loader()->subframeLoader();
224         if (loader->resourceWillUsePlugin(urlParameter, serviceType, shouldPreferPlugInsForImages()))
225             url = urlParameter;
226     }
227 }
228
229     
230 bool HTMLObjectElement::hasFallbackContent() const
231 {
232     for (Node* child = firstChild(); child; child = child->nextSibling()) {
233         // Ignore whitespace-only text, and <param> tags, any other content is fallback content.
234         if (child->isTextNode()) {
235             if (!static_cast<Text*>(child)->containsOnlyWhitespace())
236                 return true;
237         } else if (!child->hasTagName(paramTag))
238             return true;
239     }
240     return false;
241 }
242     
243 bool HTMLObjectElement::shouldAllowQuickTimeClassIdQuirk()
244 {
245     // This site-specific hack maintains compatibility with Mac OS X Wiki Server,
246     // which embeds QuickTime movies using an object tag containing QuickTime's
247     // ActiveX classid. Treat this classid as valid only if OS X Server's unique
248     // 'generator' meta tag is present. Only apply this quirk if there is no
249     // fallback content, which ensures the quirk will disable itself if Wiki
250     // Server is updated to generate an alternate embed tag as fallback content.
251     if (!document()->page()
252         || !document()->page()->settings()->needsSiteSpecificQuirks()
253         || hasFallbackContent()
254         || !equalIgnoringCase(classId(), "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"))
255         return false;
256
257     RefPtr<NodeList> metaElements = document()->getElementsByTagName(HTMLNames::metaTag.localName());
258     unsigned length = metaElements->length();
259     for (unsigned i = 0; i < length; ++i) {
260         ASSERT(metaElements->item(i)->isHTMLElement());
261         HTMLMetaElement* metaElement = static_cast<HTMLMetaElement*>(metaElements->item(i));
262         if (equalIgnoringCase(metaElement->name(), "generator") && metaElement->content().startsWith("Mac OS X Server Web Services Server", false))
263             return true;
264     }
265     
266     return false;
267 }
268     
269 bool HTMLObjectElement::hasValidClassId()
270 {
271 #if PLATFORM(QT)
272     if (equalIgnoringCase(serviceType(), "application/x-qt-plugin") || equalIgnoringCase(serviceType(), "application/x-qt-styled-widget"))
273         return true;
274 #endif
275
276     if (MIMETypeRegistry::isJavaAppletMIMEType(serviceType()) && classId().startsWith("java:", false))
277         return true;
278     
279     if (shouldAllowQuickTimeClassIdQuirk())
280         return true;
281
282     // HTML5 says that fallback content should be rendered if a non-empty
283     // classid is specified for which the UA can't find a suitable plug-in.
284     return classId().isEmpty();
285 }
286
287 // FIXME: This should be unified with HTMLEmbedElement::updateWidget and
288 // moved down into HTMLPluginImageElement.cpp
289 void HTMLObjectElement::updateWidget(PluginCreationOption pluginCreationOption)
290 {
291     ASSERT(!renderEmbeddedObject()->pluginCrashedOrWasMissing());
292     // FIXME: We should ASSERT(needsWidgetUpdate()), but currently
293     // FrameView::updateWidget() calls updateWidget(false) without checking if
294     // the widget actually needs updating!
295     setNeedsWidgetUpdate(false);
296     // FIXME: This should ASSERT isFinishedParsingChildren() instead.
297     if (!isFinishedParsingChildren())
298         return;
299     
300     String url = this->url();
301     String serviceType = this->serviceType();
302
303     // FIXME: These should be joined into a PluginParameters class.
304     Vector<String> paramNames;
305     Vector<String> paramValues;
306     parametersForPlugin(paramNames, paramValues, url, serviceType);
307
308     // Note: url is modified above by parametersForPlugin.
309     if (!allowedToLoadFrameURL(url))
310         return;
311
312     bool fallbackContent = hasFallbackContent();
313     renderEmbeddedObject()->setHasFallbackContent(fallbackContent);
314
315     if (pluginCreationOption == CreateOnlyNonNetscapePlugins && wouldLoadAsNetscapePlugin(url, serviceType))
316         return;
317
318     ASSERT(!m_inBeforeLoadEventHandler);
319     m_inBeforeLoadEventHandler = true;
320     bool beforeLoadAllowedLoad = dispatchBeforeLoadEvent(url);
321     m_inBeforeLoadEventHandler = false;
322
323     // beforeload events can modify the DOM, potentially causing
324     // RenderWidget::destroy() to be called.  Ensure we haven't been
325     // destroyed before continuing.
326     // FIXME: Should this render fallback content?
327     if (!renderer())
328         return;
329
330     SubframeLoader* loader = document()->frame()->loader()->subframeLoader();
331     bool success = beforeLoadAllowedLoad && hasValidClassId() && loader->requestObject(this, url, getAttribute(nameAttr), serviceType, paramNames, paramValues);
332
333     if (!success && fallbackContent)
334         renderFallbackContent();
335 }
336
337 bool HTMLObjectElement::rendererIsNeeded(const NodeRenderingContext& context)
338 {
339     // FIXME: This check should not be needed, detached documents never render!
340     Frame* frame = document()->frame();
341     if (!frame)
342         return false;
343
344     return HTMLPlugInImageElement::rendererIsNeeded(context);
345 }
346
347 void HTMLObjectElement::insertedIntoDocument()
348 {
349     HTMLPlugInImageElement::insertedIntoDocument();
350     if (!inDocument())
351         return;
352
353     if (isDocNamedItem() && document()->isHTMLDocument()) {
354         HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
355         document->addNamedItem(m_name);
356         document->addExtraNamedItem(m_id);
357     }
358
359     FormAssociatedElement::insertedIntoDocument();
360 }
361
362 void HTMLObjectElement::removedFromDocument()
363 {
364     if (isDocNamedItem() && document()->isHTMLDocument()) {
365         HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
366         document->removeNamedItem(m_name);
367         document->removeExtraNamedItem(m_id);
368     }
369
370     HTMLPlugInImageElement::removedFromDocument();
371     FormAssociatedElement::removedFromDocument();
372 }
373
374 void HTMLObjectElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
375 {
376     updateDocNamedItem();
377     if (inDocument() && !useFallbackContent()) {
378         setNeedsWidgetUpdate(true);
379         setNeedsStyleRecalc();
380     }
381     HTMLPlugInImageElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
382 }
383
384 bool HTMLObjectElement::isURLAttribute(Attribute *attr) const
385 {
386     return (attr->name() == dataAttr || (attr->name() == usemapAttr && attr->value().string()[0] != '#'));
387 }
388
389 const QualifiedName& HTMLObjectElement::imageSourceAttributeName() const
390 {
391     return dataAttr;
392 }
393
394 void HTMLObjectElement::renderFallbackContent()
395 {
396     if (useFallbackContent())
397         return;
398     
399     if (!inDocument())
400         return;
401
402     // Before we give up and use fallback content, check to see if this is a MIME type issue.
403     if (m_imageLoader && m_imageLoader->image() && m_imageLoader->image()->status() != CachedResource::LoadError) {
404         m_serviceType = m_imageLoader->image()->response().mimeType();
405         if (!isImageType()) {
406             // If we don't think we have an image type anymore, then clear the image from the loader.
407             m_imageLoader->setImage(0);
408             reattach();
409             return;
410         }
411     }
412
413     m_useFallbackContent = true;
414
415     // FIXME: Style gets recalculated which is suboptimal.
416     detach();
417     attach();
418 }
419
420 // FIXME: This should be removed, all callers are almost certainly wrong.
421 static bool isRecognizedTagName(const QualifiedName& tagName)
422 {
423     DEFINE_STATIC_LOCAL(HashSet<AtomicStringImpl*>, tagList, ());
424     if (tagList.isEmpty()) {
425         size_t tagCount = 0;
426         QualifiedName** tags = HTMLNames::getHTMLTags(&tagCount);
427         for (size_t i = 0; i < tagCount; i++) {
428             if (*tags[i] == bgsoundTag
429                 || *tags[i] == commandTag
430                 || *tags[i] == detailsTag
431                 || *tags[i] == figcaptionTag
432                 || *tags[i] == figureTag
433                 || *tags[i] == summaryTag
434                 || *tags[i] == trackTag) {
435                 // Even though we have atoms for these tags, we don't want to
436                 // treat them as "recognized tags" for the purpose of parsing
437                 // because that changes how we parse documents.
438                 continue;
439             }
440             tagList.add(tags[i]->localName().impl());
441         }
442     }
443     return tagList.contains(tagName.localName().impl());
444 }
445
446 void HTMLObjectElement::updateDocNamedItem()
447 {
448     // The rule is "<object> elements with no children other than
449     // <param> elements, unknown elements and whitespace can be
450     // found by name in a document, and other <object> elements cannot."
451     bool wasNamedItem = m_docNamedItem;
452     bool isNamedItem = true;
453     Node* child = firstChild();
454     while (child && isNamedItem) {
455         if (child->isElementNode()) {
456             Element* element = static_cast<Element*>(child);
457             // FIXME: Use of isRecognizedTagName is almost certainly wrong here.
458             if (isRecognizedTagName(element->tagQName()) && !element->hasTagName(paramTag))
459                 isNamedItem = false;
460         } else if (child->isTextNode()) {
461             if (!static_cast<Text*>(child)->containsOnlyWhitespace())
462                 isNamedItem = false;
463         } else
464             isNamedItem = false;
465         child = child->nextSibling();
466     }
467     if (isNamedItem != wasNamedItem && document()->isHTMLDocument()) {
468         HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
469         if (isNamedItem) {
470             document->addNamedItem(m_name);
471             document->addExtraNamedItem(m_id);
472         } else {
473             document->removeNamedItem(m_name);
474             document->removeExtraNamedItem(m_id);
475         }
476     }
477     m_docNamedItem = isNamedItem;
478 }
479
480 bool HTMLObjectElement::containsJavaApplet() const
481 {
482     if (MIMETypeRegistry::isJavaAppletMIMEType(getAttribute(typeAttr)))
483         return true;
484         
485     for (Element* child = firstElementChild(); child; child = child->nextElementSibling()) {
486         if (child->hasTagName(paramTag)
487                 && equalIgnoringCase(child->getAttribute(nameAttr), "type")
488                 && MIMETypeRegistry::isJavaAppletMIMEType(child->getAttribute(valueAttr).string()))
489             return true;
490         if (child->hasTagName(objectTag)
491                 && static_cast<HTMLObjectElement*>(child)->containsJavaApplet())
492             return true;
493         if (child->hasTagName(appletTag))
494             return true;
495     }
496     
497     return false;
498 }
499
500 void HTMLObjectElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
501 {
502     HTMLPlugInImageElement::addSubresourceAttributeURLs(urls);
503
504     addSubresourceURL(urls, document()->completeURL(getAttribute(dataAttr)));
505
506     // FIXME: Passing a string that starts with "#" to the completeURL function does
507     // not seem like it would work. The image element has similar but not identical code.
508     const AtomicString& useMap = getAttribute(usemapAttr);
509     if (useMap.startsWith("#"))
510         addSubresourceURL(urls, document()->completeURL(useMap));
511 }
512
513 void HTMLObjectElement::willMoveToNewOwnerDocument()
514 {
515     FormAssociatedElement::willMoveToNewOwnerDocument();
516     HTMLPlugInImageElement::willMoveToNewOwnerDocument();
517 }
518
519 void HTMLObjectElement::insertedIntoTree(bool deep)
520 {
521     FormAssociatedElement::insertedIntoTree();
522     HTMLPlugInImageElement::insertedIntoTree(deep);
523 }
524
525 void HTMLObjectElement::removedFromTree(bool deep)
526 {
527     FormAssociatedElement::removedFromTree();
528     HTMLPlugInImageElement::removedFromTree(deep);
529 }
530
531 bool HTMLObjectElement::appendFormData(FormDataList& encoding, bool)
532 {
533     if (name().isEmpty())
534         return false;
535
536     Widget* widget = pluginWidget();
537     if (!widget || !widget->isPluginViewBase())
538         return false;
539     String value;
540     if (!static_cast<PluginViewBase*>(widget)->getFormValue(value))
541         return false;
542     encoding.appendData(name(), value);
543     return true;
544 }
545
546 const AtomicString& HTMLObjectElement::formControlName() const
547 {
548     return m_name.isNull() ? emptyAtom : m_name;
549 }
550
551 HTMLFormElement* HTMLObjectElement::virtualForm() const
552 {
553     return FormAssociatedElement::form();
554 }
555
556 }