OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google, Inc. All Rights Reserved. | 2 * Copyright (C) 2013 Google, Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 10 matching lines...) Expand all Loading... |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "config.h" | 26 #include "config.h" |
27 #include "core/html/parser/BackgroundHTMLParser.h" | 27 #include "core/html/parser/BackgroundHTMLParser.h" |
28 | 28 |
29 #include "core/html/parser/HTMLDocumentParser.h" | 29 #include "core/html/parser/HTMLDocumentParser.h" |
30 #include "core/html/parser/HTMLParserThread.h" | 30 #include "core/html/parser/HTMLParserThread.h" |
| 31 #include "core/html/parser/TextResourceDecoder.h" |
31 #include "core/html/parser/XSSAuditor.h" | 32 #include "core/html/parser/XSSAuditor.h" |
32 #include "wtf/MainThread.h" | 33 #include "wtf/MainThread.h" |
33 #include "wtf/text/TextPosition.h" | 34 #include "wtf/text/TextPosition.h" |
34 | 35 |
35 namespace WebCore { | 36 namespace WebCore { |
36 | 37 |
37 // On a network with high latency and high bandwidth, using a device | 38 // On a network with high latency and high bandwidth, using a device |
38 // with a fast CPU, we could end up speculatively tokenizing | 39 // with a fast CPU, we could end up speculatively tokenizing |
39 // the whole document, well ahead of when the main-thread actually needs it. | 40 // the whole document, well ahead of when the main-thread actually needs it. |
40 // This is a waste of memory (and potentially time if the speculation fails). | 41 // This is a waste of memory (and potentially time if the speculation fails). |
(...skipping 28 matching lines...) Expand all Loading... |
69 } | 70 } |
70 | 71 |
71 static void checkThatXSSInfosAreSafeToSendToAnotherThread(const XSSInfoStream& i
nfos) | 72 static void checkThatXSSInfosAreSafeToSendToAnotherThread(const XSSInfoStream& i
nfos) |
72 { | 73 { |
73 for (size_t i = 0; i < infos.size(); ++i) | 74 for (size_t i = 0; i < infos.size(); ++i) |
74 ASSERT(infos[i]->isSafeToSendToAnotherThread()); | 75 ASSERT(infos[i]->isSafeToSendToAnotherThread()); |
75 } | 76 } |
76 | 77 |
77 #endif | 78 #endif |
78 | 79 |
| 80 void BackgroundHTMLParser::start(PassRefPtr<WeakReference<BackgroundHTMLParser>
> reference, PassOwnPtr<Configuration> config) |
| 81 { |
| 82 new BackgroundHTMLParser(reference, config); |
| 83 // Caller must free by calling stop(). |
| 84 } |
| 85 |
79 BackgroundHTMLParser::BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHT
MLParser> > reference, PassOwnPtr<Configuration> config) | 86 BackgroundHTMLParser::BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHT
MLParser> > reference, PassOwnPtr<Configuration> config) |
80 : m_weakFactory(reference, this) | 87 : m_weakFactory(reference, this) |
81 , m_token(adoptPtr(new HTMLToken)) | 88 , m_token(adoptPtr(new HTMLToken)) |
82 , m_tokenizer(HTMLTokenizer::create(config->options)) | 89 , m_tokenizer(HTMLTokenizer::create(config->options)) |
83 , m_treeBuilderSimulator(config->options) | 90 , m_treeBuilderSimulator(config->options) |
84 , m_options(config->options) | 91 , m_options(config->options) |
85 , m_parser(config->parser) | 92 , m_parser(config->parser) |
86 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) | 93 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) |
87 , m_xssAuditor(config->xssAuditor.release()) | 94 , m_xssAuditor(config->xssAuditor.release()) |
88 , m_preloadScanner(config->preloadScanner.release()) | 95 , m_preloadScanner(config->preloadScanner.release()) |
| 96 , m_decoder(config->decoder.release()) |
89 { | 97 { |
90 } | 98 } |
91 | 99 |
| 100 BackgroundHTMLParser::~BackgroundHTMLParser() |
| 101 { |
| 102 } |
| 103 |
92 void BackgroundHTMLParser::append(const String& input) | 104 void BackgroundHTMLParser::append(const String& input) |
93 { | 105 { |
94 ASSERT(!m_input.current().isClosed()); | 106 ASSERT(!m_input.current().isClosed()); |
95 m_input.append(input); | 107 m_input.append(input); |
96 pumpTokenizer(); | 108 pumpTokenizer(); |
97 } | 109 } |
98 | 110 |
| 111 void BackgroundHTMLParser::appendBytes(PassOwnPtr<Vector<char> > buffer) |
| 112 { |
| 113 updateDocument(m_decoder->decode(buffer->data(), buffer->size())); |
| 114 } |
| 115 |
| 116 void BackgroundHTMLParser::setDecoder(PassOwnPtr<TextResourceDecoder> decoder) |
| 117 { |
| 118 m_decoder = decoder; |
| 119 } |
| 120 |
| 121 void BackgroundHTMLParser::flush() |
| 122 { |
| 123 updateDocument(m_decoder->flush()); |
| 124 } |
| 125 |
| 126 void BackgroundHTMLParser::updateDocument(const String& decodedData) |
| 127 { |
| 128 DocumentEncodingData encodingData(*m_decoder.get()); |
| 129 |
| 130 if (encodingData != m_lastSeenEncodingData) { |
| 131 m_lastSeenEncodingData = encodingData; |
| 132 |
| 133 m_xssAuditor->setEncoding(encodingData.encoding()); |
| 134 callOnMainThread(bind(&HTMLDocumentParser::didReceiveEncodingDataFromBac
kgroundParser, m_parser, encodingData)); |
| 135 } |
| 136 |
| 137 if (decodedData.isEmpty()) |
| 138 return; |
| 139 |
| 140 append(decodedData); |
| 141 } |
| 142 |
99 void BackgroundHTMLParser::resumeFrom(PassOwnPtr<Checkpoint> checkpoint) | 143 void BackgroundHTMLParser::resumeFrom(PassOwnPtr<Checkpoint> checkpoint) |
100 { | 144 { |
101 m_parser = checkpoint->parser; | 145 m_parser = checkpoint->parser; |
102 m_token = checkpoint->token.release(); | 146 m_token = checkpoint->token.release(); |
103 m_tokenizer = checkpoint->tokenizer.release(); | 147 m_tokenizer = checkpoint->tokenizer.release(); |
104 m_treeBuilderSimulator.setState(checkpoint->treeBuilderState); | 148 m_treeBuilderSimulator.setState(checkpoint->treeBuilderState); |
105 m_input.rewindTo(checkpoint->inputCheckpoint, checkpoint->unparsedInput); | 149 m_input.rewindTo(checkpoint->inputCheckpoint, checkpoint->unparsedInput); |
106 m_preloadScanner->rewindTo(checkpoint->preloadScannerCheckpoint); | 150 m_preloadScanner->rewindTo(checkpoint->preloadScannerCheckpoint); |
107 pumpTokenizer(); | 151 pumpTokenizer(); |
108 } | 152 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 chunk->treeBuilderState = m_treeBuilderSimulator.state(); | 244 chunk->treeBuilderState = m_treeBuilderSimulator.state(); |
201 chunk->inputCheckpoint = m_input.createCheckpoint(m_pendingTokens->size()); | 245 chunk->inputCheckpoint = m_input.createCheckpoint(m_pendingTokens->size()); |
202 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); | 246 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); |
203 chunk->tokens = m_pendingTokens.release(); | 247 chunk->tokens = m_pendingTokens.release(); |
204 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou
ndParser, m_parser, chunk.release())); | 248 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou
ndParser, m_parser, chunk.release())); |
205 | 249 |
206 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); | 250 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); |
207 } | 251 } |
208 | 252 |
209 } | 253 } |
OLD | NEW |