1/*
2 * Copyright (C) 2013 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 GOOGLE 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 GOOGLE 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 BackgroundHTMLParser_h
27#define BackgroundHTMLParser_h
28
29#if ENABLE(THREADED_HTML_PARSER)
30
31#include "BackgroundHTMLInputStream.h"
32#include "CompactHTMLToken.h"
33#include "HTMLParserOptions.h"
34#include "HTMLPreloadScanner.h"
35#include "HTMLSourceTracker.h"
36#include "HTMLToken.h"
37#include "HTMLTokenizer.h"
38#include "HTMLTreeBuilderSimulator.h"
39#include "XSSAuditorDelegate.h"
40#include <wtf/PassOwnPtr.h>
41#include <wtf/RefPtr.h>
42#include <wtf/Vector.h>
43#include <wtf/WeakPtr.h>
44
45namespace WebCore {
46
47class HTMLDocumentParser;
48class XSSAuditor;
49
50class BackgroundHTMLParser {
51    WTF_MAKE_FAST_ALLOCATED;
52public:
53    struct Configuration {
54        HTMLParserOptions options;
55        WeakPtr<HTMLDocumentParser> parser;
56        OwnPtr<XSSAuditor> xssAuditor;
57        OwnPtr<TokenPreloadScanner> preloadScanner;
58    };
59
60    static void create(PassRefPtr<WeakReference<BackgroundHTMLParser> > reference, PassOwnPtr<Configuration> config)
61    {
62        new BackgroundHTMLParser(reference, config);
63        // Caller must free by calling stop().
64    }
65
66    struct Checkpoint {
67        WeakPtr<HTMLDocumentParser> parser;
68        OwnPtr<HTMLToken> token;
69        OwnPtr<HTMLTokenizer> tokenizer;
70        HTMLTreeBuilderSimulator::State treeBuilderState;
71        HTMLInputCheckpoint inputCheckpoint;
72        TokenPreloadScannerCheckpoint preloadScannerCheckpoint;
73        String unparsedInput;
74    };
75
76    void append(const String&);
77    void resumeFrom(PassOwnPtr<Checkpoint>);
78    void startedChunkWithCheckpoint(HTMLInputCheckpoint);
79    void finish();
80    void stop();
81
82    void forcePlaintextForTextDocument();
83
84private:
85    BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHTMLParser> >, PassOwnPtr<Configuration>);
86
87    void markEndOfFile();
88    void pumpTokenizer();
89    void sendTokensToMainThread();
90
91    WeakPtrFactory<BackgroundHTMLParser> m_weakFactory;
92    BackgroundHTMLInputStream m_input;
93    HTMLSourceTracker m_sourceTracker;
94    OwnPtr<HTMLToken> m_token;
95    OwnPtr<HTMLTokenizer> m_tokenizer;
96    HTMLTreeBuilderSimulator m_treeBuilderSimulator;
97    HTMLParserOptions m_options;
98    WeakPtr<HTMLDocumentParser> m_parser;
99
100    OwnPtr<CompactHTMLTokenStream> m_pendingTokens;
101    PreloadRequestStream m_pendingPreloads;
102    XSSInfoStream m_pendingXSSInfos;
103
104    OwnPtr<XSSAuditor> m_xssAuditor;
105    OwnPtr<TokenPreloadScanner> m_preloadScanner;
106};
107
108}
109
110#endif // ENABLE(THREADED_HTML_PARSER)
111
112#endif
113