Add environment_construction poison

This poison guards against accidental use of EnvironmentFactory and thus ensures low level WebRTC class would use utilities from propagated environment instead of accidentally using a default implementation.

This poison extends and thus replaces default task queue poison.

Bug: webrtc:15656
Change-Id: I577bef8af08b9c7dd649ad5a2284eb236e6f4a8f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/328380
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41247}
diff --git a/BUILD.gn b/BUILD.gn
index baf8b09..571049f 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -802,10 +802,10 @@
 group("poison_audio_codecs") {
 }
 
-group("poison_default_task_queue") {
+group("poison_default_echo_detector") {
 }
 
-group("poison_default_echo_detector") {
+group("poison_environment_construction") {
 }
 
 group("poison_software_video_codecs") {
diff --git a/api/BUILD.gn b/api/BUILD.gn
index e57092e..c187a7d 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -54,7 +54,7 @@
   visibility = [ "*" ]
   allow_poison = [
     "audio_codecs",
-    "default_task_queue",
+    "environment_construction",
     "software_video_codecs",
   ]
   sources = [
@@ -77,7 +77,7 @@
 if (!build_with_chromium) {
   rtc_library("create_peerconnection_factory") {
     visibility = [ "*" ]
-    allow_poison = [ "default_task_queue" ]
+    allow_poison = [ "environment_construction" ]
     sources = [
       "create_peerconnection_factory.cc",
       "create_peerconnection_factory.h",
diff --git a/api/environment/BUILD.gn b/api/environment/BUILD.gn
index 19d52be..c2b73b3 100644
--- a/api/environment/BUILD.gn
+++ b/api/environment/BUILD.gn
@@ -21,10 +21,7 @@
 
 rtc_library("environment_factory") {
   visibility = [ "*" ]
-
-  # TODO: bugs.webrtc.org/15656 - introduce new poison for environment
-  # construction and consume default_task_queue poison with it.
-  allow_poison = [ "default_task_queue" ]
+  poisonous = [ "environment_construction" ]
   sources = [
     "environment_factory.cc",
     "environment_factory.h",
diff --git a/api/task_queue/BUILD.gn b/api/task_queue/BUILD.gn
index e0e2e50..246164c 100644
--- a/api/task_queue/BUILD.gn
+++ b/api/task_queue/BUILD.gn
@@ -83,7 +83,10 @@
 rtc_library("default_task_queue_factory") {
   visibility = [ "*" ]
   if (!is_ios && !is_android) {
-    poisonous = [ "default_task_queue" ]
+    # Internally webrtc shouldn't rely on any specific TaskQueue implementation
+    # and should create TaskQueue using TaskQueueFactory interface.
+    # TaskQueueFactory interface can be propagated with Environment.
+    poisonous = [ "environment_construction" ]
   }
   sources = [ "default_task_queue_factory.h" ]
   deps = [
diff --git a/media/BUILD.gn b/media/BUILD.gn
index d12c929..6b8eaf7 100644
--- a/media/BUILD.gn
+++ b/media/BUILD.gn
@@ -582,7 +582,7 @@
   visibility = [ "*" ]
   allow_poison = [
     "audio_codecs",
-    "default_task_queue",
+    "environment_construction",
     "software_video_codecs",
   ]
   sources = [
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index 1ff7b99..dfb98ff 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -739,6 +739,7 @@
 rtc_source_set("peerconnection") {
   # TODO(bugs.webrtc.org/13661): Reduce visibility if possible
   visibility = [ "*" ]  # Used by Chromium and others
+  allow_poison = [ "environment_construction" ]
   cflags = []
   sources = []
 
@@ -1496,6 +1497,7 @@
 rtc_source_set("peer_connection_factory") {
   # TODO(bugs.webrtc.org/13661): Reduce visibility if possible
   visibility = [ "*" ]  # Known to be used externally
+  allow_poison = [ "environment_construction" ]
   sources = [
     "peer_connection_factory.cc",
     "peer_connection_factory.h",
@@ -1520,6 +1522,7 @@
     "../api:rtp_parameters",
     "../api:scoped_refptr",
     "../api:sequence_checker",
+    "../api/environment:environment_factory",
     "../api/metronome",
     "../api/neteq:neteq_api",
     "../api/rtc_event_log:rtc_event_log",
@@ -2061,6 +2064,7 @@
 rtc_source_set("libjingle_peerconnection") {
   # TODO(bugs.webrtc.org/13661): Reduce visibility if possible
   visibility = [ "*" ]  # Used by Chrome and others
+  allow_poison = [ "environment_construction" ]
 
   deps = [
     ":peerconnection",
diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn
index bff9b91..e28bdcc 100644
--- a/sdk/BUILD.gn
+++ b/sdk/BUILD.gn
@@ -254,7 +254,7 @@
         visibility = [ "*" ]
         allow_poison = [
           "audio_codecs",  # TODO(bugs.webrtc.org/8396): Remove.
-          "default_task_queue",
+          "environment_construction",
         ]
         sources = [
           "objc/components/renderer/opengl/RTCDisplayLinkTimer.h",
@@ -482,7 +482,7 @@
 
     rtc_library("audio_device_objc") {
       visibility = [ "*" ]
-      allow_poison = [ "default_task_queue" ]
+      allow_poison = [ "environment_construction" ]
       sources = [
         "objc/native/src/objc_audio_device.h",
         "objc/native/src/objc_audio_device.mm",
@@ -515,7 +515,7 @@
 
     rtc_library("objc_audio_device_module") {
       visibility = [ "*" ]
-      allow_poison = [ "default_task_queue" ]
+      allow_poison = [ "environment_construction" ]
       sources = [
         "objc/native/api/objc_audio_device_module.h",
         "objc/native/api/objc_audio_device_module.mm",
@@ -598,7 +598,7 @@
       visibility = [ "*" ]
       allow_poison = [
         "audio_codecs",  # TODO(bugs.webrtc.org/8396): Remove.
-        "default_task_queue",
+        "environment_construction",
       ]
       sources = [
         "objc/components/renderer/metal/RTCMTLI420Renderer.h",
@@ -920,7 +920,7 @@
       visibility = [ "*" ]
       allow_poison = [
         "audio_codecs",  # TODO(bugs.webrtc.org/8396): Remove.
-        "default_task_queue",
+        "environment_construction",
       ]
       configs += [ "..:no_global_constructors" ]
       sources = [
diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn
index b050933..e8e8075 100644
--- a/sdk/android/BUILD.gn
+++ b/sdk/android/BUILD.gn
@@ -109,6 +109,7 @@
     visibility = [ "*" ]
     allow_poison = [
       "audio_codecs",  # TODO(bugs.webrtc.org/8396): Remove.
+      "environment_construction",
       "software_video_codecs",  # TODO(bugs.webrtc.org/7925): Remove.
     ]
     public_deps = [  # no-presubmit-check TODO(webrtc:8603)
@@ -826,7 +827,6 @@
       ":generated_metrics_jni",
       ":native_api_jni",
       ":peerconnection_jni",
-      "../../pc:peerconnection",
       "../../rtc_base:stringutils",
       "../../system_wrappers:metrics",
     ]
diff --git a/webrtc.gni b/webrtc.gni
index 173d66c..447aae4 100644
--- a/webrtc.gni
+++ b/webrtc.gni
@@ -474,12 +474,14 @@
   # Encoders and decoders for specific audio codecs such as Opus and iSAC.
   "audio_codecs",
 
-  # Default task queue implementation.
-  "default_task_queue",
-
   # Default echo detector implementation.
   "default_echo_detector",
 
+  # Implementations of the utilities exposed through `Environment`.
+  # Most webrtc classes must use propagated `Environment`. Only few top-level
+  # classes are allowed to create `Environment` from individual utilities.
+  "environment_construction",
+
   # Software video codecs (VP8 and VP9 through libvpx).
   "software_video_codecs",
 ]