1/*
2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "HTMLParserScheduler.h"
28
29#include "Document.h"
30#include "FrameView.h"
31#include "HTMLDocumentParser.h"
32#include "Page.h"
33
34// defaultParserChunkSize is used to define how many tokens the parser will
35// process before checking against parserTimeLimit and possibly yielding.
36// This is a performance optimization to prevent checking after every token.
37static const int defaultParserChunkSize = 4096;
38
39// defaultParserTimeLimit is the seconds the parser will run in one write() call
40// before yielding. Inline <script> execution can cause it to exceed the limit.
41// FIXME: We would like this value to be 0.2.
42static const double defaultParserTimeLimit = 0.500;
43
44namespace WebCore {
45
46static double parserTimeLimit(Page* page)
47{
48    // We're using the poorly named customHTMLTokenizerTimeDelay setting.
49    if (page && page->hasCustomHTMLTokenizerTimeDelay())
50        return page->customHTMLTokenizerTimeDelay();
51    return defaultParserTimeLimit;
52}
53
54static int parserChunkSize(Page* page)
55{
56    // FIXME: We may need to divide the value from customHTMLTokenizerChunkSize
57    // by some constant to translate from the "character" based behavior of the
58    // old LegacyHTMLDocumentParser to the token-based behavior of this parser.
59    if (page && page->hasCustomHTMLTokenizerChunkSize())
60        return page->customHTMLTokenizerChunkSize();
61    return defaultParserChunkSize;
62}
63
64ActiveParserSession::ActiveParserSession(Document* document)
65    : m_document(document)
66{
67    if (!m_document)
68        return;
69    m_document->incrementActiveParserCount();
70}
71
72ActiveParserSession::~ActiveParserSession()
73{
74    if (!m_document)
75        return;
76    m_document->decrementActiveParserCount();
77}
78
79PumpSession::PumpSession(unsigned& nestingLevel, Document* document)
80    : NestingLevelIncrementer(nestingLevel)
81    , ActiveParserSession(document)
82    // Setting processedTokens to INT_MAX causes us to check for yields
83    // after any token during any parse where yielding is allowed.
84    // At that time we'll initialize startTime.
85    , processedTokens(INT_MAX)
86    , startTime(0)
87    , needsYield(false)
88    , didSeeScript(false)
89{
90}
91
92PumpSession::~PumpSession()
93{
94}
95
96HTMLParserScheduler::HTMLParserScheduler(HTMLDocumentParser* parser)
97    : m_parser(parser)
98    , m_parserTimeLimit(parserTimeLimit(m_parser->document()->page()))
99    , m_parserChunkSize(parserChunkSize(m_parser->document()->page()))
100    , m_continueNextChunkTimer(this, &HTMLParserScheduler::continueNextChunkTimerFired)
101    , m_isSuspendedWithActiveTimer(false)
102{
103}
104
105HTMLParserScheduler::~HTMLParserScheduler()
106{
107    m_continueNextChunkTimer.stop();
108}
109
110void HTMLParserScheduler::continueNextChunkTimerFired(Timer<HTMLParserScheduler>* timer)
111{
112    ASSERT_UNUSED(timer, timer == &m_continueNextChunkTimer);
113    // FIXME: The timer class should handle timer priorities instead of this code.
114    // If a layout is scheduled, wait again to let the layout timer run first.
115    if (m_parser->document()->isLayoutTimerActive()) {
116        m_continueNextChunkTimer.startOneShot(0);
117        return;
118    }
119    m_parser->resumeParsingAfterYield();
120}
121
122void HTMLParserScheduler::checkForYieldBeforeScript(PumpSession& session)
123{
124    // If we've never painted before and a layout is pending, yield prior to running
125    // scripts to give the page a chance to paint earlier.
126    Document* document = m_parser->document();
127    bool needsFirstPaint = document->view() && !document->view()->hasEverPainted();
128    if (needsFirstPaint && document->isLayoutTimerActive())
129        session.needsYield = true;
130    session.didSeeScript = true;
131}
132
133void HTMLParserScheduler::scheduleForResume()
134{
135    m_continueNextChunkTimer.startOneShot(0);
136}
137
138
139void HTMLParserScheduler::suspend()
140{
141    ASSERT(!m_isSuspendedWithActiveTimer);
142    if (!m_continueNextChunkTimer.isActive())
143        return;
144    m_isSuspendedWithActiveTimer = true;
145    m_continueNextChunkTimer.stop();
146}
147
148void HTMLParserScheduler::resume()
149{
150    ASSERT(!m_continueNextChunkTimer.isActive());
151    if (!m_isSuspendedWithActiveTimer)
152        return;
153    m_isSuspendedWithActiveTimer = false;
154    m_continueNextChunkTimer.startOneShot(0);
155}
156
157}
158