Refactor partitioned service worker 3p window for reusability

Future partitioned service worker tests are likely to follow the
pattern of opening a window in a third-party context and then
having that window spawn an iframe that is same origin to the test.

This change modifies the existing 3p window file to allow the test
to specify which url the window should iframe. Allowing for reuse
between tests.

Bug: 1246549
Change-Id: I07726baad26fc6ac54538344c58b59dfcfe7f6a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3259316
Commit-Queue: Steven Bingler <bingler@chromium.org>
Reviewed-by: Ben Kelly <wanderview@chromium.org>
Cr-Commit-Position: refs/heads/main@{#938562}
diff --git a/service-workers/service-worker/partitioned-service-worker.tentative.https.html b/service-workers/service-worker/partitioned-service-worker.tentative.https.html
index c006e09..b31b0ef 100644
--- a/service-workers/service-worker/partitioned-service-worker.tentative.https.html
+++ b/service-workers/service-worker/partitioned-service-worker.tentative.https.html
@@ -38,6 +38,8 @@
 
 // Loads a url for the frame type and then returns a promise for
 // the data that was postMessage'd from the loaded frame.
+// If the frame type is 'window' then `url` is encoded into the search param
+// as the url the 3p window is meant to iframe.
 function loadAndReturnSwData(t, url, frame_type) {
   if (frame_type !== 'iframe' && frame_type !== 'window') {
     return;
@@ -55,7 +57,15 @@
   }
   else {
     // 'window' case.
-    const w = window.open(url);
+    const search_param = new URLSearchParams();
+    search_param.append('target', url);
+
+    const third_party_window_url = new URL(
+    './resources/partitioned-service-worker-third-party-window.html' +
+    '?' + search_param,
+    get_host_info().HTTPS_NOTSAMESITE_ORIGIN + self.location.pathname);
+
+    const w = window.open(third_party_window_url);
     t.add_cleanup(() => w.close());
   }
 
@@ -90,13 +100,13 @@
     'The data for the 1p frame came from the wrong source');
 
   // Now create a 3p iframe that will try to resolve the SW in a 3p context.
-  const third_party_url = new URL(
-    './resources/partitioned-service-worker-third-party-window.html',
-    get_host_info().HTTPS_NOTSAMESITE_ORIGIN + self.location.pathname);
+  const third_party_iframe_url = new URL(
+    './resources/partitioned-service-worker-third-party-iframe.html',
+    get_host_info().HTTPS_ORIGIN + self.location.pathname);
 
   // Create the 3p window (which will in turn create the iframe with the SW)
   // and await on its data.
-  const frame_3p_data = await loadAndReturnSwData(t, third_party_url, 'window');
+  const frame_3p_data = await loadAndReturnSwData(t, third_party_iframe_url, 'window');
   assert_equals(frame_3p_data.source, 'From3pFrame',
     'The data for the 3p frame came from the wrong source');
 
diff --git a/service-workers/service-worker/resources/partitioned-service-worker-third-party-window.html b/service-workers/service-worker/resources/partitioned-service-worker-third-party-window.html
index 21a8380..65ac002 100644
--- a/service-workers/service-worker/resources/partitioned-service-worker-third-party-window.html
+++ b/service-workers/service-worker/resources/partitioned-service-worker-third-party-window.html
@@ -5,14 +5,9 @@
 
 
 <body>
-This page should be opened as a third-party window. It then loads an iframe that
-registers a service worker that, in turn, attempts to resolve its internal
-Promise. Once that happens this window will postMessage back to the test to let
-it know that the resolution occured.
-
-If 3p partitioning is active then this Promise resolution won't
-resolve the promise in the test's (that originally opened this page) 1p
-service worker.
+This page should be opened as a third-party window. It then loads an iframe
+specified by the query parameter. Finally it forwards the postMessage from the
+iframe up to the opener (the test).
 
 <script>
 
@@ -23,9 +18,8 @@
     });
   });
 
-  const iframe_url = new URL(
-    './partitioned-service-worker-third-party-iframe.html',
-    get_host_info().HTTPS_ORIGIN + self.location.pathname);
+  const search_param = new URLSearchParams(window.location.search);
+  const iframe_url = search_param.get('target');
 
   var frame = document.createElement('iframe');
   frame.src = iframe_url;