Chromium Code Reviews| Index: components/task_scheduler_util/common/variations_util.cc |
| diff --git a/components/task_scheduler_util/common/variations_util.cc b/components/task_scheduler_util/common/variations_util.cc |
| index bc072a208ce40112fa6b23f4fb761f5edaa56ecd..96119591f9bde3a097b11ae625ba4149db5ed8e2 100644 |
| --- a/components/task_scheduler_util/common/variations_util.cc |
| +++ b/components/task_scheduler_util/common/variations_util.cc |
| @@ -4,10 +4,12 @@ |
| #include "components/task_scheduler_util/common/variations_util.h" |
| +#include "base/command_line.h" |
| #include "base/logging.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_piece.h" |
| #include "base/strings/string_split.h" |
| +#include "base/strings/string_util.h" |
| #include "base/task_scheduler/initialization_util.h" |
| #include "base/time/time.h" |
| @@ -15,6 +17,15 @@ namespace task_scheduler_util { |
| namespace { |
| +constexpr char kTaskSchedulerVariationParamsSwitch[] = |
| + "task-scheduler-variation-params"; |
| + |
| +constexpr char kSeparator[] = "|"; |
| + |
| +bool ContainsSeparator(const std::string& str) { |
| + return str.find(kSeparator) != std::string::npos; |
| +} |
| + |
| struct SchedulerVariableWorkerPoolParams { |
| base::SchedulerWorkerPoolParams::StandbyThreadPolicy standby_thread_policy; |
| int max_threads = 0; |
| @@ -105,4 +116,51 @@ std::vector<base::SchedulerWorkerPoolParams> GetWorkerPoolParams( |
| return worker_pool_params_vector; |
| } |
| +void AddVariationParamsToCommandLine( |
| + const std::map<std::string, std::string> variation_params, |
| + base::StringPiece key_prefix, |
| + base::CommandLine* command_line) { |
| + DCHECK(command_line); |
| + |
| + std::vector<std::string> parts; |
| + for (const auto& key_value : variation_params) { |
| + if (base::StartsWith(key_value.first, key_prefix, |
| + base::CompareCase::SENSITIVE)) { |
| + if (ContainsSeparator(key_value.first) || |
|
gab
2016/12/12 17:33:33
Wrap in #if DCHECK_IS_ON() and NOTREACHED() instea
fdoray
2016/12/12 18:38:26
Are you sure? "Things like disk corruption or stra
gab
2016/12/13 20:00:00
But here, you're doing the encoding, right? It's n
robliao
2017/01/03 08:00:01
While we're doing the encoding, we should still be
|
| + ContainsSeparator(key_value.second)) { |
| + DLOG(ERROR) |
| + << "Unexpected Character in Task Scheduler Variation Params: " |
| + << key_value.first << " [" << key_value.second << "]"; |
| + return; |
| + } |
| + parts.push_back(key_value.first); |
| + parts.push_back(key_value.second); |
| + } |
| + } |
| + |
| + if (!parts.empty()) { |
| + command_line->AppendSwitchASCII(kTaskSchedulerVariationParamsSwitch, |
| + base::JoinString(parts, kSeparator)); |
| + } |
| +} |
| + |
| +std::map<std::string, std::string> GetVariationParamsFromCommandLine( |
| + const base::CommandLine& command_line) { |
| + const auto serialized_variation_params = |
| + command_line.GetSwitchValueASCII(kTaskSchedulerVariationParamsSwitch); |
| + const auto parts = |
| + base::SplitStringPiece(serialized_variation_params, kSeparator, |
| + base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| + std::map<std::string, std::string> variation_params; |
| + for (auto it = parts.begin(); it != parts.end(); ++it) { |
| + auto key = *it; |
|
gab
2016/12/12 17:33:33
const&
gab
2017/01/05 18:40:05
ping
fdoray
2017/01/06 17:29:03
This is a StringPiece. Changed auto -> StringPiece
|
| + ++it; |
| + if (it == parts.end()) |
| + return std::map<std::string, std::string>(); |
|
gab
2016/12/12 17:33:33
NOTREACHED()
robliao
2017/01/03 08:00:01
Command Lines aren't necessarily a safe data strea
gab
2017/01/05 18:40:05
Sure but there's something seriously wrong if this
fdoray
2017/01/06 17:29:03
Done.
|
| + auto value = *it; |
|
gab
2016/12/12 17:33:33
const&
gab
2017/01/05 18:40:05
ping
fdoray
2017/01/06 17:29:03
This is a StringPiece. Changed auto -> StringPiece
|
| + variation_params[key.as_string()] = value.as_string(); |
| + } |
| + return variation_params; |
| +} |
| + |
| } // namespace task_scheduler_util |