HTMLPlugInImageElement.cpp [plain text]
#include "config.h"
#include "HTMLPlugInImageElement.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "HTMLImageLoader.h"
#include "HTMLNames.h"
#include "Image.h"
#include "NodeRenderStyle.h"
#include "Page.h"
#include "RenderEmbeddedObject.h"
#include "RenderImage.h"
#include "SecurityOrigin.h"
#include "ExceptionCode.h"
#include "HTMLIFrameElement.h"
#include "RenderBlock.h"
#include "ShadowRoot.h"
#include "YouTubeEmbedShadowElement.h"
namespace WebCore {
HTMLPlugInImageElement::HTMLPlugInImageElement(const QualifiedName& tagName, Document* document, bool createdByParser, PreferPlugInsForImagesOption preferPlugInsForImagesOption)
: HTMLPlugInElement(tagName, document)
, m_needsWidgetUpdate(!createdByParser)
, m_shouldPreferPlugInsForImages(preferPlugInsForImagesOption == ShouldPreferPlugInsForImages)
, m_needsDocumentActivationCallbacks(false)
{
setHasCustomWillOrDidRecalcStyle();
}
HTMLPlugInImageElement::~HTMLPlugInImageElement()
{
if (m_needsDocumentActivationCallbacks)
document()->unregisterForPageCacheSuspensionCallbacks(this);
}
RenderEmbeddedObject* HTMLPlugInImageElement::renderEmbeddedObject() const
{
if (!renderer() || !renderer()->isEmbeddedObject())
return 0;
return toRenderEmbeddedObject(renderer());
}
bool HTMLPlugInImageElement::isImageType()
{
if (m_serviceType.isEmpty() && protocolIs(m_url, "data"))
m_serviceType = mimeTypeFromDataURL(m_url);
if (Frame* frame = document()->frame()) {
KURL completedURL = document()->completeURL(m_url);
return frame->loader()->client()->objectContentType(completedURL, m_serviceType, shouldPreferPlugInsForImages()) == ObjectContentImage;
}
return Image::supportsType(m_serviceType);
}
bool HTMLPlugInImageElement::allowedToLoadFrameURL(const String& url)
{
ASSERT(document());
ASSERT(document()->frame());
if (document()->frame()->page()->frameCount() >= Page::maxNumberOfFrames)
return false;
KURL completeURL = document()->completeURL(url);
if (contentFrame() && protocolIsJavaScript(completeURL)
&& !document()->securityOrigin()->canAccess(contentDocument()->securityOrigin()))
return false;
bool foundSelfReference = false;
for (Frame* frame = document()->frame(); frame; frame = frame->tree()->parent()) {
if (equalIgnoringFragmentIdentifier(frame->document()->url(), completeURL)) {
if (foundSelfReference)
return false;
foundSelfReference = true;
}
}
return true;
}
bool HTMLPlugInImageElement::wouldLoadAsNetscapePlugin(const String& url, const String& serviceType)
{
ASSERT(document());
ASSERT(document()->frame());
KURL completedURL;
if (!url.isEmpty())
completedURL = document()->completeURL(url);
FrameLoader* frameLoader = document()->frame()->loader();
ASSERT(frameLoader);
if (frameLoader->client()->objectContentType(completedURL, serviceType, shouldPreferPlugInsForImages()) == ObjectContentNetscapePlugin)
return true;
return false;
}
RenderObject* HTMLPlugInImageElement::createRenderer(RenderArena* arena, RenderStyle* style)
{
if (!m_needsDocumentActivationCallbacks) {
m_needsDocumentActivationCallbacks = true;
document()->registerForPageCacheSuspensionCallbacks(this);
}
if (useFallbackContent())
return RenderObject::createObject(this, style);
if (isImageType()) {
RenderImage* image = new (arena) RenderImage(this);
image->setImageResource(RenderImageResource::create());
return image;
}
if (hasShadowRoot()) {
Element* shadowElement = toElement(ensureShadowRoot()->firstChild());
if (shadowElement && shadowElement->shadowPseudoId() == "-apple-youtube-shadow-iframe")
return new (arena) RenderBlock(this);
}
return new (arena) RenderEmbeddedObject(this);
}
bool HTMLPlugInImageElement::willRecalcStyle(StyleChange)
{
if (!useFallbackContent() && needsWidgetUpdate() && renderer() && !isImageType())
reattach();
return true;
}
void HTMLPlugInImageElement::attach()
{
suspendPostAttachCallbacks();
bool isImage = isImageType();
if (!isImage)
queuePostAttachCallback(&HTMLPlugInImageElement::updateWidgetCallback, this);
HTMLPlugInElement::attach();
if (isImage && renderer() && !useFallbackContent()) {
if (!m_imageLoader)
m_imageLoader = adoptPtr(new HTMLImageLoader(this));
m_imageLoader->updateFromElement();
}
resumePostAttachCallbacks();
}
void HTMLPlugInImageElement::detach()
{
if (attached() && renderer() && !useFallbackContent())
setNeedsWidgetUpdate(true);
HTMLPlugInElement::detach();
}
void HTMLPlugInImageElement::updateWidgetIfNecessary()
{
document()->updateStyleIfNeeded();
if (!needsWidgetUpdate() || useFallbackContent() || isImageType())
return;
if (!renderEmbeddedObject() || renderEmbeddedObject()->showsUnavailablePluginIndicator())
return;
updateWidget(CreateOnlyNonNetscapePlugins);
}
void HTMLPlugInImageElement::finishParsingChildren()
{
HTMLPlugInElement::finishParsingChildren();
if (useFallbackContent())
return;
setNeedsWidgetUpdate(true);
if (inDocument())
setNeedsStyleRecalc();
}
void HTMLPlugInImageElement::didMoveToNewDocument(Document* oldDocument)
{
if (m_needsDocumentActivationCallbacks) {
if (oldDocument)
oldDocument->unregisterForPageCacheSuspensionCallbacks(this);
document()->registerForPageCacheSuspensionCallbacks(this);
}
if (m_imageLoader)
m_imageLoader->elementDidMoveToNewDocument();
HTMLPlugInElement::didMoveToNewDocument(oldDocument);
}
void HTMLPlugInImageElement::documentWillSuspendForPageCache()
{
if (RenderStyle* renderStyle = this->renderStyle()) {
m_customStyleForPageCache = RenderStyle::clone(renderStyle);
m_customStyleForPageCache->setDisplay(NONE);
setHasCustomStyleForRenderer();
recalcStyle(Force);
}
HTMLPlugInElement::documentWillSuspendForPageCache();
}
void HTMLPlugInImageElement::documentDidResumeFromPageCache()
{
if (m_customStyleForPageCache) {
m_customStyleForPageCache = 0;
clearHasCustomStyleForRenderer();
recalcStyle(Force);
}
HTMLPlugInElement::documentDidResumeFromPageCache();
}
PassRefPtr<RenderStyle> HTMLPlugInImageElement::customStyleForRenderer()
{
ASSERT(m_customStyleForPageCache);
return m_customStyleForPageCache;
}
void HTMLPlugInImageElement::updateWidgetCallback(Node* n, unsigned)
{
static_cast<HTMLPlugInImageElement*>(n)->updateWidgetIfNecessary();
}
void HTMLPlugInImageElement::createShadowIFrameSubtree(const String& src)
{
if(hasShadowRoot())
return;
if (src.isEmpty())
return;
RefPtr<YouTubeEmbedShadowElement> shadowElement = YouTubeEmbedShadowElement::create(document());
RefPtr<ShadowRoot> root = ShadowRoot::create(this, ShadowRoot::CreatingUserAgentShadowRoot);
root->appendChild(shadowElement, ASSERT_NO_EXCEPTION);
RefPtr<HTMLIFrameElement> iframeElement = HTMLIFrameElement::create(HTMLNames::iframeTag, document());
iframeElement->setAttribute(HTMLNames::widthAttr, "100%");
iframeElement->setAttribute(HTMLNames::heightAttr, "100%");
iframeElement->setAttribute(HTMLNames::srcAttr, src);
iframeElement->setAttribute(HTMLNames::frameborderAttr, "0");
iframeElement->setAttribute(HTMLNames::scrollingAttr, "no");
shadowElement->appendChild(iframeElement, ASSERT_NO_EXCEPTION);
if (renderer())
reattach();
}
}