~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/rendering/RenderPartObject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * This file is part of the KDE project.
 
3
 *
 
4
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 
5
 *           (C) 2000 Simon Hausmann <hausmann@kde.org>
 
6
 *           (C) 2000 Stefan Schimanski (1Stein@gmx.de)
 
7
 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Library General Public
 
11
 * License as published by the Free Software Foundation; either
 
12
 * version 2 of the License, or (at your option) any later version.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Library General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Library General Public License
 
20
 * along with this library; see the file COPYING.LIB.  If not, write to
 
21
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
22
 * Boston, MA 02111-1307, USA.
 
23
 *
 
24
 */
 
25
#include "config.h"
 
26
#include "RenderPartObject.h"
 
27
 
 
28
#include "Document.h"
 
29
#include "EventHandler.h"
 
30
#include "Frame.h"
 
31
#include "FrameLoader.h"
 
32
#include "FrameLoaderClient.h"
 
33
#include "FrameTree.h"
 
34
#include "FrameView.h"
 
35
#include "HTMLEmbedElement.h"
 
36
#include "HTMLIFrameElement.h"
 
37
#include "HTMLNames.h"
 
38
#include "HTMLObjectElement.h"
 
39
#include "HTMLParamElement.h"
 
40
#include "KURL.h"
 
41
#include "MIMETypeRegistry.h"
 
42
#include "Page.h"
 
43
#include "Text.h"
 
44
 
 
45
namespace WebCore {
 
46
 
 
47
using namespace HTMLNames;
 
48
 
 
49
RenderPartObject::RenderPartObject(HTMLFrameOwnerElement* element)
 
50
    : RenderPart(element)
 
51
{
 
52
    // init RenderObject attributes
 
53
    setInline(true);
 
54
    m_hasFallbackContent = false;
 
55
}
 
56
 
 
57
static bool isURLAllowed(Document *doc, const String &url)
 
58
{
 
59
    KURL newURL(doc->completeURL(url.deprecatedString()));
 
60
    newURL.setRef(DeprecatedString::null);
 
61
    
 
62
    if (doc->frame()->page()->frameCount() >= 200)
 
63
        return false;
 
64
 
 
65
    // We allow one level of self-reference because some sites depend on that.
 
66
    // But we don't allow more than one.
 
67
    bool foundSelfReference = false;
 
68
    for (Frame *frame = doc->frame(); frame; frame = frame->tree()->parent()) {
 
69
        KURL frameURL = frame->loader()->url();
 
70
        frameURL.setRef(DeprecatedString::null);
 
71
        if (frameURL == newURL) {
 
72
            if (foundSelfReference)
 
73
                return false;
 
74
            foundSelfReference = true;
 
75
        }
 
76
    }
 
77
    return true;
 
78
}
 
79
 
 
80
static inline void mapClassIdToServiceType(const String& classId, String& serviceType)
 
81
{
 
82
    // It is ActiveX, but the nsplugin system handling
 
83
    // should also work, that's why we don't override the
 
84
    // serviceType with application/x-activex-handler
 
85
    // but let the KTrader in khtmlpart::createPart() detect
 
86
    // the user's preference: launch with activex viewer or
 
87
    // with nspluginviewer (Niko)
 
88
    if (classId.contains("D27CDB6E-AE6D-11cf-96B8-444553540000"))
 
89
        serviceType = "application/x-shockwave-flash";
 
90
    else if (classId.contains("CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"))
 
91
        serviceType = "audio/x-pn-realaudio-plugin";
 
92
    else if (classId.contains("02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"))
 
93
        serviceType = "video/quicktime";
 
94
    else if (classId.contains("166B1BCA-3F9C-11CF-8075-444553540000"))
 
95
        serviceType = "application/x-director";
 
96
    else if (classId.contains("6BF52A52-394A-11d3-B153-00C04F79FAA6"))
 
97
        serviceType = "application/x-mplayer2";
 
98
    else if (!classId.isEmpty())
 
99
        // We have a clsid, means this is activex (Niko)
 
100
        serviceType = "application/x-activex-handler";
 
101
    // TODO: add more plugins here
 
102
}
 
103
 
 
104
void RenderPartObject::updateWidget(bool onlyCreateNonPlugins)
 
105
{
 
106
  String url;
 
107
  String serviceType;
 
108
  Vector<String> paramNames;
 
109
  Vector<String> paramValues;
 
110
  Frame* frame = m_view->frame();
 
111
  
 
112
  if (element()->hasTagName(objectTag)) {
 
113
 
 
114
      HTMLObjectElement* o = static_cast<HTMLObjectElement*>(element());
 
115
 
 
116
      if (!o->isComplete())
 
117
        return;
 
118
      // Check for a child EMBED tag.
 
119
      HTMLEmbedElement* embed = 0;
 
120
      for (Node* child = o->firstChild(); child;) {
 
121
          if (child->hasTagName(embedTag)) {
 
122
              embed = static_cast<HTMLEmbedElement*>(child);
 
123
              break;
 
124
          } else if (child->hasTagName(objectTag))
 
125
              child = child->nextSibling();         // Don't descend into nested OBJECT tags
 
126
          else
 
127
              child = child->traverseNextNode(o);   // Otherwise descend (EMBEDs may be inside COMMENT tags)
 
128
      }
 
129
      
 
130
      // Use the attributes from the EMBED tag instead of the OBJECT tag including WIDTH and HEIGHT.
 
131
      HTMLElement *embedOrObject;
 
132
      if (embed) {
 
133
          embedOrObject = (HTMLElement *)embed;
 
134
          String attribute = embedOrObject->getAttribute(widthAttr);
 
135
          if (!attribute.isEmpty())
 
136
              o->setAttribute(widthAttr, attribute);
 
137
          attribute = embedOrObject->getAttribute(heightAttr);
 
138
          if (!attribute.isEmpty())
 
139
              o->setAttribute(heightAttr, attribute);
 
140
          url = embed->url;
 
141
          serviceType = embed->m_serviceType;
 
142
      } else
 
143
          embedOrObject = (HTMLElement *)o;
 
144
      
 
145
      // If there was no URL or type defined in EMBED, try the OBJECT tag.
 
146
      if (url.isEmpty())
 
147
          url = o->m_url;
 
148
      if (serviceType.isEmpty())
 
149
          serviceType = o->m_serviceType;
 
150
      
 
151
      HashSet<StringImpl*, CaseInsensitiveHash<StringImpl*> > uniqueParamNames;
 
152
      
 
153
      // Scan the PARAM children.
 
154
      // Get the URL and type from the params if we don't already have them.
 
155
      // Get the attributes from the params if there is no EMBED tag.
 
156
      Node *child = o->firstChild();
 
157
      while (child && (url.isEmpty() || serviceType.isEmpty() || !embed)) {
 
158
          if (child->hasTagName(paramTag)) {
 
159
              HTMLParamElement* p = static_cast<HTMLParamElement*>(child);
 
160
              String name = p->name().lower();
 
161
              if (url.isEmpty() && (name == "src" || name == "movie" || name == "code" || name == "url"))
 
162
                  url = p->value();
 
163
              if (serviceType.isEmpty() && name == "type") {
 
164
                  serviceType = p->value();
 
165
                  int pos = serviceType.find(";");
 
166
                  if (pos != -1)
 
167
                      serviceType = serviceType.left(pos);
 
168
              }
 
169
              if (!embed && !name.isEmpty()) {
 
170
                  uniqueParamNames.add(p->name().impl());
 
171
                  paramNames.append(p->name());
 
172
                  paramValues.append(p->value());
 
173
              }
 
174
          }
 
175
          child = child->nextSibling();
 
176
      }
 
177
      
 
178
      // When OBJECT is used for an applet via Sun's Java plugin, the CODEBASE attribute in the tag
 
179
      // points to the Java plugin itself (an ActiveX component) while the actual applet CODEBASE is
 
180
      // in a PARAM tag. See <http://java.sun.com/products/plugin/1.2/docs/tags.html>. This means
 
181
      // we have to explicitly suppress the tag's CODEBASE attribute if there is none in a PARAM,
 
182
      // else our Java plugin will misinterpret it. [4004531]
 
183
      String codebase;
 
184
      if (!embed && MIMETypeRegistry::isJavaAppletMIMEType(serviceType)) {
 
185
          codebase = "codebase";
 
186
          uniqueParamNames.add(codebase.impl()); // pretend we found it in a PARAM already
 
187
      }
 
188
      
 
189
      // Turn the attributes of either the EMBED tag or OBJECT tag into arrays, but don't override PARAM values.
 
190
      NamedAttrMap* attributes = embedOrObject->attributes();
 
191
      if (attributes) {
 
192
          for (unsigned i = 0; i < attributes->length(); ++i) {
 
193
              Attribute* it = attributes->attributeItem(i);
 
194
              const AtomicString& name = it->name().localName();
 
195
              if (embed || !uniqueParamNames.contains(name.impl())) {
 
196
                  paramNames.append(name.domString());
 
197
                  paramValues.append(it->value().domString());
 
198
              }
 
199
          }
 
200
      }
 
201
      
 
202
      // If we still don't have a type, try to map from a specific CLASSID to a type.
 
203
      if (serviceType.isEmpty() && !o->m_classId.isEmpty())
 
204
          mapClassIdToServiceType(o->m_classId, serviceType);
 
205
      
 
206
      // If no URL and type, abort.
 
207
      if (url.isEmpty() && serviceType.isEmpty())
 
208
          return;
 
209
      if (!isURLAllowed(document(), url))
 
210
          return;
 
211
 
 
212
      // Find out if we support fallback content.
 
213
      m_hasFallbackContent = false;
 
214
      for (Node *child = o->firstChild(); child && !m_hasFallbackContent; child = child->nextSibling()) {
 
215
          if ((!child->isTextNode() && !child->hasTagName(embedTag) && !child->hasTagName(paramTag)) || // Discount <embed> and <param>
 
216
              (child->isTextNode() && !static_cast<Text*>(child)->containsOnlyWhitespace()))
 
217
              m_hasFallbackContent = true;
 
218
      }
 
219
      
 
220
      if (onlyCreateNonPlugins) {
 
221
          KURL completedURL;
 
222
          if (!url.isEmpty())
 
223
              completedURL = frame->loader()->completeURL(url);
 
224
        
 
225
          if (frame->loader()->client()->objectContentType(completedURL, serviceType) == ObjectContentPlugin)
 
226
              return;
 
227
      }
 
228
      
 
229
      bool success = frame->loader()->requestObject(this, url, AtomicString(o->name()), serviceType, paramNames, paramValues);
 
230
      if (!success && m_hasFallbackContent)
 
231
          o->renderFallbackContent();
 
232
  } else if (element()->hasTagName(embedTag)) {
 
233
      HTMLEmbedElement *o = static_cast<HTMLEmbedElement*>(element());
 
234
      url = o->url;
 
235
      serviceType = o->m_serviceType;
 
236
 
 
237
      if (url.isEmpty() && serviceType.isEmpty())
 
238
          return;
 
239
      if (!isURLAllowed(document(), url))
 
240
          return;
 
241
      
 
242
      // add all attributes set on the embed object
 
243
      NamedAttrMap* a = o->attributes();
 
244
      if (a) {
 
245
          for (unsigned i = 0; i < a->length(); ++i) {
 
246
              Attribute* it = a->attributeItem(i);
 
247
              paramNames.append(it->name().localName().domString());
 
248
              paramValues.append(it->value().domString());
 
249
          }
 
250
      }
 
251
      
 
252
      if (onlyCreateNonPlugins) {
 
253
          KURL completedURL;
 
254
          if (!url.isEmpty())
 
255
              completedURL = frame->loader()->completeURL(url);
 
256
          
 
257
          if (frame->loader()->client()->objectContentType(completedURL, serviceType) == ObjectContentPlugin)
 
258
              return;
 
259
      }
 
260
      
 
261
      frame->loader()->requestObject(this, url, o->getAttribute(nameAttr), serviceType, paramNames, paramValues);
 
262
  }
 
263
}
 
264
 
 
265
void RenderPartObject::layout()
 
266
{
 
267
    ASSERT(needsLayout());
 
268
 
 
269
    calcWidth();
 
270
    calcHeight();
 
271
    adjustOverflowForBoxShadow();
 
272
 
 
273
    RenderPart::layout();
 
274
 
 
275
    if (!m_widget)
 
276
      updateWidget(false);
 
277
    
 
278
    setNeedsLayout(false);
 
279
}
 
280
 
 
281
void RenderPartObject::viewCleared()
 
282
{
 
283
    if (element() && m_widget && m_widget->isFrameView()) {
 
284
        FrameView* view = static_cast<FrameView*>(m_widget);
 
285
        int marginw = -1;
 
286
        int marginh = -1;
 
287
        if (element()->hasTagName(iframeTag)) {
 
288
            HTMLIFrameElement* frame = static_cast<HTMLIFrameElement*>(element());
 
289
            marginw = frame->getMarginWidth();
 
290
            marginh = frame->getMarginHeight();
 
291
        }
 
292
        if (marginw != -1)
 
293
            view->setMarginWidth(marginw);
 
294
        if (marginh != -1)
 
295
            view->setMarginHeight(marginh);
 
296
    }
 
297
}
 
298
 
 
299
}