1/*
2 * Copyright (C) 2000 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2005, 2006 Apple Computer, Inc.
4 * Copyright (C) 2007 Samuel Weinig (sam@webkit.org)
5 * Copyright (C) 2010 Google, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifndef DocumentParser_h
25#define DocumentParser_h
26
27#include <wtf/RefCounted.h>
28
29namespace WebCore {
30
31class Document;
32class DocumentWriter;
33class SegmentedString;
34class ScriptableDocumentParser;
35
36class DocumentParser : public RefCounted<DocumentParser> {
37public:
38    virtual ~DocumentParser();
39
40    virtual ScriptableDocumentParser* asScriptableDocumentParser() { return 0; }
41
42    // http://www.whatwg.org/specs/web-apps/current-work/#insertion-point
43    virtual bool hasInsertionPoint() { return true; }
44
45    // insert is used by document.write
46    virtual void insert(const SegmentedString&) = 0;
47
48    // appendBytes is used by DocumentWriter (the loader)
49    virtual void appendBytes(DocumentWriter*, const char* bytes, int length, bool flush) = 0;
50
51    // FIXME: append() should be private, but DocumentWriter::replaceDocument
52    // uses it for now.
53    virtual void append(const SegmentedString&) = 0;
54
55    virtual void finish() = 0;
56    virtual bool finishWasCalled() = 0;
57
58    // FIXME: processingData() is only used by DocumentLoader::isLoadingInAPISense
59    // and is very unclear as to what it actually means.  The LegacyHTMLDocumentParser
60    // used to implement it.
61    virtual bool processingData() const { return false; }
62
63    // document() will return 0 after detach() is called.
64    Document* document() const { ASSERT(m_document); return m_document; }
65
66    bool isParsing() const { return m_state == ParsingState; }
67    bool isStopping() const { return m_state == StoppingState; }
68    bool isStopped() const { return m_state >= StoppedState; }
69    bool isDetached() const { return m_state == DetachedState; }
70
71    // FIXME: Is this necessary? Does XMLDocumentParserLibxml2 really need to set this?
72    virtual void startParsing();
73
74    // prepareToStop() is used when the EOF token is encountered and parsing is to be
75    // stopped normally.
76    virtual void prepareToStopParsing();
77
78    // stopParsing() is used when a load is canceled/stopped.
79    // stopParsing() is currently different from detach(), but shouldn't be.
80    // It should NOT be ok to call any methods on DocumentParser after either
81    // detach() or stopParsing() but right now only detach() will ASSERT.
82    virtual void stopParsing();
83
84    // Document is expected to detach the parser before releasing its ref.
85    // After detach, m_document is cleared.  The parser will unwind its
86    // callstacks, but not produce any more nodes.
87    // It is impossible for the parser to touch the rest of WebCore after
88    // detach is called.
89    virtual void detach();
90
91    void setDocumentWasLoadedAsPartOfNavigation() { m_documentWasLoadedAsPartOfNavigation = true; }
92    bool documentWasLoadedAsPartOfNavigation() const { return m_documentWasLoadedAsPartOfNavigation; }
93
94    // FIXME: The names are not very accurate :(
95    virtual void suspendScheduledTasks();
96    virtual void resumeScheduledTasks();
97
98protected:
99    DocumentParser(Document*);
100
101private:
102    enum ParserState {
103        ParsingState,
104        StoppingState,
105        StoppedState,
106        DetachedState
107    };
108    ParserState m_state;
109    bool m_documentWasLoadedAsPartOfNavigation;
110
111    // Every DocumentParser needs a pointer back to the document.
112    // m_document will be 0 after the parser is stopped.
113    Document* m_document;
114};
115
116} // namespace WebCore
117
118#endif // DocumentParser_h
119