blob: a5250b5a22c6a743f87e4c8dbbdc70e4ca6822cd [file] [log] [blame]
Mike Jacksone2aa7af2023-05-17 06:45:071// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_SYSTEM_ENTROPY_UTILS_H_
6#define CONTENT_BROWSER_RENDERER_HOST_SYSTEM_ENTROPY_UTILS_H_
7
8#include "content/browser/renderer_host/frame_tree_node.h"
9#include "content/public/common/content_client.h"
10#include "third_party/blink/public/mojom/navigation/system_entropy.mojom.h"
11
12namespace content {
13
14class SystemEntropyUtils {
15 public:
16 // Determines the current system entropy value for `frame_tree_node`.
17 //
18 // Returns `SystemEntropy::kHigh` for all non-off-the-record top level
19 // navigations that occur between the start of the browser process up until
20 // the first visible page completes loading.
21 //
22 // Returns `SystemEntropy::kNormal` for all off-the-record top level
23 // navigations, or top level navigations that occur after the first visible
24 // page has completed loading.
25 //
26 // Returns `SystemEntropy::kEmpty` for all framed navigations, or if the
27 // `suggested_system_entropy` is `SystemEntropy::kEmpty`.
28 static blink::mojom::SystemEntropy ComputeSystemEntropyForFrameTreeNode(
29 FrameTreeNode* frame_tree_node,
30 blink::mojom::SystemEntropy suggested_system_entropy) {
31 // If the suggested system entropy is `SystemEntropy::kEmpty`, or this is
32 // a framed navigation, return `SystemEntropy::kEmpty`.
33 if (suggested_system_entropy == blink::mojom::SystemEntropy::kEmpty ||
34 !frame_tree_node->IsOutermostMainFrame()) {
35 return blink::mojom::SystemEntropy::kEmpty;
36 }
37
38 // Off-the-record navigations always return `SystemEntropy::kNormal`.
39 if (frame_tree_node->navigator()
40 .controller()
41 .GetBrowserContext()
42 ->IsOffTheRecord()) {
43 return blink::mojom::SystemEntropy::kNormal;
44 }
45
46 // During browser startup, return `SystemEntropy::kHigh`.
47 if (!GetContentClient()->browser()->IsBrowserStartupComplete()) {
48 return blink::mojom::SystemEntropy::kHigh;
49 }
50
51 CHECK_NE(suggested_system_entropy, blink::mojom::SystemEntropy::kEmpty);
52 return suggested_system_entropy;
53 }
54};
55} // namespace content
56
57#endif // CONTENT_BROWSER_RENDERER_HOST_SYSTEM_ENTROPY_UTILS_H_