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 "CSSStyleSheet.h"
28#include "CachedStyleSheetClient.h"
29#include "CachedResourceHandle.h"
30#include "DOMSettableTokenList.h"
31#include "HTMLElement.h"
32#include "IconURL.h"
33#include "LinkLoader.h"
34#include "LinkLoaderClient.h"
35#include "LinkRelAttribute.h"
36#include "Timer.h"
37
38namespace WebCore {
39
40class HTMLLinkElement;
41class KURL;
42
43template<typename T> class EventSender;
44typedef EventSender<HTMLLinkElement> LinkEventSender;
45
46class HTMLLinkElement FINAL : public HTMLElement, public CachedStyleSheetClient, public LinkLoaderClient {
47public:
48    static PassRefPtr<HTMLLinkElement> create(const QualifiedName&, Document*, bool createdByParser);
49    virtual ~HTMLLinkElement();
50
51    KURL href() const;
52    String rel() const;
53
54    virtual String target() const;
55
56    String type() const;
57
58    IconType iconType() const;
59
60    // the icon size string as parsed from the HTML attribute
61    String iconSizes() const;
62
63    CSSStyleSheet* sheet() const { return m_sheet.get(); }
64
65    bool styleSheetIsLoading() const;
66
67    bool isDisabled() const { return m_disabledState == Disabled; }
68    bool isEnabledViaScript() const { return m_disabledState == EnabledViaScript; }
69    void setSizes(const String&);
70    DOMSettableTokenList* sizes() const;
71
72    void dispatchPendingEvent(LinkEventSender*);
73    static void dispatchPendingLoadEvents();
74
75private:
76    virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
77
78    virtual bool shouldLoadLink();
79    void process();
80    static void processCallback(Node*);
81    void clearSheet();
82
83    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
84    virtual void removedFrom(ContainerNode*) OVERRIDE;
85
86    // from CachedResourceClient
87    virtual void setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CachedCSSStyleSheet* sheet);
88    virtual bool sheetLoaded();
89    virtual void notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred);
90    virtual void startLoadingDynamicSheet();
91
92    virtual void linkLoaded() OVERRIDE;
93    virtual void linkLoadingErrored() OVERRIDE;
94
95    bool isAlternate() const { return m_disabledState == Unset && m_relAttribute.m_isAlternate; }
96
97    void setDisabledState(bool);
98
99    virtual bool isURLAttribute(const Attribute&) const OVERRIDE;
100
101private:
102    virtual void addSubresourceAttributeURLs(ListHashSet<KURL>&) const;
103
104    virtual void finishParsingChildren();
105
106    enum PendingSheetType { Unknown, ActiveSheet, InactiveSheet };
107    void addPendingSheet(PendingSheetType);
108
109    enum RemovePendingSheetNotificationType {
110        RemovePendingSheetNotifyImmediately,
111        RemovePendingSheetNotifyLater
112    };
113
114    void removePendingSheet(RemovePendingSheetNotificationType = RemovePendingSheetNotifyImmediately);
115
116#if ENABLE(MICRODATA)
117    virtual String itemValueText() const OVERRIDE;
118    virtual void setItemValueText(const String&, ExceptionCode&) OVERRIDE;
119#endif
120
121private:
122    HTMLLinkElement(const QualifiedName&, Document*, bool createdByParser);
123
124    LinkLoader m_linkLoader;
125    CachedResourceHandle<CachedCSSStyleSheet> m_cachedSheet;
126    RefPtr<CSSStyleSheet> m_sheet;
127    enum DisabledState {
128        Unset,
129        EnabledViaScript,
130        Disabled
131    };
132
133    String m_type;
134    String m_media;
135    RefPtr<DOMSettableTokenList> m_sizes;
136    DisabledState m_disabledState;
137    LinkRelAttribute m_relAttribute;
138    bool m_loading;
139    bool m_createdByParser;
140    bool m_isInShadowTree;
141    bool m_firedLoad;
142    bool m_loadedSheet;
143
144    PendingSheetType m_pendingSheetType;
145};
146
147} //namespace
148
149#endif
150