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 "content/public/browser/background_tracing_manager.h" |
| 6 |
| 7 namespace content { |
| 8 |
| 9 namespace { |
| 10 |
| 11 const char kConfigsKey[] = "configs"; |
| 12 |
| 13 const char kConfigModeKey[] = "mode"; |
| 14 const char kConfigModePreemptive[] = "preemptive"; |
| 15 const char kConfigModeReactive[] = "reactive"; |
| 16 |
| 17 const char kConfigCategoryKey[] = "category"; |
| 18 const char kConfigCategoryBenchmark[] = "benchmark"; |
| 19 const char kConfigCategoryBenchmarkDeep[] = "benchmark-deep"; |
| 20 |
| 21 const char kConfigRuleKey[] = "rule"; |
| 22 const char kConfigRuleMonitorNamed[] = "monitor_named"; |
| 23 const char kConfigRuleTriggerNameKey[] = "trigger_name"; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 BackgroundTracingConfig::BackgroundTracingConfig(Mode mode) : mode_(mode) { |
| 28 } |
| 29 |
| 30 BackgroundTracingConfig::~BackgroundTracingConfig() { |
| 31 } |
| 32 |
| 33 BackgroundTracingConfig::Mode BackgroundTracingConfig::mode() const { |
| 34 return mode_; |
| 35 } |
| 36 |
| 37 scoped_refptr<BackgroundTracingConfig> BackgroundTracingConfig::FromDict( |
| 38 const base::DictionaryValue* dict) { |
| 39 std::string mode; |
| 40 if (!dict->GetString(kConfigModeKey, &mode)) |
| 41 return NULL; |
| 42 |
| 43 scoped_refptr<BackgroundTracingConfig> config = NULL; |
| 44 |
| 45 if (mode == kConfigModePreemptive) { |
| 46 config = BackgroundTracingPreemptiveConfig::FromDict(dict); |
| 47 } else if (mode == kConfigModeReactive) { |
| 48 config = BackgroundTracingReactiveConfig::FromDict(dict); |
| 49 } else { |
| 50 CHECK(false); |
| 51 } |
| 52 |
| 53 return config; |
| 54 } |
| 55 |
| 56 void BackgroundTracingConfig::IntoDict(base::DictionaryValue* dict) { |
| 57 CHECK(dict); |
| 58 |
| 59 switch (mode_) { |
| 60 case PREEMPTIVE_TRACING_MODE: |
| 61 dict->SetString(kConfigModeKey, kConfigModePreemptive); |
| 62 static_cast<BackgroundTracingPreemptiveConfig*>(this)->AsDict(dict); |
| 63 break; |
| 64 case REACTIVE_TRACING_MODE: |
| 65 dict->SetString(kConfigModeKey, kConfigModeReactive); |
| 66 static_cast<BackgroundTracingPreemptiveConfig*>(this)->AsDict(dict); |
| 67 break; |
| 68 } |
| 69 } |
| 70 |
| 71 BackgroundTracingPreemptiveConfig::BackgroundTracingPreemptiveConfig() |
| 72 : BackgroundTracingConfig(BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE), |
| 73 category_preset_(BackgroundTracingConfig::BENCHMARK) { |
| 74 } |
| 75 |
| 76 BackgroundTracingPreemptiveConfig::~BackgroundTracingPreemptiveConfig() { |
| 77 } |
| 78 |
| 79 void BackgroundTracingPreemptiveConfig::AddHistogramTriggerRule( |
| 80 const std::string& histogram_name, |
| 81 int histogram_bin) { |
| 82 MonitoringRule rule; |
| 83 rule.type = MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE; |
| 84 rule.histogram_trigger_info.histogram_name_to_trigger_on = histogram_name; |
| 85 rule.histogram_trigger_info.histogram_bin_to_trigger_on = histogram_bin; |
| 86 configs_.push_back(rule); |
| 87 } |
| 88 |
| 89 void BackgroundTracingPreemptiveConfig::AddNamedTriggerRule( |
| 90 const std::string& name) { |
| 91 MonitoringRule rule; |
| 92 rule.type = MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED; |
| 93 rule.named_trigger_info.trigger_name = name; |
| 94 configs_.push_back(rule); |
| 95 } |
| 96 |
| 97 BackgroundTracingConfig::CategoryPreset |
| 98 BackgroundTracingPreemptiveConfig::category_preset() const { |
| 99 return category_preset_; |
| 100 } |
| 101 |
| 102 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>& |
| 103 BackgroundTracingPreemptiveConfig::configs() const { |
| 104 return configs_; |
| 105 } |
| 106 |
| 107 scoped_refptr<BackgroundTracingPreemptiveConfig> |
| 108 BackgroundTracingPreemptiveConfig::FromDict(const base::DictionaryValue* dict) { |
| 109 scoped_refptr<BackgroundTracingPreemptiveConfig> config( |
| 110 new BackgroundTracingPreemptiveConfig()); |
| 111 |
| 112 std::string category_preset; |
| 113 if (!dict->GetString(kConfigCategoryKey, &category_preset)) |
| 114 return NULL; |
| 115 |
| 116 if (category_preset == kConfigCategoryBenchmark) { |
| 117 config->category_preset_ = BENCHMARK; |
| 118 } else if (category_preset == kConfigCategoryBenchmarkDeep) { |
| 119 config->category_preset_ = BENCHMARK_DEEP; |
| 120 } else { |
| 121 CHECK(false); |
| 122 } |
| 123 |
| 124 const base::ListValue* configs_list = NULL; |
| 125 if (!dict->GetList(kConfigsKey, &configs_list)) |
| 126 return NULL; |
| 127 |
| 128 for (auto it = configs_list->begin(); it != configs_list->end(); ++it) { |
| 129 const base::DictionaryValue* config_dict = NULL; |
| 130 if (!(*it)->GetAsDictionary(&config_dict)) |
| 131 return NULL; |
| 132 |
| 133 std::string type; |
| 134 if (!config_dict->GetString(kConfigRuleKey, &type)) |
| 135 return NULL; |
| 136 |
| 137 if (type == kConfigRuleMonitorNamed) { |
| 138 std::string trigger_name; |
| 139 if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name)) |
| 140 return NULL; |
| 141 |
| 142 config->AddNamedTriggerRule(trigger_name); |
| 143 } else { |
| 144 CHECK(false); |
| 145 } |
| 146 } |
| 147 |
| 148 return config.Pass(); |
| 149 } |
| 150 |
| 151 void BackgroundTracingPreemptiveConfig::AsDict(base::DictionaryValue* dict) { |
| 152 // TODO: Move to function. |
| 153 switch (category_preset_) { |
| 154 case BENCHMARK: |
| 155 dict->SetString(kConfigCategoryKey, kConfigCategoryBenchmark); |
| 156 break; |
| 157 case BENCHMARK_DEEP: |
| 158 dict->SetString(kConfigCategoryKey, kConfigCategoryBenchmarkDeep); |
| 159 break; |
| 160 } |
| 161 |
| 162 base::ListValue* configs_list = new base::ListValue(); |
| 163 |
| 164 for (size_t i = 0; i < configs_.size(); ++i) { |
| 165 base::DictionaryValue* config_dict = new base::DictionaryValue(); |
| 166 |
| 167 if (configs_[i].type == MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) { |
| 168 config_dict->SetString(kConfigRuleKey, kConfigRuleMonitorNamed); |
| 169 config_dict->SetString( |
| 170 kConfigRuleTriggerNameKey, |
| 171 configs_[i].named_trigger_info.trigger_name.c_str()); |
| 172 } |
| 173 |
| 174 configs_list->Append(config_dict); |
| 175 } |
| 176 |
| 177 dict->Set(kConfigsKey, configs_list); |
| 178 } |
| 179 |
| 180 BackgroundTracingReactiveConfig::BackgroundTracingReactiveConfig() |
| 181 : BackgroundTracingConfig(BackgroundTracingConfig::REACTIVE_TRACING_MODE) { |
| 182 } |
| 183 |
| 184 BackgroundTracingReactiveConfig::~BackgroundTracingReactiveConfig() { |
| 185 } |
| 186 |
| 187 void BackgroundTracingReactiveConfig::AddTracingRule( |
| 188 RuleType rule_type, |
| 189 const char* trigger_name, |
| 190 CategoryPreset category_preset) { |
| 191 // TODO(simonhatch): Implement this. |
| 192 CHECK(false); |
| 193 } |
| 194 |
| 195 scoped_refptr<BackgroundTracingReactiveConfig> |
| 196 BackgroundTracingReactiveConfig::FromDict(const base::DictionaryValue* dict) { |
| 197 // TODO(simonhatch): Implement this. |
| 198 CHECK(false); |
| 199 return NULL; |
| 200 } |
| 201 |
| 202 void BackgroundTracingReactiveConfig::AsDict(base::DictionaryValue* dict) { |
| 203 // TODO(simonhatch): Implement this. |
| 204 CHECK(false); |
| 205 } |
| 206 } |
| 207 // namespace content |
OLD | NEW |