Chromium Code Reviews| 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" | |
|
abarth-chromium
2013/11/18 06:57:10
Should this class move to the core/html/parser dir
oystein (OOO til 10th of July)
2013/11/18 22:18:42
It does live in an awkward location right now, but
| |
| 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 |
| 80 BackgroundHTMLParser::BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHT MLParser> > reference, PassOwnPtr<Configuration> config) | 83 void BackgroundHTMLParser::create(PassRefPtr<WeakReference<BackgroundHTMLParser> > reference, PassOwnPtr<Configuration> config, PassOwnPtr<TextResourceDecoder> decoder) |
| 84 { | |
| 85 new BackgroundHTMLParser(reference, config, decoder); | |
|
eseidel
2013/11/18 20:07:40
This reads very strange.
oystein (OOO til 10th of July)
2013/11/18 22:18:42
I agree (I just moved this from the header), I don
| |
| 86 // Caller must free by calling stop(). | |
|
eseidel
2013/11/18 20:07:40
If this is deleted via "stop" maybe it should be c
oystein (OOO til 10th of July)
2013/11/18 22:18:42
Done.
| |
| 87 } | |
| 88 | |
| 89 BackgroundHTMLParser::BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHT MLParser> > reference, PassOwnPtr<Configuration> config, PassOwnPtr<TextResource Decoder> decoder) | |
| 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(decoder) | |
| 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(PassRefPtr<SharedBuffer> data) | |
| 115 { | |
| 116 RefPtr<SharedBuffer> buffer = data; | |
| 117 String decoded = m_decoder->decode(buffer->data(), buffer->size()); | |
| 118 updateDocument(decoded); | |
| 119 } | |
| 120 | |
| 121 void BackgroundHTMLParser::flush() | |
| 122 { | |
| 123 String remainingData = m_decoder->flush(); | |
|
eseidel
2013/11/18 20:07:40
I'm not sure the local buys you much.
oystein (OOO til 10th of July)
2013/11/18 22:18:42
error: non-const lvalue reference to type 'WTF::St
| |
| 124 updateDocument(remainingData); | |
| 125 } | |
| 126 | |
| 127 void BackgroundHTMLParser::updateDocument(String& decodedData) | |
| 128 { | |
| 129 DocumentEncodingData encodingData; | |
| 130 encodingData.encoding = m_decoder->encoding(); | |
|
eseidel
2013/11/18 20:07:40
This feels like DocumentEndodingData should be som
oystein (OOO til 10th of July)
2013/11/18 22:18:42
Done; Added TextResourceDecoder::getEncodingData()
| |
| 131 encodingData.wasDetectedHeuristically = m_decoder->encodingWasDetectedHeuris tically(); | |
| 132 encodingData.sawDecodingError = m_decoder->sawError(); | |
| 133 | |
| 134 if (encodingData != m_lastSeenEncodingData) { | |
| 135 m_lastSeenEncodingData = encodingData; | |
| 136 callOnMainThread(bind(&HTMLDocumentParser::didReceiveEncodingDataFromBac kgroundParser, m_parser, encodingData)); | |
| 137 } | |
| 138 | |
| 139 if (decodedData.isEmpty()) | |
| 140 return; | |
| 141 | |
| 142 append(decodedData); | |
| 143 } | |
| 144 | |
| 100 void BackgroundHTMLParser::resumeFrom(PassOwnPtr<Checkpoint> checkpoint) | 145 void BackgroundHTMLParser::resumeFrom(PassOwnPtr<Checkpoint> checkpoint) |
| 101 { | 146 { |
| 102 m_parser = checkpoint->parser; | 147 m_parser = checkpoint->parser; |
| 103 m_token = checkpoint->token.release(); | 148 m_token = checkpoint->token.release(); |
| 104 m_tokenizer = checkpoint->tokenizer.release(); | 149 m_tokenizer = checkpoint->tokenizer.release(); |
| 105 m_treeBuilderSimulator.setState(checkpoint->treeBuilderState); | 150 m_treeBuilderSimulator.setState(checkpoint->treeBuilderState); |
| 106 m_input.rewindTo(checkpoint->inputCheckpoint, checkpoint->unparsedInput); | 151 m_input.rewindTo(checkpoint->inputCheckpoint, checkpoint->unparsedInput); |
| 107 m_preloadScanner->rewindTo(checkpoint->preloadScannerCheckpoint); | 152 m_preloadScanner->rewindTo(checkpoint->preloadScannerCheckpoint); |
| 108 pumpTokenizer(); | 153 pumpTokenizer(); |
| 109 } | 154 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 chunk->tokenizerState = m_tokenizer->state(); | 246 chunk->tokenizerState = m_tokenizer->state(); |
| 202 chunk->treeBuilderState = m_treeBuilderSimulator.state(); | 247 chunk->treeBuilderState = m_treeBuilderSimulator.state(); |
| 203 chunk->inputCheckpoint = m_input.createCheckpoint(); | 248 chunk->inputCheckpoint = m_input.createCheckpoint(); |
| 204 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); | 249 chunk->preloadScannerCheckpoint = m_preloadScanner->createCheckpoint(); |
| 205 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou ndParser, m_parser, chunk.release())); | 250 callOnMainThread(bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgrou ndParser, m_parser, chunk.release())); |
| 206 | 251 |
| 207 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); | 252 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream); |
| 208 } | 253 } |
| 209 | 254 |
| 210 } | 255 } |
| OLD | NEW |