| OLD | NEW |
| 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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 m_preloadScanner.clear(); | 135 m_preloadScanner.clear(); |
| 136 m_parserScheduler.clear(); // Deleting the scheduler will clear any timers. | 136 m_parserScheduler.clear(); // Deleting the scheduler will clear any timers. |
| 137 } | 137 } |
| 138 | 138 |
| 139 void HTMLDocumentParser::stopParsing() | 139 void HTMLDocumentParser::stopParsing() |
| 140 { | 140 { |
| 141 DocumentParser::stopParsing(); | 141 DocumentParser::stopParsing(); |
| 142 m_parserScheduler.clear(); // Deleting the scheduler will clear any timers. | 142 m_parserScheduler.clear(); // Deleting the scheduler will clear any timers. |
| 143 } | 143 } |
| 144 | 144 |
| 145 // This kicks off "Once the user agent stops parsing" as described by: | |
| 146 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#the-
end | |
| 147 void HTMLDocumentParser::prepareToStopParsing() | |
| 148 { | |
| 149 ASSERT(!hasInsertionPoint()); | |
| 150 | |
| 151 // pumpTokenizer can cause this parser to be detached from the Document, | |
| 152 // but we need to ensure it isn't deleted yet. | |
| 153 RefPtr<HTMLDocumentParser> protect(this); | |
| 154 | |
| 155 // FIXME: Set the current document readiness to "interactive". | |
| 156 | |
| 157 // NOTE: This pump should only ever emit buffered character tokens, | |
| 158 // so ForceSynchronous vs. AllowYield should be meaningless. | |
| 159 pumpTokenizerIfPossible(ForceSynchronous); | |
| 160 | |
| 161 DocumentParser::prepareToStopParsing(); | |
| 162 if (m_scriptRunner && !m_scriptRunner->executeScriptsWaitingForParsing()) | |
| 163 return; | |
| 164 end(); | |
| 165 } | |
| 166 | |
| 167 bool HTMLDocumentParser::processingData() const | 145 bool HTMLDocumentParser::processingData() const |
| 168 { | 146 { |
| 169 return isScheduledForResume() || inWrite(); | 147 return isScheduledForResume() || inWrite(); |
| 170 } | 148 } |
| 171 | 149 |
| 172 void HTMLDocumentParser::pumpTokenizerIfPossible(SynchronousMode mode) | 150 void HTMLDocumentParser::pumpTokenizerIfPossible(SynchronousMode mode) |
| 173 { | 151 { |
| 174 if (isStopped() || m_treeBuilder->isPaused()) | 152 if (m_parserStopped || m_treeBuilder->isPaused()) |
| 175 return; | 153 return; |
| 176 | 154 |
| 177 // Once a resume is scheduled, HTMLParserScheduler controls when we next pum
p. | 155 // Once a resume is scheduled, HTMLParserScheduler controls when we next pum
p. |
| 178 if (isScheduledForResume()) { | 156 if (isScheduledForResume()) { |
| 179 ASSERT(mode == AllowYield); | 157 ASSERT(mode == AllowYield); |
| 180 return; | 158 return; |
| 181 } | 159 } |
| 182 | 160 |
| 183 pumpTokenizer(mode); | 161 pumpTokenizer(mode); |
| 184 } | 162 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 208 int scriptStartLine = 0; | 186 int scriptStartLine = 0; |
| 209 RefPtr<Element> scriptElement = m_treeBuilder->takeScriptToProcess(scriptSta
rtLine); | 187 RefPtr<Element> scriptElement = m_treeBuilder->takeScriptToProcess(scriptSta
rtLine); |
| 210 // We will not have a scriptRunner when parsing a DocumentFragment. | 188 // We will not have a scriptRunner when parsing a DocumentFragment. |
| 211 if (!m_scriptRunner) | 189 if (!m_scriptRunner) |
| 212 return true; | 190 return true; |
| 213 return m_scriptRunner->execute(scriptElement.release(), scriptStartLine); | 191 return m_scriptRunner->execute(scriptElement.release(), scriptStartLine); |
| 214 } | 192 } |
| 215 | 193 |
| 216 void HTMLDocumentParser::pumpTokenizer(SynchronousMode mode) | 194 void HTMLDocumentParser::pumpTokenizer(SynchronousMode mode) |
| 217 { | 195 { |
| 218 ASSERT(!isStopped()); | 196 ASSERT(!isDetached()); |
| 197 ASSERT(!m_parserStopped); |
| 219 ASSERT(!m_treeBuilder->isPaused()); | 198 ASSERT(!m_treeBuilder->isPaused()); |
| 220 ASSERT(!isScheduledForResume()); | 199 ASSERT(!isScheduledForResume()); |
| 221 // ASSERT that this object is both attached to the Document and protected. | 200 // ASSERT that this object is both attached to the Document and protected. |
| 222 ASSERT(refCount() >= 2); | 201 ASSERT(refCount() >= 2); |
| 223 | 202 |
| 224 // We tell the InspectorTimelineAgent about every pump, even if we | 203 // We tell the InspectorTimelineAgent about every pump, even if we |
| 225 // end up pumping nothing. It can filter out empty pumps itself. | 204 // end up pumping nothing. It can filter out empty pumps itself. |
| 226 willPumpLexer(); | 205 willPumpLexer(); |
| 227 | 206 |
| 228 HTMLParserScheduler::PumpSession session; | 207 HTMLParserScheduler::PumpSession session; |
| 229 // FIXME: This loop body has is now too long and needs cleanup. | 208 // FIXME: This loop body has is now too long and needs cleanup. |
| 230 while (mode == ForceSynchronous || m_parserScheduler->shouldContinueParsing(
session)) { | 209 while (mode == ForceSynchronous || m_parserScheduler->shouldContinueParsing(
session)) { |
| 231 if (!m_tokenizer->nextToken(m_input.current(), m_token)) | 210 if (!m_tokenizer->nextToken(m_input.current(), m_token)) |
| 232 break; | 211 break; |
| 233 | 212 |
| 234 m_treeBuilder->constructTreeFromToken(m_token); | 213 m_treeBuilder->constructTreeFromToken(m_token); |
| 235 m_token.clear(); | 214 m_token.clear(); |
| 236 | 215 |
| 237 // JavaScript may have stopped or detached the parser. | 216 // JavaScript may have stopped or detached the parser. |
| 238 if (isStopped()) | 217 if (isDetached() || m_parserStopped) |
| 239 return; | 218 return; |
| 240 | 219 |
| 241 // The parser will pause itself when waiting on a script to load or run. | 220 // The parser will pause itself when waiting on a script to load or run. |
| 242 if (!m_treeBuilder->isPaused()) | 221 if (!m_treeBuilder->isPaused()) |
| 243 continue; | 222 continue; |
| 244 | 223 |
| 245 // If we're paused waiting for a script, we try to execute scripts befor
e continuing. | 224 // If we're paused waiting for a script, we try to execute scripts befor
e continuing. |
| 246 bool shouldContinueParsing = runScriptsForPausedTreeBuilder(); | 225 bool shouldContinueParsing = runScriptsForPausedTreeBuilder(); |
| 247 m_treeBuilder->setPaused(!shouldContinueParsing); | 226 m_treeBuilder->setPaused(!shouldContinueParsing); |
| 248 | 227 |
| 249 // JavaScript may have stopped or detached the parser. | 228 // JavaScript may have stopped or detached the parser. |
| 250 if (isStopped()) | 229 if (isDetached() || m_parserStopped) |
| 251 return; | 230 return; |
| 252 | 231 |
| 253 if (!shouldContinueParsing) | 232 if (!shouldContinueParsing) |
| 254 break; | 233 break; |
| 255 } | 234 } |
| 256 | 235 |
| 257 // Ensure we haven't been totally deref'ed after pumping. Any caller of this | 236 // Ensure we haven't been totally deref'ed after pumping. Any caller of this |
| 258 // function should be holding a RefPtr to this to ensure we weren't deleted. | 237 // function should be holding a RefPtr to this to ensure we weren't deleted. |
| 259 ASSERT(refCount() >= 1); | 238 ASSERT(refCount() >= 1); |
| 260 | 239 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 289 #endif | 268 #endif |
| 290 } | 269 } |
| 291 | 270 |
| 292 bool HTMLDocumentParser::hasInsertionPoint() | 271 bool HTMLDocumentParser::hasInsertionPoint() |
| 293 { | 272 { |
| 294 return m_input.hasInsertionPoint(); | 273 return m_input.hasInsertionPoint(); |
| 295 } | 274 } |
| 296 | 275 |
| 297 void HTMLDocumentParser::insert(const SegmentedString& source) | 276 void HTMLDocumentParser::insert(const SegmentedString& source) |
| 298 { | 277 { |
| 299 if (isStopped()) | 278 if (m_parserStopped) |
| 300 return; | 279 return; |
| 301 | 280 |
| 302 // pumpTokenizer can cause this parser to be detached from the Document, | 281 // pumpTokenizer can cause this parser to be detached from the Document, |
| 303 // but we need to ensure it isn't deleted yet. | 282 // but we need to ensure it isn't deleted yet. |
| 304 RefPtr<HTMLDocumentParser> protect(this); | 283 RefPtr<HTMLDocumentParser> protect(this); |
| 305 | 284 |
| 306 { | 285 { |
| 307 NestingLevelIncrementer nestingLevelIncrementer(m_writeNestingLevel); | 286 NestingLevelIncrementer nestingLevelIncrementer(m_writeNestingLevel); |
| 308 | 287 |
| 309 SegmentedString excludedLineNumberSource(source); | 288 SegmentedString excludedLineNumberSource(source); |
| 310 excludedLineNumberSource.setExcludeLineNumbers(); | 289 excludedLineNumberSource.setExcludeLineNumbers(); |
| 311 m_input.insertAtCurrentInsertionPoint(excludedLineNumberSource); | 290 m_input.insertAtCurrentInsertionPoint(excludedLineNumberSource); |
| 312 pumpTokenizerIfPossible(ForceSynchronous); | 291 pumpTokenizerIfPossible(ForceSynchronous); |
| 313 } | 292 } |
| 314 | 293 |
| 315 endIfDelayed(); | 294 endIfDelayed(); |
| 316 } | 295 } |
| 317 | 296 |
| 318 void HTMLDocumentParser::append(const SegmentedString& source) | 297 void HTMLDocumentParser::append(const SegmentedString& source) |
| 319 { | 298 { |
| 320 if (isStopped()) | 299 if (m_parserStopped) |
| 321 return; | 300 return; |
| 322 | 301 |
| 323 // pumpTokenizer can cause this parser to be detached from the Document, | 302 // pumpTokenizer can cause this parser to be detached from the Document, |
| 324 // but we need to ensure it isn't deleted yet. | 303 // but we need to ensure it isn't deleted yet. |
| 325 RefPtr<HTMLDocumentParser> protect(this); | 304 RefPtr<HTMLDocumentParser> protect(this); |
| 326 | 305 |
| 327 { | 306 { |
| 328 NestingLevelIncrementer nestingLevelIncrementer(m_writeNestingLevel); | 307 NestingLevelIncrementer nestingLevelIncrementer(m_writeNestingLevel); |
| 329 | 308 |
| 330 m_input.appendToEnd(source); | 309 m_input.appendToEnd(source); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 342 } | 321 } |
| 343 | 322 |
| 344 endIfDelayed(); | 323 endIfDelayed(); |
| 345 } | 324 } |
| 346 | 325 |
| 347 void HTMLDocumentParser::end() | 326 void HTMLDocumentParser::end() |
| 348 { | 327 { |
| 349 ASSERT(!isDetached()); | 328 ASSERT(!isDetached()); |
| 350 ASSERT(!isScheduledForResume()); | 329 ASSERT(!isScheduledForResume()); |
| 351 | 330 |
| 331 // pumpTokenizer can cause this parser to be detached from the Document, |
| 332 // but we need to ensure it isn't deleted yet. |
| 333 RefPtr<HTMLDocumentParser> protect(this); |
| 334 |
| 335 // NOTE: This pump should only ever emit buffered character tokens, |
| 336 // so ForceSynchronous vs. AllowYield should be meaningless. |
| 337 pumpTokenizerIfPossible(ForceSynchronous); |
| 338 |
| 352 // Informs the the rest of WebCore that parsing is really finished (and dele
tes this). | 339 // Informs the the rest of WebCore that parsing is really finished (and dele
tes this). |
| 353 m_treeBuilder->finished(); | 340 m_treeBuilder->finished(); |
| 354 } | 341 } |
| 355 | 342 |
| 356 void HTMLDocumentParser::attemptToEnd() | 343 void HTMLDocumentParser::attemptToEnd() |
| 357 { | 344 { |
| 358 // finish() indicates we will not receive any more data. If we are waiting o
n | 345 // finish() indicates we will not receive any more data. If we are waiting o
n |
| 359 // an external script to load, we can't finish parsing quite yet. | 346 // an external script to load, we can't finish parsing quite yet. |
| 360 | 347 |
| 361 if (shouldDelayEnd()) { | 348 if (shouldDelayEnd()) { |
| 362 m_endWasDelayed = true; | 349 m_endWasDelayed = true; |
| 363 return; | 350 return; |
| 364 } | 351 } |
| 365 prepareToStopParsing(); | 352 end(); |
| 366 } | 353 } |
| 367 | 354 |
| 368 void HTMLDocumentParser::endIfDelayed() | 355 void HTMLDocumentParser::endIfDelayed() |
| 369 { | 356 { |
| 370 // If we've already been detached, don't bother ending. | 357 // If we've already been detached, don't bother ending. |
| 371 if (isDetached()) | 358 if (isDetached()) |
| 372 return; | 359 return; |
| 373 | 360 |
| 374 if (!m_endWasDelayed || shouldDelayEnd()) | 361 if (!m_endWasDelayed || shouldDelayEnd()) |
| 375 return; | 362 return; |
| 376 | 363 |
| 377 m_endWasDelayed = false; | 364 m_endWasDelayed = false; |
| 378 prepareToStopParsing(); | 365 end(); |
| 379 } | 366 } |
| 380 | 367 |
| 381 void HTMLDocumentParser::finish() | 368 void HTMLDocumentParser::finish() |
| 382 { | 369 { |
| 383 // FIXME: We should ASSERT(!m_parserStopped) here, since it does not | 370 // FIXME: We should ASSERT(!m_parserStopped) here, since it does not |
| 384 // makes sense to call any methods on DocumentParser once it's been stopped. | 371 // makes sense to call any methods on DocumentParser once it's been stopped. |
| 385 // However, FrameLoader::stop calls Document::finishParsing unconditionally | 372 // However, FrameLoader::stop calls Document::finishParsing unconditionally |
| 386 // which in turn calls m_parser->finish(). | 373 // which in turn calls m_parser->finish(). |
| 387 | 374 |
| 388 // We're not going to get any more data off the network, so we tell the | 375 // We're not going to get any more data off the network, so we tell the |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 | 440 |
| 454 bool HTMLDocumentParser::shouldLoadExternalScriptFromSrc(const AtomicString& src
Value) | 441 bool HTMLDocumentParser::shouldLoadExternalScriptFromSrc(const AtomicString& src
Value) |
| 455 { | 442 { |
| 456 if (!xssAuditor()) | 443 if (!xssAuditor()) |
| 457 return true; | 444 return true; |
| 458 return xssAuditor()->canLoadExternalScriptFromSrc(srcValue); | 445 return xssAuditor()->canLoadExternalScriptFromSrc(srcValue); |
| 459 } | 446 } |
| 460 | 447 |
| 461 void HTMLDocumentParser::notifyFinished(CachedResource* cachedResource) | 448 void HTMLDocumentParser::notifyFinished(CachedResource* cachedResource) |
| 462 { | 449 { |
| 463 if (isStopping()) { | |
| 464 prepareToStopParsing(); | |
| 465 return; | |
| 466 } | |
| 467 | |
| 468 // pumpTokenizer can cause this parser to be detached from the Document, | 450 // pumpTokenizer can cause this parser to be detached from the Document, |
| 469 // but we need to ensure it isn't deleted yet. | 451 // but we need to ensure it isn't deleted yet. |
| 470 RefPtr<HTMLDocumentParser> protect(this); | 452 RefPtr<HTMLDocumentParser> protect(this); |
| 471 | 453 |
| 472 ASSERT(m_scriptRunner); | 454 ASSERT(m_scriptRunner); |
| 473 ASSERT(!inScriptExecution()); | 455 ASSERT(!inScriptExecution()); |
| 474 ASSERT(m_treeBuilder->isPaused()); | 456 ASSERT(m_treeBuilder->isPaused()); |
| 475 // Note: We only ever wait on one script at a time, so we always know this | 457 // Note: We only ever wait on one script at a time, so we always know this |
| 476 // is the one we were waiting on and can un-pause the tree builder. | 458 // is the one we were waiting on and can un-pause the tree builder. |
| 477 m_treeBuilder->setPaused(false); | 459 m_treeBuilder->setPaused(false); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 void HTMLDocumentParser::parseDocumentFragment(const String& source, DocumentFra
gment* fragment, Element* contextElement, FragmentScriptingPermission scriptingP
ermission) | 497 void HTMLDocumentParser::parseDocumentFragment(const String& source, DocumentFra
gment* fragment, Element* contextElement, FragmentScriptingPermission scriptingP
ermission) |
| 516 { | 498 { |
| 517 RefPtr<HTMLDocumentParser> parser = HTMLDocumentParser::create(fragment, con
textElement, scriptingPermission); | 499 RefPtr<HTMLDocumentParser> parser = HTMLDocumentParser::create(fragment, con
textElement, scriptingPermission); |
| 518 parser->insert(source); // Use insert() so that the parser will not yield. | 500 parser->insert(source); // Use insert() so that the parser will not yield. |
| 519 parser->finish(); | 501 parser->finish(); |
| 520 ASSERT(!parser->processingData()); // Make sure we're done. <rdar://problem/
3963151> | 502 ASSERT(!parser->processingData()); // Make sure we're done. <rdar://problem/
3963151> |
| 521 parser->detach(); // Allows ~DocumentParser to assert it was detached before
destruction. | 503 parser->detach(); // Allows ~DocumentParser to assert it was detached before
destruction. |
| 522 } | 504 } |
| 523 | 505 |
| 524 } | 506 } |
| OLD | NEW |