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 "platform/heap/Handle.h" | 9 #include "platform/heap/Handle.h" |
10 #include "wtf/RefCounted.h" | 10 #include "wtf/RefCounted.h" |
11 | 11 |
12 #include <v8.h> | 12 #include <v8.h> |
13 | 13 |
14 namespace blink { | 14 namespace blink { |
15 | 15 |
16 class PendingScript; | 16 class PendingScript; |
17 class Resource; | 17 class Resource; |
18 class ScriptResource; | 18 class ScriptResource; |
19 class ScriptResourceClient; | |
20 class ScriptState; | 19 class ScriptState; |
21 class Settings; | 20 class Settings; |
22 class SourceStream; | 21 class SourceStream; |
23 class WebTaskRunner; | 22 class WebTaskRunner; |
24 | 23 |
25 // ScriptStreamer streams incomplete script data to V8 so that it can be parsed | 24 // 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 | 25 // while it's loaded. PendingScript holds a reference to ScriptStreamer. At the |
27 // moment, ScriptStreamer is only used for parser blocking scripts; this means | 26 // 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 | 27 // 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 | 28 // streaming. It is possible, though, that Document and the PendingScript are |
30 // destroyed while the streaming is in progress, and ScriptStreamer handles it | 29 // destroyed while the streaming is in progress, and ScriptStreamer handles it |
31 // gracefully. | 30 // gracefully. |
32 class CORE_EXPORT ScriptStreamer final : public RefCountedWillBeRefCountedGarbag
eCollected<ScriptStreamer> { | 31 class CORE_EXPORT ScriptStreamer final : public RefCountedWillBeRefCountedGarbag
eCollected<ScriptStreamer> { |
33 WTF_MAKE_NONCOPYABLE(ScriptStreamer); | 32 WTF_MAKE_NONCOPYABLE(ScriptStreamer); |
34 public: | 33 public: |
35 enum Type { | 34 enum Type { |
36 ParsingBlocking, | 35 ParsingBlocking, |
37 Deferred, | 36 Deferred, |
38 Async | 37 Async |
39 }; | 38 }; |
40 | 39 |
41 static PassRefPtrWillBeRawPtr<ScriptStreamer> create(ScriptResource* resourc
e, Type scriptType, ScriptState* scriptState, v8::ScriptCompiler::CompileOptions
compileOptions, WebTaskRunner* loadingTaskRunner) | |
42 { | |
43 return adoptRefWillBeNoop(new ScriptStreamer(resource, scriptType, scrip
tState, compileOptions, loadingTaskRunner)); | |
44 } | |
45 | |
46 ~ScriptStreamer(); | 40 ~ScriptStreamer(); |
47 DECLARE_TRACE(); | 41 DECLARE_TRACE(); |
48 | 42 |
49 // 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 |
50 // PendingScript into V8 as it loads. | 44 // PendingScript into V8 as it loads. |
51 static void startStreaming(PendingScript*, Type, Settings*, ScriptState*, We
bTaskRunner*); | 45 static void startStreaming(PendingScript*, Type, Settings*, ScriptState*, We
bTaskRunner*); |
52 | 46 |
53 // Returns false if we cannot stream the given encoding. | 47 // Returns false if we cannot stream the given encoding. |
54 static bool convertEncoding(const char* encodingName, v8::ScriptCompiler::St
reamedSource::Encoding*); | 48 static bool convertEncoding(const char* encodingName, v8::ScriptCompiler::St
reamedSource::Encoding*); |
55 | 49 |
(...skipping 15 matching lines...) Expand all Loading... |
71 // we have the code cache for the script) and we still want to parse and | 65 // we have the code cache for the script) and we still want to parse and |
72 // execute it when it has finished loading. | 66 // execute it when it has finished loading. |
73 void suppressStreaming(); | 67 void suppressStreaming(); |
74 bool streamingSuppressed() const { return m_streamingSuppressed; } | 68 bool streamingSuppressed() const { return m_streamingSuppressed; } |
75 | 69 |
76 v8::ScriptCompiler::CompileOptions compileOptions() const | 70 v8::ScriptCompiler::CompileOptions compileOptions() const |
77 { | 71 { |
78 return m_compileOptions; | 72 return m_compileOptions; |
79 } | 73 } |
80 | 74 |
81 void addClient(ScriptResourceClient* client) | |
82 { | |
83 ASSERT(!m_client); | |
84 m_client = client; | |
85 notifyFinishedToClient(); | |
86 } | |
87 | |
88 void removeClient(ScriptResourceClient* client) | |
89 { | |
90 ASSERT(m_client == client); | |
91 m_client = 0; | |
92 } | |
93 | |
94 // Called by PendingScript when data arrives from the network. | 75 // Called by PendingScript when data arrives from the network. |
95 void notifyAppendData(ScriptResource*); | 76 void notifyAppendData(ScriptResource*); |
96 void notifyFinished(Resource*); | 77 void notifyFinished(Resource*); |
97 | 78 |
98 // 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 |
99 // has processed it. | 80 // has processed it. |
100 void streamingCompleteOnBackgroundThread(); | 81 void streamingCompleteOnBackgroundThread(); |
101 | 82 |
102 v8::ScriptCompiler::StreamedSource::Encoding encoding() const { return m_enc
oding; } | 83 v8::ScriptCompiler::StreamedSource::Encoding encoding() const { return m_enc
oding; } |
103 | 84 |
104 static void setSmallScriptThresholdForTesting(size_t threshold) | 85 static void setSmallScriptThresholdForTesting(size_t threshold) |
105 { | 86 { |
106 s_smallScriptThreshold = threshold; | 87 s_smallScriptThreshold = threshold; |
107 } | 88 } |
108 | 89 |
109 static size_t smallScriptThreshold() { return s_smallScriptThreshold; } | 90 static size_t smallScriptThreshold() { return s_smallScriptThreshold; } |
110 | 91 |
111 private: | 92 private: |
112 // 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 |
113 // streamed. Non-const for testing. | 94 // streamed. Non-const for testing. |
114 static size_t s_smallScriptThreshold; | 95 static size_t s_smallScriptThreshold; |
115 | 96 |
116 ScriptStreamer(ScriptResource*, Type, ScriptState*, v8::ScriptCompiler::Comp
ileOptions, WebTaskRunner*); | 97 static PassRefPtrWillBeRawPtr<ScriptStreamer> create(PendingScript* script,
Type scriptType, ScriptState* scriptState, v8::ScriptCompiler::CompileOptions co
mpileOptions, WebTaskRunner* loadingTaskRunner) |
| 98 { |
| 99 return adoptRefWillBeNoop(new ScriptStreamer(script, scriptType, scriptS
tate, compileOptions, loadingTaskRunner)); |
| 100 } |
| 101 ScriptStreamer(PendingScript*, Type, ScriptState*, v8::ScriptCompiler::Compi
leOptions, WebTaskRunner*); |
117 | 102 |
118 void streamingComplete(); | 103 void streamingComplete(); |
119 void notifyFinishedToClient(); | 104 void notifyFinishedToClient(); |
120 | 105 |
121 static bool startStreamingInternal(PendingScript*, Type, Settings*, ScriptSt
ate*, WebTaskRunner*); | 106 static bool startStreamingInternal(PendingScript*, Type, Settings*, ScriptSt
ate*, WebTaskRunner*); |
122 | 107 |
| 108 RawPtrWillBeMember<PendingScript> m_pendingScript; |
123 // This pointer is weak. If PendingScript and its Resource are deleted | 109 // This pointer is weak. If PendingScript and its Resource are deleted |
124 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its | 110 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its |
125 // deletion by calling cancel(). | 111 // deletion by calling cancel(). |
126 RawPtrWillBeMember<ScriptResource> m_resource; | 112 RawPtrWillBeMember<ScriptResource> m_resource; |
127 // Whether ScriptStreamer is detached from the Resource. In those cases, the | 113 // Whether ScriptStreamer is detached from the Resource. In those cases, the |
128 // script data is not needed any more, and the client won't get notified | 114 // script data is not needed any more, and the client won't get notified |
129 // when the loading and streaming are done. | 115 // when the loading and streaming are done. |
130 bool m_detached; | 116 bool m_detached; |
131 | 117 |
132 SourceStream* m_stream; | 118 SourceStream* m_stream; |
133 OwnPtr<v8::ScriptCompiler::StreamedSource> m_source; | 119 OwnPtr<v8::ScriptCompiler::StreamedSource> m_source; |
134 ScriptResourceClient* m_client; | |
135 bool m_loadingFinished; // Whether loading from the network is done. | 120 bool m_loadingFinished; // Whether loading from the network is done. |
136 // Whether the V8 side processing is done. Will be used by the main thread | 121 // Whether the V8 side processing is done. Will be used by the main thread |
137 // and the streamer thread; guarded by m_mutex. | 122 // and the streamer thread; guarded by m_mutex. |
138 bool m_parsingFinished; | 123 bool m_parsingFinished; |
139 // Whether we have received enough data to start the streaming. | 124 // Whether we have received enough data to start the streaming. |
140 bool m_haveEnoughDataForStreaming; | 125 bool m_haveEnoughDataForStreaming; |
141 | 126 |
142 // Whether the script source code should be retrieved from the Resource | 127 // Whether the script source code should be retrieved from the Resource |
143 // instead of the ScriptStreamer; guarded by m_mutex. | 128 // instead of the ScriptStreamer; guarded by m_mutex. |
144 bool m_streamingSuppressed; | 129 bool m_streamingSuppressed; |
(...skipping 10 matching lines...) Expand all Loading... |
155 | 140 |
156 // Encoding of the streamed script. Saved for sanity checking purposes. | 141 // Encoding of the streamed script. Saved for sanity checking purposes. |
157 v8::ScriptCompiler::StreamedSource::Encoding m_encoding; | 142 v8::ScriptCompiler::StreamedSource::Encoding m_encoding; |
158 | 143 |
159 OwnPtr<WebTaskRunner> m_loadingTaskRunner; | 144 OwnPtr<WebTaskRunner> m_loadingTaskRunner; |
160 }; | 145 }; |
161 | 146 |
162 } // namespace blink | 147 } // namespace blink |
163 | 148 |
164 #endif // ScriptStreamer_h | 149 #endif // ScriptStreamer_h |
OLD | NEW |