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 "bindings/core/v8/ScriptStreamerThread.h" | 7 #include "bindings/core/v8/ScriptStreamerThread.h" |
8 #include "bindings/core/v8/V8ScriptRunner.h" | 8 #include "bindings/core/v8/V8ScriptRunner.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/dom/Element.h" | 10 #include "core/dom/Element.h" |
11 #include "core/dom/PendingScript.h" | 11 #include "core/dom/PendingScript.h" |
12 #include "core/fetch/ScriptResource.h" | 12 #include "core/fetch/ScriptResource.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 "platform/Histogram.h" | 15 #include "platform/Histogram.h" |
16 #include "platform/SharedBuffer.h" | 16 #include "platform/SharedBuffer.h" |
17 #include "platform/ThreadSafeFunctional.h" | 17 #include "platform/ThreadSafeFunctional.h" |
18 #include "platform/TraceEvent.h" | 18 #include "platform/TraceEvent.h" |
19 #include "public/platform/WebScheduler.h" | 19 #include "public/platform/WebScheduler.h" |
20 #include "wtf/Deque.h" | 20 #include "wtf/Deque.h" |
| 21 #include "wtf/PtrUtil.h" |
21 #include "wtf/text/TextEncodingRegistry.h" | 22 #include "wtf/text/TextEncodingRegistry.h" |
| 23 #include <memory> |
22 | 24 |
23 namespace blink { | 25 namespace blink { |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
27 void recordStartedStreamingHistogram(ScriptStreamer::Type scriptType, int reason
) | 29 void recordStartedStreamingHistogram(ScriptStreamer::Type scriptType, int reason
) |
28 { | 30 { |
29 switch (scriptType) { | 31 switch (scriptType) { |
30 case ScriptStreamer::ParsingBlocking: { | 32 case ScriptStreamer::ParsingBlocking: { |
31 DEFINE_STATIC_LOCAL(EnumerationHistogram, parseBlockingHistogram, ("WebC
ore.Scripts.ParsingBlocking.StartedStreaming", 2)); | 33 DEFINE_STATIC_LOCAL(EnumerationHistogram, parseBlockingHistogram, ("WebC
ore.Scripts.ParsingBlocking.StartedStreaming", 2)); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 WTF_MAKE_NONCOPYABLE(SourceStream); | 174 WTF_MAKE_NONCOPYABLE(SourceStream); |
173 public: | 175 public: |
174 explicit SourceStream(WebTaskRunner* loadingTaskRunner) | 176 explicit SourceStream(WebTaskRunner* loadingTaskRunner) |
175 : v8::ScriptCompiler::ExternalSourceStream() | 177 : v8::ScriptCompiler::ExternalSourceStream() |
176 , m_cancelled(false) | 178 , m_cancelled(false) |
177 , m_finished(false) | 179 , m_finished(false) |
178 , m_queueLeadPosition(0) | 180 , m_queueLeadPosition(0) |
179 , m_queueTailPosition(0) | 181 , m_queueTailPosition(0) |
180 , m_bookmarkPosition(0) | 182 , m_bookmarkPosition(0) |
181 , m_lengthOfBOM(0) | 183 , m_lengthOfBOM(0) |
182 , m_loadingTaskRunner(adoptPtr(loadingTaskRunner->clone())) | 184 , m_loadingTaskRunner(wrapUnique(loadingTaskRunner->clone())) |
183 { | 185 { |
184 } | 186 } |
185 | 187 |
186 virtual ~SourceStream() override { } | 188 virtual ~SourceStream() override { } |
187 | 189 |
188 // Called by V8 on a background thread. Should block until we can return | 190 // Called by V8 on a background thread. Should block until we can return |
189 // some data. | 191 // some data. |
190 size_t GetMoreData(const uint8_t** src) override | 192 size_t GetMoreData(const uint8_t** src) override |
191 { | 193 { |
192 ASSERT(!isMainThread()); | 194 ASSERT(!isMainThread()); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 // (i.e., without BOM) | 382 // (i.e., without BOM) |
381 // - m_queueTailPosition counts the bytes that Chrome has sent | 383 // - m_queueTailPosition counts the bytes that Chrome has sent |
382 // (i.e., with BOM) | 384 // (i.e., with BOM) |
383 // So when resetting the bookmark, we have to adjust the lead position | 385 // So when resetting the bookmark, we have to adjust the lead position |
384 // to account for the BOM (which happens implicitly in the regular | 386 // to account for the BOM (which happens implicitly in the regular |
385 // streaming case). | 387 // streaming case). |
386 // We store this separately, to avoid having to guard all | 388 // We store this separately, to avoid having to guard all |
387 // m_queueLeadPosition references with a mutex. | 389 // m_queueLeadPosition references with a mutex. |
388 size_t m_lengthOfBOM; // Used by both threads; guarded by m_mutex. | 390 size_t m_lengthOfBOM; // Used by both threads; guarded by m_mutex. |
389 | 391 |
390 OwnPtr<WebTaskRunner> m_loadingTaskRunner; | 392 std::unique_ptr<WebTaskRunner> m_loadingTaskRunner; |
391 }; | 393 }; |
392 | 394 |
393 size_t ScriptStreamer::s_smallScriptThreshold = 30 * 1024; | 395 size_t ScriptStreamer::s_smallScriptThreshold = 30 * 1024; |
394 | 396 |
395 void ScriptStreamer::startStreaming(PendingScript* script, Type scriptType, Sett
ings* settings, ScriptState* scriptState, WebTaskRunner* loadingTaskRunner) | 397 void ScriptStreamer::startStreaming(PendingScript* script, Type scriptType, Sett
ings* settings, ScriptState* scriptState, WebTaskRunner* loadingTaskRunner) |
396 { | 398 { |
397 // We don't yet know whether the script will really be streamed. E.g., | 399 // We don't yet know whether the script will really be streamed. E.g., |
398 // suppressing streaming for short scripts is done later. Record only the | 400 // suppressing streaming for short scripts is done later. Record only the |
399 // sure negative cases here. | 401 // sure negative cases here. |
400 bool startedStreaming = startStreamingInternal(script, scriptType, settings,
scriptState, loadingTaskRunner); | 402 bool startedStreaming = startStreamingInternal(script, scriptType, settings,
scriptState, loadingTaskRunner); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 // possible that resource->encoding() returns a different encoding | 495 // possible that resource->encoding() returns a different encoding |
494 // before the loading has started and after we got some data. In | 496 // before the loading has started and after we got some data. In |
495 // addition, check for byte order marks. Note that checking the byte | 497 // addition, check for byte order marks. Note that checking the byte |
496 // order mark might change the encoding. We cannot decode the full text | 498 // order mark might change the encoding. We cannot decode the full text |
497 // here, because it might contain incomplete UTF-8 characters. Also note | 499 // here, because it might contain incomplete UTF-8 characters. Also note |
498 // that have at least s_smallScriptThreshold worth of data, which is mor
e | 500 // that have at least s_smallScriptThreshold worth of data, which is mor
e |
499 // than enough for detecting a BOM. | 501 // than enough for detecting a BOM. |
500 const char* data = 0; | 502 const char* data = 0; |
501 size_t length = resource->resourceBuffer()->getSomeData(data, static_cas
t<size_t>(0)); | 503 size_t length = resource->resourceBuffer()->getSomeData(data, static_cas
t<size_t>(0)); |
502 | 504 |
503 OwnPtr<TextResourceDecoder> decoder(TextResourceDecoder::create("applica
tion/javascript", resource->encoding())); | 505 std::unique_ptr<TextResourceDecoder> decoder(TextResourceDecoder::create
("application/javascript", resource->encoding())); |
504 lengthOfBOM = decoder->checkForBOM(data, length); | 506 lengthOfBOM = decoder->checkForBOM(data, length); |
505 | 507 |
506 // Maybe the encoding changed because we saw the BOM; get the encoding | 508 // Maybe the encoding changed because we saw the BOM; get the encoding |
507 // from the decoder. | 509 // from the decoder. |
508 if (!convertEncoding(decoder->encoding().name(), &m_encoding)) { | 510 if (!convertEncoding(decoder->encoding().name(), &m_encoding)) { |
509 suppressStreaming(); | 511 suppressStreaming(); |
510 recordNotStreamingReasonHistogram(m_scriptType, EncodingNotSupported
); | 512 recordNotStreamingReasonHistogram(m_scriptType, EncodingNotSupported
); |
511 recordStartedStreamingHistogram(m_scriptType, 0); | 513 recordStartedStreamingHistogram(m_scriptType, 0); |
512 return; | 514 return; |
513 } | 515 } |
(...skipping 12 matching lines...) Expand all Loading... |
526 suppressStreaming(); | 528 suppressStreaming(); |
527 recordNotStreamingReasonHistogram(m_scriptType, ContextNotValid); | 529 recordNotStreamingReasonHistogram(m_scriptType, ContextNotValid); |
528 recordStartedStreamingHistogram(m_scriptType, 0); | 530 recordStartedStreamingHistogram(m_scriptType, 0); |
529 return; | 531 return; |
530 } | 532 } |
531 | 533 |
532 ASSERT(!m_stream); | 534 ASSERT(!m_stream); |
533 ASSERT(!m_source); | 535 ASSERT(!m_source); |
534 m_stream = new SourceStream(m_loadingTaskRunner.get()); | 536 m_stream = new SourceStream(m_loadingTaskRunner.get()); |
535 // m_source takes ownership of m_stream. | 537 // m_source takes ownership of m_stream. |
536 m_source = adoptPtr(new v8::ScriptCompiler::StreamedSource(m_stream, m_e
ncoding)); | 538 m_source = wrapUnique(new v8::ScriptCompiler::StreamedSource(m_stream, m
_encoding)); |
537 | 539 |
538 ScriptState::Scope scope(m_scriptState.get()); | 540 ScriptState::Scope scope(m_scriptState.get()); |
539 OwnPtr<v8::ScriptCompiler::ScriptStreamingTask> scriptStreamingTask(adop
tPtr(v8::ScriptCompiler::StartStreamingScript(m_scriptState->isolate(), m_source
.get(), m_compileOptions))); | 541 std::unique_ptr<v8::ScriptCompiler::ScriptStreamingTask> scriptStreaming
Task(wrapUnique(v8::ScriptCompiler::StartStreamingScript(m_scriptState->isolate(
), m_source.get(), m_compileOptions))); |
540 if (!scriptStreamingTask) { | 542 if (!scriptStreamingTask) { |
541 // V8 cannot stream the script. | 543 // V8 cannot stream the script. |
542 suppressStreaming(); | 544 suppressStreaming(); |
543 m_stream = 0; | 545 m_stream = 0; |
544 m_source.reset(); | 546 m_source.reset(); |
545 recordNotStreamingReasonHistogram(m_scriptType, V8CannotStream); | 547 recordNotStreamingReasonHistogram(m_scriptType, V8CannotStream); |
546 recordStartedStreamingHistogram(m_scriptType, 0); | 548 recordStartedStreamingHistogram(m_scriptType, 0); |
547 return; | 549 return; |
548 } | 550 } |
549 | 551 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 , m_loadingFinished(false) | 584 , m_loadingFinished(false) |
583 , m_parsingFinished(false) | 585 , m_parsingFinished(false) |
584 , m_haveEnoughDataForStreaming(false) | 586 , m_haveEnoughDataForStreaming(false) |
585 , m_streamingSuppressed(false) | 587 , m_streamingSuppressed(false) |
586 , m_compileOptions(compileOptions) | 588 , m_compileOptions(compileOptions) |
587 , m_scriptState(scriptState) | 589 , m_scriptState(scriptState) |
588 , m_scriptType(scriptType) | 590 , m_scriptType(scriptType) |
589 , m_scriptURLString(m_resource->url().copy().getString()) | 591 , m_scriptURLString(m_resource->url().copy().getString()) |
590 , m_scriptResourceIdentifier(m_resource->identifier()) | 592 , m_scriptResourceIdentifier(m_resource->identifier()) |
591 , m_encoding(v8::ScriptCompiler::StreamedSource::TWO_BYTE) // Unfortunately
there's no dummy encoding value in the enum; let's use one we don't stream. | 593 , m_encoding(v8::ScriptCompiler::StreamedSource::TWO_BYTE) // Unfortunately
there's no dummy encoding value in the enum; let's use one we don't stream. |
592 , m_loadingTaskRunner(adoptPtr(loadingTaskRunner->clone())) | 594 , m_loadingTaskRunner(wrapUnique(loadingTaskRunner->clone())) |
593 { | 595 { |
594 } | 596 } |
595 | 597 |
596 ScriptStreamer::~ScriptStreamer() | 598 ScriptStreamer::~ScriptStreamer() |
597 { | 599 { |
598 } | 600 } |
599 | 601 |
600 DEFINE_TRACE(ScriptStreamer) | 602 DEFINE_TRACE(ScriptStreamer) |
601 { | 603 { |
602 visitor->trace(m_pendingScript); | 604 visitor->trace(m_pendingScript); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 | 674 |
673 // The Resource might go out of scope if the script is no longer | 675 // The Resource might go out of scope if the script is no longer |
674 // needed. This makes PendingScript notify the ScriptStreamer when it is | 676 // needed. This makes PendingScript notify the ScriptStreamer when it is |
675 // destroyed. | 677 // destroyed. |
676 script->setStreamer(ScriptStreamer::create(script, scriptType, scriptState,
compileOption, loadingTaskRunner)); | 678 script->setStreamer(ScriptStreamer::create(script, scriptType, scriptState,
compileOption, loadingTaskRunner)); |
677 | 679 |
678 return true; | 680 return true; |
679 } | 681 } |
680 | 682 |
681 } // namespace blink | 683 } // namespace blink |
OLD | NEW |