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