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