Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/task_scheduler_util/common/variations_util.h" | 5 #include "components/task_scheduler_util/common/variations_util.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 10 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 12 #include "base/strings/string_util.h" | |
| 11 #include "base/task_scheduler/initialization_util.h" | 13 #include "base/task_scheduler/initialization_util.h" |
| 12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 13 | 15 |
| 14 namespace task_scheduler_util { | 16 namespace task_scheduler_util { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 20 constexpr char kTaskSchedulerVariationParamsSwitch[] = | |
| 21 "task-scheduler-variation-params"; | |
| 22 | |
| 23 constexpr char kSeparator[] = "|"; | |
| 24 | |
| 25 bool ContainsSeparator(const std::string& str) { | |
| 26 return str.find(kSeparator) != std::string::npos; | |
| 27 } | |
| 28 | |
| 18 struct SchedulerVariableWorkerPoolParams { | 29 struct SchedulerVariableWorkerPoolParams { |
| 19 base::SchedulerWorkerPoolParams::StandbyThreadPolicy standby_thread_policy; | 30 base::SchedulerWorkerPoolParams::StandbyThreadPolicy standby_thread_policy; |
| 20 int max_threads = 0; | 31 int max_threads = 0; |
| 21 base::TimeDelta detach_period; | 32 base::TimeDelta detach_period; |
| 22 }; | 33 }; |
| 23 | 34 |
| 24 // Converts |pool_descriptor| to a SchedulerWorkerPoolVariableParams. Returns a | 35 // Converts |pool_descriptor| to a SchedulerWorkerPoolVariableParams. Returns a |
| 25 // default SchedulerWorkerPoolVariableParams on failure. | 36 // default SchedulerWorkerPoolVariableParams on failure. |
| 26 // | 37 // |
| 27 // |pool_descriptor| is a semi-colon separated value string with the following | 38 // |pool_descriptor| is a semi-colon separated value string with the following |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 } | 109 } |
| 99 worker_pool_params_vector.emplace_back( | 110 worker_pool_params_vector.emplace_back( |
| 100 worker_pool_name, constant_worker_pool_params.priority_hint, | 111 worker_pool_name, constant_worker_pool_params.priority_hint, |
| 101 variable_worker_pool_params.standby_thread_policy, | 112 variable_worker_pool_params.standby_thread_policy, |
| 102 variable_worker_pool_params.max_threads, | 113 variable_worker_pool_params.max_threads, |
| 103 variable_worker_pool_params.detach_period); | 114 variable_worker_pool_params.detach_period); |
| 104 } | 115 } |
| 105 return worker_pool_params_vector; | 116 return worker_pool_params_vector; |
| 106 } | 117 } |
| 107 | 118 |
| 119 void AddVariationParamsToCommandLine( | |
| 120 const std::map<std::string, std::string> variation_params, | |
| 121 base::StringPiece key_prefix, | |
| 122 base::CommandLine* command_line) { | |
| 123 DCHECK(command_line); | |
| 124 | |
| 125 std::vector<std::string> parts; | |
| 126 for (const auto& key_value : variation_params) { | |
| 127 if (base::StartsWith(key_value.first, key_prefix, | |
| 128 base::CompareCase::SENSITIVE)) { | |
| 129 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
| |
| 130 ContainsSeparator(key_value.second)) { | |
| 131 DLOG(ERROR) | |
| 132 << "Unexpected Character in Task Scheduler Variation Params: " | |
| 133 << key_value.first << " [" << key_value.second << "]"; | |
| 134 return; | |
| 135 } | |
| 136 parts.push_back(key_value.first); | |
| 137 parts.push_back(key_value.second); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 if (!parts.empty()) { | |
| 142 command_line->AppendSwitchASCII(kTaskSchedulerVariationParamsSwitch, | |
| 143 base::JoinString(parts, kSeparator)); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 std::map<std::string, std::string> GetVariationParamsFromCommandLine( | |
| 148 const base::CommandLine& command_line) { | |
| 149 const auto serialized_variation_params = | |
| 150 command_line.GetSwitchValueASCII(kTaskSchedulerVariationParamsSwitch); | |
| 151 const auto parts = | |
| 152 base::SplitStringPiece(serialized_variation_params, kSeparator, | |
| 153 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 154 std::map<std::string, std::string> variation_params; | |
| 155 for (auto it = parts.begin(); it != parts.end(); ++it) { | |
| 156 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
| |
| 157 ++it; | |
| 158 if (it == parts.end()) | |
| 159 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.
| |
| 160 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
| |
| 161 variation_params[key.as_string()] = value.as_string(); | |
| 162 } | |
| 163 return variation_params; | |
| 164 } | |
| 165 | |
| 108 } // namespace task_scheduler_util | 166 } // namespace task_scheduler_util |
| OLD | NEW |