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