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_TRACING_MODE"; | |
| 18 const char kConfigModeReactive[] = "REACTIVE_TRACING_MODE"; | |
| 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 kConfigRuleTriggerNameKey[] = "trigger_name"; | |
| 26 | |
| 27 const char kPreemptiveConfigRuleMonitorNamed[] = | |
| 28 "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED"; | |
| 29 | |
| 30 const char kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull[] = | |
| 31 "TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL"; | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 std::string CategoryPresetToString( | |
|
dsinclair
2015/06/01 14:29:54
Any reason these aren't all in the anonymous names
shatch
2015/06/01 17:52:26
Done.
| |
| 36 BackgroundTracingConfig::CategoryPreset category_preset) { | |
| 37 switch (category_preset) { | |
| 38 case BackgroundTracingConfig::BENCHMARK: | |
| 39 return kConfigCategoryBenchmark; | |
| 40 case BackgroundTracingConfig::BENCHMARK_DEEP: | |
| 41 return kConfigCategoryBenchmarkDeep; | |
| 42 break; | |
| 43 } | |
| 44 NOTREACHED(); | |
| 45 return ""; | |
| 46 } | |
| 47 | |
| 48 bool IsStringValidCategoryPreset(const std::string& category_preset_string) { | |
| 49 if (category_preset_string == kConfigCategoryBenchmark || | |
| 50 category_preset_string == kConfigCategoryBenchmarkDeep) { | |
| 51 return true; | |
| 52 } | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 bool StringToCategoryPreset( | |
| 57 const std::string& category_preset_string, | |
| 58 BackgroundTracingConfig::CategoryPreset* category_preset) { | |
| 59 if (category_preset_string == kConfigCategoryBenchmark) { | |
| 60 *category_preset = BackgroundTracingConfig::BENCHMARK; | |
| 61 return true; | |
| 62 } else if (category_preset_string == kConfigCategoryBenchmarkDeep) { | |
| 63 *category_preset = BackgroundTracingConfig::BENCHMARK_DEEP; | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 scoped_ptr<BackgroundTracingPreemptiveConfig> | |
| 71 BackgroundTracingPreemptiveConfig_FromDict(const base::DictionaryValue* dict) { | |
| 72 scoped_ptr<BackgroundTracingPreemptiveConfig> config( | |
|
dsinclair
2015/06/01 14:29:54
ASSERT or DCHECK that dict is not null.
shatch
2015/06/01 17:52:26
Done.
| |
| 73 new BackgroundTracingPreemptiveConfig()); | |
| 74 | |
| 75 std::string category_preset_string; | |
| 76 if (!dict->GetString(kConfigCategoryKey, &category_preset_string)) | |
| 77 return NULL; | |
|
dsinclair
2015/06/01 14:29:53
s/NULL/nullptr/ (Does chromium use nullptr yet?) a
shatch
2015/06/01 17:52:26
Looks like this is recommended for new code: https
| |
| 78 | |
| 79 if (!StringToCategoryPreset(category_preset_string, &config->category_preset)) | |
| 80 return NULL; | |
| 81 | |
| 82 const base::ListValue* configs_list = NULL; | |
| 83 if (!dict->GetList(kConfigsKey, &configs_list)) | |
| 84 return NULL; | |
| 85 | |
| 86 for (auto it = configs_list->begin(); it != configs_list->end(); ++it) { | |
| 87 const base::DictionaryValue* config_dict = NULL; | |
| 88 if (!(*it)->GetAsDictionary(&config_dict)) | |
| 89 return NULL; | |
| 90 | |
| 91 std::string type; | |
| 92 if (!config_dict->GetString(kConfigRuleKey, &type)) | |
| 93 return NULL; | |
| 94 | |
| 95 if (type == kPreemptiveConfigRuleMonitorNamed) { | |
| 96 std::string trigger_name; | |
| 97 if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name)) | |
| 98 return NULL; | |
| 99 | |
| 100 BackgroundTracingPreemptiveConfig::MonitoringRule rule; | |
| 101 rule.type = BackgroundTracingPreemptiveConfig:: | |
| 102 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED; | |
| 103 rule.named_trigger_info.trigger_name = trigger_name; | |
| 104 | |
| 105 config->configs.push_back(rule); | |
| 106 } else { | |
| 107 // TODO(simonhatch): Implement UMA triggers. | |
| 108 return NULL; | |
|
dsinclair
2015/06/01 14:29:54
Should this be a continue just to be on the safe s
shatch
2015/06/01 17:52:26
Yeah I guess there's no reason to completely bail
| |
| 109 } | |
| 110 } | |
| 111 | |
| 112 return config.Pass(); | |
| 113 } | |
| 114 | |
| 115 scoped_ptr<BackgroundTracingReactiveConfig> | |
| 116 BackgroundTracingReactiveConfig_FromDict(const base::DictionaryValue* dict) { | |
| 117 scoped_ptr<BackgroundTracingReactiveConfig> config( | |
| 118 new BackgroundTracingReactiveConfig()); | |
| 119 | |
| 120 const base::ListValue* configs_list = NULL; | |
| 121 if (!dict->GetList(kConfigsKey, &configs_list)) | |
| 122 return NULL; | |
| 123 | |
| 124 for (auto it = configs_list->begin(); it != configs_list->end(); ++it) { | |
| 125 const base::DictionaryValue* config_dict = NULL; | |
| 126 if (!(*it)->GetAsDictionary(&config_dict)) | |
| 127 return NULL; | |
| 128 | |
| 129 BackgroundTracingReactiveConfig::TracingRule rule; | |
| 130 | |
| 131 std::string category_preset_string; | |
| 132 if (!config_dict->GetString(kConfigCategoryKey, &category_preset_string)) | |
| 133 return NULL; | |
| 134 | |
| 135 if (!StringToCategoryPreset(category_preset_string, &rule.category_preset)) | |
| 136 return NULL; | |
| 137 | |
| 138 std::string type; | |
| 139 if (!config_dict->GetString(kConfigRuleKey, &type)) | |
| 140 return NULL; | |
| 141 | |
| 142 if (type == kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull) { | |
|
dsinclair
2015/06/01 14:29:54
if (type != kRea...)
return nullptr;
shatch
2015/06/01 17:52:26
Done.
| |
| 143 rule.type = BackgroundTracingReactiveConfig:: | |
| 144 TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL; | |
| 145 } else { | |
| 146 return NULL; | |
| 147 } | |
| 148 | |
| 149 std::string trigger_name; | |
| 150 if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name)) | |
| 151 return NULL; | |
| 152 | |
| 153 rule.trigger_name = trigger_name; | |
| 154 | |
| 155 config->configs.push_back(rule); | |
| 156 } | |
| 157 | |
| 158 return config.Pass(); | |
| 159 } | |
| 160 | |
| 161 bool BackgroundTracingPreemptiveConfig_IntoDict( | |
| 162 const BackgroundTracingPreemptiveConfig* config, | |
| 163 base::DictionaryValue* dict) { | |
| 164 dict->SetString(kConfigCategoryKey, | |
| 165 CategoryPresetToString(config->category_preset)); | |
| 166 | |
| 167 base::ListValue* configs_list = new base::ListValue(); | |
| 168 | |
| 169 for (size_t i = 0; i < config->configs.size(); ++i) { | |
| 170 base::DictionaryValue* config_dict = new base::DictionaryValue(); | |
| 171 | |
| 172 if (config->configs[i].type == BackgroundTracingPreemptiveConfig:: | |
| 173 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) { | |
| 174 config_dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorNamed); | |
| 175 config_dict->SetString( | |
| 176 kConfigRuleTriggerNameKey, | |
| 177 config->configs[i].named_trigger_info.trigger_name.c_str()); | |
| 178 } else { | |
| 179 // TODO(simonhatch): Implement UMA triggers. | |
| 180 return false; | |
|
dsinclair
2015/06/01 14:29:53
Should this be a continue? Right now, if one of th
shatch
2015/06/01 17:52:26
Ya true, done.
| |
| 181 } | |
| 182 | |
| 183 configs_list->Append(config_dict); | |
| 184 } | |
| 185 | |
| 186 dict->Set(kConfigsKey, configs_list); | |
| 187 | |
| 188 return true; | |
| 189 } | |
| 190 | |
| 191 bool BackgroundTracingReactiveConfig_IntoDict( | |
| 192 const BackgroundTracingReactiveConfig* config, | |
| 193 base::DictionaryValue* dict) { | |
| 194 scoped_ptr<base::ListValue> configs_list(new base::ListValue()); | |
| 195 | |
| 196 for (size_t i = 0; i < config->configs.size(); ++i) { | |
| 197 scoped_ptr<base::DictionaryValue> config_dict(new base::DictionaryValue()); | |
| 198 | |
| 199 config_dict->SetString( | |
| 200 kConfigCategoryKey, | |
| 201 CategoryPresetToString(config->configs[i].category_preset)); | |
| 202 | |
| 203 switch (config->configs[i].type) { | |
| 204 case BackgroundTracingReactiveConfig:: | |
| 205 TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL: | |
| 206 config_dict->SetString( | |
| 207 kConfigRuleKey, | |
| 208 kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull); | |
| 209 break; | |
| 210 default: | |
| 211 return false; | |
|
dsinclair
2015/06/01 14:29:53
continue? Or NOTREACHED and continue?
shatch
2015/06/01 17:52:26
Done.
| |
| 212 } | |
| 213 | |
| 214 config_dict->SetString(kConfigRuleTriggerNameKey, | |
| 215 config->configs[i].trigger_name.c_str()); | |
| 216 | |
| 217 configs_list->Append(config_dict.Pass()); | |
| 218 } | |
| 219 | |
| 220 dict->Set(kConfigsKey, configs_list.Pass()); | |
| 221 | |
| 222 return true; | |
| 223 } | |
| 224 | |
| 225 scoped_ptr<BackgroundTracingConfig> BackgroundTracingConfig::FromDict( | |
| 226 const base::DictionaryValue* dict) { | |
| 227 DCHECK(dict); | |
| 228 | |
| 229 std::string mode; | |
| 230 if (!dict->GetString(kConfigModeKey, &mode)) | |
| 231 return NULL; | |
| 232 | |
| 233 scoped_ptr<BackgroundTracingConfig> config; | |
| 234 | |
| 235 if (mode == kConfigModePreemptive) { | |
| 236 config = BackgroundTracingPreemptiveConfig_FromDict(dict); | |
| 237 } else if (mode == kConfigModeReactive) { | |
| 238 config = BackgroundTracingReactiveConfig_FromDict(dict); | |
| 239 } else { | |
| 240 return NULL; | |
| 241 } | |
| 242 | |
| 243 return config.Pass(); | |
| 244 } | |
| 245 | |
| 246 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config, | |
| 247 base::DictionaryValue* dict) { | |
| 248 switch (config->mode) { | |
| 249 case PREEMPTIVE_TRACING_MODE: | |
| 250 dict->SetString(kConfigModeKey, kConfigModePreemptive); | |
| 251 if (!BackgroundTracingPreemptiveConfig_IntoDict( | |
| 252 static_cast<const BackgroundTracingPreemptiveConfig*>(config), | |
| 253 dict)) | |
| 254 dict->Clear(); | |
| 255 break; | |
| 256 case REACTIVE_TRACING_MODE: | |
| 257 dict->SetString(kConfigModeKey, kConfigModeReactive); | |
| 258 if (!BackgroundTracingReactiveConfig_IntoDict( | |
| 259 static_cast<const BackgroundTracingReactiveConfig*>(config), | |
| 260 dict)) | |
| 261 dict->Clear(); | |
| 262 break; | |
| 263 } | |
| 264 } | |
| 265 | |
| 266 } // namspace content | |
| OLD | NEW |