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"
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
;
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
;
62 } else if (category_preset_string
== kConfigCategoryBenchmarkDeep
) {
63 *category_preset
= BackgroundTracingConfig::BENCHMARK_DEEP
;
65 } else if (category_preset_string
== kConfigCategoryBenchmarkGPU
) {
66 *category_preset
= BackgroundTracingConfig::BENCHMARK_GPU
;
68 } else if (category_preset_string
== kConfigCategoryBenchmarkIPC
) {
69 *category_preset
= BackgroundTracingConfig::BENCHMARK_IPC
;
76 scoped_ptr
<BackgroundTracingPreemptiveConfig
>
77 BackgroundTracingPreemptiveConfig_FromDict(const base::DictionaryValue
* dict
) {
80 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config(
81 new BackgroundTracingPreemptiveConfig());
83 std::string category_preset_string
;
84 if (!dict
->GetString(kConfigCategoryKey
, &category_preset_string
))
87 if (!StringToCategoryPreset(category_preset_string
, &config
->category_preset
))
90 const base::ListValue
* configs_list
= nullptr;
91 if (!dict
->GetList(kConfigsKey
, &configs_list
))
94 for (const auto& it
: *configs_list
) {
95 const base::DictionaryValue
* config_dict
= nullptr;
96 if (!it
->GetAsDictionary(&config_dict
))
100 if (!config_dict
->GetString(kConfigRuleKey
, &type
))
103 if (type
== kPreemptiveConfigRuleMonitorNamed
) {
104 std::string trigger_name
;
105 if (!config_dict
->GetString(kConfigRuleTriggerNameKey
, &trigger_name
))
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
))
120 if (!config_dict
->GetInteger(kConfigRuleHistogramValueKey
,
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
);
136 return config
.Pass();
139 scoped_ptr
<BackgroundTracingReactiveConfig
>
140 BackgroundTracingReactiveConfig_FromDict(const base::DictionaryValue
* dict
) {
143 scoped_ptr
<BackgroundTracingReactiveConfig
> config(
144 new BackgroundTracingReactiveConfig());
146 const base::ListValue
* configs_list
= nullptr;
147 if (!dict
->GetList(kConfigsKey
, &configs_list
))
150 for (const auto& it
: *configs_list
) {
151 const base::DictionaryValue
* config_dict
= nullptr;
152 if (!it
->GetAsDictionary(&config_dict
))
155 BackgroundTracingReactiveConfig::TracingRule rule
;
157 std::string category_preset_string
;
158 if (!config_dict
->GetString(kConfigCategoryKey
, &category_preset_string
))
161 if (!StringToCategoryPreset(category_preset_string
, &rule
.category_preset
))
165 if (!config_dict
->GetString(kConfigRuleKey
, &type
))
168 if (type
!= kReactiveConfigRuleTraceFor10sOrTriggerOrFull
)
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
))
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
);
215 configs_list
->Append(config_dict
.Pass());
218 dict
->Set(kConfigsKey
, configs_list
.Pass());
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
));
235 case BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL
:
236 config_dict
->SetString(
238 kReactiveConfigRuleTraceFor10sOrTriggerOrFull
);
245 config_dict
->SetString(kConfigRuleTriggerNameKey
, it
.trigger_name
.c_str());
246 configs_list
->Append(config_dict
.Pass());
249 dict
->Set(kConfigsKey
, configs_list
.Pass());
256 scoped_ptr
<BackgroundTracingConfig
> BackgroundTracingConfig::FromDict(
257 const base::DictionaryValue
* dict
) {
261 if (!dict
->GetString(kConfigModeKey
, &mode
))
264 scoped_ptr
<BackgroundTracingConfig
> config
;
266 if (mode
== kConfigModePreemptive
) {
267 config
= BackgroundTracingPreemptiveConfig_FromDict(dict
);
268 } else if (mode
== kConfigModeReactive
) {
269 config
= BackgroundTracingReactiveConfig_FromDict(dict
);
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
),
287 case REACTIVE_TRACING_MODE
:
288 dict
->SetString(kConfigModeKey
, kConfigModeReactive
);
289 if (!BackgroundTracingReactiveConfig_IntoDict(
290 static_cast<const BackgroundTracingReactiveConfig
*>(config
),
297 } // namspace content