Change record_wpr to setup the binary_manager. Should fix running reocrd_wpr on android.
[chromium-blink-merge.git] / content / browser / tracing / background_tracing_config_dict.cc
blobdc0cc168fd71f01ecb1f27d70c24f68e42515d18
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.
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"
10 namespace content {
12 namespace {
14 const char kConfigsKey[] = "configs";
16 const char kConfigModeKey[] = "mode";
17 const char kConfigModePreemptive[] = "PREEMPTIVE_TRACING_MODE";
18 const char kConfigModeReactive[] = "REACTIVE_TRACING_MODE";
20 const char kConfigCategoryKey[] = "category";
21 const char kConfigCategoryBenchmark[] = "BENCHMARK";
22 const char kConfigCategoryBenchmarkDeep[] = "BENCHMARK_DEEP";
23 const char kConfigCategoryBenchmarkGPU[] = "BENCHMARK_GPU";
24 const char kConfigCategoryBenchmarkIPC[] = "BENCHMARK_IPC";
26 const char kConfigRuleKey[] = "rule";
27 const char kConfigRuleTriggerNameKey[] = "trigger_name";
28 const char kConfigRuleHistogramNameKey[] = "histogram_name";
29 const char kConfigRuleHistogramValueKey[] = "histogram_value";
31 const char kPreemptiveConfigRuleMonitorNamed[] =
32 "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED";
34 const char kPreemptiveConfigRuleMonitorHistogram[] =
35 "MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE";
37 const char kReactiveConfigRuleTraceFor10sOrTriggerOrFull[] =
38 "TRACE_FOR_10S_OR_TRIGGER_OR_FULL";
40 std::string CategoryPresetToString(
41 BackgroundTracingConfig::CategoryPreset category_preset) {
42 switch (category_preset) {
43 case BackgroundTracingConfig::BENCHMARK:
44 return kConfigCategoryBenchmark;
45 case BackgroundTracingConfig::BENCHMARK_DEEP:
46 return kConfigCategoryBenchmarkDeep;
47 case BackgroundTracingConfig::BENCHMARK_GPU:
48 return kConfigCategoryBenchmarkGPU;
49 case BackgroundTracingConfig::BENCHMARK_IPC:
50 return kConfigCategoryBenchmarkIPC;
52 NOTREACHED();
53 return "";
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 } else if (category_preset_string == kConfigCategoryBenchmarkGPU) {
66 *category_preset = BackgroundTracingConfig::BENCHMARK_GPU;
67 return true;
68 } else if (category_preset_string == kConfigCategoryBenchmarkIPC) {
69 *category_preset = BackgroundTracingConfig::BENCHMARK_IPC;
70 return true;
73 return false;
76 scoped_ptr<BackgroundTracingPreemptiveConfig>
77 BackgroundTracingPreemptiveConfig_FromDict(const base::DictionaryValue* dict) {
78 DCHECK(dict);
80 scoped_ptr<BackgroundTracingPreemptiveConfig> config(
81 new BackgroundTracingPreemptiveConfig());
83 std::string category_preset_string;
84 if (!dict->GetString(kConfigCategoryKey, &category_preset_string))
85 return nullptr;
87 if (!StringToCategoryPreset(category_preset_string, &config->category_preset))
88 return nullptr;
90 const base::ListValue* configs_list = nullptr;
91 if (!dict->GetList(kConfigsKey, &configs_list))
92 return nullptr;
94 for (const auto& it : *configs_list) {
95 const base::DictionaryValue* config_dict = nullptr;
96 if (!it->GetAsDictionary(&config_dict))
97 return nullptr;
99 std::string type;
100 if (!config_dict->GetString(kConfigRuleKey, &type))
101 return nullptr;
103 if (type == kPreemptiveConfigRuleMonitorNamed) {
104 std::string trigger_name;
105 if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name))
106 return nullptr;
108 BackgroundTracingPreemptiveConfig::MonitoringRule rule;
109 rule.type = BackgroundTracingPreemptiveConfig::
110 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED;
111 rule.named_trigger_info.trigger_name = trigger_name;
113 config->configs.push_back(rule);
114 } else if (type == kPreemptiveConfigRuleMonitorHistogram) {
115 std::string histogram_name;
116 if (!config_dict->GetString(kConfigRuleHistogramNameKey, &histogram_name))
117 return nullptr;
119 int histogram_value;
120 if (!config_dict->GetInteger(kConfigRuleHistogramValueKey,
121 &histogram_value))
122 return nullptr;
124 BackgroundTracingPreemptiveConfig::MonitoringRule rule;
125 rule.type = BackgroundTracingPreemptiveConfig::
126 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE;
127 rule.histogram_trigger_info.histogram_name = histogram_name;
128 rule.histogram_trigger_info.histogram_value = histogram_value;
130 config->configs.push_back(rule);
131 } else {
132 continue;
136 return config.Pass();
139 scoped_ptr<BackgroundTracingReactiveConfig>
140 BackgroundTracingReactiveConfig_FromDict(const base::DictionaryValue* dict) {
141 DCHECK(dict);
143 scoped_ptr<BackgroundTracingReactiveConfig> config(
144 new BackgroundTracingReactiveConfig());
146 const base::ListValue* configs_list = nullptr;
147 if (!dict->GetList(kConfigsKey, &configs_list))
148 return nullptr;
150 for (const auto& it : *configs_list) {
151 const base::DictionaryValue* config_dict = nullptr;
152 if (!it->GetAsDictionary(&config_dict))
153 return nullptr;
155 BackgroundTracingReactiveConfig::TracingRule rule;
157 std::string category_preset_string;
158 if (!config_dict->GetString(kConfigCategoryKey, &category_preset_string))
159 return nullptr;
161 if (!StringToCategoryPreset(category_preset_string, &rule.category_preset))
162 return nullptr;
164 std::string type;
165 if (!config_dict->GetString(kConfigRuleKey, &type))
166 return nullptr;
168 if (type != kReactiveConfigRuleTraceFor10sOrTriggerOrFull)
169 return nullptr;
171 rule.type = BackgroundTracingReactiveConfig::
172 TRACE_FOR_10S_OR_TRIGGER_OR_FULL;
174 std::string trigger_name;
175 if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name))
176 return nullptr;
178 rule.trigger_name = trigger_name;
180 config->configs.push_back(rule);
183 return config.Pass();
186 bool BackgroundTracingPreemptiveConfig_IntoDict(
187 const BackgroundTracingPreemptiveConfig* config,
188 base::DictionaryValue* dict) {
189 dict->SetString(kConfigCategoryKey,
190 CategoryPresetToString(config->category_preset));
192 scoped_ptr<base::ListValue> configs_list(new base::ListValue());
194 for (const auto& it : config->configs) {
195 scoped_ptr<base::DictionaryValue> config_dict(new base::DictionaryValue());
197 if (it.type == BackgroundTracingPreemptiveConfig::
198 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) {
199 config_dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorNamed);
200 config_dict->SetString(kConfigRuleTriggerNameKey,
201 it.named_trigger_info.trigger_name.c_str());
202 } else if (it.type ==
203 BackgroundTracingPreemptiveConfig::
204 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE) {
205 config_dict->SetString(kConfigRuleKey,
206 kPreemptiveConfigRuleMonitorHistogram);
207 config_dict->SetString(kConfigRuleHistogramNameKey,
208 it.histogram_trigger_info.histogram_name.c_str());
209 config_dict->SetInteger(kConfigRuleHistogramValueKey,
210 it.histogram_trigger_info.histogram_value);
211 } else {
212 continue;
215 configs_list->Append(config_dict.Pass());
218 dict->Set(kConfigsKey, configs_list.Pass());
220 return true;
223 bool BackgroundTracingReactiveConfig_IntoDict(
224 const BackgroundTracingReactiveConfig* config,
225 base::DictionaryValue* dict) {
226 scoped_ptr<base::ListValue> configs_list(new base::ListValue());
228 for (const auto& it : config->configs) {
229 scoped_ptr<base::DictionaryValue> config_dict(new base::DictionaryValue());
231 config_dict->SetString(kConfigCategoryKey,
232 CategoryPresetToString(it.category_preset));
234 switch (it.type) {
235 case BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL:
236 config_dict->SetString(
237 kConfigRuleKey,
238 kReactiveConfigRuleTraceFor10sOrTriggerOrFull);
239 break;
240 default:
241 NOTREACHED();
242 continue;
245 config_dict->SetString(kConfigRuleTriggerNameKey, it.trigger_name.c_str());
246 configs_list->Append(config_dict.Pass());
249 dict->Set(kConfigsKey, configs_list.Pass());
251 return true;
254 } // namespace
256 scoped_ptr<BackgroundTracingConfig> BackgroundTracingConfig::FromDict(
257 const base::DictionaryValue* dict) {
258 DCHECK(dict);
260 std::string mode;
261 if (!dict->GetString(kConfigModeKey, &mode))
262 return nullptr;
264 scoped_ptr<BackgroundTracingConfig> config;
266 if (mode == kConfigModePreemptive) {
267 config = BackgroundTracingPreemptiveConfig_FromDict(dict);
268 } else if (mode == kConfigModeReactive) {
269 config = BackgroundTracingReactiveConfig_FromDict(dict);
270 } else {
271 return nullptr;
274 return config.Pass();
277 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config,
278 base::DictionaryValue* dict) {
279 switch (config->mode) {
280 case PREEMPTIVE_TRACING_MODE:
281 dict->SetString(kConfigModeKey, kConfigModePreemptive);
282 if (!BackgroundTracingPreemptiveConfig_IntoDict(
283 static_cast<const BackgroundTracingPreemptiveConfig*>(config),
284 dict))
285 dict->Clear();
286 break;
287 case REACTIVE_TRACING_MODE:
288 dict->SetString(kConfigModeKey, kConfigModeReactive);
289 if (!BackgroundTracingReactiveConfig_IntoDict(
290 static_cast<const BackgroundTracingReactiveConfig*>(config),
291 dict))
292 dict->Clear();
293 break;
297 } // namspace content