HTMLParserScheduler.h [plain text]
#ifndef HTMLParserScheduler_h
#define HTMLParserScheduler_h
#include "NestingLevelIncrementer.h"
#include "Timer.h"
#include <wtf/CurrentTime.h>
#include <wtf/RefPtr.h>
#if PLATFORM(IOS)
#include "WebCoreThread.h"
#endif
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:
explicit HTMLParserScheduler(HTMLDocumentParser&);
~HTMLParserScheduler();
void checkForYieldBeforeToken(PumpSession& session)
{
#if PLATFORM(IOS)
if (WebThreadShouldYield())
session.needsYield = true;
#endif
if (session.processedTokens > m_parserChunkSize || session.didSeeScript) {
if (!session.startTime)
session.startTime = monotonicallyIncreasingTime();
session.processedTokens = 0;
session.didSeeScript = false;
double elapsedTime = monotonicallyIncreasingTime() - session.startTime;
if (elapsedTime > m_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:
void continueNextChunkTimerFired(Timer<HTMLParserScheduler>&);
HTMLDocumentParser& m_parser;
double m_parserTimeLimit;
int m_parserChunkSize;
Timer<HTMLParserScheduler> m_continueNextChunkTimer;
bool m_isSuspendedWithActiveTimer;
#if !ASSERT_DISABLED
bool m_suspended;
#endif
};
}
#endif