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