| OLD | NEW |
| 1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2010 Google, Inc. All Rights Reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * | 3 // found in the LICENSE file. |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | 4 |
| 26 #ifndef PendingScript_h | 5 #ifndef ClassicPendingScript_h |
| 27 #define PendingScript_h | 6 #define ClassicPendingScript_h |
| 28 | 7 |
| 29 #include "bindings/core/v8/ScriptStreamer.h" | 8 #include "bindings/core/v8/ScriptStreamer.h" |
| 30 #include "core/CoreExport.h" | 9 #include "core/dom/ClassicScript.h" |
| 31 #include "core/dom/ScriptElementBase.h" | 10 #include "core/dom/PendingScript.h" |
| 32 #include "core/loader/resource/ScriptResource.h" | 11 #include "core/loader/resource/ScriptResource.h" |
| 33 #include "platform/MemoryCoordinator.h" | 12 #include "platform/MemoryCoordinator.h" |
| 34 #include "platform/heap/Handle.h" | |
| 35 #include "platform/loader/fetch/ResourceOwner.h" | 13 #include "platform/loader/fetch/ResourceOwner.h" |
| 36 #include "platform/wtf/Noncopyable.h" | |
| 37 #include "platform/wtf/text/TextPosition.h" | |
| 38 | 14 |
| 39 namespace blink { | 15 namespace blink { |
| 40 | 16 |
| 41 class PendingScript; | 17 // PendingScript for a classic script |
| 42 class ClassicScript; | 18 // https://html.spec.whatwg.org/#classic-script. |
| 43 | |
| 44 class CORE_EXPORT PendingScriptClient : public GarbageCollectedMixin { | |
| 45 public: | |
| 46 virtual ~PendingScriptClient() {} | |
| 47 | |
| 48 // Invoked when the pending script has finished loading. This could be during | |
| 49 // |watchForLoad| (if the pending script was already ready), or when the | |
| 50 // resource loads (if script streaming is not occurring), or when script | |
| 51 // streaming finishes. | |
| 52 virtual void PendingScriptFinished(PendingScript*) = 0; | |
| 53 | |
| 54 DEFINE_INLINE_VIRTUAL_TRACE() {} | |
| 55 }; | |
| 56 | |
| 57 // A container for an external script which may be loaded and executed. | |
| 58 // | 19 // |
| 59 // TODO(kochi): The comment below is from pre-oilpan age and may not be correct | 20 // TODO(kochi): The comment below is from pre-oilpan age and may not be correct |
| 60 // now. | 21 // now. |
| 61 // A RefPtr alone does not prevent the underlying Resource from purging its data | 22 // A RefPtr alone does not prevent the underlying Resource from purging its data |
| 62 // buffer. This class holds a dummy client open for its lifetime in order to | 23 // buffer. This class holds a dummy client open for its lifetime in order to |
| 63 // guarantee that the data buffer will not be purged. | 24 // guarantee that the data buffer will not be purged. |
| 64 class CORE_EXPORT PendingScript final | 25 class CORE_EXPORT ClassicPendingScript final |
| 65 : public GarbageCollectedFinalized<PendingScript>, | 26 : public PendingScript, |
| 66 public ResourceOwner<ScriptResource>, | 27 public ResourceOwner<ScriptResource>, |
| 67 public MemoryCoordinatorClient { | 28 public MemoryCoordinatorClient { |
| 68 USING_GARBAGE_COLLECTED_MIXIN(PendingScript); | 29 USING_GARBAGE_COLLECTED_MIXIN(ClassicPendingScript); |
| 69 USING_PRE_FINALIZER(PendingScript, Dispose); | 30 |
| 70 WTF_MAKE_NONCOPYABLE(PendingScript); | 31 // In order to call Dispose() before ResourceOwner's prefinalizer, we |
| 32 // also register ClassicPendingScript::Dispose() as the prefinalizer of |
| 33 // ClassicPendingScript here. https://crbug.com/711703 |
| 34 USING_PRE_FINALIZER(ClassicPendingScript, Dispose); |
| 71 | 35 |
| 72 public: | 36 public: |
| 73 // For script from an external file. | 37 // For script from an external file. |
| 74 static PendingScript* Create(ScriptElementBase*, ScriptResource*); | 38 static ClassicPendingScript* Create(ScriptElementBase*, ScriptResource*); |
| 75 // For inline script. | 39 // For inline script. |
| 76 static PendingScript* Create(ScriptElementBase*, const TextPosition&); | 40 static ClassicPendingScript* Create(ScriptElementBase*, const TextPosition&); |
| 77 | 41 |
| 78 static PendingScript* CreateForTesting(ScriptResource*); | 42 static ClassicPendingScript* CreateForTesting(ScriptResource*); |
| 79 | 43 |
| 80 ~PendingScript() override; | 44 ~ClassicPendingScript() override; |
| 81 | |
| 82 TextPosition StartingPosition() const { return starting_position_; } | |
| 83 void MarkParserBlockingLoadStartTime(); | |
| 84 // Returns the time the load of this script started blocking the parser, or | |
| 85 // zero if this script hasn't yet blocked the parser, in | |
| 86 // monotonicallyIncreasingTime. | |
| 87 double ParserBlockingLoadStartTime() const { | |
| 88 return parser_blocking_load_start_time_; | |
| 89 } | |
| 90 | |
| 91 void WatchForLoad(PendingScriptClient*); | |
| 92 void StopWatchingForLoad(); | |
| 93 | |
| 94 ScriptElementBase* GetElement() const; | |
| 95 | |
| 96 DECLARE_TRACE(); | |
| 97 | |
| 98 ClassicScript* GetSource(const KURL& document_url, | |
| 99 bool& error_occurred) const; | |
| 100 | 45 |
| 101 void SetStreamer(ScriptStreamer*); | 46 void SetStreamer(ScriptStreamer*); |
| 102 void StreamingFinished(); | 47 void StreamingFinished(); |
| 103 | 48 |
| 104 bool IsReady() const; | 49 DECLARE_TRACE(); |
| 105 bool ErrorOccurred() const; | |
| 106 | 50 |
| 107 void StartStreamingIfPossible(Document*, ScriptStreamer::Type); | 51 blink::ScriptType GetScriptType() const override { |
| 52 return blink::ScriptType::kClassic; |
| 53 } |
| 54 |
| 55 ClassicScript* GetSource(const KURL& document_url, |
| 56 bool& error_occurred) const override; |
| 57 bool IsReady() const override; |
| 58 KURL Url() const override; |
| 59 bool IsExternal() const override { return GetResource(); } |
| 60 bool ErrorOccurred() const override; |
| 61 bool WasCanceled() const override; |
| 62 void StartStreamingIfPossible(Document*, ScriptStreamer::Type) override; |
| 63 void RemoveFromMemoryCache() override; |
| 64 void DisposeInternal() override; |
| 65 |
| 66 // Just used as the prefinalizer, does the same as PendingScript::Dispose(). |
| 67 // We define Dispose() with NOINLINE in ClassicPendingScript just to make |
| 68 // the prefinalizers of PendingScript and ClassicPendingScript have |
| 69 // different addresses to avoid assertion failures on Windows test bots. |
| 108 void Dispose(); | 70 void Dispose(); |
| 109 | 71 |
| 110 private: | 72 private: |
| 111 PendingScript(ScriptElementBase*, | 73 ClassicPendingScript(ScriptElementBase*, |
| 112 ScriptResource*, | 74 ScriptResource*, |
| 113 const TextPosition&, | 75 const TextPosition&, |
| 114 bool is_for_testing = false); | 76 bool is_for_testing = false); |
| 115 PendingScript() = delete; | 77 ClassicPendingScript() = delete; |
| 116 | 78 |
| 117 void CheckState() const; | 79 void CheckState() const override; |
| 118 | 80 |
| 119 // ScriptResourceClient | 81 // ScriptResourceClient |
| 120 void NotifyFinished(Resource*) override; | 82 void NotifyFinished(Resource*) override; |
| 121 String DebugName() const override { return "PendingScript"; } | 83 String DebugName() const override { return "PendingScript"; } |
| 122 void NotifyAppendData(ScriptResource*) override; | 84 void NotifyAppendData(ScriptResource*) override; |
| 123 | 85 |
| 124 // MemoryCoordinatorClient | 86 // MemoryCoordinatorClient |
| 125 void OnPurgeMemory() override; | 87 void OnPurgeMemory() override; |
| 126 | 88 |
| 127 bool watching_for_load_; | |
| 128 | |
| 129 // |m_element| must points to the corresponding ScriptLoader's | |
| 130 // ScriptElementBase and thus must be non-null before dispose() is called | |
| 131 // (except for unit tests). | |
| 132 Member<ScriptElementBase> element_; | |
| 133 | |
| 134 TextPosition starting_position_; // Only used for inline script tags. | |
| 135 bool integrity_failure_; | 89 bool integrity_failure_; |
| 136 double parser_blocking_load_start_time_; | |
| 137 | 90 |
| 138 Member<ScriptStreamer> streamer_; | 91 Member<ScriptStreamer> streamer_; |
| 139 Member<PendingScriptClient> client_; | |
| 140 | 92 |
| 141 // This flag is used to skip non-null checks of |m_element| in unit | 93 // This flag is used to skip non-null checks of |m_element| in unit |
| 142 // tests, because |m_element| can be null in unit tests. | 94 // tests, because |m_element| can be null in unit tests. |
| 143 const bool is_for_testing_; | 95 const bool is_for_testing_; |
| 144 }; | 96 }; |
| 145 | 97 |
| 146 } // namespace blink | 98 } // namespace blink |
| 147 | 99 |
| 148 #endif // PendingScript_h | 100 #endif // PendingScript_h |
| OLD | NEW |