Check DNT header for service worker's update checking

This CL adds a test to verify if DNT request header exists in a request for
update checking.

Bug: 972458
Change-Id: I58da5ce2bea9d952de2784548c6ca8a32ff9565a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1669132
Commit-Queue: Makoto Shimazu <shimazu@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Auto-Submit: Makoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#671581}
diff --git a/content/browser/do_not_track_browsertest.cc b/content/browser/do_not_track_browsertest.cc
index 957c0f8..4177c35c 100644
--- a/content/browser/do_not_track_browsertest.cc
+++ b/content/browser/do_not_track_browsertest.cc
@@ -2,6 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "base/barrier_closure.h"
 #include "base/bind.h"
 #include "base/strings/utf_string_conversions.h"
 #include "build/build_config.h"
@@ -181,7 +182,8 @@
   EXPECT_TRUE(header_map.find("DNT") != header_map.end());
   EXPECT_EQ("1", header_map["DNT"]);
 
-  // Wait until the worker script is loaded.
+  // Wait until the worker script is loaded to stop the test from crashing
+  // during destruction.
   EXPECT_EQ("DONE", EvalJs(shell(), "waitForMessage();"));
 }
 
@@ -212,13 +214,14 @@
   EXPECT_TRUE(header_map.find("DNT") != header_map.end());
   EXPECT_EQ("1", header_map["DNT"]);
 
-  // Wait until the worker script is loaded.
+  // Wait until the worker script is loaded to stop the test from crashing
+  // during destruction.
   EXPECT_EQ("DONE", EvalJs(shell(), "waitForMessage();"));
 }
 
 // Checks that the DNT header is sent in a request for a service worker
 // script.
-IN_PROC_BROWSER_TEST_F(DoNotTrackTest, ServiceWorker) {
+IN_PROC_BROWSER_TEST_F(DoNotTrackTest, ServiceWorker_Register) {
   const std::string kWorkerScript = "// empty";
   net::test_server::HttpRequest::HeaderMap header_map;
   base::RunLoop loop;
@@ -241,6 +244,35 @@
   // been completed.
 }
 
+// Checks that the DNT header is sent in a request for a service worker
+// script during update checking.
+IN_PROC_BROWSER_TEST_F(DoNotTrackTest, ServiceWorker_Update) {
+  const std::string kWorkerScript = "// empty";
+  net::test_server::HttpRequest::HeaderMap header_map;
+  base::RunLoop loop;
+  // Wait for two requests to capture the request header for updating.
+  embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
+      &CaptureHeaderHandlerAndReturnScript, "/capture", &header_map,
+      kWorkerScript, base::BarrierClosure(2, loop.QuitClosure())));
+  ASSERT_TRUE(embedded_test_server()->Start());
+  if (!EnableDoNotTrack())
+    return;
+
+  // Register a service worker, trigger update, then wait until the handler sees
+  // the second request.
+  NavigateToURL(shell(), GetURL("/service_worker/create_service_worker.html"));
+  EXPECT_EQ("DONE", EvalJs(shell(), "register('/capture');"));
+  EXPECT_EQ("DONE", EvalJs(shell(), "update();"));
+  loop.Run();
+
+  EXPECT_TRUE(header_map.find("DNT") != header_map.end());
+  EXPECT_EQ("1", header_map["DNT"]);
+
+  // Service worker doesn't have to wait for onmessage event because
+  // waiting for a promise by registration.update() can ensure that the script
+  // load has been completed.
+}
+
 // Checks that the DNT header is preserved when fetching from a dedicated
 // worker.
 IN_PROC_BROWSER_TEST_F(DoNotTrackTest, FetchFromWorker) {
diff --git a/content/test/data/service_worker/create_service_worker.html b/content/test/data/service_worker/create_service_worker.html
index 1dbb35b6..76bc0da 100644
--- a/content/test/data/service_worker/create_service_worker.html
+++ b/content/test/data/service_worker/create_service_worker.html
@@ -11,5 +11,15 @@
     return `${error}`;
   }
 }
+
+async function update() {
+  try {
+    const registration = await navigator.serviceWorker.ready;
+    await registration.update();
+    return 'DONE';
+  } catch (error) {
+    return `${error}`;
+  }
+}
 </script>
 </html>