1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
|
/*
* Copyright (C) 2008-2023 Apple Inc. All Rights Reserved.
* Copyright (C) 2009 Torch Mobile, Inc. http://www.torchmobile.com/
* Copyright (C) 2010-2021 Google Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "HTMLPreloadScanner.h"
#include "HTMLImageElement.h"
#include "HTMLNames.h"
#include "HTMLSrcsetParser.h"
#include "HTMLTokenizer.h"
#include "InputTypeNames.h"
#include "JSRequestPriority.h"
#include "LinkLoader.h"
#include "LinkRelAttribute.h"
#include "Logging.h"
#include "MIMETypeRegistry.h"
#include "MediaList.h"
#include "MediaQueryEvaluator.h"
#include "MediaQueryParser.h"
#include "RenderView.h"
#include "ScriptElement.h"
#include "SecurityPolicy.h"
#include "Settings.h"
#include "SizesAttributeParser.h"
#include <wtf/MainThread.h>
#include <wtf/SortedArrayMap.h>
namespace WebCore {
using namespace HTMLNames;
TokenPreloadScanner::TagId TokenPreloadScanner::tagIdFor(const HTMLToken::DataVector& data)
{
static constexpr std::pair<PackedASCIILiteral<uint64_t>, TokenPreloadScanner::TagId> mappings[] = {
{ "base", TagId::Base },
{ "img", TagId::Img },
{ "input", TagId::Input },
{ "link", TagId::Link },
{ "meta", TagId::Meta },
{ "picture", TagId::Picture },
{ "script", TagId::Script },
{ "source", TagId::Source },
{ "style", TagId::Style },
{ "template", TagId::Template },
};
static constexpr SortedArrayMap map { mappings };
return map.get(data.span(), TagId::Unknown);
}
ASCIILiteral TokenPreloadScanner::initiatorFor(TagId tagId)
{
switch (tagId) {
case TagId::Source:
case TagId::Img:
return "img"_s;
case TagId::Input:
return "input"_s;
case TagId::Link:
return "link"_s;
case TagId::Script:
return "script"_s;
case TagId::Unknown:
case TagId::Style:
case TagId::Base:
case TagId::Template:
case TagId::Meta:
case TagId::Picture:
ASSERT_NOT_REACHED();
return "unknown"_s;
}
ASSERT_NOT_REACHED();
return "unknown"_s;
}
class TokenPreloadScanner::StartTagScanner {
public:
explicit StartTagScanner(Document& document, TagId tagId, float deviceScaleFactor = 1.0)
: m_document(document)
, m_tagId(tagId)
, m_linkIsStyleSheet(false)
, m_linkIsPreload(false)
, m_metaIsViewport(false)
, m_metaIsDisabledAdaptations(false)
, m_inputIsImage(false)
, m_deviceScaleFactor(deviceScaleFactor)
{
}
void processAttributes(const HTMLToken::AttributeList& attributes, Vector<bool>& pictureState)
{
ASSERT(isMainThread());
if (m_tagId >= TagId::Unknown)
return;
for (auto& attribute : attributes) {
auto knownAttributeName = AtomString::lookUp(attribute.name.data(), attribute.name.size());
StringView attributeValue { attribute.value.data(), static_cast<unsigned>(attribute.value.size()) };
processAttribute(knownAttributeName, attributeValue, pictureState);
}
if (m_tagId == TagId::Source && !pictureState.isEmpty() && !pictureState.last() && m_mediaMatched && m_typeMatched && !m_srcSetAttribute.isEmpty()) {
auto sourceSize = SizesAttributeParser(m_sizesAttribute, m_document).length();
ImageCandidate imageCandidate = bestFitSourceForImageAttributes(m_deviceScaleFactor, AtomString { m_urlToLoad }, m_srcSetAttribute, sourceSize);
if (!imageCandidate.isEmpty()) {
pictureState.last() = true;
setURLToLoadAllowingReplacement(imageCandidate.string);
}
}
// Resolve between src and srcSet if we have them and the tag is img.
if (m_tagId == TagId::Img && !m_srcSetAttribute.isEmpty()) {
auto sourceSize = SizesAttributeParser(m_sizesAttribute, m_document).length();
ImageCandidate imageCandidate = bestFitSourceForImageAttributes(m_deviceScaleFactor, AtomString { m_urlToLoad }, m_srcSetAttribute, sourceSize);
setURLToLoadAllowingReplacement(imageCandidate.string);
}
if (m_metaIsViewport && !m_metaContent.isNull())
m_document.processViewport(m_metaContent, ViewportArguments::ViewportMeta);
if (m_metaIsDisabledAdaptations && !m_metaContent.isNull())
m_document.processDisabledAdaptations(m_metaContent);
}
std::unique_ptr<PreloadRequest> createPreloadRequest(const URL& predictedBaseURL)
{
if (!shouldPreload())
return nullptr;
auto type = resourceType();
if (!type)
return nullptr;
if (m_tagId == TagId::Link && !LinkLoader::isSupportedType(type.value(), m_typeAttribute, m_document))
return nullptr;
// Do not preload if lazyload is possible but metadata fetch is disabled.
if (HTMLImageElement::hasLazyLoadableAttributeValue(m_lazyloadAttribute))
return nullptr;
std::optional<ScriptType> scriptType;
if (m_tagId == TagId::Script) {
scriptType = ScriptElement::determineScriptType(m_typeAttribute, m_languageAttribute);
if (!scriptType)
return nullptr;
if (scriptType != ScriptType::Module && m_scriptIsNomodule)
return nullptr;
}
auto request = makeUnique<PreloadRequest>(initiatorFor(m_tagId), m_urlToLoad, predictedBaseURL, type.value(), m_mediaAttribute, scriptType.value_or(ScriptType::Classic), m_referrerPolicy, m_fetchPriorityHint);
request->setCrossOriginMode(m_crossOriginMode);
request->setNonce(m_nonceAttribute);
request->setScriptIsAsync(m_scriptIsAsync);
// According to the spec, the module tag ignores the "charset" attribute as the same to the worker's
// importScript. But WebKit supports the "charset" for importScript intentionally. So to be consistent,
// even for the module tags, we handle the "charset" attribute.
request->setCharset(charset());
return request;
}
static bool match(const AtomString& name, const QualifiedName& qName)
{
ASSERT(isMainThread());
return qName.localName() == name;
}
private:
void processImageAndScriptAttribute(const AtomString& attributeName, StringView attributeValue)
{
if (match(attributeName, srcAttr))
setURLToLoad(attributeValue);
else if (match(attributeName, crossoriginAttr))
m_crossOriginMode = attributeValue.trim(isASCIIWhitespace<UChar>).toString();
else if (match(attributeName, charsetAttr))
m_charset = attributeValue.toString();
}
void processAttribute(const AtomString& attributeName, StringView attributeValue, const Vector<bool>& pictureState)
{
bool inPicture = !pictureState.isEmpty();
bool alreadyMatchedSource = inPicture && pictureState.last();
switch (m_tagId) {
case TagId::Img:
if (inPicture && alreadyMatchedSource)
break;
if (match(attributeName, srcsetAttr) && m_srcSetAttribute.isNull()) {
m_srcSetAttribute = attributeValue.toString();
break;
}
if (match(attributeName, sizesAttr) && m_sizesAttribute.isNull()) {
m_sizesAttribute = attributeValue.toString();
break;
}
if (match(attributeName, fetchpriorityAttr) && m_document.settings().fetchPriorityEnabled()) {
m_fetchPriorityHint = parseEnumerationFromString<RequestPriority>(attributeValue.toString()).value_or(RequestPriority::Auto);
break;
}
if (match(attributeName, referrerpolicyAttr)) {
m_referrerPolicy = parseReferrerPolicy(attributeValue, ReferrerPolicySource::ReferrerPolicyAttribute).value_or(ReferrerPolicy::EmptyString);
break;
}
if (m_document.settings().lazyImageLoadingEnabled()) {
if (match(attributeName, loadingAttr) && m_lazyloadAttribute.isNull()) {
m_lazyloadAttribute = attributeValue.toString();
break;
}
}
processImageAndScriptAttribute(attributeName, attributeValue);
break;
case TagId::Source:
if (inPicture && alreadyMatchedSource)
break;
if (match(attributeName, srcsetAttr) && m_srcSetAttribute.isNull()) {
m_srcSetAttribute = attributeValue.toString();
break;
}
if (match(attributeName, sizesAttr) && m_sizesAttribute.isNull()) {
m_sizesAttribute = attributeValue.toString();
break;
}
if (match(attributeName, mediaAttr) && m_mediaAttribute.isNull()) {
m_mediaAttribute = attributeValue.toString();
auto mediaQueries = MQ::MediaQueryParser::parse(m_mediaAttribute, { m_document });
RefPtr documentElement = m_document.documentElement();
LOG(MediaQueries, "HTMLPreloadScanner %p processAttribute evaluating media queries", this);
m_mediaMatched = MQ::MediaQueryEvaluator { m_document.printing() ? printAtom() : screenAtom(), m_document, documentElement ? documentElement->computedStyle() : nullptr }.evaluate(mediaQueries);
}
if (match(attributeName, typeAttr) && m_typeAttribute.isNull()) {
// when multiple type attributes present: first value wins, ignore subsequent (to match ImageElement parser and Blink behaviours)
m_typeAttribute = attributeValue.toString();
m_typeMatched &= MIMETypeRegistry::isSupportedImageVideoOrSVGMIMEType(m_typeAttribute);
}
break;
case TagId::Script:
if (match(attributeName, typeAttr)) {
m_typeAttribute = attributeValue.toString();
break;
} else if (match(attributeName, languageAttr)) {
m_languageAttribute = attributeValue.toString();
break;
} else if (match(attributeName, nonceAttr)) {
m_nonceAttribute = attributeValue.toString();
break;
} else if (match(attributeName, referrerpolicyAttr)) {
m_referrerPolicy = parseReferrerPolicy(attributeValue, ReferrerPolicySource::ReferrerPolicyAttribute).value_or(ReferrerPolicy::EmptyString);
break;
} else if (match(attributeName, nomoduleAttr)) {
m_scriptIsNomodule = true;
break;
} else if (match(attributeName, asyncAttr)) {
m_scriptIsAsync = true;
break;
} else if (match(attributeName, fetchpriorityAttr) && m_document.settings().fetchPriorityEnabled()) {
m_fetchPriorityHint = parseEnumerationFromString<RequestPriority>(attributeValue.toString()).value_or(RequestPriority::Auto);
break;
}
processImageAndScriptAttribute(attributeName, attributeValue);
break;
case TagId::Link:
if (match(attributeName, hrefAttr))
setURLToLoad(attributeValue);
else if (match(attributeName, relAttr)) {
LinkRelAttribute parsedAttribute { m_document, attributeValue };
m_linkIsStyleSheet = relAttributeIsStyleSheet(parsedAttribute);
m_linkIsPreload = parsedAttribute.isLinkPreload;
} else if (match(attributeName, mediaAttr))
m_mediaAttribute = attributeValue.toString();
else if (match(attributeName, charsetAttr))
m_charset = attributeValue.toString();
else if (match(attributeName, crossoriginAttr))
m_crossOriginMode = attributeValue.trim(isASCIIWhitespace<UChar>).toString();
else if (match(attributeName, nonceAttr))
m_nonceAttribute = attributeValue.toString();
else if (match(attributeName, asAttr))
m_asAttribute = attributeValue.toString();
else if (match(attributeName, typeAttr))
m_typeAttribute = attributeValue.toString();
else if (match(attributeName, referrerpolicyAttr))
m_referrerPolicy = parseReferrerPolicy(attributeValue, ReferrerPolicySource::ReferrerPolicyAttribute).value_or(ReferrerPolicy::EmptyString);
else if (match(attributeName, fetchpriorityAttr) && m_document.settings().fetchPriorityEnabled())
m_fetchPriorityHint = parseEnumerationFromString<RequestPriority>(attributeValue.toString()).value_or(RequestPriority::Auto);
break;
case TagId::Input:
if (match(attributeName, srcAttr))
setURLToLoad(attributeValue);
else if (match(attributeName, typeAttr))
m_inputIsImage = equalLettersIgnoringASCIICase(attributeValue, "image"_s);
break;
case TagId::Meta:
if (match(attributeName, contentAttr))
m_metaContent = attributeValue.toString();
else if (match(attributeName, nameAttr))
m_metaIsViewport = equalLettersIgnoringASCIICase(attributeValue, "viewport"_s);
else if (m_document.settings().disabledAdaptationsMetaTagEnabled() && match(attributeName, nameAttr))
m_metaIsDisabledAdaptations = equalLettersIgnoringASCIICase(attributeValue, "disabled-adaptations"_s);
break;
case TagId::Base:
case TagId::Style:
case TagId::Template:
case TagId::Picture:
case TagId::Unknown:
break;
}
}
static bool relAttributeIsStyleSheet(const LinkRelAttribute& parsedAttribute)
{
return parsedAttribute.isStyleSheet && !parsedAttribute.isAlternate && !parsedAttribute.iconType && !parsedAttribute.isDNSPrefetch;
}
void setURLToLoad(StringView value)
{
// We only respect the first src/href, per HTML5:
// http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#attribute-name-state
if (!m_urlToLoad.isEmpty())
return;
setURLToLoadAllowingReplacement(value);
}
void setURLToLoadAllowingReplacement(StringView value)
{
auto trimmedURL = value.trim(isASCIIWhitespace<UChar>);
if (trimmedURL.isEmpty())
return;
m_urlToLoad = trimmedURL.toString();
}
const String& charset() const
{
return m_charset;
}
std::optional<CachedResource::Type> resourceType() const
{
switch (m_tagId) {
case TagId::Script:
return CachedResource::Type::Script;
case TagId::Img:
case TagId::Input:
case TagId::Source:
ASSERT(m_tagId != TagId::Input || m_inputIsImage);
return CachedResource::Type::ImageResource;
case TagId::Link:
if (m_linkIsStyleSheet)
return CachedResource::Type::CSSStyleSheet;
if (m_linkIsPreload)
return LinkLoader::resourceTypeFromAsAttribute(m_asAttribute, m_document);
break;
case TagId::Meta:
case TagId::Unknown:
case TagId::Style:
case TagId::Base:
case TagId::Template:
case TagId::Picture:
break;
}
ASSERT_NOT_REACHED();
return CachedResource::Type::RawResource;
}
bool shouldPreload()
{
if (m_urlToLoad.isEmpty())
return false;
if (protocolIs(m_urlToLoad, "data"_s) || protocolIs(m_urlToLoad, "about"_s))
return false;
if (m_tagId == TagId::Link && !m_linkIsStyleSheet && !m_linkIsPreload)
return false;
if (m_tagId == TagId::Input && !m_inputIsImage)
return false;
return true;
}
Document& m_document;
TagId m_tagId;
String m_urlToLoad;
String m_srcSetAttribute;
String m_sizesAttribute;
bool m_mediaMatched { true };
bool m_typeMatched { true };
String m_charset;
String m_crossOriginMode;
bool m_linkIsStyleSheet;
bool m_linkIsPreload;
String m_mediaAttribute;
String m_nonceAttribute;
String m_metaContent;
String m_asAttribute;
String m_typeAttribute;
String m_languageAttribute;
String m_lazyloadAttribute;
bool m_metaIsViewport;
bool m_metaIsDisabledAdaptations;
bool m_inputIsImage;
bool m_scriptIsNomodule { false };
bool m_scriptIsAsync { false };
float m_deviceScaleFactor;
ReferrerPolicy m_referrerPolicy { ReferrerPolicy::EmptyString };
RequestPriority m_fetchPriorityHint { RequestPriority::Auto };
};
TokenPreloadScanner::TokenPreloadScanner(const URL& documentURL, float deviceScaleFactor)
: m_documentURL(documentURL)
, m_deviceScaleFactor(deviceScaleFactor)
{
}
void TokenPreloadScanner::scan(const HTMLToken& token, Vector<std::unique_ptr<PreloadRequest>>& requests, Document& document)
{
switch (token.type()) {
case HTMLToken::Type::Character:
if (!m_inStyle)
return;
m_cssScanner.scan(token.characters(), requests);
return;
case HTMLToken::Type::EndTag: {
TagId tagId = tagIdFor(token.name());
if (tagId == TagId::Template) {
if (m_templateCount)
--m_templateCount;
return;
}
if (tagId == TagId::Style) {
if (m_inStyle)
m_cssScanner.reset();
m_inStyle = false;
} else if (tagId == TagId::Picture && !m_pictureSourceState.isEmpty())
m_pictureSourceState.removeLast();
return;
}
case HTMLToken::Type::StartTag: {
TagId tagId = tagIdFor(token.name());
if (tagId == TagId::Template) {
++m_templateCount;
return;
}
if (m_templateCount)
return;
if (tagId == TagId::Style) {
m_inStyle = true;
return;
}
if (tagId == TagId::Base) {
// The first <base> element is the one that wins.
if (!m_predictedBaseElementURL.isEmpty())
return;
updatePredictedBaseURL(token, document.settings().shouldRestrictBaseURLSchemes());
return;
}
if (tagId == TagId::Picture) {
m_pictureSourceState.append(false);
return;
}
StartTagScanner scanner(document, tagId, m_deviceScaleFactor);
scanner.processAttributes(token.attributes(), m_pictureSourceState);
if (auto request = scanner.createPreloadRequest(m_predictedBaseElementURL))
requests.append(WTFMove(request));
return;
}
default:
return;
}
}
void TokenPreloadScanner::updatePredictedBaseURL(const HTMLToken& token, bool shouldRestrictBaseURLSchemes)
{
ASSERT(m_predictedBaseElementURL.isEmpty());
static constexpr UChar hrefAsUChar[] = { 'h', 'r', 'e', 'f' };
auto* hrefAttribute = findAttribute(token.attributes(), hrefAsUChar);
if (!hrefAttribute)
return;
URL temp { m_documentURL, StringImpl::create8BitIfPossible(hrefAttribute->value) };
if (!shouldRestrictBaseURLSchemes || SecurityPolicy::isBaseURLSchemeAllowed(temp))
m_predictedBaseElementURL = WTFMove(temp).isolatedCopy();
}
HTMLPreloadScanner::HTMLPreloadScanner(const HTMLParserOptions& options, const URL& documentURL, float deviceScaleFactor)
: m_scanner(documentURL, deviceScaleFactor)
, m_tokenizer(options)
{
}
void HTMLPreloadScanner::appendToEnd(const SegmentedString& source)
{
m_source.append(source);
}
void HTMLPreloadScanner::scan(HTMLResourcePreloader& preloader, Document& document)
{
ASSERT(isMainThread()); // HTMLTokenizer::updateStateFor only works on the main thread.
const URL& startingBaseElementURL = document.baseElementURL();
// When we start scanning, our best prediction of the baseElementURL is the real one!
if (!startingBaseElementURL.isEmpty())
m_scanner.setPredictedBaseElementURL(startingBaseElementURL);
PreloadRequestStream requests;
while (auto token = m_tokenizer.nextToken(m_source)) {
if (token->type() == HTMLToken::Type::StartTag)
m_tokenizer.updateStateFor(AtomString::lookUp(token->name().data(), token->name().size()));
m_scanner.scan(*token, requests, document);
}
preloader.preload(WTFMove(requests));
}
bool testPreloadScannerViewportSupport(Document* document)
{
ASSERT(document);
HTMLParserOptions options(*document);
HTMLPreloadScanner scanner(options, document->url());
HTMLResourcePreloader preloader(*document);
scanner.appendToEnd(String("<meta name=viewport content='width=400'>"_s));
scanner.scan(preloader, *document);
return (document->viewportArguments().width == 400);
}
}
|