#ifndef HTMLParserScheduler_h
#define HTMLParserScheduler_h
#include "core/html/parser/NestingLevelIncrementer.h"
#include "platform/Timer.h"
#include "wtf/CurrentTime.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/RefPtr.h"
namespace WebCore {
class Document;
class HTMLDocumentParser;
class ActiveParserSession {
public:
explicit ActiveParserSession(Document*);
~ActiveParserSession();
private:
RefPtr<Document> m_document;
};
class PumpSession : public NestingLevelIncrementer, public ActiveParserSession {
public:
PumpSession(unsigned& nestingLevel, Document*);
~PumpSession();
int processedTokens;
double startTime;
bool needsYield;
bool didSeeScript;
};
class HTMLParserScheduler {
WTF_MAKE_NONCOPYABLE(HTMLParserScheduler); WTF_MAKE_FAST_ALLOCATED;
public:
static PassOwnPtr<HTMLParserScheduler> create(HTMLDocumentParser* parser)
{
return adoptPtr(new HTMLParserScheduler(parser));
}
~HTMLParserScheduler();
void checkForYieldBeforeToken(PumpSession& session)
{
if (session.processedTokens > parserChunkSize || session.didSeeScript) {
if (!session.startTime)
session.startTime = currentTime();
session.processedTokens = 0;
session.didSeeScript = false;
double elapsedTime = currentTime() - session.startTime;
if (elapsedTime > parserTimeLimit)
session.needsYield = true;
}
++session.processedTokens;
}
void checkForYieldBeforeScript(PumpSession&);
void scheduleForResume();
bool isScheduledForResume() const { return m_isSuspendedWithActiveTimer || m_continueNextChunkTimer.isActive(); }
void suspend();
void resume();
private:
static const double parserTimeLimit;
static const int parserChunkSize;
HTMLParserScheduler(HTMLDocumentParser*);
void continueNextChunkTimerFired(Timer<HTMLParserScheduler>*);
HTMLDocumentParser* m_parser;
Timer<HTMLParserScheduler> m_continueNextChunkTimer;
bool m_isSuspendedWithActiveTimer;
};
}
#endif