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