blob: a8cdb42779b9897f3d02434c1834814f9c3e3409 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2019 The Chromium Authors
Makoto Shimazu71a06332019-07-17 06:45:192// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/loader/browser_initiated_resource_request.h"
6
Makoto Shimazu71a06332019-07-17 06:45:197#include "content/public/browser/browser_context.h"
Hans Wennborg5ffd1392019-10-16 11:00:028#include "content/public/browser/browser_thread.h"
Makoto Shimazu71a06332019-07-17 06:45:199#include "content/public/browser/content_browser_client.h"
10#include "content/public/common/content_client.h"
Makoto Shimazu71a06332019-07-17 06:45:1911#include "net/http/http_request_headers.h"
Minggang Wang105c243a2021-04-26 15:59:5212#include "third_party/blink/public/common/loader/loader_constants.h"
Mario Sanchez Prada0bd8b8c2020-10-21 17:49:2313#include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h"
Makoto Shimazu71a06332019-07-17 06:45:1914
15namespace content {
16
Makoto Shimazu71a06332019-07-17 06:45:1917void UpdateAdditionalHeadersForBrowserInitiatedRequest(
18 net::HttpRequestHeaders* headers,
19 BrowserContext* browser_context,
20 bool should_update_existing_headers,
Ari Chivukula116f0fc42022-04-13 23:26:1821 const blink::RendererPreferences& renderer_preferences,
22 bool is_for_worker_script) {
Makoto Shimazu71a06332019-07-17 06:45:1923 DCHECK_CURRENTLY_ON(BrowserThread::UI);
24
25 // Set the DoNotTrack header if appropriate.
26 // https://w3c.github.io/dnt/drafts/tracking-dnt.html#expression-format
27 if (renderer_preferences.enable_do_not_track) {
28 if (should_update_existing_headers) {
Minggang Wang105c243a2021-04-26 15:59:5229 headers->RemoveHeader(blink::kDoNotTrackHeader);
Makoto Shimazu71a06332019-07-17 06:45:1930 }
Minggang Wang105c243a2021-04-26 15:59:5231 headers->SetHeaderIfMissing(blink::kDoNotTrackHeader, "1");
Makoto Shimazu71a06332019-07-17 06:45:1932 }
33
Alison Gale47d1537d2024-04-19 21:31:4634 // TODO(crbug.com/40833603): WARNING: This bypasses the permissions policy.
Ari Chivukula116f0fc42022-04-13 23:26:1835 // Unfortunately, workers lack a permissions policy and to derive proper hints
36 // https://github.com/w3c/webappsec-permissions-policy/issues/207.
37 // Save-Data was previously included in hints for workers, thus we cannot
38 // remove it for the time being. If you're reading this, consider building
39 // permissions policies for workers and/or deprecating this inclusion.
40 if (is_for_worker_script &&
41 GetContentClient()->browser()->IsDataSaverEnabled(browser_context)) {
Makoto Shimazu71a06332019-07-17 06:45:1942 if (should_update_existing_headers) {
43 headers->RemoveHeader("Save-Data");
44 }
45 headers->SetHeaderIfMissing("Save-Data", "on");
46 }
47}
48
49} // namespace content