blob: 34e1fc28ea554b123a067cce1ec7818600c0686d [file] [log] [blame]
Simon Pelchat9ed3e992023-02-17 01:16:161// 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_PRELOADING_PRELOADING_CONFIG_H_
6#define CONTENT_BROWSER_PRELOADING_PRELOADING_CONFIG_H_
7
Md Hasibul Hasana963a9342024-04-03 10:15:148#include <string_view>
9
Simon Pelchat9ed3e992023-02-17 01:16:1610#include "base/containers/flat_map.h"
11#include "base/feature_list.h"
Arthur Sonzogni0d8cb552024-02-15 16:16:3212#include "base/no_destructor.h"
Simon Pelchat9ed3e992023-02-17 01:16:1613#include "base/values.h"
14#include "content/public/browser/preloading.h"
15
16namespace content {
17
Simon Pelchatb2ce6df2023-07-26 21:48:3718namespace test {
19class PreloadingConfigOverride;
20} // namespace test
21
Simon Pelchat9ed3e992023-02-17 01:16:1622class CONTENT_EXPORT PreloadingConfig {
23 public:
Simon Pelchatb2ce6df2023-07-26 21:48:3724 PreloadingConfig();
25 ~PreloadingConfig();
26
Simon Pelchat9ed3e992023-02-17 01:16:1627 static PreloadingConfig& GetInstance();
28
29 // Whether the given (|preloading_type|, |predictor|) combination should be
30 // held back in order to evaluate how well this type of preloading is
31 // performing. This is controlled via field trial configuration.
32 bool ShouldHoldback(PreloadingType preloading_type,
33 PreloadingPredictor predictor);
34
35 // Whether the given (|preloading_type|, |predictor|) combination logging
36 // should be sampled. Some types of preloading trigger more than others so
37 // we randomly drop logging for a fraction of page loads of the more noisy
38 // preloading. The sampling rate is configured via field trial.
39 double SamplingLikelihood(PreloadingType preloading_type,
40 PreloadingPredictor predictor);
41
42 // Initializes the PreloadingConfig from the FeatureParams. Exported
43 // publicly only for tests.
44 void ParseConfig();
45
46 private:
Simon Pelchatb2ce6df2023-07-26 21:48:3747 friend class content::test::PreloadingConfigOverride;
Simon Pelchat9ed3e992023-02-17 01:16:1648
49 struct Key {
Md Hasibul Hasana963a9342024-04-03 10:15:1450 Key(std::string_view preloading_type, std::string_view predictdor);
Simon Pelchat9ed3e992023-02-17 01:16:1651 static Key FromEnums(PreloadingType preloading_type,
52 PreloadingPredictor predictor);
53
54 std::string preloading_type_;
55 std::string predictor_;
56 };
57
58 struct Entry {
59 static Entry FromDict(const base::Value::Dict* dict);
60
61 bool holdback_ = false;
62 float sampling_likelihood_ = 1.0;
63 };
64
65 struct KeyCompare {
66 bool operator()(const Key& lhs, const Key& rhs) const;
67 };
68
Simon Pelchatb2ce6df2023-07-26 21:48:3769 // Overrides the PreloadingConfig for testing. Returns the previous override,
70 // if any. The caller is responsible for calling OverrideForTesting with the
71 // previous value once they're done.
72 static PreloadingConfig* OverrideForTesting(
73 PreloadingConfig* config_override);
74
75 // Sets whether the given feature should be held back (disabled) and prevents
76 // sampling UKM logs for that feature.
77 void SetHoldbackForTesting(PreloadingType preloading_type,
78 PreloadingPredictor predictor,
79 bool holdback);
Md Hasibul Hasana963a9342024-04-03 10:15:1480 void SetHoldbackForTesting(std::string_view preloading_type,
81 std::string_view predictdor,
Simon Pelchatb2ce6df2023-07-26 21:48:3782 bool holdback);
Simon Pelchat9ed3e992023-02-17 01:16:1683
84 base::flat_map<Key, Entry, KeyCompare> entries_;
85};
86
87} // namespace content
88
89#endif // CONTENT_BROWSER_PRELOADING_PRELOADING_CONFIG_H_