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
|
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
* Copyright (C) 2009 Torch Mobile, Inc. http://www.torchmobile.com/
* Copyright (C) 2010 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 "HTMLNames.h"
#include "HTMLParserIdioms.h"
#include "HTMLParserOptions.h"
#include "HTMLTokenizer.h"
#include "InputTypeNames.h"
#include "LinkRelAttribute.h"
#include "MediaList.h"
#include "MediaQueryEvaluator.h"
#include <wtf/MainThread.h>
namespace WebCore {
using namespace HTMLNames;
TokenPreloadScanner::TagId TokenPreloadScanner::tagIdFor(const HTMLToken::DataVector& data)
{
AtomicString tagName(data);
if (tagName == imgTag)
return ImgTagId;
if (tagName == inputTag)
return InputTagId;
if (tagName == linkTag)
return LinkTagId;
if (tagName == scriptTag)
return ScriptTagId;
if (tagName == styleTag)
return StyleTagId;
if (tagName == baseTag)
return BaseTagId;
if (tagName == templateTag)
return TemplateTagId;
return UnknownTagId;
}
#if ENABLE(THREADED_HTML_PARSER)
TokenPreloadScanner::TagId TokenPreloadScanner::tagIdFor(const HTMLIdentifier& tagName)
{
if (threadSafeHTMLNamesMatch(tagName, imgTag))
return ImgTagId;
if (threadSafeHTMLNamesMatch(tagName, inputTag))
return InputTagId;
if (threadSafeHTMLNamesMatch(tagName, linkTag))
return LinkTagId;
if (threadSafeHTMLNamesMatch(tagName, scriptTag))
return ScriptTagId;
if (threadSafeHTMLNamesMatch(tagName, styleTag))
return StyleTagId;
if (threadSafeHTMLNamesMatch(tagName, baseTag))
return BaseTagId;
if (threadSafeHTMLNamesMatch(tagName, templateTag))
return TemplateTagId;
return UnknownTagId;
}
#endif
String TokenPreloadScanner::initiatorFor(TagId tagId)
{
switch (tagId) {
case ImgTagId:
return "img";
case InputTagId:
return "input";
case LinkTagId:
return "link";
case ScriptTagId:
return "script";
case UnknownTagId:
case StyleTagId:
case BaseTagId:
case TemplateTagId:
ASSERT_NOT_REACHED();
return "unknown";
}
ASSERT_NOT_REACHED();
return "unknown";
}
class TokenPreloadScanner::StartTagScanner {
public:
explicit StartTagScanner(TagId tagId)
: m_tagId(tagId)
, m_linkIsStyleSheet(false)
, m_linkMediaAttributeIsScreen(true)
, m_inputIsImage(false)
{
}
void processAttributes(const HTMLToken::AttributeList& attributes)
{
ASSERT(isMainThread());
if (m_tagId >= UnknownTagId)
return;
for (HTMLToken::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter) {
AtomicString attributeName(iter->name);
String attributeValue = StringImpl::create8BitIfPossible(iter->value);
processAttribute(attributeName, attributeValue);
}
}
#if ENABLE(THREADED_HTML_PARSER)
void processAttributes(const Vector<CompactHTMLToken::Attribute>& attributes)
{
if (m_tagId >= UnknownTagId)
return;
for (Vector<CompactHTMLToken::Attribute>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
processAttribute(iter->name, iter->value);
}
#endif
PassOwnPtr<PreloadRequest> createPreloadRequest(const KURL& predictedBaseURL)
{
if (!shouldPreload())
return nullptr;
OwnPtr<PreloadRequest> request = PreloadRequest::create(initiatorFor(m_tagId), m_urlToLoad, predictedBaseURL, resourceType());
request->setCrossOriginModeAllowsCookies(crossOriginModeAllowsCookies());
request->setCharset(charset());
return request.release();
}
static bool match(const AtomicString& name, const QualifiedName& qName)
{
ASSERT(isMainThread());
return qName.localName() == name;
}
#if ENABLE(THREADED_HTML_PARSER)
static bool match(const HTMLIdentifier& name, const QualifiedName& qName)
{
return threadSafeHTMLNamesMatch(name, qName);
}
#endif
private:
template<typename NameType>
void processAttribute(const NameType& attributeName, const String& attributeValue)
{
if (match(attributeName, charsetAttr))
m_charset = attributeValue;
if (m_tagId == ScriptTagId || m_tagId == ImgTagId) {
if (match(attributeName, srcAttr))
setUrlToLoad(attributeValue);
else if (match(attributeName, crossoriginAttr) && !attributeValue.isNull())
m_crossOriginMode = stripLeadingAndTrailingHTMLSpaces(attributeValue);
} else if (m_tagId == LinkTagId) {
if (match(attributeName, hrefAttr))
setUrlToLoad(attributeValue);
else if (match(attributeName, relAttr))
m_linkIsStyleSheet = relAttributeIsStyleSheet(attributeValue);
else if (match(attributeName, mediaAttr))
m_linkMediaAttributeIsScreen = linkMediaAttributeIsScreen(attributeValue);
} else if (m_tagId == InputTagId) {
if (match(attributeName, srcAttr))
setUrlToLoad(attributeValue);
else if (match(attributeName, typeAttr))
m_inputIsImage = equalIgnoringCase(attributeValue, InputTypeNames::image());
}
}
static bool relAttributeIsStyleSheet(const String& attributeValue)
{
LinkRelAttribute rel(attributeValue);
return rel.m_isStyleSheet && !rel.m_isAlternate && rel.m_iconType == InvalidIcon && !rel.m_isDNSPrefetch;
}
static bool linkMediaAttributeIsScreen(const String& attributeValue)
{
if (attributeValue.isEmpty())
return true;
RefPtr<MediaQuerySet> mediaQueries = MediaQuerySet::createAllowingDescriptionSyntax(attributeValue);
// Only preload screen media stylesheets. Used this way, the evaluator evaluates to true for any
// rules containing complex queries (full evaluation is possible but it requires a frame and a style selector which
// may be problematic here).
MediaQueryEvaluator mediaQueryEvaluator("screen");
return mediaQueryEvaluator.eval(mediaQueries.get());
}
void setUrlToLoad(const String& attributeValue)
{
// 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;
m_urlToLoad = stripLeadingAndTrailingHTMLSpaces(attributeValue);
}
const String& charset() const
{
// FIXME: Its not clear that this if is needed, the loader probably ignores charset for image requests anyway.
if (m_tagId == ImgTagId)
return emptyString();
return m_charset;
}
CachedResource::Type resourceType() const
{
if (m_tagId == ScriptTagId)
return CachedResource::Script;
if (m_tagId == ImgTagId || (m_tagId == InputTagId && m_inputIsImage))
return CachedResource::ImageResource;
if (m_tagId == LinkTagId && m_linkIsStyleSheet && m_linkMediaAttributeIsScreen)
return CachedResource::CSSStyleSheet;
ASSERT_NOT_REACHED();
return CachedResource::RawResource;
}
bool shouldPreload()
{
if (m_urlToLoad.isEmpty())
return false;
if (m_tagId == LinkTagId && (!m_linkIsStyleSheet || !m_linkMediaAttributeIsScreen))
return false;
if (m_tagId == InputTagId && !m_inputIsImage)
return false;
return true;
}
bool crossOriginModeAllowsCookies()
{
return m_crossOriginMode.isNull() || equalIgnoringCase(m_crossOriginMode, "use-credentials");
}
TagId m_tagId;
String m_urlToLoad;
String m_charset;
String m_crossOriginMode;
bool m_linkIsStyleSheet;
bool m_linkMediaAttributeIsScreen;
bool m_inputIsImage;
};
TokenPreloadScanner::TokenPreloadScanner(const KURL& documentURL)
: m_documentURL(documentURL)
, m_inStyle(false)
#if ENABLE(TEMPLATE_ELEMENT)
, m_templateCount(0)
#endif
{
}
TokenPreloadScanner::~TokenPreloadScanner()
{
}
TokenPreloadScannerCheckpoint TokenPreloadScanner::createCheckpoint()
{
TokenPreloadScannerCheckpoint checkpoint = m_checkpoints.size();
m_checkpoints.append(Checkpoint(m_predictedBaseElementURL, m_inStyle
#if ENABLE(TEMPLATE_ELEMENT)
, m_templateCount
#endif
));
return checkpoint;
}
void TokenPreloadScanner::rewindTo(TokenPreloadScannerCheckpoint checkpointIndex)
{
ASSERT(checkpointIndex < m_checkpoints.size()); // If this ASSERT fires, checkpointIndex is invalid.
const Checkpoint& checkpoint = m_checkpoints[checkpointIndex];
m_predictedBaseElementURL = checkpoint.predictedBaseElementURL;
m_inStyle = checkpoint.inStyle;
#if ENABLE(TEMPLATE_ELEMENT)
m_templateCount = checkpoint.templateCount;
#endif
m_cssScanner.reset();
m_checkpoints.clear();
}
void TokenPreloadScanner::scan(const HTMLToken& token, Vector<OwnPtr<PreloadRequest> >& requests)
{
scanCommon(token, requests);
}
#if ENABLE(THREADED_HTML_PARSER)
void TokenPreloadScanner::scan(const CompactHTMLToken& token, Vector<OwnPtr<PreloadRequest> >& requests)
{
scanCommon(token, requests);
}
#endif
template<typename Token>
void TokenPreloadScanner::scanCommon(const Token& token, Vector<OwnPtr<PreloadRequest> >& requests)
{
switch (token.type()) {
case HTMLToken::Character: {
if (!m_inStyle)
return;
m_cssScanner.scan(token.data(), requests);
return;
}
case HTMLToken::EndTag: {
TagId tagId = tagIdFor(token.data());
#if ENABLE(TEMPLATE_ELEMENT)
if (tagId == TemplateTagId) {
if (m_templateCount)
--m_templateCount;
return;
}
#endif
if (tagId == StyleTagId) {
if (m_inStyle)
m_cssScanner.reset();
m_inStyle = false;
}
return;
}
case HTMLToken::StartTag: {
#if ENABLE(TEMPLATE_ELEMENT)
if (m_templateCount)
return;
#endif
TagId tagId = tagIdFor(token.data());
#if ENABLE(TEMPLATE_ELEMENT)
if (tagId == TemplateTagId) {
++m_templateCount;
return;
}
#endif
if (tagId == StyleTagId) {
m_inStyle = true;
return;
}
if (tagId == BaseTagId) {
// The first <base> element is the one that wins.
if (!m_predictedBaseElementURL.isEmpty())
return;
updatePredictedBaseURL(token);
return;
}
StartTagScanner scanner(tagId);
scanner.processAttributes(token.attributes());
OwnPtr<PreloadRequest> request = scanner.createPreloadRequest(m_predictedBaseElementURL);
if (request)
requests.append(request.release());
return;
}
default: {
return;
}
}
}
template<typename Token>
void TokenPreloadScanner::updatePredictedBaseURL(const Token& token)
{
ASSERT(m_predictedBaseElementURL.isEmpty());
if (const typename Token::Attribute* hrefAttribute = token.getAttributeItem(hrefAttr))
m_predictedBaseElementURL = KURL(m_documentURL, stripLeadingAndTrailingHTMLSpaces(hrefAttribute->value)).copy();
}
HTMLPreloadScanner::HTMLPreloadScanner(const HTMLParserOptions& options, const KURL& documentURL)
: m_scanner(documentURL)
, m_tokenizer(HTMLTokenizer::create(options))
{
}
HTMLPreloadScanner::~HTMLPreloadScanner()
{
}
void HTMLPreloadScanner::appendToEnd(const SegmentedString& source)
{
m_source.append(source);
}
void HTMLPreloadScanner::scan(HTMLResourcePreloader* preloader, const KURL& startingBaseElementURL)
{
ASSERT(isMainThread()); // HTMLTokenizer::updateStateFor only works on the main thread.
// When we start scanning, our best prediction of the baseElementURL is the real one!
if (!startingBaseElementURL.isEmpty())
m_scanner.setPredictedBaseElementURL(startingBaseElementURL);
PreloadRequestStream requests;
while (m_tokenizer->nextToken(m_source, m_token)) {
if (m_token.type() == HTMLToken::StartTag)
m_tokenizer->updateStateFor(AtomicString(m_token.name()));
m_scanner.scan(m_token, requests);
m_token.clear();
}
preloader->takeAndPreload(requests);
}
}
|