From: csagan5 <32685696+csagan5@users.noreply.github.com>
Date: Mon, 22 Oct 2018 21:29:53 +0200
Subject: Add flags to disable device motion and orientation APIs

Both flags will be disabled by default and prevent usage of gyroscope and
legacy acceleration events.
---
 chrome/browser/about_flags.cc                                  |  6 ++++++
 chrome/browser/flag_descriptions.cc                            |  8 ++++++++
 chrome/browser/flag_descriptions.h                             |  6 ++++++
 content/child/runtime_features.cc                              |  3 +++
 content/public/common/content_features.cc                      | 10 ++++++++++
 content/public/common/content_features.h                       |  2 ++
 third_party/blink/public/platform/web_runtime_features.h       |  2 ++
 third_party/blink/renderer/modules/modules_initializer.cc      |  9 ++++++---
 .../blink/renderer/platform/exported/web_runtime_features.cc   |  8 ++++++++
 .../blink/renderer/platform/runtime_enabled_features.json5     |  8 ++++++++
 10 files changed, 59 insertions(+), 3 deletions(-)

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
@@ -2473,6 +2473,12 @@ const FeatureEntry kFeatureEntries[] = {
     {"enable-generic-sensor", flag_descriptions::kEnableGenericSensorName,
      flag_descriptions::kEnableGenericSensorDescription, kOsAll,
      FEATURE_VALUE_TYPE(features::kGenericSensor)},
+    {"enable-device-motion", flag_descriptions::kEnableDeviceMotionName,
+     flag_descriptions::kEnableDeviceMotionDescription, kOsAll,
+     FEATURE_VALUE_TYPE(features::kDeviceMotion)},
+    {"enable-device-orientation", flag_descriptions::kEnableDeviceOrientationName,
+     flag_descriptions::kEnableDeviceOrientationDescription, kOsAll,
+     FEATURE_VALUE_TYPE(features::kDeviceOrientation)},
     {"enable-generic-sensor-extra-classes",
      flag_descriptions::kEnableGenericSensorExtraClassesName,
      flag_descriptions::kEnableGenericSensorExtraClassesDescription, kOsAll,
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
@@ -600,6 +600,14 @@ const char kEnableGenericSensorDescription[] =
     "Accelerometer, LinearAccelerationSensor, Gyroscope, "
     "AbsoluteOrientationSensor and RelativeOrientationSensor interfaces.";
 
+const char kEnableDeviceMotionName[] = "Enable device motion";
+const char kEnableDeviceMotionDescription[] =
+    "Enable device motion API which is used to detect changes in acceleration";
+
+const char kEnableDeviceOrientationName[] = "Enable device orientation";
+const char kEnableDeviceOrientationDescription[] =
+    "Enable device orientation API which is used to detect changes in orientation";
+
 const char kEnableGenericSensorExtraClassesName[] =
     "Generic Sensor Extra Classes";
 const char kEnableGenericSensorExtraClassesDescription[] =
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
@@ -382,6 +382,12 @@ extern const char kEnforceTLS13DowngradeDescription[];
 extern const char kEnableGenericSensorName[];
 extern const char kEnableGenericSensorDescription[];
 
+extern const char kEnableDeviceMotionName[];
+extern const char kEnableDeviceMotionDescription[];
+
+extern const char kEnableDeviceOrientationName[];
+extern const char kEnableDeviceOrientationDescription[];
+
 extern const char kEnableGenericSensorExtraClassesName[];
 extern const char kEnableGenericSensorExtraClassesDescription[];
 
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
@@ -93,6 +93,9 @@ void SetIndividualRuntimeFeatures(
       base::FeatureList::IsEnabled(features::kSharedArrayBuffer) ||
       base::FeatureList::IsEnabled(features::kWebAssemblyThreads));
 
+  WebRuntimeFeatures::EnableDeviceMotion(base::FeatureList::IsEnabled(features::kDeviceMotion));
+  WebRuntimeFeatures::EnableDeviceOrientation(base::FeatureList::IsEnabled(features::kDeviceOrientation));
+
   if (command_line.HasSwitch(switches::kDisableSharedWorkers))
     WebRuntimeFeatures::EnableSharedWorker(false);
 
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
@@ -596,6 +596,16 @@ const base::Feature kExperimentalProductivityFeatures{
 // TODO(rouslan): Remove this.
 const base::Feature kWebPayments{"WebPayments",
                                  base::FEATURE_ENABLED_BY_DEFAULT};
+// Enables the device motion API used to track device acceleration;
+// no user authorization or notifications happens when in use.
+const base::Feature kDeviceMotion{"DeviceMotion",
+                                    base::FEATURE_DISABLED_BY_DEFAULT};
+
+// Enables the device orientation API used to track device orientation;
+// no user authorization or notifications happens when in use.
+const base::Feature kDeviceOrientation{"DeviceOrientation",
+                                    base::FEATURE_DISABLED_BY_DEFAULT};
+
 
 // Makes WebRTC use ECDSA certs by default (i.e., when no cert type was
 // specified in JS).
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
@@ -158,6 +158,8 @@ CONTENT_EXPORT extern const base::Feature kWarmUpNetworkProcess;
 CONTENT_EXPORT extern const base::Feature kWebNfc;
 #endif  // defined(OS_ANDROID)
 
+CONTENT_EXPORT extern const base::Feature kDeviceMotion, kDeviceOrientation;
+
 #if !defined(OS_ANDROID)
 CONTENT_EXPORT extern const base::Feature kWebUIPolymer2;
 #endif  // !defined(OS_ANDROID)
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
@@ -97,6 +97,8 @@ class WebRuntimeFeatures {
   BLINK_PLATFORM_EXPORT static void EnableScrollTopLeftInterop(bool);
   BLINK_PLATFORM_EXPORT static void EnableKeyboardFocusableScrollers(bool);
   BLINK_PLATFORM_EXPORT static void EnableDatabase(bool);
+  BLINK_PLATFORM_EXPORT static void EnableDeviceMotion(bool);
+  BLINK_PLATFORM_EXPORT static void EnableDeviceOrientation(bool);
   BLINK_PLATFORM_EXPORT static void EnableDecodeToYUV(bool);
   BLINK_PLATFORM_EXPORT static void EnableDisplayCutoutAPI(bool);
   BLINK_PLATFORM_EXPORT static void EnableFallbackCursorMode(bool);
diff --git a/third_party/blink/renderer/modules/modules_initializer.cc b/third_party/blink/renderer/modules/modules_initializer.cc
--- a/third_party/blink/renderer/modules/modules_initializer.cc
+++ b/third_party/blink/renderer/modules/modules_initializer.cc
@@ -233,9 +233,12 @@ void ModulesInitializer::InitInspectorAgentSession(
 void ModulesInitializer::OnClearWindowObjectInMainWorld(
     Document& document,
     const Settings& settings) const {
-  DeviceMotionController::From(document);
-  DeviceOrientationController::From(document);
-  DeviceOrientationAbsoluteController::From(document);
+  if (RuntimeEnabledFeatures::DeviceMotionEnabled())
+    DeviceMotionController::From(document);
+  if (RuntimeEnabledFeatures::DeviceOrientationEnabled()) {
+    DeviceOrientationController::From(document);
+    DeviceOrientationAbsoluteController::From(document);
+  }
   NavigatorGamepad::From(document);
   NavigatorServiceWorker::From(document);
   DOMWindowStorageController::From(document);
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
@@ -165,6 +165,14 @@ void WebRuntimeFeatures::EnableDatabase(bool enable) {
   RuntimeEnabledFeatures::SetDatabaseEnabled(enable);
 }
 
+void WebRuntimeFeatures::EnableDeviceMotion(bool enable) {
+  RuntimeEnabledFeatures::SetDeviceMotionEnabled(enable);
+}
+
+void WebRuntimeFeatures::EnableDeviceOrientation(bool enable) {
+  RuntimeEnabledFeatures::SetDeviceOrientationEnabled(enable);
+}
+
 void WebRuntimeFeatures::EnableDecodeToYUV(bool enable) {
   RuntimeEnabledFeatures::SetDecodeToYUVEnabled(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
@@ -409,6 +409,14 @@
       status: "stable",
     },
     {
+      name: "DeviceMotion",
+      status: "stable",
+    },
+    {
+      name: "DeviceOrientation",
+      status: "stable",
+    },
+    {
       name: "DecodeToYUV",
       status: "experimental",
     },
-- 
2.11.0

