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 20 matching lines...) Expand all Loading... |
31 #include "core/dom/Element.h" | 31 #include "core/dom/Element.h" |
32 #include "core/html/HTMLDocument.h" | 32 #include "core/html/HTMLDocument.h" |
33 #include "core/html/parser/AtomicHTMLToken.h" | 33 #include "core/html/parser/AtomicHTMLToken.h" |
34 #include "core/html/parser/BackgroundHTMLParser.h" | 34 #include "core/html/parser/BackgroundHTMLParser.h" |
35 #include "core/html/parser/HTMLParserScheduler.h" | 35 #include "core/html/parser/HTMLParserScheduler.h" |
36 #include "core/html/parser/HTMLParserThread.h" | 36 #include "core/html/parser/HTMLParserThread.h" |
37 #include "core/html/parser/HTMLScriptRunner.h" | 37 #include "core/html/parser/HTMLScriptRunner.h" |
38 #include "core/html/parser/HTMLTreeBuilder.h" | 38 #include "core/html/parser/HTMLTreeBuilder.h" |
39 #include "core/inspector/InspectorInstrumentation.h" | 39 #include "core/inspector/InspectorInstrumentation.h" |
40 #include "core/frame/Frame.h" | 40 #include "core/frame/Frame.h" |
41 #include "platform/SharedBuffer.h" | |
42 #include "platform/TraceEvent.h" | 41 #include "platform/TraceEvent.h" |
43 #include "wtf/Functional.h" | 42 #include "wtf/Functional.h" |
44 | 43 |
45 namespace WebCore { | 44 namespace WebCore { |
46 | 45 |
47 using namespace HTMLNames; | 46 using namespace HTMLNames; |
48 | 47 |
49 // This is a direct transcription of step 4 from: | 48 // This is a direct transcription of step 4 from: |
50 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#frag
ment-case | 49 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#frag
ment-case |
51 static HTMLTokenizer::State tokenizerStateForContextElement(Element* contextElem
ent, bool reportErrors, const HTMLParserOptions& options) | 50 static HTMLTokenizer::State tokenizerStateForContextElement(Element* contextElem
ent, bool reportErrors, const HTMLParserOptions& options) |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 // processParsedChunkFromBackgroundParser can cause this parser to be detach
ed from the Document, | 309 // processParsedChunkFromBackgroundParser can cause this parser to be detach
ed from the Document, |
311 // but we need to ensure it isn't deleted yet. | 310 // but we need to ensure it isn't deleted yet. |
312 RefPtr<HTMLDocumentParser> protect(this); | 311 RefPtr<HTMLDocumentParser> protect(this); |
313 | 312 |
314 ASSERT(m_speculations.isEmpty()); | 313 ASSERT(m_speculations.isEmpty()); |
315 chunk->preloads.clear(); // We don't need to preload because we're going to
parse immediately. | 314 chunk->preloads.clear(); // We don't need to preload because we're going to
parse immediately. |
316 m_speculations.append(chunk); | 315 m_speculations.append(chunk); |
317 pumpPendingSpeculations(); | 316 pumpPendingSpeculations(); |
318 } | 317 } |
319 | 318 |
320 void HTMLDocumentParser::didReceiveEncodingDataFromBackgroundParser(const Docume
ntEncodingData& data) | |
321 { | |
322 document()->setEncodingData(data); | |
323 } | |
324 | |
325 void HTMLDocumentParser::validateSpeculations(PassOwnPtr<ParsedChunk> chunk) | 319 void HTMLDocumentParser::validateSpeculations(PassOwnPtr<ParsedChunk> chunk) |
326 { | 320 { |
327 ASSERT(chunk); | 321 ASSERT(chunk); |
328 if (isWaitingForScripts()) { | 322 if (isWaitingForScripts()) { |
329 // We're waiting on a network script, just save the chunk, we'll get | 323 // We're waiting on a network script, just save the chunk, we'll get |
330 // a second validateSpeculations call after the script completes. | 324 // a second validateSpeculations call after the script completes. |
331 // This call should have been made immediately after runScriptsForPaused
TreeBuilder | 325 // This call should have been made immediately after runScriptsForPaused
TreeBuilder |
332 // which may have started a network load and left us waiting. | 326 // which may have started a network load and left us waiting. |
333 ASSERT(!m_lastChunkBeforeScript); | 327 ASSERT(!m_lastChunkBeforeScript); |
334 m_lastChunkBeforeScript = chunk; | 328 m_lastChunkBeforeScript = chunk; |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 | 659 |
666 RefPtr<WeakReference<BackgroundHTMLParser> > reference = WeakReference<Backg
roundHTMLParser>::createUnbound(); | 660 RefPtr<WeakReference<BackgroundHTMLParser> > reference = WeakReference<Backg
roundHTMLParser>::createUnbound(); |
667 m_backgroundParser = WeakPtr<BackgroundHTMLParser>(reference); | 661 m_backgroundParser = WeakPtr<BackgroundHTMLParser>(reference); |
668 | 662 |
669 OwnPtr<BackgroundHTMLParser::Configuration> config = adoptPtr(new Background
HTMLParser::Configuration); | 663 OwnPtr<BackgroundHTMLParser::Configuration> config = adoptPtr(new Background
HTMLParser::Configuration); |
670 config->options = m_options; | 664 config->options = m_options; |
671 config->parser = m_weakFactory.createWeakPtr(); | 665 config->parser = m_weakFactory.createWeakPtr(); |
672 config->xssAuditor = adoptPtr(new XSSAuditor); | 666 config->xssAuditor = adoptPtr(new XSSAuditor); |
673 config->xssAuditor->init(document(), &m_xssAuditorDelegate); | 667 config->xssAuditor->init(document(), &m_xssAuditorDelegate); |
674 config->preloadScanner = adoptPtr(new TokenPreloadScanner(document()->url().
copy(), document()->devicePixelRatio())); | 668 config->preloadScanner = adoptPtr(new TokenPreloadScanner(document()->url().
copy(), document()->devicePixelRatio())); |
675 config->decoder = takeDecoder(); | |
676 | 669 |
677 ASSERT(config->xssAuditor->isSafeToSendToAnotherThread()); | 670 ASSERT(config->xssAuditor->isSafeToSendToAnotherThread()); |
678 ASSERT(config->preloadScanner->isSafeToSendToAnotherThread()); | 671 ASSERT(config->preloadScanner->isSafeToSendToAnotherThread()); |
679 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::start, refe
rence.release(), config.release())); | 672 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::create, ref
erence.release(), config.release())); |
680 } | 673 } |
681 | 674 |
682 void HTMLDocumentParser::stopBackgroundParser() | 675 void HTMLDocumentParser::stopBackgroundParser() |
683 { | 676 { |
684 ASSERT(shouldUseThreading()); | 677 ASSERT(shouldUseThreading()); |
685 ASSERT(m_haveBackgroundParser); | 678 ASSERT(m_haveBackgroundParser); |
686 m_haveBackgroundParser = false; | 679 m_haveBackgroundParser = false; |
687 | 680 |
688 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::stop, m_bac
kgroundParser)); | 681 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::stop, m_bac
kgroundParser)); |
689 m_weakFactory.revokeAll(); | 682 m_weakFactory.revokeAll(); |
690 } | 683 } |
691 | 684 |
692 void HTMLDocumentParser::append(PassRefPtr<StringImpl> inputSource) | 685 void HTMLDocumentParser::append(PassRefPtr<StringImpl> inputSource) |
693 { | 686 { |
694 if (isStopped()) | 687 if (isStopped()) |
695 return; | 688 return; |
696 | 689 |
697 // We should never reach this point if we're using a parser thread, | 690 if (shouldUseThreading()) { |
698 // as appendBytes() will directly ship the data to the thread. | 691 if (!m_haveBackgroundParser) |
699 ASSERT(!shouldUseThreading()); | 692 startBackgroundParser(); |
| 693 |
| 694 ASSERT(inputSource->hasOneRef()); |
| 695 TRACE_EVENT1("net", "HTMLDocumentParser::append", "size", inputSource->l
ength()); |
| 696 // NOTE: Important that the String temporary is destroyed before we post
the task |
| 697 // otherwise the String could call deref() on a StringImpl now owned by
the background parser. |
| 698 // We would like to ASSERT(closure.arg3()->hasOneRef()) but sadly the ar
gs are private. |
| 699 Closure closure = bind(&BackgroundHTMLParser::append, m_backgroundParser
, String(inputSource)); |
| 700 HTMLParserThread::shared()->postTask(closure); |
| 701 return; |
| 702 } |
700 | 703 |
701 // pumpTokenizer can cause this parser to be detached from the Document, | 704 // pumpTokenizer can cause this parser to be detached from the Document, |
702 // but we need to ensure it isn't deleted yet. | 705 // but we need to ensure it isn't deleted yet. |
703 RefPtr<HTMLDocumentParser> protect(this); | 706 RefPtr<HTMLDocumentParser> protect(this); |
704 TRACE_EVENT1("net", "HTMLDocumentParser::append", "size", inputSource->lengt
h()); | 707 TRACE_EVENT1("net", "HTMLDocumentParser::append", "size", inputSource->lengt
h()); |
705 String source(inputSource); | 708 String source(inputSource); |
706 | 709 |
707 if (m_preloadScanner) { | 710 if (m_preloadScanner) { |
708 if (m_input.current().isEmpty() && !isWaitingForScripts()) { | 711 if (m_input.current().isEmpty() && !isWaitingForScripts()) { |
709 // We have parsed until the end of the current input and so are now
moving ahead of the preload scanner. | 712 // We have parsed until the end of the current input and so are now
moving ahead of the preload scanner. |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
955 if (m_parserScheduler) | 958 if (m_parserScheduler) |
956 m_parserScheduler->suspend(); | 959 m_parserScheduler->suspend(); |
957 } | 960 } |
958 | 961 |
959 void HTMLDocumentParser::resumeScheduledTasks() | 962 void HTMLDocumentParser::resumeScheduledTasks() |
960 { | 963 { |
961 if (m_parserScheduler) | 964 if (m_parserScheduler) |
962 m_parserScheduler->resume(); | 965 m_parserScheduler->resume(); |
963 } | 966 } |
964 | 967 |
965 void HTMLDocumentParser::appendBytes(const char* data, size_t length) | |
966 { | |
967 if (!length || isDetached()) | |
968 return; | |
969 | |
970 if (shouldUseThreading()) { | |
971 if (!m_haveBackgroundParser) | |
972 startBackgroundParser(); | |
973 | |
974 OwnPtr<Vector<char> > buffer = adoptPtr(new Vector<char>(length)); | |
975 memcpy(buffer->data(), data, length); | |
976 TRACE_EVENT1("net", "HTMLDocumentParser::appendBytes", "size", (unsigned
)length); | |
977 | |
978 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::appendB
ytes, m_backgroundParser, buffer.release())); | |
979 return; | |
980 } | |
981 | |
982 DecodedDataDocumentParser::appendBytes(data, length); | |
983 } | 968 } |
984 | |
985 void HTMLDocumentParser::flush() | |
986 { | |
987 // If we've got no decoder, we never received any data. | |
988 if (isDetached() || needsDecoder()) | |
989 return; | |
990 | |
991 if (m_haveBackgroundParser) | |
992 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::flush,
m_backgroundParser)); | |
993 else | |
994 DecodedDataDocumentParser::flush(); | |
995 } | |
996 | |
997 void HTMLDocumentParser::setDecoder(PassOwnPtr<TextResourceDecoder> decoder) | |
998 { | |
999 DecodedDataDocumentParser::setDecoder(decoder); | |
1000 | |
1001 if (m_haveBackgroundParser) | |
1002 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::setDeco
der, m_backgroundParser, takeDecoder())); | |
1003 } | |
1004 | |
1005 } | |
OLD | NEW |