Index: base/metrics/field_trial.cc |
diff --git a/base/metrics/field_trial.cc b/base/metrics/field_trial.cc |
index 45ee22d0b5d3980e6d065d0dc0144a1b865d744c..b8456b4b5c3b19eed7ede46a1b33f0771cab9e68 100644 |
--- a/base/metrics/field_trial.cc |
+++ b/base/metrics/field_trial.cc |
@@ -7,7 +7,10 @@ |
#include <algorithm> |
#include <utility> |
+#include "base/base_switches.h" |
#include "base/build_time.h" |
+#include "base/command_line.h" |
+#include "base/feature_list.h" |
#include "base/logging.h" |
#include "base/rand_util.h" |
#include "base/strings/string_number_conversions.h" |
@@ -558,6 +561,71 @@ bool FieldTrialList::CreateTrialsFromString( |
} |
// static |
+void FieldTrialList::CreateTrialsFromCommandLine( |
+ const base::CommandLine cmd_line, |
+ const char* field_trial_handle_switch) { |
+ DCHECK(global_); |
+ |
+ if (cmd_line.HasSwitch(field_trial_handle_switch)) { |
+#if defined(OS_WIN) |
Alexei Svitkine (slow)
2016/10/06 13:45:30
Ifdef should be around the if.
lawrencewu
2016/10/06 20:14:57
Done.
|
+ std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); |
+ size_t token = arg.find(","); |
+ int field_trial_handle = std::stoi(arg.substr(0, token)); |
+ int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); |
+ |
+ HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
+ base::SharedMemoryHandle shm_handle = |
+ base::SharedMemoryHandle(handle, base::GetCurrentProcId()); |
+ |
+ base::SharedMemory shared_memory(shm_handle, false); |
+ shared_memory.Map(field_trial_length); |
+ |
+ char* field_trial_state = static_cast<char*>(shared_memory.memory()); |
+ bool result = FieldTrialList::CreateTrialsFromString( |
+ std::string(field_trial_state), std::set<std::string>()); |
+ DCHECK(result); |
+#endif |
+ } |
Alexei Svitkine (slow)
2016/10/06 13:45:30
Add a return statement to the block above.
lawrencewu
2016/10/06 20:14:57
Done.
|
+ |
+ if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { |
+ bool result = FieldTrialList::CreateTrialsFromString( |
+ cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), |
+ std::set<std::string>()); |
+ DCHECK(result); |
+ } |
+} |
+ |
+// static |
+std::unique_ptr<base::SharedMemory> |
+FieldTrialList::CopyFieldTrialStateToSharedMemory( |
+ base::CommandLine* cmd_line, |
Alexei Svitkine (slow)
2016/10/06 13:45:30
Non-const params should be last.
lawrencewu
2016/10/06 20:14:57
Done.
|
+ const char* field_trial_handle_switch) { |
+ std::string field_trial_states; |
+ FieldTrialList::AllStatesToString(&field_trial_states); |
+ if (!field_trial_states.empty()) { |
+#if defined(OS_WIN) |
+ std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
+ size_t length = field_trial_states.size() + 1; |
+ shm->CreateAndMapAnonymous(length); |
+ memcpy(shm->memory(), field_trial_states.c_str(), length); |
+ |
+ // HANDLE is just typedef'd to void * |
+ auto uintptr_handle = |
+ reinterpret_cast<std::uintptr_t>(shm->handle().GetHandle()); |
+ std::string field_trial_handle = |
+ std::to_string(uintptr_handle) + "," + std::to_string(length); |
+ |
+ cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); |
+ return shm; |
+#else |
+ return std::unique_ptr<base::SharedMemory>(nullptr); |
+#endif |
+ } else { |
+ return std::unique_ptr<base::SharedMemory>(nullptr); |
+ } |
+} |
+ |
+// static |
FieldTrial* FieldTrialList::CreateFieldTrial( |
const std::string& name, |
const std::string& group_name) { |