1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2004, 2009 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
23#pragma once
24
25#include "CachedImageClient.h"
26#include "CachedResourceHandle.h"
27#include "JSDOMPromiseDeferred.h"
28#include "Timer.h"
29#include <wtf/Vector.h>
30#include <wtf/text/AtomString.h>
31
32namespace WebCore {
33
34class Element;
35class ImageLoader;
36class RenderImageResource;
37
38template<typename T> class EventSender;
39typedef EventSender<ImageLoader> ImageEventSender;
40
41class ImageLoader : public CachedImageClient {
42 WTF_MAKE_FAST_ALLOCATED;
43public:
44 virtual ~ImageLoader();
45
46 // This function should be called when the element is attached to a document; starts
47 // loading if a load hasn't already been started.
48 void updateFromElement();
49
50 // This function should be called whenever the 'src' attribute is set, even if its value
51 // doesn't change; starts new load unconditionally (matches Firefox and Opera behavior).
52 void updateFromElementIgnoringPreviousError();
53
54 void elementDidMoveToNewDocument();
55
56 Element& element() { return m_element; }
57 const Element& element() const { return m_element; }
58
59 bool imageComplete() const { return m_imageComplete; }
60
61 CachedImage* image() const { return m_image.get(); }
62 void clearImage(); // Cancels pending beforeload and load events, and doesn't dispatch new ones.
63
64 void decode(Ref<DeferredPromise>&&);
65
66 void setLoadManually(bool loadManually) { m_loadManually = loadManually; }
67
68 bool hasPendingBeforeLoadEvent() const { return m_hasPendingBeforeLoadEvent; }
69 bool hasPendingActivity() const { return m_hasPendingLoadEvent || m_hasPendingErrorEvent; }
70
71 void dispatchPendingEvent(ImageEventSender*);
72
73 static void dispatchPendingBeforeLoadEvents();
74 static void dispatchPendingLoadEvents();
75 static void dispatchPendingErrorEvents();
76
77protected:
78 explicit ImageLoader(Element&);
79 void notifyFinished(CachedResource&) override;
80
81private:
82 virtual void dispatchLoadEvent() = 0;
83 virtual String sourceURI(const AtomString&) const = 0;
84
85 void updatedHasPendingEvent();
86
87 void dispatchPendingBeforeLoadEvent();
88 void dispatchPendingLoadEvent();
89 void dispatchPendingErrorEvent();
90
91 RenderImageResource* renderImageResource();
92 void updateRenderer();
93
94 void clearImageWithoutConsideringPendingLoadEvent();
95 void clearFailedLoadURL();
96
97 bool hasPendingDecodePromises() const { return !m_decodingPromises.isEmpty(); }
98 void decodeError(const char*);
99 void decode();
100
101 void timerFired();
102
103 Element& m_element;
104 CachedResourceHandle<CachedImage> m_image;
105 Timer m_derefElementTimer;
106 RefPtr<Element> m_protectedElement;
107 AtomString m_failedLoadURL;
108 Vector<RefPtr<DeferredPromise>, 1> m_decodingPromises;
109 bool m_hasPendingBeforeLoadEvent : 1;
110 bool m_hasPendingLoadEvent : 1;
111 bool m_hasPendingErrorEvent : 1;
112 bool m_imageComplete : 1;
113 bool m_loadManually : 1;
114 bool m_elementIsProtected : 1;
115};
116
117}
118