OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef ScriptStreamer_h | |
6 #define ScriptStreamer_h | |
7 | |
8 #include "wtf/RefCounted.h" | |
9 | |
10 #include <v8.h> | |
11 | |
12 namespace blink { | |
13 | |
14 class PendingScript; | |
15 class ScriptResource; | |
16 class ScriptResourceClient; | |
17 class SourceStream; | |
18 | |
19 // ScriptStreamer streams incomplete script data to V8 so that it can be parsed | |
20 // while it's loaded. PendingScript holds a reference to ScriptStreamer. At the | |
21 // moment, ScriptStreamer is only used for parser blocking scripts; this means | |
22 // that the Document stays stable and no other scripts are executing while we're | |
23 // streaming. It is possible, though, that Document and the PendingScript are | |
24 // destroyed while the streaming is in progress, and ScriptStreamer handles it | |
25 // gracefully. | |
26 class ScriptStreamer : public RefCounted<ScriptStreamer> { | |
27 public: | |
28 // Launches a task (on a background thread) which will stream the given | |
29 // PendingScript into V8 as it loads. It's also possible that V8 cannot | |
30 // stream the given script; in that case this function returns | |
31 // false. Internally, this constructs a ScriptStreamer and attaches it to | |
32 // the PendingScript. Use ScriptStreamer::addClient to get notified when the | |
33 // streaming finishes. | |
34 static bool startStreaming(PendingScript&); | |
35 | |
36 bool streamingInProgress() const { return !m_loadingFinished || !m_v8Done; } | |
37 | |
38 v8::ScriptCompiler::StreamedSource* source() { return &m_source; } | |
39 ScriptResource* resource() const { return m_resource; } | |
40 | |
41 // Called when the upper layer doesn't need the streamed PendingScript any | |
42 // more. After calling cancel, the upper layer (PendingScript) can drop its | |
haraken
2014/09/10 05:57:51
This comment is a bit confusing, though I know wha
marja
2014/09/11 09:15:37
Clarified this:
// Called when the script is
| |
43 // reference to ScriptStreamer, and ScriptStreamer takes care of eventually | |
44 // deleting itself (after the V8 side has finished too). | |
45 void cancel(); | |
46 | |
47 // When the streaming is suppressed, the data is not given to V8, but | |
48 // ScriptStreamer still watches the resource load and notifies the upper | |
49 // layers when loading is finished. It is used in situations when we have | |
50 // started streaming but then we detect we don't want to stream (e.g., when | |
51 // we have the code cache for the script) and we still want to parse and | |
52 // execute it when it has finished loading. | |
53 void suppressStreaming() { m_streamingSuppressed = true; } | |
54 bool streamingSuppressed() const { return m_streamingSuppressed; } | |
55 | |
56 unsigned cachedDataDataType() const { return m_cachedDataDataType; } | |
57 | |
58 void addClient(ScriptResourceClient* client) | |
59 { | |
60 ASSERT(!m_client); | |
61 m_client = client; | |
62 } | |
63 | |
64 void removeClient(ScriptResourceClient* client) | |
65 { | |
66 ASSERT(m_client == client); | |
67 m_client = 0; | |
68 } | |
69 | |
70 // Called by PendingScript when data arrives from the network. | |
71 void notifyAppendData(); | |
72 void notifyFinished(); | |
73 | |
74 // Called by ScriptStreamingTask when it has streamed all data to V8 and V8 | |
75 // has processed it. | |
76 void streamingComplete(); | |
77 | |
78 private: | |
79 ScriptStreamer(ScriptResource*, v8::ScriptCompiler::StreamedSource::Encoding ); | |
80 | |
81 void notifyFinishedToClient(); | |
82 | |
83 // This pointer is weak. If PendingScript and its Resource are deleted | |
84 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its | |
85 // deletion by calling cancel(). | |
86 ScriptResource* m_resource; | |
haraken
2014/09/10 05:57:51
Can we avoid adding the m_resource member to Scrip
marja
2014/09/11 09:15:37
We need it in e.g., notifyFinishedToClient which m
| |
87 | |
88 SourceStream* m_stream; | |
89 v8::ScriptCompiler::StreamedSource m_source; | |
90 ScriptResourceClient* m_client; | |
91 bool m_loadingFinished; // Whether loading from the network is done. | |
92 bool m_v8Done; // Whether the V8 side processing is done. | |
haraken
2014/09/10 05:57:51
m_v8Done => m_parsingFinished ? (for consistency w
marja
2014/09/11 09:15:37
Done.
| |
93 | |
94 // Whether the script source code should be retrieved from the Resource | |
95 // instead of the ScriptStreamer. | |
96 bool m_streamingSuppressed; | |
97 | |
98 // What kind of cached data V8 produces during streaming. | |
99 unsigned m_cachedDataDataType; | |
haraken
2014/09/10 05:57:51
m_cachedDataDataType => m_cachedDataType ?
marja
2014/09/11 09:15:37
Done.
| |
100 }; | |
101 | |
102 } // namespace blink | |
103 | |
104 #endif // ScriptStreamer_h | |
OLD | NEW |