| 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/HTMLParserIdioms.h" |
| 30 #include "core/html/parser/TextResourceDecoder.h" | 31 #include "core/html/parser/TextResourceDecoder.h" |
| 31 #include "wtf/MainThread.h" | 32 #include "wtf/MainThread.h" |
| 32 #include "wtf/text/TextPosition.h" | 33 #include "wtf/text/TextPosition.h" |
| 33 | 34 |
| 34 namespace blink { | 35 namespace blink { |
| 35 | 36 |
| 36 // We limit our chucks to 1000 tokens, to make sure the main | 37 // We limit our chucks to 1000 tokens, to make sure the main |
| 37 // thread is never waiting on the parser thread for tokens. | 38 // thread is never waiting on the parser thread for tokens. |
| 38 // This was tuned in https://bugs.webkit.org/show_bug.cgi?id=110408. | 39 // This was tuned in https://bugs.webkit.org/show_bug.cgi?id=110408. |
| 39 static const size_t pendingTokenLimit = 1000; | 40 static const size_t pendingTokenLimit = 1000; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 51 base::WeakPtr<BackgroundHTMLParser> BackgroundHTMLParser::create(PassOwnPtr<Back
groundHTMLParser::Configuration> config) | 52 base::WeakPtr<BackgroundHTMLParser> BackgroundHTMLParser::create(PassOwnPtr<Back
groundHTMLParser::Configuration> config) |
| 52 { | 53 { |
| 53 // Caller must free by calling stop(). | 54 // Caller must free by calling stop(). |
| 54 BackgroundHTMLParser* parser = new BackgroundHTMLParser(config); | 55 BackgroundHTMLParser* parser = new BackgroundHTMLParser(config); |
| 55 return parser->m_weakFactory.GetWeakPtr(); | 56 return parser->m_weakFactory.GetWeakPtr(); |
| 56 } | 57 } |
| 57 | 58 |
| 58 BackgroundHTMLParser::BackgroundHTMLParser(PassOwnPtr<Configuration> config) | 59 BackgroundHTMLParser::BackgroundHTMLParser(PassOwnPtr<Configuration> config) |
| 59 : m_token(adoptPtr(new HTMLToken)) | 60 : m_token(adoptPtr(new HTMLToken)) |
| 60 , m_tokenizer(HTMLTokenizer::create(config->options)) | 61 , m_tokenizer(HTMLTokenizer::create(config->options)) |
| 61 , m_treeBuilderSimulator(config->options) | |
| 62 , m_options(config->options) | 62 , m_options(config->options) |
| 63 , m_parser(config->parser) | 63 , m_parser(config->parser) |
| 64 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) | 64 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream)) |
| 65 , m_decoder(TextResourceDecoder::create()) | 65 , m_decoder(TextResourceDecoder::create()) |
| 66 , m_weakFactory(this) | 66 , m_weakFactory(this) |
| 67 { | 67 { |
| 68 } | 68 } |
| 69 | 69 |
| 70 BackgroundHTMLParser::~BackgroundHTMLParser() | 70 BackgroundHTMLParser::~BackgroundHTMLParser() |
| 71 { | 71 { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 delete this; | 112 delete this; |
| 113 } | 113 } |
| 114 | 114 |
| 115 void BackgroundHTMLParser::markEndOfFile() | 115 void BackgroundHTMLParser::markEndOfFile() |
| 116 { | 116 { |
| 117 ASSERT(!m_input.isClosed()); | 117 ASSERT(!m_input.isClosed()); |
| 118 m_input.append(SegmentedString(String(&kEndOfFileMarker, 1))); | 118 m_input.append(SegmentedString(String(&kEndOfFileMarker, 1))); |
| 119 m_input.close(); | 119 m_input.close(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 bool BackgroundHTMLParser::updateTokenizerState(const CompactHTMLToken& token) |
| 123 { |
| 124 if (token.type() == HTMLToken::StartTag) { |
| 125 const String& tagName = token.data(); |
| 126 // FIXME: This is just a copy of Tokenizer::updateStateFor which uses th
readSafeMatches. |
| 127 if (threadSafeMatch(tagName, HTMLNames::scriptTag)) |
| 128 m_tokenizer->setState(HTMLTokenizer::ScriptDataState); |
| 129 else if (threadSafeMatch(tagName, HTMLNames::styleTag)) |
| 130 m_tokenizer->setState(HTMLTokenizer::RAWTEXTState); |
| 131 } |
| 132 |
| 133 if (token.type() == HTMLToken::EndTag) { |
| 134 const String& tagName = token.data(); |
| 135 if (threadSafeMatch(tagName, HTMLNames::scriptTag)) { |
| 136 m_tokenizer->setState(HTMLTokenizer::DataState); |
| 137 return false; |
| 138 } |
| 139 } |
| 140 |
| 141 return true; |
| 142 } |
| 143 |
| 122 void BackgroundHTMLParser::pumpTokenizer() | 144 void BackgroundHTMLParser::pumpTokenizer() |
| 123 { | 145 { |
| 124 while (true) { | 146 while (true) { |
| 125 if (!m_tokenizer->nextToken(m_input, *m_token)) { | 147 if (!m_tokenizer->nextToken(m_input, *m_token)) { |
| 126 // We've reached the end of our current input. | 148 // We've reached the end of our current input. |
| 127 sendTokensToMainThread(); | 149 sendTokensToMainThread(); |
| 128 break; | 150 break; |
| 129 } | 151 } |
| 130 | 152 |
| 131 { | 153 { |
| 132 CompactHTMLToken token(m_token.get(), TextPosition(m_input.currentLi
ne(), m_input.currentColumn())); | 154 CompactHTMLToken token(m_token.get(), TextPosition(m_input.currentLi
ne(), m_input.currentColumn())); |
| 133 m_pendingTokens->append(token); | 155 m_pendingTokens->append(token); |
| 134 } | 156 } |
| 135 | 157 |
| 136 m_token->clear(); | 158 m_token->clear(); |
| 137 | 159 |
| 138 if (!m_treeBuilderSimulator.simulate(m_pendingTokens->last(), m_tokenize
r.get()) || m_pendingTokens->size() >= pendingTokenLimit) | 160 if (!updateTokenizerState(m_pendingTokens->last()) || m_pendingTokens->s
ize() >= pendingTokenLimit) |
| 139 sendTokensToMainThread(); | 161 sendTokensToMainThread(); |
| 140 } | 162 } |
| 141 } | 163 } |
| 142 | 164 |
| 143 void BackgroundHTMLParser::sendTokensToMainThread() | 165 void BackgroundHTMLParser::sendTokensToMainThread() |
| 144 { | 166 { |
| 145 if (m_pendingTokens->isEmpty()) | 167 if (m_pendingTokens->isEmpty()) |
| 146 return; | 168 return; |
| 147 | 169 |
| 148 #if ENABLE(ASSERT) | 170 #if ENABLE(ASSERT) |
| 149 checkThatTokensAreSafeToSendToAnotherThread(m_pendingTokens.get()); | 171 checkThatTokensAreSafeToSendToAnotherThread(m_pendingTokens.get()); |
| 150 #endif | 172 #endif |
| 151 | 173 |
| 152 OwnPtr<HTMLDocumentParser::ParsedChunk> chunk = adoptPtr(new HTMLDocumentPar
ser::ParsedChunk); | 174 OwnPtr<HTMLDocumentParser::ParsedChunk> chunk = adoptPtr(new HTMLDocumentPar
ser::ParsedChunk); |
| 153 chunk->tokenizerState = m_tokenizer->state(); | 175 chunk->tokenizerState = m_tokenizer->state(); |
| 154 chunk->tokens = m_pendingTokens.release(); | 176 chunk->tokens = m_pendingTokens.release(); |
| 155 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou
ndParser, m_parser, chunk.release())); | 177 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou
ndParser, m_parser, chunk.release())); |
| 156 | 178 |
| 157 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); | 179 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); |
| 158 } | 180 } |
| 159 | 181 |
| 160 } | 182 } |
| OLD | NEW |