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

Side by Side Diff: Source/core/html/parser/HTMLDocumentParser.cpp

Issue 779393002: Turn DocumentParser::pinToMainThread into a cleaner api (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years 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
1 /* 1 /*
2 * Copyright (C) 2010 Google, Inc. All Rights Reserved. 2 * Copyright (C) 2010 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
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 if (HTMLParserThread::shared()) 95 if (HTMLParserThread::shared())
96 return &HTMLParserThread::shared()->platformThread(); 96 return &HTMLParserThread::shared()->platformThread();
97 97
98 return nullptr; 98 return nullptr;
99 } 99 }
100 100
101 private: 101 private:
102 WeakPtr<BackgroundHTMLParser> m_backgroundParser; 102 WeakPtr<BackgroundHTMLParser> m_backgroundParser;
103 }; 103 };
104 104
105 HTMLDocumentParser::HTMLDocumentParser(HTMLDocument& document, bool reportErrors ) 105 HTMLDocumentParser::HTMLDocumentParser(HTMLDocument& document, bool reportErrors , ParserSynchronizationPolicy backgroundParsingPolicy)
106 : ScriptableDocumentParser(document) 106 : ScriptableDocumentParser(document)
107 , m_options(&document) 107 , m_options(&document)
108 , m_token(nullptr) 108 , m_token(backgroundParsingPolicy == ForceSynchronousParsing ? adoptPtr(new HTMLToken) : nullptr)
109 , m_tokenizer(nullptr) 109 , m_tokenizer(backgroundParsingPolicy == ForceSynchronousParsing ? HTMLToken izer::create(m_options) : nullptr)
110 , m_scriptRunner(HTMLScriptRunner::create(&document, this)) 110 , m_scriptRunner(HTMLScriptRunner::create(&document, this))
111 , m_treeBuilder(HTMLTreeBuilder::create(this, &document, parserContentPolicy (), reportErrors, m_options)) 111 , m_treeBuilder(HTMLTreeBuilder::create(this, &document, parserContentPolicy (), reportErrors, m_options))
112 , m_parserScheduler(HTMLParserScheduler::create(this)) 112 , m_parserScheduler(HTMLParserScheduler::create(this))
113 , m_xssAuditorDelegate(&document) 113 , m_xssAuditorDelegate(&document)
114 , m_weakFactory(this) 114 , m_weakFactory(this)
115 , m_preloader(HTMLResourcePreloader::create(document)) 115 , m_preloader(HTMLResourcePreloader::create(document))
116 , m_isPinnedToMainThread(false) 116 , m_shouldUseThreading(backgroundParsingPolicy == AllowAsynchronousParsing)
117 , m_endWasDelayed(false) 117 , m_endWasDelayed(false)
118 , m_haveBackgroundParser(false) 118 , m_haveBackgroundParser(false)
119 , m_tasksWereSuspended(false) 119 , m_tasksWereSuspended(false)
120 , m_pumpSessionNestingLevel(0) 120 , m_pumpSessionNestingLevel(0)
121 , m_pumpSpeculationsSessionNestingLevel(0) 121 , m_pumpSpeculationsSessionNestingLevel(0)
122 { 122 {
123 ASSERT(shouldUseThreading() || (m_token && m_tokenizer)); 123 ASSERT(shouldUseThreading() || (m_token && m_tokenizer));
124 } 124 }
125 125
126 // FIXME: Member variables should be grouped into self-initializing structs to 126 // FIXME: Member variables should be grouped into self-initializing structs to
127 // minimize code duplication between these constructors. 127 // minimize code duplication between these constructors.
128 HTMLDocumentParser::HTMLDocumentParser(DocumentFragment* fragment, Element* cont extElement, ParserContentPolicy parserContentPolicy) 128 HTMLDocumentParser::HTMLDocumentParser(DocumentFragment* fragment, Element* cont extElement, ParserContentPolicy parserContentPolicy)
129 : ScriptableDocumentParser(fragment->document(), parserContentPolicy) 129 : ScriptableDocumentParser(fragment->document(), parserContentPolicy)
130 , m_options(&fragment->document()) 130 , m_options(&fragment->document())
131 , m_token(adoptPtr(new HTMLToken)) 131 , m_token(adoptPtr(new HTMLToken))
132 , m_tokenizer(HTMLTokenizer::create(m_options)) 132 , m_tokenizer(HTMLTokenizer::create(m_options))
133 , m_treeBuilder(HTMLTreeBuilder::create(this, fragment, contextElement, this ->parserContentPolicy(), m_options)) 133 , m_treeBuilder(HTMLTreeBuilder::create(this, fragment, contextElement, this ->parserContentPolicy(), m_options))
134 , m_xssAuditorDelegate(&fragment->document()) 134 , m_xssAuditorDelegate(&fragment->document())
135 , m_weakFactory(this) 135 , m_weakFactory(this)
136 , m_isPinnedToMainThread(true) 136 , m_shouldUseThreading(false)
137 , m_endWasDelayed(false) 137 , m_endWasDelayed(false)
138 , m_haveBackgroundParser(false) 138 , m_haveBackgroundParser(false)
139 , m_tasksWereSuspended(false) 139 , m_tasksWereSuspended(false)
140 , m_pumpSessionNestingLevel(0) 140 , m_pumpSessionNestingLevel(0)
141 , m_pumpSpeculationsSessionNestingLevel(0) 141 , m_pumpSpeculationsSessionNestingLevel(0)
142 { 142 {
143 ASSERT(!shouldUseThreading());
144 bool reportErrors = false; // For now document fragment parsing never report s errors. 143 bool reportErrors = false; // For now document fragment parsing never report s errors.
145 m_tokenizer->setState(tokenizerStateForContextElement(contextElement, report Errors, m_options)); 144 m_tokenizer->setState(tokenizerStateForContextElement(contextElement, report Errors, m_options));
146 m_xssAuditor.initForFragment(); 145 m_xssAuditor.initForFragment();
147 } 146 }
148 147
149 HTMLDocumentParser::~HTMLDocumentParser() 148 HTMLDocumentParser::~HTMLDocumentParser()
150 { 149 {
151 #if ENABLE(OILPAN) 150 #if ENABLE(OILPAN)
152 if (m_haveBackgroundParser) 151 if (m_haveBackgroundParser)
153 stopBackgroundParser(); 152 stopBackgroundParser();
(...skipping 14 matching lines...) Expand all
168 void HTMLDocumentParser::trace(Visitor* visitor) 167 void HTMLDocumentParser::trace(Visitor* visitor)
169 { 168 {
170 visitor->trace(m_treeBuilder); 169 visitor->trace(m_treeBuilder);
171 visitor->trace(m_xssAuditorDelegate); 170 visitor->trace(m_xssAuditorDelegate);
172 visitor->trace(m_scriptRunner); 171 visitor->trace(m_scriptRunner);
173 visitor->trace(m_preloader); 172 visitor->trace(m_preloader);
174 ScriptableDocumentParser::trace(visitor); 173 ScriptableDocumentParser::trace(visitor);
175 HTMLScriptRunnerHost::trace(visitor); 174 HTMLScriptRunnerHost::trace(visitor);
176 } 175 }
177 176
178 void HTMLDocumentParser::pinToMainThread()
179 {
180 ASSERT(!m_haveBackgroundParser);
181 ASSERT(!m_isPinnedToMainThread);
182 m_isPinnedToMainThread = true;
183 if (!m_tokenizer) {
184 ASSERT(!m_token);
185 m_token = adoptPtr(new HTMLToken);
186 m_tokenizer = HTMLTokenizer::create(m_options);
187 }
188 }
189
190 void HTMLDocumentParser::detach() 177 void HTMLDocumentParser::detach()
191 { 178 {
192 if (m_haveBackgroundParser) 179 if (m_haveBackgroundParser)
193 stopBackgroundParser(); 180 stopBackgroundParser();
194 DocumentParser::detach(); 181 DocumentParser::detach();
195 if (m_scriptRunner) 182 if (m_scriptRunner)
196 m_scriptRunner->detach(); 183 m_scriptRunner->detach();
197 m_treeBuilder->detach(); 184 m_treeBuilder->detach();
198 // FIXME: It seems wrong that we would have a preload scanner here. 185 // FIXME: It seems wrong that we would have a preload scanner here.
199 // Yet during fast/dom/HTMLScriptElement/script-load-events.html we do. 186 // Yet during fast/dom/HTMLScriptElement/script-load-events.html we do.
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 } 257 }
271 258
272 bool HTMLDocumentParser::isScheduledForResume() const 259 bool HTMLDocumentParser::isScheduledForResume() const
273 { 260 {
274 return m_parserScheduler && m_parserScheduler->isScheduledForResume(); 261 return m_parserScheduler && m_parserScheduler->isScheduledForResume();
275 } 262 }
276 263
277 // Used by HTMLParserScheduler 264 // Used by HTMLParserScheduler
278 void HTMLDocumentParser::resumeParsingAfterYield() 265 void HTMLDocumentParser::resumeParsingAfterYield()
279 { 266 {
280 ASSERT(!m_isPinnedToMainThread); 267 ASSERT(shouldUseThreading());
281 ASSERT(m_haveBackgroundParser); 268 ASSERT(m_haveBackgroundParser);
282 269
283 // pumpPendingSpeculations can cause this parser to be detached from the Doc ument, 270 // pumpPendingSpeculations can cause this parser to be detached from the Doc ument,
284 // but we need to ensure it isn't deleted yet. 271 // but we need to ensure it isn't deleted yet.
285 RefPtrWillBeRawPtr<HTMLDocumentParser> protect(this); 272 RefPtrWillBeRawPtr<HTMLDocumentParser> protect(this);
286 pumpPendingSpeculations(); 273 pumpPendingSpeculations();
287 } 274 }
288 275
289 void HTMLDocumentParser::runScriptsForPausedTreeBuilder() 276 void HTMLDocumentParser::runScriptsForPausedTreeBuilder()
290 { 277 {
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after
790 777
791 m_input.appendToEnd(source); 778 m_input.appendToEnd(source);
792 779
793 if (inPumpSession()) { 780 if (inPumpSession()) {
794 // We've gotten data off the network in a nested write. 781 // We've gotten data off the network in a nested write.
795 // We don't want to consume any more of the input stream now. Do 782 // We don't want to consume any more of the input stream now. Do
796 // not worry. We'll consume this data in a less-nested write(). 783 // not worry. We'll consume this data in a less-nested write().
797 return; 784 return;
798 } 785 }
799 786
800 // A couple pinToMainThread() callers require synchronous parsing, but can't
801 // easily use the insert() method, so we hack append() for them to be synchr onous.
802 // javascript: url handling is one such caller.
803 // FIXME: This is gross, and we should separate the concept of synchronous p arsing
804 // from insert() so that only document.write() uses insert.
kbalazs 2014/12/05 23:42:04 This comment was pretty confusing also. No one els
805 ASSERT(m_isPinnedToMainThread);
806 pumpTokenizerIfPossible(); 787 pumpTokenizerIfPossible();
807
808 endIfDelayed(); 788 endIfDelayed();
809 } 789 }
810 790
811 void HTMLDocumentParser::end() 791 void HTMLDocumentParser::end()
812 { 792 {
813 ASSERT(!isDetached()); 793 ASSERT(!isDetached());
814 ASSERT(!isScheduledForResume()); 794 ASSERT(!isScheduledForResume());
815 795
816 if (m_haveBackgroundParser) 796 if (m_haveBackgroundParser)
817 stopBackgroundParser(); 797 stopBackgroundParser();
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
1001 // but we need to ensure it isn't deleted yet. 981 // but we need to ensure it isn't deleted yet.
1002 RefPtrWillBeRawPtr<HTMLDocumentParser> protect(this); 982 RefPtrWillBeRawPtr<HTMLDocumentParser> protect(this);
1003 m_scriptRunner->executeScriptsWaitingForResources(); 983 m_scriptRunner->executeScriptsWaitingForResources();
1004 if (!isWaitingForScripts()) 984 if (!isWaitingForScripts())
1005 resumeParsingAfterScriptExecution(); 985 resumeParsingAfterScriptExecution();
1006 } 986 }
1007 987
1008 void HTMLDocumentParser::parseDocumentFragment(const String& source, DocumentFra gment* fragment, Element* contextElement, ParserContentPolicy parserContentPolic y) 988 void HTMLDocumentParser::parseDocumentFragment(const String& source, DocumentFra gment* fragment, Element* contextElement, ParserContentPolicy parserContentPolic y)
1009 { 989 {
1010 RefPtrWillBeRawPtr<HTMLDocumentParser> parser = HTMLDocumentParser::create(f ragment, contextElement, parserContentPolicy); 990 RefPtrWillBeRawPtr<HTMLDocumentParser> parser = HTMLDocumentParser::create(f ragment, contextElement, parserContentPolicy);
1011 parser->insert(source); // Use insert() so that the parser will not yield. 991 parser->insert(source); // Use insert() so that the parser will not yield.
kouhei (in TOK) 2014/12/08 01:35:27 should we change this to append?
1012 parser->finish(); 992 parser->finish();
1013 ASSERT(!parser->processingData()); // Make sure we're done. <rdar://problem/ 3963151> 993 ASSERT(!parser->processingData()); // Make sure we're done. <rdar://problem/ 3963151>
1014 parser->detach(); // Allows ~DocumentParser to assert it was detached before destruction. 994 parser->detach(); // Allows ~DocumentParser to assert it was detached before destruction.
1015 } 995 }
1016 996
1017 void HTMLDocumentParser::suspendScheduledTasks() 997 void HTMLDocumentParser::suspendScheduledTasks()
1018 { 998 {
1019 ASSERT(!m_tasksWereSuspended); 999 ASSERT(!m_tasksWereSuspended);
1020 m_tasksWereSuspended = true; 1000 m_tasksWereSuspended = true;
1021 if (m_parserScheduler) 1001 if (m_parserScheduler)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 void HTMLDocumentParser::setDecoder(PassOwnPtr<TextResourceDecoder> decoder) 1045 void HTMLDocumentParser::setDecoder(PassOwnPtr<TextResourceDecoder> decoder)
1066 { 1046 {
1067 ASSERT(decoder); 1047 ASSERT(decoder);
1068 DecodedDataDocumentParser::setDecoder(decoder); 1048 DecodedDataDocumentParser::setDecoder(decoder);
1069 1049
1070 if (m_haveBackgroundParser) 1050 if (m_haveBackgroundParser)
1071 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::setDeco der, m_backgroundParser, takeDecoder())); 1051 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::setDeco der, m_backgroundParser, takeDecoder()));
1072 } 1052 }
1073 1053
1074 } 1054 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698