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 "platform/heap/Handle.h"
28#include "wtf/Forward.h"
29#include "wtf/RefCounted.h"
30
31namespace blink {
32
33class Document;
34class DocumentParserClient;
35class SegmentedString;
36class ScriptableDocumentParser;
37class TextResourceDecoder;
38
39class DocumentParser : public RefCountedWillBeGarbageCollectedFinalized<DocumentParser> {
40public:
41    virtual ~DocumentParser();
42    virtual void trace(Visitor*);
43
44    virtual ScriptableDocumentParser* asScriptableDocumentParser() { return 0; }
45
46    // http://www.whatwg.org/specs/web-apps/current-work/#insertion-point
47    virtual bool hasInsertionPoint() { return true; }
48
49    // insert is used by document.write.
50    virtual void insert(const SegmentedString&) = 0;
51
52    // The below functions are used by DocumentWriter (the loader).
53    virtual void appendBytes(const char* bytes, size_t length) = 0;
54    virtual bool needsDecoder() const { return false; }
55    virtual void setDecoder(PassOwnPtr<TextResourceDecoder>);
56    virtual TextResourceDecoder* decoder();
57    virtual void setHasAppendedData() { }
58
59    // pinToMainThread also makes append() not yield before completion of that chunk.
60    virtual void pinToMainThread() { }
61
62    // FIXME: append() should be private, but DocumentWriter::replaceDocumentWhileExecutingJavaScriptURL uses it for now.
63    // FIXME: This really should take a PassOwnPtr to signify that it expects to take
64    // ownership of the buffer. The parser expects the PassRefPtr to hold the only ref of the StringImpl.
65    virtual void append(PassRefPtr<StringImpl>) = 0;
66
67    virtual void finish() = 0;
68
69    // FIXME: processingData() is only used by DocumentLoader::isLoadingInAPISense
70    // and is very unclear as to what it actually means.  The LegacyHTMLDocumentParser
71    // used to implement it.
72    virtual bool processingData() const { return false; }
73
74    // document() will return 0 after detach() is called.
75    Document* document() const { ASSERT(m_document); return m_document; }
76
77    bool isParsing() const { return m_state == ParsingState; }
78    bool isStopping() const { return m_state == StoppingState; }
79    bool isStopped() const { return m_state >= StoppedState; }
80    bool isDetached() const { return m_state == DetachedState; }
81
82    // prepareToStop() is used when the EOF token is encountered and parsing is to be
83    // stopped normally.
84    virtual void prepareToStopParsing();
85
86    // stopParsing() is used when a load is canceled/stopped.
87    // stopParsing() is currently different from detach(), but shouldn't be.
88    // It should NOT be ok to call any methods on DocumentParser after either
89    // detach() or stopParsing() but right now only detach() will ASSERT.
90    virtual void stopParsing();
91
92    // Document is expected to detach the parser before releasing its ref.
93    // After detach, m_document is cleared.  The parser will unwind its
94    // callstacks, but not produce any more nodes.
95    // It is impossible for the parser to touch the rest of WebCore after
96    // detach is called.
97    // Oilpan: We don't need to call detach when a Document is destructed.
98    virtual void detach();
99
100    void setDocumentWasLoadedAsPartOfNavigation() { m_documentWasLoadedAsPartOfNavigation = true; }
101    bool documentWasLoadedAsPartOfNavigation() const { return m_documentWasLoadedAsPartOfNavigation; }
102
103    // FIXME: The names are not very accurate :(
104    virtual void suspendScheduledTasks();
105    virtual void resumeScheduledTasks();
106
107    void addClient(DocumentParserClient*);
108    void removeClient(DocumentParserClient*);
109
110protected:
111    explicit DocumentParser(Document*);
112
113    virtual void flush() = 0;
114
115private:
116    enum ParserState {
117        ParsingState,
118        StoppingState,
119        StoppedState,
120        DetachedState
121    };
122    ParserState m_state;
123    bool m_documentWasLoadedAsPartOfNavigation;
124
125    // Every DocumentParser needs a pointer back to the document.
126    // m_document will be 0 after the parser is stopped.
127    RawPtrWillBeMember<Document> m_document;
128
129    WillBeHeapHashSet<RawPtrWillBeWeakMember<DocumentParserClient> > m_clients;
130};
131
132} // namespace blink
133
134#endif // DocumentParser_h
135