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#ifndef HTMLParserScheduler_h
27#define HTMLParserScheduler_h
28
29#include <limits.h>
30
31#include "NestingLevelIncrementer.h"
32#include "Timer.h"
33#include <wtf/CurrentTime.h>
34#include <wtf/PassOwnPtr.h>
35
36namespace WebCore {
37
38class HTMLDocumentParser;
39
40class PumpSession : public NestingLevelIncrementer {
41public:
42    PumpSession(unsigned& nestingLevel)
43        : NestingLevelIncrementer(nestingLevel)
44        // Setting processedTokens to INT_MAX causes us to check for yields
45        // after any token during any parse where yielding is allowed.
46        // At that time we'll initialize startTime.
47        , processedTokens(INT_MAX)
48        , startTime(0)
49        , needsYield(false)
50    {
51    }
52
53    int processedTokens;
54    double startTime;
55    bool needsYield;
56};
57
58class HTMLParserScheduler {
59    WTF_MAKE_NONCOPYABLE(HTMLParserScheduler); WTF_MAKE_FAST_ALLOCATED;
60public:
61    static PassOwnPtr<HTMLParserScheduler> create(HTMLDocumentParser* parser)
62    {
63        return adoptPtr(new HTMLParserScheduler(parser));
64    }
65    ~HTMLParserScheduler();
66
67    // Inline as this is called after every token in the parser.
68    void checkForYieldBeforeToken(PumpSession& session)
69    {
70        if (session.processedTokens > m_parserChunkSize) {
71            // currentTime() can be expensive.  By delaying, we avoided calling
72            // currentTime() when constructing non-yielding PumpSessions.
73            if (!session.startTime)
74                session.startTime = currentTime();
75
76            session.processedTokens = 0;
77            double elapsedTime = currentTime() - session.startTime;
78            if (elapsedTime > m_parserTimeLimit)
79                session.needsYield = true;
80        }
81        ++session.processedTokens;
82    }
83    void checkForYieldBeforeScript(PumpSession&);
84
85    void scheduleForResume();
86    bool isScheduledForResume() const { return m_isSuspendedWithActiveTimer || m_continueNextChunkTimer.isActive(); }
87
88    void suspend();
89    void resume();
90
91private:
92    HTMLParserScheduler(HTMLDocumentParser*);
93
94    void continueNextChunkTimerFired(Timer<HTMLParserScheduler>*);
95
96    HTMLDocumentParser* m_parser;
97
98    double m_parserTimeLimit;
99    int m_parserChunkSize;
100    Timer<HTMLParserScheduler> m_continueNextChunkTimer;
101    bool m_isSuspendedWithActiveTimer;
102};
103
104}
105
106#endif
107