Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 1 | // 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 Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 8 | #include <string_view> |
| 9 | |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 10 | #include "base/containers/flat_map.h" |
| 11 | #include "base/feature_list.h" |
Arthur Sonzogni | 0d8cb55 | 2024-02-15 16:16:32 | [diff] [blame] | 12 | #include "base/no_destructor.h" |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 13 | #include "base/values.h" |
| 14 | #include "content/public/browser/preloading.h" |
| 15 | |
| 16 | namespace content { |
| 17 | |
Simon Pelchat | b2ce6df | 2023-07-26 21:48:37 | [diff] [blame] | 18 | namespace test { |
| 19 | class PreloadingConfigOverride; |
| 20 | } // namespace test |
| 21 | |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 22 | class CONTENT_EXPORT PreloadingConfig { |
| 23 | public: |
Simon Pelchat | b2ce6df | 2023-07-26 21:48:37 | [diff] [blame] | 24 | PreloadingConfig(); |
| 25 | ~PreloadingConfig(); |
| 26 | |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 27 | 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 Pelchat | b2ce6df | 2023-07-26 21:48:37 | [diff] [blame] | 47 | friend class content::test::PreloadingConfigOverride; |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 48 | |
| 49 | struct Key { |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 50 | Key(std::string_view preloading_type, std::string_view predictdor); |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 51 | 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 Pelchat | b2ce6df | 2023-07-26 21:48:37 | [diff] [blame] | 69 | // 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 Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 80 | void SetHoldbackForTesting(std::string_view preloading_type, |
| 81 | std::string_view predictdor, |
Simon Pelchat | b2ce6df | 2023-07-26 21:48:37 | [diff] [blame] | 82 | bool holdback); |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 83 | |
| 84 | base::flat_map<Key, Entry, KeyCompare> entries_; |
| 85 | }; |
| 86 | |
| 87 | } // namespace content |
| 88 | |
| 89 | #endif // CONTENT_BROWSER_PRELOADING_PRELOADING_CONFIG_H_ |