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 #ifndef ImageLoader_h 24 #define ImageLoader_h 25 26 #include "core/fetch/ImageResource.h" 27 #include "core/fetch/ImageResourceClient.h" 28 #include "core/fetch/ResourcePtr.h" 29 #include "wtf/HashSet.h" 30 #include "wtf/text/AtomicString.h" 31 32 namespace WebCore { 33 34 class ImageLoaderClient { 35 public: 36 virtual void notifyImageSourceChanged() = 0; 37 38 // Determines whether the observed ImageResource should have higher priority in the decoded resources cache. requestsHighLiveResourceCachePriority()39 virtual bool requestsHighLiveResourceCachePriority() { return false; } 40 41 protected: ImageLoaderClient()42 ImageLoaderClient() { } 43 }; 44 45 class Element; 46 class ImageLoader; 47 class RenderImageResource; 48 49 template<typename T> class EventSender; 50 typedef EventSender<ImageLoader> ImageEventSender; 51 52 class ImageLoader : public ImageResourceClient { 53 public: 54 explicit ImageLoader(Element*); 55 virtual ~ImageLoader(); 56 57 // This function should be called when the element is attached to a document; starts 58 // loading if a load hasn't already been started. 59 void updateFromElement(); 60 61 // This function should be called whenever the 'src' attribute is set, even if its value 62 // doesn't change; starts new load unconditionally (matches Firefox and Opera behavior). 63 void updateFromElementIgnoringPreviousError(); 64 65 void elementDidMoveToNewDocument(); 66 element()67 Element* element() const { return m_element; } imageComplete()68 bool imageComplete() const { return m_imageComplete; } 69 image()70 ImageResource* image() const { return m_image.get(); } 71 void setImage(ImageResource*); // Cancels pending beforeload and load events, and doesn't dispatch new ones. 72 setLoadManually(bool loadManually)73 void setLoadManually(bool loadManually) { m_loadManually = loadManually; } 74 hasPendingBeforeLoadEvent()75 bool hasPendingBeforeLoadEvent() const { return m_hasPendingBeforeLoadEvent; } hasPendingActivity()76 bool hasPendingActivity() const { return m_hasPendingLoadEvent || m_hasPendingErrorEvent; } 77 78 void dispatchPendingEvent(ImageEventSender*); 79 80 static void dispatchPendingBeforeLoadEvents(); 81 static void dispatchPendingLoadEvents(); 82 static void dispatchPendingErrorEvents(); 83 84 void addClient(ImageLoaderClient*); 85 void removeClient(ImageLoaderClient*); 86 87 protected: 88 virtual void notifyFinished(Resource*); 89 90 private: 91 virtual void dispatchLoadEvent() = 0; 92 virtual String sourceURI(const AtomicString&) const = 0; 93 94 void updatedHasPendingEvent(); 95 96 void dispatchPendingBeforeLoadEvent(); 97 void dispatchPendingLoadEvent(); 98 void dispatchPendingErrorEvent(); 99 100 RenderImageResource* renderImageResource(); 101 void updateRenderer(); 102 103 void setImageWithoutConsideringPendingLoadEvent(ImageResource*); 104 void sourceImageChanged(); 105 void clearFailedLoadURL(); 106 107 void timerFired(Timer<ImageLoader>*); 108 109 Element* m_element; 110 ResourcePtr<ImageResource> m_image; 111 HashSet<ImageLoaderClient*> m_clients; 112 Timer<ImageLoader> m_derefElementTimer; 113 AtomicString m_failedLoadURL; 114 bool m_hasPendingBeforeLoadEvent : 1; 115 bool m_hasPendingLoadEvent : 1; 116 bool m_hasPendingErrorEvent : 1; 117 bool m_imageComplete : 1; 118 bool m_loadManually : 1; 119 bool m_elementIsProtected : 1; 120 unsigned m_highPriorityClientCount; 121 }; 122 123 } 124 125 #endif 126