Prerender: Run WPT for CSP with target hint

Bug: 1501674
Change-Id: I7428467793000f1f37acdcde0b5310a9a3b364d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5043782
Reviewed-by: Lingqi Chi <lingqi@chromium.org>
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1226729}
diff --git a/speculation-rules/prerender/csp-script-src-elem-inline-speculation-rules.html b/speculation-rules/prerender/csp-script-src-elem-inline-speculation-rules.html
index 6268918..fa8a2cc 100644
--- a/speculation-rules/prerender/csp-script-src-elem-inline-speculation-rules.html
+++ b/speculation-rules/prerender/csp-script-src-elem-inline-speculation-rules.html
@@ -1,4 +1,6 @@
 <!DOCTYPE html>
+<meta name="variant" content="?target_hint=_self">
+<meta name="variant" content="?target_hint=_blank">
 <meta name="timeout" content="long">
 <script src="/common/utils.js"></script>
 <script src="/resources/testharness.js"></script>
@@ -10,6 +12,9 @@
 <script>
 setup(() => assertSpeculationRulesIsSupported());
 
+const params = new URLSearchParams(window.location.search);
+const target_hint = params.get('target_hint');
+
 promise_test(async t => {
   // The key used for storing a test result in the server.
   const key = token();
@@ -17,7 +22,7 @@
   // Open the test runner in a popup - it will prerender itself, record the
   // test results, and send them back to this harness.
   const url =
-    `resources/csp-script-src-elem-inline-speculation-rules.html?key=${key}`;
+    `resources/csp-script-src-elem-inline-speculation-rules.html?key=${key}&target_hint=${target_hint}`;
   window.open(url, '_blank', 'noopener');
 
   // Wait until the test sends us the results.
diff --git a/speculation-rules/prerender/csp-script-src-inline-speculation-rules.html b/speculation-rules/prerender/csp-script-src-inline-speculation-rules.html
index 6c3a817..b2f451d 100644
--- a/speculation-rules/prerender/csp-script-src-inline-speculation-rules.html
+++ b/speculation-rules/prerender/csp-script-src-inline-speculation-rules.html
@@ -1,5 +1,7 @@
 <!DOCTYPE html>
 <meta name="timeout" content="long">
+<meta name="variant" content="?target_hint=_self">
+<meta name="variant" content="?target_hint=_blank">
 <script src="/common/utils.js"></script>
 <script src="/resources/testharness.js"></script>
 <script src="/resources/testharnessreport.js"></script>
@@ -10,6 +12,9 @@
 <script>
 setup(() => assertSpeculationRulesIsSupported());
 
+const params = new URLSearchParams(window.location.search);
+const target_hint = params.get('target_hint');
+
 promise_test(async t => {
   // The key used for storing a test result in the server.
   const key = token();
@@ -17,7 +22,7 @@
   // Open the test runner in a popup - it will prerender itself, record the
   // test results, and send them back to this harness.
   const url =
-    `resources/csp-script-src-inline-speculation-rules.html?key=${key}`;
+    `resources/csp-script-src-inline-speculation-rules.html?key=${key}&target_hint=${target_hint}`;
   window.open(url, '_blank', 'noopener');
 
   // Wait until the test sends us the results.
diff --git a/speculation-rules/prerender/csp-script-src-self.html b/speculation-rules/prerender/csp-script-src-self.html
index 46fbfd7..0f9045d 100644
--- a/speculation-rules/prerender/csp-script-src-self.html
+++ b/speculation-rules/prerender/csp-script-src-self.html
@@ -1,4 +1,6 @@
 <!DOCTYPE html>
+<meta name="variant" content="?target_hint=_self">
+<meta name="variant" content="?target_hint=_blank">
 <meta name="timeout" content="long">
 <script src="/common/utils.js"></script>
 <script src="/resources/testharness.js"></script>
@@ -10,6 +12,9 @@
 <script>
 setup(() => assertSpeculationRulesIsSupported());
 
+const params = new URLSearchParams(window.location.search);
+const target_hint = params.get('target_hint');
+
 promise_test(async t => {
   // The key used for storing a test result in the server.
   const key = token();
@@ -17,7 +22,7 @@
   // Open the test runner in a popup - it will prerender itself, record the
   // test results, and send them back to this harness.
   const url =
-    `resources/csp-script-src-self.html?key=${key}`;
+    `resources/csp-script-src-self.html?key=${key}&target_hint=${target_hint}`;
   window.open(url, '_blank', 'noopener');
 
   // Wait until the test sends us the results.
diff --git a/speculation-rules/prerender/csp-script-src-strict-dynamic.html b/speculation-rules/prerender/csp-script-src-strict-dynamic.html
index d84ea3e..b27e802 100644
--- a/speculation-rules/prerender/csp-script-src-strict-dynamic.html
+++ b/speculation-rules/prerender/csp-script-src-strict-dynamic.html
@@ -1,4 +1,6 @@
 <!DOCTYPE html>
+<meta name="variant" content="?target_hint=_self">
+<meta name="variant" content="?target_hint=_blank">
 <meta name="timeout" content="long">
 <script src="/common/utils.js"></script>
 <script src="/resources/testharness.js"></script>
@@ -10,6 +12,9 @@
 <script>
 setup(() => assertSpeculationRulesIsSupported());
 
+const params = new URLSearchParams(window.location.search);
+const target_hint = params.get('target_hint');
+
 promise_test(async t => {
   // The key used for storing a test result in the server.
   const key = token();
@@ -17,7 +22,7 @@
   // Open the test runner in a popup - it will prerender itself, record the
   // test results, and send them back to this harness.
   const url =
-    `resources/csp-script-src-strict-dynamic.html?key=${key}`;
+    `resources/csp-script-src-strict-dynamic.html?key=${key}&target_hint=${target_hint}`;
   window.open(url, '_blank', 'noopener');
 
   // Wait until the test sends us the results.
diff --git a/speculation-rules/prerender/csp-script-src-unsafe-inline.html b/speculation-rules/prerender/csp-script-src-unsafe-inline.html
index ea2879b..cbe47cf 100644
--- a/speculation-rules/prerender/csp-script-src-unsafe-inline.html
+++ b/speculation-rules/prerender/csp-script-src-unsafe-inline.html
@@ -1,4 +1,6 @@
 <!DOCTYPE html>
+<meta name="variant" content="?target_hint=_self">
+<meta name="variant" content="?target_hint=_blank">
 <meta name="timeout" content="long">
 <script src="/common/utils.js"></script>
 <script src="/resources/testharness.js"></script>
@@ -10,6 +12,9 @@
 <script>
 setup(() => assertSpeculationRulesIsSupported());
 
+const params = new URLSearchParams(window.location.search);
+const target_hint = params.get('target_hint');
+
 promise_test(async t => {
   // The key used for storing a test result in the server.
   const key = token();
@@ -17,7 +22,7 @@
   // Open the test runner in a popup - it will prerender itself, record the
   // test results, and send them back to this harness.
   const url =
-    `resources/csp-script-src-unsafe-inline.html?key=${key}`;
+    `resources/csp-script-src-unsafe-inline.html?key=${key}&target_hint=${target_hint}`;
   window.open(url, '_blank', 'noopener');
 
   // Wait until the test sends us the results.
diff --git a/speculation-rules/prerender/resources/csp-script-src.js b/speculation-rules/prerender/resources/csp-script-src.js
index 866acaa..8927efb 100644
--- a/speculation-rules/prerender/resources/csp-script-src.js
+++ b/speculation-rules/prerender/resources/csp-script-src.js
@@ -3,6 +3,10 @@
 // Take a key used for storing a test result in the server.
 const key = params.get('key');
 
+// Take a target hint to decide a target context for prerendering.
+const target_hint = params.get('target_hint');
+const rule_extras = {target_hint};
+
 // Speculation rules injection is blocked in the csp-script-src 'self' test.
 const block = location.pathname.endsWith('csp-script-src-self.html');
 
@@ -37,7 +41,7 @@
     });
   }
 
-  startPrerendering(url.toString());
+  startPrerendering(url.toString(), rule_extras);
 
   // Wait until the prerendered page signals us it's ready to close.
   nextValueFromServer(done_key).then(() => {