Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 27 | #include "wtf/Forward.h" |
| 28 | #include "wtf/RefCounted.h" |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 29 | |
| 30 | namespace WebCore { |
| 31 | |
| 32 | class Document; |
| 33 | class DocumentWriter; |
| 34 | class SegmentedString; |
| 35 | class ScriptableDocumentParser; |
| 36 | |
| 37 | class DocumentParser : public RefCounted<DocumentParser> { |
| 38 | public: |
| 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(const SegmentedString&) = 0; |
| 48 | |
| 49 | // appendBytes and flush are used by DocumentWriter (the loader). |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 50 | virtual size_t appendBytes(const char* bytes, size_t length) = 0; |
| 51 | virtual size_t flush() = 0; |
| 52 | virtual bool needsDecoder() const { return false; } |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 53 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 54 | // pinToMainThread also makes append() not yield before completion of that chunk. |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 55 | virtual void pinToMainThread() { } |
| 56 | |
| 57 | // FIXME: append() should be private, but DocumentWriter::replaceDocument uses it for now. |
| 58 | // FIXME: This really should take a PassOwnPtr to signify that it expects to take |
| 59 | // ownership of the buffer. The parser expects the PassRefPtr to hold the only ref of the StringImpl. |
| 60 | virtual void append(PassRefPtr<StringImpl>) = 0; |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 61 | |
| 62 | virtual void finish() = 0; |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 63 | |
| 64 | // FIXME: processingData() is only used by DocumentLoader::isLoadingInAPISense |
| 65 | // and is very unclear as to what it actually means. The LegacyHTMLDocumentParser |
| 66 | // used to implement it. |
| 67 | virtual bool processingData() const { return false; } |
| 68 | |
| 69 | // document() will return 0 after detach() is called. |
| 70 | Document* document() const { ASSERT(m_document); return m_document; } |
| 71 | |
| 72 | bool isParsing() const { return m_state == ParsingState; } |
| 73 | bool isStopping() const { return m_state == StoppingState; } |
| 74 | bool isStopped() const { return m_state >= StoppedState; } |
| 75 | bool isDetached() const { return m_state == DetachedState; } |
| 76 | |
| 77 | // FIXME: Is this necessary? Does XMLDocumentParserLibxml2 really need to set this? |
| 78 | virtual void startParsing(); |
| 79 | |
| 80 | // prepareToStop() is used when the EOF token is encountered and parsing is to be |
| 81 | // stopped normally. |
| 82 | virtual void prepareToStopParsing(); |
| 83 | |
| 84 | // stopParsing() is used when a load is canceled/stopped. |
| 85 | // stopParsing() is currently different from detach(), but shouldn't be. |
| 86 | // It should NOT be ok to call any methods on DocumentParser after either |
| 87 | // detach() or stopParsing() but right now only detach() will ASSERT. |
| 88 | virtual void stopParsing(); |
| 89 | |
| 90 | // Document is expected to detach the parser before releasing its ref. |
| 91 | // After detach, m_document is cleared. The parser will unwind its |
| 92 | // callstacks, but not produce any more nodes. |
| 93 | // It is impossible for the parser to touch the rest of WebCore after |
| 94 | // detach is called. |
| 95 | virtual void detach(); |
| 96 | |
| 97 | void setDocumentWasLoadedAsPartOfNavigation() { m_documentWasLoadedAsPartOfNavigation = true; } |
| 98 | bool documentWasLoadedAsPartOfNavigation() const { return m_documentWasLoadedAsPartOfNavigation; } |
| 99 | |
| 100 | // FIXME: The names are not very accurate :( |
| 101 | virtual void suspendScheduledTasks(); |
| 102 | virtual void resumeScheduledTasks(); |
| 103 | |
| 104 | protected: |
| 105 | explicit DocumentParser(Document*); |
| 106 | |
| 107 | private: |
| 108 | enum ParserState { |
| 109 | ParsingState, |
| 110 | StoppingState, |
| 111 | StoppedState, |
| 112 | DetachedState |
| 113 | }; |
| 114 | ParserState m_state; |
| 115 | bool m_documentWasLoadedAsPartOfNavigation; |
| 116 | |
| 117 | // Every DocumentParser needs a pointer back to the document. |
| 118 | // m_document will be 0 after the parser is stopped. |
| 119 | Document* m_document; |
| 120 | }; |
| 121 | |
| 122 | } // namespace WebCore |
| 123 | |
| 124 | #endif // DocumentParser_h |