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