OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "bindings/core/v8/ScriptStreamer.h" | 5 #include "bindings/core/v8/ScriptStreamer.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include "bindings/core/v8/ScriptStreamerThread.h" | 8 #include "bindings/core/v8/ScriptStreamerThread.h" |
9 #include "bindings/core/v8/V8ScriptRunner.h" | 9 #include "bindings/core/v8/V8ScriptRunner.h" |
| 10 #include "core/dom/ClassicPendingScript.h" |
10 #include "core/dom/Document.h" | 11 #include "core/dom/Document.h" |
11 #include "core/dom/Element.h" | 12 #include "core/dom/Element.h" |
12 #include "core/dom/PendingScript.h" | |
13 #include "core/frame/Settings.h" | 13 #include "core/frame/Settings.h" |
14 #include "core/html/parser/TextResourceDecoder.h" | 14 #include "core/html/parser/TextResourceDecoder.h" |
15 #include "core/loader/resource/ScriptResource.h" | 15 #include "core/loader/resource/ScriptResource.h" |
16 #include "platform/CrossThreadFunctional.h" | 16 #include "platform/CrossThreadFunctional.h" |
17 #include "platform/Histogram.h" | 17 #include "platform/Histogram.h" |
18 #include "platform/SharedBuffer.h" | 18 #include "platform/SharedBuffer.h" |
19 #include "platform/instrumentation/tracing/TraceEvent.h" | 19 #include "platform/instrumentation/tracing/TraceEvent.h" |
20 #include "platform/loader/fetch/CachedMetadata.h" | 20 #include "platform/loader/fetch/CachedMetadata.h" |
21 #include "platform/wtf/Deque.h" | 21 #include "platform/wtf/Deque.h" |
22 #include "platform/wtf/PtrUtil.h" | 22 #include "platform/wtf/PtrUtil.h" |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 // bookmarkPosition: position of the bookmark. | 315 // bookmarkPosition: position of the bookmark. |
316 SourceStreamDataQueue m_dataQueue; // Thread safe. | 316 SourceStreamDataQueue m_dataQueue; // Thread safe. |
317 size_t m_queueLeadPosition; // Only used by v8 thread. | 317 size_t m_queueLeadPosition; // Only used by v8 thread. |
318 size_t m_queueTailPosition; // Used by both threads; guarded by m_mutex. | 318 size_t m_queueTailPosition; // Used by both threads; guarded by m_mutex. |
319 | 319 |
320 RefPtr<WebTaskRunner> m_loadingTaskRunner; | 320 RefPtr<WebTaskRunner> m_loadingTaskRunner; |
321 }; | 321 }; |
322 | 322 |
323 size_t ScriptStreamer::s_smallScriptThreshold = 30 * 1024; | 323 size_t ScriptStreamer::s_smallScriptThreshold = 30 * 1024; |
324 | 324 |
325 void ScriptStreamer::startStreaming(PendingScript* script, | 325 void ScriptStreamer::startStreaming(ClassicPendingScript* script, |
326 Type scriptType, | 326 Type scriptType, |
327 Settings* settings, | 327 Settings* settings, |
328 ScriptState* scriptState, | 328 ScriptState* scriptState, |
329 RefPtr<WebTaskRunner> loadingTaskRunner) { | 329 RefPtr<WebTaskRunner> loadingTaskRunner) { |
330 // We don't yet know whether the script will really be streamed. E.g., | 330 // We don't yet know whether the script will really be streamed. E.g., |
331 // suppressing streaming for short scripts is done later. Record only the | 331 // suppressing streaming for short scripts is done later. Record only the |
332 // sure negative cases here. | 332 // sure negative cases here. |
333 bool startedStreaming = startStreamingInternal( | 333 bool startedStreaming = startStreamingInternal( |
334 script, scriptType, settings, scriptState, std::move(loadingTaskRunner)); | 334 script, scriptType, settings, scriptState, std::move(loadingTaskRunner)); |
335 if (!startedStreaming) | 335 if (!startedStreaming) |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 suppressStreaming(); | 504 suppressStreaming(); |
505 } | 505 } |
506 if (m_stream) | 506 if (m_stream) |
507 m_stream->didFinishLoading(); | 507 m_stream->didFinishLoading(); |
508 m_loadingFinished = true; | 508 m_loadingFinished = true; |
509 | 509 |
510 notifyFinishedToClient(); | 510 notifyFinishedToClient(); |
511 } | 511 } |
512 | 512 |
513 ScriptStreamer::ScriptStreamer( | 513 ScriptStreamer::ScriptStreamer( |
514 PendingScript* script, | 514 ClassicPendingScript* script, |
515 Type scriptType, | 515 Type scriptType, |
516 ScriptState* scriptState, | 516 ScriptState* scriptState, |
517 v8::ScriptCompiler::CompileOptions compileOptions, | 517 v8::ScriptCompiler::CompileOptions compileOptions, |
518 RefPtr<WebTaskRunner> loadingTaskRunner) | 518 RefPtr<WebTaskRunner> loadingTaskRunner) |
519 : m_pendingScript(script), | 519 : m_pendingScript(script), |
520 m_resource(script->resource()), | 520 m_resource(script->resource()), |
521 m_detached(false), | 521 m_detached(false), |
522 m_stream(0), | 522 m_stream(0), |
523 m_loadingFinished(false), | 523 m_loadingFinished(false), |
524 m_parsingFinished(false), | 524 m_parsingFinished(false), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 // parse error, the V8 side can complete before loading has finished. Send | 568 // parse error, the V8 side can complete before loading has finished. Send |
569 // the notification after both loading and V8 side operations have | 569 // the notification after both loading and V8 side operations have |
570 // completed. | 570 // completed. |
571 if (!isFinished()) | 571 if (!isFinished()) |
572 return; | 572 return; |
573 | 573 |
574 m_pendingScript->streamingFinished(); | 574 m_pendingScript->streamingFinished(); |
575 } | 575 } |
576 | 576 |
577 bool ScriptStreamer::startStreamingInternal( | 577 bool ScriptStreamer::startStreamingInternal( |
578 PendingScript* script, | 578 ClassicPendingScript* script, |
579 Type scriptType, | 579 Type scriptType, |
580 Settings* settings, | 580 Settings* settings, |
581 ScriptState* scriptState, | 581 ScriptState* scriptState, |
582 RefPtr<WebTaskRunner> loadingTaskRunner) { | 582 RefPtr<WebTaskRunner> loadingTaskRunner) { |
583 DCHECK(isMainThread()); | 583 DCHECK(isMainThread()); |
584 DCHECK(scriptState->contextIsValid()); | 584 DCHECK(scriptState->contextIsValid()); |
585 ScriptResource* resource = script->resource(); | 585 ScriptResource* resource = script->resource(); |
586 if (resource->isLoaded()) { | 586 if (resource->isLoaded()) { |
587 recordNotStreamingReasonHistogram(scriptType, AlreadyLoaded); | 587 recordNotStreamingReasonHistogram(scriptType, AlreadyLoaded); |
588 return false; | 588 return false; |
589 } | 589 } |
590 if (!resource->url().protocolIsInHTTPFamily()) { | 590 if (!resource->url().protocolIsInHTTPFamily()) { |
591 recordNotStreamingReasonHistogram(scriptType, NotHTTP); | 591 recordNotStreamingReasonHistogram(scriptType, NotHTTP); |
592 return false; | 592 return false; |
593 } | 593 } |
594 if (resource->isCacheValidator()) { | 594 if (resource->isCacheValidator()) { |
595 recordNotStreamingReasonHistogram(scriptType, Reload); | 595 recordNotStreamingReasonHistogram(scriptType, Reload); |
596 // This happens e.g., during reloads. We're actually not going to load | 596 // This happens e.g., during reloads. We're actually not going to load |
597 // the current Resource of the PendingScript but switch to another | 597 // the current Resource of the ClassicPendingScript but switch to another |
598 // Resource -> don't stream. | 598 // Resource -> don't stream. |
599 return false; | 599 return false; |
600 } | 600 } |
601 // We cannot filter out short scripts, even if we wait for the HTTP headers | 601 // We cannot filter out short scripts, even if we wait for the HTTP headers |
602 // to arrive: the Content-Length HTTP header is not sent for chunked | 602 // to arrive: the Content-Length HTTP header is not sent for chunked |
603 // downloads. | 603 // downloads. |
604 | 604 |
605 // Decide what kind of cached data we should produce while streaming. Only | 605 // Decide what kind of cached data we should produce while streaming. Only |
606 // produce parser cache if the non-streaming compile takes advantage of it. | 606 // produce parser cache if the non-streaming compile takes advantage of it. |
607 v8::ScriptCompiler::CompileOptions compileOption = | 607 v8::ScriptCompiler::CompileOptions compileOption = |
608 v8::ScriptCompiler::kNoCompileOptions; | 608 v8::ScriptCompiler::kNoCompileOptions; |
609 if (settings->getV8CacheOptions() == V8CacheOptionsParse) | 609 if (settings->getV8CacheOptions() == V8CacheOptionsParse) |
610 compileOption = v8::ScriptCompiler::kProduceParserCache; | 610 compileOption = v8::ScriptCompiler::kProduceParserCache; |
611 | 611 |
612 // The Resource might go out of scope if the script is no longer | 612 // The Resource might go out of scope if the script is no longer |
613 // needed. This makes PendingScript notify the ScriptStreamer when it is | 613 // needed. This makes ClassicPendingScript notify the ScriptStreamer when it |
614 // destroyed. | 614 // is destroyed. |
615 script->setStreamer(ScriptStreamer::create(script, scriptType, scriptState, | 615 script->setStreamer(ScriptStreamer::create(script, scriptType, scriptState, |
616 compileOption, | 616 compileOption, |
617 std::move(loadingTaskRunner))); | 617 std::move(loadingTaskRunner))); |
618 | 618 |
619 return true; | 619 return true; |
620 } | 620 } |
621 | 621 |
622 } // namespace blink | 622 } // namespace blink |
OLD | NEW |