WebCore/html/HTMLParserScheduler.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 
       
    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 
       
    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 
       
    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 {
       
    69 }
       
    70 
       
    71 HTMLParserScheduler::~HTMLParserScheduler()
       
    72 {
       
    73     m_continueNextChunkTimer.stop();
       
    74 }
       
    75 
       
    76 // FIXME: This belongs on Document.
       
    77 static bool isLayoutTimerActive(Document* doc)
       
    78 {
       
    79     ASSERT(doc);
       
    80     return doc->view() && doc->view()->layoutPending() && !doc->minimumLayoutDelay();
       
    81 }
       
    82 
       
    83 void HTMLParserScheduler::continueNextChunkTimerFired(Timer<HTMLParserScheduler>* timer)
       
    84 {
       
    85     ASSERT_UNUSED(timer, timer == &m_continueNextChunkTimer);
       
    86     // FIXME: The timer class should handle timer priorities instead of this code.
       
    87     // If a layout is scheduled, wait again to let the layout timer run first.
       
    88     if (isLayoutTimerActive(m_parser->document())) {
       
    89         m_continueNextChunkTimer.startOneShot(0);
       
    90         return;
       
    91     }
       
    92     m_parser->resumeParsingAfterYield();
       
    93 }
       
    94 
       
    95 }