Remove webrtc::test::InitFieldTrialsFromString(const std::string&).

This is done to solve a problem where a string literal is implicitly cast
to a temporary std::string when calling webrtc::test::InitFieldTrialsFromString
which passes a pointer to the internal representation to
webrtc::field_trial::InitFieldTrialFromString(char*). This pointer is
stored for later use, but the temporary std::string is destroyed as soon
as the function returns.

Using webrtc::field_trial::InitFieldTrialFromString(char*) instead,
avoids the implicit casts (but the caller still needs to ensure that
the char* outlives the program). The validation previously done by
webrtc::test::InitFieldTrialsFromString can now be done by manually
calling webrtc::test::ValidateFieldTrialsStringOrDie(const std::string&).

Add system_wrappers:field_trial_default as a direct dependency to
various targets to allow including the field_trials_default.h header.

Bug: webrtc:8812
Change-Id: Ib5a641ea255b1c16a8f7f35e1fe67f6c38a61da6
Reviewed-on: https://webrtc-review.googlesource.com/46141
Reviewed-by: Tommi <tommi@webrtc.org>
Commit-Queue: Oleh Prypin <oprypin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21856}
diff --git a/test/BUILD.gn b/test/BUILD.gn
index bfc521d..72bc203 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -227,6 +227,7 @@
       ":field_trial",
       ":perf_test",
       "../rtc_base:rtc_base_approved",
+      "../system_wrappers:field_trial_default",
       "../system_wrappers:metrics_default",
       "../system_wrappers:runtime_enabled_features_default",
       "//testing/gtest",
diff --git a/test/field_trial.cc b/test/field_trial.cc
index 00e5b69..9be36c5 100644
--- a/test/field_trial.cc
+++ b/test/field_trial.cc
@@ -26,9 +26,8 @@
 }  // namespace
 
 namespace test {
-// Note: this code is copied from src/base/metrics/field_trial.cc since the aim
-// is to mimic chromium --force-fieldtrials.
-void InitFieldTrialsFromString(const std::string& trials_string) {
+
+void ValidateFieldTrialsStringOrDie(const std::string& trials_string) {
   static const char kPersistentStringSeparator = '/';
 
   // Catch an error if this is called more than once.
@@ -63,7 +62,7 @@
 
     // Successfully parsed all field trials from the string.
     if (next_item == trials_string.length()) {
-      webrtc::field_trial::InitFieldTrialsFromString(trials_string.c_str());
+      // webrtc::field_trial::InitFieldTrialsFromString(trials_string.c_str());
       return;
     }
   }
@@ -79,7 +78,8 @@
   assert(field_trials_initiated_);
   field_trials_initiated_ = false;
   current_field_trials_ = config;
-  InitFieldTrialsFromString(current_field_trials_);
+  ValidateFieldTrialsStringOrDie(current_field_trials_);
+  webrtc::field_trial::InitFieldTrialsFromString(current_field_trials_.c_str());
 }
 
 ScopedFieldTrials::~ScopedFieldTrials() {
diff --git a/test/field_trial.h b/test/field_trial.h
index 99ace4e..40f7b1b 100644
--- a/test/field_trial.h
+++ b/test/field_trial.h
@@ -30,7 +30,7 @@
 //
 // Note: This method crashes with an error message if an invalid config is
 // passed to it. That can be used to find out if a binary is parsing the flags.
-void InitFieldTrialsFromString(const std::string& config);
+void ValidateFieldTrialsStringOrDie(const std::string& config);
 
 // This class is used to override field-trial configs within specific tests.
 // After this class goes out of scope previous field trials will be restored.
diff --git a/test/test_main.cc b/test/test_main.cc
index f86dd1f..962c677 100644
--- a/test/test_main.cc
+++ b/test/test_main.cc
@@ -10,6 +10,7 @@
 
 #include "rtc_base/flags.h"
 #include "rtc_base/logging.h"
+#include "system_wrappers/include/field_trial_default.h"
 #include "system_wrappers/include/metrics_default.h"
 #include "test/field_trial.h"
 #include "test/gmock.h"
@@ -70,8 +71,10 @@
   }
 
   webrtc::test::SetExecutablePath(argv[0]);
-  std::string fieldtrials = FLAG_force_fieldtrials;
-  webrtc::test::InitFieldTrialsFromString(fieldtrials);
+  webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
+  // InitFieldTrialsFromString stores the char*, so the char array must outlive
+  // the application.
+  webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
   webrtc::metrics::Enable();