Add field trial for using different network cost cellular types

This field trial will be used to rollout the cellular costs added
in https://webrtc-review.googlesource.com/c/src/+/172582 in
a controlled fashion.

Bug: webrtc:11473
Change-Id: I14fd5cada187ba161124325a7ff69d355ef52b25
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/174880
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31233}
diff --git a/rtc_base/network.cc b/rtc_base/network.cc
index f30063d..64aee4b 100644
--- a/rtc_base/network.cc
+++ b/rtc_base/network.cc
@@ -35,6 +35,7 @@
 #include "rtc_base/string_utils.h"
 #include "rtc_base/strings/string_builder.h"
 #include "rtc_base/thread.h"
+#include "system_wrappers/include/field_trial.h"
 
 namespace rtc {
 namespace {
@@ -85,7 +86,8 @@
   return a->key() < b->key();
 }
 
-uint16_t ComputeNetworkCostByType(int type) {
+uint16_t ComputeNetworkCostByType(int type,
+                                  bool use_differentiated_cellular_costs) {
   // TODO(jonaso) : Rollout support for cellular network cost using A/B
   // experiment to make sure it does not introduce regressions.
   switch (type) {
@@ -95,11 +97,19 @@
     case rtc::ADAPTER_TYPE_WIFI:
       return kNetworkCostLow;
     case rtc::ADAPTER_TYPE_CELLULAR:
-    case rtc::ADAPTER_TYPE_CELLULAR_2G:
-    case rtc::ADAPTER_TYPE_CELLULAR_3G:
-    case rtc::ADAPTER_TYPE_CELLULAR_4G:
-    case rtc::ADAPTER_TYPE_CELLULAR_5G:
       return kNetworkCostCellular;
+    case rtc::ADAPTER_TYPE_CELLULAR_2G:
+      return use_differentiated_cellular_costs ? kNetworkCostCellular2G
+                                               : kNetworkCostCellular;
+    case rtc::ADAPTER_TYPE_CELLULAR_3G:
+      return use_differentiated_cellular_costs ? kNetworkCostCellular3G
+                                               : kNetworkCostCellular;
+    case rtc::ADAPTER_TYPE_CELLULAR_4G:
+      return use_differentiated_cellular_costs ? kNetworkCostCellular4G
+                                               : kNetworkCostCellular;
+    case rtc::ADAPTER_TYPE_CELLULAR_5G:
+      return use_differentiated_cellular_costs ? kNetworkCostCellular5G
+                                               : kNetworkCostCellular;
     case rtc::ADAPTER_TYPE_ANY:
       // Candidates gathered from the any-address/wildcard ports, as backups,
       // are given the maximum cost so that if there are other candidates with
@@ -930,7 +940,9 @@
       scope_id_(0),
       ignored_(false),
       type_(ADAPTER_TYPE_UNKNOWN),
-      preference_(0) {}
+      preference_(0),
+      use_differentiated_cellular_costs_(webrtc::field_trial::IsEnabled(
+          "WebRTC-UseDifferentiatedCellularCosts")) {}
 
 Network::Network(const std::string& name,
                  const std::string& desc,
@@ -945,7 +957,9 @@
       scope_id_(0),
       ignored_(false),
       type_(type),
-      preference_(0) {}
+      preference_(0),
+      use_differentiated_cellular_costs_(webrtc::field_trial::IsEnabled(
+          "WebRTC-UseDifferentiatedCellularCosts")) {}
 
 Network::Network(const Network&) = default;
 
@@ -1017,7 +1031,7 @@
 
 uint16_t Network::GetCost() const {
   AdapterType type = IsVpn() ? underlying_type_for_vpn_ : type_;
-  return ComputeNetworkCostByType(type);
+  return ComputeNetworkCostByType(type, use_differentiated_cellular_costs_);
 }
 
 std::string Network::ToString() const {
diff --git a/rtc_base/network.h b/rtc_base/network.h
index bd05b6a..a67d2a2 100644
--- a/rtc_base/network.h
+++ b/rtc_base/network.h
@@ -462,6 +462,7 @@
   int preference_;
   bool active_ = true;
   uint16_t id_ = 0;
+  bool use_differentiated_cellular_costs_ = false;
 
   friend class NetworkManager;
 };