| 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 <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "core/CoreExport.h" | 10 #include "core/CoreExport.h" |
| 11 #include "platform/WebTaskRunner.h" |
| 11 #include "platform/heap/Handle.h" | 12 #include "platform/heap/Handle.h" |
| 12 #include "platform/wtf/Noncopyable.h" | 13 #include "platform/wtf/Noncopyable.h" |
| 13 #include "platform/wtf/text/WTFString.h" | 14 #include "platform/wtf/text/WTFString.h" |
| 14 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
| 15 | 16 |
| 16 namespace blink { | 17 namespace blink { |
| 17 | 18 |
| 18 class PendingScript; | 19 class ClassicPendingScript; |
| 19 class Resource; | 20 class Resource; |
| 20 class ScriptResource; | 21 class ScriptResource; |
| 21 class ScriptState; | 22 class ScriptState; |
| 22 class Settings; | 23 class Settings; |
| 23 class SourceStream; | 24 class SourceStream; |
| 24 class WebTaskRunner; | 25 class WebTaskRunner; |
| 25 | 26 |
| 26 // ScriptStreamer streams incomplete script data to V8 so that it can be parsed | 27 // ScriptStreamer streams incomplete script data to V8 so that it can be parsed |
| 27 // while it's loaded. PendingScript holds a reference to ScriptStreamer. At the | 28 // while it's loaded. ClassicPendingScript holds a reference to ScriptStreamer. |
| 28 // moment, ScriptStreamer is only used for parser blocking scripts; this means | 29 // At the moment, ScriptStreamer is only used for parser blocking scripts; this |
| 29 // that the Document stays stable and no other scripts are executing while we're | 30 // means that the Document stays stable and no other scripts are executing |
| 30 // streaming. It is possible, though, that Document and the PendingScript are | 31 // while we're streaming. It is possible, though, that Document and the |
| 31 // destroyed while the streaming is in progress, and ScriptStreamer handles it | 32 // ClassicPendingScript are destroyed while the streaming is in progress, and |
| 32 // gracefully. | 33 // ScriptStreamer handles it gracefully. |
| 33 class CORE_EXPORT ScriptStreamer final | 34 class CORE_EXPORT ScriptStreamer final |
| 34 : public GarbageCollectedFinalized<ScriptStreamer> { | 35 : public GarbageCollectedFinalized<ScriptStreamer> { |
| 35 WTF_MAKE_NONCOPYABLE(ScriptStreamer); | 36 WTF_MAKE_NONCOPYABLE(ScriptStreamer); |
| 36 | 37 |
| 37 public: | 38 public: |
| 38 enum Type { ParsingBlocking, Deferred, Async }; | 39 enum Type { ParsingBlocking, Deferred, Async }; |
| 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 // ClassicPendingScript into V8 as it loads. |
| 45 static void startStreaming(PendingScript*, | 46 static void startStreaming(ClassicPendingScript*, |
| 46 Type, | 47 Type, |
| 47 Settings*, | 48 Settings*, |
| 48 ScriptState*, | 49 ScriptState*, |
| 49 RefPtr<WebTaskRunner>); | 50 RefPtr<WebTaskRunner>); |
| 50 | 51 |
| 51 // Returns false if we cannot stream the given encoding. | 52 // Returns false if we cannot stream the given encoding. |
| 52 static bool convertEncoding(const char* encodingName, | 53 static bool convertEncoding(const char* encodingName, |
| 53 v8::ScriptCompiler::StreamedSource::Encoding*); | 54 v8::ScriptCompiler::StreamedSource::Encoding*); |
| 54 | 55 |
| 55 bool isFinished() const; | 56 bool isFinished() const; |
| 56 | 57 |
| 57 v8::ScriptCompiler::StreamedSource* source() { return m_source.get(); } | 58 v8::ScriptCompiler::StreamedSource* source() { return m_source.get(); } |
| 58 ScriptResource* resource() const { return m_resource; } | 59 ScriptResource* resource() const { return m_resource; } |
| 59 | 60 |
| 60 // Called when the script is not needed any more (e.g., loading was | 61 // Called when the script is not needed any more (e.g., loading was |
| 61 // cancelled). After calling cancel, PendingScript can drop its reference to | 62 // cancelled). After calling cancel, ClassicPendingScript can drop its |
| 62 // ScriptStreamer, and ScriptStreamer takes care of eventually deleting | 63 // reference to ScriptStreamer, and ScriptStreamer takes care of eventually |
| 63 // itself (after the V8 side has finished too). | 64 // deleting itself (after the V8 side has finished too). |
| 64 void cancel(); | 65 void cancel(); |
| 65 | 66 |
| 66 // When the streaming is suppressed, the data is not given to V8, but | 67 // When the streaming is suppressed, the data is not given to V8, but |
| 67 // ScriptStreamer still watches the resource load and notifies the upper | 68 // ScriptStreamer still watches the resource load and notifies the upper |
| 68 // layers when loading is finished. It is used in situations when we have | 69 // layers when loading is finished. It is used in situations when we have |
| 69 // started streaming but then we detect we don't want to stream (e.g., when | 70 // started streaming but then we detect we don't want to stream (e.g., when |
| 70 // we have the code cache for the script) and we still want to parse and | 71 // we have the code cache for the script) and we still want to parse and |
| 71 // execute it when it has finished loading. | 72 // execute it when it has finished loading. |
| 72 void suppressStreaming(); | 73 void suppressStreaming(); |
| 73 bool streamingSuppressed() const { return m_streamingSuppressed; } | 74 bool streamingSuppressed() const { return m_streamingSuppressed; } |
| 74 | 75 |
| 75 v8::ScriptCompiler::CompileOptions compileOptions() const { | 76 v8::ScriptCompiler::CompileOptions compileOptions() const { |
| 76 return m_compileOptions; | 77 return m_compileOptions; |
| 77 } | 78 } |
| 78 | 79 |
| 79 // Called by PendingScript when data arrives from the network. | 80 // Called by ClassicPendingScript when data arrives from the network. |
| 80 void notifyAppendData(ScriptResource*); | 81 void notifyAppendData(ScriptResource*); |
| 81 void notifyFinished(Resource*); | 82 void notifyFinished(Resource*); |
| 82 | 83 |
| 83 // Called by ScriptStreamingTask when it has streamed all data to V8 and V8 | 84 // Called by ScriptStreamingTask when it has streamed all data to V8 and V8 |
| 84 // has processed it. | 85 // has processed it. |
| 85 void streamingCompleteOnBackgroundThread(); | 86 void streamingCompleteOnBackgroundThread(); |
| 86 | 87 |
| 87 v8::ScriptCompiler::StreamedSource::Encoding encoding() const { | 88 v8::ScriptCompiler::StreamedSource::Encoding encoding() const { |
| 88 return m_encoding; | 89 return m_encoding; |
| 89 } | 90 } |
| 90 | 91 |
| 91 const String& scriptURLString() const { return m_scriptURLString; } | 92 const String& scriptURLString() const { return m_scriptURLString; } |
| 92 unsigned long scriptResourceIdentifier() const { | 93 unsigned long scriptResourceIdentifier() const { |
| 93 return m_scriptResourceIdentifier; | 94 return m_scriptResourceIdentifier; |
| 94 } | 95 } |
| 95 | 96 |
| 96 static void setSmallScriptThresholdForTesting(size_t threshold) { | 97 static void setSmallScriptThresholdForTesting(size_t threshold) { |
| 97 s_smallScriptThreshold = threshold; | 98 s_smallScriptThreshold = threshold; |
| 98 } | 99 } |
| 99 | 100 |
| 100 static size_t smallScriptThreshold() { return s_smallScriptThreshold; } | 101 static size_t smallScriptThreshold() { return s_smallScriptThreshold; } |
| 101 | 102 |
| 102 private: | 103 private: |
| 103 // Scripts whose first data chunk is smaller than this constant won't be | 104 // Scripts whose first data chunk is smaller than this constant won't be |
| 104 // streamed. Non-const for testing. | 105 // streamed. Non-const for testing. |
| 105 static size_t s_smallScriptThreshold; | 106 static size_t s_smallScriptThreshold; |
| 106 | 107 |
| 107 static ScriptStreamer* create( | 108 static ScriptStreamer* create( |
| 108 PendingScript* script, | 109 ClassicPendingScript* script, |
| 109 Type scriptType, | 110 Type scriptType, |
| 110 ScriptState* scriptState, | 111 ScriptState* scriptState, |
| 111 v8::ScriptCompiler::CompileOptions compileOptions, | 112 v8::ScriptCompiler::CompileOptions compileOptions, |
| 112 RefPtr<WebTaskRunner> loadingTaskRunner) { | 113 RefPtr<WebTaskRunner> loadingTaskRunner) { |
| 113 return new ScriptStreamer(script, scriptType, scriptState, compileOptions, | 114 return new ScriptStreamer(script, scriptType, scriptState, compileOptions, |
| 114 std::move(loadingTaskRunner)); | 115 std::move(loadingTaskRunner)); |
| 115 } | 116 } |
| 116 ScriptStreamer(PendingScript*, | 117 ScriptStreamer(ClassicPendingScript*, |
| 117 Type, | 118 Type, |
| 118 ScriptState*, | 119 ScriptState*, |
| 119 v8::ScriptCompiler::CompileOptions, | 120 v8::ScriptCompiler::CompileOptions, |
| 120 RefPtr<WebTaskRunner>); | 121 RefPtr<WebTaskRunner>); |
| 121 | 122 |
| 122 void streamingComplete(); | 123 void streamingComplete(); |
| 123 void notifyFinishedToClient(); | 124 void notifyFinishedToClient(); |
| 124 | 125 |
| 125 static bool startStreamingInternal(PendingScript*, | 126 static bool startStreamingInternal(ClassicPendingScript*, |
| 126 Type, | 127 Type, |
| 127 Settings*, | 128 Settings*, |
| 128 ScriptState*, | 129 ScriptState*, |
| 129 RefPtr<WebTaskRunner>); | 130 RefPtr<WebTaskRunner>); |
| 130 | 131 |
| 131 Member<PendingScript> m_pendingScript; | 132 Member<ClassicPendingScript> m_pendingScript; |
| 132 // This pointer is weak. If PendingScript and its Resource are deleted | 133 // This pointer is weak. If ClassicPendingScript and its Resource are deleted |
| 133 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its | 134 // before ScriptStreamer, ClassicPendingScript will notify ScriptStreamer of |
| 134 // deletion by calling cancel(). | 135 // its deletion by calling cancel(). |
| 135 Member<ScriptResource> m_resource; | 136 Member<ScriptResource> m_resource; |
| 136 // Whether ScriptStreamer is detached from the Resource. In those cases, the | 137 // Whether ScriptStreamer is detached from the Resource. In those cases, the |
| 137 // script data is not needed any more, and the client won't get notified | 138 // script data is not needed any more, and the client won't get notified |
| 138 // when the loading and streaming are done. | 139 // when the loading and streaming are done. |
| 139 bool m_detached; | 140 bool m_detached; |
| 140 | 141 |
| 141 SourceStream* m_stream; | 142 SourceStream* m_stream; |
| 142 std::unique_ptr<v8::ScriptCompiler::StreamedSource> m_source; | 143 std::unique_ptr<v8::ScriptCompiler::StreamedSource> m_source; |
| 143 bool m_loadingFinished; // Whether loading from the network is done. | 144 bool m_loadingFinished; // Whether loading from the network is done. |
| 144 bool m_parsingFinished; // Whether the V8 side processing is done. | 145 bool m_parsingFinished; // Whether the V8 side processing is done. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 165 | 166 |
| 166 // Encoding of the streamed script. Saved for sanity checking purposes. | 167 // Encoding of the streamed script. Saved for sanity checking purposes. |
| 167 v8::ScriptCompiler::StreamedSource::Encoding m_encoding; | 168 v8::ScriptCompiler::StreamedSource::Encoding m_encoding; |
| 168 | 169 |
| 169 RefPtr<WebTaskRunner> m_loadingTaskRunner; | 170 RefPtr<WebTaskRunner> m_loadingTaskRunner; |
| 170 }; | 171 }; |
| 171 | 172 |
| 172 } // namespace blink | 173 } // namespace blink |
| 173 | 174 |
| 174 #endif // ScriptStreamer_h | 175 #endif // ScriptStreamer_h |
| OLD | NEW |