Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/macros.h" | |
| 6 #include "base/values.h" | |
| 7 #include "content/public/browser/background_tracing_preemptive_config.h" | |
| 8 #include "content/public/browser/background_tracing_reactive_config.h" | |
| 9 | |
| 10 namespace content { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 const char kConfigsKey[] = "configs"; | |
| 15 | |
| 16 const char kConfigModeKey[] = "mode"; | |
| 17 const char kConfigModePreemptive[] = "preemptive"; | |
| 18 const char kConfigModeReactive[] = "reactive"; | |
| 19 | |
| 20 const char kConfigCategoryKey[] = "category"; | |
| 21 const char kConfigCategoryBenchmark[] = "benchmark"; | |
| 22 const char kConfigCategoryBenchmarkDeep[] = "benchmark-deep"; | |
| 23 | |
| 24 const char kConfigRuleKey[] = "rule"; | |
| 25 const char kConfigRuleMonitorNamed[] = "monitor_named"; | |
| 26 const char kConfigRuleTriggerNameKey[] = "trigger_name"; | |
| 27 const char kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull[] = | |
| 28 "trace_on_trigger_or_10s_or_trigger_or_full"; | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
Shouldn't this be trace_on_trigger_until_10s_or_tr
shatch
2015/05/28 19:17:15
Ya was trying to shorten it since it was so long,
| |
| 29 | |
| 30 const char kReactiveConfigTriggerNameKey[] = "trigger_name"; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 std::string CategoryPresetToString( | |
| 35 BackgroundTracingConfig::CategoryPreset category_preset) { | |
| 36 switch (category_preset) { | |
| 37 case BackgroundTracingConfig::BENCHMARK: | |
| 38 return kConfigCategoryBenchmark; | |
| 39 case BackgroundTracingConfig::BENCHMARK_DEEP: | |
| 40 return kConfigCategoryBenchmarkDeep; | |
| 41 break; | |
| 42 } | |
| 43 CHECK(false); | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
NOTREACHED()?
shatch
2015/05/28 19:17:15
Done.
| |
| 44 return ""; | |
| 45 } | |
| 46 | |
| 47 bool IsStringValidCategoryPreset(const std::string& category_preset_string) { | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
Feels like IsStringValidCategoryPreset() and Strin
shatch
2015/05/28 19:17:14
Done.
| |
| 48 if (category_preset_string == kConfigCategoryBenchmark || | |
| 49 category_preset_string == kConfigCategoryBenchmarkDeep) { | |
| 50 return true; | |
| 51 } | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 BackgroundTracingConfig::CategoryPreset StringToCategoryPreset( | |
| 56 const std::string& category_preset_string) { | |
| 57 if (category_preset_string == kConfigCategoryBenchmark) { | |
| 58 return BackgroundTracingConfig::BENCHMARK; | |
| 59 } else if (category_preset_string == kConfigCategoryBenchmarkDeep) { | |
| 60 return BackgroundTracingConfig::BENCHMARK_DEEP; | |
| 61 } | |
| 62 | |
| 63 CHECK(false); | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
NOTREACHED()?
shatch
2015/05/28 19:17:15
Done.
| |
| 64 return BackgroundTracingConfig::BENCHMARK; | |
| 65 } | |
| 66 | |
| 67 BackgroundTracingReactiveConfig::RuleType StringToReactiveConfigRuleType( | |
| 68 const std::string& rule_type) { | |
| 69 if (rule_type == kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull) { | |
| 70 return BackgroundTracingReactiveConfig:: | |
| 71 TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL; | |
| 72 } | |
| 73 CHECK(false); | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
NOTREACHED()?
shatch
2015/05/28 19:17:15
Done.
| |
| 74 return BackgroundTracingReactiveConfig:: | |
| 75 TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL; | |
| 76 } | |
| 77 | |
| 78 scoped_ptr<BackgroundTracingPreemptiveConfig> | |
| 79 BackgroundTracingPreemptiveConfig_FromDict(const base::DictionaryValue* dict) { | |
| 80 scoped_ptr<BackgroundTracingPreemptiveConfig> config( | |
| 81 new BackgroundTracingPreemptiveConfig()); | |
| 82 | |
| 83 std::string category_preset_string; | |
| 84 if (!dict->GetString(kConfigCategoryKey, &category_preset_string)) | |
| 85 return NULL; | |
| 86 | |
| 87 if (!IsStringValidCategoryPreset(category_preset_string)) | |
| 88 return NULL; | |
| 89 | |
| 90 config->category_preset = StringToCategoryPreset(category_preset_string); | |
| 91 | |
| 92 const base::ListValue* configs_list = NULL; | |
| 93 if (!dict->GetList(kConfigsKey, &configs_list)) | |
| 94 return NULL; | |
| 95 | |
| 96 for (auto it = configs_list->begin(); it != configs_list->end(); ++it) { | |
| 97 const base::DictionaryValue* config_dict = NULL; | |
| 98 if (!(*it)->GetAsDictionary(&config_dict)) | |
| 99 return NULL; | |
| 100 | |
| 101 std::string type; | |
| 102 if (!config_dict->GetString(kConfigRuleKey, &type)) | |
| 103 return NULL; | |
| 104 | |
| 105 if (type == kConfigRuleMonitorNamed) { | |
| 106 std::string trigger_name; | |
| 107 if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name)) | |
| 108 return NULL; | |
| 109 | |
| 110 BackgroundTracingPreemptiveConfig::MonitoringRule rule; | |
| 111 rule.type = BackgroundTracingPreemptiveConfig:: | |
| 112 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED; | |
| 113 rule.named_trigger_info.trigger_name = trigger_name; | |
| 114 | |
| 115 config->configs.push_back(rule); | |
| 116 } else { | |
| 117 // TODO(simonhatch): Implement UMA triggers. | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
Brainstorming for later CLs: Let's add UMAs that c
| |
| 118 return NULL; | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 return config.Pass(); | |
| 123 } | |
| 124 | |
| 125 scoped_ptr<BackgroundTracingReactiveConfig> | |
| 126 BackgroundTracingReactiveConfig_FromDict(const base::DictionaryValue* dict) { | |
| 127 scoped_ptr<BackgroundTracingReactiveConfig> config( | |
| 128 new BackgroundTracingReactiveConfig()); | |
| 129 | |
| 130 const base::ListValue* configs_list = NULL; | |
| 131 if (!dict->GetList(kConfigsKey, &configs_list)) | |
| 132 return NULL; | |
| 133 | |
| 134 for (auto it = configs_list->begin(); it != configs_list->end(); ++it) { | |
| 135 const base::DictionaryValue* config_dict = NULL; | |
| 136 if (!(*it)->GetAsDictionary(&config_dict)) | |
| 137 return NULL; | |
| 138 | |
| 139 BackgroundTracingReactiveConfig::TracingRule rule; | |
| 140 | |
| 141 std::string category_preset_string; | |
| 142 if (!config_dict->GetString(kConfigCategoryKey, &category_preset_string)) | |
| 143 return NULL; | |
| 144 | |
| 145 if (!IsStringValidCategoryPreset(category_preset_string)) | |
| 146 return NULL; | |
| 147 | |
| 148 rule.category_preset = StringToCategoryPreset(category_preset_string); | |
| 149 | |
| 150 std::string type; | |
| 151 if (!config_dict->GetString(kConfigRuleKey, &type)) | |
| 152 return NULL; | |
| 153 | |
| 154 rule.type = StringToReactiveConfigRuleType(type); | |
| 155 | |
| 156 std::string trigger_name; | |
| 157 if (!config_dict->GetString(kReactiveConfigTriggerNameKey, &trigger_name)) | |
| 158 return NULL; | |
| 159 | |
| 160 rule.trigger_name = trigger_name; | |
| 161 | |
| 162 config->configs.push_back(rule); | |
| 163 } | |
| 164 | |
| 165 return config.Pass(); | |
| 166 } | |
| 167 | |
| 168 bool BackgroundTracingPreemptiveConfig_IntoDict( | |
| 169 const BackgroundTracingPreemptiveConfig* config, | |
| 170 base::DictionaryValue* dict) { | |
| 171 dict->SetString(kConfigCategoryKey, | |
| 172 CategoryPresetToString(config->category_preset)); | |
| 173 | |
| 174 base::ListValue* configs_list = new base::ListValue(); | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:47
Is configs_list and config_dict leaking if we retu
shatch
2015/05/28 19:17:15
Done.
| |
| 175 | |
| 176 for (size_t i = 0; i < config->configs.size(); ++i) { | |
| 177 base::DictionaryValue* config_dict = new base::DictionaryValue(); | |
| 178 | |
| 179 if (config->configs[i].type == BackgroundTracingPreemptiveConfig:: | |
| 180 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) { | |
| 181 config_dict->SetString(kConfigRuleKey, kConfigRuleMonitorNamed); | |
| 182 config_dict->SetString( | |
| 183 kConfigRuleTriggerNameKey, | |
| 184 config->configs[i].named_trigger_info.trigger_name.c_str()); | |
| 185 } else { | |
| 186 // TODO(simonhatch): Implement UMA triggers. | |
| 187 return false; | |
| 188 } | |
| 189 | |
| 190 configs_list->Append(config_dict); | |
| 191 } | |
| 192 | |
| 193 dict->Set(kConfigsKey, configs_list); | |
| 194 | |
| 195 return true; | |
| 196 } | |
| 197 | |
| 198 bool BackgroundTracingReactiveConfig_IntoDict( | |
| 199 const BackgroundTracingReactiveConfig* config, | |
| 200 base::DictionaryValue* dict) { | |
| 201 base::ListValue* configs_list = new base::ListValue(); | |
| 202 | |
| 203 for (size_t i = 0; i < config->configs.size(); ++i) { | |
| 204 base::DictionaryValue* config_dict = new base::DictionaryValue(); | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
Same leak comment as above
shatch
2015/05/28 19:17:15
Done.
| |
| 205 | |
| 206 config_dict->SetString( | |
| 207 kConfigCategoryKey, | |
| 208 CategoryPresetToString(config->configs[i].category_preset)); | |
| 209 | |
| 210 switch (config->configs[i].type) { | |
| 211 case BackgroundTracingReactiveConfig:: | |
| 212 TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL: | |
| 213 config_dict->SetString( | |
| 214 kConfigRuleKey, | |
| 215 kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull); | |
| 216 break; | |
| 217 default: | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
Do we need the default?
shatch
2015/05/28 19:17:15
Yeah to error out for any enums we don't support y
| |
| 218 return false; | |
| 219 } | |
| 220 | |
| 221 config_dict->SetString(kConfigRuleTriggerNameKey, | |
| 222 config->configs[i].trigger_name.c_str()); | |
| 223 | |
| 224 configs_list->Append(config_dict); | |
| 225 } | |
| 226 | |
| 227 dict->Set(kConfigsKey, configs_list); | |
| 228 | |
| 229 return true; | |
| 230 } | |
| 231 | |
| 232 scoped_ptr<BackgroundTracingConfig> BackgroundTracingConfig::FromDict( | |
| 233 const base::DictionaryValue* dict) { | |
| 234 if (!dict) | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
Can this ever really be NULL? Feels like it should
shatch
2015/05/28 19:17:15
Done.
| |
| 235 return NULL; | |
| 236 | |
| 237 std::string mode; | |
| 238 if (!dict->GetString(kConfigModeKey, &mode)) | |
| 239 return NULL; | |
| 240 | |
| 241 scoped_ptr<BackgroundTracingConfig> config; | |
| 242 | |
| 243 if (mode == kConfigModePreemptive) { | |
| 244 config = BackgroundTracingPreemptiveConfig_FromDict(dict); | |
| 245 } else if (mode == kConfigModeReactive) { | |
| 246 config = BackgroundTracingReactiveConfig_FromDict(dict); | |
| 247 } else { | |
| 248 return NULL; | |
| 249 } | |
| 250 | |
| 251 return config.Pass(); | |
| 252 } | |
| 253 | |
| 254 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config, | |
| 255 base::DictionaryValue* dict) { | |
| 256 switch (config->mode) { | |
|
oystein (OOO til 10th of July)
2015/05/28 17:00:46
Switching on this and then static_casting() makes
shatch
2015/05/28 19:17:15
Chatted offline, maybe we can clean this up with a
| |
| 257 case PREEMPTIVE_TRACING_MODE: | |
| 258 dict->SetString(kConfigModeKey, kConfigModePreemptive); | |
| 259 if (!BackgroundTracingPreemptiveConfig_IntoDict( | |
| 260 static_cast<const BackgroundTracingPreemptiveConfig*>(config), | |
| 261 dict)) | |
| 262 dict->Clear(); | |
| 263 break; | |
| 264 case REACTIVE_TRACING_MODE: | |
| 265 dict->SetString(kConfigModeKey, kConfigModeReactive); | |
| 266 if (!BackgroundTracingReactiveConfig_IntoDict( | |
| 267 static_cast<const BackgroundTracingReactiveConfig*>(config), | |
| 268 dict)) | |
| 269 dict->Clear(); | |
| 270 break; | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 } // namspace content | |
| OLD | NEW |