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 (const auto& it
: *configs_list
) {
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 (const auto& it
: *configs_list
) {
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 (const auto& it
: config
->configs
) {
184 scoped_ptr
<base::DictionaryValue
> config_dict(new base::DictionaryValue());
186 if (it
.type
== BackgroundTracingPreemptiveConfig::
187 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED
) {
188 config_dict
->SetString(kConfigRuleKey
, kPreemptiveConfigRuleMonitorNamed
);
189 config_dict
->SetString(kConfigRuleTriggerNameKey
,
190 it
.named_trigger_info
.trigger_name
.c_str());
191 } else if (it
.type
==
192 BackgroundTracingPreemptiveConfig::
193 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE
) {
194 config_dict
->SetString(kConfigRuleKey
,
195 kPreemptiveConfigRuleMonitorHistogram
);
196 config_dict
->SetString(kConfigRuleHistogramNameKey
,
197 it
.histogram_trigger_info
.histogram_name
.c_str());
198 config_dict
->SetInteger(kConfigRuleHistogramValueKey
,
199 it
.histogram_trigger_info
.histogram_value
);
204 configs_list
->Append(config_dict
.Pass());
207 dict
->Set(kConfigsKey
, configs_list
.Pass());
212 bool BackgroundTracingReactiveConfig_IntoDict(
213 const BackgroundTracingReactiveConfig
* config
,
214 base::DictionaryValue
* dict
) {
215 scoped_ptr
<base::ListValue
> configs_list(new base::ListValue());
217 for (const auto& it
: config
->configs
) {
218 scoped_ptr
<base::DictionaryValue
> config_dict(new base::DictionaryValue());
220 config_dict
->SetString(kConfigCategoryKey
,
221 CategoryPresetToString(it
.category_preset
));
224 case BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL
:
225 config_dict
->SetString(
227 kReactiveConfigRuleTraceFor10sOrTriggerOrFull
);
234 config_dict
->SetString(kConfigRuleTriggerNameKey
, it
.trigger_name
.c_str());
235 configs_list
->Append(config_dict
.Pass());
238 dict
->Set(kConfigsKey
, configs_list
.Pass());
245 scoped_ptr
<BackgroundTracingConfig
> BackgroundTracingConfig::FromDict(
246 const base::DictionaryValue
* dict
) {
250 if (!dict
->GetString(kConfigModeKey
, &mode
))
253 scoped_ptr
<BackgroundTracingConfig
> config
;
255 if (mode
== kConfigModePreemptive
) {
256 config
= BackgroundTracingPreemptiveConfig_FromDict(dict
);
257 } else if (mode
== kConfigModeReactive
) {
258 config
= BackgroundTracingReactiveConfig_FromDict(dict
);
263 return config
.Pass();
266 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig
* config
,
267 base::DictionaryValue
* dict
) {
268 switch (config
->mode
) {
269 case PREEMPTIVE_TRACING_MODE
:
270 dict
->SetString(kConfigModeKey
, kConfigModePreemptive
);
271 if (!BackgroundTracingPreemptiveConfig_IntoDict(
272 static_cast<const BackgroundTracingPreemptiveConfig
*>(config
),
276 case REACTIVE_TRACING_MODE
:
277 dict
->SetString(kConfigModeKey
, kConfigModeReactive
);
278 if (!BackgroundTracingReactiveConfig_IntoDict(
279 static_cast<const BackgroundTracingReactiveConfig
*>(config
),
286 } // namspace content