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