| 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 13 matching lines...) Expand all Loading... |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "core/html/parser/HTMLParserScriptRunner.h" | 26 #include "core/html/parser/HTMLParserScriptRunner.h" |
| 27 | 27 |
| 28 #include <inttypes.h> | 28 #include <inttypes.h> |
| 29 #include <memory> | 29 #include <memory> |
| 30 #include "bindings/core/v8/Microtask.h" | 30 #include "bindings/core/v8/Microtask.h" |
| 31 #include "bindings/core/v8/ScriptSourceCode.h" | 31 #include "bindings/core/v8/ScriptSourceCode.h" |
| 32 #include "bindings/core/v8/V8Binding.h" | 32 #include "bindings/core/v8/V8Binding.h" |
| 33 #include "bindings/core/v8/V8PerIsolateData.h" | 33 #include "bindings/core/v8/V8PerIsolateData.h" |
| 34 #include "core/dom/ClassicPendingScript.h" | |
| 35 #include "core/dom/ClassicScript.h" | 34 #include "core/dom/ClassicScript.h" |
| 36 #include "core/dom/DocumentParserTiming.h" | 35 #include "core/dom/DocumentParserTiming.h" |
| 37 #include "core/dom/Element.h" | 36 #include "core/dom/Element.h" |
| 38 #include "core/dom/IgnoreDestructiveWriteCountIncrementer.h" | 37 #include "core/dom/IgnoreDestructiveWriteCountIncrementer.h" |
| 39 #include "core/dom/ScriptLoader.h" | 38 #include "core/dom/ScriptLoader.h" |
| 40 #include "core/dom/TaskRunnerHelper.h" | 39 #include "core/dom/TaskRunnerHelper.h" |
| 41 #include "core/events/Event.h" | 40 #include "core/events/Event.h" |
| 42 #include "core/frame/LocalFrame.h" | 41 #include "core/frame/LocalFrame.h" |
| 43 #include "core/html/parser/HTMLInputStream.h" | 42 #include "core/html/parser/HTMLInputStream.h" |
| 44 #include "core/html/parser/HTMLParserScriptRunnerHost.h" | 43 #include "core/html/parser/HTMLParserScriptRunnerHost.h" |
| 45 #include "core/html/parser/NestingLevelIncrementer.h" | 44 #include "core/html/parser/NestingLevelIncrementer.h" |
| 46 #include "core/inspector/ConsoleMessage.h" | 45 #include "core/inspector/ConsoleMessage.h" |
| 47 #include "core/loader/resource/ScriptResource.h" | 46 #include "core/loader/resource/ScriptResource.h" |
| 48 #include "platform/Histogram.h" | 47 #include "platform/Histogram.h" |
| 49 #include "platform/WebFrameScheduler.h" | 48 #include "platform/WebFrameScheduler.h" |
| 50 #include "platform/instrumentation/tracing/TraceEvent.h" | 49 #include "platform/instrumentation/tracing/TraceEvent.h" |
| 51 #include "platform/instrumentation/tracing/TracedValue.h" | 50 #include "platform/instrumentation/tracing/TracedValue.h" |
| 51 #include "platform/loader/fetch/MemoryCache.h" |
| 52 #include "public/platform/Platform.h" | 52 #include "public/platform/Platform.h" |
| 53 | 53 |
| 54 namespace blink { | 54 namespace blink { |
| 55 | 55 |
| 56 namespace { | 56 namespace { |
| 57 | 57 |
| 58 // TODO(bmcquade): move this to a shared location if we find ourselves wanting | 58 // TODO(bmcquade): move this to a shared location if we find ourselves wanting |
| 59 // to trace similar data elsewhere in the codebase. | 59 // to trace similar data elsewhere in the codebase. |
| 60 std::unique_ptr<TracedValue> GetTraceArgsForScriptElement( | 60 std::unique_ptr<TracedValue> GetTraceArgsForScriptElement( |
| 61 ScriptElementBase* element, | 61 ScriptElementBase* element, |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 | 328 |
| 329 if (ParserBlockingScript() != pending_script) | 329 if (ParserBlockingScript() != pending_script) |
| 330 return; | 330 return; |
| 331 | 331 |
| 332 ScriptElementBase* element = ParserBlockingScript()->GetElement(); | 332 ScriptElementBase* element = ParserBlockingScript()->GetElement(); |
| 333 | 333 |
| 334 ScriptLoader* script_loader = element->Loader(); | 334 ScriptLoader* script_loader = element->Loader(); |
| 335 if (!script_loader || !script_loader->DisallowedFetchForDocWrittenScript()) | 335 if (!script_loader || !script_loader->DisallowedFetchForDocWrittenScript()) |
| 336 return; | 336 return; |
| 337 | 337 |
| 338 // We don't allow document.write() and its intervention with module scripts. | |
| 339 CHECK_EQ(pending_script->GetScriptType(), ScriptType::kClassic); | |
| 340 | |
| 341 if (!pending_script->ErrorOccurred()) { | 338 if (!pending_script->ErrorOccurred()) { |
| 342 EmitWarningForDocWriteScripts(pending_script->Url().GetString(), | 339 EmitWarningForDocWriteScripts( |
| 343 *document_); | 340 pending_script->GetResource()->Url().GetString(), *document_); |
| 344 return; | 341 return; |
| 345 } | 342 } |
| 346 | 343 |
| 347 // Due to dependency violation, not able to check the exact error to be | 344 // Due to dependency violation, not able to check the exact error to be |
| 348 // ERR_CACHE_MISS but other errors are rare with | 345 // ERR_CACHE_MISS but other errors are rare with |
| 349 // WebCachePolicy::ReturnCacheDataDontLoad. | 346 // WebCachePolicy::ReturnCacheDataDontLoad. |
| 350 | 347 |
| 351 EmitErrorForDocWriteScripts(pending_script->Url().GetString(), *document_); | 348 EmitErrorForDocWriteScripts(pending_script->GetResource()->Url().GetString(), |
| 349 *document_); |
| 352 TextPosition starting_position = ParserBlockingScript()->StartingPosition(); | 350 TextPosition starting_position = ParserBlockingScript()->StartingPosition(); |
| 353 bool is_parser_inserted = script_loader->IsParserInserted(); | 351 bool is_parser_inserted = script_loader->IsParserInserted(); |
| 354 // Remove this resource entry from memory cache as the new request | 352 // Remove this resource entry from memory cache as the new request |
| 355 // should not join onto this existing entry. | 353 // should not join onto this existing entry. |
| 356 pending_script->RemoveFromMemoryCache(); | 354 GetMemoryCache()->Remove(pending_script->GetResource()); |
| 357 FetchBlockedDocWriteScript(element, is_parser_inserted, starting_position); | 355 FetchBlockedDocWriteScript(element, is_parser_inserted, starting_position); |
| 358 } | 356 } |
| 359 | 357 |
| 360 void HTMLParserScriptRunner::PendingScriptFinished( | 358 void HTMLParserScriptRunner::PendingScriptFinished( |
| 361 PendingScript* pending_script) { | 359 PendingScript* pending_script) { |
| 362 // Handle cancellations of parser-blocking script loads without | 360 // Handle cancellations of parser-blocking script loads without |
| 363 // notifying the host (i.e., parser) if these were initiated by nested | 361 // notifying the host (i.e., parser) if these were initiated by nested |
| 364 // document.write()s. The cancellation may have been triggered by | 362 // document.write()s. The cancellation may have been triggered by |
| 365 // script execution to signal an abrupt stop (e.g., window.close().) | 363 // script execution to signal an abrupt stop (e.g., window.close().) |
| 366 // | 364 // |
| 367 // The parser is unprepared to be told, and doesn't need to be. | 365 // The parser is unprepared to be told, and doesn't need to be. |
| 368 if (IsExecutingScript() && pending_script->WasCanceled()) { | 366 if (IsExecutingScript() && pending_script->GetResource()->WasCanceled()) { |
| 369 pending_script->Dispose(); | 367 pending_script->Dispose(); |
| 370 | 368 |
| 371 if (pending_script == ParserBlockingScript()) { | 369 if (pending_script == ParserBlockingScript()) { |
| 372 parser_blocking_script_ = nullptr; | 370 parser_blocking_script_ = nullptr; |
| 373 } else { | 371 } else { |
| 374 CHECK_EQ(pending_script, scripts_to_execute_after_parsing_.front()); | 372 CHECK_EQ(pending_script, scripts_to_execute_after_parsing_.front()); |
| 375 | 373 |
| 376 // TODO(hiroshige): Remove this CHECK() before going to beta. | 374 // TODO(hiroshige): Remove this CHECK() before going to beta. |
| 377 // This is only to make clusterfuzz to find a test case that executes | 375 // This is only to make clusterfuzz to find a test case that executes |
| 378 // this code path. | 376 // this code path. |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 // Step 3 of https://html.spec.whatwg.org/#the-end: | 498 // Step 3 of https://html.spec.whatwg.org/#the-end: |
| 501 // "If the list of scripts that will execute when the document has | 499 // "If the list of scripts that will execute when the document has |
| 502 // finished parsing is not empty, run these substeps:" | 500 // finished parsing is not empty, run these substeps:" |
| 503 bool HTMLParserScriptRunner::ExecuteScriptsWaitingForParsing() { | 501 bool HTMLParserScriptRunner::ExecuteScriptsWaitingForParsing() { |
| 504 TRACE_EVENT0("blink", | 502 TRACE_EVENT0("blink", |
| 505 "HTMLParserScriptRunner::executeScriptsWaitingForParsing"); | 503 "HTMLParserScriptRunner::executeScriptsWaitingForParsing"); |
| 506 | 504 |
| 507 while (!scripts_to_execute_after_parsing_.IsEmpty()) { | 505 while (!scripts_to_execute_after_parsing_.IsEmpty()) { |
| 508 DCHECK(!IsExecutingScript()); | 506 DCHECK(!IsExecutingScript()); |
| 509 DCHECK(!HasParserBlockingScript()); | 507 DCHECK(!HasParserBlockingScript()); |
| 510 DCHECK(scripts_to_execute_after_parsing_.front()->IsExternal()); | 508 DCHECK(scripts_to_execute_after_parsing_.front()->GetResource()); |
| 511 | 509 |
| 512 // 1. "Spin the event loop until the first script in the list of scripts | 510 // 1. "Spin the event loop until the first script in the list of scripts |
| 513 // that will execute when the document has finished parsing | 511 // that will execute when the document has finished parsing |
| 514 // has its "ready to be parser-executed" flag set and | 512 // has its "ready to be parser-executed" flag set and |
| 515 // the parser's Document has no style sheet that is blocking scripts." | 513 // the parser's Document has no style sheet that is blocking scripts." |
| 516 // TODO(hiroshige): Is the latter part checked anywhere? | 514 // TODO(hiroshige): Is the latter part checked anywhere? |
| 517 if (!scripts_to_execute_after_parsing_.front()->IsReady()) { | 515 if (!scripts_to_execute_after_parsing_.front()->IsReady()) { |
| 518 scripts_to_execute_after_parsing_.front()->WatchForLoad(this); | 516 scripts_to_execute_after_parsing_.front()->WatchForLoad(this); |
| 519 TraceParserBlockingScript(scripts_to_execute_after_parsing_.front().Get(), | 517 TraceParserBlockingScript(scripts_to_execute_after_parsing_.front().Get(), |
| 520 !document_->IsScriptExecutionReady()); | 518 !document_->IsScriptExecutionReady()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 546 // 2nd Clause, Step 23 of https://html.spec.whatwg.org/#prepare-a-script | 544 // 2nd Clause, Step 23 of https://html.spec.whatwg.org/#prepare-a-script |
| 547 void HTMLParserScriptRunner::RequestParsingBlockingScript(Element* element) { | 545 void HTMLParserScriptRunner::RequestParsingBlockingScript(Element* element) { |
| 548 // "The element is the pending parsing-blocking script of the Document of | 546 // "The element is the pending parsing-blocking script of the Document of |
| 549 // the parser that created the element. | 547 // the parser that created the element. |
| 550 // (There can only be one such script per Document at a time.)" | 548 // (There can only be one such script per Document at a time.)" |
| 551 CHECK(!ParserBlockingScript()); | 549 CHECK(!ParserBlockingScript()); |
| 552 parser_blocking_script_ = RequestPendingScript(element); | 550 parser_blocking_script_ = RequestPendingScript(element); |
| 553 if (!ParserBlockingScript()) | 551 if (!ParserBlockingScript()) |
| 554 return; | 552 return; |
| 555 | 553 |
| 556 DCHECK(ParserBlockingScript()->IsExternal()); | 554 DCHECK(ParserBlockingScript()->GetResource()); |
| 557 | 555 |
| 558 // We only care about a load callback if resource is not already in the cache. | 556 // We only care about a load callback if resource is not already in the cache. |
| 559 // Callers will attempt to run the m_parserBlockingScript if possible before | 557 // Callers will attempt to run the m_parserBlockingScript if possible before |
| 560 // returning control to the parser. | 558 // returning control to the parser. |
| 561 if (!ParserBlockingScript()->IsReady()) { | 559 if (!ParserBlockingScript()->IsReady()) { |
| 562 parser_blocking_script_->StartStreamingIfPossible( | 560 parser_blocking_script_->StartStreamingIfPossible( |
| 563 document_, ScriptStreamer::kParsingBlocking); | 561 document_, ScriptStreamer::kParsingBlocking); |
| 564 parser_blocking_script_->WatchForLoad(this); | 562 parser_blocking_script_->WatchForLoad(this); |
| 565 } | 563 } |
| 566 } | 564 } |
| 567 | 565 |
| 568 // 1st Clause, Step 23 of https://html.spec.whatwg.org/#prepare-a-script | 566 // 1st Clause, Step 23 of https://html.spec.whatwg.org/#prepare-a-script |
| 569 void HTMLParserScriptRunner::RequestDeferredScript(Element* element) { | 567 void HTMLParserScriptRunner::RequestDeferredScript(Element* element) { |
| 570 PendingScript* pending_script = RequestPendingScript(element); | 568 PendingScript* pending_script = RequestPendingScript(element); |
| 571 if (!pending_script) | 569 if (!pending_script) |
| 572 return; | 570 return; |
| 573 | 571 |
| 574 if (!pending_script->IsReady()) { | 572 if (!pending_script->IsReady()) { |
| 575 pending_script->StartStreamingIfPossible(document_, | 573 pending_script->StartStreamingIfPossible(document_, |
| 576 ScriptStreamer::kDeferred); | 574 ScriptStreamer::kDeferred); |
| 577 } | 575 } |
| 578 | 576 |
| 579 DCHECK(pending_script->IsExternal()); | 577 DCHECK(pending_script->GetResource()); |
| 580 | 578 |
| 581 // "Add the element to the end of the list of scripts that will execute | 579 // "Add the element to the end of the list of scripts that will execute |
| 582 // when the document has finished parsing associated with the Document | 580 // when the document has finished parsing associated with the Document |
| 583 // of the parser that created the element." | 581 // of the parser that created the element." |
| 584 scripts_to_execute_after_parsing_.push_back(pending_script); | 582 scripts_to_execute_after_parsing_.push_back(pending_script); |
| 585 } | 583 } |
| 586 | 584 |
| 587 PendingScript* HTMLParserScriptRunner::RequestPendingScript( | 585 PendingScript* HTMLParserScriptRunner::RequestPendingScript( |
| 588 Element* element) const { | 586 Element* element) const { |
| 589 ScriptElementBase* script_element = | 587 ScriptElementBase* script_element = |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 } else if (script_loader->ReadyToBeParserExecuted()) { | 637 } else if (script_loader->ReadyToBeParserExecuted()) { |
| 640 // 5th Clause of Step 23. | 638 // 5th Clause of Step 23. |
| 641 // "If ... it's an HTML parser | 639 // "If ... it's an HTML parser |
| 642 // whose script nesting level is not greater than one" | 640 // whose script nesting level is not greater than one" |
| 643 if (reentry_permit_->ScriptNestingLevel() == 1u) { | 641 if (reentry_permit_->ScriptNestingLevel() == 1u) { |
| 644 // "The element is the pending parsing-blocking script of the | 642 // "The element is the pending parsing-blocking script of the |
| 645 // Document of the parser that created the element. | 643 // Document of the parser that created the element. |
| 646 // (There can only be one such script per Document at a time.)" | 644 // (There can only be one such script per Document at a time.)" |
| 647 CHECK(!parser_blocking_script_); | 645 CHECK(!parser_blocking_script_); |
| 648 parser_blocking_script_ = | 646 parser_blocking_script_ = |
| 649 ClassicPendingScript::Create(element, script_start_position); | 647 PendingScript::Create(element, script_start_position); |
| 650 } else { | 648 } else { |
| 651 // 6th Clause of Step 23. | 649 // 6th Clause of Step 23. |
| 652 // "Immediately execute the script block, | 650 // "Immediately execute the script block, |
| 653 // even if other scripts are already executing." | 651 // even if other scripts are already executing." |
| 654 // TODO(hiroshige): Merge the block into ScriptLoader::prepareScript(). | 652 // TODO(hiroshige): Merge the block into ScriptLoader::prepareScript(). |
| 655 DCHECK_GT(reentry_permit_->ScriptNestingLevel(), 1u); | 653 DCHECK_GT(reentry_permit_->ScriptNestingLevel(), 1u); |
| 656 if (parser_blocking_script_) | 654 if (parser_blocking_script_) |
| 657 parser_blocking_script_->Dispose(); | 655 parser_blocking_script_->Dispose(); |
| 658 parser_blocking_script_ = nullptr; | 656 parser_blocking_script_ = nullptr; |
| 659 ScriptSourceCode source_code(script->textContent(), | 657 ScriptSourceCode source_code(script->textContent(), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 679 | 677 |
| 680 DEFINE_TRACE(HTMLParserScriptRunner) { | 678 DEFINE_TRACE(HTMLParserScriptRunner) { |
| 681 visitor->Trace(document_); | 679 visitor->Trace(document_); |
| 682 visitor->Trace(host_); | 680 visitor->Trace(host_); |
| 683 visitor->Trace(parser_blocking_script_); | 681 visitor->Trace(parser_blocking_script_); |
| 684 visitor->Trace(scripts_to_execute_after_parsing_); | 682 visitor->Trace(scripts_to_execute_after_parsing_); |
| 685 PendingScriptClient::Trace(visitor); | 683 PendingScriptClient::Trace(visitor); |
| 686 } | 684 } |
| 687 | 685 |
| 688 } // namespace blink | 686 } // namespace blink |
| OLD | NEW |