Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / loader / ImageLoader.h
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 "platform/heap/Handle.h"
30 #include "wtf/HashSet.h"
31 #include "wtf/text/AtomicString.h"
32
33 namespace WebCore {
34
35 class ImageLoaderClient : public WillBeGarbageCollectedMixin {
36 public:
37     virtual void notifyImageSourceChanged() = 0;
38
39     // Determines whether the observed ImageResource should have higher priority in the decoded resources cache.
40     virtual bool requestsHighLiveResourceCachePriority() { return false; }
41
42     virtual void trace(Visitor*) { }
43
44 protected:
45     ImageLoaderClient() { }
46 };
47
48 class Element;
49 class ImageLoader;
50 class RenderImageResource;
51
52 template<typename T> class EventSender;
53 typedef EventSender<ImageLoader> ImageEventSender;
54
55 class ImageLoader : public ImageResourceClient {
56 public:
57     explicit ImageLoader(Element*);
58     virtual ~ImageLoader();
59
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();
63
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();
67
68     void elementDidMoveToNewDocument();
69
70     Element* element() const { return m_element; }
71     bool imageComplete() const { return m_imageComplete; }
72
73     ImageResource* image() const { return m_image.get(); }
74     void setImage(ImageResource*); // Cancels pending load events, and doesn't dispatch new ones.
75
76     void setLoadManually(bool loadManually) { m_loadManually = loadManually; }
77
78     bool hasPendingActivity() const { return m_hasPendingLoadEvent || m_hasPendingErrorEvent; }
79
80     void dispatchPendingEvent(ImageEventSender*);
81
82     static void dispatchPendingLoadEvents();
83     static void dispatchPendingErrorEvents();
84
85     void addClient(ImageLoaderClient*);
86     void removeClient(ImageLoaderClient*);
87
88 protected:
89     virtual void notifyFinished(Resource*) OVERRIDE;
90
91 private:
92     virtual void dispatchLoadEvent() = 0;
93     virtual String sourceURI(const AtomicString&) const = 0;
94
95     void updatedHasPendingEvent();
96
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     typedef WillBePersistentHeapHashSet<RawPtrWillBeWeakMember<ImageLoaderClient> > ImageLoaderClientSet;
110
111     Element* m_element;
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;
122 };
123
124 }
125
126 #endif