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";
24 const char kConfigRuleKey
[] = "rule";
25 const char kConfigRuleTriggerNameKey
[] = "trigger_name";
26 const char kConfigRuleHistogramNameKey
[] = "histogram_name";
27 const char kConfigRuleHistogramValueKey
[] = "histogram_value";
29 const char kPreemptiveConfigRuleMonitorNamed
[] =
30 "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED";
32 const char kPreemptiveConfigRuleMonitorHistogram
[] =
33 "MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE";
35 const char kReactiveConfigRuleTraceFor10sOrTriggerOrFull
[] =
36 "TRACE_FOR_10S_OR_TRIGGER_OR_FULL";
38 std::string
CategoryPresetToString(
39 BackgroundTracingConfig::CategoryPreset category_preset
) {
40 switch (category_preset
) {
41 case BackgroundTracingConfig::BENCHMARK
:
42 return kConfigCategoryBenchmark
;
43 case BackgroundTracingConfig::BENCHMARK_DEEP
:
44 return kConfigCategoryBenchmarkDeep
;
51 bool StringToCategoryPreset(
52 const std::string
& category_preset_string
,
53 BackgroundTracingConfig::CategoryPreset
* category_preset
) {
54 if (category_preset_string
== kConfigCategoryBenchmark
) {
55 *category_preset
= BackgroundTracingConfig::BENCHMARK
;
57 } else if (category_preset_string
== kConfigCategoryBenchmarkDeep
) {
58 *category_preset
= BackgroundTracingConfig::BENCHMARK_DEEP
;
65 scoped_ptr
<BackgroundTracingPreemptiveConfig
>
66 BackgroundTracingPreemptiveConfig_FromDict(const base::DictionaryValue
* dict
) {
69 scoped_ptr
<BackgroundTracingPreemptiveConfig
> config(
70 new BackgroundTracingPreemptiveConfig());
72 std::string category_preset_string
;
73 if (!dict
->GetString(kConfigCategoryKey
, &category_preset_string
))
76 if (!StringToCategoryPreset(category_preset_string
, &config
->category_preset
))
79 const base::ListValue
* configs_list
= nullptr;
80 if (!dict
->GetList(kConfigsKey
, &configs_list
))
83 for (auto it
= configs_list
->begin(); it
!= configs_list
->end(); ++it
) {
84 const base::DictionaryValue
* config_dict
= nullptr;
85 if (!(*it
)->GetAsDictionary(&config_dict
))
89 if (!config_dict
->GetString(kConfigRuleKey
, &type
))
92 if (type
== kPreemptiveConfigRuleMonitorNamed
) {
93 std::string trigger_name
;
94 if (!config_dict
->GetString(kConfigRuleTriggerNameKey
, &trigger_name
))
97 BackgroundTracingPreemptiveConfig::MonitoringRule rule
;
98 rule
.type
= BackgroundTracingPreemptiveConfig::
99 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
;
100 rule
.named_trigger_info
.trigger_name
= trigger_name
;
102 config
->configs
.push_back(rule
);
103 } else if (type
== kPreemptiveConfigRuleMonitorHistogram
) {
104 std::string histogram_name
;
105 if (!config_dict
->GetString(kConfigRuleHistogramNameKey
, &histogram_name
))
109 if (!config_dict
->GetInteger(kConfigRuleHistogramValueKey
,
113 BackgroundTracingPreemptiveConfig::MonitoringRule rule
;
114 rule
.type
= BackgroundTracingPreemptiveConfig::
115 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE
;
116 rule
.histogram_trigger_info
.histogram_name
= histogram_name
;
117 rule
.histogram_trigger_info
.histogram_value
= histogram_value
;
119 config
->configs
.push_back(rule
);
125 return config
.Pass();
128 scoped_ptr
<BackgroundTracingReactiveConfig
>
129 BackgroundTracingReactiveConfig_FromDict(const base::DictionaryValue
* dict
) {
132 scoped_ptr
<BackgroundTracingReactiveConfig
> config(
133 new BackgroundTracingReactiveConfig());
135 const base::ListValue
* configs_list
= nullptr;
136 if (!dict
->GetList(kConfigsKey
, &configs_list
))
139 for (auto it
= configs_list
->begin(); it
!= configs_list
->end(); ++it
) {
140 const base::DictionaryValue
* config_dict
= nullptr;
141 if (!(*it
)->GetAsDictionary(&config_dict
))
144 BackgroundTracingReactiveConfig::TracingRule rule
;
146 std::string category_preset_string
;
147 if (!config_dict
->GetString(kConfigCategoryKey
, &category_preset_string
))
150 if (!StringToCategoryPreset(category_preset_string
, &rule
.category_preset
))
154 if (!config_dict
->GetString(kConfigRuleKey
, &type
))
157 if (type
!= kReactiveConfigRuleTraceFor10sOrTriggerOrFull
)
160 rule
.type
= BackgroundTracingReactiveConfig::
161 TRACE_FOR_10S_OR_TRIGGER_OR_FULL
;
163 std::string trigger_name
;
164 if (!config_dict
->GetString(kConfigRuleTriggerNameKey
, &trigger_name
))
167 rule
.trigger_name
= trigger_name
;
169 config
->configs
.push_back(rule
);
172 return config
.Pass();
175 bool BackgroundTracingPreemptiveConfig_IntoDict(
176 const BackgroundTracingPreemptiveConfig
* config
,
177 base::DictionaryValue
* dict
) {
178 dict
->SetString(kConfigCategoryKey
,
179 CategoryPresetToString(config
->category_preset
));
181 scoped_ptr
<base::ListValue
> configs_list(new base::ListValue());
183 for (size_t i
= 0; i
< config
->configs
.size(); ++i
) {
184 scoped_ptr
<base::DictionaryValue
> config_dict(new base::DictionaryValue());
186 if (config
->configs
[i
].type
== BackgroundTracingPreemptiveConfig::
187 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
) {
188 config_dict
->SetString(kConfigRuleKey
, kPreemptiveConfigRuleMonitorNamed
);
189 config_dict
->SetString(
190 kConfigRuleTriggerNameKey
,
191 config
->configs
[i
].named_trigger_info
.trigger_name
.c_str());
192 } else if (config
->configs
[i
].type
==
193 BackgroundTracingPreemptiveConfig::
194 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE
) {
195 config_dict
->SetString(kConfigRuleKey
,
196 kPreemptiveConfigRuleMonitorHistogram
);
197 config_dict
->SetString(
198 kConfigRuleHistogramNameKey
,
199 config
->configs
[i
].histogram_trigger_info
.histogram_name
.c_str());
200 config_dict
->SetInteger(
201 kConfigRuleHistogramValueKey
,
202 config
->configs
[i
].histogram_trigger_info
.histogram_value
);
207 configs_list
->Append(config_dict
.Pass());
210 dict
->Set(kConfigsKey
, configs_list
.Pass());
215 bool BackgroundTracingReactiveConfig_IntoDict(
216 const BackgroundTracingReactiveConfig
* config
,
217 base::DictionaryValue
* dict
) {
218 scoped_ptr
<base::ListValue
> configs_list(new base::ListValue());
220 for (size_t i
= 0; i
< config
->configs
.size(); ++i
) {
221 scoped_ptr
<base::DictionaryValue
> config_dict(new base::DictionaryValue());
223 config_dict
->SetString(
225 CategoryPresetToString(config
->configs
[i
].category_preset
));
227 switch (config
->configs
[i
].type
) {
228 case BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL
:
229 config_dict
->SetString(
231 kReactiveConfigRuleTraceFor10sOrTriggerOrFull
);
238 config_dict
->SetString(kConfigRuleTriggerNameKey
,
239 config
->configs
[i
].trigger_name
.c_str());
241 configs_list
->Append(config_dict
.Pass());
244 dict
->Set(kConfigsKey
, configs_list
.Pass());
251 scoped_ptr
<BackgroundTracingConfig
> BackgroundTracingConfig::FromDict(
252 const base::DictionaryValue
* dict
) {
256 if (!dict
->GetString(kConfigModeKey
, &mode
))
259 scoped_ptr
<BackgroundTracingConfig
> config
;
261 if (mode
== kConfigModePreemptive
) {
262 config
= BackgroundTracingPreemptiveConfig_FromDict(dict
);
263 } else if (mode
== kConfigModeReactive
) {
264 config
= BackgroundTracingReactiveConfig_FromDict(dict
);
269 return config
.Pass();
272 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig
* config
,
273 base::DictionaryValue
* dict
) {
274 switch (config
->mode
) {
275 case PREEMPTIVE_TRACING_MODE
:
276 dict
->SetString(kConfigModeKey
, kConfigModePreemptive
);
277 if (!BackgroundTracingPreemptiveConfig_IntoDict(
278 static_cast<const BackgroundTracingPreemptiveConfig
*>(config
),
282 case REACTIVE_TRACING_MODE
:
283 dict
->SetString(kConfigModeKey
, kConfigModeReactive
);
284 if (!BackgroundTracingReactiveConfig_IntoDict(
285 static_cast<const BackgroundTracingReactiveConfig
*>(config
),
292 } // namspace content