1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23#include "HTMLImageLoader.h"
24
25#include "CachedImage.h"
26#include "Element.h"
27#include "Event.h"
28#include "EventNames.h"
29#include "HTMLNames.h"
30#include "HTMLObjectElement.h"
31#include "HTMLParserIdioms.h"
32#include "Settings.h"
33
34#include "JSDOMWindowBase.h"
35#include <runtime/JSCInlines.h>
36#include <runtime/JSLock.h>
37
38namespace WebCore {
39
40HTMLImageLoader::HTMLImageLoader(Element& element)
41    : ImageLoader(element)
42{
43}
44
45HTMLImageLoader::~HTMLImageLoader()
46{
47}
48
49void HTMLImageLoader::dispatchLoadEvent()
50{
51    // HTMLVideoElement uses this class to load the poster image, but it should not fire events for loading or failure.
52    if (isHTMLVideoElement(element()))
53        return;
54
55    bool errorOccurred = image()->errorOccurred();
56    if (!errorOccurred && image()->response().httpStatusCode() >= 400)
57        errorOccurred = isHTMLObjectElement(element()); // An <object> considers a 404 to be an error and should fire onerror.
58    element().dispatchEvent(Event::create(errorOccurred ? eventNames().errorEvent : eventNames().loadEvent, false, false));
59}
60
61String HTMLImageLoader::sourceURI(const AtomicString& attr) const
62{
63#if ENABLE(DASHBOARD_SUPPORT)
64    Settings* settings = element().document().settings();
65    if (settings && settings->usesDashboardBackwardCompatibilityMode() && attr.length() > 7 && attr.startsWith("url(\"") && attr.endsWith("\")"))
66        return attr.string().substring(5, attr.length() - 7);
67#endif
68
69    return stripLeadingAndTrailingHTMLSpaces(attr);
70}
71
72void HTMLImageLoader::notifyFinished(CachedResource*)
73{
74    CachedImage* cachedImage = image();
75
76    Ref<Element> protect(element());
77    ImageLoader::notifyFinished(cachedImage);
78
79    bool loadError = cachedImage->errorOccurred() || cachedImage->response().httpStatusCode() >= 400;
80    if (!loadError) {
81        if (!element().inDocument()) {
82            JSC::VM& vm = JSDOMWindowBase::commonVM();
83            JSC::JSLockHolder lock(vm);
84            vm.heap.reportExtraMemoryCost(cachedImage->encodedSize());
85        }
86    }
87
88    if (loadError && isHTMLObjectElement(element()))
89        toHTMLObjectElement(element()).renderFallbackContent();
90}
91
92}
93