From 4d7801a898f9b9e1353167bc6058b275746ba593 Mon Sep 17 00:00:00 2001
From: Jonathan Lee <jonathanjlee@google.com>
Date: Fri, 3 May 2024 21:21:19 -0400
Subject: [PATCH] [wptrunner] Grant background-sync permission by default

This is necessary for reporting API tests to work in headless shell
(i.e., `chrome --headless=old`). See https://crbug.com/338470458.
---
 .../wptrunner/executors/executorchrome.py      | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/tools/wptrunner/wptrunner/executors/executorchrome.py b/tools/wptrunner/wptrunner/executors/executorchrome.py
index d531d51b4760ae..46d38f14c55ee4 100644
--- a/tools/wptrunner/wptrunner/executors/executorchrome.py
+++ b/tools/wptrunner/wptrunner/executors/executorchrome.py
@@ -226,6 +226,24 @@ def __init__(self, *args, reuse_window=False, **kwargs):
         super().__init__(*args, **kwargs)
         self.protocol.reuse_window = reuse_window
 
+    def setup(self, runner):
+        super().setup(runner)
+        # Chromium requires the `background-sync` permission for reporting APIs
+        # to work. Not all embedders (notably, `chrome --headless=old`) grant
+        # `background-sync` by default, so this CDP call ensures the permission
+        # is granted for all origins, in line with the background sync spec's
+        # recommendation [0].
+        #
+        # WebDriver's "Set Permission" command can only act on the test's
+        # origin, which may be too limited.
+        #
+        # [0]: https://wicg.github.io/background-sync/spec/#permission
+        params = {
+            "permission": {"name": "background-sync"},
+            "setting": "granted",
+        }
+        self.protocol.cdp.execute_cdp_command("Browser.setPermission", params)
+
 
 class ChromeDriverPrintRefTestExecutor(ChromeDriverRefTestExecutor):
     protocol_cls = ChromeDriverProtocol