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 HTMLDocumentParser_h 27#define HTMLDocumentParser_h 28 29#include "CachedResourceClient.h" 30#include "FragmentScriptingPermission.h" 31#include "HTMLInputStream.h" 32#include "HTMLScriptRunnerHost.h" 33#include "HTMLSourceTracker.h" 34#include "HTMLToken.h" 35#include "ScriptableDocumentParser.h" 36#include "SegmentedString.h" 37#include "Timer.h" 38#include "XSSFilter.h" 39#include <wtf/OwnPtr.h> 40 41namespace WebCore { 42 43class Document; 44class DocumentFragment; 45class HTMLDocument; 46class HTMLParserScheduler; 47class HTMLTokenizer; 48class HTMLScriptRunner; 49class HTMLTreeBuilder; 50class HTMLPreloadScanner; 51class ScriptController; 52class ScriptSourceCode; 53 54class PumpSession; 55 56class HTMLDocumentParser : public ScriptableDocumentParser, HTMLScriptRunnerHost, CachedResourceClient { 57 WTF_MAKE_FAST_ALLOCATED; 58public: 59 static PassRefPtr<HTMLDocumentParser> create(HTMLDocument* document, bool reportErrors) 60 { 61 return adoptRef(new HTMLDocumentParser(document, reportErrors)); 62 } 63 static PassRefPtr<HTMLDocumentParser> create(DocumentFragment* fragment, Element* contextElement, FragmentScriptingPermission permission) 64 { 65 return adoptRef(new HTMLDocumentParser(fragment, contextElement, permission)); 66 } 67 68 virtual ~HTMLDocumentParser(); 69 70 // Exposed for HTMLParserScheduler 71 void resumeParsingAfterYield(); 72 73 static void parseDocumentFragment(const String&, DocumentFragment*, Element* contextElement, FragmentScriptingPermission = FragmentScriptingAllowed); 74 75 static bool usePreHTML5ParserQuirks(Document*); 76 77 HTMLTokenizer* tokenizer() const { return m_tokenizer.get(); } 78 String sourceForToken(const HTMLToken&); 79 80 virtual TextPosition0 textPosition() const; 81 virtual int lineNumber() const; 82 83 virtual void suspendScheduledTasks(); 84 virtual void resumeScheduledTasks(); 85 86protected: 87 virtual void insert(const SegmentedString&); 88 virtual void append(const SegmentedString&); 89 virtual void finish(); 90 91 HTMLDocumentParser(HTMLDocument*, bool reportErrors); 92 HTMLDocumentParser(DocumentFragment*, Element* contextElement, FragmentScriptingPermission); 93 94 HTMLTreeBuilder* treeBuilder() const { return m_treeBuilder.get(); } 95 96private: 97 // DocumentParser 98 virtual void detach(); 99 virtual bool hasInsertionPoint(); 100 virtual bool finishWasCalled(); 101 virtual bool processingData() const; 102 virtual void prepareToStopParsing(); 103 virtual void stopParsing(); 104 virtual bool isWaitingForScripts() const; 105 virtual bool isExecutingScript() const; 106 virtual void executeScriptsWaitingForStylesheets(); 107 108 // HTMLScriptRunnerHost 109 virtual void watchForLoad(CachedResource*); 110 virtual void stopWatchingForLoad(CachedResource*); 111 virtual HTMLInputStream& inputStream() { return m_input; } 112 virtual bool hasPreloadScanner() const { return m_preloadScanner.get(); } 113 virtual void appendCurrentInputStreamToPreloadScannerAndScan(); 114 115 // CachedResourceClient 116 virtual void notifyFinished(CachedResource*); 117 118 enum SynchronousMode { 119 AllowYield, 120 ForceSynchronous, 121 }; 122 bool canTakeNextToken(SynchronousMode, PumpSession&); 123 void pumpTokenizer(SynchronousMode); 124 void pumpTokenizerIfPossible(SynchronousMode); 125 126 bool runScriptsForPausedTreeBuilder(); 127 void resumeParsingAfterScriptExecution(); 128 129 void begin(); 130 void attemptToEnd(); 131 void endIfDelayed(); 132 void attemptToRunDeferredScriptsAndEnd(); 133 void end(); 134 135 bool isParsingFragment() const; 136 bool isScheduledForResume() const; 137 bool inScriptExecution() const; 138 bool inPumpSession() const { return m_pumpSessionNestingLevel > 0; } 139 bool shouldDelayEnd() const { return inPumpSession() || isWaitingForScripts() || inScriptExecution() || isScheduledForResume(); } 140 141 ScriptController* script() const; 142 143 HTMLInputStream m_input; 144 145 // We hold m_token here because it might be partially complete. 146 HTMLToken m_token; 147 148 OwnPtr<HTMLTokenizer> m_tokenizer; 149 OwnPtr<HTMLScriptRunner> m_scriptRunner; 150 OwnPtr<HTMLTreeBuilder> m_treeBuilder; 151 OwnPtr<HTMLPreloadScanner> m_preloadScanner; 152 OwnPtr<HTMLParserScheduler> m_parserScheduler; 153 HTMLSourceTracker m_sourceTracker; 154 XSSFilter m_xssFilter; 155 156 bool m_endWasDelayed; 157 unsigned m_pumpSessionNestingLevel; 158}; 159 160} 161 162#endif 163