1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29

content / test / data / browsing_topics / topics_service_worker.js [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

self.addEventListener('fetch', event => {
  event.waitUntil(async function () {
    if (!event.clientId) return;
    const client = await clients.get(event.clientId);
    if (!client) return;

    client.postMessage({
      url: event.request.url,
      topicsHeader: String(event.request.headers.get("Sec-Browsing-Topics"))
    });
  }());
});

self.addEventListener('message', event => {
  if (event.data.fetchUrl) {
    clients.matchAll().then((clients) => {
      fetch(event.data.fetchUrl, {browsingTopics: true}).then((response) => {
        // clients[0] is the most recently focused one
        clients[0].postMessage({
          finishedFetch: true
        });
      });
    });
  }
});