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/fetch/TextResourceDecoder.h" |
29 #include "core/html/parser/HTMLDocumentParser.h" | 30 #include "core/html/parser/HTMLDocumentParser.h" |
30 #include "core/html/parser/HTMLParserThread.h" | 31 #include "core/html/parser/HTMLParserThread.h" |
| 32 #include "core/html/parser/HTMLToken.h" |
31 #include "core/html/parser/HTMLTokenizer.h" | 33 #include "core/html/parser/HTMLTokenizer.h" |
32 #include "core/html/parser/XSSAuditor.h" | 34 #include "core/html/parser/XSSAuditor.h" |
| 35 #include "platform/SharedBuffer.h" |
33 #include "wtf/MainThread.h" | 36 #include "wtf/MainThread.h" |
34 #include "wtf/text/TextPosition.h" | 37 #include "wtf/text/TextPosition.h" |
35 | 38 |
36 namespace WebCore { | 39 namespace WebCore { |
37 | 40 |
38 // On a network with high latency and high bandwidth, using a device | 41 // On a network with high latency and high bandwidth, using a device |
39 // with a fast CPU, we could end up speculatively tokenizing | 42 // with a fast CPU, we could end up speculatively tokenizing |
40 // the whole document, well ahead of when the main-thread actually needs it. | 43 // the whole document, well ahead of when the main-thread actually needs it. |
41 // This is a waste of memory (and potentially time if the speculation fails). | 44 // This is a waste of memory (and potentially time if the speculation fails). |
42 // So we limit our outstanding speculations arbitrarily to 10. | 45 // So we limit our outstanding speculations arbitrarily to 10. |
(...skipping 27 matching lines...) Expand all Loading... |
70 } | 73 } |
71 | 74 |
72 static void checkThatXSSInfosAreSafeToSendToAnotherThread(const XSSInfoStream& i
nfos) | 75 static void checkThatXSSInfosAreSafeToSendToAnotherThread(const XSSInfoStream& i
nfos) |
73 { | 76 { |
74 for (size_t i = 0; i < infos.size(); ++i) | 77 for (size_t i = 0; i < infos.size(); ++i) |
75 ASSERT(infos[i]->isSafeToSendToAnotherThread()); | 78 ASSERT(infos[i]->isSafeToSendToAnotherThread()); |
76 } | 79 } |
77 | 80 |
78 #endif | 81 #endif |
79 | 82 |
| 83 void BackgroundHTMLParser::start(PassRefPtr<WeakReference<BackgroundHTMLParser>
> reference, PassOwnPtr<Configuration> config) |
| 84 { |
| 85 new BackgroundHTMLParser(reference, config); |
| 86 // Caller must free by calling stop(). |
| 87 } |
| 88 |
80 BackgroundHTMLParser::BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHT
MLParser> > reference, PassOwnPtr<Configuration> config) | 89 BackgroundHTMLParser::BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHT
MLParser> > reference, PassOwnPtr<Configuration> config) |
81 : m_weakFactory(reference, this) | 90 : m_weakFactory(reference, this) |
82 , m_token(adoptPtr(new HTMLToken)) | 91 , m_token(adoptPtr(new HTMLToken)) |
83 , m_tokenizer(HTMLTokenizer::create(config->options)) | 92 , m_tokenizer(HTMLTokenizer::create(config->options)) |
84 , m_treeBuilderSimulator(config->options) | 93 , m_treeBuilderSimulator(config->options) |
85 , m_options(config->options) | 94 , m_options(config->options) |
86 , m_parser(config->parser) | 95 , m_parser(config->parser) |
87 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) | 96 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) |
88 , m_xssAuditor(config->xssAuditor.release()) | 97 , m_xssAuditor(config->xssAuditor.release()) |
89 , m_preloadScanner(config->preloadScanner.release()) | 98 , m_preloadScanner(config->preloadScanner.release()) |
| 99 , m_decoder(config->decoder.release()) |
90 { | 100 { |
91 } | 101 } |
92 | 102 |
| 103 BackgroundHTMLParser::~BackgroundHTMLParser() |
| 104 { |
| 105 } |
| 106 |
93 void BackgroundHTMLParser::append(const String& input) | 107 void BackgroundHTMLParser::append(const String& input) |
94 { | 108 { |
95 ASSERT(!m_input.current().isClosed()); | 109 ASSERT(!m_input.current().isClosed()); |
96 m_input.append(input); | 110 m_input.append(input); |
97 pumpTokenizer(); | 111 pumpTokenizer(); |
98 } | 112 } |
99 | 113 |
| 114 void BackgroundHTMLParser::appendBytes(PassOwnPtr<Vector<char> > buffer) |
| 115 { |
| 116 updateDocument(m_decoder->decode(buffer->data(), buffer->size())); |
| 117 } |
| 118 |
| 119 void BackgroundHTMLParser::setDecoder(PassOwnPtr<TextResourceDecoder> decoder) |
| 120 { |
| 121 m_decoder = decoder; |
| 122 } |
| 123 |
| 124 void BackgroundHTMLParser::flush() |
| 125 { |
| 126 updateDocument(m_decoder->flush()); |
| 127 } |
| 128 |
| 129 void BackgroundHTMLParser::updateDocument(const String& decodedData) |
| 130 { |
| 131 DocumentEncodingData encodingData(*m_decoder.get()); |
| 132 |
| 133 if (encodingData != m_lastSeenEncodingData) { |
| 134 m_lastSeenEncodingData = encodingData; |
| 135 |
| 136 m_xssAuditor->setEncoding(encodingData.encoding()); |
| 137 callOnMainThread(bind(&HTMLDocumentParser::didReceiveEncodingDataFromBac
kgroundParser, m_parser, encodingData)); |
| 138 } |
| 139 |
| 140 if (decodedData.isEmpty()) |
| 141 return; |
| 142 |
| 143 append(decodedData); |
| 144 } |
| 145 |
100 void BackgroundHTMLParser::resumeFrom(PassOwnPtr<Checkpoint> checkpoint) | 146 void BackgroundHTMLParser::resumeFrom(PassOwnPtr<Checkpoint> checkpoint) |
101 { | 147 { |
102 m_parser = checkpoint->parser; | 148 m_parser = checkpoint->parser; |
103 m_token = checkpoint->token.release(); | 149 m_token = checkpoint->token.release(); |
104 m_tokenizer = checkpoint->tokenizer.release(); | 150 m_tokenizer = checkpoint->tokenizer.release(); |
105 m_treeBuilderSimulator.setState(checkpoint->treeBuilderState); | 151 m_treeBuilderSimulator.setState(checkpoint->treeBuilderState); |
106 m_input.rewindTo(checkpoint->inputCheckpoint, checkpoint->unparsedInput); | 152 m_input.rewindTo(checkpoint->inputCheckpoint, checkpoint->unparsedInput); |
107 m_preloadScanner->rewindTo(checkpoint->preloadScannerCheckpoint); | 153 m_preloadScanner->rewindTo(checkpoint->preloadScannerCheckpoint); |
108 pumpTokenizer(); | 154 pumpTokenizer(); |
109 } | 155 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 chunk->tokenizerState = m_tokenizer->state(); | 247 chunk->tokenizerState = m_tokenizer->state(); |
202 chunk->treeBuilderState = m_treeBuilderSimulator.state(); | 248 chunk->treeBuilderState = m_treeBuilderSimulator.state(); |
203 chunk->inputCheckpoint = m_input.createCheckpoint(); | 249 chunk->inputCheckpoint = m_input.createCheckpoint(); |
204 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); | 250 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); |
205 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou
ndParser, m_parser, chunk.release())); | 251 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou
ndParser, m_parser, chunk.release())); |
206 | 252 |
207 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); | 253 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); |
208 } | 254 } |
209 | 255 |
210 } | 256 } |
OLD | NEW |