| 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 #ifndef ScriptStreamer_h | 5 #ifndef ScriptStreamerImpl_h |
| 6 #define ScriptStreamer_h | 6 #define ScriptStreamerImpl_h |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "bindings/core/v8/ScriptStreamer.h" |
| 10 #include "core/CoreExport.h" | 11 #include "core/CoreExport.h" |
| 11 #include "platform/heap/Handle.h" | 12 #include "platform/heap/Handle.h" |
| 12 #include "platform/wtf/Noncopyable.h" | 13 #include "platform/wtf/Noncopyable.h" |
| 13 #include "platform/wtf/text/WTFString.h" | 14 #include "platform/wtf/text/WTFString.h" |
| 14 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
| 15 | 16 |
| 16 namespace blink { | 17 namespace blink { |
| 17 | 18 |
| 18 class PendingScript; | 19 class PendingScript; |
| 19 class Resource; | 20 class Resource; |
| 20 class ScriptResource; | 21 class ScriptResource; |
| 21 class ScriptState; | 22 class ScriptState; |
| 22 class Settings; | 23 class Settings; |
| 23 class SourceStream; | 24 class SourceStream; |
| 24 class WebTaskRunner; | 25 class WebTaskRunner; |
| 25 | 26 |
| 26 // ScriptStreamer streams incomplete script data to V8 so that it can be parsed | 27 class CORE_EXPORT ScriptStreamerImpl final : public ScriptStreamer { |
| 27 // while it's loaded. PendingScript holds a reference to ScriptStreamer. At the | 28 WTF_MAKE_NONCOPYABLE(ScriptStreamerImpl); |
| 28 // moment, ScriptStreamer is only used for parser blocking scripts; this means | |
| 29 // that the Document stays stable and no other scripts are executing while we're | |
| 30 // streaming. It is possible, though, that Document and the PendingScript are | |
| 31 // destroyed while the streaming is in progress, and ScriptStreamer handles it | |
| 32 // gracefully. | |
| 33 class CORE_EXPORT ScriptStreamer final | |
| 34 : public GarbageCollectedFinalized<ScriptStreamer> { | |
| 35 WTF_MAKE_NONCOPYABLE(ScriptStreamer); | |
| 36 | 29 |
| 37 public: | 30 public: |
| 38 enum Type { kParsingBlocking, kDeferred, kAsync }; | 31 ~ScriptStreamerImpl(); |
| 39 | |
| 40 ~ScriptStreamer(); | |
| 41 DECLARE_TRACE(); | 32 DECLARE_TRACE(); |
| 42 | 33 |
| 43 // Launches a task (on a background thread) which will stream the given | 34 // Launches a task (on a background thread) which will stream the given |
| 44 // PendingScript into V8 as it loads. | 35 // PendingScript into V8 as it loads. |
| 45 static void StartStreaming(PendingScript*, | 36 static void StartStreaming(PendingScript*, |
| 46 Type, | 37 Type, |
| 47 Settings*, | 38 Settings*, |
| 48 ScriptState*, | 39 ScriptState*, |
| 49 RefPtr<WebTaskRunner>); | 40 RefPtr<WebTaskRunner>); |
| 50 | 41 |
| 51 // Returns false if we cannot stream the given encoding. | 42 // Returns false if we cannot stream the given encoding. |
| 52 static bool ConvertEncoding(const char* encoding_name, | 43 static bool ConvertEncoding(const char* encoding_name, |
| 53 v8::ScriptCompiler::StreamedSource::Encoding*); | 44 v8::ScriptCompiler::StreamedSource::Encoding*); |
| 54 | 45 |
| 55 bool IsFinished() const; | 46 v8::ScriptCompiler::StreamedSource* Source() override { |
| 47 return source_.get(); |
| 48 } |
| 49 ScriptResource* GetResource() const override { return resource_; } |
| 56 | 50 |
| 57 v8::ScriptCompiler::StreamedSource* Source() { return source_.get(); } | |
| 58 ScriptResource* GetResource() const { return resource_; } | |
| 59 | |
| 60 // Called when the script is not needed any more (e.g., loading was | |
| 61 // cancelled). After calling cancel, PendingScript can drop its reference to | |
| 62 // ScriptStreamer, and ScriptStreamer takes care of eventually deleting | |
| 63 // itself (after the V8 side has finished too). | |
| 64 void Cancel(); | |
| 65 | |
| 66 // When the streaming is suppressed, the data is not given to V8, but | |
| 67 // ScriptStreamer still watches the resource load and notifies the upper | |
| 68 // layers when loading is finished. It is used in situations when we have | |
| 69 // started streaming but then we detect we don't want to stream (e.g., when | |
| 70 // we have the code cache for the script) and we still want to parse and | |
| 71 // execute it when it has finished loading. | |
| 72 void SuppressStreaming(); | 51 void SuppressStreaming(); |
| 73 bool StreamingSuppressed() const { return streaming_suppressed_; } | |
| 74 | |
| 75 // Called by PendingScript when data arrives from the network. | |
| 76 void NotifyAppendData(ScriptResource*); | |
| 77 void NotifyFinished(Resource*); | |
| 78 | 52 |
| 79 // Called by ScriptStreamingTask when it has streamed all data to V8 and V8 | 53 // Called by ScriptStreamingTask when it has streamed all data to V8 and V8 |
| 80 // has processed it. | 54 // has processed it. |
| 81 void StreamingCompleteOnBackgroundThread(); | 55 void StreamingCompleteOnBackgroundThread(); |
| 82 | 56 |
| 83 const String& ScriptURLString() const { return script_url_string_; } | 57 const String& ScriptURLString() const { return script_url_string_; } |
| 84 unsigned long ScriptResourceIdentifier() const { | 58 unsigned long ScriptResourceIdentifier() const { |
| 85 return script_resource_identifier_; | 59 return script_resource_identifier_; |
| 86 } | 60 } |
| 87 | 61 |
| 88 static void SetSmallScriptThresholdForTesting(size_t threshold) { | 62 static void SetSmallScriptThresholdForTesting(size_t threshold) { |
| 89 small_script_threshold_ = threshold; | 63 small_script_threshold_ = threshold; |
| 90 } | 64 } |
| 91 | 65 |
| 92 private: | 66 private: |
| 93 // Scripts whose first data chunk is smaller than this constant won't be | 67 // Scripts whose first data chunk is smaller than this constant won't be |
| 94 // streamed. Non-const for testing. | 68 // streamed. Non-const for testing. |
| 95 static size_t small_script_threshold_; | 69 static size_t small_script_threshold_; |
| 96 | 70 |
| 97 static ScriptStreamer* Create( | 71 static ScriptStreamerImpl* Create( |
| 98 PendingScript* script, | 72 PendingScript* script, |
| 99 Type script_type, | 73 Type script_type, |
| 100 ScriptState* script_state, | 74 ScriptState* script_state, |
| 101 v8::ScriptCompiler::CompileOptions compile_options, | 75 v8::ScriptCompiler::CompileOptions compile_options, |
| 102 RefPtr<WebTaskRunner> loading_task_runner) { | 76 RefPtr<WebTaskRunner> loading_task_runner) { |
| 103 return new ScriptStreamer(script, script_type, script_state, | 77 return new ScriptStreamerImpl(script, script_type, script_state, |
| 104 compile_options, std::move(loading_task_runner)); | 78 compile_options, |
| 79 std::move(loading_task_runner)); |
| 105 } | 80 } |
| 106 ScriptStreamer(PendingScript*, | 81 ScriptStreamerImpl(PendingScript*, |
| 107 Type, | 82 Type, |
| 108 ScriptState*, | 83 ScriptState*, |
| 109 v8::ScriptCompiler::CompileOptions, | 84 v8::ScriptCompiler::CompileOptions, |
| 110 RefPtr<WebTaskRunner>); | 85 RefPtr<WebTaskRunner>); |
| 111 | 86 |
| 112 void StreamingComplete(); | 87 void StreamingComplete(); |
| 113 void NotifyFinishedToClient(); | 88 void NotifyFinishedToClient(); |
| 114 | 89 |
| 90 bool IsFinished() const override; |
| 91 bool StreamingSuppressed() const override { return streaming_suppressed_; } |
| 92 void NotifyAppendData(ScriptResource*) override; |
| 93 void NotifyFinished(Resource*) override; |
| 94 void Cancel() override; |
| 95 |
| 115 static bool StartStreamingInternal(PendingScript*, | 96 static bool StartStreamingInternal(PendingScript*, |
| 116 Type, | 97 Type, |
| 117 Settings*, | 98 Settings*, |
| 118 ScriptState*, | 99 ScriptState*, |
| 119 RefPtr<WebTaskRunner>); | 100 RefPtr<WebTaskRunner>); |
| 120 | 101 |
| 121 Member<PendingScript> pending_script_; | 102 Member<PendingScript> pending_script_; |
| 122 // This pointer is weak. If PendingScript and its Resource are deleted | 103 // This pointer is weak. If PendingScript and its Resource are deleted |
| 123 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its | 104 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its |
| 124 // deletion by calling cancel(). | 105 // deletion by calling cancel(). |
| (...skipping 30 matching lines...) Expand all Loading... |
| 155 | 136 |
| 156 // Encoding of the streamed script. Saved for sanity checking purposes. | 137 // Encoding of the streamed script. Saved for sanity checking purposes. |
| 157 v8::ScriptCompiler::StreamedSource::Encoding encoding_; | 138 v8::ScriptCompiler::StreamedSource::Encoding encoding_; |
| 158 | 139 |
| 159 RefPtr<WebTaskRunner> loading_task_runner_; | 140 RefPtr<WebTaskRunner> loading_task_runner_; |
| 160 }; | 141 }; |
| 161 | 142 |
| 162 } // namespace blink | 143 } // namespace blink |
| 163 | 144 |
| 164 #endif // ScriptStreamer_h | 145 #endif // ScriptStreamer_h |
| OLD | NEW |