OSDN Git Service

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