• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "FrameView.h" // Only for isLayoutTimerActive
30 #include "HTMLDocumentParser.h"
31 #include "Document.h"
32 
33 // defaultParserChunkSize is used to define how many tokens the parser will
34 // process before checking against parserTimeLimit and possibly yielding.
35 // This is a performance optimization to prevent checking after every token.
36 static const int defaultParserChunkSize = 4096;
37 
38 // defaultParserTimeLimit is the seconds the parser will run in one write() call
39 // before yielding.  Inline <script> execution can cause it to excede the limit.
40 // FIXME: We would like this value to be 0.2.
41 static const double defaultParserTimeLimit = 0.500;
42 
43 namespace WebCore {
44 
parserTimeLimit(Page * page)45 static double parserTimeLimit(Page* page)
46 {
47     // We're using the poorly named customHTMLTokenizerTimeDelay setting.
48     if (page && page->hasCustomHTMLTokenizerTimeDelay())
49         return page->customHTMLTokenizerTimeDelay();
50     return defaultParserTimeLimit;
51 }
52 
parserChunkSize(Page * page)53 static int parserChunkSize(Page* page)
54 {
55     // FIXME: We may need to divide the value from customHTMLTokenizerChunkSize
56     // by some constant to translate from the "character" based behavior of the
57     // old LegacyHTMLDocumentParser to the token-based behavior of this parser.
58     if (page && page->hasCustomHTMLTokenizerChunkSize())
59         return page->customHTMLTokenizerChunkSize();
60     return defaultParserChunkSize;
61 }
62 
HTMLParserScheduler(HTMLDocumentParser * parser)63 HTMLParserScheduler::HTMLParserScheduler(HTMLDocumentParser* parser)
64     : m_parser(parser)
65     , m_parserTimeLimit(parserTimeLimit(m_parser->document()->page()))
66     , m_parserChunkSize(parserChunkSize(m_parser->document()->page()))
67     , m_continueNextChunkTimer(this, &HTMLParserScheduler::continueNextChunkTimerFired)
68     , m_isSuspendedWithActiveTimer(false)
69 {
70 }
71 
~HTMLParserScheduler()72 HTMLParserScheduler::~HTMLParserScheduler()
73 {
74     m_continueNextChunkTimer.stop();
75 }
76 
continueNextChunkTimerFired(Timer<HTMLParserScheduler> * timer)77 void HTMLParserScheduler::continueNextChunkTimerFired(Timer<HTMLParserScheduler>* timer)
78 {
79     ASSERT_UNUSED(timer, timer == &m_continueNextChunkTimer);
80     // FIXME: The timer class should handle timer priorities instead of this code.
81     // If a layout is scheduled, wait again to let the layout timer run first.
82     if (m_parser->document()->isLayoutTimerActive()) {
83         m_continueNextChunkTimer.startOneShot(0);
84         return;
85     }
86     m_parser->resumeParsingAfterYield();
87 }
88 
checkForYieldBeforeScript(PumpSession & session)89 void HTMLParserScheduler::checkForYieldBeforeScript(PumpSession& session)
90 {
91     // If we've never painted before and a layout is pending, yield prior to running
92     // scripts to give the page a chance to paint earlier.
93     Document* document = m_parser->document();
94     bool needsFirstPaint = document->view() && !document->view()->hasEverPainted();
95     if (needsFirstPaint && document->isLayoutTimerActive())
96         session.needsYield = true;
97 }
98 
scheduleForResume()99 void HTMLParserScheduler::scheduleForResume()
100 {
101     m_continueNextChunkTimer.startOneShot(0);
102 }
103 
104 
suspend()105 void HTMLParserScheduler::suspend()
106 {
107     ASSERT(!m_isSuspendedWithActiveTimer);
108     if (!m_continueNextChunkTimer.isActive())
109         return;
110     m_isSuspendedWithActiveTimer = true;
111     m_continueNextChunkTimer.stop();
112 }
113 
resume()114 void HTMLParserScheduler::resume()
115 {
116     ASSERT(!m_continueNextChunkTimer.isActive());
117     if (!m_isSuspendedWithActiveTimer)
118         return;
119     m_isSuspendedWithActiveTimer = false;
120     m_continueNextChunkTimer.startOneShot(0);
121 }
122 
123 }
124