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 THIRD_PARTY_BLINK_RENDERER_CORE_HTML_HTML_LINK_ELEMENT_H_
25 #define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_HTML_LINK_ELEMENT_H_
26 
27 #include <memory>
28 
29 #include "base/single_thread_task_runner.h"
30 #include "third_party/blink/renderer/core/core_export.h"
31 #include "third_party/blink/renderer/core/dom/create_element_flags.h"
32 #include "third_party/blink/renderer/core/dom/dom_token_list.h"
33 #include "third_party/blink/renderer/core/dom/increment_load_event_delay_count.h"
34 #include "third_party/blink/renderer/core/html/html_element.h"
35 #include "third_party/blink/renderer/core/html/link_rel_attribute.h"
36 #include "third_party/blink/renderer/core/html/link_resource.h"
37 #include "third_party/blink/renderer/core/html/link_style.h"
38 #include "third_party/blink/renderer/core/html/rel_list.h"
39 #include "third_party/blink/renderer/core/loader/link_loader_client.h"
40 #include "third_party/blink/renderer/platform/loader/fetch/fetch_parameters.h"
41 #include "third_party/blink/renderer/platform/wtf/hash_set.h"
42 
43 namespace blink {
44 
45 class KURL;
46 class LinkImport;
47 class LinkLoader;
48 struct LinkLoadParameters;
49 
50 class CORE_EXPORT HTMLLinkElement final : public HTMLElement,
51                                           public LinkLoaderClient {
52   DEFINE_WRAPPERTYPEINFO();
53 
54  public:
55   HTMLLinkElement(Document&, const CreateElementFlags);
56   ~HTMLLinkElement() override;
57 
58   KURL Href() const;
59   const AtomicString& Rel() const;
Media()60   String Media() const { return media_; }
TypeValue()61   String TypeValue() const { return type_; }
AsValue()62   String AsValue() const { return as_; }
IntegrityValue()63   String IntegrityValue() const { return integrity_; }
ImportanceValue()64   String ImportanceValue() const { return importance_; }
GetReferrerPolicy()65   network::mojom::ReferrerPolicy GetReferrerPolicy() const {
66     return referrer_policy_;
67   }
RelAttribute()68   const LinkRelAttribute& RelAttribute() const { return rel_attribute_; }
relList()69   DOMTokenList& relList() const {
70     return static_cast<DOMTokenList&>(*rel_list_);
71   }
Scope()72   String Scope() const { return scope_; }
73 
74   const AtomicString& GetType() const;
75 
76   mojom::blink::FaviconIconType GetIconType() const;
77 
78   // the icon sizes as parsed from the HTML attribute
79   const Vector<gfx::Size>& IconSizes() const;
80 
81   bool Async() const;
82 
sheet()83   CSSStyleSheet* sheet() const {
84     return GetLinkStyle() ? GetLinkStyle()->Sheet() : nullptr;
85   }
86   Document* import() const;
87 
88   bool StyleSheetIsLoading() const;
89 
IsImport()90   bool IsImport() const { return GetLinkImport(); }
IsDisabled()91   bool IsDisabled() const {
92     return GetLinkStyle() && GetLinkStyle()->IsDisabled();
93   }
IsEnabledViaScript()94   bool IsEnabledViaScript() const {
95     return GetLinkStyle() && GetLinkStyle()->IsEnabledViaScript();
96   }
97 
98   DOMTokenList* sizes() const;
99 
100   // IDL method.
101   DOMTokenList* resources() const;
102 
ValidResourceUrls()103   const HashSet<KURL>& ValidResourceUrls() const {
104     return valid_resource_urls_;
105   }
106 
107   void ScheduleEvent();
108 
109   // From LinkLoaderClient
110   bool ShouldLoadLink() override;
111   bool IsLinkCreatedByParser() override;
112 
113   // For LinkStyle
114   bool LoadLink(const LinkLoadParameters&);
115   void LoadStylesheet(const LinkLoadParameters&,
116                       const WTF::TextEncoding&,
117                       FetchParameters::DeferOption,
118                       ResourceClient*);
IsAlternate()119   bool IsAlternate() const {
120     // TODO(crbug.com/1087043): Remove this if() condition once the feature has
121     // landed and no compat issues are reported.
122     bool not_explicitly_enabled =
123         !GetLinkStyle()->IsExplicitlyEnabled() ||
124         !RuntimeEnabledFeatures::LinkDisabledNewSpecBehaviorEnabled(
125             GetExecutionContext());
126     return GetLinkStyle()->IsUnset() && rel_attribute_.IsAlternate() &&
127            not_explicitly_enabled;
128   }
ShouldProcessStyle()129   bool ShouldProcessStyle() {
130     return LinkResourceToProcess() && GetLinkStyle();
131   }
IsCreatedByParser()132   bool IsCreatedByParser() const { return created_by_parser_; }
133 
134   void Trace(Visitor*) const override;
135 
136  private:
137   LinkStyle* GetLinkStyle() const;
138   LinkImport* GetLinkImport() const;
139   LinkResource* LinkResourceToProcess();
140 
141   void Process();
142   static void ProcessCallback(Node*);
143 
144   // Always call this asynchronously because this can cause synchronous
145   // Document load event and JavaScript execution.
146   void DispatchPendingEvent(std::unique_ptr<IncrementLoadEventDelayCount>);
147 
148   // From Node and subclassses
149   void ParseAttribute(const AttributeModificationParams&) override;
150   InsertionNotificationRequest InsertedInto(ContainerNode&) override;
151   void RemovedFrom(ContainerNode&) override;
152   bool IsURLAttribute(const Attribute&) const override;
153   bool HasLegalLinkAttribute(const QualifiedName&) const override;
154   const QualifiedName& SubResourceAttributeName() const override;
155   bool SheetLoaded() override;
156   void NotifyLoadedSheetAndAllCriticalSubresources(
157       LoadedSheetErrorStatus) override;
158   void StartLoadingDynamicSheet() override;
159   void FinishParsingChildren() override;
160   bool HasActivationBehavior() const override;
161 
162   // From LinkLoaderClient
163   void LinkLoaded() override;
164   void LinkLoadingErrored() override;
165   void DidStartLinkPrerender() override;
166   void DidStopLinkPrerender() override;
167   void DidSendLoadForLinkPrerender() override;
168   void DidSendDOMContentLoadedForLinkPrerender() override;
169   scoped_refptr<base::SingleThreadTaskRunner> GetLoadingTaskRunner() override;
170 
171   Member<LinkResource> link_;
172   Member<LinkLoader> link_loader_;
173 
174   String type_;
175   String as_;
176   String media_;
177   String integrity_;
178   String importance_;
179   network::mojom::ReferrerPolicy referrer_policy_;
180   Member<DOMTokenList> sizes_;
181   Vector<gfx::Size> icon_sizes_;
182   Member<RelList> rel_list_;
183   LinkRelAttribute rel_attribute_;
184   String scope_;
185   Member<DOMTokenList> resources_;
186   HashSet<KURL> valid_resource_urls_;
187 
188   bool created_by_parser_;
189 };
190 
191 }  // namespace blink
192 
193 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_HTML_HTML_LINK_ELEMENT_H_
194