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 |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
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" | |
31 #include "core/html/parser/TextResourceDecoder.h" | 30 #include "core/html/parser/TextResourceDecoder.h" |
32 #include "core/html/parser/XSSAuditor.h" | 31 #include "core/html/parser/XSSAuditor.h" |
33 #include "wtf/MainThread.h" | 32 #include "wtf/MainThread.h" |
34 #include "wtf/text/TextPosition.h" | 33 #include "wtf/text/TextPosition.h" |
35 | 34 |
36 namespace WebCore { | 35 namespace WebCore { |
37 | 36 |
38 // On a network with high latency and high bandwidth, using a device | 37 // On a network with high latency and high bandwidth, using a device |
39 // with a fast CPU, we could end up speculatively tokenizing | 38 // with a fast CPU, we could end up speculatively tokenizing |
40 // the whole document, well ahead of when the main-thread actually needs it. | 39 // the whole document, well ahead of when the main-thread actually needs it. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 : m_weakFactory(reference, this) | 86 : m_weakFactory(reference, this) |
88 , m_token(adoptPtr(new HTMLToken)) | 87 , m_token(adoptPtr(new HTMLToken)) |
89 , m_tokenizer(HTMLTokenizer::create(config->options)) | 88 , m_tokenizer(HTMLTokenizer::create(config->options)) |
90 , m_treeBuilderSimulator(config->options) | 89 , m_treeBuilderSimulator(config->options) |
91 , m_options(config->options) | 90 , m_options(config->options) |
92 , m_parser(config->parser) | 91 , m_parser(config->parser) |
93 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) | 92 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) |
94 , m_xssAuditor(config->xssAuditor.release()) | 93 , m_xssAuditor(config->xssAuditor.release()) |
95 , m_preloadScanner(config->preloadScanner.release()) | 94 , m_preloadScanner(config->preloadScanner.release()) |
96 , m_decoder(config->decoder.release()) | 95 , m_decoder(config->decoder.release()) |
96 , m_resourceBridge(config->resourceBridge.release()) | |
97 , m_parserThreadIsStandalone(false) | |
97 { | 98 { |
99 // Tell the resource bridge that we're ready to start receiving | |
100 // chunks of data. | |
101 if (m_resourceBridge) | |
102 m_resourceBridge->setPeer(this); | |
98 } | 103 } |
99 | 104 |
100 BackgroundHTMLParser::~BackgroundHTMLParser() | 105 BackgroundHTMLParser::~BackgroundHTMLParser() |
101 { | 106 { |
107 if (m_resourceBridge) { | |
108 // Tell the resource bridge that calling into this instance | |
109 // is no longer safe and any further data received should be | |
110 // ignored. | |
111 m_resourceBridge->setPeer(0); | |
112 // The resource bridge needs to be destructed on the main thread, | |
113 // as the filter it installs on the I/O thread can only be removed | |
114 // from the main thread... | |
115 // Note we can only call static functions on the HTMLDocumentParser at t his point | |
116 // as the weakptr we have for it is likely invalidated now. | |
117 callOnMainThread(bind(&HTMLDocumentParser::destructResourceBridge, m_res ourceBridge.release())); | |
118 } | |
119 } | |
120 | |
121 void BackgroundHTMLParser::OnReceivedData(const char* data, size_t length) | |
122 { | |
123 if (m_parserThreadIsStandalone) { | |
124 updateDocument(m_decoder->decode(data, length)); | |
125 return; | |
126 } | |
127 | |
128 // If the parser thread is not standalone yet, it means we may get | |
129 // further data packets from the main thread and will need to queue | |
130 // up any data coming directly from the parser thread until we're | |
131 // sure we're complete. | |
132 | |
133 // Storing the raw pointer here is safe as we keep the shared | |
134 // memory alive for as long as this object is alive (via its | |
135 // attached WebParserResourceBridge) | |
abarth-chromium
2013/12/18 18:28:49
That sounds very fragile. How about we just make
oystein (OOO til 10th of July)
2014/01/13 23:19:50
The lifetime coupling is still needed to be able t
| |
136 m_queuedData.push_back(std::make_pair(data, length)); | |
102 } | 137 } |
103 | 138 |
104 void BackgroundHTMLParser::append(const String& input) | 139 void BackgroundHTMLParser::append(const String& input) |
105 { | 140 { |
106 ASSERT(!m_input.current().isClosed()); | 141 ASSERT(!m_input.current().isClosed()); |
107 m_input.append(input); | 142 m_input.append(input); |
108 pumpTokenizer(); | 143 pumpTokenizer(); |
109 } | 144 } |
110 | 145 |
111 void BackgroundHTMLParser::appendBytes(PassOwnPtr<Vector<char> > buffer) | 146 void BackgroundHTMLParser::appendBytes(PassOwnPtr<Vector<char> > buffer) |
112 { | 147 { |
148 ASSERT(!m_parserThreadIsStandalone); | |
113 updateDocument(m_decoder->decode(buffer->data(), buffer->size())); | 149 updateDocument(m_decoder->decode(buffer->data(), buffer->size())); |
114 } | 150 } |
115 | 151 |
116 void BackgroundHTMLParser::setDecoder(PassOwnPtr<TextResourceDecoder> decoder) | 152 void BackgroundHTMLParser::setDecoder(PassOwnPtr<TextResourceDecoder> decoder) |
117 { | 153 { |
118 m_decoder = decoder; | 154 m_decoder = decoder; |
119 } | 155 } |
120 | 156 |
121 void BackgroundHTMLParser::flush() | 157 void BackgroundHTMLParser::flush() |
122 { | 158 { |
123 updateDocument(m_decoder->flush()); | 159 updateDocument(m_decoder->flush()); |
124 } | 160 } |
125 | 161 |
126 void BackgroundHTMLParser::updateDocument(const String& decodedData) | 162 void BackgroundHTMLParser::updateDocument(const String& decodedData) |
127 { | 163 { |
128 DocumentEncodingData encodingData(*m_decoder.get()); | 164 DocumentEncodingData encodingData(*m_decoder.get()); |
129 | 165 |
130 if (encodingData != m_lastSeenEncodingData) { | 166 if (encodingData != m_lastSeenEncodingData) { |
131 m_lastSeenEncodingData = encodingData; | 167 m_lastSeenEncodingData = encodingData; |
132 | |
133 m_xssAuditor->setEncoding(encodingData.encoding()); | 168 m_xssAuditor->setEncoding(encodingData.encoding()); |
134 callOnMainThread(bind(&HTMLDocumentParser::didReceiveEncodingDataFromBac kgroundParser, m_parser, encodingData)); | 169 callOnMainThread(bind(&HTMLDocumentParser::didReceiveEncodingDataFromBac kgroundParser, m_parser, encodingData)); |
135 } | 170 } |
136 | 171 |
137 if (decodedData.isEmpty()) | 172 if (decodedData.isEmpty()) |
138 return; | 173 return; |
139 | 174 |
140 append(decodedData); | 175 append(decodedData); |
141 } | 176 } |
142 | 177 |
(...skipping 20 matching lines...) Expand all Loading... | |
163 { | 198 { |
164 markEndOfFile(); | 199 markEndOfFile(); |
165 pumpTokenizer(); | 200 pumpTokenizer(); |
166 } | 201 } |
167 | 202 |
168 void BackgroundHTMLParser::stop() | 203 void BackgroundHTMLParser::stop() |
169 { | 204 { |
170 delete this; | 205 delete this; |
171 } | 206 } |
172 | 207 |
208 void BackgroundHTMLParser::resourceFilterAdded() | |
209 { | |
210 m_parserThreadIsStandalone = true; | |
abarth-chromium
2013/12/18 18:28:49
I'm not sure what this state variable means. It l
oystein (OOO til 10th of July)
2014/01/13 23:19:50
Renamed to m_receivingDataOnlyFromResourceProvider
| |
211 | |
212 // At this point we know for sure that no further data will | |
213 // be coming from the main thread, so we can process anything | |
214 // we've received directly on the parser thread in the meantime. | |
215 for (DataQueue::iterator entry = m_queuedData.begin(); entry != m_queuedData .end(); ++entry) | |
216 updateDocument(m_decoder->decode(entry->first, entry->second)); | |
abarth-chromium
2013/12/18 18:28:49
Do we need to worry about one of these updateDocum
oystein (OOO til 10th of July)
2014/01/13 23:19:50
Shouldn't matter. As soon as a parsed chunk is pro
| |
217 | |
218 // After this point we'll never need m_queuedData again, so let's release it s | |
219 // memory. | |
220 DataQueue emptyQueue; | |
221 std::swap(emptyQueue, m_queuedData); | |
abarth-chromium
2013/12/18 18:28:49
These idioms will be different once you switch to
| |
222 } | |
223 | |
173 void BackgroundHTMLParser::forcePlaintextForTextDocument() | 224 void BackgroundHTMLParser::forcePlaintextForTextDocument() |
174 { | 225 { |
175 // This is only used by the TextDocumentParser (a subclass of HTMLDocumentPa rser) | 226 // This is only used by the TextDocumentParser (a subclass of HTMLDocumentPa rser) |
176 // to force us into the PLAINTEXT state w/o using a <plaintext> tag. | 227 // to force us into the PLAINTEXT state w/o using a <plaintext> tag. |
177 // The TextDocumentParser uses a <pre> tag for historical/compatibility reas ons. | 228 // The TextDocumentParser uses a <pre> tag for historical/compatibility reas ons. |
178 m_tokenizer->setState(HTMLTokenizer::PLAINTEXTState); | 229 m_tokenizer->setState(HTMLTokenizer::PLAINTEXTState); |
179 } | 230 } |
180 | 231 |
181 void BackgroundHTMLParser::markEndOfFile() | 232 void BackgroundHTMLParser::markEndOfFile() |
182 { | 233 { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 chunk->treeBuilderState = m_treeBuilderSimulator.state(); | 295 chunk->treeBuilderState = m_treeBuilderSimulator.state(); |
245 chunk->inputCheckpoint = m_input.createCheckpoint(m_pendingTokens->size()); | 296 chunk->inputCheckpoint = m_input.createCheckpoint(m_pendingTokens->size()); |
246 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); | 297 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); |
247 chunk->tokens = m_pendingTokens.release(); | 298 chunk->tokens = m_pendingTokens.release(); |
248 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou ndParser, m_parser, chunk.release())); | 299 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou ndParser, m_parser, chunk.release())); |
249 | 300 |
250 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); | 301 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); |
251 } | 302 } |
252 | 303 |
253 } | 304 } |
OLD | NEW |