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