Chromium Code Reviews| Index: base/metrics/field_trial.cc |
| diff --git a/base/metrics/field_trial.cc b/base/metrics/field_trial.cc |
| index 45ee22d0b5d3980e6d065d0dc0144a1b865d744c..66f645cf0cc41e86d97a4f1d09184f201f7ea57c 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" |
| @@ -28,6 +31,10 @@ const char kPersistentStringSeparator = '/'; // Currently a slash. |
| // command line which forces its activation. |
| const char kActivationMarker = '*'; |
| +// Use shared memory to communicate field trial (experiment) state. |
| +const base::Feature kShareFieldTrialStateViaSharedMemory{ |
| + "ShareFieldTrialStateViaSharedMemory", base::FEATURE_DISABLED_BY_DEFAULT}; |
|
Alexei Svitkine (slow)
2016/10/06 21:39:28
Nit: How about "UseSharedMemoryForFieldTrials" - a
lawrencewu
2016/10/07 15:07:21
Done.
|
| + |
| // Created a time value based on |year|, |month| and |day_of_month| parameters. |
| Time CreateTimeFromParams(int year, int month, int day_of_month) { |
| DCHECK_GT(year, 1970); |
| @@ -558,6 +565,78 @@ bool FieldTrialList::CreateTrialsFromString( |
| } |
| // static |
| +void FieldTrialList::CreateTrialsFromCommandLine( |
| + const base::CommandLine& cmd_line, |
| + const char* field_trial_handle_switch) { |
| + DCHECK(global_); |
| + |
| +#if defined(OS_WIN) |
| + if (cmd_line.HasSwitch(field_trial_handle_switch)) { |
| + 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())); |
|
Alexei Svitkine (slow)
2016/10/06 21:39:28
Do you need to do any kind of error handling here?
lawrencewu
2016/10/07 15:07:21
I thought about it, and I think it is unnecessary
|
| + |
| + HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
| + base::SharedMemoryHandle shm_handle = |
| + base::SharedMemoryHandle(handle, base::GetCurrentProcId()); |
| + |
| + base::SharedMemory shared_memory(shm_handle, false); |
|
Alexei Svitkine (slow)
2016/10/06 21:39:28
Add a comment that this will be deleted when this
lawrencewu
2016/10/07 15:07:21
Done.
|
| + 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); |
| + return; |
| + } |
| +#endif |
| + |
| + 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::CopyFieldTrialStateToFlags( |
| + const char* field_trial_handle_switch, |
| + base::CommandLine* cmd_line) { |
| + std::string field_trial_states; |
| + base::FieldTrialList::AllStatesToString(&field_trial_states); |
| + if (!field_trial_states.empty()) { |
| + // Use shared memory to pass the state if the feature is enabled, otherwise |
| + // fallback to passing it via the command line as a string. |
| + if (base::FeatureList::IsEnabled(kShareFieldTrialStateViaSharedMemory)) { |
| +#if defined(OS_WIN) |
|
Alexei Svitkine (slow)
2016/10/06 21:39:28
Put the whole if under ifdef and remove the else -
lawrencewu
2016/10/07 15:07:21
Done.
|
| + 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); |
|
Alexei Svitkine (slow)
2016/10/06 21:39:28
Is it guaranteed to be null terminated? If not, pl
lawrencewu
2016/10/07 15:07:21
c_str() is guaranteed to be null terminated: http:
|
| + |
| + // 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 { |
| + cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, |
| + field_trial_states); |
| + } |
| + } |
| + return std::unique_ptr<base::SharedMemory>(nullptr); |
| +} |
| + |
| +// static |
| FieldTrial* FieldTrialList::CreateFieldTrial( |
| const std::string& name, |
| const std::string& group_name) { |