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, 2015 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 "CommonVM.h"
27#include "DOMWindow.h"
28#include "Element.h"
29#include "Event.h"
30#include "EventNames.h"
31#include "HTMLNames.h"
32#include "HTMLObjectElement.h"
33#include "HTMLParserIdioms.h"
34#include "HTMLVideoElement.h"
35#include "Settings.h"
36
37#include "JSDOMWindowBase.h"
38#include <JavaScriptCore/JSCInlines.h>
39#include <JavaScriptCore/JSLock.h>
40
41namespace WebCore {
42
43HTMLImageLoader::HTMLImageLoader(Element& element)
44 : ImageLoader(element)
45{
46}
47
48HTMLImageLoader::~HTMLImageLoader() = default;
49
50void HTMLImageLoader::dispatchLoadEvent()
51{
52#if ENABLE(VIDEO)
53 // HTMLVideoElement uses this class to load the poster image, but it should not fire events for loading or failure.
54 if (is<HTMLVideoElement>(element()))
55 return;
56#endif
57
58 bool errorOccurred = image()->errorOccurred();
59 if (!errorOccurred && image()->response().httpStatusCode() >= 400)
60 errorOccurred = is<HTMLObjectElement>(element()); // An <object> considers a 404 to be an error and should fire onerror.
61 element().dispatchEvent(Event::create(errorOccurred ? eventNames().errorEvent : eventNames().loadEvent, Event::CanBubble::No, Event::IsCancelable::No));
62}
63
64String HTMLImageLoader::sourceURI(const AtomicString& attr) const
65{
66#if ENABLE(DASHBOARD_SUPPORT)
67 if (element().document().settings().usesDashboardBackwardCompatibilityMode() && attr.length() > 7 && attr.startsWith("url(\"") && attr.endsWith("\")"))
68 return attr.string().substring(5, attr.length() - 7);
69#endif
70
71 return stripLeadingAndTrailingHTMLSpaces(attr);
72}
73
74void HTMLImageLoader::notifyFinished(CachedResource&)
75{
76 ASSERT(image());
77 CachedImage& cachedImage = *image();
78
79 Ref<Element> protect(element());
80 ImageLoader::notifyFinished(cachedImage);
81
82 bool loadError = cachedImage.errorOccurred() || cachedImage.response().httpStatusCode() >= 400;
83 if (!loadError) {
84 if (!element().isConnected()) {
85 JSC::VM& vm = commonVM();
86 JSC::JSLockHolder lock(vm);
87 // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
88 // https://bugs.webkit.org/show_bug.cgi?id=142595
89 vm.heap.deprecatedReportExtraMemory(cachedImage.encodedSize());
90 }
91 }
92
93 if (loadError && is<HTMLObjectElement>(element()))
94 downcast<HTMLObjectElement>(element()).renderFallbackContent();
95}
96
97}
98