Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(403)

Side by Side Diff: sky/engine/core/html/parser/BackgroundHTMLParser.cpp

Issue 1215103007: Remove remaining HTML elements (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
24 */
25
26 #include "sky/engine/core/html/parser/BackgroundHTMLParser.h"
27
28 #include "base/bind.h"
29 #include "base/single_thread_task_runner.h"
30 #include "sky/engine/core/html/parser/HTMLDocumentParser.h"
31 #include "sky/engine/core/html/parser/HTMLParserIdioms.h"
32 #include "sky/engine/core/html/parser/TextResourceDecoder.h"
33 #include "sky/engine/public/platform/Platform.h"
34 #include "sky/engine/wtf/MainThread.h"
35 #include "sky/engine/wtf/text/TextPosition.h"
36
37 namespace blink {
38
39 // We limit our chucks to 1000 tokens, to make sure the main
40 // thread is never waiting on the parser thread for tokens.
41 // This was tuned in https://bugs.webkit.org/show_bug.cgi?id=110408.
42 static const size_t pendingTokenLimit = 1000;
43
44 #if ENABLE(ASSERT)
45
46 static void checkThatTokensAreSafeToSendToAnotherThread(const CompactHTMLTokenSt ream* tokens)
47 {
48 for (size_t i = 0; i < tokens->size(); ++i)
49 ASSERT(tokens->at(i).isSafeToSendToAnotherThread());
50 }
51
52 #endif
53
54 base::WeakPtr<BackgroundHTMLParser> BackgroundHTMLParser::create(PassOwnPtr<Back groundHTMLParser::Configuration> config)
55 {
56 // Caller must free by calling stop().
57 BackgroundHTMLParser* parser = new BackgroundHTMLParser(config);
58 return parser->m_weakFactory.GetWeakPtr();
59 }
60
61 BackgroundHTMLParser::BackgroundHTMLParser(PassOwnPtr<Configuration> config)
62 : m_state(InitialState)
63 , m_token(adoptPtr(new HTMLToken))
64 , m_tokenizer(HTMLTokenizer::create())
65 , m_parser(config->parser)
66 , m_pendingTokens(adoptPtr(new CompactHTMLTokenStream))
67 , m_decoder(TextResourceDecoder::create())
68 , m_source(config->source.Pass())
69 , m_weakFactory(this)
70 {
71 }
72
73 BackgroundHTMLParser::~BackgroundHTMLParser()
74 {
75 }
76
77 void BackgroundHTMLParser::start()
78 {
79 m_drainer = adoptPtr(new mojo::common::DataPipeDrainer(this, m_source.Pass() ));
80 }
81
82 void BackgroundHTMLParser::stop()
83 {
84 delete this;
85 }
86
87 void BackgroundHTMLParser::OnDataAvailable(const void* data, size_t numberOfByte s)
88 {
89 ASSERT(!m_input.isClosed());
90 String input = m_decoder->decode(static_cast<const char*>(data), numberOfByt es);
91 m_input.append(SegmentedString(input));
92 pumpTokenizer();
93 }
94
95 void BackgroundHTMLParser::OnDataComplete()
96 {
97 ASSERT(!m_input.isClosed());
98 finish();
99 }
100
101 void BackgroundHTMLParser::finish()
102 {
103 m_input.append(SegmentedString(m_decoder->flush()));
104 markEndOfFile();
105 pumpTokenizer();
106 }
107
108 void BackgroundHTMLParser::markEndOfFile()
109 {
110 ASSERT(!m_input.isClosed());
111 m_input.append(SegmentedString(String(&kEndOfFileMarker, 1)));
112 m_input.close();
113 }
114
115 BackgroundHTMLParser::ContinueBehavior BackgroundHTMLParser::updateTokenizerStat e(const CompactHTMLToken& lastToken)
116 {
117 if (lastToken.type() == HTMLToken::StartTag) {
118 const String& tagName = lastToken.data();
119
120 if (threadSafeMatch(tagName, HTMLNames::scriptTag) || threadSafeMatch(ta gName, HTMLNames::styleTag))
121 m_tokenizer->setState(HTMLTokenizer::RawDataState);
122
123 if (threadSafeMatch(tagName, HTMLNames::importTag)) {
124 m_state = DidSeeImportState;
125 return ContinueParsing;
126 }
127
128 if (m_state == InitialState)
129 return ContinueParsing;
130
131 // If we hit a start tag which is not an <import> tag while in the
132 // have-seen-import state, we need to send all but the last token.
133 // This lets the main thread see all import tags in one chunk.
134 // TODO(eseidel): We could replace this with a preloader and then
135 // simply yield after every </import> regardless.
136 m_state = InitialState;
137 return SendTokensExceptingLast;
138 }
139
140 // We send all tokens at the end of every </script>
141 if (lastToken.type() == HTMLToken::EndTag && threadSafeMatch(lastToken.data( ), HTMLNames::scriptTag))
142 return SendTokensIncludingLast;
143
144 return ContinueParsing;
145 }
146
147 void BackgroundHTMLParser::pumpTokenizer()
148 {
149 while (true) {
150 if (!m_tokenizer->nextToken(m_input, *m_token)) {
151 // We've reached the end of our current input.
152 sendTokensToMainThread();
153 break;
154 }
155
156 ContinueBehavior result;
157 {
158 CompactHTMLToken token(m_token.get(), TextPosition(m_input.currentLi ne(), m_input.currentColumn()));
159 result = updateTokenizerState(token);
160 if (result == SendTokensExceptingLast)
161 sendTokensToMainThread();
162
163 m_pendingTokens->append(token);
164 }
165
166 m_token->clear();
167
168 if (result == SendTokensIncludingLast || m_pendingTokens->size() >= pend ingTokenLimit)
169 sendTokensToMainThread();
170 }
171 }
172
173 void BackgroundHTMLParser::sendTokensToMainThread()
174 {
175 if (m_pendingTokens->isEmpty())
176 return;
177
178 #if ENABLE(ASSERT)
179 checkThatTokensAreSafeToSendToAnotherThread(m_pendingTokens.get());
180 #endif
181
182 OwnPtr<HTMLDocumentParser::ParsedChunk> chunk = adoptPtr(new HTMLDocumentPar ser::ParsedChunk);
183 chunk->tokens = m_pendingTokens.release();
184 Platform::current()->mainThreadTaskRunner()->PostTask(FROM_HERE,
185 base::Bind(&HTMLDocumentParser::didReceiveParsedChunkFromBackgroundParse r, m_parser, chunk.release()));
186
187 m_pendingTokens = adoptPtr(new CompactHTMLTokenStream);
188 }
189
190 }
OLDNEW
« no previous file with comments | « sky/engine/core/html/parser/BackgroundHTMLParser.h ('k') | sky/engine/core/html/parser/CompactHTMLToken.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698