2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
5 * Copyright (C) 2008 Alp Toker <alp@atoker.com>
6 * Copyright (C) Research In Motion Limited 2009. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
18 * its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "SubframeLoader.h"
36 #include "ContentSecurityPolicy.h"
38 #include "FrameLoaderClient.h"
39 #include "HTMLAppletElement.h"
40 #include "HTMLFrameElementBase.h"
41 #include "HTMLNames.h"
42 #include "HTMLPlugInImageElement.h"
43 #include "MIMETypeRegistry.h"
45 #include "PluginData.h"
46 #include "PluginDocument.h"
47 #include "RenderEmbeddedObject.h"
48 #include "RenderView.h"
49 #include "SecurityOrigin.h"
52 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
53 #include "HTMLMediaElement.h"
54 #include "RenderVideo.h"
59 using namespace HTMLNames;
61 SubframeLoader::SubframeLoader(Frame* frame)
62 : m_containsPlugins(false)
67 void SubframeLoader::clear()
69 m_containsPlugins = false;
72 bool SubframeLoader::requestFrame(HTMLFrameOwnerElement* ownerElement, const String& urlString, const AtomicString& frameName, bool lockHistory, bool lockBackForwardList)
74 // Support for <frame src="javascript:string">
77 if (protocolIsJavaScript(urlString)) {
78 scriptURL = completeURL(urlString); // completeURL() encodes the URL.
81 url = completeURL(urlString);
83 Frame* frame = loadOrRedirectSubframe(ownerElement, url, frameName, lockHistory, lockBackForwardList);
87 if (!scriptURL.isEmpty())
88 frame->script()->executeIfJavaScriptURL(scriptURL);
93 bool SubframeLoader::resourceWillUsePlugin(const String& url, const String& mimeType, bool shouldPreferPlugInsForImages)
97 completedURL = completeURL(url);
100 return shouldUsePlugin(completedURL, mimeType, shouldPreferPlugInsForImages, false, useFallback);
103 bool SubframeLoader::requestPlugin(HTMLPlugInImageElement* ownerElement, const KURL& url, const String& mimeType, const Vector<String>& paramNames, const Vector<String>& paramValues, bool useFallback)
105 Settings* settings = m_frame->settings();
106 if ((!allowPlugins(AboutToInstantiatePlugin)
107 // Application plug-ins are plug-ins implemented by the user agent, for example Qt plug-ins,
108 // as opposed to third-party code such as Flash. The user agent decides whether or not they are
109 // permitted, rather than WebKit.
110 && !MIMETypeRegistry::isApplicationPluginMIMEType(mimeType))
111 || (!settings->isJavaEnabled() && MIMETypeRegistry::isJavaAppletMIMEType(mimeType)))
114 if (m_frame->document()) {
115 if (m_frame->document()->securityOrigin()->isSandboxed(SandboxPlugins))
117 if (!m_frame->document()->contentSecurityPolicy()->allowObjectFromSource(url))
121 ASSERT(ownerElement->hasTagName(objectTag) || ownerElement->hasTagName(embedTag));
122 return loadPlugin(ownerElement, url, mimeType, paramNames, paramValues, useFallback);
125 bool SubframeLoader::requestObject(HTMLPlugInImageElement* ownerElement, const String& url, const AtomicString& frameName, const String& mimeType, const Vector<String>& paramNames, const Vector<String>& paramValues)
127 if (url.isEmpty() && mimeType.isEmpty())
130 // FIXME: None of this code should use renderers!
131 RenderEmbeddedObject* renderer = ownerElement->renderEmbeddedObject();
138 completedURL = completeURL(url);
141 if (shouldUsePlugin(completedURL, mimeType, ownerElement->shouldPreferPlugInsForImages(), renderer->hasFallbackContent(), useFallback))
142 return requestPlugin(ownerElement, completedURL, mimeType, paramNames, paramValues, useFallback);
144 // If the plug-in element already contains a subframe, loadOrRedirectSubframe will re-use it. Otherwise,
145 // it will create a new frame and set it as the RenderPart's widget, causing what was previously
146 // in the widget to be torn down.
147 return loadOrRedirectSubframe(ownerElement, completedURL, frameName, true, true);
150 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
151 PassRefPtr<Widget> SubframeLoader::loadMediaPlayerProxyPlugin(Node* node, const KURL& url,
152 const Vector<String>& paramNames, const Vector<String>& paramValues)
154 ASSERT(node->hasTagName(videoTag) || node->hasTagName(audioTag));
158 completedURL = completeURL(url);
160 if (!m_frame->document()->securityOrigin()->canDisplay(completedURL)) {
161 FrameLoader::reportLocalLoadFailed(m_frame, completedURL.string());
165 if (!m_frame->document()->contentSecurityPolicy()->allowMediaFromSource(completedURL))
168 HTMLMediaElement* mediaElement = static_cast<HTMLMediaElement*>(node);
169 RenderPart* renderer = toRenderPart(node->renderer());
173 size = IntSize(renderer->contentWidth(), renderer->contentHeight());
174 else if (mediaElement->isVideo())
175 size = RenderVideo::defaultSize();
177 if (!m_frame->loader()->checkIfRunInsecureContent(m_frame->document()->securityOrigin(), completedURL))
180 RefPtr<Widget> widget = m_frame->loader()->client()->createMediaPlayerProxyPlugin(size, mediaElement, completedURL,
181 paramNames, paramValues, "application/x-media-element-proxy-plugin");
183 if (widget && renderer) {
184 renderer->setWidget(widget);
185 renderer->node()->setNeedsStyleRecalc(SyntheticStyleChange);
187 m_containsPlugins = true;
189 return widget ? widget.release() : 0;
191 #endif // ENABLE(PLUGIN_PROXY_FOR_VIDEO)
193 PassRefPtr<Widget> SubframeLoader::createJavaAppletWidget(const IntSize& size, HTMLAppletElement* element, const HashMap<String, String>& args)
195 String baseURLString;
196 String codeBaseURLString;
197 Vector<String> paramNames;
198 Vector<String> paramValues;
199 HashMap<String, String>::const_iterator end = args.end();
200 for (HashMap<String, String>::const_iterator it = args.begin(); it != end; ++it) {
201 if (equalIgnoringCase(it->first, "baseurl"))
202 baseURLString = it->second;
203 else if (equalIgnoringCase(it->first, "codebase"))
204 codeBaseURLString = it->second;
205 paramNames.append(it->first);
206 paramValues.append(it->second);
209 if (!codeBaseURLString.isEmpty()) {
210 KURL codeBaseURL = completeURL(codeBaseURLString);
211 if (!element->document()->securityOrigin()->canDisplay(codeBaseURL)) {
212 FrameLoader::reportLocalLoadFailed(m_frame, codeBaseURL.string());
216 if (!element->document()->contentSecurityPolicy()->allowObjectFromSource(codeBaseURL))
220 if (baseURLString.isEmpty())
221 baseURLString = m_frame->document()->baseURL().string();
222 KURL baseURL = completeURL(baseURLString);
224 RefPtr<Widget> widget;
225 if (allowPlugins(AboutToInstantiatePlugin))
226 widget = m_frame->loader()->client()->createJavaAppletWidget(size, element, baseURL, paramNames, paramValues);
230 m_containsPlugins = true;
234 Frame* SubframeLoader::loadOrRedirectSubframe(HTMLFrameOwnerElement* ownerElement, const KURL& url, const AtomicString& frameName, bool lockHistory, bool lockBackForwardList)
236 Frame* frame = ownerElement->contentFrame();
238 frame->navigationScheduler()->scheduleLocationChange(m_frame->document()->securityOrigin(), url.string(), m_frame->loader()->outgoingReferrer(), lockHistory, lockBackForwardList);
240 frame = loadSubframe(ownerElement, url, frameName, m_frame->loader()->outgoingReferrer());
244 Frame* SubframeLoader::loadSubframe(HTMLFrameOwnerElement* ownerElement, const KURL& url, const String& name, const String& referrer)
246 bool allowsScrolling = true;
247 int marginWidth = -1;
248 int marginHeight = -1;
249 if (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag)) {
250 HTMLFrameElementBase* o = static_cast<HTMLFrameElementBase*>(ownerElement);
251 allowsScrolling = o->scrollingMode() != ScrollbarAlwaysOff;
252 marginWidth = o->marginWidth();
253 marginHeight = o->marginHeight();
256 if (!ownerElement->document()->securityOrigin()->canDisplay(url)) {
257 FrameLoader::reportLocalLoadFailed(m_frame, url.string());
261 if (!ownerElement->document()->contentSecurityPolicy()->allowChildFrameFromSource(url))
264 bool hideReferrer = SecurityOrigin::shouldHideReferrer(url, referrer);
265 RefPtr<Frame> frame = m_frame->loader()->client()->createFrame(url, name, ownerElement, hideReferrer ? String() : referrer, allowsScrolling, marginWidth, marginHeight);
268 m_frame->loader()->checkCallImplicitClose();
272 // All new frames will have m_isComplete set to true at this point due to synchronously loading
273 // an empty document in FrameLoader::init(). But many frames will now be starting an
274 // asynchronous load of url, so we set m_isComplete to false and then check if the load is
275 // actually completed below. (Note that we set m_isComplete to false even for synchronous
276 // loads, so that checkCompleted() below won't bail early.)
277 // FIXME: Can we remove this entirely? m_isComplete normally gets set to false when a load is committed.
278 frame->loader()->started();
280 RenderObject* renderer = ownerElement->renderer();
281 FrameView* view = frame->view();
282 if (renderer && renderer->isWidget() && view)
283 toRenderWidget(renderer)->setWidget(view);
285 m_frame->loader()->checkCallImplicitClose();
287 // Some loads are performed synchronously (e.g., about:blank and loads
288 // cancelled by returning a null ResourceRequest from requestFromDelegate).
289 // In these cases, the synchronous load would have finished
290 // before we could connect the signals, so make sure to send the
291 // completed() signal for the child by hand and mark the load as being
293 // FIXME: In this case the Frame will have finished loading before
294 // it's being added to the child list. It would be a good idea to
295 // create the child first, then invoke the loader separately.
296 if (frame->loader()->state() == FrameStateComplete && !frame->loader()->policyDocumentLoader())
297 frame->loader()->checkCompleted();
302 bool SubframeLoader::allowPlugins(ReasonForCallingAllowPlugins reason)
304 Settings* settings = m_frame->settings();
305 bool allowed = m_frame->loader()->client()->allowPlugins(settings && settings->arePluginsEnabled());
306 if (!allowed && reason == AboutToInstantiatePlugin)
307 m_frame->loader()->client()->didNotAllowPlugins();
311 bool SubframeLoader::shouldUsePlugin(const KURL& url, const String& mimeType, bool shouldPreferPlugInsForImages, bool hasFallback, bool& useFallback)
313 if (m_frame->loader()->client()->shouldUsePluginDocument(mimeType)) {
318 // Allow other plug-ins to win over QuickTime because if the user has installed a plug-in that
319 // can handle TIFF (which QuickTime can also handle) they probably intended to override QT.
320 if (m_frame->page() && (mimeType == "image/tiff" || mimeType == "image/tif" || mimeType == "image/x-tiff")) {
321 const PluginData* pluginData = m_frame->page()->pluginData();
322 String pluginName = pluginData ? pluginData->pluginNameForMimeType(mimeType) : String();
323 if (!pluginName.isEmpty() && !pluginName.contains("QuickTime", false))
327 ObjectContentType objectType = m_frame->loader()->client()->objectContentType(url, mimeType, shouldPreferPlugInsForImages);
328 // If an object's content can't be handled and it has no fallback, let
329 // it be handled as a plugin to show the broken plugin icon.
330 useFallback = objectType == ObjectContentNone && hasFallback;
331 return objectType == ObjectContentNone || objectType == ObjectContentNetscapePlugin || objectType == ObjectContentOtherPlugin;
334 Document* SubframeLoader::document() const
336 return m_frame->document();
339 bool SubframeLoader::loadPlugin(HTMLPlugInImageElement* pluginElement, const KURL& url, const String& mimeType,
340 const Vector<String>& paramNames, const Vector<String>& paramValues, bool useFallback)
342 RenderEmbeddedObject* renderer = pluginElement->renderEmbeddedObject();
344 // FIXME: This code should not depend on renderer!
345 if (!renderer || useFallback)
348 if (!document()->securityOrigin()->canDisplay(url)) {
349 FrameLoader::reportLocalLoadFailed(m_frame, url.string());
353 if (!document()->contentSecurityPolicy()->allowObjectFromSource(url))
356 FrameLoader* frameLoader = m_frame->loader();
357 if (!frameLoader->checkIfRunInsecureContent(document()->securityOrigin(), url))
360 IntSize contentSize(renderer->contentWidth(), renderer->contentHeight());
361 bool loadManually = document()->isPluginDocument() && !m_containsPlugins && toPluginDocument(document())->shouldLoadPluginManually();
362 RefPtr<Widget> widget = frameLoader->client()->createPlugin(contentSize,
363 pluginElement, url, paramNames, paramValues, mimeType, loadManually);
366 renderer->setShowsMissingPluginIndicator();
370 renderer->setWidget(widget);
371 m_containsPlugins = true;
373 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO) || ENABLE(3D_PLUGIN)
374 pluginElement->setNeedsStyleRecalc(SyntheticStyleChange);
379 KURL SubframeLoader::completeURL(const String& url) const
381 ASSERT(m_frame->document());
382 return m_frame->document()->completeURL(url);
385 } // namespace WebCore