From: csagan5 <32685696+csagan5@users.noreply.github.com>
Date: Sun, 27 Jun 2021 17:35:39 +0200
Subject: Add flag to disable vibration

License: GPL-3.0-only - https://spdx.org/licenses/GPL-3.0-only.html
---
 chrome/browser/about_flags.cc                                 | 4 ++++
 chrome/browser/flag_descriptions.cc                           | 3 +++
 chrome/browser/flag_descriptions.h                            | 2 ++
 content/child/runtime_features.cc                             | 1 +
 content/public/common/content_features.cc                     | 4 ++++
 content/public/common/content_features.h                      | 2 ++
 third_party/blink/public/platform/web_runtime_features.h      | 1 +
 .../blink/renderer/modules/vibration/vibration_controller.cc  | 3 +++
 .../blink/renderer/platform/exported/web_runtime_features.cc  | 4 ++++
 .../blink/renderer/platform/runtime_enabled_features.json5    | 4 ++++
 10 files changed, 28 insertions(+)

diff --git a/chrome/browser/about_flags.cc b/chrome/browser/about_flags.cc
--- a/chrome/browser/about_flags.cc
+++ b/chrome/browser/about_flags.cc
@@ -7467,6 +7467,10 @@ const FeatureEntry kFeatureEntries[] = {
      FEATURE_VALUE_TYPE(features::kNewMacNotificationAPI)},
 #endif
 
+    {"enable-vibration", flag_descriptions::kEnableVibrationName,
+     flag_descriptions::kEnableVibrationDescription, kOsAll,
+     FEATURE_VALUE_TYPE(features::kVibration)},
+
 #if BUILDFLAG(IS_CHROMEOS_ASH)
     {"exo-gamepad-vibration", flag_descriptions::kExoGamepadVibrationName,
      flag_descriptions::kExoGamepadVibrationDescription, kOsCrOS,
diff --git a/chrome/browser/flag_descriptions.cc b/chrome/browser/flag_descriptions.cc
--- a/chrome/browser/flag_descriptions.cc
+++ b/chrome/browser/flag_descriptions.cc
@@ -6463,6 +6463,9 @@ extern const char kLacrosScreenCoordinatesDescription[];
 
 #endif  // #if BUILDFLAG(IS_CHROMEOS_LACROS)
 
+const char kEnableVibrationName[] = "Vibration";
+const char kEnableVibrationDescription[] = "Enable vibration API; an user gesture will still be needed.";
+
 #if BUILDFLAG(IS_CHROMEOS)
 const char kAllowDefaultWebAppMigrationForChromeOsManagedUsersName[] =
     "Allow default web app migration for ChromeOS managed users";
diff --git a/chrome/browser/flag_descriptions.h b/chrome/browser/flag_descriptions.h
--- a/chrome/browser/flag_descriptions.h
+++ b/chrome/browser/flag_descriptions.h
@@ -3938,6 +3938,8 @@ extern const char kWebUITabStripTabDragIntegrationName[];
 extern const char kWebUITabStripTabDragIntegrationDescription[];
 #endif  // BUILDFLAG(ENABLE_WEBUI_TAB_STRIP) && BUILDFLAG(IS_CHROMEOS_ASH)
 
+extern const char kEnableVibrationName[];
+extern const char kEnableVibrationDescription[];
 #if defined(TOOLKIT_VIEWS) || BUILDFLAG(IS_ANDROID)
 
 extern const char kAutofillCreditCardUploadName[];
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
@@ -101,6 +101,7 @@ void SetRuntimeFeatureDefaultsForPlatform(
 #if BUILDFLAG(IS_ANDROID)
   if (command_line.HasSwitch(switches::kDisableMediaSessionAPI))
     WebRuntimeFeatures::EnableMediaSession(false);
+  WebRuntimeFeatures::EnableVibration(base::FeatureList::IsEnabled(features::kVibration));
 #endif
 
 #if BUILDFLAG(IS_ANDROID)
diff --git a/content/public/common/content_features.cc b/content/public/common/content_features.cc
--- a/content/public/common/content_features.cc
+++ b/content/public/common/content_features.cc
@@ -130,6 +130,10 @@ BASE_FEATURE(kBackForwardCacheMemoryControls,
 #endif
 );
 
+// Enables vibration; an user gesture will still be required if enabled.
+const base::Feature kVibration{"Vibration",
+                                    base::FEATURE_DISABLED_BY_DEFAULT};
+
 // When this feature is enabled, private network requests initiated from
 // non-secure contexts in the `public` address space  are blocked.
 //
diff --git a/content/public/common/content_features.h b/content/public/common/content_features.h
--- a/content/public/common/content_features.h
+++ b/content/public/common/content_features.h
@@ -322,6 +322,8 @@ CONTENT_EXPORT extern const base::FeatureParam<base::TimeDelta>
     kCompositorLockTimeout;
 
 #endif  // BUILDFLAG(IS_ANDROID)
+CONTENT_EXPORT extern const base::Feature kVibration;
+
 
 #if BUILDFLAG(IS_MAC)
 CONTENT_EXPORT BASE_DECLARE_FEATURE(kDeviceMonitorMac);
diff --git a/third_party/blink/public/platform/web_runtime_features.h b/third_party/blink/public/platform/web_runtime_features.h
--- a/third_party/blink/public/platform/web_runtime_features.h
+++ b/third_party/blink/public/platform/web_runtime_features.h
@@ -70,6 +70,7 @@ class BLINK_PLATFORM_EXPORT WebRuntimeFeatures : public WebRuntimeFeaturesBase {
   static void EnableFingerprintingCanvasMeasureTextNoise(bool);
   static void EnableFingerprintingCanvasImageDataNoise(bool);
   static void EnableFluentScrollbars(bool);
+  static void EnableVibration(bool);
 
   WebRuntimeFeatures() = delete;
 };
diff --git a/third_party/blink/renderer/modules/vibration/vibration_controller.cc b/third_party/blink/renderer/modules/vibration/vibration_controller.cc
--- a/third_party/blink/renderer/modules/vibration/vibration_controller.cc
+++ b/third_party/blink/renderer/modules/vibration/vibration_controller.cc
@@ -29,6 +29,7 @@
 #include "third_party/blink/renderer/core/frame/local_frame.h"
 #include "third_party/blink/renderer/core/frame/navigator.h"
 #include "third_party/blink/renderer/core/page/page.h"
+#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
 
 // Maximum number of entries in a vibration pattern.
 const unsigned kVibrationPatternLengthMax = 99;
@@ -152,6 +153,8 @@ bool VibrationController::vibrate(Navigator& navigator,
   // reference to |window| or |navigator| was retained in another window.
   if (!navigator.DomWindow())
     return false;
+  if (!RuntimeEnabledFeatures::VibrationEnabled())
+    return false;
   return From(navigator).Vibrate(pattern);
 }
 
diff --git a/third_party/blink/renderer/platform/exported/web_runtime_features.cc b/third_party/blink/renderer/platform/exported/web_runtime_features.cc
--- a/third_party/blink/renderer/platform/exported/web_runtime_features.cc
+++ b/third_party/blink/renderer/platform/exported/web_runtime_features.cc
@@ -52,6 +52,10 @@ void WebRuntimeFeatures::EnableTestOnlyFeatures(bool enable) {
   RuntimeEnabledFeatures::SetTestFeaturesEnabled(enable);
 }
 
+void WebRuntimeFeatures::EnableVibration(bool enable) {
+  RuntimeEnabledFeatures::SetVibrationEnabled(enable);
+}
+
 void WebRuntimeFeatures::EnableOriginTrialControlledFeatures(bool enable) {
   RuntimeEnabledFeatures::SetOriginTrialControlledFeaturesEnabled(enable);
 }
diff --git a/third_party/blink/renderer/platform/runtime_enabled_features.json5 b/third_party/blink/renderer/platform/runtime_enabled_features.json5
--- a/third_party/blink/renderer/platform/runtime_enabled_features.json5
+++ b/third_party/blink/renderer/platform/runtime_enabled_features.json5
@@ -1827,6 +1827,10 @@
       name: "OrientationEvent",
       status: {"Android": "stable"},
     },
+    {
+      name: "Vibration",
+      status: "stable",
+    },
     {
       name: "OriginIsolationHeader",
       status: "stable",
--
2.25.1
