1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Simon Hausmann <hausmann@kde.org>
4 * (C) 2000 Stefan Schimanski (1Stein@gmx.de)
5 * Copyright (C) 2004, 2005, 2006, 2008, 2009 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24 #include "config.h"
25 #include "RenderPartObject.h"
26
27 #include "Frame.h"
28 #include "FrameLoaderClient.h"
29 #include "HTMLEmbedElement.h"
30 #include "HTMLIFrameElement.h"
31 #include "HTMLNames.h"
32 #include "HTMLObjectElement.h"
33 #include "HTMLParamElement.h"
34 #include "MIMETypeRegistry.h"
35 #include "Page.h"
36 #include "PluginData.h"
37 #include "RenderView.h"
38 #include "Text.h"
39
40 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
41 #include "HTMLVideoElement.h"
42 #endif
43
44 namespace WebCore {
45
46 using namespace HTMLNames;
47
RenderPartObject(Element * element)48 RenderPartObject::RenderPartObject(Element* element)
49 : RenderPart(element)
50 {
51 // init RenderObject attributes
52 setInline(true);
53 m_hasFallbackContent = false;
54
55 if (element->hasTagName(embedTag) || element->hasTagName(objectTag))
56 view()->frameView()->setIsVisuallyNonEmpty();
57 }
58
~RenderPartObject()59 RenderPartObject::~RenderPartObject()
60 {
61 if (frameView())
62 frameView()->removeWidgetToUpdate(this);
63 }
64
isURLAllowed(Document * doc,const String & url)65 static bool isURLAllowed(Document* doc, const String& url)
66 {
67 if (doc->frame()->page()->frameCount() >= 200)
68 return false;
69
70 // We allow one level of self-reference because some sites depend on that.
71 // But we don't allow more than one.
72 KURL completeURL = doc->completeURL(url);
73 bool foundSelfReference = false;
74 for (Frame* frame = doc->frame(); frame; frame = frame->tree()->parent()) {
75 if (equalIgnoringFragmentIdentifier(frame->loader()->url(), completeURL)) {
76 if (foundSelfReference)
77 return false;
78 foundSelfReference = true;
79 }
80 }
81 return true;
82 }
83
84 typedef HashMap<String, String, CaseFoldingHash> ClassIdToTypeMap;
85
createClassIdToTypeMap()86 static ClassIdToTypeMap* createClassIdToTypeMap()
87 {
88 ClassIdToTypeMap* map = new ClassIdToTypeMap;
89 map->add("clsid:D27CDB6E-AE6D-11CF-96B8-444553540000", "application/x-shockwave-flash");
90 map->add("clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA", "audio/x-pn-realaudio-plugin");
91 map->add("clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B", "video/quicktime");
92 map->add("clsid:166B1BCA-3F9C-11CF-8075-444553540000", "application/x-director");
93 #if ENABLE(ACTIVEX_TYPE_CONVERSION_WMPLAYER)
94 map->add("clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6", "application/x-mplayer2");
95 map->add("clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95", "application/x-mplayer2");
96 #endif
97 return map;
98 }
99
activeXType()100 static const String& activeXType()
101 {
102 DEFINE_STATIC_LOCAL(String, activeXType, ("application/x-oleobject"));
103 return activeXType;
104 }
105
havePlugin(const PluginData * pluginData,const String & type)106 static inline bool havePlugin(const PluginData* pluginData, const String& type)
107 {
108 return pluginData && !type.isEmpty() && pluginData->supportsMimeType(type);
109 }
110
serviceTypeForClassId(const String & classId,const PluginData * pluginData)111 static String serviceTypeForClassId(const String& classId, const PluginData* pluginData)
112 {
113 // Return early if classId is empty (since we won't do anything below).
114 // Furthermore, if classId is null, calling get() below will crash.
115 if (classId.isEmpty())
116 return String();
117
118 static ClassIdToTypeMap* map = createClassIdToTypeMap();
119 String type = map->get(classId);
120
121 // If we do have a plug-in that supports generic ActiveX content and don't have a plug-in
122 // for the MIME type we came up with, ignore the MIME type we came up with and just use
123 // the ActiveX type.
124 if (havePlugin(pluginData, activeXType()) && !havePlugin(pluginData, type))
125 return activeXType();
126
127 return type;
128 }
129
shouldUseEmbedDescendant(HTMLObjectElement * objectElement,const PluginData * pluginData)130 static inline bool shouldUseEmbedDescendant(HTMLObjectElement* objectElement, const PluginData* pluginData)
131 {
132 #if PLATFORM(MAC)
133 UNUSED_PARAM(objectElement);
134 UNUSED_PARAM(pluginData);
135 // On Mac, we always want to use the embed descendant.
136 return true;
137 #else
138 // If we have both an <object> and <embed>, we always want to use the <embed> except when we have
139 // an ActiveX plug-in and plan to use it.
140 return !(havePlugin(pluginData, activeXType())
141 && serviceTypeForClassId(objectElement->classId(), pluginData) == activeXType());
142 #endif
143 }
144
mapDataParamToSrc(Vector<String> * paramNames,Vector<String> * paramValues)145 static void mapDataParamToSrc(Vector<String>* paramNames, Vector<String>* paramValues)
146 {
147 // Some plugins don't understand the "data" attribute of the OBJECT tag (i.e. Real and WMP
148 // require "src" attribute).
149 int srcIndex = -1, dataIndex = -1;
150 for (unsigned int i = 0; i < paramNames->size(); ++i) {
151 if (equalIgnoringCase((*paramNames)[i], "src"))
152 srcIndex = i;
153 else if (equalIgnoringCase((*paramNames)[i], "data"))
154 dataIndex = i;
155 }
156
157 if (srcIndex == -1 && dataIndex != -1) {
158 paramNames->append("src");
159 paramValues->append((*paramValues)[dataIndex]);
160 }
161 }
162
updateWidget(bool onlyCreateNonNetscapePlugins)163 void RenderPartObject::updateWidget(bool onlyCreateNonNetscapePlugins)
164 {
165 String url;
166 String serviceType;
167 Vector<String> paramNames;
168 Vector<String> paramValues;
169 Frame* frame = frameView()->frame();
170
171 if (node()->hasTagName(objectTag)) {
172 HTMLObjectElement* o = static_cast<HTMLObjectElement*>(node());
173
174 o->setNeedWidgetUpdate(false);
175 if (!o->isFinishedParsingChildren())
176 return;
177
178 // Check for a child EMBED tag.
179 HTMLEmbedElement* embed = 0;
180 const PluginData* pluginData = frame->page()->pluginData();
181 if (shouldUseEmbedDescendant(o, pluginData)) {
182 for (Node* child = o->firstChild(); child; ) {
183 if (child->hasTagName(embedTag)) {
184 embed = static_cast<HTMLEmbedElement*>(child);
185 break;
186 } else if (child->hasTagName(objectTag))
187 child = child->nextSibling(); // Don't descend into nested OBJECT tags
188 else
189 child = child->traverseNextNode(o); // Otherwise descend (EMBEDs may be inside COMMENT tags)
190 }
191 }
192
193 // Use the attributes from the EMBED tag instead of the OBJECT tag including WIDTH and HEIGHT.
194 HTMLElement *embedOrObject;
195 if (embed) {
196 embedOrObject = (HTMLElement *)embed;
197 url = embed->url();
198 serviceType = embed->serviceType();
199 } else
200 embedOrObject = (HTMLElement *)o;
201
202 // If there was no URL or type defined in EMBED, try the OBJECT tag.
203 if (url.isEmpty())
204 url = o->url();
205 if (serviceType.isEmpty())
206 serviceType = o->serviceType();
207
208 HashSet<StringImpl*, CaseFoldingHash> uniqueParamNames;
209
210 // Scan the PARAM children.
211 // Get the URL and type from the params if we don't already have them.
212 // Get the attributes from the params if there is no EMBED tag.
213 Node *child = o->firstChild();
214 while (child && (url.isEmpty() || serviceType.isEmpty() || !embed)) {
215 if (child->hasTagName(paramTag)) {
216 HTMLParamElement* p = static_cast<HTMLParamElement*>(child);
217 String name = p->name();
218 if (url.isEmpty() && (equalIgnoringCase(name, "src") || equalIgnoringCase(name, "movie") || equalIgnoringCase(name, "code") || equalIgnoringCase(name, "url")))
219 url = p->value();
220 if (serviceType.isEmpty() && equalIgnoringCase(name, "type")) {
221 serviceType = p->value();
222 int pos = serviceType.find(";");
223 if (pos != -1)
224 serviceType = serviceType.left(pos);
225 }
226 if (!embed && !name.isEmpty()) {
227 uniqueParamNames.add(name.impl());
228 paramNames.append(p->name());
229 paramValues.append(p->value());
230 }
231 }
232 child = child->nextSibling();
233 }
234
235 // When OBJECT is used for an applet via Sun's Java plugin, the CODEBASE attribute in the tag
236 // points to the Java plugin itself (an ActiveX component) while the actual applet CODEBASE is
237 // in a PARAM tag. See <http://java.sun.com/products/plugin/1.2/docs/tags.html>. This means
238 // we have to explicitly suppress the tag's CODEBASE attribute if there is none in a PARAM,
239 // else our Java plugin will misinterpret it. [4004531]
240 String codebase;
241 if (!embed && MIMETypeRegistry::isJavaAppletMIMEType(serviceType)) {
242 codebase = "codebase";
243 uniqueParamNames.add(codebase.impl()); // pretend we found it in a PARAM already
244 }
245
246 // Turn the attributes of either the EMBED tag or OBJECT tag into arrays, but don't override PARAM values.
247 NamedNodeMap* attributes = embedOrObject->attributes();
248 if (attributes) {
249 for (unsigned i = 0; i < attributes->length(); ++i) {
250 Attribute* it = attributes->attributeItem(i);
251 const AtomicString& name = it->name().localName();
252 if (embed || !uniqueParamNames.contains(name.impl())) {
253 paramNames.append(name.string());
254 paramValues.append(it->value().string());
255 }
256 }
257 }
258
259 mapDataParamToSrc(¶mNames, ¶mValues);
260
261 // If we still don't have a type, try to map from a specific CLASSID to a type.
262 if (serviceType.isEmpty())
263 serviceType = serviceTypeForClassId(o->classId(), pluginData);
264
265 if (!isURLAllowed(document(), url))
266 return;
267
268 // Find out if we support fallback content.
269 m_hasFallbackContent = false;
270 for (Node *child = o->firstChild(); child && !m_hasFallbackContent; child = child->nextSibling()) {
271 if ((!child->isTextNode() && !child->hasTagName(embedTag) && !child->hasTagName(paramTag)) || // Discount <embed> and <param>
272 (child->isTextNode() && !static_cast<Text*>(child)->containsOnlyWhitespace()))
273 m_hasFallbackContent = true;
274 }
275
276 if (onlyCreateNonNetscapePlugins) {
277 KURL completedURL;
278 if (!url.isEmpty())
279 completedURL = frame->loader()->completeURL(url);
280
281 if (frame->loader()->client()->objectContentType(completedURL, serviceType) == ObjectContentNetscapePlugin)
282 return;
283 }
284
285 bool success = frame->loader()->requestObject(this, url, AtomicString(o->name()), serviceType, paramNames, paramValues);
286 if (!success && m_hasFallbackContent)
287 o->renderFallbackContent();
288 } else if (node()->hasTagName(embedTag)) {
289 HTMLEmbedElement *o = static_cast<HTMLEmbedElement*>(node());
290 o->setNeedWidgetUpdate(false);
291 url = o->url();
292 serviceType = o->serviceType();
293
294 if (url.isEmpty() && serviceType.isEmpty())
295 return;
296 if (!isURLAllowed(document(), url))
297 return;
298
299 // add all attributes set on the embed object
300 NamedNodeMap* a = o->attributes();
301 if (a) {
302 for (unsigned i = 0; i < a->length(); ++i) {
303 Attribute* it = a->attributeItem(i);
304 paramNames.append(it->name().localName().string());
305 paramValues.append(it->value().string());
306 }
307 }
308
309 if (onlyCreateNonNetscapePlugins) {
310 KURL completedURL;
311 if (!url.isEmpty())
312 completedURL = frame->loader()->completeURL(url);
313
314 if (frame->loader()->client()->objectContentType(completedURL, serviceType) == ObjectContentNetscapePlugin)
315 return;
316
317 }
318
319 frame->loader()->requestObject(this, url, o->getAttribute(nameAttr), serviceType, paramNames, paramValues);
320 }
321 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
322 else if (node()->hasTagName(videoTag) || node()->hasTagName(audioTag)) {
323 HTMLMediaElement* o = static_cast<HTMLMediaElement*>(node());
324
325 o->setNeedWidgetUpdate(false);
326 if (node()->hasTagName(videoTag)) {
327 HTMLVideoElement* vid = static_cast<HTMLVideoElement*>(node());
328 String poster = vid->poster();
329 if (!poster.isEmpty()) {
330 paramNames.append("_media_element_poster_");
331 paramValues.append(poster);
332 }
333 }
334
335 url = o->initialURL();
336 if (!url.isEmpty()) {
337 paramNames.append("_media_element_src_");
338 paramValues.append(url);
339 }
340
341 serviceType = "application/x-media-element-proxy-plugin";
342 frame->loader()->requestObject(this, url, nullAtom, serviceType, paramNames, paramValues);
343 }
344 #endif
345 }
346
layout()347 void RenderPartObject::layout()
348 {
349 ASSERT(needsLayout());
350
351 #ifdef FLATTEN_IFRAME
352 RenderPart::calcWidth();
353 RenderPart::calcHeight();
354 // Some IFrames have a width and/or height of 1 when they are meant to be
355 // hidden. If that is the case, do not try to expand.
356 if (node()->hasTagName(iframeTag) && widget() && widget()->isFrameView()
357 && width() > 1 && height() > 1) {
358 HTMLIFrameElement* element = static_cast<HTMLIFrameElement*>(node());
359 bool scrolling = element->scrollingMode() != ScrollbarAlwaysOff;
360 bool widthIsFixed = style()->width().isFixed();
361 bool heightIsFixed = style()->height().isFixed();
362 // If an iframe has a fixed dimension and suppresses scrollbars, it
363 // will disrupt layout if we force it to expand. Plus on a desktop,
364 // the extra content is not accessible.
365 if (scrolling || !widthIsFixed || !heightIsFixed) {
366 FrameView* view = static_cast<FrameView*>(widget());
367 RenderView* root = view ? view->frame()->contentRenderer() : NULL;
368 RenderPart* owner = view->frame()->ownerRenderer();
369 if (root && style()->visibility() != HIDDEN
370 && (!owner || owner->style()->visibility() != HIDDEN)) {
371 // Update the dimensions to get the correct minimum preferred
372 // width
373 updateWidgetPosition();
374
375 int extraWidth = paddingLeft() + paddingRight() + borderLeft() + borderRight();
376 int extraHeight = paddingTop() + paddingBottom() + borderTop() + borderBottom();
377 // Use the preferred width if it is larger and only if
378 // scrollbars are visible or the width style is not fixed.
379 if (scrolling || !widthIsFixed)
380 setWidth(max(width(), root->minPrefWidth()) + extraWidth);
381
382 // Resize the view to recalc the height.
383 int h = height() - extraHeight;
384 int w = width() - extraWidth;
385 if (w > view->width())
386 h = 0;
387 if (w != view->width() || h != view->height()) {
388 view->resize(w, h);
389 }
390
391 // Layout the view.
392 do {
393 view->layout();
394 } while (view->layoutPending() || root->needsLayout());
395
396 int contentHeight = view->contentsHeight();
397 int contentWidth = view->contentsWidth();
398 // Only change the width or height if scrollbars are visible or
399 // if the style is not a fixed value. Use the maximum value so
400 // that iframes never shrink.
401 if (scrolling || !heightIsFixed)
402 setHeight(max(height(), contentHeight + extraHeight));
403 if (scrolling || !widthIsFixed)
404 setWidth(max(width(), contentWidth + extraWidth));
405
406 // Update one last time
407 updateWidgetPosition();
408
409 #if !ASSERT_DISABLED
410 ASSERT(!view->layoutPending());
411 ASSERT(!root->needsLayout());
412 // Sanity check when assertions are enabled.
413 RenderObject* c = root->nextInPreOrder();
414 while (c) {
415 ASSERT(!c->needsLayout());
416 c = c->nextInPreOrder();
417 }
418 Node* body = document()->body();
419 if (body)
420 ASSERT(!body->renderer()->needsLayout());
421 #endif
422 }
423 }
424 }
425 #else
426 calcWidth();
427 calcHeight();
428 #endif
429
430 adjustOverflowForBoxShadowAndReflect();
431
432 RenderPart::layout();
433
434 if (!widget() && frameView())
435 frameView()->addWidgetToUpdate(this);
436
437 setNeedsLayout(false);
438 }
439
440 #ifdef FLATTEN_IFRAME
calcWidth()441 void RenderPartObject::calcWidth() {
442 RenderPart::calcWidth();
443 if (!node()->hasTagName(iframeTag) || !widget() || !widget()->isFrameView())
444 return;
445 FrameView* view = static_cast<FrameView*>(widget());
446 RenderView* root = static_cast<RenderView*>(view->frame()->contentRenderer());
447 if (!root)
448 return;
449 // Do not expand if the scrollbars are suppressed and the width is fixed.
450 bool scrolling = static_cast<HTMLIFrameElement*>(node())->scrollingMode() != ScrollbarAlwaysOff;
451 if (!scrolling && style()->width().isFixed())
452 return;
453 // Update the dimensions to get the correct minimum preferred
454 // width
455 updateWidgetPosition();
456
457 int extraWidth = paddingLeft() + paddingRight() + borderLeft() + borderRight();
458 // Set the width
459 setWidth(max(width(), root->minPrefWidth()) + extraWidth);
460
461 // Update based on the new width
462 updateWidgetPosition();
463
464 // Layout to get the content width
465 do {
466 view->layout();
467 } while (view->layoutPending() || root->needsLayout());
468
469 setWidth(max(width(), view->contentsWidth() + extraWidth));
470
471 // Update one last time to ensure the dimensions.
472 updateWidgetPosition();
473 }
474
calcHeight()475 void RenderPartObject::calcHeight() {
476 RenderPart::calcHeight();
477 if (!node()->hasTagName(iframeTag) || !widget() || !widget()->isFrameView())
478 return;
479 FrameView* view = static_cast<FrameView*>(widget());
480 RenderView* root = static_cast<RenderView*>(view->frame()->contentRenderer());
481 if (!root)
482 return;
483 // Do not expand if the scrollbars are suppressed and the height is fixed.
484 bool scrolling = static_cast<HTMLIFrameElement*>(node())->scrollingMode() != ScrollbarAlwaysOff;
485 if (!scrolling && style()->height().isFixed())
486 return;
487 // Update the widget
488 updateWidgetPosition();
489
490 // Layout to get the content height
491 do {
492 view->layout();
493 } while (view->layoutPending() || root->needsLayout());
494
495 int extraHeight = paddingTop() + paddingBottom() + borderTop() + borderBottom();
496 setHeight(max(width(), view->contentsHeight() + extraHeight));
497
498 // Update one last time to ensure the dimensions.
499 updateWidgetPosition();
500 }
501 #endif
502
viewCleared()503 void RenderPartObject::viewCleared()
504 {
505 if (node() && widget() && widget()->isFrameView()) {
506 FrameView* view = static_cast<FrameView*>(widget());
507 int marginw = -1;
508 int marginh = -1;
509 if (node()->hasTagName(iframeTag)) {
510 HTMLIFrameElement* frame = static_cast<HTMLIFrameElement*>(node());
511 marginw = frame->getMarginWidth();
512 marginh = frame->getMarginHeight();
513 }
514 if (marginw != -1)
515 view->setMarginWidth(marginw);
516 if (marginh != -1)
517 view->setMarginHeight(marginh);
518 }
519 }
520
521 }
522