Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLLinkElement.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  * Copyright (C) 2003, 2008, 2010 Apple Inc. All rights reserved.
5  * Copyright (C) 2011 Google Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #ifndef HTMLLinkElement_h
25 #define HTMLLinkElement_h
26
27 #include "core/css/CSSStyleSheet.h"
28 #include "core/dom/DOMSettableTokenList.h"
29 #include "core/dom/IconURL.h"
30 #include "core/fetch/ResourceOwner.h"
31 #include "core/fetch/StyleSheetResource.h"
32 #include "core/fetch/StyleSheetResourceClient.h"
33 #include "core/html/HTMLElement.h"
34 #include "core/html/LinkRelAttribute.h"
35 #include "core/html/LinkResource.h"
36 #include "core/loader/LinkLoader.h"
37 #include "core/loader/LinkLoaderClient.h"
38
39 namespace WebCore {
40
41 class DocumentFragment;
42 class HTMLLinkElement;
43 class KURL;
44 class LinkImport;
45
46 template<typename T> class EventSender;
47 typedef EventSender<HTMLLinkElement> LinkEventSender;
48
49 //
50 // LinkStyle handles dynaically change-able link resources, which is
51 // typically @rel="stylesheet".
52 //
53 // It could be @rel="shortcut icon" or soething else though. Each of
54 // types might better be handled by a separate class, but dynamically
55 // changing @rel makes it harder to move such a design so we are
56 // sticking current way so far.
57 //
58 class LinkStyle FINAL : public LinkResource, ResourceOwner<StyleSheetResource> {
59     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
60 public:
61     static PassOwnPtrWillBeRawPtr<LinkStyle> create(HTMLLinkElement* owner);
62
63     explicit LinkStyle(HTMLLinkElement* owner);
64     virtual ~LinkStyle();
65
66     virtual Type type() const OVERRIDE { return Style; }
67     virtual void process() OVERRIDE;
68     virtual void ownerRemoved() OVERRIDE;
69     virtual bool hasLoaded() const OVERRIDE { return m_loadedSheet; }
70     virtual void trace(Visitor*) OVERRIDE;
71
72     void startLoadingDynamicSheet();
73     void notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred);
74     bool sheetLoaded();
75
76     void setDisabledState(bool);
77     void setSheetTitle(const String&);
78
79     bool styleSheetIsLoading() const;
80     bool hasSheet() const { return m_sheet; }
81     bool isDisabled() const { return m_disabledState == Disabled; }
82     bool isEnabledViaScript() const { return m_disabledState == EnabledViaScript; }
83     bool isUnset() const { return m_disabledState == Unset; }
84
85     CSSStyleSheet* sheet() const { return m_sheet.get(); }
86
87 private:
88     // From StyleSheetResourceClient
89     virtual void setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CSSStyleSheetResource*) OVERRIDE;
90
91     enum DisabledState {
92         Unset,
93         EnabledViaScript,
94         Disabled
95     };
96
97     enum PendingSheetType {
98         None,
99         NonBlocking,
100         Blocking
101     };
102
103     enum RemovePendingSheetNotificationType {
104         RemovePendingSheetNotifyImmediately,
105         RemovePendingSheetNotifyLater
106     };
107
108     void clearSheet();
109     void addPendingSheet(PendingSheetType);
110     void removePendingSheet(RemovePendingSheetNotificationType = RemovePendingSheetNotifyImmediately);
111     Document& document();
112
113     RefPtrWillBeMember<CSSStyleSheet> m_sheet;
114     DisabledState m_disabledState;
115     PendingSheetType m_pendingSheetType;
116     bool m_loading;
117     bool m_firedLoad;
118     bool m_loadedSheet;
119 };
120
121
122 class HTMLLinkElement FINAL : public HTMLElement, public LinkLoaderClient {
123 public:
124     static PassRefPtrWillBeRawPtr<HTMLLinkElement> create(Document&, bool createdByParser);
125     virtual ~HTMLLinkElement();
126
127     KURL href() const;
128     const AtomicString& rel() const;
129     String media() const { return m_media; }
130     String typeValue() const { return m_type; }
131     const LinkRelAttribute& relAttribute() const { return m_relAttribute; }
132
133     const AtomicString& type() const;
134
135     IconType iconType() const;
136
137     // the icon sizes as parsed from the HTML attribute
138     const Vector<IntSize>& iconSizes() const;
139
140     bool async() const;
141
142     CSSStyleSheet* sheet() const { return linkStyle() ? linkStyle()->sheet() : 0; }
143     Document* import() const;
144
145     bool styleSheetIsLoading() const;
146
147     bool isImport() const { return linkImport(); }
148     bool isDisabled() const { return linkStyle() && linkStyle()->isDisabled(); }
149     bool isEnabledViaScript() const { return linkStyle() && linkStyle()->isEnabledViaScript(); }
150     DOMSettableTokenList* sizes() const;
151
152     void dispatchPendingEvent(LinkEventSender*);
153     void scheduleEvent();
154     void dispatchEventImmediately();
155     static void dispatchPendingLoadEvents();
156
157     // From LinkLoaderClient
158     virtual bool shouldLoadLink() OVERRIDE;
159
160     // For LinkStyle
161     bool loadLink(const String& type, const KURL&);
162     bool isAlternate() const { return linkStyle()->isUnset() && m_relAttribute.isAlternate(); }
163     bool shouldProcessStyle() { return linkResourceToProcess() && linkStyle(); }
164     bool isCreatedByParser() const { return m_createdByParser; }
165
166     // Parse the icon size attribute into |iconSizes|, make this method public
167     // visible for testing purpose.
168     static void parseSizesAttribute(const AtomicString& value, Vector<IntSize>& iconSizes);
169
170     virtual void trace(Visitor*) OVERRIDE;
171
172 private:
173     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
174
175     LinkStyle* linkStyle() const;
176     LinkImport* linkImport() const;
177     LinkResource* linkResourceToProcess();
178
179     void process();
180     static void processCallback(Node*);
181
182     // From Node and subclassses
183     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
184     virtual void removedFrom(ContainerNode*) OVERRIDE;
185     virtual bool isURLAttribute(const Attribute&) const OVERRIDE;
186     virtual bool hasLegalLinkAttribute(const QualifiedName&) const OVERRIDE;
187     virtual const QualifiedName& subResourceAttributeName() const OVERRIDE;
188     virtual bool sheetLoaded() OVERRIDE;
189     virtual void notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred) OVERRIDE;
190     virtual void startLoadingDynamicSheet() OVERRIDE;
191     virtual void finishParsingChildren() OVERRIDE;
192
193     // From LinkLoaderClient
194     virtual void linkLoaded() OVERRIDE;
195     virtual void linkLoadingErrored() OVERRIDE;
196     virtual void didStartLinkPrerender() OVERRIDE;
197     virtual void didStopLinkPrerender() OVERRIDE;
198     virtual void didSendLoadForLinkPrerender() OVERRIDE;
199     virtual void didSendDOMContentLoadedForLinkPrerender() OVERRIDE;
200
201 private:
202     HTMLLinkElement(Document&, bool createdByParser);
203
204     OwnPtrWillBeMember<LinkResource> m_link;
205     LinkLoader m_linkLoader;
206
207     String m_type;
208     String m_media;
209     RefPtrWillBeMember<DOMSettableTokenList> m_sizes;
210     Vector<IntSize> m_iconSizes;
211     LinkRelAttribute m_relAttribute;
212
213     bool m_createdByParser;
214     bool m_isInShadowTree;
215 };
216
217 } //namespace
218
219 #endif