1/*
2 * Copyright (C) 2000 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2005, 2006 Apple 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#pragma once
25
26#include <wtf/Forward.h>
27#include <wtf/RefCounted.h>
28#include <wtf/WeakPtr.h>
29
30namespace WebCore {
31
32class Document;
33class DocumentWriter;
34class SegmentedString;
35class ScriptableDocumentParser;
36
37class DocumentParser : public RefCounted<DocumentParser> {
38public:
39 virtual ~DocumentParser();
40
41 virtual ScriptableDocumentParser* asScriptableDocumentParser() { return 0; }
42
43 // http://www.whatwg.org/specs/web-apps/current-work/#insertion-point
44 virtual bool hasInsertionPoint() { return true; }
45
46 // insert is used by document.write.
47 virtual void insert(SegmentedString&&) = 0;
48
49 // appendBytes and flush are used by DocumentWriter (the loader).
50 virtual void appendBytes(DocumentWriter&, const char* bytes, size_t length) = 0;
51 virtual void flush(DocumentWriter&) = 0;
52
53 // FIXME: append() should be private, but DocumentWriter::replaceDocument uses it for now.
54 // FIXME: This really should take a std::unique_ptr to signify that it expects to take
55 // ownership of the buffer. The parser expects the RefPtr to hold the only ref of the StringImpl.
56 virtual void append(RefPtr<StringImpl>&&) = 0;
57
58 virtual void finish() = 0;
59
60 // FIXME: processingData() is only used by DocumentLoader::isLoadingInAPISense
61 // and is very unclear as to what it actually means. The LegacyHTMLDocumentParser
62 // used to implement it.
63 virtual bool processingData() const { return false; }
64
65 // document() will return 0 after detach() is called.
66 Document* document() const;
67
68 bool isParsing() const { return m_state == ParsingState; }
69 bool isStopping() const { return m_state == StoppingState; }
70 bool isStopped() const { return m_state >= StoppedState; }
71 bool isDetached() const { return m_state == DetachedState; }
72
73 // FIXME: Is this necessary? Does XMLDocumentParserLibxml2 really need to set this?
74 virtual void startParsing();
75
76 // prepareToStop() is used when the EOF token is encountered and parsing is to be
77 // stopped normally.
78 virtual void prepareToStopParsing();
79
80 // stopParsing() is used when a load is canceled/stopped.
81 // stopParsing() is currently different from detach(), but shouldn't be.
82 // It should NOT be ok to call any methods on DocumentParser after either
83 // detach() or stopParsing() but right now only detach() will ASSERT.
84 virtual void stopParsing();
85
86 // Document is expected to detach the parser before releasing its ref.
87 // After detach, m_document is cleared. The parser will unwind its
88 // callstacks, but not produce any more nodes.
89 // It is impossible for the parser to touch the rest of WebCore after
90 // detach is called.
91 virtual void detach();
92
93 void setDocumentWasLoadedAsPartOfNavigation() { m_documentWasLoadedAsPartOfNavigation = true; }
94 bool documentWasLoadedAsPartOfNavigation() const { return m_documentWasLoadedAsPartOfNavigation; }
95
96 // FIXME: The names are not very accurate :(
97 virtual void suspendScheduledTasks();
98 virtual void resumeScheduledTasks();
99
100 virtual void didBeginYieldingParser() { }
101 virtual void didEndYieldingParser() { }
102
103protected:
104 explicit DocumentParser(Document&);
105
106private:
107 enum ParserState {
108 ParsingState,
109 StoppingState,
110 StoppedState,
111 DetachedState
112 };
113 ParserState m_state;
114 bool m_documentWasLoadedAsPartOfNavigation;
115
116 // Every DocumentParser needs a pointer back to the document.
117 // m_document will be 0 after the parser is stopped.
118 WeakPtr<Document> m_document;
119};
120
121} // namespace WebCore
122