OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource
, DebugMessageLevel, String("Preconnect triggered for " + href.host()))); | 138 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource
, DebugMessageLevel, String("Preconnect triggered for " + href.host()))); |
139 if (crossOrigin != CrossOriginAttributeNotSet) { | 139 if (crossOrigin != CrossOriginAttributeNotSet) { |
140 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSo
urce, DebugMessageLevel, | 140 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSo
urce, DebugMessageLevel, |
141 String("Preconnect CORS setting is ") + String((crossOrigin
== CrossOriginAttributeAnonymous) ? "anonymous" : "use-credentials"))); | 141 String("Preconnect CORS setting is ") + String((crossOrigin
== CrossOriginAttributeAnonymous) ? "anonymous" : "use-credentials"))); |
142 } | 142 } |
143 } | 143 } |
144 preconnect(href, crossOrigin); | 144 preconnect(href, crossOrigin); |
145 } | 145 } |
146 } | 146 } |
147 | 147 |
148 // TODO(yoav): Replace Resource:Type here with some way that conveys priority bu
t not context. | 148 static bool getPriorityTypeFromAsAttribute(const String& as, Resource::Type& typ
e) |
149 static bool getTypeFromAsAttribute(const String& as, Resource::Type& type) | |
150 { | 149 { |
151 if (as.isEmpty()) | 150 if (as.isEmpty()) |
152 return false; | 151 return false; |
153 // TODO(yoav): Return false also when the `as` value is not a valid one. | 152 |
154 // TODO(yoav): Add actual types here and make sure priorities work according
ly. | 153 if (equalIgnoringCase(as, "image")) |
155 type = Resource::LinkSubresource; | 154 type = Resource::Image; |
| 155 else if (equalIgnoringCase(as, "script")) |
| 156 type = Resource::Script; |
| 157 else if (equalIgnoringCase(as, "stylesheet")) |
| 158 type = Resource::CSSStyleSheet; |
| 159 else |
| 160 return false; |
| 161 |
156 return true; | 162 return true; |
157 } | 163 } |
158 | 164 |
159 void LinkLoader::preloadIfNeeded(const LinkRelAttribute& relAttribute, const KUR
L& href, Document& document, const String& as) | 165 void LinkLoader::preloadIfNeeded(const LinkRelAttribute& relAttribute, const KUR
L& href, Document& document, const String& as) |
160 { | 166 { |
161 if (relAttribute.isLinkPreload()) { | 167 if (relAttribute.isLinkPreload()) { |
162 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled()); | 168 ASSERT(RuntimeEnabledFeatures::linkPreloadEnabled()); |
163 if (!href.isValid() || href.isEmpty()) { | 169 if (!href.isValid() || href.isEmpty()) { |
164 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource
, WarningMessageLevel, String("<link rel=preload> has an invalid `href` value"))
); | 170 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource
, WarningMessageLevel, String("<link rel=preload> has an invalid `href` value"))
); |
165 return; | 171 return; |
166 } | 172 } |
167 Resource::Type type; | 173 // TODO(yoav): Figure out a way that 'as' would be used to set request h
eaders. |
168 if (!getTypeFromAsAttribute(as, type)) { | 174 Resource::Type priorityType; |
| 175 if (!getPriorityTypeFromAsAttribute(as, priorityType)) { |
169 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource
, WarningMessageLevel, String("<link rel=preload> must have a valid `as` value")
)); | 176 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource
, WarningMessageLevel, String("<link rel=preload> must have a valid `as` value")
)); |
170 return; | 177 return; |
171 } | 178 } |
172 FetchRequest linkRequest(ResourceRequest(document.completeURL(href)), Fe
tchInitiatorTypeNames::link); | 179 FetchRequest linkRequest(ResourceRequest(document.completeURL(href)), Fe
tchInitiatorTypeNames::link); |
| 180 linkRequest.setPriority(ResourceFetcher::loadPriority(priorityType, link
Request)); |
173 Settings* settings = document.settings(); | 181 Settings* settings = document.settings(); |
174 if (settings && settings->logPreload()) | 182 if (settings && settings->logPreload()) |
175 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource
, DebugMessageLevel, String("Preload triggered for " + href.host() + href.path()
))); | 183 document.addConsoleMessage(ConsoleMessage::create(OtherMessageSource
, DebugMessageLevel, String("Preload triggered for " + href.host() + href.path()
))); |
176 setResource(document.fetcher()->fetchLinkPreloadResource(type, linkReque
st)); | 184 setResource(document.fetcher()->fetchLinkPreloadResource(Resource::LinkP
reload, linkRequest)); |
177 } | 185 } |
178 } | 186 } |
179 | 187 |
180 bool LinkLoader::loadLinkFromHeader(const String& headerValue, Document* documen
t) | 188 bool LinkLoader::loadLinkFromHeader(const String& headerValue, Document* documen
t) |
181 { | 189 { |
182 if (!document) | 190 if (!document) |
183 return false; | 191 return false; |
184 LinkHeaderSet headerSet(headerValue); | 192 LinkHeaderSet headerSet(headerValue); |
185 for (auto& header : headerSet) { | 193 for (auto& header : headerSet) { |
186 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty()) | 194 if (!header.valid() || header.url().isEmpty() || header.rel().isEmpty()) |
187 return false; | 195 return false; |
188 LinkRelAttribute relAttribute(header.rel()); | 196 LinkRelAttribute relAttribute(header.rel()); |
189 KURL url = document->completeURL(header.url()); | 197 KURL url = document->completeURL(header.url()); |
190 if (RuntimeEnabledFeatures::linkHeaderEnabled()) | 198 if (RuntimeEnabledFeatures::linkHeaderEnabled()) |
191 dnsPrefetchIfNeeded(relAttribute, url, *document); | 199 dnsPrefetchIfNeeded(relAttribute, url, *document); |
192 | 200 |
193 if (RuntimeEnabledFeatures::linkPreconnectEnabled()) | 201 if (RuntimeEnabledFeatures::linkPreconnectEnabled()) |
194 preconnectIfNeeded(relAttribute, url, *document, header.crossOrigin(
)); | 202 preconnectIfNeeded(relAttribute, url, *document, header.crossOrigin(
)); |
195 | 203 |
196 // FIXME: Add more supported headers as needed. | 204 // FIXME: Add more supported headers as needed. |
197 } | 205 } |
198 return true; | 206 return true; |
199 } | 207 } |
200 | 208 |
201 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const AtomicStri
ng& crossOriginMode, const String& type, const String& as, const KURL& href, Doc
ument& document) | 209 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const AtomicStri
ng& crossOriginMode, const String& type, const String& as, const KURL& href, Doc
ument& document) |
202 { | 210 { |
| 211 // TODO(yoav): Do all links need to load only after they're in document??? |
| 212 |
203 // TODO(yoav): Convert all uses of the CrossOriginAttribute to CrossOriginAt
tributeValue. crbug.com/486689 | 213 // TODO(yoav): Convert all uses of the CrossOriginAttribute to CrossOriginAt
tributeValue. crbug.com/486689 |
204 // FIXME(crbug.com/463266): We're ignoring type here. Maybe we shouldn't. | 214 // FIXME(crbug.com/463266): We're ignoring type here. Maybe we shouldn't. |
205 dnsPrefetchIfNeeded(relAttribute, href, document); | 215 dnsPrefetchIfNeeded(relAttribute, href, document); |
206 | 216 |
207 preconnectIfNeeded(relAttribute, href, document, crossOriginAttributeValue(c
rossOriginMode)); | 217 preconnectIfNeeded(relAttribute, href, document, crossOriginAttributeValue(c
rossOriginMode)); |
208 | 218 |
209 preloadIfNeeded(relAttribute, href, document, as); | 219 if (m_client->shouldLoadLink()) |
| 220 preloadIfNeeded(relAttribute, href, document, as); |
210 | 221 |
211 // FIXME(crbug.com/323096): Should take care of import. | 222 // FIXME(crbug.com/323096): Should take care of import. |
212 if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource() || re
lAttribute.isTransitionExitingStylesheet()) && href.isValid() && document.frame(
)) { | 223 if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource() || re
lAttribute.isTransitionExitingStylesheet()) && href.isValid() && document.frame(
)) { |
213 if (!m_client->shouldLoadLink()) | 224 if (!m_client->shouldLoadLink()) |
214 return false; | 225 return false; |
215 Resource::Type type = relAttribute.isLinkSubresource() ? Resource::Link
Subresource : Resource::LinkPrefetch; | 226 Resource::Type type = relAttribute.isLinkSubresource() ? Resource::Link
Subresource : Resource::LinkPrefetch; |
216 FetchRequest linkRequest(ResourceRequest(document.completeURL(href)), Fe
tchInitiatorTypeNames::link); | 227 FetchRequest linkRequest(ResourceRequest(document.completeURL(href)), Fe
tchInitiatorTypeNames::link); |
217 if (!crossOriginMode.isNull()) | 228 if (!crossOriginMode.isNull()) |
218 linkRequest.setCrossOriginAccessControl(document.securityOrigin(), c
rossOriginMode); | 229 linkRequest.setCrossOriginAccessControl(document.securityOrigin(), c
rossOriginMode); |
219 setResource(document.fetcher()->fetchLinkResource(type, linkRequest)); | 230 setResource(document.fetcher()->fetchLinkResource(type, linkRequest)); |
(...skipping 23 matching lines...) Expand all Loading... |
243 m_prerender.clear(); | 254 m_prerender.clear(); |
244 } | 255 } |
245 } | 256 } |
246 | 257 |
247 DEFINE_TRACE(LinkLoader) | 258 DEFINE_TRACE(LinkLoader) |
248 { | 259 { |
249 visitor->trace(m_prerender); | 260 visitor->trace(m_prerender); |
250 } | 261 } |
251 | 262 |
252 } | 263 } |
OLD | NEW |