| 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 "core/dom/PendingScript.h" | 9 #include "core/dom/PendingScript.h" |
| 10 #include "platform/heap/Handle.h" | 10 #include "platform/heap/Handle.h" |
| 11 #include "wtf/RefCounted.h" | 11 #include "wtf/RefCounted.h" |
| 12 | 12 |
| 13 #include <v8.h> | 13 #include <v8.h> |
| 14 | 14 |
| 15 namespace blink { | 15 namespace blink { |
| 16 | 16 |
| 17 class PendingScript; | 17 class PendingScript; |
| 18 class Resource; | 18 class Resource; |
| 19 class ScriptResource; | 19 class ScriptResource; |
| 20 class ScriptResourceClient; | 20 class ScriptResourceClient; |
| 21 class ScriptState; | 21 class ScriptState; |
| 22 class Settings; | 22 class Settings; |
| 23 class SourceStream; | 23 class SourceStream; |
| 24 class WebTaskRunner; | |
| 25 | 24 |
| 26 // ScriptStreamer streams incomplete script data to V8 so that it can be parsed | 25 // 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 | 26 // while it's loaded. PendingScript holds a reference to ScriptStreamer. At the |
| 28 // moment, ScriptStreamer is only used for parser blocking scripts; this means | 27 // moment, ScriptStreamer is only used for parser blocking scripts; this means |
| 29 // that the Document stays stable and no other scripts are executing while we're | 28 // that the Document stays stable and no other scripts are executing while we're |
| 30 // streaming. It is possible, though, that Document and the PendingScript are | 29 // streaming. It is possible, though, that Document and the PendingScript are |
| 31 // destroyed while the streaming is in progress, and ScriptStreamer handles it | 30 // destroyed while the streaming is in progress, and ScriptStreamer handles it |
| 32 // gracefully. | 31 // gracefully. |
| 33 class CORE_EXPORT ScriptStreamer final : public RefCountedWillBeRefCountedGarbag
eCollected<ScriptStreamer> { | 32 class CORE_EXPORT ScriptStreamer final : public RefCountedWillBeRefCountedGarbag
eCollected<ScriptStreamer> { |
| 34 WTF_MAKE_NONCOPYABLE(ScriptStreamer); | 33 WTF_MAKE_NONCOPYABLE(ScriptStreamer); |
| 35 public: | 34 public: |
| 36 static PassRefPtrWillBeRawPtr<ScriptStreamer> create(ScriptResource* resourc
e, PendingScript::Type scriptType, ScriptState* scriptState, v8::ScriptCompiler:
:CompileOptions compileOptions, WebTaskRunner* loadingTaskRunner) | 35 static PassRefPtrWillBeRawPtr<ScriptStreamer> create(ScriptResource* resourc
e, PendingScript::Type scriptType, ScriptState* scriptState, v8::ScriptCompiler:
:CompileOptions compileOptions) |
| 37 { | 36 { |
| 38 return adoptRefWillBeNoop(new ScriptStreamer(resource, scriptType, scrip
tState, compileOptions, loadingTaskRunner)); | 37 return adoptRefWillBeNoop(new ScriptStreamer(resource, scriptType, scrip
tState, compileOptions)); |
| 39 } | 38 } |
| 40 | 39 |
| 41 ~ScriptStreamer(); | 40 ~ScriptStreamer(); |
| 42 DECLARE_TRACE(); | 41 DECLARE_TRACE(); |
| 43 | 42 |
| 44 // 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 |
| 45 // PendingScript into V8 as it loads. | 44 // PendingScript into V8 as it loads. |
| 46 static void startStreaming(PendingScript&, PendingScript::Type, Settings*, S
criptState*, WebTaskRunner*); | 45 static void startStreaming(PendingScript&, PendingScript::Type, Settings*, S
criptState*); |
| 47 | 46 |
| 48 // Returns false if we cannot stream the given encoding. | 47 // Returns false if we cannot stream the given encoding. |
| 49 static bool convertEncoding(const char* encodingName, v8::ScriptCompiler::St
reamedSource::Encoding*); | 48 static bool convertEncoding(const char* encodingName, v8::ScriptCompiler::St
reamedSource::Encoding*); |
| 50 | 49 |
| 51 bool isFinished() const; | 50 bool isFinished() const; |
| 52 | 51 |
| 53 v8::ScriptCompiler::StreamedSource* source() { return m_source.get(); } | 52 v8::ScriptCompiler::StreamedSource* source() { return m_source.get(); } |
| 54 ScriptResource* resource() const { return m_resource; } | 53 ScriptResource* resource() const { return m_resource; } |
| 55 | 54 |
| 56 // Called when the script is not needed any more (e.g., loading was | 55 // Called when the script is not needed any more (e.g., loading was |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 kSmallScriptThreshold = threshold; | 100 kSmallScriptThreshold = threshold; |
| 102 } | 101 } |
| 103 | 102 |
| 104 static size_t smallScriptThreshold() { return kSmallScriptThreshold; } | 103 static size_t smallScriptThreshold() { return kSmallScriptThreshold; } |
| 105 | 104 |
| 106 private: | 105 private: |
| 107 // Scripts whose first data chunk is smaller than this constant won't be | 106 // Scripts whose first data chunk is smaller than this constant won't be |
| 108 // streamed. Non-const for testing. | 107 // streamed. Non-const for testing. |
| 109 static size_t kSmallScriptThreshold; | 108 static size_t kSmallScriptThreshold; |
| 110 | 109 |
| 111 ScriptStreamer(ScriptResource*, PendingScript::Type, ScriptState*, v8::Scrip
tCompiler::CompileOptions, WebTaskRunner*); | 110 ScriptStreamer(ScriptResource*, PendingScript::Type, ScriptState*, v8::Scrip
tCompiler::CompileOptions); |
| 112 | 111 |
| 113 void streamingComplete(); | 112 void streamingComplete(); |
| 114 void notifyFinishedToClient(); | 113 void notifyFinishedToClient(); |
| 115 | 114 |
| 116 static bool startStreamingInternal(PendingScript&, PendingScript::Type, Sett
ings*, ScriptState*, WebTaskRunner*); | 115 static bool startStreamingInternal(PendingScript&, PendingScript::Type, Sett
ings*, ScriptState*); |
| 117 | 116 |
| 118 // This pointer is weak. If PendingScript and its Resource are deleted | 117 // This pointer is weak. If PendingScript and its Resource are deleted |
| 119 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its | 118 // before ScriptStreamer, PendingScript will notify ScriptStreamer of its |
| 120 // deletion by calling cancel(). | 119 // deletion by calling cancel(). |
| 121 RawPtrWillBeMember<ScriptResource> m_resource; | 120 RawPtrWillBeMember<ScriptResource> m_resource; |
| 122 // Whether ScriptStreamer is detached from the Resource. In those cases, the | 121 // Whether ScriptStreamer is detached from the Resource. In those cases, the |
| 123 // script data is not needed any more, and the client won't get notified | 122 // script data is not needed any more, and the client won't get notified |
| 124 // when the loading and streaming are done. | 123 // when the loading and streaming are done. |
| 125 bool m_detached; | 124 bool m_detached; |
| 126 | 125 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 143 | 142 |
| 144 RefPtr<ScriptState> m_scriptState; | 143 RefPtr<ScriptState> m_scriptState; |
| 145 | 144 |
| 146 // For recording metrics for different types of scripts separately. | 145 // For recording metrics for different types of scripts separately. |
| 147 PendingScript::Type m_scriptType; | 146 PendingScript::Type m_scriptType; |
| 148 | 147 |
| 149 mutable Mutex m_mutex; | 148 mutable Mutex m_mutex; |
| 150 | 149 |
| 151 // Encoding of the streamed script. Saved for sanity checking purposes. | 150 // Encoding of the streamed script. Saved for sanity checking purposes. |
| 152 v8::ScriptCompiler::StreamedSource::Encoding m_encoding; | 151 v8::ScriptCompiler::StreamedSource::Encoding m_encoding; |
| 153 | |
| 154 OwnPtr<WebTaskRunner> m_loadingTaskRunner; | |
| 155 }; | 152 }; |
| 156 | 153 |
| 157 } // namespace blink | 154 } // namespace blink |
| 158 | 155 |
| 159 #endif // ScriptStreamer_h | 156 #endif // ScriptStreamer_h |
| OLD | NEW |