Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Makoto Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 2 | // 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 Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 7 | #include "content/public/browser/browser_context.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 8 | #include "content/public/browser/browser_thread.h" |
Makoto Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 9 | #include "content/public/browser/content_browser_client.h" |
| 10 | #include "content/public/common/content_client.h" |
Makoto Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 11 | #include "net/http/http_request_headers.h" |
Minggang Wang | 105c243a | 2021-04-26 15:59:52 | [diff] [blame] | 12 | #include "third_party/blink/public/common/loader/loader_constants.h" |
Mario Sanchez Prada | 0bd8b8c | 2020-10-21 17:49:23 | [diff] [blame] | 13 | #include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h" |
Makoto Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 14 | |
| 15 | namespace content { |
| 16 | |
Makoto Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 17 | void UpdateAdditionalHeadersForBrowserInitiatedRequest( |
| 18 | net::HttpRequestHeaders* headers, |
| 19 | BrowserContext* browser_context, |
| 20 | bool should_update_existing_headers, |
Ari Chivukula | 116f0fc4 | 2022-04-13 23:26:18 | [diff] [blame] | 21 | const blink::RendererPreferences& renderer_preferences, |
| 22 | bool is_for_worker_script) { |
Makoto Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 23 | 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 Wang | 105c243a | 2021-04-26 15:59:52 | [diff] [blame] | 29 | headers->RemoveHeader(blink::kDoNotTrackHeader); |
Makoto Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 30 | } |
Minggang Wang | 105c243a | 2021-04-26 15:59:52 | [diff] [blame] | 31 | headers->SetHeaderIfMissing(blink::kDoNotTrackHeader, "1"); |
Makoto Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 32 | } |
| 33 | |
Alison Gale | 47d1537d | 2024-04-19 21:31:46 | [diff] [blame] | 34 | // TODO(crbug.com/40833603): WARNING: This bypasses the permissions policy. |
Ari Chivukula | 116f0fc4 | 2022-04-13 23:26:18 | [diff] [blame] | 35 | // 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 Shimazu | 71a0633 | 2019-07-17 06:45:19 | [diff] [blame] | 42 | if (should_update_existing_headers) { |
| 43 | headers->RemoveHeader("Save-Data"); |
| 44 | } |
| 45 | headers->SetHeaderIfMissing("Save-Data", "on"); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | } // namespace content |