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
  608
  609
  610
  611
  612
  613
  614
  615
  616
  617
  618
  619
  620
  621
  622
  623
  624
  625
  626
  627
  628
  629
  630
  631
  632
  633
  634
  635
  636
  637
  638
  639
  640
  641
  642
  643
  644
  645
  646
  647
  648
  649
  650
  651
  652
  653
  654
  655
  656
  657
  658
  659
  660
  661
  662
  663
  664
  665
  666
  667
  668
  669
  670
  671
  672
  673
  674
  675
  676
  677
  678
  679
  680
  681
  682
  683
  684
  685
  686
  687
  688
  689
  690
  691
  692
  693
  694
  695
  696
  697
  698
  699
  700
  701
  702
  703
  704
  705
  706
  707
  708
  709
  710
  711
  712
  713
  714
  715
  716
  717
  718
  719
  720
  721
  722
  723
  724
  725
  726
  727
  728
  729
  730
  731
  732
  733
  734
  735
  736
  737
  738
  739
  740
  741
  742
  743
  744
  745
  746
  747
  748
  749
  750
  751
  752
  753
  754
  755
  756
  757
  758
  759
  760
  761
  762
  763
  764
  765
  766
  767
  768
  769
  770
  771
  772
  773
  774
  775
  776
  777
  778
  779
  780
  781
  782
  783
  784
  785

content / child / runtime_features.cc [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/child/runtime_features.h"

#include <string>
#include <vector>

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/memory/raw_ref.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "cc/base/features.h"
#include "components/attribution_reporting/features.h"
#include "content/common/content_navigation_policy.h"
#include "content/common/content_switches_internal.h"
#include "content/common/features.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "device/base/features.h"
#include "device/fido/features.h"
#include "device/gamepad/public/cpp/gamepad_features.h"
#include "device/vr/buildflags/buildflags.h"
#include "gpu/config/gpu_finch_features.h"
#include "gpu/config/gpu_switches.h"
#include "media/base/media_switches.h"
#include "services/device/public/cpp/device_features.h"
#include "services/network/public/cpp/features.h"
#include "services/webnn/public/mojom/features.mojom-features.h"
#include "third_party/blink/public/common/buildflags.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/features_generated.h"
#include "third_party/blink/public/common/loader/referrer_utils.h"
#include "third_party/blink/public/common/switches.h"
#include "third_party/blink/public/platform/web_runtime_features.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/blink/blink_features.h"
#include "ui/gfx/switches.h"
#include "ui/gl/gl_switches.h"
#include "ui/native_theme/native_theme_features.h"
#include "ui/native_theme/native_theme_utils.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#endif

#if BUILDFLAG(ENABLE_VR)
#include "device/vr/public/cpp/features.h"
#endif

using blink::WebRuntimeFeatures;

namespace {

// Sets blink runtime features for specific platforms.
// This should be a last resort vs runtime_enabled_features.json5.
void SetRuntimeFeatureDefaultsForPlatform(
    const base::CommandLine& command_line) {
  // Please consider setting up feature defaults for different platforms
  // in runtime_enabled_features.json5 instead of here
  // TODO(rodneyding): Move the more common cases here
  // to baseFeature/switch functions below and move more complex
  // ones to special case functions.
#if defined(USE_AURA)
  WebRuntimeFeatures::EnableCompositedSelectionUpdate(true);
#endif

#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_CHROMEOS_LACROS)
  const bool enable_canvas_2d_image_chromium =
      command_line.HasSwitch(
          blink::switches::kEnableGpuMemoryBufferCompositorResources) &&
      !command_line.HasSwitch(switches::kDisable2dCanvasImageChromium) &&
      !command_line.HasSwitch(switches::kDisableGpu) &&
      base::FeatureList::IsEnabled(features::kCanvas2DImageChromium);
#else
  constexpr bool enable_canvas_2d_image_chromium = false;
#endif
  WebRuntimeFeatures::EnableCanvas2dImageChromium(
      enable_canvas_2d_image_chromium);

#if BUILDFLAG(IS_APPLE)
  const bool enable_web_gl_image_chromium =
      command_line.HasSwitch(
          blink::switches::kEnableGpuMemoryBufferCompositorResources) &&
      !command_line.HasSwitch(switches::kDisableWebGLImageChromium) &&
      !command_line.HasSwitch(switches::kDisableGpu);
#else
  const bool enable_web_gl_image_chromium =
      command_line.HasSwitch(switches::kEnableWebGLImageChromium);
#endif
  WebRuntimeFeatures::EnableWebGLImageChromium(enable_web_gl_image_chromium);

#if BUILDFLAG(IS_ANDROID)
  if (command_line.HasSwitch(switches::kDisableMediaSessionAPI)) {
    WebRuntimeFeatures::EnableMediaSession(false);
  }
#endif

#if BUILDFLAG(IS_ANDROID)
  if (base::android::BuildInfo::GetInstance()->sdk_int() >=
      base::android::SDK_VERSION_P) {
    // Display Cutout is limited to Android P+.
    WebRuntimeFeatures::EnableDisplayCutoutAPI(true);
  }
#endif

#if BUILDFLAG(IS_ANDROID)
  WebRuntimeFeatures::EnableMediaControlsExpandGesture(
      base::FeatureList::IsEnabled(media::kMediaControlsExpandGesture));
#endif
}

enum RuntimeFeatureEnableOptions {
  // - If the base::Feature default is overridden by field trial or command
  //   line, set Blink feature to the state of the base::Feature;
  // - Otherwise if the base::Feature is enabled, enable the Blink feature.
  // - Otherwise no change.
  kDefault,
  // Enables the Blink feature when the base::Feature is overridden by field
  // trial or command line. Otherwise no change. Its difference from kDefault is
  // that the Blink feature isn't affected by the default state of the
  // base::Feature. This is useful for Blink origin trial features especially
  // those implemented in both Chromium and Blink. As origin trial only controls
  // the Blink features, for now we require the base::Feature to be enabled by
  // default, but we don't want the default enabled status affect the Blink
  // feature. See also https://crbug.com/1048656#c10.
  // This can also be used for features that are enabled by default in Chromium
  // but not in Blink on all platforms and we want to use the Blink status.
  // However, we would prefer consistent Chromium and Blink status to this.
  kSetOnlyIfOverridden,
};

template <typename T>
// Helper class that describes the desired actions for the runtime feature
// depending on a check for chromium base::Feature.
struct RuntimeFeatureToChromiumFeatureMap {
  // This can be either an enabler function defined in web_runtime_features.cc
  // or the string name of the feature in runtime_enabled_features.json5.
  T feature_enabler;
  // The chromium base::Feature to check.
  const raw_ref<const base::Feature> chromium_feature;
  const RuntimeFeatureEnableOptions option = kDefault;
};

template <typename Enabler>
void SetRuntimeFeatureFromChromiumFeature(const base::Feature& chromium_feature,
                                          RuntimeFeatureEnableOptions option,
                                          const Enabler& enabler) {
  using FeatureList = base::FeatureList;
  const bool feature_enabled = FeatureList::IsEnabled(chromium_feature);
  const bool is_overridden =
      FeatureList::GetStateIfOverridden(chromium_feature).has_value();
  switch (option) {
    case kSetOnlyIfOverridden:
      if (is_overridden) {
        enabler(feature_enabled);
      }
      break;
    case kDefault:
      if (feature_enabled || is_overridden) {
        enabler(feature_enabled);
      }
      break;
    default:
      NOTREACHED_IN_MIGRATION();
  }
}

// Sets blink runtime features that are either directly
// controlled by Chromium base::Feature or are overridden
// by base::Feature states.
void SetRuntimeFeaturesFromChromiumFeatures() {
  using wf = WebRuntimeFeatures;
  // To add a runtime feature control, add a new
  // RuntimeFeatureToChromiumFeatureMap entry here if there is a custom
  // enabler function defined. Otherwise add the entry with string name
  // in the next list.
  const RuntimeFeatureToChromiumFeatureMap<void (*)(bool)>
      blinkFeatureToBaseFeatureMapping[] = {
          {wf::EnableAccessibilityAriaVirtualContent,
           raw_ref(features::kEnableAccessibilityAriaVirtualContent)},
#if BUILDFLAG(IS_ANDROID)
          {wf::EnableAccessibilityPageZoom,
           raw_ref(features::kAccessibilityPageZoom)},
#endif
          {wf::EnableAccessibilityUseAXPositionForDocumentMarkers,
           raw_ref(features::kUseAXPositionForDocumentMarkers)},
          {wf::EnableAOMAriaRelationshipProperties,
           raw_ref(features::kEnableAriaElementReflection)},
          {wf::EnableBackgroundFetch, raw_ref(features::kBackgroundFetch)},
          {wf::EnableBoundaryEventDispatchTracksNodeRemoval,
           raw_ref(blink::features::kBoundaryEventDispatchTracksNodeRemoval)},
          {wf::EnableBrowserVerifiedUserActivationKeyboard,
           raw_ref(features::kBrowserVerifiedUserActivationKeyboard)},
          {wf::EnableBrowserVerifiedUserActivationMouse,
           raw_ref(features::kBrowserVerifiedUserActivationMouse)},
          {wf::EnableCompositeBGColorAnimation,
           raw_ref(features::kCompositeBGColorAnimation)},
          {wf::EnableCooperativeScheduling,
           raw_ref(features::kCooperativeScheduling)},
          {wf::EnableDigitalGoods, raw_ref(features::kDigitalGoodsApi),
           kSetOnlyIfOverridden},
          {wf::EnableDocumentPolicyNegotiation,
           raw_ref(features::kDocumentPolicyNegotiation)},
          {wf::EnableEyeDropperAPI, raw_ref(features::kEyeDropper),
           kSetOnlyIfOverridden},
          {wf::EnableFedCm, raw_ref(features::kFedCm), kSetOnlyIfOverridden},
          {wf::EnableFedCmButtonMode, raw_ref(features::kFedCmButtonMode),
           kSetOnlyIfOverridden},
          {wf::EnableFedCmAuthz, raw_ref(features::kFedCmAuthz), kDefault},
          {wf::EnableFedCmIdPRegistration,
           raw_ref(features::kFedCmIdPRegistration), kDefault},
          {wf::EnableFedCmIdpSigninStatus,
           raw_ref(features::kFedCmIdpSigninStatusEnabled),
           kSetOnlyIfOverridden},
          {wf::EnableGamepadMultitouch,
           raw_ref(features::kEnableGamepadMultitouch)},
          {wf::EnableSharedStorageAPI,
           raw_ref(features::kPrivacySandboxAdsAPIsOverride),
           kSetOnlyIfOverridden},
          {wf::EnableSharedStorageAPI,
           raw_ref(features::kPrivacySandboxAdsAPIsM1Override)},
          {wf::EnableSharedStorageAPIM118,
           raw_ref(blink::features::kSharedStorageAPIM118), kDefault},
          {wf::EnableSharedStorageAPIM125,
           raw_ref(blink::features::kSharedStorageAPIM125), kDefault},
          {wf::EnableFedCmMultipleIdentityProviders,
           raw_ref(features::kFedCmMultipleIdentityProviders),
           kSetOnlyIfOverridden},
          {wf::EnableFedCmSelectiveDisclosure,
           raw_ref(features::kFedCmSelectiveDisclosure), kDefault},
          {wf::EnableFencedFrames,
           raw_ref(features::kPrivacySandboxAdsAPIsOverride),
           kSetOnlyIfOverridden},
          {wf::EnableFencedFrames,
           raw_ref(features::kPrivacySandboxAdsAPIsM1Override)},
          {wf::EnableForcedColors, raw_ref(features::kForcedColors)},
          {wf::EnableFractionalScrollOffsets,
           raw_ref(features::kFractionalScrollOffsets)},
          {wf::EnableSensorExtraClasses,
           raw_ref(features::kGenericSensorExtraClasses)},
#if BUILDFLAG(IS_ANDROID)
          {wf::EnableGetDisplayMedia,
           raw_ref(features::kUserMediaScreenCapturing)},
#endif
          {wf::EnableInstalledApp, raw_ref(features::kInstalledApp)},
          {wf::EnableLazyInitializeMediaControls,
           raw_ref(features::kLazyInitializeMediaControls)},
#if BUILDFLAG(IS_CHROMEOS)
          {wf::EnableLockedMode, raw_ref(blink::features::kLockedMode)},
#endif
          {wf::EnableMediaCastOverlayButton,
           raw_ref(media::kMediaCastOverlayButton)},
          {wf::EnableMediaEngagementBypassAutoplayPolicies,
           raw_ref(media::kMediaEngagementBypassAutoplayPolicies)},
          {wf::EnableNotificationContentImage,
           raw_ref(features::kNotificationContentImage), kSetOnlyIfOverridden},
          {wf::EnablePaymentApp, raw_ref(features::kServiceWorkerPaymentApps)},
          {wf::EnablePaymentRequest, raw_ref(features::kWebPayments)},
          {wf::EnablePercentBasedScrolling,
           raw_ref(features::kWindowsScrollingPersonality)},
          {wf::EnablePeriodicBackgroundSync,
           raw_ref(features::kPeriodicBackgroundSync)},
          {wf::EnablePushMessagingSubscriptionChange,
           raw_ref(features::kPushSubscriptionChangeEvent)},
          {wf::EnableRestrictGamepadAccess,
           raw_ref(features::kRestrictGamepadAccess)},
          {wf::EnableSecurePaymentConfirmation,
           raw_ref(features::kSecurePaymentConfirmation)},
          {wf::EnableSecurePaymentConfirmationDebug,
           raw_ref(features::kSecurePaymentConfirmationDebug)},
          {wf::EnableSendBeaconThrowForBlobWithNonSimpleType,
           raw_ref(features::kSendBeaconThrowForBlobWithNonSimpleType)},
          {wf::EnableSharedArrayBuffer, raw_ref(features::kSharedArrayBuffer)},
          {wf::EnableSharedArrayBufferOnDesktop,
           raw_ref(features::kSharedArrayBufferOnDesktop)},
#if BUILDFLAG(IS_ANDROID)
          {wf::EnableSmartZoom, raw_ref(features::kSmartZoom)},
#endif
          {wf::EnableTouchDragAndContextMenu,
           raw_ref(features::kTouchDragAndContextMenu)},
          {wf::EnableUserActivationSameOriginVisibility,
           raw_ref(features::kUserActivationSameOriginVisibility)},
          {wf::EnableWebAuthenticationAmbient,
           raw_ref(device::kWebAuthnAmbientSignin)},
          {wf::EnableWebBluetooth, raw_ref(features::kWebBluetooth),
           kSetOnlyIfOverridden},
          {wf::EnableWebBluetoothGetDevices,
           raw_ref(features::kWebBluetoothNewPermissionsBackend),
           kSetOnlyIfOverridden},
          {wf::EnableWebBluetoothWatchAdvertisements,
           raw_ref(features::kWebBluetoothNewPermissionsBackend),
           kSetOnlyIfOverridden},
#if BUILDFLAG(IS_ANDROID)
          {wf::EnableWebNFC, raw_ref(features::kWebNfc), kSetOnlyIfOverridden},
#endif
          {wf::EnableWebIdentityDigitalCredentials,
           raw_ref(features::kWebIdentityDigitalCredentials),
           kSetOnlyIfOverridden},
          {wf::EnableWebOTP, raw_ref(features::kWebOTP), kSetOnlyIfOverridden},
          {wf::EnableWebOTPAssertionFeaturePolicy,
           raw_ref(features::kWebOTPAssertionFeaturePolicy),
           kSetOnlyIfOverridden},
          {wf::EnableWebUSB, raw_ref(features::kWebUsb)},
          {wf::EnableWebXR, raw_ref(features::kWebXr)},
#if BUILDFLAG(ENABLE_VR)
          {wf::EnableWebXRFrontFacing,
           raw_ref(device::features::kWebXrIncubations)},
          {wf::EnableWebXRFrameRate,
           raw_ref(device::features::kWebXrIncubations)},
          {wf::EnableWebXRGPUBinding,
           raw_ref(device::features::kWebXrIncubations)},
          {wf::EnableWebXRHandInput,
           raw_ref(device::features::kWebXrHandInput)},
          {wf::EnableWebXRImageTracking,
           raw_ref(device::features::kWebXrIncubations)},
          {wf::EnableWebXRLayers, raw_ref(device::features::kWebXrLayers)},
          {wf::EnableWebXRPlaneDetection,
           raw_ref(device::features::kWebXrIncubations)},
          {wf::EnableWebXRPoseMotionData,
           raw_ref(device::features::kWebXrIncubations)},
          {wf::EnableWebXRSpecParity,
           raw_ref(device::features::kWebXrIncubations)},
#endif
          {wf::EnableServiceWorkerStaticRouter,
           raw_ref(features::kServiceWorkerStaticRouter)},
          {wf::EnablePermissions, raw_ref(features::kWebPermissionsApi),
           kSetOnlyIfOverridden},
      };
  for (const auto& mapping : blinkFeatureToBaseFeatureMapping) {
    SetRuntimeFeatureFromChromiumFeature(
        *mapping.chromium_feature, mapping.option, mapping.feature_enabler);
  }

  // TODO(crbug.com/40571563): Cleanup the inconsistency between custom WRF
  // enabler function and using feature string name with
  // EnableFeatureFromString.
  const RuntimeFeatureToChromiumFeatureMap<const char*>
      runtimeFeatureNameToChromiumFeatureMapping[] = {
          {"AllowContentInitiatedDataUrlNavigations",
           raw_ref(features::kAllowContentInitiatedDataUrlNavigations)},
          {"AllowURNsInIframes", raw_ref(blink::features::kAllowURNsInIframes)},
          {"AllowURNsInIframes",
           raw_ref(features::kPrivacySandboxAdsAPIsOverride),
           kSetOnlyIfOverridden},
          {"AllowURNsInIframes",
           raw_ref(features::kPrivacySandboxAdsAPIsM1Override)},
          {"AttributionReporting",
           raw_ref(features::kPrivacySandboxAdsAPIsOverride),
           kSetOnlyIfOverridden},
          {"AttributionReporting",
           raw_ref(features::kPrivacySandboxAdsAPIsM1Override)},
          {"AttributionReportingCrossAppWeb",
           raw_ref(features::kPrivacySandboxAdsAPIsOverride),
           kSetOnlyIfOverridden},
          {"AndroidDownloadableFontsMatching",
           raw_ref(features::kAndroidDownloadableFontsMatching)},
#if BUILDFLAG(IS_ANDROID)
          {"CCTNewRFMPushBehavior",
           raw_ref(blink::features::kCCTNewRFMPushBehavior)},
#endif
          {"CompressionDictionaryTransport",
           raw_ref(network::features::kCompressionDictionaryTransport)},
          {"CompressionDictionaryTransportBackend",
           raw_ref(network::features::kCompressionDictionaryTransportBackend)},
          {"CookieDeprecationFacilitatedTesting",
           raw_ref(features::kCookieDeprecationFacilitatedTesting)},
          {"Database", raw_ref(blink::features::kWebSQLAccess),
           kSetOnlyIfOverridden},
          {"DocumentPolicyIncludeJSCallStacksInCrashReports",
           raw_ref(blink::features::
                       kDocumentPolicyIncludeJSCallStacksInCrashReports),
           kSetOnlyIfOverridden},
          {"FencedFramesLocalUnpartitionedDataAccess",
           raw_ref(blink::features::kFencedFramesLocalUnpartitionedDataAccess)},
          {"Fledge", raw_ref(blink::features::kFledge)},
          {"Fledge", raw_ref(features::kPrivacySandboxAdsAPIsOverride),
           kSetOnlyIfOverridden},
          {"Fledge", raw_ref(features::kPrivacySandboxAdsAPIsM1Override),
           kSetOnlyIfOverridden},
          {"FledgeBiddingAndAuctionServerAPI",
           raw_ref(blink::features::kFledgeBiddingAndAuctionServer), kDefault},
          {"FontationsFontBackend",
           raw_ref(blink::features::kFontationsFontBackend)},
          {"FontSrcLocalMatching", raw_ref(features::kFontSrcLocalMatching)},
          {"LegacyWindowsDWriteFontFallback",
           raw_ref(features::kLegacyWindowsDWriteFontFallback)},
          {"MachineLearningNeuralNetwork",
           raw_ref(webnn::mojom::features::kWebMachineLearningNeuralNetwork),
           kSetOnlyIfOverridden},
          {"OriginIsolationHeader", raw_ref(features::kOriginIsolationHeader)},
          {"ReduceAcceptLanguage",
           raw_ref(network::features::kReduceAcceptLanguage)},
          {"SerialPortConnected", raw_ref(features::kSerialPortConnected)},
          {"TopicsAPI", raw_ref(features::kPrivacySandboxAdsAPIsOverride),
           kSetOnlyIfOverridden},
          {"TopicsAPI", raw_ref(features::kPrivacySandboxAdsAPIsM1Override)},
          {"TopicsDocumentAPI",
           raw_ref(features::kPrivacySandboxAdsAPIsOverride),
           kSetOnlyIfOverridden},
          {"TopicsDocumentAPI",
           raw_ref(features::kPrivacySandboxAdsAPIsM1Override)},
          {"TouchTextEditingRedesign",
           raw_ref(features::kTouchTextEditingRedesign)},
          {"TrustedTypesFromLiteral",
           raw_ref(features::kTrustedTypesFromLiteral)},
          {"WebSerialBluetooth",
           raw_ref(features::kEnableBluetoothSerialPortProfileInSerialApi)},
          {"MediaStreamTrackTransfer",
           raw_ref(features::kMediaStreamTrackTransfer)},
          {"PrivateNetworkAccessPermissionPrompt",
           raw_ref(network::features::kPrivateNetworkAccessPermissionPrompt),
           kSetOnlyIfOverridden},
          {"ExperimentalMachineLearningNeuralNetwork",
           raw_ref(webnn::mojom::features::
                       kExperimentalWebMachineLearningNeuralNetwork),
           kSetOnlyIfOverridden}};
  for (const auto& mapping : runtimeFeatureNameToChromiumFeatureMapping) {
    SetRuntimeFeatureFromChromiumFeature(
        *mapping.chromium_feature, mapping.option, [&mapping](bool enabled) {
          wf::EnableFeatureFromString(mapping.feature_enabler, enabled);
        });
  }

  WebRuntimeFeatures::UpdateStatusFromBaseFeatures();
}

// Helper class that describes the desired enable/disable action
// for a runtime feature when a command line switch exists.
struct SwitchToFeatureMap {
  // The enabler function defined in web_runtime_features.cc.
  void (*feature_enabler)(bool);
  // The switch to check for on command line.
  const char* switch_name;
  // This is the desired state for the runtime feature if the
  // switch exists on command line.
  bool target_enabled_state;
};

// Sets blink runtime features controlled by command line switches.
void SetRuntimeFeaturesFromCommandLine(const base::CommandLine& command_line) {
  // To add a new switch-controlled runtime feature, add a new
  // SwitchToFeatureMap entry to the initializer list below.
  // Note: command line switches are now discouraged, please consider
  // using base::Feature instead.
  // https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/configuration.md#switches
  using wrf = WebRuntimeFeatures;
  const SwitchToFeatureMap switchToFeatureMapping[] = {
      // Stable Features
      {wrf::EnablePresentation, switches::kDisablePresentationAPI, false},
      {wrf::EnableRemotePlayback, switches::kDisableRemotePlaybackAPI, false},
      {wrf::EnableTimerThrottlingForBackgroundTabs,
       switches::kDisableBackgroundTimerThrottling, false},
      // End of Stable Features
      {wrf::EnableAutomationControlled, switches::kEnableAutomation, true},
      {wrf::EnableAutomationControlled, switches::kHeadless, true},
      {wrf::EnableAutomationControlled, switches::kRemoteDebuggingPipe, true},
      {wrf::EnableDatabase, switches::kDisableDatabases, false},
      {wrf::EnableFileSystem, switches::kDisableFileSystem, false},
      {wrf::EnableNetInfoDownlinkMax,
       switches::kEnableNetworkInformationDownlinkMax, true},
      {wrf::EnableNotifications, switches::kDisableNotifications, false},
      {wrf::EnablePreciseMemoryInfo, switches::kEnablePreciseMemoryInfo, true},
      // Chrome's Push Messaging implementation relies on Web Notifications.
      {wrf::EnablePushMessaging, switches::kDisableNotifications, false},
      {wrf::EnableScriptedSpeechRecognition, switches::kDisableSpeechAPI,
       false},
      {wrf::EnableScriptedSpeechSynthesis, switches::kDisableSpeechAPI, false},
      {wrf::EnableScriptedSpeechSynthesis, switches::kDisableSpeechSynthesisAPI,
       false},
      {wrf::EnableSharedWorker, switches::kDisableSharedWorkers, false},
      {wrf::EnableMutationEvents, blink::switches::kMutationEventsEnabled,
       true},
      {wrf::EnableKeyboardFocusableScrollers,
       blink::switches::kKeyboardFocusableScrollersEnabled, true},
      {wrf::EnableKeyboardFocusableScrollers,
       blink::switches::kKeyboardFocusableScrollersOptOut, false},
      {wrf::EnableStandardizedBrowserZoom,
       blink::switches::kDisableStandardizedBrowserZoom, false},
      {wrf::EnableCSSCustomStateDeprecatedSyntax,
       blink::switches::kCSSCustomStateDeprecatedSyntaxEnabled, true},
      {wrf::EnableSelectParserRelaxation,
       blink::switches::kDisableSelectParserRelaxation, false},
      {wrf::EnableTextFragmentIdentifiers,
       switches::kDisableScrollToTextFragment, false},
      {wrf::EnableWebAuthenticationRemoteDesktopSupport,
       switches::kWebAuthRemoteDesktopSupport, true},
      {wrf::EnableWebGLDeveloperExtensions,
       switches::kEnableWebGLDeveloperExtensions, true},
      {wrf::EnableWebGLDraftExtensions, switches::kEnableWebGLDraftExtensions,
       true},
      {wrf::EnableWebGPUDeveloperFeatures,
       switches::kEnableWebGPUDeveloperFeatures, true},
      {wrf::EnableWebGPUExperimentalFeatures, switches::kEnableUnsafeWebGPU,
       true},
  };

  for (const auto& mapping : switchToFeatureMapping) {
    if (command_line.HasSwitch(mapping.switch_name)) {
      mapping.feature_enabler(mapping.target_enabled_state);
    }
  }

  // Set EnableAutomationControlled if the caller passes
  // --remote-debugging-port=0 on the command line. This means
  // the caller has requested an ephemeral port which is how ChromeDriver
  // launches the browser by default.
  // If the caller provides a specific port number, this is
  // more likely for attaching a debugger, so we should leave
  // EnableAutomationControlled unset to ensure the browser behaves as it does
  // when not under automation control.
  if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
    std::string port_str =
        command_line.GetSwitchValueASCII(::switches::kRemoteDebuggingPort);
    int port;
    if (base::StringToInt(port_str, &port) && port == 0) {
      WebRuntimeFeatures::EnableAutomationControlled(true);
    }
  }
}

// Sets blink runtime features that depend on a combination
// of args rather than a single check of base::Feature or switch.
// This can be a combination of both or custom checking logic
// not covered by other functions. In short, this should be used
// as a last resort.
void SetCustomizedRuntimeFeaturesFromCombinedArgs(
    const base::CommandLine& command_line) {
  // CAUTION: Only add custom enabling logic here if it cannot
  // be covered by the other functions.

  // These checks are custom wrappers around base::FeatureList::IsEnabled
  // They're moved here to distinguish them from actual base checks
  WebRuntimeFeatures::EnableOverlayScrollbars(ui::IsOverlayScrollbarEnabled());
  WebRuntimeFeatures::EnableFluentScrollbars(ui::IsFluentScrollbarEnabled());
  WebRuntimeFeatures::EnableFluentOverlayScrollbars(
      ui::IsFluentOverlayScrollbarEnabled());

  // TODO(rodneyding): This is a rare case for a stable feature
  // Need to investigate more to determine whether to refactor it.
  if (command_line.HasSwitch(switches::kDisableV8IdleTasks)) {
    WebRuntimeFeatures::EnableV8IdleTasks(false);
  } else {
    WebRuntimeFeatures::EnableV8IdleTasks(true);
  }

  WebRuntimeFeatures::EnableBackForwardCache(
      content::IsBackForwardCacheEnabled());

  if (base::FeatureList::IsEnabled(network::features::kPrivateStateTokens)) {
    WebRuntimeFeatures::EnablePrivateStateTokens(true);
    WebRuntimeFeatures::EnablePrivateStateTokensAlwaysAllowIssuance(true);
  } else if (base::FeatureList::IsEnabled(network::features::kFledgePst)) {
    // See https://bit.ly/configuring-trust-tokens.
    using network::features::TrustTokenOriginTrialSpec;
    switch (
        network::features::kTrustTokenOperationsRequiringOriginTrial.Get()) {
      case TrustTokenOriginTrialSpec::kOriginTrialNotRequired:
        // Setting PrivateStateTokens=true enables the Trust Tokens interface;
        // PrivateStateTokensAlwaysAllowIssuance disables a runtime check
        // during issuance that the origin trial is active (see
        // blink/.../trust_token_issuance_authorization.h).
        WebRuntimeFeatures::EnablePrivateStateTokens(true);
        WebRuntimeFeatures::EnablePrivateStateTokensAlwaysAllowIssuance(true);
        break;
      case TrustTokenOriginTrialSpec::kAllOperationsRequireOriginTrial:
        // The origin trial itself will be responsible for enabling the
        // PrivateStateTokens RuntimeEnabledFeature.
        WebRuntimeFeatures::EnablePrivateStateTokens(false);
        WebRuntimeFeatures::EnablePrivateStateTokensAlwaysAllowIssuance(false);
        break;
      case TrustTokenOriginTrialSpec::kOnlyIssuanceRequiresOriginTrial:
        // At issuance, a runtime check will be responsible for checking that
        // the origin trial is present.
        WebRuntimeFeatures::EnablePrivateStateTokens(true);
        WebRuntimeFeatures::EnablePrivateStateTokensAlwaysAllowIssuance(false);
        break;
    }
  }
}

// Ensures that the various ways of enabling/disabling features do not produce
// an invalid configuration.
void ResolveInvalidConfigurations() {
  // Fenced frames cannot be enabled without the support of the
  // browser process.
  if ((base::FeatureList::IsEnabled(features::kPrivacySandboxAdsAPIsOverride) ||
       base::FeatureList::IsEnabled(
           features::kPrivacySandboxAdsAPIsM1Override)) &&
      !base::FeatureList::IsEnabled(blink::features::kFencedFrames)) {
    LOG_IF(WARNING, WebRuntimeFeatures::IsFencedFramesEnabled())
        << "Fenced frames cannot be enabled in this configuration. Use --"
        << switches::kEnableFeatures << "="
        << blink::features::kFencedFrames.name << " instead.";
    WebRuntimeFeatures::EnableFencedFrames(false);
  }

  if (!base::FeatureList::IsEnabled(blink::features::kFencedFrames) &&
      base::FeatureList::IsEnabled(
          blink::features::kFencedFramesLocalUnpartitionedDataAccess)) {
    LOG_IF(
        WARNING,
        WebRuntimeFeatures::IsFencedFramesLocalUnpartitionedDataAccessEnabled())
        << "Fenced frames must be enabled in order to enable local "
           "unpartitioned "
        << "data access. Use --" << switches::kEnableFeatures << "="
        << blink::features::kFencedFrames.name << " in addition.";
    WebRuntimeFeatures::EnableFeatureFromString(
        "FencedFramesLocalUnpartitionedDataAccess", false);
  }

  // Topics API cannot be enabled without the support of the browser process.
  // The Document API should be additionally gated by the
  // `kBrowsingTopicsDocumentAPI` feature.
  if (!base::FeatureList::IsEnabled(blink::features::kBrowsingTopics)) {
    LOG_IF(WARNING, WebRuntimeFeatures::IsTopicsAPIEnabled())
        << "Topics cannot be enabled in this configuration. Use --"
        << switches::kEnableFeatures << "="
        << blink::features::kBrowsingTopics.name << " in addition.";
    WebRuntimeFeatures::EnableTopicsAPI(false);
    WebRuntimeFeatures::EnableTopicsDocumentAPI(false);
  } else {
    if (!base::FeatureList::IsEnabled(
            blink::features::kBrowsingTopicsDocumentAPI)) {
      LOG_IF(WARNING, WebRuntimeFeatures::IsTopicsDocumentAPIEnabled())
          << "Topics Document API cannot be enabled in this configuration. Use "
             "--"
          << switches::kEnableFeatures << "="
          << blink::features::kBrowsingTopicsDocumentAPI.name
          << " in addition.";
      WebRuntimeFeatures::EnableTopicsDocumentAPI(false);
    }
  }

  if (!base::FeatureList::IsEnabled(blink::features::kSharedStorageAPI)) {
    LOG_IF(WARNING, WebRuntimeFeatures::IsSharedStorageAPIEnabled())
        << "SharedStorage cannot be enabled in this "
           "configuration. Use --"
        << switches::kEnableFeatures << "="
        << blink::features::kSharedStorageAPI.name << " in addition.";
    WebRuntimeFeatures::EnableSharedStorageAPI(false);
  }

  if (!base::FeatureList::IsEnabled(blink::features::kSharedStorageAPIM118) ||
      !base::FeatureList::IsEnabled(blink::features::kSharedStorageAPI)) {
    LOG_IF(WARNING, WebRuntimeFeatures::IsSharedStorageAPIM118Enabled())
        << "SharedStorage for M118+ cannot be enabled in this "
           "configuration. Use --"
        << switches::kEnableFeatures << "="
        << blink::features::kSharedStorageAPI.name << ","
        << blink::features::kSharedStorageAPIM118.name << " in addition.";
    WebRuntimeFeatures::EnableSharedStorageAPIM118(false);
  }

  if (!base::FeatureList::IsEnabled(blink::features::kSharedStorageAPIM125) ||
      !base::FeatureList::IsEnabled(blink::features::kSharedStorageAPI)) {
    LOG_IF(WARNING, WebRuntimeFeatures::IsSharedStorageAPIM125Enabled())
        << "SharedStorage for M125+ cannot be enabled in this "
           "configuration. Use --"
        << switches::kEnableFeatures << "="
        << blink::features::kSharedStorageAPI.name << ","
        << blink::features::kSharedStorageAPIM125.name << " in addition.";
    WebRuntimeFeatures::EnableSharedStorageAPIM125(false);
  }

  if (!base::FeatureList::IsEnabled(
          attribution_reporting::features::kConversionMeasurement)) {
    LOG_IF(WARNING, WebRuntimeFeatures::IsAttributionReportingEnabled())
        << "AttributionReporting cannot be enabled in this "
           "configuration. Use --"
        << switches::kEnableFeatures << "="
        << attribution_reporting::features::kConversionMeasurement.name
        << " in addition.";
    WebRuntimeFeatures::EnableAttributionReporting(false);
  }

  if (!base::FeatureList::IsEnabled(
          attribution_reporting::features::kConversionMeasurement) ||
      !base::FeatureList::IsEnabled(
          network::features::kAttributionReportingCrossAppWeb)) {
    LOG_IF(WARNING,
           WebRuntimeFeatures::IsAttributionReportingCrossAppWebEnabled())
        << "AttributionReportingCrossAppWeb cannot be enabled in this "
           "configuration. Use --"
        << switches::kEnableFeatures << "="
        << attribution_reporting::features::kConversionMeasurement.name << ","
        << network::features::kAttributionReportingCrossAppWeb.name
        << " in addition.";
    WebRuntimeFeatures::EnableAttributionReportingCrossAppWeb(false);
  }

  if (!base::FeatureList::IsEnabled(blink::features::kInterestGroupStorage)) {
    LOG_IF(WARNING, WebRuntimeFeatures::IsAdInterestGroupAPIEnabled())
        << "AdInterestGroupAPI cannot be enabled in this "
           "configuration. Use --"
        << switches::kEnableFeatures << "="
        << blink::features::kInterestGroupStorage.name << " in addition.";
    WebRuntimeFeatures::EnableAdInterestGroupAPI(false);
    WebRuntimeFeatures::EnableFledge(false);
  }

  if (base::FeatureList::IsEnabled(
          features::kCookieDeprecationFacilitatedTesting)) {
    WebRuntimeFeatures::EnableFledgeMultiBid(false);
    WebRuntimeFeatures::EnableFledgeRealTimeReporting(false);

    if (!base::FeatureList::IsEnabled(
            blink::features::
                kAlwaysAllowFledgeDeprecatedRenderURLReplacements)) {
      WebRuntimeFeatures::EnableFledgeDeprecatedRenderURLReplacements(false);
    }
  }

  // PermissionElement cannot be enabled without the support of the
  // browser process.
  if (!base::FeatureList::IsEnabled(blink::features::kPermissionElement)) {
    LOG_IF(WARNING, WebRuntimeFeatures::IsPermissionElementEnabled())
        << "PermissionElement cannot be enabled in this configuration. Use --"
        << switches::kEnableFeatures << "="
        << blink::features::kPermissionElement.name << " instead.";
    WebRuntimeFeatures::EnablePermissionElement(false);
  }
}

}  // namespace

namespace content {

void SetRuntimeFeaturesDefaultsAndUpdateFromArgs(
    const base::CommandLine& command_line) {
  // Sets experimental features.
  bool enable_experimental_web_platform_features =
      command_line.HasSwitch(switches::kEnableExperimentalWebPlatformFeatures);
  bool enable_blink_test_features =
      command_line.HasSwitch(switches::kEnableBlinkTestFeatures);

  if (enable_blink_test_features) {
    enable_experimental_web_platform_features = true;
    WebRuntimeFeatures::EnableTestOnlyFeatures(true);
  }

  if (enable_experimental_web_platform_features) {
    WebRuntimeFeatures::EnableExperimentalFeatures(true);
  }

  SetRuntimeFeatureDefaultsForPlatform(command_line);

  // Sets origin trial features.
  if (command_line.HasSwitch(
          switches::kDisableOriginTrialControlledBlinkFeatures)) {
    WebRuntimeFeatures::EnableOriginTrialControlledFeatures(false);
  }

  // TODO(rodneyding): add doc explaining ways to add new runtime features
  // controls in the following functions.

  SetRuntimeFeaturesFromChromiumFeatures();

  SetRuntimeFeaturesFromCommandLine(command_line);

  SetCustomizedRuntimeFeaturesFromCombinedArgs(command_line);

  // Enable explicitly enabled features, and then disable explicitly disabled
  // ones.
  for (const std::string& feature :
       FeaturesFromSwitch(command_line, switches::kEnableBlinkFeatures)) {
    WebRuntimeFeatures::EnableFeatureFromString(feature, true);
  }
  for (const std::string& feature :
       FeaturesFromSwitch(command_line, switches::kDisableBlinkFeatures)) {
    WebRuntimeFeatures::EnableFeatureFromString(feature, false);
  }

  ResolveInvalidConfigurations();
}

}  // namespace content