From: uazo <uazo@users.noreply.github.com>
Date: Fri, 6 May 2022 14:27:17 +0000
Subject: Add webRTC site settings

Requires patch: Content-settings-infrastructure.patch

Original License: GPL-2.0-or-later - https://spdx.org/licenses/GPL-2.0-or-later.html
License: GPL-3.0-only - https://spdx.org/licenses/GPL-3.0-only.html
---
 .../browser_ui/site_settings/android/BUILD.gn |  3 +
 .../BromiteCustomContentSettingImpl.java      |  1 +
 .../BromiteWebRTCContentSetting.java          | 86 +++++++++++++++++++
 .../site_settings/SiteSettingsCategory.java   |  5 +-
 .../strings/android/browser_ui_strings.grd    |  1 +
 .../browser_ui/strings/android/webrtc.grdp    | 18 ++++
 components/components_strings.grd             |  1 +
 .../core/browser/content_settings_registry.cc | 14 +++
 .../core/browser/content_settings_utils.cc    |  2 +
 .../core/common/content_settings.cc           |  4 +-
 .../core/common/content_settings.h            |  1 +
 .../core/common/content_settings.mojom        |  1 +
 .../common/content_settings_mojom_traits.cc   |  3 +-
 .../common/content_settings_mojom_traits.h    |  5 ++
 .../core/common/content_settings_types.h      |  2 +
 .../renderer/content_settings_agent_impl.cc   |  9 ++
 .../renderer/content_settings_agent_impl.h    |  1 +
 .../platform/web_content_settings_client.h    |  2 +
 .../peer_connection_dependency_factory.cc     |  6 ++
 19 files changed, 161 insertions(+), 4 deletions(-)
 create mode 100644 components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/BromiteWebRTCContentSetting.java
 create mode 100644 components/browser_ui/strings/android/webrtc.grdp

diff --git a/components/browser_ui/site_settings/android/BUILD.gn b/components/browser_ui/site_settings/android/BUILD.gn
--- a/components/browser_ui/site_settings/android/BUILD.gn
+++ b/components/browser_ui/site_settings/android/BUILD.gn
@@ -95,6 +95,9 @@ android_library("java") {
   sources += [
     "java/src/org/chromium/components/browser_ui/site_settings/BromiteWebGLContentSetting.java",
   ]
+  sources += [
+    "java/src/org/chromium/components/browser_ui/site_settings/BromiteWebRTCContentSetting.java",
+  ]
   annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
   resources_package = "org.chromium.components.browser_ui.site_settings"
   deps = [
diff --git a/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/BromiteCustomContentSettingImpl.java b/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/BromiteCustomContentSettingImpl.java
--- a/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/BromiteCustomContentSettingImpl.java
+++ b/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/BromiteCustomContentSettingImpl.java
@@ -44,6 +44,7 @@ public abstract class BromiteCustomContentSettingImpl {
     static {
         mItemList = new ArrayList<BromiteCustomContentSetting>();
         mItemList.add(new BromiteWebGLContentSetting());
+        mItemList.add(new BromiteWebRTCContentSetting());
     }
 
     public static SiteSettingsCategory createFromType(
diff --git a/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/BromiteWebRTCContentSetting.java b/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/BromiteWebRTCContentSetting.java
new file mode 100644
--- /dev/null
+++ b/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/BromiteWebRTCContentSetting.java
@@ -0,0 +1,86 @@
+/*
+    This file is part of Bromite.
+
+    Bromite is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Bromite is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Bromite. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+package org.chromium.components.browser_ui.site_settings;
+
+import org.chromium.components.browser_ui.site_settings.ContentSettingsResources;
+import org.chromium.components.browser_ui.site_settings.SiteSettingsCategory;
+import org.chromium.components.content_settings.ContentSettingValues;
+import org.chromium.components.content_settings.ContentSettingsType;
+import org.chromium.content_public.browser.BrowserContextHandle;
+
+import androidx.annotation.Nullable;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+import java.util.ArrayList;
+
+public class BromiteWebRTCContentSetting extends BromiteCustomContentSetting {
+    public BromiteWebRTCContentSetting() {
+        super(/*contentSettingsType*/ ContentSettingsType.WEBRTC,
+              /*siteSettingsCategory*/ SiteSettingsCategory.Type.WEBRTC,
+              /*defaultEnabledValue*/ ContentSettingValues.ALLOW,
+              /*defaultDisabledValue*/ ContentSettingValues.BLOCK,
+              /*allowException*/ true,
+              /*preferenceKey*/ "webrtc",
+              /*profilePrefKey*/ "webrtc");
+    }
+
+    @Override
+    public ContentSettingsResources.ResourceItem getResourceItem() {
+        return new ContentSettingsResources.ResourceItem(
+            /*icon*/ R.drawable.web_asset,
+            /*title*/ R.string.webrtc_permission_title,
+            /*defaultEnabledValue*/ getDefaultEnabledValue(),
+            /*defaultDisabledValue*/ getDefaultDisabledValue(),
+            /*enabledSummary*/ R.string.website_settings_category_webrtc_enabled,
+            /*disabledSummary*/ R.string.website_settings_category_webrtc_disabled);
+    }
+
+    @Override
+    public int getCategorySummary(@Nullable @ContentSettingValues int value) {
+        switch (value) {
+            case ContentSettingValues.ALLOW:
+                return R.string.website_settings_category_webrtc_enabled;
+            case ContentSettingValues.BLOCK:
+                return R.string.website_settings_category_webrtc_disabled;
+            default:
+                // this will cause a runtime exception
+                return 0;
+        }
+    }
+
+    @Override
+    public boolean requiresTriStateContentSetting() {
+        return false;
+    }
+
+    @Override
+    public boolean showOnlyDescriptions() {
+        return true;
+    }
+
+    @Override
+    public int getAddExceptionDialogMessage() {
+        return R.string.website_settings_category_webrtc_enabled;
+    }
+
+    @Override
+    public @Nullable Boolean considerException(SiteSettingsCategory category, @ContentSettingValues int value) {
+        return value != ContentSettingValues.BLOCK;
+    }
+}
diff --git a/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/SiteSettingsCategory.java b/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/SiteSettingsCategory.java
--- a/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/SiteSettingsCategory.java
+++ b/components/browser_ui/site_settings/android/java/src/org/chromium/components/browser_ui/site_settings/SiteSettingsCategory.java
@@ -44,7 +44,7 @@ public class SiteSettingsCategory {
             Type.PROTECTED_MEDIA, Type.SENSORS, Type.SOUND, Type.USB, Type.VIRTUAL_REALITY,
             Type.USE_STORAGE, Type.AUTO_DARK_WEB_CONTENT, Type.REQUEST_DESKTOP_SITE,
             Type.FEDERATED_IDENTITY_API, Type.TIMEZONE_OVERRIDE, Type.AUTOPLAY, Type.JAVASCRIPT_JIT,
-            Type.IMAGES, Type.WEBGL})
+            Type.IMAGES, Type.WEBGL, Type.WEBRTC})
     @Retention(RetentionPolicy.SOURCE)
     public @interface Type {
         // All updates here must also be reflected in {@link #preferenceKey(int)
@@ -80,10 +80,11 @@ public class SiteSettingsCategory {
         int JAVASCRIPT_JIT = 28;
         int IMAGES = 29;
         int WEBGL = 30;
+        int WEBRTC = 31;
         /**
          * Number of handled categories used for calculating array sizes.
          */
-        int NUM_ENTRIES = 31;
+        int NUM_ENTRIES = 32;
     }
 
     private final BrowserContextHandle mBrowserContextHandle;
diff --git a/components/browser_ui/strings/android/browser_ui_strings.grd b/components/browser_ui/strings/android/browser_ui_strings.grd
--- a/components/browser_ui/strings/android/browser_ui_strings.grd
+++ b/components/browser_ui/strings/android/browser_ui_strings.grd
@@ -175,6 +175,7 @@
     <messages fallback_to_english="true">
       <part file="site_settings.grdp" />
       <part file="webgl.grdp" />
+      <part file="webrtc.grdp" />
 
       <message name="IDS_GOT_IT" desc="Button for the user to accept a disclosure/message" formatter_data="android_java">
         Got it
diff --git a/components/browser_ui/strings/android/webrtc.grdp b/components/browser_ui/strings/android/webrtc.grdp
new file mode 100644
--- /dev/null
+++ b/components/browser_ui/strings/android/webrtc.grdp
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<grit-part>
+  <message name="IDS_SITE_SETTINGS_TYPE_WEBRTC" desc="The label used for webrtc site settings controls.">
+    WebRTC
+  </message>
+  <message name="IDS_SITE_SETTINGS_TYPE_WEBRTC_MID_SENTENCE" desc="The label used for webrtc site settings controls when used mid-sentence.">
+    webRTC
+  </message>
+  <message name="IDS_WEBRTC_PERMISSION_TITLE" desc="Title of the permission to use webrtc [CHAR-LIMIT=32]">
+    WebRTC
+  </message>
+  <message name="IDS_WEBSITE_SETTINGS_CATEGORY_WEBRTC_ENABLED" desc="Summary text explaining that webrtc is full enabled.">
+    Enabled
+  </message>
+  <message name="IDS_WEBSITE_SETTINGS_CATEGORY_WEBRTC_DISABLED" desc="Summary text explaining that webrtc is full disabled.">
+    Disabled
+  </message>
+</grit-part>
diff --git a/components/components_strings.grd b/components/components_strings.grd
--- a/components/components_strings.grd
+++ b/components/components_strings.grd
@@ -339,6 +339,7 @@
       <part file="webapps_strings.grdp" />
 
       <part file="browser_ui/strings/android/webgl.grdp" />
+      <part file="browser_ui/strings/android/webrtc.grdp" />
       <if expr="not is_ios">
         <part file="history_clusters_strings.grdp" />
       </if>
diff --git a/components/content_settings/core/browser/content_settings_registry.cc b/components/content_settings/core/browser/content_settings_registry.cc
--- a/components/content_settings/core/browser/content_settings_registry.cc
+++ b/components/content_settings/core/browser/content_settings_registry.cc
@@ -683,6 +683,20 @@ void ContentSettingsRegistry::Init() {
            /*show_into_info_page*/ true,
            /*permission_type_ui*/ IDS_SITE_SETTINGS_TYPE_WEBGL,
            /*permission_type_ui_mid_sentence*/ IDS_SITE_SETTINGS_TYPE_WEBGL_MID_SENTENCE);
+
+  Register(ContentSettingsType::WEBRTC, "webrtc", CONTENT_SETTING_BLOCK,
+           WebsiteSettingsInfo::SYNCABLE,
+           AllowlistedSchemes(),
+           ValidSettings(CONTENT_SETTING_ALLOW,
+                         CONTENT_SETTING_BLOCK),
+           WebsiteSettingsInfo::TOP_ORIGIN_ONLY_SCOPE,
+           WebsiteSettingsRegistry::PLATFORM_ANDROID,
+           ContentSettingsInfo::INHERIT_IN_INCOGNITO,
+           ContentSettingsInfo::PERSISTENT,
+           ContentSettingsInfo::EXCEPTIONS_ON_SECURE_AND_INSECURE_ORIGINS,
+           /*show_into_info_page*/ true,
+           /*permission_type_ui*/ IDS_SITE_SETTINGS_TYPE_WEBRTC,
+           /*permission_type_ui_mid_sentence*/ IDS_SITE_SETTINGS_TYPE_WEBRTC_MID_SENTENCE);
 }
 
 void ContentSettingsRegistry::Register(
diff --git a/components/content_settings/core/browser/content_settings_utils.cc b/components/content_settings/core/browser/content_settings_utils.cc
--- a/components/content_settings/core/browser/content_settings_utils.cc
+++ b/components/content_settings/core/browser/content_settings_utils.cc
@@ -158,6 +158,8 @@ void GetRendererContentSettingRules(const HostContentSettingsMap* map,
   rules->timezone_override_value = timezone;
   map->GetSettingsForOneType(ContentSettingsType::WEBGL,
                              &(rules->webgl_rules));
+  map->GetSettingsForOneType(ContentSettingsType::WEBRTC,
+                             &(rules->webrtc_rules));
 }
 
 bool IsMorePermissive(ContentSetting a, ContentSetting b) {
diff --git a/components/content_settings/core/common/content_settings.cc b/components/content_settings/core/common/content_settings.cc
--- a/components/content_settings/core/common/content_settings.cc
+++ b/components/content_settings/core/common/content_settings.cc
@@ -207,7 +207,8 @@ bool RendererContentSettingRules::IsRendererContentSetting(
          content_type == ContentSettingsType::MIXEDSCRIPT ||
          content_type == ContentSettingsType::AUTO_DARK_WEB_CONTENT ||
          content_type == ContentSettingsType::TIMEZONE_OVERRIDE ||
-         content_type == ContentSettingsType::WEBGL;
+         content_type == ContentSettingsType::WEBGL ||
+         content_type == ContentSettingsType::WEBRTC;
 }
 
 void RendererContentSettingRules::FilterRulesByOutermostMainFrameURL(
@@ -220,6 +221,7 @@ void RendererContentSettingRules::FilterRulesByOutermostMainFrameURL(
   FilterRulesForType(timezone_override_rules, outermost_main_frame_url);
   FilterRulesForType(autoplay_rules, outermost_main_frame_url);
   FilterRulesForType(webgl_rules, outermost_main_frame_url);
+  FilterRulesForType(webrtc_rules, outermost_main_frame_url);
 }
 
 RendererContentSettingRules::RendererContentSettingRules() = default;
diff --git a/components/content_settings/core/common/content_settings.h b/components/content_settings/core/common/content_settings.h
--- a/components/content_settings/core/common/content_settings.h
+++ b/components/content_settings/core/common/content_settings.h
@@ -97,6 +97,7 @@ struct RendererContentSettingRules {
   ContentSettingsForOneType timezone_override_rules;
   std::string timezone_override_value;
   ContentSettingsForOneType webgl_rules;
+  ContentSettingsForOneType webrtc_rules;
 };
 
 namespace content_settings {
diff --git a/components/content_settings/core/common/content_settings.mojom b/components/content_settings/core/common/content_settings.mojom
--- a/components/content_settings/core/common/content_settings.mojom
+++ b/components/content_settings/core/common/content_settings.mojom
@@ -82,4 +82,5 @@ struct RendererContentSettingRules {
   array<ContentSettingPatternSource> timezone_override_rules;
   string timezone_override_value;
   array<ContentSettingPatternSource> webgl_rules;
+  array<ContentSettingPatternSource> webrtc_rules;
 };
diff --git a/components/content_settings/core/common/content_settings_mojom_traits.cc b/components/content_settings/core/common/content_settings_mojom_traits.cc
--- a/components/content_settings/core/common/content_settings_mojom_traits.cc
+++ b/components/content_settings/core/common/content_settings_mojom_traits.cc
@@ -106,7 +106,8 @@ bool StructTraits<content_settings::mojom::RendererContentSettingRulesDataView,
          data.ReadAutoDarkContentRules(&out->auto_dark_content_rules) &&
          data.ReadTimezoneOverrideRules(&out->timezone_override_rules) &&
          data.ReadTimezoneOverrideValue(&out->timezone_override_value) &&
-         data.ReadWebglRules(&out->webgl_rules);
+         data.ReadWebglRules(&out->webgl_rules) &&
+         data.ReadWebrtcRules(&out->webrtc_rules);
 }
 
 }  // namespace mojo
diff --git a/components/content_settings/core/common/content_settings_mojom_traits.h b/components/content_settings/core/common/content_settings_mojom_traits.h
--- a/components/content_settings/core/common/content_settings_mojom_traits.h
+++ b/components/content_settings/core/common/content_settings_mojom_traits.h
@@ -170,6 +170,11 @@ struct StructTraits<
     return r.webgl_rules;
   }
 
+  static const std::vector<ContentSettingPatternSource>& webrtc_rules(
+      const RendererContentSettingRules& r) {
+    return r.webrtc_rules;
+  }
+
   static bool Read(
       content_settings::mojom::RendererContentSettingRulesDataView data,
       RendererContentSettingRules* out);
diff --git a/components/content_settings/core/common/content_settings_types.h b/components/content_settings/core/common/content_settings_types.h
--- a/components/content_settings/core/common/content_settings_types.h
+++ b/components/content_settings/core/common/content_settings_types.h
@@ -279,6 +279,8 @@ enum class ContentSettingsType : int32_t {
 
   WEBGL,
 
+  WEBRTC,
+
   // Setting to indicate whether browser should allow signing into a website via
   // the browser FedCM API.
   FEDERATED_IDENTITY_API,
diff --git a/components/content_settings/renderer/content_settings_agent_impl.cc b/components/content_settings/renderer/content_settings_agent_impl.cc
--- a/components/content_settings/renderer/content_settings_agent_impl.cc
+++ b/components/content_settings/renderer/content_settings_agent_impl.cc
@@ -458,6 +458,15 @@ bool ContentSettingsAgentImpl::AllowWebgl(bool enabled_per_settings) {
              url::Origin(frame->GetDocument().GetSecurityOrigin()).GetURL());
 }
 
+bool ContentSettingsAgentImpl::AllowWebRTC(bool enabled_per_settings) {
+  if (!content_setting_rules_)
+    return false;
+  blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
+  return CONTENT_SETTING_ALLOW == GetContentSettingFromRules(
+             content_setting_rules_->webrtc_rules,
+             url::Origin(frame->GetDocument().GetSecurityOrigin()).GetURL());
+}
+
 bool ContentSettingsAgentImpl::IsAllowlistedForContentSettings() const {
   if (should_allowlist_)
     return true;
diff --git a/components/content_settings/renderer/content_settings_agent_impl.h b/components/content_settings/renderer/content_settings_agent_impl.h
--- a/components/content_settings/renderer/content_settings_agent_impl.h
+++ b/components/content_settings/renderer/content_settings_agent_impl.h
@@ -100,6 +100,7 @@ class ContentSettingsAgentImpl
   bool AllowPopupsAndRedirects(bool default_value) override;
   bool ShouldAutoupgradeMixedContent() override;
   bool AllowWebgl(bool enabled_per_settings) override;
+  bool AllowWebRTC(bool enabled_per_settings) override;
 
   bool allow_running_insecure_content() const {
     return allow_running_insecure_content_;
diff --git a/third_party/blink/public/platform/web_content_settings_client.h b/third_party/blink/public/platform/web_content_settings_client.h
--- a/third_party/blink/public/platform/web_content_settings_client.h
+++ b/third_party/blink/public/platform/web_content_settings_client.h
@@ -101,6 +101,8 @@ class WebContentSettingsClient {
 
   virtual bool AllowWebgl(bool default_value) { return default_value; }
 
+  virtual bool AllowWebRTC(bool default_value) { return default_value; }
+
   // Reports that passive mixed content was found at the provided URL.
   virtual void PassiveInsecureContentFound(const WebURL&) {}
 
diff --git a/third_party/blink/renderer/modules/peerconnection/peer_connection_dependency_factory.cc b/third_party/blink/renderer/modules/peerconnection/peer_connection_dependency_factory.cc
--- a/third_party/blink/renderer/modules/peerconnection/peer_connection_dependency_factory.cc
+++ b/third_party/blink/renderer/modules/peerconnection/peer_connection_dependency_factory.cc
@@ -33,6 +33,7 @@
 #include "third_party/blink/public/common/peerconnection/webrtc_ip_handling_policy.h"
 #include "third_party/blink/public/platform/modules/webrtc/webrtc_logging.h"
 #include "third_party/blink/public/platform/platform.h"
+#include "third_party/blink/public/platform/web_content_settings_client.h"
 #include "third_party/blink/public/platform/web_url.h"
 #include "third_party/blink/public/web/modules/mediastream/media_stream_video_source.h"
 #include "third_party/blink/public/web/web_document.h"
@@ -765,6 +766,11 @@ PeerConnectionDependencyFactory::CreatePortAllocator(
       // origin.
       WebRTCIPHandlingPolicy policy =
           GetWebRTCIPHandlingPolicy(webrtc_ip_handling_policy);
+      blink::WebContentSettingsClient* settings = web_frame->GetContentSettingsClient();
+      if (settings && settings->AllowWebRTC(false)) {
+        policy = kDefault;
+      }
+
       switch (policy) {
         // TODO(guoweis): specify the flag of disabling local candidate
         // collection when webrtc is updated.
--
2.25.1
