From: uazo Date: Fri, 21 Apr 2023 13:10:20 +0000 Subject: Add a flag to disable GamePad API Adds restrict-gamepad-access flag (default active) to disable GamePad API. License: GPL-2.0-or-later - https://spdx.org/licenses/GPL-2.0-or-later.html --- chrome/browser/flag-metadata.json | 4 ++-- content/child/runtime_features.cc | 2 ++ .../about_flags_cc/Add-a-flag-to-disable-GamePad-API.inc | 7 +++++++ device/gamepad/public/cpp/gamepad_features.cc | 8 ++++++++ device/gamepad/public/cpp/gamepad_features.h | 2 ++ .../blink/renderer/modules/gamepad/navigator_gamepad.cc | 7 +++++++ 6 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 cromite_flags/chrome/browser/about_flags_cc/Add-a-flag-to-disable-GamePad-API.inc diff --git a/chrome/browser/flag-metadata.json b/chrome/browser/flag-metadata.json --- a/chrome/browser/flag-metadata.json +++ b/chrome/browser/flag-metadata.json @@ -7726,9 +7726,9 @@ "expiry_milestone" : 130 }, { - "name": "restrict-gamepad-access", + "name": "restrict-gamepad-access", // restrict-gamepad-access" "owners": [ "//device/gamepad/OWNERS", "jameshollyer@chromium.org" ], - "expiry_milestone": 96 + "expiry_milestone": -1 }, { "name": "retain-omnibox-on-focus", diff --git a/content/child/runtime_features.cc b/content/child/runtime_features.cc --- a/content/child/runtime_features.cc +++ b/content/child/runtime_features.cc @@ -271,6 +271,8 @@ void SetRuntimeFeaturesFromChromiumFeatures() { raw_ref(features::kPeriodicBackgroundSync)}, {wf::EnablePushMessagingSubscriptionChange, raw_ref(features::kPushSubscriptionChangeEvent)}, + {wf::EnableRestrictGamepadAccess, + raw_ref(features::kRestrictGamepadAccess)}, {wf::EnableSecurePaymentConfirmation, raw_ref(features::kSecurePaymentConfirmation)}, {wf::EnableSecurePaymentConfirmationDebug, diff --git a/cromite_flags/chrome/browser/about_flags_cc/Add-a-flag-to-disable-GamePad-API.inc b/cromite_flags/chrome/browser/about_flags_cc/Add-a-flag-to-disable-GamePad-API.inc new file mode 100644 --- /dev/null +++ b/cromite_flags/chrome/browser/about_flags_cc/Add-a-flag-to-disable-GamePad-API.inc @@ -0,0 +1,7 @@ +#ifdef FLAG_SECTION + + {"restrict-gamepad-access", "Restrict gamepad access", + "Disable the Gamepad API", kOsAll, + FEATURE_VALUE_TYPE(features::kRestrictGamepadAccess)}, + +#endif diff --git a/device/gamepad/public/cpp/gamepad_features.cc b/device/gamepad/public/cpp/gamepad_features.cc --- a/device/gamepad/public/cpp/gamepad_features.cc +++ b/device/gamepad/public/cpp/gamepad_features.cc @@ -27,4 +27,12 @@ bool IsGamepadMultitouchEnabled() { return false; } +CROMITE_FEATURE(kRestrictGamepadAccess, + "RestrictGamepadAccess", + base::FEATURE_ENABLED_BY_DEFAULT); + +bool IsRestrictGamepadAccessEnabled() { + return base::FeatureList::IsEnabled(kRestrictGamepadAccess); +} + } // namespace features diff --git a/device/gamepad/public/cpp/gamepad_features.h b/device/gamepad/public/cpp/gamepad_features.h --- a/device/gamepad/public/cpp/gamepad_features.h +++ b/device/gamepad/public/cpp/gamepad_features.h @@ -11,8 +11,10 @@ namespace features { GAMEPAD_FEATURES_EXPORT BASE_DECLARE_FEATURE(kEnableGamepadMultitouch); +GAMEPAD_FEATURES_EXPORT BASE_DECLARE_FEATURE(kRestrictGamepadAccess); GAMEPAD_FEATURES_EXPORT bool IsGamepadMultitouchEnabled(); +GAMEPAD_FEATURES_EXPORT bool IsRestrictGamepadAccessEnabled(); } // namespace features diff --git a/third_party/blink/renderer/modules/gamepad/navigator_gamepad.cc b/third_party/blink/renderer/modules/gamepad/navigator_gamepad.cc --- a/third_party/blink/renderer/modules/gamepad/navigator_gamepad.cc +++ b/third_party/blink/renderer/modules/gamepad/navigator_gamepad.cc @@ -27,6 +27,7 @@ #include "base/auto_reset.h" #include "device/gamepad/public/cpp/gamepads.h" +#include "device/gamepad/public/cpp/gamepad_features.h" #include "third_party/blink/public/common/privacy_budget/identifiability_metric_builder.h" #include "third_party/blink/public/common/privacy_budget/identifiability_study_settings.h" #include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom-blink.h" @@ -112,6 +113,10 @@ void RecordGamepadsForIdentifiabilityStudy( HeapVector> NavigatorGamepad::getGamepads( Navigator& navigator, ExceptionState& exception_state) { + if (::features::IsRestrictGamepadAccessEnabled()) { + exception_state.ThrowSecurityError("Access to the feature \"gamepad\" is denied"); + return HeapVector>(); + } if (!navigator.DomWindow()) { // Using an existing NavigatorGamepad if one exists, but don't create one // for a detached window, as its subclasses depend on a non-null window. @@ -418,6 +423,8 @@ void NavigatorGamepad::SampleAndCompareGamepadState() { void NavigatorGamepad::DispatchGamepadEvent(const AtomicString& event_name, Gamepad* gamepad) { + if (::features::IsRestrictGamepadAccessEnabled()) + return; // Ensure that we're blocking re-entrancy. DCHECK(processing_events_); DCHECK(has_connection_event_listener_); --