[blink] Move ClassicPendingScript prefinalizer to ScriptStreamer

The prefinalizer in CPS no longer does anything except for cancelling
the ScriptStreamer. Since ScriptStreamer could operate independently of
the CPS, we can simply cancel the ScriptStreamer in its own
prefinalizer.

Bug: 715309
Change-Id: I5866f510bea62d633390e247eac33653a0f30a71
Reviewed-on: https://chromium-review.googlesource.com/1242466
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#593908}
diff --git a/third_party/blink/renderer/bindings/core/v8/script_streamer.cc b/third_party/blink/renderer/bindings/core/v8/script_streamer.cc
index cec82c8..486ef25 100644
--- a/third_party/blink/renderer/bindings/core/v8/script_streamer.cc
+++ b/third_party/blink/renderer/bindings/core/v8/script_streamer.cc
@@ -510,6 +510,11 @@
 
 ScriptStreamer::~ScriptStreamer() = default;
 
+void ScriptStreamer::Prefinalize() {
+  Cancel();
+  prefinalizer_called_ = true;
+}
+
 void ScriptStreamer::Trace(blink::Visitor* visitor) {
   visitor->Trace(pending_script_);
 }
diff --git a/third_party/blink/renderer/bindings/core/v8/script_streamer.h b/third_party/blink/renderer/bindings/core/v8/script_streamer.h
index 149c03e..0891268 100644
--- a/third_party/blink/renderer/bindings/core/v8/script_streamer.h
+++ b/third_party/blink/renderer/bindings/core/v8/script_streamer.h
@@ -31,6 +31,7 @@
 class CORE_EXPORT ScriptStreamer final
     : public GarbageCollectedFinalized<ScriptStreamer> {
   WTF_MAKE_NONCOPYABLE(ScriptStreamer);
+  USING_PRE_FINALIZER(ScriptStreamer, Prefinalize);
 
  public:
   // For tracking why some scripts are not streamed. Not streaming is part of
@@ -125,6 +126,8 @@
                  v8::ScriptCompiler::CompileOptions,
                  scoped_refptr<base::SingleThreadTaskRunner>);
 
+  void Prefinalize();
+
   void StreamingComplete();
   void NotifyFinishedToClient();
   bool HasEnoughDataForStreaming(size_t resource_buffer_size);
@@ -165,6 +168,12 @@
   v8::ScriptCompiler::StreamedSource::Encoding encoding_;
 
   scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_;
+
+  // This is a temporary flag to confirm that ScriptStreamer is not
+  // touched after its refinalizer call and thus https://crbug.com/715309
+  // doesn't break assumptions.
+  // TODO(hiroshige): Check the state in more general way.
+  bool prefinalizer_called_ = false;
 };
 
 }  // namespace blink
diff --git a/third_party/blink/renderer/core/script/classic_pending_script.cc b/third_party/blink/renderer/core/script/classic_pending_script.cc
index f2ae402..bad410a 100644
--- a/third_party/blink/renderer/core/script/classic_pending_script.cc
+++ b/third_party/blink/renderer/core/script/classic_pending_script.cc
@@ -100,7 +100,6 @@
 
 NOINLINE void ClassicPendingScript::CheckState() const {
   // TODO(hiroshige): Turn these CHECK()s into DCHECK() before going to beta.
-  CHECK(!prefinalizer_called_);
   CHECK(GetElement());
   CHECK_EQ(is_external_, !!GetResource());
   CHECK(GetResource() || !streamer_);
@@ -187,13 +186,6 @@
   }
 }
 
-void ClassicPendingScript::Prefinalize() {
-  // TODO(hiroshige): Consider moving this to ScriptStreamer's prefinalizer.
-  // https://crbug.com/715309
-  CancelStreaming();
-  prefinalizer_called_ = true;
-}
-
 void ClassicPendingScript::DisposeInternal() {
   MemoryCoordinator::Instance().UnregisterClient(this);
   ClearResource();
diff --git a/third_party/blink/renderer/core/script/classic_pending_script.h b/third_party/blink/renderer/core/script/classic_pending_script.h
index bd220ad..e99a296 100644
--- a/third_party/blink/renderer/core/script/classic_pending_script.h
+++ b/third_party/blink/renderer/core/script/classic_pending_script.h
@@ -27,7 +27,6 @@
                                                public ResourceClient,
                                                public MemoryCoordinatorClient {
   USING_GARBAGE_COLLECTED_MIXIN(ClassicPendingScript);
-  USING_PRE_FINALIZER(ClassicPendingScript, Prefinalize);
 
  public:
   // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-script
@@ -72,8 +71,6 @@
     not_streamed_reason_ = reason;
   }
 
-  void Prefinalize();
-
  private:
   // See AdvanceReadyState implementation for valid state transitions.
   enum ReadyState {
@@ -155,12 +152,6 @@
 
   // Specifies the reason that script was never streamed.
   ScriptStreamer::NotStreamingReason not_streamed_reason_;
-
-  // This is a temporary flag to confirm that ClassicPendingScript is not
-  // touched after its refinalizer call and thus https://crbug.com/715309
-  // doesn't break assumptions.
-  // TODO(hiroshige): Check the state in more general way.
-  bool prefinalizer_called_ = false;
 };
 
 }  // namespace blink