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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 // bookmarkPosition: position of the bookmark. | 320 // bookmarkPosition: position of the bookmark. |
321 SourceStreamDataQueue data_queue_; // Thread safe. | 321 SourceStreamDataQueue data_queue_; // Thread safe. |
322 size_t queue_lead_position_; // Only used by v8 thread. | 322 size_t queue_lead_position_; // Only used by v8 thread. |
323 size_t queue_tail_position_; // Used by both threads; guarded by m_mutex. | 323 size_t queue_tail_position_; // Used by both threads; guarded by m_mutex. |
324 | 324 |
325 RefPtr<WebTaskRunner> loading_task_runner_; | 325 RefPtr<WebTaskRunner> loading_task_runner_; |
326 }; | 326 }; |
327 | 327 |
328 size_t ScriptStreamer::small_script_threshold_ = 30 * 1024; | 328 size_t ScriptStreamer::small_script_threshold_ = 30 * 1024; |
329 | 329 |
330 void ScriptStreamer::StartStreaming(PendingScript* script, | 330 void ScriptStreamer::StartStreaming(ClassicPendingScript* script, |
331 Type script_type, | 331 Type script_type, |
332 Settings* settings, | 332 Settings* settings, |
333 ScriptState* script_state, | 333 ScriptState* script_state, |
334 RefPtr<WebTaskRunner> loading_task_runner) { | 334 RefPtr<WebTaskRunner> loading_task_runner) { |
335 // We don't yet know whether the script will really be streamed. E.g., | 335 // We don't yet know whether the script will really be streamed. E.g., |
336 // suppressing streaming for short scripts is done later. Record only the | 336 // suppressing streaming for short scripts is done later. Record only the |
337 // sure negative cases here. | 337 // sure negative cases here. |
338 bool started_streaming = | 338 bool started_streaming = |
339 StartStreamingInternal(script, script_type, settings, script_state, | 339 StartStreamingInternal(script, script_type, settings, script_state, |
340 std::move(loading_task_runner)); | 340 std::move(loading_task_runner)); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 SuppressStreaming(); | 510 SuppressStreaming(); |
511 } | 511 } |
512 if (stream_) | 512 if (stream_) |
513 stream_->DidFinishLoading(); | 513 stream_->DidFinishLoading(); |
514 loading_finished_ = true; | 514 loading_finished_ = true; |
515 | 515 |
516 NotifyFinishedToClient(); | 516 NotifyFinishedToClient(); |
517 } | 517 } |
518 | 518 |
519 ScriptStreamer::ScriptStreamer( | 519 ScriptStreamer::ScriptStreamer( |
520 PendingScript* script, | 520 ClassicPendingScript* script, |
521 Type script_type, | 521 Type script_type, |
522 ScriptState* script_state, | 522 ScriptState* script_state, |
523 v8::ScriptCompiler::CompileOptions compile_options, | 523 v8::ScriptCompiler::CompileOptions compile_options, |
524 RefPtr<WebTaskRunner> loading_task_runner) | 524 RefPtr<WebTaskRunner> loading_task_runner) |
525 : pending_script_(script), | 525 : pending_script_(script), |
526 resource_(script->GetResource()), | 526 resource_(script->GetResource()), |
527 detached_(false), | 527 detached_(false), |
528 stream_(0), | 528 stream_(0), |
529 loading_finished_(false), | 529 loading_finished_(false), |
530 parsing_finished_(false), | 530 parsing_finished_(false), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 // parse error, the V8 side can complete before loading has finished. Send | 574 // parse error, the V8 side can complete before loading has finished. Send |
575 // the notification after both loading and V8 side operations have | 575 // the notification after both loading and V8 side operations have |
576 // completed. | 576 // completed. |
577 if (!IsFinished()) | 577 if (!IsFinished()) |
578 return; | 578 return; |
579 | 579 |
580 pending_script_->StreamingFinished(); | 580 pending_script_->StreamingFinished(); |
581 } | 581 } |
582 | 582 |
583 bool ScriptStreamer::StartStreamingInternal( | 583 bool ScriptStreamer::StartStreamingInternal( |
584 PendingScript* script, | 584 ClassicPendingScript* script, |
585 Type script_type, | 585 Type script_type, |
586 Settings* settings, | 586 Settings* settings, |
587 ScriptState* script_state, | 587 ScriptState* script_state, |
588 RefPtr<WebTaskRunner> loading_task_runner) { | 588 RefPtr<WebTaskRunner> loading_task_runner) { |
589 DCHECK(IsMainThread()); | 589 DCHECK(IsMainThread()); |
590 DCHECK(script_state->ContextIsValid()); | 590 DCHECK(script_state->ContextIsValid()); |
591 ScriptResource* resource = script->GetResource(); | 591 ScriptResource* resource = script->GetResource(); |
592 if (resource->IsLoaded()) { | 592 if (resource->IsLoaded()) { |
593 RecordNotStreamingReasonHistogram(script_type, kAlreadyLoaded); | 593 RecordNotStreamingReasonHistogram(script_type, kAlreadyLoaded); |
594 return false; | 594 return false; |
595 } | 595 } |
596 if (!resource->Url().ProtocolIsInHTTPFamily()) { | 596 if (!resource->Url().ProtocolIsInHTTPFamily()) { |
597 RecordNotStreamingReasonHistogram(script_type, kNotHTTP); | 597 RecordNotStreamingReasonHistogram(script_type, kNotHTTP); |
598 return false; | 598 return false; |
599 } | 599 } |
600 if (resource->IsCacheValidator()) { | 600 if (resource->IsCacheValidator()) { |
601 RecordNotStreamingReasonHistogram(script_type, kReload); | 601 RecordNotStreamingReasonHistogram(script_type, kReload); |
602 // This happens e.g., during reloads. We're actually not going to load | 602 // This happens e.g., during reloads. We're actually not going to load |
603 // the current Resource of the PendingScript but switch to another | 603 // the current Resource of the ClassicPendingScript but switch to another |
604 // Resource -> don't stream. | 604 // Resource -> don't stream. |
605 return false; | 605 return false; |
606 } | 606 } |
607 // We cannot filter out short scripts, even if we wait for the HTTP headers | 607 // We cannot filter out short scripts, even if we wait for the HTTP headers |
608 // to arrive: the Content-Length HTTP header is not sent for chunked | 608 // to arrive: the Content-Length HTTP header is not sent for chunked |
609 // downloads. | 609 // downloads. |
610 | 610 |
611 // Decide what kind of cached data we should produce while streaming. Only | 611 // Decide what kind of cached data we should produce while streaming. Only |
612 // produce parser cache if the non-streaming compile takes advantage of it. | 612 // produce parser cache if the non-streaming compile takes advantage of it. |
613 v8::ScriptCompiler::CompileOptions compile_option = | 613 v8::ScriptCompiler::CompileOptions compile_option = |
614 v8::ScriptCompiler::kNoCompileOptions; | 614 v8::ScriptCompiler::kNoCompileOptions; |
615 if (settings->GetV8CacheOptions() == kV8CacheOptionsParse) | 615 if (settings->GetV8CacheOptions() == kV8CacheOptionsParse) |
616 compile_option = v8::ScriptCompiler::kProduceParserCache; | 616 compile_option = v8::ScriptCompiler::kProduceParserCache; |
617 | 617 |
618 // The Resource might go out of scope if the script is no longer | 618 // The Resource might go out of scope if the script is no longer needed. |
619 // needed. This makes PendingScript notify the ScriptStreamer when it is | 619 // This makes ClassicPendingScript notify the ScriptStreamer when it is |
620 // destroyed. | 620 // destroyed. |
621 script->SetStreamer(ScriptStreamer::Create(script, script_type, script_state, | 621 script->SetStreamer(ScriptStreamer::Create(script, script_type, script_state, |
622 compile_option, | 622 compile_option, |
623 std::move(loading_task_runner))); | 623 std::move(loading_task_runner))); |
624 | 624 |
625 return true; | 625 return true; |
626 } | 626 } |
627 | 627 |
628 } // namespace blink | 628 } // namespace blink |
OLD | NEW |