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 "core/CoreExport.h" | 8 #include "core/CoreExport.h" |
9 #include "core/dom/PendingScript.h" | |
10 #include "platform/heap/Handle.h" | 9 #include "platform/heap/Handle.h" |
11 #include "wtf/RefCounted.h" | 10 #include "wtf/RefCounted.h" |
12 | 11 |
13 #include <v8.h> | 12 #include <v8.h> |
14 | 13 |
15 namespace blink { | 14 namespace blink { |
16 | 15 |
17 class PendingScript; | 16 class PendingScript; |
18 class Resource; | 17 class Resource; |
19 class ScriptResource; | 18 class ScriptResource; |
20 class ScriptResourceClient; | 19 class ScriptResourceClient; |
21 class ScriptState; | 20 class ScriptState; |
22 class Settings; | 21 class Settings; |
23 class SourceStream; | 22 class SourceStream; |
24 class WebTaskRunner; | 23 class WebTaskRunner; |
25 | 24 |
26 // ScriptStreamer streams incomplete script data to V8 so that it can be parsed | 25 // 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 | 26 // while it's loaded. PendingScript holds a reference to ScriptStreamer. At the |
28 // moment, ScriptStreamer is only used for parser blocking scripts; this means | 27 // 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 | 28 // 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 | 29 // streaming. It is possible, though, that Document and the PendingScript are |
31 // destroyed while the streaming is in progress, and ScriptStreamer handles it | 30 // destroyed while the streaming is in progress, and ScriptStreamer handles it |
32 // gracefully. | 31 // gracefully. |
33 class CORE_EXPORT ScriptStreamer final : public RefCountedWillBeRefCountedGarbag
eCollected<ScriptStreamer> { | 32 class CORE_EXPORT ScriptStreamer final : public RefCountedWillBeRefCountedGarbag
eCollected<ScriptStreamer> { |
34 WTF_MAKE_NONCOPYABLE(ScriptStreamer); | 33 WTF_MAKE_NONCOPYABLE(ScriptStreamer); |
35 public: | 34 public: |
36 static PassRefPtrWillBeRawPtr<ScriptStreamer> create(ScriptResource* resourc
e, PendingScript::Type scriptType, ScriptState* scriptState, v8::ScriptCompiler:
:CompileOptions compileOptions, WebTaskRunner* loadingTaskRunner) | 35 enum Type { |
| 36 ParsingBlocking, |
| 37 Deferred, |
| 38 Async |
| 39 }; |
| 40 |
| 41 static PassRefPtrWillBeRawPtr<ScriptStreamer> create(ScriptResource* resourc
e, Type scriptType, ScriptState* scriptState, v8::ScriptCompiler::CompileOptions
compileOptions, WebTaskRunner* loadingTaskRunner) |
37 { | 42 { |
38 return adoptRefWillBeNoop(new ScriptStreamer(resource, scriptType, scrip
tState, compileOptions, loadingTaskRunner)); | 43 return adoptRefWillBeNoop(new ScriptStreamer(resource, scriptType, scrip
tState, compileOptions, loadingTaskRunner)); |
39 } | 44 } |
40 | 45 |
41 ~ScriptStreamer(); | 46 ~ScriptStreamer(); |
42 DECLARE_TRACE(); | 47 DECLARE_TRACE(); |
43 | 48 |
44 // Launches a task (on a background thread) which will stream the given | 49 // Launches a task (on a background thread) which will stream the given |
45 // PendingScript into V8 as it loads. | 50 // PendingScript into V8 as it loads. |
46 static void startStreaming(PendingScript&, PendingScript::Type, Settings*, S
criptState*, WebTaskRunner*); | 51 static void startStreaming(PendingScript*, Type, Settings*, ScriptState*, We
bTaskRunner*); |
47 | 52 |
48 // Returns false if we cannot stream the given encoding. | 53 // Returns false if we cannot stream the given encoding. |
49 static bool convertEncoding(const char* encodingName, v8::ScriptCompiler::St
reamedSource::Encoding*); | 54 static bool convertEncoding(const char* encodingName, v8::ScriptCompiler::St
reamedSource::Encoding*); |
50 | 55 |
51 bool isFinished() const; | 56 bool isFinished() const; |
52 | 57 |
53 v8::ScriptCompiler::StreamedSource* source() { return m_source.get(); } | 58 v8::ScriptCompiler::StreamedSource* source() { return m_source.get(); } |
54 ScriptResource* resource() const { return m_resource; } | 59 ScriptResource* resource() const { return m_resource; } |
55 | 60 |
56 // 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 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 kSmallScriptThreshold = threshold; | 106 kSmallScriptThreshold = threshold; |
102 } | 107 } |
103 | 108 |
104 static size_t smallScriptThreshold() { return kSmallScriptThreshold; } | 109 static size_t smallScriptThreshold() { return kSmallScriptThreshold; } |
105 | 110 |
106 private: | 111 private: |
107 // Scripts whose first data chunk is smaller than this constant won't be | 112 // Scripts whose first data chunk is smaller than this constant won't be |
108 // streamed. Non-const for testing. | 113 // streamed. Non-const for testing. |
109 static size_t kSmallScriptThreshold; | 114 static size_t kSmallScriptThreshold; |
110 | 115 |
111 ScriptStreamer(ScriptResource*, PendingScript::Type, ScriptState*, v8::Scrip
tCompiler::CompileOptions, WebTaskRunner*); | 116 ScriptStreamer(ScriptResource*, Type, ScriptState*, v8::ScriptCompiler::Comp
ileOptions, WebTaskRunner*); |
112 | 117 |
113 void streamingComplete(); | 118 void streamingComplete(); |
114 void notifyFinishedToClient(); | 119 void notifyFinishedToClient(); |
115 | 120 |
116 static bool startStreamingInternal(PendingScript&, PendingScript::Type, Sett
ings*, ScriptState*, WebTaskRunner*); | 121 static bool startStreamingInternal(PendingScript*, Type, Settings*, ScriptSt
ate*, WebTaskRunner*); |
117 | 122 |
118 // This pointer is weak. If PendingScript and its Resource are deleted | 123 // This pointer is weak. If PendingScript and its Resource are deleted |
119 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its | 124 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its |
120 // deletion by calling cancel(). | 125 // deletion by calling cancel(). |
121 RawPtrWillBeMember<ScriptResource> m_resource; | 126 RawPtrWillBeMember<ScriptResource> m_resource; |
122 // Whether ScriptStreamer is detached from the Resource. In those cases, the | 127 // Whether ScriptStreamer is detached from the Resource. In those cases, the |
123 // script data is not needed any more, and the client won't get notified | 128 // script data is not needed any more, and the client won't get notified |
124 // when the loading and streaming are done. | 129 // when the loading and streaming are done. |
125 bool m_detached; | 130 bool m_detached; |
126 | 131 |
(...skipping 10 matching lines...) Expand all Loading... |
137 // Whether the script source code should be retrieved from the Resource | 142 // Whether the script source code should be retrieved from the Resource |
138 // instead of the ScriptStreamer; guarded by m_mutex. | 143 // instead of the ScriptStreamer; guarded by m_mutex. |
139 bool m_streamingSuppressed; | 144 bool m_streamingSuppressed; |
140 | 145 |
141 // What kind of cached data V8 produces during streaming. | 146 // What kind of cached data V8 produces during streaming. |
142 v8::ScriptCompiler::CompileOptions m_compileOptions; | 147 v8::ScriptCompiler::CompileOptions m_compileOptions; |
143 | 148 |
144 RefPtr<ScriptState> m_scriptState; | 149 RefPtr<ScriptState> m_scriptState; |
145 | 150 |
146 // For recording metrics for different types of scripts separately. | 151 // For recording metrics for different types of scripts separately. |
147 PendingScript::Type m_scriptType; | 152 Type m_scriptType; |
148 | 153 |
149 mutable Mutex m_mutex; | 154 mutable Mutex m_mutex; |
150 | 155 |
151 // Encoding of the streamed script. Saved for sanity checking purposes. | 156 // Encoding of the streamed script. Saved for sanity checking purposes. |
152 v8::ScriptCompiler::StreamedSource::Encoding m_encoding; | 157 v8::ScriptCompiler::StreamedSource::Encoding m_encoding; |
153 | 158 |
154 OwnPtr<WebTaskRunner> m_loadingTaskRunner; | 159 OwnPtr<WebTaskRunner> m_loadingTaskRunner; |
155 }; | 160 }; |
156 | 161 |
157 } // namespace blink | 162 } // namespace blink |
158 | 163 |
159 #endif // ScriptStreamer_h | 164 #endif // ScriptStreamer_h |
OLD | NEW |