File: optimization_guide_features.cc

package info (click to toggle)
chromium 144.0.7559.109-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,915,868 kB
  • sloc: cpp: 35,866,215; ansic: 7,599,035; javascript: 3,623,761; python: 1,639,407; xml: 833,084; asm: 716,173; pascal: 185,323; sh: 88,763; perl: 88,699; objc: 79,984; sql: 58,217; cs: 42,430; fortran: 24,101; makefile: 20,747; tcl: 15,277; php: 14,022; yacc: 9,059; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (607 lines) | stat: -rw-r--r-- 22,747 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/optimization_guide/core/optimization_guide_features.h"

#include <cstring>
#include <optional>

#include "base/byte_count.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/containers/enum_set.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/to_string.h"
#include "base/system/sys_info.h"
#include "base/time/time.h"
#include "components/optimization_guide/core/feature_registry/mqls_feature_registry.h"
#include "components/optimization_guide/core/model_execution/feature_keys.h"
#include "components/optimization_guide/core/optimization_guide_constants.h"
#include "components/optimization_guide/core/optimization_guide_enums.h"
#include "components/optimization_guide/core/optimization_guide_switches.h"
#include "components/optimization_guide/machine_learning_tflite_buildflags.h"
#include "components/optimization_guide/proto/common_types.pb.h"
#include "components/variations/hashing.h"
#include "google_apis/gaia/gaia_constants.h"
#include "google_apis/google_api_keys.h"
#include "net/base/url_util.h"
#include "ui/base/l10n/l10n_util.h"

namespace optimization_guide {
namespace features {

namespace {

constexpr auto enabled_by_default_mobile_only =
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
    base::FEATURE_ENABLED_BY_DEFAULT;
#else
    base::FEATURE_DISABLED_BY_DEFAULT;
#endif

}  // namespace

// Enables the syncing of the Optimization Hints component, which provides
// hints for what optimizations can be applied on a page load.
BASE_FEATURE(kOptimizationHints, base::FEATURE_ENABLED_BY_DEFAULT);

// Enables the prediction of optimization targets.
BASE_FEATURE(kOptimizationTargetPrediction, base::FEATURE_ENABLED_BY_DEFAULT);

// This feature flag does not turn off any behavior, it is only used for
// experiment parameters.
BASE_FEATURE(kPageTextExtraction,
             "OptimizationGuidePageContentExtraction",
             base::FEATURE_ENABLED_BY_DEFAULT);

// Enables the validation of optimization guide metadata.
BASE_FEATURE(kOptimizationGuideMetadataValidation,
             base::FEATURE_DISABLED_BY_DEFAULT);

BASE_FEATURE(kPreventLongRunningPredictionModels,
             base::FEATURE_ENABLED_BY_DEFAULT);

BASE_FEATURE(kOverrideNumThreadsForModelExecution,
             base::FEATURE_DISABLED_BY_DEFAULT);

// Killswitch for fetching on search results from a remote Optimization Guide
// Service.
BASE_FEATURE(kOptimizationGuideFetchingForSRP,
             "OptimizationHintsFetchingSRP",
             base::FEATURE_ENABLED_BY_DEFAULT);

// Kill switch for disabling model quality logging.
BASE_FEATURE(kModelQualityLogging, base::FEATURE_ENABLED_BY_DEFAULT);

// An emergency kill switch feature to stop serving certain model versions per
// optimization target. This is useful in exceptional situations when a bad
// model version got served that lead to crashes or critical failures, and an
// immediate remedy is needed to stop serving those versions.
BASE_FEATURE(kOptimizationGuidePredictionModelKillswitch,
             base::FEATURE_DISABLED_BY_DEFAULT);

// Whether to enable model execution.
BASE_FEATURE(kOptimizationGuideModelExecution,
             base::FEATURE_ENABLED_BY_DEFAULT);

// Whether to use the on device model service in optimization guide.
BASE_FEATURE(kOptimizationGuideOnDeviceModel,
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_CHROMEOS)
             base::FEATURE_ENABLED_BY_DEFAULT);
#else
             base::FEATURE_DISABLED_BY_DEFAULT);
#endif

// Whether the on device service is launched after a delay on startup to log
// metrics.
BASE_FEATURE(kLogOnDeviceMetricsOnStartup, base::FEATURE_DISABLED_BY_DEFAULT);

// Whether to download the text safety classifier model.
BASE_FEATURE(kTextSafetyClassifier, base::FEATURE_ENABLED_BY_DEFAULT);

// Whether to scan the full text when running the language detection in the text
// safety classifier.
BASE_FEATURE(kTextSafetyScanLanguageDetection,
             base::FEATURE_DISABLED_BY_DEFAULT);

// Whether performance class should be fetched each startup or just after a
// version update.
BASE_FEATURE(kOnDeviceModelFetchPerformanceClassEveryStartup,
             base::FEATURE_DISABLED_BY_DEFAULT);

// Force show the AI page and all AI feature sub-pages in settings, even if they
// would be unavailable otherwise. This is meant for development and test
// purposes only.
BASE_FEATURE(kAiSettingsPageForceAvailable, base::FEATURE_DISABLED_BY_DEFAULT);

BASE_FEATURE(kOnDeviceModelPerformanceParams, base::FEATURE_ENABLED_BY_DEFAULT);

BASE_FEATURE(kAnnotatedPageContentWithActionableElements,
             base::FEATURE_DISABLED_BY_DEFAULT);

BASE_FEATURE(kAnnotatedPageContentWithMediaData,
             base::FEATURE_DISABLED_BY_DEFAULT);

const base::FeatureParam<std::string> kPerformanceClassListForOnDeviceModel{
    &kOnDeviceModelPerformanceParams,
    "compatible_on_device_performance_classes", "3,4,5,6"};

const base::FeatureParam<std::string>
    kLowTierPerformanceClassListForOnDeviceModel{
        &kOnDeviceModelPerformanceParams,
        "compatible_low_tier_on_device_performance_classes", "3,4"};

const base::FeatureParam<std::string> kPerformanceClassListForImageInput{
    &kOnDeviceModelPerformanceParams,
    "compatible_on_device_performance_classes_image_input", "3,4,5,6"};

const base::FeatureParam<std::string> kPerformanceClassListForAudioInput{
    &kOnDeviceModelPerformanceParams,
    "compatible_on_device_performance_classes_audio_input", "5,6"};

BASE_FEATURE(kOptimizationGuideIconView, base::FEATURE_DISABLED_BY_DEFAULT);

BASE_FEATURE(kBrokerModelSessionsForUntrustedProcesses,
             base::FEATURE_DISABLED_BY_DEFAULT);

// Enables proactively sending GAIA information to the Optimization Guide
// Service.
BASE_FEATURE(kOptimizationGuideProactivePersonalizedHintsFetching,
             base::FEATURE_DISABLED_BY_DEFAULT);

BASE_FEATURE(kOptimizationGuideBypassFormsClassificationAuth,
             base::FEATURE_DISABLED_BY_DEFAULT);

// Controls whether to enforce a timeout for subframe page content extraction.
// If enabled, defaults to 1 second. If disabled, wait indefinitely for all
// subframes to respond.
BASE_FEATURE(kGetAIPageContentSubframeTimeoutEnabled,
             base::FEATURE_ENABLED_BY_DEFAULT);
const base::FeatureParam<base::TimeDelta> kGetAIPageContentSubframeTimeoutParam{
    &kGetAIPageContentSubframeTimeoutEnabled, "timeout", base::Seconds(1)};

// Controls whether to enforce a timeout for main frame page content extraction.
// If enabled, defaults to 10 seconds. If disabled, wait indefinitely for the
// main frame to respond.
BASE_FEATURE(kGetAIPageContentMainFrameTimeoutEnabled,
             base::FEATURE_ENABLED_BY_DEFAULT);
const base::FeatureParam<base::TimeDelta>
    kGetAIPageContentMainFrameTimeoutParam{
        &kGetAIPageContentMainFrameTimeoutEnabled, "timeout",
        base::Seconds(10)};

// The default value here is a bit of a guess.
// TODO(crbug.com/40163041): This should be tuned once metrics are available.
base::TimeDelta PageTextExtractionOutstandingRequestsGracePeriod() {
  return base::Milliseconds(GetFieldTrialParamByFeatureAsInt(
      kPageTextExtraction, "outstanding_requests_grace_period_ms", 1000));
}

size_t MaxResultsForSRPFetch() {
  static int max_urls = GetFieldTrialParamByFeatureAsInt(
      kOptimizationGuideFetchingForSRP, "max_urls_for_srp_fetch", 10);
  return max_urls;
}

std::string GetOptimizationGuideServiceAPIKey() {
  // Command line override takes priority.
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(switches::kOptimizationGuideServiceAPIKey)) {
    return command_line->GetSwitchValueASCII(
        switches::kOptimizationGuideServiceAPIKey);
  }

  return google_apis::GetAPIKey();
}

GURL GetOptimizationGuideServiceGetModelsURL() {
  // Command line override takes priority.
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(
          switches::kOptimizationGuideServiceGetModelsURL)) {
    // Assume the command line switch is correct and return it.
    return GURL(command_line->GetSwitchValueASCII(
        switches::kOptimizationGuideServiceGetModelsURL));
  }

  GURL get_models_url(kOptimizationGuideServiceGetModelsDefaultURL);
  CHECK(get_models_url.SchemeIs(url::kHttpsScheme));
  return get_models_url;
}

bool IsOptimizationTargetPredictionEnabled() {
  return base::FeatureList::IsEnabled(kOptimizationTargetPrediction);
}

bool IsOptimizationHintsEnabled() {
  return base::FeatureList::IsEnabled(kOptimizationHints);
}

bool IsModelQualityLoggingEnabled() {
  return base::FeatureList::IsEnabled(kModelQualityLogging);
}

bool IsModelQualityLoggingEnabledForFeature(
    const MqlsFeatureMetadata* metadata) {
  if (!IsModelQualityLoggingEnabled()) {
    return false;
  }

  return metadata->LoggingEnabledViaFieldTrial();
}

bool IsSRPFetchingEnabled() {
  return base::FeatureList::IsEnabled(kOptimizationGuideFetchingForSRP);
}

bool IsPushNotificationsEnabled() {
  return enabled_by_default_mobile_only;
}

size_t MaxHostKeyedHintCacheSize() {
  size_t max_host_keyed_hint_cache_size = GetFieldTrialParamByFeatureAsInt(
      kOptimizationHints, "max_host_keyed_hint_cache_size", 30);
  return max_host_keyed_hint_cache_size;
}

bool ShouldPersistHintsToDisk() {
  return GetFieldTrialParamByFeatureAsBool(kOptimizationHints,
                                           "persist_hints_to_disk", true);
}

RequestContextSet GetAllowedContextsForPersonalizedMetadata() {
  RequestContextSet allowed_contexts;
  allowed_contexts.Put(proto::RequestContext::CONTEXT_PAGE_INSIGHTS_HUB);
  return allowed_contexts;
}

OptimizationTypeSet GetAllowedOptimizationTypesForProactivePersonalization() {
  OptimizationTypeSet allowed_optimization_types;
  if (!base::FeatureList::IsEnabled(
          kOptimizationGuideProactivePersonalizedHintsFetching)) {
    return allowed_optimization_types;
  }
  base::FieldTrialParams params;
  if (base::GetFieldTrialParamsByFeature(
          kOptimizationGuideProactivePersonalizedHintsFetching, &params) &&
      params.contains("allowed_optimization_types")) {
    for (const auto& context_str : base::SplitString(
             base::GetFieldTrialParamValueByFeature(
                 kOptimizationGuideProactivePersonalizedHintsFetching,
                 "allowed_optimization_types"),
             ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
      proto::OptimizationType optimization_type;
      if (proto::OptimizationType_Parse(context_str, &optimization_type)) {
        allowed_optimization_types.Put(optimization_type);
      }
    }
  }
  return allowed_optimization_types;
}

base::TimeDelta PredictionModelFetchRandomMinDelay() {
  return base::Seconds(GetFieldTrialParamByFeatureAsInt(
      kOptimizationTargetPrediction, "fetch_random_min_delay_secs", 30));
}

base::TimeDelta PredictionModelFetchRandomMaxDelay() {
  return base::Seconds(GetFieldTrialParamByFeatureAsInt(
      kOptimizationTargetPrediction, "fetch_random_max_delay_secs", 60));
}

base::TimeDelta PredictionModelFetchRetryDelay() {
  return base::Minutes(GetFieldTrialParamByFeatureAsInt(
      kOptimizationTargetPrediction, "fetch_retry_minutes", 2));
}

base::TimeDelta PredictionModelFetchStartupDelay() {
  return base::Milliseconds(GetFieldTrialParamByFeatureAsInt(
      kOptimizationTargetPrediction, "fetch_startup_delay_ms", 10000));
}

base::TimeDelta PredictionModelFetchInterval() {
  return base::Hours(GetFieldTrialParamByFeatureAsInt(
      kOptimizationTargetPrediction, "fetch_interval_hours", 24));
}

base::TimeDelta PredictionModelNewRegistrationFetchRandomDelay() {
  static const base::FeatureParam<base::TimeDelta> kMinDelay{
      &kOptimizationTargetPrediction, "new_registration_fetch_min_delay",
      base::Seconds(5)};
  static const base::FeatureParam<base::TimeDelta> kMaxDelay{
      &kOptimizationTargetPrediction, "new_registration_fetch_max_delay",
      base::Seconds(10)};
  return base::RandTimeDelta(kMinDelay.Get(), kMaxDelay.Get());
}

bool IsModelExecutionWatchdogEnabled() {
  return base::FeatureList::IsEnabled(kPreventLongRunningPredictionModels);
}

base::TimeDelta ModelExecutionWatchdogDefaultTimeout() {
  return base::Milliseconds(GetFieldTrialParamByFeatureAsInt(
      kPreventLongRunningPredictionModels, "model_execution_timeout_ms",
#if defined(_DEBUG)
      // Debug builds take a much longer time to run.
      60 * 1000
#else
      2000
#endif
      ));
}

bool ShouldMetadataValidationFetchHostKeyed() {
  DCHECK(base::FeatureList::IsEnabled(kOptimizationGuideMetadataValidation));
  return GetFieldTrialParamByFeatureAsBool(kOptimizationGuideMetadataValidation,
                                           "is_host_keyed", true);
}

bool ShouldDeferStartupActiveTabsHintsFetch() {
  return GetFieldTrialParamByFeatureAsBool(
      kOptimizationHints, "defer_startup_active_tabs_hints_fetch",
#if BUILDFLAG(IS_ANDROID)
      true
#else
      false
#endif
  );
}

std::optional<int> OverrideNumThreadsForOptTarget(
    proto::OptimizationTarget opt_target) {
  if (!base::FeatureList::IsEnabled(kOverrideNumThreadsForModelExecution)) {
    return std::nullopt;
  }

  // 0 is an invalid value to pass to TFLite, so make that nullopt. -1 is valid,
  // but not anything less than that.
  int num_threads = GetFieldTrialParamByFeatureAsInt(
      kOverrideNumThreadsForModelExecution,
      proto::OptimizationTarget_Name(opt_target), 0);
  if (num_threads == 0 || num_threads < -1) {
    return std::nullopt;
  }

  // Cap to the number of CPUs on the device.
  return std::min(num_threads, base::SysInfo::NumberOfProcessors());
}

std::map<proto::OptimizationTarget, std::set<int64_t>>
GetPredictionModelVersionsInKillSwitch() {
  if (!base::FeatureList::IsEnabled(
          kOptimizationGuidePredictionModelKillswitch)) {
    return {};
  }
  base::FieldTrialParams killswitch_params;
  if (!GetFieldTrialParamsByFeature(kOptimizationGuidePredictionModelKillswitch,
                                    &killswitch_params)) {
    return {};
  }
  std::map<proto::OptimizationTarget, std::set<int64_t>>
      killswitch_model_versions;
  for (const auto& killswitch_param : killswitch_params) {
    proto::OptimizationTarget opt_target;
    if (!proto::OptimizationTarget_Parse(killswitch_param.first, &opt_target)) {
      continue;
    }
    for (const std::string& opt_taget_killswitch_version :
         base::SplitString(killswitch_param.second, ",", base::TRIM_WHITESPACE,
                           base::SPLIT_WANT_NONEMPTY)) {
      int64_t opt_taget_killswitch_version_num;
      if (base::StringToInt64(opt_taget_killswitch_version,
                              &opt_taget_killswitch_version_num)) {
        killswitch_model_versions[opt_target].insert(
            opt_taget_killswitch_version_num);
      }
    }
  }
  return killswitch_model_versions;
}

bool ShouldLoadOnDeviceModelExecutionConfigWithHigherPriority() {
  return base::GetFieldTrialParamByFeatureAsBool(
      kOptimizationGuideOnDeviceModel, "ondevice_config_high_priority", true);
}

base::TimeDelta GetOnDeviceModelIdleTimeout() {
  static const base::FeatureParam<base::TimeDelta>
      kOnDeviceModelServiceIdleTimeout{&kOptimizationGuideOnDeviceModel,
                                       "on_device_model_service_idle_timeout",
                                       base::Minutes(1)};
  return kOnDeviceModelServiceIdleTimeout.Get();
}

base::TimeDelta GetOnDeviceModelExecutionValidationStartupDelay() {
  static const base::FeatureParam<base::TimeDelta>
      kOnDeviceModelExecutionValidationStartupDelay{
          &kOptimizationGuideOnDeviceModel,
          "on_device_model_execution_validation_startup_delay",
          base::Seconds(5)};
  return kOnDeviceModelExecutionValidationStartupDelay.Get();
}

int GetOnDeviceModelCrashCountBeforeDisable() {
  static const base::FeatureParam<int> kOnDeviceModelDisableCrashCount{
      &kOptimizationGuideOnDeviceModel, "on_device_model_disable_crash_count",
      3};
  return kOnDeviceModelDisableCrashCount.Get();
}

base::TimeDelta GetOnDeviceModelMaxCrashBackoffTime() {
  static const base::FeatureParam<base::TimeDelta>
      kOnDeviceModelMaxCrashBackoffTime{
          &kOptimizationGuideOnDeviceModel,
          "on_device_model_max_crash_backoff_time", base::Hours(1)};
  return kOnDeviceModelMaxCrashBackoffTime.Get();
}

base::TimeDelta GetOnDeviceModelCrashBackoffBaseTime() {
  static const base::FeatureParam<base::TimeDelta>
      kOnDeviceModelCrashBackoffBaseTime{
          &kOptimizationGuideOnDeviceModel,
          "on_device_model_crash_backoff_base_time", base::Minutes(1)};
  return kOnDeviceModelCrashBackoffBaseTime.Get();
}

base::TimeDelta GetOnDeviceStartupMetricDelay() {
  static const base::FeatureParam<base::TimeDelta> kOnDeviceStartupMetricDelay{
      &kLogOnDeviceMetricsOnStartup, "on_device_startup_metric_delay",
      base::Minutes(3)};
  return kOnDeviceStartupMetricDelay.Get();
}

bool CanLaunchOnDeviceModelService() {
  return base::FeatureList::IsEnabled(kOptimizationGuideOnDeviceModel) ||
         base::FeatureList::IsEnabled(kLogOnDeviceMetricsOnStartup);
}

bool IsOnDeviceExecutionEnabled() {
  return base::FeatureList::IsEnabled(
             features::kOptimizationGuideModelExecution) &&
         base::FeatureList::IsEnabled(kOptimizationGuideOnDeviceModel);
}

base::TimeDelta GetOnDeviceEligibleModelFeatureRecentUsePeriod() {
  return base::GetFieldTrialParamByFeatureAsTimeDelta(
      kOptimizationGuideOnDeviceModel,
      "on_device_model_feature_recent_use_period", base::Days(30));
}

base::TimeDelta GetOnDeviceModelRetentionTime() {
  return base::GetFieldTrialParamByFeatureAsTimeDelta(
      kOptimizationGuideOnDeviceModel, "on_device_model_retention_time",
      base::Days(30));
}

base::ByteCount GetDiskSpaceRequiredForOnDeviceModelInstall() {
  return base::MiB(base::GetFieldTrialParamByFeatureAsInt(
      kOptimizationGuideOnDeviceModel,
      "on_device_model_free_space_mb_required_to_install",
      base::GiB(20).InMiB()));
}

bool IsFreeDiskSpaceSufficientForOnDeviceModelInstall(
    base::ByteCount free_disk_space_bytes) {
  return GetDiskSpaceRequiredForOnDeviceModelInstall() <= free_disk_space_bytes;
}

bool IsFreeDiskSpaceTooLowForOnDeviceModelInstall(
    base::ByteCount free_disk_space_bytes) {
  return base::MiB(base::GetFieldTrialParamByFeatureAsInt(
             kOptimizationGuideOnDeviceModel,
             "on_device_model_free_space_mb_required_to_retain",
             base::GiB(5).InMiB())) >= free_disk_space_bytes;
}

bool GetOnDeviceModelRetractUnsafeContent() {
  static const base::FeatureParam<bool>
      kOnDeviceModelShouldRetractUnsafeContent{
          &kTextSafetyClassifier, "on_device_retract_unsafe_content", true};
  return kOnDeviceModelShouldRetractUnsafeContent.Get();
}

bool ShouldUseTextSafetyClassifierModel() {
  return base::FeatureList::IsEnabled(kTextSafetyClassifier);
}

bool ShouldUseGeneralizedSafetyModel() {
  static const base::FeatureParam<bool> kUseGeneralizedSafetyModel{
      &kTextSafetyClassifier, "use_generalized_safety_model", false};
  return kUseGeneralizedSafetyModel.Get();
}

double GetOnDeviceModelLanguageDetectionMinimumReliability() {
  static const base::FeatureParam<double>
      kOnDeviceModelLanguageDetectionMinimumReliability{
          &kTextSafetyClassifier,
          "on_device_language_detection_minimum_reliability", 0.8};
  return kOnDeviceModelLanguageDetectionMinimumReliability.Get();
}

int GetOnDeviceModelNumRepeats() {
  static const base::FeatureParam<int> kOnDeviceModelNumRepeats{
      &kOptimizationGuideOnDeviceModel, "on_device_model_num_repeats", 2};
  return kOnDeviceModelNumRepeats.Get();
}

int GetOnDeviceModelMinRepeatChars() {
  static const base::FeatureParam<int> kOnDeviceModelMinRepeatChars{
      &kOptimizationGuideOnDeviceModel, "on_device_model_min_repeat_chars", 16};
  return kOnDeviceModelMinRepeatChars.Get();
}

bool GetOnDeviceModelRetractRepeats() {
  static const base::FeatureParam<bool> kOnDeviceModelRetractRepeats{
      &kOptimizationGuideOnDeviceModel, "on_device_model_retract_repeats",
      true};
  return kOnDeviceModelRetractRepeats.Get();
}

int GetOnDeviceModelDefaultTopK() {
  static const base::FeatureParam<int> kTopK{
      &optimization_guide::features::kOptimizationGuideOnDeviceModel,
      "on_device_model_topk", 3};
  return kTopK.Get();
}

int GetOnDeviceModelMaxTopK() {
  static const base::FeatureParam<int> kMaxTopK{
      &optimization_guide::features::kOptimizationGuideOnDeviceModel,
      "on_device_model_max_topk", 128};
  return kMaxTopK.Get();
}

double GetOnDeviceModelDefaultTemperature() {
  static const base::FeatureParam<double> kTemperature{
      &kOptimizationGuideOnDeviceModel, "on_device_model_temperature", 0.8};
  return kTemperature.Get();
}

std::vector<uint32_t> GetOnDeviceModelAllowedAdaptationRanks() {
  static const base::FeatureParam<std::string>
      kOnDeviceModelAllowedAdaptationRanks{&kOptimizationGuideOnDeviceModel,
                                           "allowed_adaptation_ranks", "32"};
  std::vector<uint32_t> ranks;
  const auto ranks_str = kOnDeviceModelAllowedAdaptationRanks.Get();
  auto rank_strs = base::SplitStringPiece(
      ranks_str, ",", base::WhitespaceHandling::TRIM_WHITESPACE,
      base::SplitResult::SPLIT_WANT_NONEMPTY);
  ranks.reserve(rank_strs.size());
  for (const auto& rank_str : rank_strs) {
    uint32_t rank;
    if (base::StringToUint(rank_str, &rank)) {
      ranks.push_back(rank);
    }
  }
  return ranks;
}

bool ShouldEnableOptimizationGuideIconView() {
  return base::FeatureList::IsEnabled(kOptimizationGuideIconView);
}

std::optional<base::TimeDelta> GetSubframeGetAIPageContentTimeout() {
  if (!base::FeatureList::IsEnabled(kGetAIPageContentSubframeTimeoutEnabled)) {
    return std::nullopt;
  }
  return kGetAIPageContentSubframeTimeoutParam.Get();
}

std::optional<base::TimeDelta> GetMainFrameGetAIPageContentTimeout() {
  if (!base::FeatureList::IsEnabled(kGetAIPageContentMainFrameTimeoutEnabled)) {
    return std::nullopt;
  }
  return kGetAIPageContentMainFrameTimeoutParam.Get();
}

}  // namespace features
}  // namespace optimization_guide