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.
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.
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.
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.
26 #include "core/fetch/ImageResource.h"
27 #include "core/fetch/ImageResourceClient.h"
28 #include "core/fetch/ResourcePtr.h"
29 #include "platform/heap/Handle.h"
30 #include "wtf/HashSet.h"
31 #include "wtf/text/AtomicString.h"
35 class ImageLoaderClient : public WillBeGarbageCollectedMixin {
37 virtual void notifyImageSourceChanged() = 0;
39 // Determines whether the observed ImageResource should have higher priority in the decoded resources cache.
40 virtual bool requestsHighLiveResourceCachePriority() { return false; }
42 virtual void trace(Visitor*) { }
45 ImageLoaderClient() { }
50 class RenderImageResource;
52 template<typename T> class EventSender;
53 typedef EventSender<ImageLoader> ImageEventSender;
55 class ImageLoader : public ImageResourceClient {
57 explicit ImageLoader(Element*);
58 virtual ~ImageLoader();
60 // This function should be called when the element is attached to a document; starts
61 // loading if a load hasn't already been started.
62 void updateFromElement();
64 // This function should be called whenever the 'src' attribute is set, even if its value
65 // doesn't change; starts new load unconditionally (matches Firefox and Opera behavior).
66 void updateFromElementIgnoringPreviousError();
68 void elementDidMoveToNewDocument();
70 Element* element() const { return m_element; }
71 bool imageComplete() const { return m_imageComplete; }
73 ImageResource* image() const { return m_image.get(); }
74 void setImage(ImageResource*); // Cancels pending load events, and doesn't dispatch new ones.
76 void setLoadManually(bool loadManually) { m_loadManually = loadManually; }
78 bool hasPendingActivity() const { return m_hasPendingLoadEvent || m_hasPendingErrorEvent; }
80 void dispatchPendingEvent(ImageEventSender*);
82 static void dispatchPendingLoadEvents();
83 static void dispatchPendingErrorEvents();
85 void addClient(ImageLoaderClient*);
86 void removeClient(ImageLoaderClient*);
89 virtual void notifyFinished(Resource*) OVERRIDE;
92 virtual void dispatchLoadEvent() = 0;
93 virtual String sourceURI(const AtomicString&) const = 0;
95 void updatedHasPendingEvent();
97 void dispatchPendingLoadEvent();
98 void dispatchPendingErrorEvent();
100 RenderImageResource* renderImageResource();
101 void updateRenderer();
103 void setImageWithoutConsideringPendingLoadEvent(ImageResource*);
104 void sourceImageChanged();
105 void clearFailedLoadURL();
107 void timerFired(Timer<ImageLoader>*);
109 typedef WillBePersistentHeapHashSet<RawPtrWillBeWeakMember<ImageLoaderClient> > ImageLoaderClientSet;
112 ResourcePtr<ImageResource> m_image;
113 ImageLoaderClientSet m_clients;
114 Timer<ImageLoader> m_derefElementTimer;
115 AtomicString m_failedLoadURL;
116 bool m_hasPendingLoadEvent : 1;
117 bool m_hasPendingErrorEvent : 1;
118 bool m_imageComplete : 1;
119 bool m_loadManually : 1;
120 bool m_elementIsProtected : 1;
121 unsigned m_highPriorityClientCount;