Chromium Code Reviews| Index: content/browser/tracing/background_tracing_config.cc |
| diff --git a/content/browser/tracing/background_tracing_config.cc b/content/browser/tracing/background_tracing_config.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3b0b5bd4fadbfa684808a79c3dc7880edb44479b |
| --- /dev/null |
| +++ b/content/browser/tracing/background_tracing_config.cc |
| @@ -0,0 +1,266 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/macros.h" |
| +#include "base/values.h" |
| +#include "content/public/browser/background_tracing_preemptive_config.h" |
| +#include "content/public/browser/background_tracing_reactive_config.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +const char kConfigsKey[] = "configs"; |
| + |
| +const char kConfigModeKey[] = "mode"; |
| +const char kConfigModePreemptive[] = "PREEMPTIVE_TRACING_MODE"; |
| +const char kConfigModeReactive[] = "REACTIVE_TRACING_MODE"; |
| + |
| +const char kConfigCategoryKey[] = "category"; |
| +const char kConfigCategoryBenchmark[] = "BENCHMARK"; |
| +const char kConfigCategoryBenchmarkDeep[] = "BENCHMARK_DEEP"; |
| + |
| +const char kConfigRuleKey[] = "rule"; |
| +const char kConfigRuleTriggerNameKey[] = "trigger_name"; |
| + |
| +const char kPreemptiveConfigRuleMonitorNamed[] = |
| + "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED"; |
| + |
| +const char kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull[] = |
| + "TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL"; |
| + |
| +} // namespace |
| + |
| +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.
|
| + BackgroundTracingConfig::CategoryPreset category_preset) { |
| + switch (category_preset) { |
| + case BackgroundTracingConfig::BENCHMARK: |
| + return kConfigCategoryBenchmark; |
| + case BackgroundTracingConfig::BENCHMARK_DEEP: |
| + return kConfigCategoryBenchmarkDeep; |
| + break; |
| + } |
| + NOTREACHED(); |
| + return ""; |
| +} |
| + |
| +bool IsStringValidCategoryPreset(const std::string& category_preset_string) { |
| + if (category_preset_string == kConfigCategoryBenchmark || |
| + category_preset_string == kConfigCategoryBenchmarkDeep) { |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +bool StringToCategoryPreset( |
| + const std::string& category_preset_string, |
| + BackgroundTracingConfig::CategoryPreset* category_preset) { |
| + if (category_preset_string == kConfigCategoryBenchmark) { |
| + *category_preset = BackgroundTracingConfig::BENCHMARK; |
| + return true; |
| + } else if (category_preset_string == kConfigCategoryBenchmarkDeep) { |
| + *category_preset = BackgroundTracingConfig::BENCHMARK_DEEP; |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +scoped_ptr<BackgroundTracingPreemptiveConfig> |
| +BackgroundTracingPreemptiveConfig_FromDict(const base::DictionaryValue* dict) { |
| + 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.
|
| + new BackgroundTracingPreemptiveConfig()); |
| + |
| + std::string category_preset_string; |
| + if (!dict->GetString(kConfigCategoryKey, &category_preset_string)) |
| + 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
|
| + |
| + if (!StringToCategoryPreset(category_preset_string, &config->category_preset)) |
| + return NULL; |
| + |
| + const base::ListValue* configs_list = NULL; |
| + if (!dict->GetList(kConfigsKey, &configs_list)) |
| + return NULL; |
| + |
| + for (auto it = configs_list->begin(); it != configs_list->end(); ++it) { |
| + const base::DictionaryValue* config_dict = NULL; |
| + if (!(*it)->GetAsDictionary(&config_dict)) |
| + return NULL; |
| + |
| + std::string type; |
| + if (!config_dict->GetString(kConfigRuleKey, &type)) |
| + return NULL; |
| + |
| + if (type == kPreemptiveConfigRuleMonitorNamed) { |
| + std::string trigger_name; |
| + if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name)) |
| + return NULL; |
| + |
| + BackgroundTracingPreemptiveConfig::MonitoringRule rule; |
| + rule.type = BackgroundTracingPreemptiveConfig:: |
| + MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED; |
| + rule.named_trigger_info.trigger_name = trigger_name; |
| + |
| + config->configs.push_back(rule); |
| + } else { |
| + // TODO(simonhatch): Implement UMA triggers. |
| + 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
|
| + } |
| + } |
| + |
| + return config.Pass(); |
| +} |
| + |
| +scoped_ptr<BackgroundTracingReactiveConfig> |
| +BackgroundTracingReactiveConfig_FromDict(const base::DictionaryValue* dict) { |
| + scoped_ptr<BackgroundTracingReactiveConfig> config( |
| + new BackgroundTracingReactiveConfig()); |
| + |
| + const base::ListValue* configs_list = NULL; |
| + if (!dict->GetList(kConfigsKey, &configs_list)) |
| + return NULL; |
| + |
| + for (auto it = configs_list->begin(); it != configs_list->end(); ++it) { |
| + const base::DictionaryValue* config_dict = NULL; |
| + if (!(*it)->GetAsDictionary(&config_dict)) |
| + return NULL; |
| + |
| + BackgroundTracingReactiveConfig::TracingRule rule; |
| + |
| + std::string category_preset_string; |
| + if (!config_dict->GetString(kConfigCategoryKey, &category_preset_string)) |
| + return NULL; |
| + |
| + if (!StringToCategoryPreset(category_preset_string, &rule.category_preset)) |
| + return NULL; |
| + |
| + std::string type; |
| + if (!config_dict->GetString(kConfigRuleKey, &type)) |
| + return NULL; |
| + |
| + if (type == kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull) { |
|
dsinclair
2015/06/01 14:29:54
if (type != kRea...)
return nullptr;
shatch
2015/06/01 17:52:26
Done.
|
| + rule.type = BackgroundTracingReactiveConfig:: |
| + TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL; |
| + } else { |
| + return NULL; |
| + } |
| + |
| + std::string trigger_name; |
| + if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name)) |
| + return NULL; |
| + |
| + rule.trigger_name = trigger_name; |
| + |
| + config->configs.push_back(rule); |
| + } |
| + |
| + return config.Pass(); |
| +} |
| + |
| +bool BackgroundTracingPreemptiveConfig_IntoDict( |
| + const BackgroundTracingPreemptiveConfig* config, |
| + base::DictionaryValue* dict) { |
| + dict->SetString(kConfigCategoryKey, |
| + CategoryPresetToString(config->category_preset)); |
| + |
| + base::ListValue* configs_list = new base::ListValue(); |
| + |
| + for (size_t i = 0; i < config->configs.size(); ++i) { |
| + base::DictionaryValue* config_dict = new base::DictionaryValue(); |
| + |
| + if (config->configs[i].type == BackgroundTracingPreemptiveConfig:: |
| + MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) { |
| + config_dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorNamed); |
| + config_dict->SetString( |
| + kConfigRuleTriggerNameKey, |
| + config->configs[i].named_trigger_info.trigger_name.c_str()); |
| + } else { |
| + // TODO(simonhatch): Implement UMA triggers. |
| + 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.
|
| + } |
| + |
| + configs_list->Append(config_dict); |
| + } |
| + |
| + dict->Set(kConfigsKey, configs_list); |
| + |
| + return true; |
| +} |
| + |
| +bool BackgroundTracingReactiveConfig_IntoDict( |
| + const BackgroundTracingReactiveConfig* config, |
| + base::DictionaryValue* dict) { |
| + scoped_ptr<base::ListValue> configs_list(new base::ListValue()); |
| + |
| + for (size_t i = 0; i < config->configs.size(); ++i) { |
| + scoped_ptr<base::DictionaryValue> config_dict(new base::DictionaryValue()); |
| + |
| + config_dict->SetString( |
| + kConfigCategoryKey, |
| + CategoryPresetToString(config->configs[i].category_preset)); |
| + |
| + switch (config->configs[i].type) { |
| + case BackgroundTracingReactiveConfig:: |
| + TRACE_ON_MANUAL_TRIGGER_UNTIL_10S_OR_NEXT_TRIGGER_OR_FULL: |
| + config_dict->SetString( |
| + kConfigRuleKey, |
| + kReactiveConfigRuleTraceOnTriggerOr10sOrTriggerOrFull); |
| + break; |
| + default: |
| + return false; |
|
dsinclair
2015/06/01 14:29:53
continue? Or NOTREACHED and continue?
shatch
2015/06/01 17:52:26
Done.
|
| + } |
| + |
| + config_dict->SetString(kConfigRuleTriggerNameKey, |
| + config->configs[i].trigger_name.c_str()); |
| + |
| + configs_list->Append(config_dict.Pass()); |
| + } |
| + |
| + dict->Set(kConfigsKey, configs_list.Pass()); |
| + |
| + return true; |
| +} |
| + |
| +scoped_ptr<BackgroundTracingConfig> BackgroundTracingConfig::FromDict( |
| + const base::DictionaryValue* dict) { |
| + DCHECK(dict); |
| + |
| + std::string mode; |
| + if (!dict->GetString(kConfigModeKey, &mode)) |
| + return NULL; |
| + |
| + scoped_ptr<BackgroundTracingConfig> config; |
| + |
| + if (mode == kConfigModePreemptive) { |
| + config = BackgroundTracingPreemptiveConfig_FromDict(dict); |
| + } else if (mode == kConfigModeReactive) { |
| + config = BackgroundTracingReactiveConfig_FromDict(dict); |
| + } else { |
| + return NULL; |
| + } |
| + |
| + return config.Pass(); |
| +} |
| + |
| +void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config, |
| + base::DictionaryValue* dict) { |
| + switch (config->mode) { |
| + case PREEMPTIVE_TRACING_MODE: |
| + dict->SetString(kConfigModeKey, kConfigModePreemptive); |
| + if (!BackgroundTracingPreemptiveConfig_IntoDict( |
| + static_cast<const BackgroundTracingPreemptiveConfig*>(config), |
| + dict)) |
| + dict->Clear(); |
| + break; |
| + case REACTIVE_TRACING_MODE: |
| + dict->SetString(kConfigModeKey, kConfigModeReactive); |
| + if (!BackgroundTracingReactiveConfig_IntoDict( |
| + static_cast<const BackgroundTracingReactiveConfig*>(config), |
| + dict)) |
| + dict->Clear(); |
| + break; |
| + } |
| +} |
| + |
| +} // namspace content |