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